Changeset 13857


Ignore:
Timestamp:
10/30/12 15:03:19 (12 years ago)
Author:
jschierm
Message:

NEW: Added python model.extract and contourenvelope (plus matlab cosmetic changes).

Location:
issm/trunk-jpl/src/m
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/boundaryconditions/SetMarineIceSheetBC.py

    r13470 r13857  
    3838        pos=numpy.nonzero(numpy.logical_and(md.mesh.vertexonboundary,numpy.logical_not(vertexonicefront)))[0]
    3939        if not numpy.size(pos):
    40                 print("SetMarineIceSheetBC warning: ice front all around the glacier, no dirichlet found. Dirichlet must be added manually.")
     40                print "SetMarineIceSheetBC warning: ice front all around the glacier, no dirichlet found. Dirichlet must be added manually."
    4141
    4242        md.diagnostic.spcvx=float('nan')*numpy.ones((md.mesh.numberofvertices,1))
     
    5050        #Dirichlet Values
    5151        if isinstance(md.inversion.vx_obs,numpy.ndarray) and numpy.size(md.inversion.vx_obs,axis=0)==md.mesh.numberofvertices and isinstance(md.inversion.vy_obs,numpy.ndarray) and numpy.size(md.inversion.vy_obs,axis=0)==md.mesh.numberofvertices:
    52                 print("      boundary conditions for diagnostic model: spc set as observed velocities")
     52                print "      boundary conditions for diagnostic model: spc set as observed velocities"
    5353                md.diagnostic.spcvx[pos]=md.inversion.vx_obs[pos]
    5454                md.diagnostic.spcvy[pos]=md.inversion.vy_obs[pos]
    5555        else:
    56                 print("      boundary conditions for diagnostic model: spc set as zero")
     56                print "      boundary conditions for diagnostic model: spc set as zero"
    5757
    5858        md.hydrology.spcwatercolumn=numpy.zeros((md.mesh.numberofvertices,2))
     
    8383        if numpy.all(numpy.isnan(md.surfaceforcings.precipitation)) and (md.surfaceforcings.ispdd==1):
    8484                md.surfaceforcings.precipitation=numpy.zeros((md.mesh.numberofvertices,1))
    85                 print("      no surfaceforcings.precipitation specified: values set as zero")
     85                print "      no surfaceforcings.precipitation specified: values set as zero"
    8686        if numpy.all(numpy.isnan(md.surfaceforcings.mass_balance)) and (md.surfaceforcings.ispdd==0):
    8787                md.surfaceforcings.mass_balance=numpy.zeros((md.mesh.numberofvertices,1))
    88                 print("      no surfaceforcings.mass_balance specified: values set as zero")
     88                print "      no surfaceforcings.mass_balance specified: values set as zero"
    8989        if numpy.all(numpy.isnan(md.basalforcings.melting_rate)):
    9090                md.basalforcings.melting_rate=numpy.zeros((md.mesh.numberofvertices,1))
    91                 print("      no basalforcings.melting_rate specified: values set as zero")
     91                print "      no basalforcings.melting_rate specified: values set as zero"
    9292        if numpy.all(numpy.isnan(md.balancethickness.thickening_rate)):
    9393                md.balancethickness.thickening_rate=numpy.zeros((md.mesh.numberofvertices,1))
    94                 print("      no balancethickness.thickening_rate specified: values set as zero")
     94                print "      no balancethickness.thickening_rate specified: values set as zero"
    9595
    9696        md.prognostic.spcthickness=float('nan')*numpy.ones((md.mesh.numberofvertices,1))
     
    104104                if not isinstance(md.basalforcings.geothermalflux,numpy.ndarray) or not numpy.size(md.basalforcings.geothermalflux,axis=0)==md.mesh.numberofvertices:
    105105                        md.basalforcings.geothermalflux=numpy.zeros((md.mesh.numberofvertices,1))
    106                         md.basalforcings.geothermalflux[numpy.nonzero(md.mask.vertexongroundedice)]=50.*10.**-3; #50mW/m2
     106                        md.basalforcings.geothermalflux[numpy.nonzero(md.mask.vertexongroundedice)]=50.*10.**-3    #50mW/m2
    107107        else:
    108                 print("      no thermal boundary conditions created: no observed temperature found");
     108                print "      no thermal boundary conditions created: no observed temperature found"
    109109
    110110        return md
  • issm/trunk-jpl/src/m/classes/model/model.m

    r13719 r13857  
    252252                        %   an empty string '' will be considered as an empty domain
    253253                        %   a string 'all' will be considered as the entire domain
    254                         %   add an argument 0 if you do not want the elements to be checked (faster)
    255254                        %
    256255                        %   Usage:
     
    272271                        end
    273272
    274                         %get check option
    275                         if (nargin==3 & varargin{1}==0),
    276                                 checkoutline=0;
    277                         else
    278                                 checkoutline=1;
    279                         end
    280 
    281273                        %get elements that are inside area
    282274                        flag_elem=FlagElements(md1,area);
     
    311303                        Pnode(pos_node)=[1:numberofvertices2]';
    312304
    313                         %renumber the elements (some node won't exist anymore)
     305                        %renumber the elements (some nodes won't exist anymore)
    314306                        elements_1=md1.mesh.elements;
    315307                        elements_2=elements_1(pos_elem,:);
     
    323315                        end
    324316
    325                         %OK, now create the new model !
    326 
    327                         %take every fields from model
     317                        %OK, now create the new model!
     318
     319                        %take every field from model
    328320                        md2=md1;
    329321
     
    347339                                                elseif (fieldsize(1)==numberofvertices1+1)
    348340                                                        md2.(model_fields{i}).(object_fields{j})=[field(pos_node,:); field(end,:)];
    349                                                         %size = number of elements * n
     341                                                %size = number of elements * n
    350342                                                elseif fieldsize(1)==numberofelements1
    351343                                                        md2.(model_fields{i}).(object_fields{j})=field(pos_elem,:);
     
    358350                                        elseif (fieldsize(1)==numberofvertices1+1)
    359351                                                md2.(model_fields{i})=[field(pos_node,:); field(end,:)];
    360                                                 %size = number of elements * n
     352                                        %size = number of elements * n
    361353                                        elseif fieldsize(1)==numberofelements1
    362354                                                md2.(model_fields{i})=field(pos_elem,:);
     
    413405                                %renumber first two columns
    414406                                pos=find(md2.mesh.edges(:,4)~=-1);
    415                                 md2.mesh.edges(:  ,1)=Pnode(md2.mesh.edges(:,1)); 
    416                                 md2.mesh.edges(:  ,2)=Pnode(md2.mesh.edges(:,2)); 
     407                                md2.mesh.edges(:  ,1)=Pnode(md2.mesh.edges(:,1));
     408                                md2.mesh.edges(:  ,2)=Pnode(md2.mesh.edges(:,2));
    417409                                md2.mesh.edges(:  ,3)=Pelem(md2.mesh.edges(:,3));
    418410                                md2.mesh.edges(pos,4)=Pelem(md2.mesh.edges(pos,4));
    419411                                %remove edges when the 2 vertices are not in the domain.
    420412                                md2.mesh.edges=md2.mesh.edges(find(md2.mesh.edges(:,1) & md2.mesh.edges(:,2)),:);
    421                                 %Replace all zeros by -1 in the last two columns;
     413                                %Replace all zeros by -1 in the last two columns
    422414                                pos=find(md2.mesh.edges(:,3)==0);
    423415                                md2.mesh.edges(pos,3)=-1;
  • issm/trunk-jpl/src/m/classes/model/model.py

    r13692 r13857  
    11#module imports {{{
    22import numpy
     3import copy
    34from mesh import mesh
    45from mask import mask
     
    3940from iluasmoptions import *
    4041from project3d import *
     42from FlagElements import *
     43from NodeConnectivity import *
     44from ElementConnectivity import *
     45from contourenvelope import *
    4146#}}}
    4247
     
    170175        # }}}
    171176
    172         """
    173         function md2 = extract(md,area) % {{{
    174                 %extract - extract a model according to an Argus contour or flag list
    175                 %
    176                 %   This routine extracts a submodel from a bigger model with respect to a given contour
    177                 %   md must be followed by the corresponding exp file or flags list
    178                 %   It can either be a domain file (argus type, .exp extension), or an array of element flags.
    179                 %   If user wants every element outside the domain to be
    180                 %   extract2d, add '~' to the name of the domain file (ex: '~Pattyn.exp');
    181                 %   an empty string '' will be considered as an empty domain
    182                 %   a string 'all' will be considered as the entire domain
    183                 %   add an argument 0 if you do not want the elements to be checked (faster)
    184                 %
    185                 %   Usage:
    186                 %      md2=extract(md,area);
    187                 %
    188                 %   Examples:
    189                 %      md2=extract(md,'Domain.exp');
    190                 %      md2=extract(md,md.mask.elementonfloatingice);
    191                 %
    192                 %   See also: EXTRUDE, COLLAPSE
    193 
    194                 %copy model
    195                 md1=md;
    196 
    197                 %some checks
    198                 if ((nargin~=2) | (nargout~=1)),
    199                         help extract
    200                         error('extract error message: bad usage');
    201                 end
    202 
    203                 %get check option
    204                 if (nargin==3 & varargin{1}==0),
    205                         checkoutline=0;
    206                 else
    207                         checkoutline=1;
    208                 end
    209 
    210                 %get elements that are inside area
    211                 flag_elem=FlagElements(md1,area);
    212                 if ~any(flag_elem),
    213                         error('extracted model is empty');
    214                 end
    215 
    216                 %kick out all elements with 3 dirichlets
    217                 spc_elem=find(~flag_elem);
    218                 spc_node=sort(unique(md1.mesh.elements(spc_elem,:)));
    219                 flag=ones(md1.mesh.numberofvertices,1);
    220                 flag(spc_node)=0;
    221                 pos=find(sum(flag(md1.mesh.elements),2)==0);
    222                 flag_elem(pos)=0;
    223 
    224                 %extracted elements and nodes lists
    225                 pos_elem=find(flag_elem);
    226                 pos_node=sort(unique(md1.mesh.elements(pos_elem,:)));
    227 
    228                 %keep track of some fields
    229                 numberofvertices1=md1.mesh.numberofvertices;
    230                 numberofelements1=md1.mesh.numberofelements;
    231                 numberofvertices2=length(pos_node);
    232                 numberofelements2=length(pos_elem);
    233                 flag_node=zeros(numberofvertices1,1);
    234                 flag_node(pos_node)=1;
    235 
    236                 %Create Pelem and Pnode (transform old nodes in new nodes and same thing for the elements)
    237                 Pelem=zeros(numberofelements1,1);
    238                 Pelem(pos_elem)=[1:numberofelements2]';
    239                 Pnode=zeros(numberofvertices1,1);
    240                 Pnode(pos_node)=[1:numberofvertices2]';
    241 
    242                 %renumber the elements (some node won't exist anymore)
    243                 elements_1=md1.mesh.elements;
    244                 elements_2=elements_1(pos_elem,:);
    245                 elements_2(:,1)=Pnode(elements_2(:,1));
    246                 elements_2(:,2)=Pnode(elements_2(:,2));
    247                 elements_2(:,3)=Pnode(elements_2(:,3));
    248                 if md1.mesh.dimension==3,
    249                         elements_2(:,4)=Pnode(elements_2(:,4));
    250                         elements_2(:,5)=Pnode(elements_2(:,5));
    251                         elements_2(:,6)=Pnode(elements_2(:,6));
    252                 end
    253 
    254                 %OK, now create the new model !
    255 
    256                 %take every fields from model
    257                 md2=md1;
    258 
    259                 %automatically modify fields
    260 
    261                 %loop over model fields
    262                 model_fields=fields(md1);
    263                 for i=1:length(model_fields),
    264                         %get field
    265                         field=md1.(model_fields{i});
    266                         fieldsize=size(field);
    267                         if isobject(field), %recursive call
    268                                 object_fields=fields(md1.(model_fields{i}));
    269                                 for j=1:length(object_fields),
    270                                         %get field
    271                                         field=md1.(model_fields{i}).(object_fields{j});
    272                                         fieldsize=size(field);
    273                                         %size = number of nodes * n
    274                                         if fieldsize(1)==numberofvertices1
    275                                                 md2.(model_fields{i}).(object_fields{j})=field(pos_node,:);
    276                                         elseif (fieldsize(1)==numberofvertices1+1)
    277                                                 md2.(model_fields{i}).(object_fields{j})=[field(pos_node,:); field(end,:)];
    278                                                 %size = number of elements * n
    279                                         elseif fieldsize(1)==numberofelements1
    280                                                 md2.(model_fields{i}).(object_fields{j})=field(pos_elem,:);
    281                                         end
    282                                 end
    283                         else
    284                                 %size = number of nodes * n
    285                                 if fieldsize(1)==numberofvertices1
    286                                         md2.(model_fields{i})=field(pos_node,:);
    287                                 elseif (fieldsize(1)==numberofvertices1+1)
    288                                         md2.(model_fields{i})=[field(pos_node,:); field(end,:)];
    289                                         %size = number of elements * n
    290                                 elseif fieldsize(1)==numberofelements1
    291                                         md2.(model_fields{i})=field(pos_elem,:);
    292                                 end
    293                         end
    294                 end
    295 
    296                 %modify some specific fields
    297 
    298                 %Mesh
    299                 md2.mesh.numberofelements=numberofelements2;
    300                 md2.mesh.numberofvertices=numberofvertices2;
    301                 md2.mesh.elements=elements_2;
    302 
    303                 %mesh.uppervertex mesh.lowervertex
    304                 if md1.mesh.dimension==3
    305                         md2.mesh.uppervertex=md1.mesh.uppervertex(pos_node);
    306                         pos=find(~isnan(md2.mesh.uppervertex));
    307                         md2.mesh.uppervertex(pos)=Pnode(md2.mesh.uppervertex(pos));
    308 
    309                         md2.mesh.lowervertex=md1.mesh.lowervertex(pos_node);
    310                         pos=find(~isnan(md2.mesh.lowervertex));
    311                         md2.mesh.lowervertex(pos)=Pnode(md2.mesh.lowervertex(pos));
    312 
    313                         md2.mesh.upperelements=md1.mesh.upperelements(pos_elem);
    314                         pos=find(~isnan(md2.mesh.upperelements));
    315                         md2.mesh.upperelements(pos)=Pelem(md2.mesh.upperelements(pos));
    316 
    317                         md2.mesh.lowerelements=md1.mesh.lowerelements(pos_elem);
    318                         pos=find(~isnan(md2.mesh.lowerelements));
    319                         md2.mesh.lowerelements(pos)=Pelem(md2.mesh.lowerelements(pos));
    320                 end
    321 
    322                 %Initial 2d mesh
    323                 if md1.mesh.dimension==3
    324                         flag_elem_2d=flag_elem(1:md1.mesh.numberofelements2d);
    325                         pos_elem_2d=find(flag_elem_2d);
    326                         flag_node_2d=flag_node(1:md1.mesh.numberofvertices2d);
    327                         pos_node_2d=find(flag_node_2d);
    328 
    329                         md2.mesh.numberofelements2d=length(pos_elem_2d);
    330                         md2.mesh.numberofvertices2d=length(pos_node_2d);
    331                         md2.mesh.elements2d=md1.mesh.elements2d(pos_elem_2d,:);
    332                         md2.mesh.elements2d(:,1)=Pnode(md2.mesh.elements2d(:,1));
    333                         md2.mesh.elements2d(:,2)=Pnode(md2.mesh.elements2d(:,2));
    334                         md2.mesh.elements2d(:,3)=Pnode(md2.mesh.elements2d(:,3));
    335 
    336                         md2.mesh.x2d=md1.mesh.x(pos_node_2d);
    337                         md2.mesh.y2d=md1.mesh.y(pos_node_2d);
    338                 end
    339 
    340                 %Edges
    341                 if size(md2.mesh.edges,2)>1, %do not use ~isnan because there are some NaNs...
    342                         %renumber first two columns
    343                         pos=find(md2.mesh.edges(:,4)~=-1);
    344                         md2.mesh.edges(:  ,1)=Pnode(md2.mesh.edges(:,1));
    345                         md2.mesh.edges(:  ,2)=Pnode(md2.mesh.edges(:,2));
    346                         md2.mesh.edges(:  ,3)=Pelem(md2.mesh.edges(:,3));
    347                         md2.mesh.edges(pos,4)=Pelem(md2.mesh.edges(pos,4));
    348                         %remove edges when the 2 vertices are not in the domain.
    349                         md2.mesh.edges=md2.mesh.edges(find(md2.mesh.edges(:,1) & md2.mesh.edges(:,2)),:);
    350                         %Replace all zeros by -1 in the last two columns;
    351                         pos=find(md2.mesh.edges(:,3)==0);
    352                         md2.mesh.edges(pos,3)=-1;
    353                         pos=find(md2.mesh.edges(:,4)==0);
    354                         md2.mesh.edges(pos,4)=-1;
    355                         %Invert -1 on the third column with last column (Also invert first two columns!!)
    356                         pos=find(md2.mesh.edges(:,3)==-1);
    357                         md2.mesh.edges(pos,3)=md2.mesh.edges(pos,4);
    358                         md2.mesh.edges(pos,4)=-1;
    359                         values=md2.mesh.edges(pos,2);
    360                         md2.mesh.edges(pos,2)=md2.mesh.edges(pos,1);
    361                         md2.mesh.edges(pos,1)=values;
    362                         %Finally remove edges that do not belong to any element
    363                         pos=find(md2.mesh.edges(:,3)==-1 & md2.mesh.edges(:,4)==-1);
    364                         md2.mesh.edges(pos,:)=[];
    365                 end
    366 
    367                 %Penalties
    368                 if ~isnan(md2.diagnostic.vertex_pairing),
    369                         for i=1:size(md1.diagnostic.vertex_pairing,1);
    370                                 md2.diagnostic.vertex_pairing(i,:)=Pnode(md1.diagnostic.vertex_pairing(i,:));
    371                         end
    372                         md2.diagnostic.vertex_pairing=md2.diagnostic.vertex_pairing(find(md2.diagnostic.vertex_pairing(:,1)),:);
    373                 end
    374                 if ~isnan(md2.prognostic.vertex_pairing),
    375                         for i=1:size(md1.prognostic.vertex_pairing,1);
    376                                 md2.prognostic.vertex_pairing(i,:)=Pnode(md1.prognostic.vertex_pairing(i,:));
    377                         end
    378                         md2.prognostic.vertex_pairing=md2.prognostic.vertex_pairing(find(md2.prognostic.vertex_pairing(:,1)),:);
    379                 end
    380 
    381                 %recreate segments
    382                 if md1.mesh.dimension==2
    383                         md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices);
    384                         md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity);
    385                         md2.mesh.segments=contourenvelope(md2);
    386                         md2.mesh.vertexonboundary=zeros(numberofvertices2,1); md2.mesh.vertexonboundary(md2.mesh.segments(:,1:2))=1;
    387                 else
    388                         %First do the connectivity for the contourenvelope in 2d
    389                         md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements2d,md2.mesh.numberofvertices2d);
    390                         md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements2d,md2.mesh.vertexconnectivity);
    391                         md2.mesh.segments=contourenvelope(md2);
    392                         md2.mesh.vertexonboundary=zeros(numberofvertices2/md2.mesh.numberoflayers,1); md2.mesh.vertexonboundary(md2.mesh.segments(:,1:2))=1;
    393                         md2.mesh.vertexonboundary=repmat(md2.mesh.vertexonboundary,md2.mesh.numberoflayers,1);
    394                         %Then do it for 3d as usual
    395                         md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices);
    396                         md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity);
    397                 end
    398 
    399                 %Boundary conditions: Dirichlets on new boundary
    400                 %Catch the elements that have not been extracted
    401                 orphans_elem=find(~flag_elem);
    402                 orphans_node=unique(md1.mesh.elements(orphans_elem,:))';
    403                 %Figure out which node are on the boundary between md2 and md1
    404                 nodestoflag1=intersect(orphans_node,pos_node);
    405                 nodestoflag2=Pnode(nodestoflag1);
    406                 if numel(md1.diagnostic.spcvx)>1 & numel(md1.diagnostic.spcvy)>2 & numel(md1.diagnostic.spcvz)>2,
    407                         if numel(md1.inversion.vx_obs)>1 & numel(md1.inversion.vy_obs)>1
    408                                 md2.diagnostic.spcvx(nodestoflag2)=md2.inversion.vx_obs(nodestoflag2);
    409                                 md2.diagnostic.spcvy(nodestoflag2)=md2.inversion.vy_obs(nodestoflag2);
    410                         else
    411                                 md2.diagnostic.spcvx(nodestoflag2)=NaN;
    412                                 md2.diagnostic.spcvy(nodestoflag2)=NaN;
    413                                 disp(' ')
    414                                 disp('!! extract warning: spc values should be checked !!')
    415                                 disp(' ')
    416                         end
    417                         %put 0 for vz
    418                         md2.diagnostic.spcvz(nodestoflag2)=0;
    419                 end
    420                 if ~isnan(md1.thermal.spctemperature),
    421                         md2.thermal.spctemperature(nodestoflag2,1)=1;
    422                 end
    423 
    424                 %Diagnostic
    425                 if ~isnan(md2.diagnostic.icefront)
    426                         md2.diagnostic.icefront(:,1)=Pnode(md1.diagnostic.icefront(:,1));
    427                         md2.diagnostic.icefront(:,2)=Pnode(md1.diagnostic.icefront(:,2));
    428                         md2.diagnostic.icefront(:,end-1)=Pelem(md1.diagnostic.icefront(:,end-1));
    429                         if md1.mesh.dimension==3
    430                                 md2.diagnostic.icefront(:,3)=Pnode(md1.diagnostic.icefront(:,3));
    431                                 md2.diagnostic.icefront(:,4)=Pnode(md1.diagnostic.icefront(:,4));
    432                         end
    433                         md2.diagnostic.icefront=md2.diagnostic.icefront(find(md2.diagnostic.icefront(:,1) & md2.diagnostic.icefront(:,2) & md2.diagnostic.icefront(:,end)),:);
    434                 end
    435 
    436                 %Results fields
    437                 if isstruct(md1.results),
    438                         md2.results=struct();
    439                         solutionfields=fields(md1.results);
    440                         for i=1:length(solutionfields),
    441                                 %get subfields
    442                                 solutionsubfields=fields(md1.results.(solutionfields{i}));
    443                                 for j=1:length(solutionsubfields),
    444                                         field=md1.results.(solutionfields{i}).(solutionsubfields{j});
    445                                         if length(field)==numberofvertices1,
    446                                                 md2.results.(solutionfields{i}).(solutionsubfields{j})=field(pos_node);
    447                                         elseif length(field)==numberofelements1,
    448                                                 md2.results.(solutionfields{i}).(solutionsubfields{j})=field(pos_elem);
    449                                         else
    450                                                 md2.results.(solutionfields{i}).(solutionsubfields{j})=field;
    451                                         end
    452                                 end
    453                         end
    454                 end
    455 
    456                 %Keep track of pos_node and pos_elem
    457                 md2.mesh.extractedvertices=pos_node;
    458                 md2.mesh.extractedelements=pos_elem;
    459         end % }}}
    460         """
     177        def extract(md,area):    # {{{
     178                """
     179                extract - extract a model according to an Argus contour or flag list
     180
     181                   This routine extracts a submodel from a bigger model with respect to a given contour
     182                   md must be followed by the corresponding exp file or flags list
     183                   It can either be a domain file (argus type, .exp extension), or an array of element flags.
     184                   If user wants every element outside the domain to be
     185                   extract2d, add '~' to the name of the domain file (ex: '~Pattyn.exp');
     186                   an empty string '' will be considered as an empty domain
     187                   a string 'all' will be considered as the entire domain
     188
     189                   Usage:
     190                      md2=extract(md,area);
     191
     192                   Examples:
     193                      md2=extract(md,'Domain.exp');
     194                      md2=extract(md,md.mask.elementonfloatingice);
     195
     196                   See also: EXTRUDE, COLLAPSE
     197                """
     198
     199                #copy model
     200                md1=copy.deepcopy(md)
     201
     202                #get elements that are inside area
     203                flag_elem=FlagElements(md1,area)
     204                if not numpy.any(flag_elem):
     205                        raise RuntimeError("extracted model is empty")
     206
     207                #kick out all elements with 3 dirichlets
     208                spc_elem=numpy.nonzero(numpy.logical_not(flag_elem))[0]
     209                spc_node=numpy.unique(md1.mesh.elements[spc_elem,:]).astype(int)-1
     210                flag=numpy.ones(md1.mesh.numberofvertices)
     211                flag[spc_node]=0
     212                pos=numpy.nonzero(numpy.logical_not(numpy.sum(flag[md1.mesh.elements.astype(int)-1],axis=1)))[0]
     213                flag_elem[pos]=0
     214
     215                #extracted elements and nodes lists
     216                pos_elem=numpy.nonzero(flag_elem)[0]
     217                pos_node=numpy.unique(md1.mesh.elements[pos_elem,:]).astype(int)-1
     218
     219                #keep track of some fields
     220                numberofvertices1=md1.mesh.numberofvertices
     221                numberofelements1=md1.mesh.numberofelements
     222                numberofvertices2=numpy.size(pos_node)
     223                numberofelements2=numpy.size(pos_elem)
     224                flag_node=numpy.zeros(numberofvertices1)
     225                flag_node[pos_node]=1
     226
     227                #Create Pelem and Pnode (transform old nodes in new nodes and same thing for the elements)
     228                Pelem=numpy.zeros(numberofelements1)
     229                Pelem[pos_elem]=numpy.arange(1,numberofelements2+1)
     230                Pnode=numpy.zeros(numberofvertices1)
     231                Pnode[pos_node]=numpy.arange(1,numberofvertices2+1)
     232
     233                #renumber the elements (some node won't exist anymore)
     234                elements_1=copy.deepcopy(md1.mesh.elements)
     235                elements_2=elements_1[pos_elem,:]
     236                elements_2[:,0]=Pnode[elements_2[:,0].astype(int)-1]
     237                elements_2[:,1]=Pnode[elements_2[:,1].astype(int)-1]
     238                elements_2[:,2]=Pnode[elements_2[:,2].astype(int)-1]
     239                if md1.mesh.dimension==3:
     240                        elements_2[:,3]=Pnode[elements_2[:,3].astype(int)-1]
     241                        elements_2[:,4]=Pnode[elements_2[:,4].astype(int)-1]
     242                        elements_2[:,5]=Pnode[elements_2[:,5].astype(int)-1]
     243
     244                #OK, now create the new model!
     245
     246                #take every field from model
     247                md2=copy.deepcopy(md1)
     248
     249                #automatically modify fields
     250
     251                #loop over model fields
     252                model_fields=vars(md1)
     253                for fieldi in model_fields:
     254                        #get field
     255                        field=getattr(md1,fieldi)
     256                        fieldsize=numpy.shape(field)
     257                        if hasattr(field,'__dict__') and not ismember(fieldi,['results'])[0]:    #recursive call
     258                                object_fields=vars(field)
     259                                for fieldj in object_fields:
     260                                        #get field
     261                                        field=getattr(getattr(md1,fieldi),fieldj)
     262                                        fieldsize=numpy.shape(field)
     263                                        if len(fieldsize):
     264                                                #size = number of nodes * n
     265                                                if   fieldsize[0]==numberofvertices1:
     266                                                        setattr(getattr(md2,fieldi),fieldj,field[pos_node,:])
     267                                                elif fieldsize[0]==numberofvertices1+1:
     268                                                        setattr(getattr(md2,fieldi),fieldj,numpy.vstack((field[pos_node,:],field[-1,:])))
     269                                                #size = number of elements * n
     270                                                elif fieldsize[0]==numberofelements1:
     271                                                        setattr(getattr(md2,fieldi),fieldj,field[pos_elem,:])
     272                        else:
     273                                if len(fieldsize):
     274                                        #size = number of nodes * n
     275                                        if   fieldsize[0]==numberofvertices1:
     276                                                setattr(md2,fieldi,field[pos_node,:])
     277                                        elif fieldsize[0]==numberofvertices1+1:
     278                                                setattr(md2,fieldi,numpy.hstack((field[pos_node,:],field[-1,:])))
     279                                        #size = number of elements * n
     280                                        elif fieldsize[0]==numberofelements1:
     281                                                setattr(md2,fieldi,field[pos_elem,:])
     282
     283                #modify some specific fields
     284
     285                #Mesh
     286                md2.mesh.numberofelements=numberofelements2
     287                md2.mesh.numberofvertices=numberofvertices2
     288                md2.mesh.elements=elements_2
     289
     290                #mesh.uppervertex mesh.lowervertex
     291                if md1.mesh.dimension==3:
     292                        md2.mesh.uppervertex=md1.mesh.uppervertex[pos_node]
     293                        pos=numpy.nonzero(numpy.logical_not(numpy.isnan(md2.mesh.uppervertex)))[0]
     294                        md2.mesh.uppervertex[pos]=Pnode[md2.mesh.uppervertex[pos].astype(int)-1]
     295
     296                        md2.mesh.lowervertex=md1.mesh.lowervertex[pos_node]
     297                        pos=numpy.nonzero(numpy.logical_not(numpy.isnan(md2.mesh.lowervertex)))[0]
     298                        md2.mesh.lowervertex[pos]=Pnode[md2.mesh.lowervertex[pos].astype(int)-1]
     299
     300                        md2.mesh.upperelements=md1.mesh.upperelements[pos_elem]
     301                        pos=numpy.nonzero(numpy.logical_not(numpy.isnan(md2.mesh.upperelements)))[0]
     302                        md2.mesh.upperelements[pos]=Pelem[md2.mesh.upperelements[pos].astype(int)-1]
     303
     304                        md2.mesh.lowerelements=md1.mesh.lowerelements[pos_elem]
     305                        pos=numpy.nonzero(numpy.logical_not(numpy.isnan(md2.mesh.lowerelements)))[0]
     306                        md2.mesh.lowerelements[pos]=Pelem[md2.mesh.lowerelements[pos].astype(int)-1]
     307
     308                #Initial 2d mesh
     309                if md1.mesh.dimension==3:
     310                        flag_elem_2d=flag_elem[numpy.arange(0,md1.mesh.numberofelements2d)]
     311                        pos_elem_2d=numpy.nonzero(flag_elem_2d)[0]
     312                        flag_node_2d=flag_node[numpy.arange(0,md1.mesh.numberofvertices2d)]
     313                        pos_node_2d=numpy.nonzero(flag_node_2d)[0]
     314
     315                        md2.mesh.numberofelements2d=numpy.size(pos_elem_2d)
     316                        md2.mesh.numberofvertices2d=numpy.size(pos_node_2d)
     317                        md2.mesh.elements2d=md1.mesh.elements2d[pos_elem_2d,:]
     318                        md2.mesh.elements2d[:,0]=Pnode[md2.mesh.elements2d[:,0].astype(int)-1]
     319                        md2.mesh.elements2d[:,1]=Pnode[md2.mesh.elements2d[:,1].astype(int)-1]
     320                        md2.mesh.elements2d[:,2]=Pnode[md2.mesh.elements2d[:,2].astype(int)-1]
     321
     322                        md2.mesh.x2d=md1.mesh.x[pos_node_2d]
     323                        md2.mesh.y2d=md1.mesh.y[pos_node_2d]
     324
     325                #Edges
     326                if len(numpy.shape(md2.mesh.edges))>1 and numpy.size(md2.mesh.edges,axis=1)>1:    #do not use ~isnan because there are some NaNs...
     327                        #renumber first two columns
     328                        pos=numpy.nonzero(md2.mesh.edges[:,3]!=-1)[0]
     329                        md2.mesh.edges[:  ,0]=Pnode[md2.mesh.edges[:,0].astype(int)-1]
     330                        md2.mesh.edges[:  ,1]=Pnode[md2.mesh.edges[:,1].astype(int)-1]
     331                        md2.mesh.edges[:  ,2]=Pelem[md2.mesh.edges[:,2].astype(int)-1]
     332                        md2.mesh.edges[pos,3]=Pelem[md2.mesh.edges[pos,3].astype(int)-1]
     333                        #remove edges when the 2 vertices are not in the domain.
     334                        md2.mesh.edges=md2.mesh.edges[numpy.nonzero(numpy.logical_and(md2.mesh.edges[:,0],md2.mesh.edges[:,1]))[0],:]
     335                        #Replace all zeros by -1 in the last two columns
     336                        pos=numpy.nonzero(md2.mesh.edges[:,2]==0)[0]
     337                        md2.mesh.edges[pos,2]=-1
     338                        pos=numpy.nonzero(md2.mesh.edges[:,3]==0)[0]
     339                        md2.mesh.edges[pos,3]=-1
     340                        #Invert -1 on the third column with last column (Also invert first two columns!!)
     341                        pos=numpy.nonzero(md2.mesh.edges[:,2]==-1)[0]
     342                        md2.mesh.edges[pos,2]=md2.mesh.edges[pos,3]
     343                        md2.mesh.edges[pos,3]=-1
     344                        values=md2.mesh.edges[pos,1]
     345                        md2.mesh.edges[pos,1]=md2.mesh.edges[pos,0]
     346                        md2.mesh.edges[pos,0]=values
     347                        #Finally remove edges that do not belong to any element
     348                        pos=numpy.nonzero(numpy.logical_and(md2.mesh.edges[:,1]==-1,md2.mesh.edges[:,2]==-1))[0]
     349                        md2.mesh.edges=numpy.delete(md2.mesh.edges,pos,axis=0)
     350
     351                #Penalties
     352                if numpy.any(numpy.logical_not(numpy.isnan(md2.diagnostic.vertex_pairing))):
     353                        for i in xrange(numpy.size(md1.diagnostic.vertex_pairing,axis=0)):
     354                                md2.diagnostic.vertex_pairing[i,:]=Pnode[md1.diagnostic.vertex_pairing[i,:]]
     355                        md2.diagnostic.vertex_pairing=md2.diagnostic.vertex_pairing[numpy.nonzero(md2.diagnostic.vertex_pairing[:,0])[0],:]
     356                if numpy.any(numpy.logical_not(numpy.isnan(md2.prognostic.vertex_pairing))):
     357                        for i in xrange(numpy.size(md1.prognostic.vertex_pairing,axis=0)):
     358                                md2.prognostic.vertex_pairing[i,:]=Pnode[md1.prognostic.vertex_pairing[i,:]]
     359                        md2.prognostic.vertex_pairing=md2.prognostic.vertex_pairing[numpy.nonzero(md2.prognostic.vertex_pairing[:,0])[0],:]
     360
     361                #recreate segments
     362                if md1.mesh.dimension==2:
     363                        [md2.mesh.vertexconnectivity]=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices)
     364                        [md2.mesh.elementconnectivity]=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity)
     365                        md2.mesh.segments=contourenvelope(md2)
     366                        md2.mesh.vertexonboundary=numpy.zeros(numberofvertices2)
     367                        md2.mesh.vertexonboundary[md2.mesh.segments[:,0:2].astype(int)-1]=1
     368                else:
     369                        #First do the connectivity for the contourenvelope in 2d
     370                        [md2.mesh.vertexconnectivity]=NodeConnectivity(md2.mesh.elements2d,md2.mesh.numberofvertices2d)
     371                        [md2.mesh.elementconnectivity]=ElementConnectivity(md2.mesh.elements2d,md2.mesh.vertexconnectivity)
     372                        md2.mesh.segments=contourenvelope(md2)
     373                        md2.mesh.vertexonboundary=numpy.zeros(numberofvertices2/md2.mesh.numberoflayers)
     374                        md2.mesh.vertexonboundary[md2.mesh.segments[:,0:2].astype(int)-1]=1
     375                        md2.mesh.vertexonboundary=numpy.tile(md2.mesh.vertexonboundary,md2.mesh.numberoflayers)
     376                        #Then do it for 3d as usual
     377                        [md2.mesh.vertexconnectivity]=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices)
     378                        [md2.mesh.elementconnectivity]=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity)
     379
     380                #Boundary conditions: Dirichlets on new boundary
     381                #Catch the elements that have not been extracted
     382                orphans_elem=numpy.nonzero(numpy.logical_not(flag_elem))[0]
     383                orphans_node=numpy.unique(md1.mesh.elements[orphans_elem,:]).astype(int)-1
     384                #Figure out which node are on the boundary between md2 and md1
     385                nodestoflag1=numpy.intersect1d(orphans_node,pos_node)
     386                nodestoflag2=Pnode[nodestoflag1].astype(int)-1
     387                if numpy.size(md1.diagnostic.spcvx)>1 and numpy.size(md1.diagnostic.spcvy)>2 and numpy.size(md1.diagnostic.spcvz)>2:
     388                        if numpy.size(md1.inversion.vx_obs)>1 and numpy.size(md1.inversion.vy_obs)>1:
     389                                md2.diagnostic.spcvx[nodestoflag2]=md2.inversion.vx_obs[nodestoflag2]
     390                                md2.diagnostic.spcvy[nodestoflag2]=md2.inversion.vy_obs[nodestoflag2]
     391                        else:
     392                                md2.diagnostic.spcvx[nodestoflag2]=float('NaN')
     393                                md2.diagnostic.spcvy[nodestoflag2]=float('NaN')
     394                                print "\n!! extract warning: spc values should be checked !!\n\n"
     395                        #put 0 for vz
     396                        md2.diagnostic.spcvz[nodestoflag2]=0
     397                if numpy.any(numpy.logical_not(numpy.isnan(md1.thermal.spctemperature))):
     398                        md2.thermal.spctemperature[nodestoflag2,0]=1
     399
     400                #Diagnostic
     401                if numpy.any(numpy.logical_not(numpy.isnan(md2.diagnostic.icefront))):
     402                        md2.diagnostic.icefront[:,0]=Pnode[md1.diagnostic.icefront[:,0].astype(int)-1]
     403                        md2.diagnostic.icefront[:,1]=Pnode[md1.diagnostic.icefront[:,1].astype(int)-1]
     404                        md2.diagnostic.icefront[:,-2]=Pelem[md1.diagnostic.icefront[:,-2].astype(int)-1]
     405                        if md1.mesh.dimension==3:
     406                                md2.diagnostic.icefront[:,2]=Pnode[md1.diagnostic.icefront[:,2].astype(int)-1]
     407                                md2.diagnostic.icefront[:,3]=Pnode[md1.diagnostic.icefront[:,3].astype(int)-1]
     408                        md2.diagnostic.icefront=md2.diagnostic.icefront[numpy.nonzero(numpy.logical_and(numpy.logical_and(md2.diagnostic.icefront[:,0],md2.diagnostic.icefront[:,1]),md2.diagnostic.icefront[:,-1]))[0],:]
     409
     410                #Results fields
     411                if md1.results:
     412                        md2.results=OrderedDict()
     413                        for solutionfield in md1.results.iterkeys():
     414                                #get time step
     415                                for i in m1.results[solutionfield].iterkeys():
     416                                        #get subfields
     417                                        for solutionsubfield,field in m1.results[solutionfield][i].iteritems():
     418                                                if   numpy.size(field)==numberofvertices1:
     419                                                        md2.results[solutionfield][i][solutionsubfield]=field[pos_node]
     420                                                elif numpy.size(field)==numberofelements1:
     421                                                        md2.results[solutionfield][i][solutionsubfield]=field[pos_elem]
     422                                                else:
     423                                                        md2.results[solutionfield][i][solutionsubfield]=field
     424
     425                #Keep track of pos_node and pos_elem
     426                md2.mesh.extractedvertices=pos_node.astype(float)+1
     427                md2.mesh.extractedelements=pos_elem.astype(float)+1
     428
     429                return md2
     430        # }}}
    461431
    462432        def extrude(md,*args):    # {{{
  • issm/trunk-jpl/src/m/classes/verbose.py

    r13740 r13857  
    6161                        #Cast to logicals
    6262                        listproperties=vars(self)
    63                         for [fieldname,fieldvalue] in listproperties.iteritems():
     63                        for fieldname,fieldvalue in listproperties.iteritems():
    6464                                if isinstance(fieldvalue,bool) or isinstance(fieldvalue,(int,long,float)):
    6565                                        setattr(self,fieldname,bool(fieldvalue))
  • issm/trunk-jpl/src/m/mesh/bamg.py

    r13496 r13857  
    123123                                elif numpy.any(numpy.logical_not(flags)):
    124124                                        #We LOTS of work to do
    125                                         print("Rift tip outside of or on the domain has been detected and is being processed...")
     125                                        print "Rift tip outside of or on the domain has been detected and is being processed..."
    126126
    127127                                        #check that only one point is outside (for now)
     
    166166
    167167                                                        if numpy.min(tipdis)/segdis < options.getfieldvalue('toltip',0):
    168                                                                 print("moving tip-domain intersection point")
     168                                                                print "moving tip-domain intersection point"
    169169
    170170                                                                #Get position of the closer point
     
    350350        raise RuntimeError("bamg.py/processgeometry is not complete.")
    351351        #Deal with edges
    352         print("Checking Edge crossing...")
     352        print "Checking Edge crossing..."
    353353        i=0
    354354        while (i<numpy.size(geom.Edges,axis=0)):
     
    405405
    406406        #Check point outside
    407         print("Checking for points outside the domain...")
     407        print "Checking for points outside the domain..."
    408408        i=0
    409409        num=0
     
    435435
    436436        if num:
    437                 print("WARNING: %d points outside the domain outline have been removed" % num)
     437                print "WARNING: %d points outside the domain outline have been removed" % num
    438438
    439439        """
  • issm/trunk-jpl/src/m/parameterization/contourenvelope.m

    r13646 r13857  
    2020        if ischar(flags),
    2121                file=flags;
    22                 file=varargin{1};
    2322                if ~exist(file),
    2423                        error(['contourenvelope error message: file ' file ' not found']);
     
    2928                isfile=0;
    3029        else
    31                 error('contourenvelope error message:  second argument should a file or an elements flag');
     30                error('contourenvelope error message:  second argument should be a file or an elements flag');
    3231        end
    3332end
     
    7069        else
    7170                %get flag list of elements and nodes inside the contour
    72                 nodein=zeros(mesh.numberofvertices,1); 
    73                 elemin=zeros(mesh.numberofelements,1); 
     71                nodein=zeros(mesh.numberofvertices,1);
     72                elemin=zeros(mesh.numberofelements,1);
    7473
    75                 pos=find(flags); 
     74                pos=find(flags);
    7675                elemin(pos)=1;
    7776                nodein(mesh.elements(pos,:))=1;
     
    9594pos=find(elementonboundary);
    9695num_segments=length(pos);
    97 segments=zeros(num_segments,3);
     96segments=zeros(num_segments*3,3);
    9897count=1;
    9998
     
    138137        end
    139138end
     139segments=segments(1:count-1,:);
     140
Note: See TracChangeset for help on using the changeset viewer.