Ignore:
Timestamp:
05/10/18 10:24:27 (7 years ago)
Author:
Mathieu Morlighem
Message:

merged trunk-jpl and trunk for revision 22757

Location:
issm/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk

  • issm/trunk/src

  • issm/trunk/src/m/mesh/bamg.py

    r21341 r22758  
    11import os.path
    22import numpy as  np
    3 from mesh2d import mesh2d
     3from mesh2d import *
     4from mesh2dvertical import *
     5from mesh3dsurface import *
    46from collections import OrderedDict
    57from pairoptions import pairoptions
     
    2022
    2123           - domain :            followed by an ARGUS file that prescribes the domain outline
     24           - holes :             followed by an ARGUS file that prescribes the holes
     25     - subdomains :        followed by an ARGUS file that prescribes the list of
     26                                 subdomains (that need to be inside domain)
     27
    2228           - hmin :              minimum edge length (default is 10^-100)
    2329           - hmax :              maximum edge length (default is 10^100)
     
    3743                                 1 -> use Green formula
    3844           - KeepVertices :      try to keep initial vertices when adaptation is done on an existing mesh (default 1)
    39            - MaxCornerAngle :    maximum angle of corners in degree (default is 10)
    4045           - maxnbv :            maximum number of vertices used to allocate memory (default is 10^6)
    4146           - maxsubdiv :         maximum subdivision of exisiting elements (default is 10)
     
    4954           - power :             power applied to the metric (default is 1)
    5055           - splitcorners :      split triangles whuch have 3 vertices on the outline (default is 1)
    51            - geometricalmetric : take the geometry into account to generate the metric (default is 0)
    5256           - verbose :           level of verbosity (default is 1)
    5357
     
    7478        bamg_mesh=bamgmesh()
    7579
     80        subdomain_ref = 1
     81        hole_ref = 1
     82
    7683        # Bamg Geometry parameters {{{
    7784        if options.exist('domain'):
    78 
    7985                #Check that file exists
    8086                domainfile=options.getfieldvalue('domain')
    81                 if not os.path.exists(domainfile):
    82                         raise IOError("bamg error message: file '%s' not found" % domainfile)
    83                 domain=expread(domainfile)
     87                if type(domainfile) == str:
     88                        if not os.path.exists(domainfile):
     89                                raise IOError("bamg error message: file '%s' not found" % domainfile)
     90                        domain=expread(domainfile)
     91                else:
     92                        domain=domainfile
    8493
    8594                #Build geometry
    8695                count=0
    8796                for i,domaini in enumerate(domain):
    88 
    8997                        #Check that the domain is closed
    9098                        if (domaini['x'][0]!=domaini['x'][-1] or domaini['y'][0]!=domaini['y'][-1]):
    9199                                raise RuntimeError("bamg error message: all contours provided in ''domain'' should be closed")
    92100
    93                         #Checks that all holes are INSIDE the principle domain outline
     101                        #Checks that all holes are INSIDE the principle domain outline princial domain should be index 0
    94102                        if i:
    95103                                flags=ContourToNodes(domaini['x'],domaini['y'],domainfile,0)[0]
    96104                                if np.any(np.logical_not(flags)):
    97105                                        raise RuntimeError("bamg error message: All holes should be strictly inside the principal domain")
     106
     107                        #Check orientation
     108                        nods=domaini['nods']-1#the domain are closed 1=end;
     109
     110                        test = np.sum((domaini['x'][1:nods+1] - domaini['x'][0:nods])*(domaini['y'][1:nods+1] + domaini['y'][0:nods]))
     111                        if (i==0 and test>0) or (i>0 and test<0):
     112                                print('At least one contour was not correctly oriented and has been re-oriented')
     113                                domaini['x'] = np.flipud(domaini['x'])
     114                                domaini['y'] = np.flipud(domaini['y'])
     115
    98116
    99117                        #Add all points to bamg_geometry
     
    102120                        bamg_geometry.Edges   =np.vstack((bamg_geometry.Edges,np.vstack((np.arange(count+1,count+nods+1),np.hstack((np.arange(count+2,count+nods+1),count+1)),1.*np.ones((nods)))).T))
    103121                        if i:
    104                                 bamg_geometry.SubDomains=np.vstack((bamg_geometry.SubDomains,[2,count+1,1,1]))
    105 
    106                         # bamg_geometry.Vertices=np.hstack((bamg_geometry.Vertices,np.vstack((domaini['x'][0:nods].reshape(-1),domaini['y'][0:nods].reshape(-1),np.ones((nods))))))
    107                         # bamg_geometry.Edges   =np.vstack((bamg_geometry.Edges,np.hstack((np.arange(count+1,count+nods+1).reshape(-1,),np.hstack((np.arange(count+2,count+nods+1),count+1)).reshape(-1,),1.*np.ones((nods,))))))
    108                         # if i:
    109                         #       bamg_geometry.SubDomains=np.vstack((bamg_geometry.SubDomains,[2,count+1,1,1]))
    110 
     122                                bamg_geometry.SubDomains=np.vstack((bamg_geometry.SubDomains,[2,count+1,1,-subdomain_ref]))
     123                                subdomain_ref = subdomain_ref+1;
     124                        else:
     125                                bamg_geometry.SubDomains=np.vstack((bamg_geometry.SubDomains,[2,count+1,1,0]))
     126                       
    111127                        #update counter
    112128                        count+=nods
     129
     130                #Deal with domain holes
     131                if options.exist('holes'):
     132                        holesfile=options.getfieldvalue('holes')
     133                        if type(holesfile) == str:
     134                                if not os.path.exists(holesfile):
     135                                        raise IOError("bamg error message: file '%s' not found" % holesfile)
     136                                holes=expread(holesfile)
     137                        else:
     138                                holes=holesfile
     139
     140                        #Build geometry
     141                        for i,holei in enumerate(holes):
     142                                #Check that the hole is closed
     143                                if (holei['x'][0]!=holei['x'][-1] or holei['y'][0]!=holei['y'][-1]):
     144                                        raise RuntimeError("bamg error message: all contours provided in ''hole'' should be closed")
     145
     146                                #Checks that all holes are INSIDE the principle domain outline princial domain should be index 0
     147                                flags=ContourToNodes(holei['x'],holei['y'],domainfile,0)[0]
     148                                if np.any(np.logical_not(flags)):
     149                                        raise RuntimeError("bamg error message: All holes should be strictly inside the principal domain")
     150
     151                                #Check orientation
     152                                nods=holei['nods']-1#the hole are closed 1=end;
     153                                test = np.sum((holei['x'][1:nods+1] - holei['x'][0:nods])*(holei['y'][1:nods+1] + holei['y'][0:nods]))
     154                                if test<0:
     155                                        print('At least one hole was not correctly oriented and has been re-oriented')
     156                                        holei['x'] = np.flipud(holei['x'])
     157                                        holei['y'] = np.flipud(holei['y'])
     158
     159                                #Add all points to bamg_geometry
     160                                nods=holei['nods']-1    #the hole are closed 0=end
     161                                bamg_geometry.Vertices=np.vstack((bamg_geometry.Vertices,np.vstack((holei['x'][0:nods],holei['y'][0:nods],np.ones((nods)))).T))
     162                                bamg_geometry.Edges   =np.vstack((bamg_geometry.Edges,np.vstack((np.arange(count+1,count+nods+1),np.hstack((np.arange(count+2,count+nods+1),count+1)),1.*np.ones((nods)))).T))
     163                                #update counter
     164                                count+=nods
     165
     166                #And subdomains
     167                if options.exist('subdomains'):
     168                        subdomainfile=options.getfieldvalue('subdomains')
     169                        if type(subdomainfile) == str:
     170                                if not os.path.exists(subdomainfile):
     171                                        raise IOError("bamg error message: file '%s' not found" % subdomainfile)
     172                                subdomains=expread(subdomainfile)
     173                        else:
     174                                subdomains=subdomainfile
     175
     176                        #Build geometry
     177                        for i,subdomaini in enumerate(subdomains):
     178                                #Check that the subdomain is closed
     179                                if (subdomaini['x'][0]!=subdomaini['x'][-1] or subdomaini['y'][0]!=subdomaini['y'][-1]):
     180                                        raise RuntimeError("bamg error message: all contours provided in ''subdomain'' should be closed")
     181
     182                                #Checks that all subdomains are INSIDE the principle subdomain outline princial domain should be index 0
     183                                if i:
     184                                        flags=ContourToNodes(subdomaini['x'],subdomaini['y'],domainfile,0)[0]
     185                                        if np.any(np.logical_not(flags)):
     186                                                raise RuntimeError("bamg error message: All subdomains should be strictly inside the principal subdomain")
     187
     188                                #Check orientation
     189                                nods=subdomaini['nods']-1#the subdomain are closed 1=end;
     190
     191                                test = np.sum((subdomaini['x'][1:nods+1] - subdomaini['x'][0:nods])*(subdomaini['y'][1:nods+1] + subdomaini['y'][0:nods]))
     192                                if test>0:
     193                                        print('At least one subcontour was not correctly oriented and has been re-oriented')
     194                                        subdomaini['x'] = np.flipud(subdomaini['x'])
     195                                        subdomaini['y'] = np.flipud(subdomaini['y'])
     196
     197                                #Add all points to bamg_geometry
     198                                nods=subdomaini['nods']-1    #the subdomain are closed 0=end
     199                                bamg_geometry.Vertices=np.vstack((bamg_geometry.Vertices,np.vstack((subdomaini['x'][0:nods],subdomaini['y'][0:nods],np.ones((nods)))).T))
     200                                bamg_geometry.Edges   =np.vstack((bamg_geometry.Edges,np.vstack((np.arange(count+1,count+nods+1),np.hstack((np.arange(count+2,count+nods+1),count+1)),1.*np.ones((nods)))).T))
     201                                #update counter
     202                                count+=nods
     203                       
     204                if options.getfieldvalue('vertical',0):
     205                        if np.size(options.getfieldvalue('Markers',[]))!=np.size(bamg_geometry.Edges,0):
     206                                raise RuntimeError('for 2d vertical mesh, ''Markers'' option is required, and should be of size ' + str(np.size(bamg_geometry.Edges,0)))
     207                        if np.size(options.getfieldvalue('Markers',[]))==np.size(bamg_geometry.Edges,0):
     208                                bamg_geometry.Edges[:,2]=options.getfieldvalue('Markers')
    113209
    114210                #take care of rifts
     
    303399        bamg_options['hVertices']=options.getfieldvalue('hVertices',np.empty((0,1)))
    304400        bamg_options['KeepVertices']=options.getfieldvalue('KeepVertices',1)
    305         bamg_options['MaxCornerAngle']=options.getfieldvalue('MaxCornerAngle',10.)
    306401        bamg_options['maxnbv']=options.getfieldvalue('maxnbv',10**6)
    307402        bamg_options['maxsubdiv']=options.getfieldvalue('maxsubdiv',10.)
     
    313408        bamg_options['power']=options.getfieldvalue('power',1.)
    314409        bamg_options['splitcorners']=options.getfieldvalue('splitcorners',1)
    315         bamg_options['geometricalmetric']=options.getfieldvalue('geometricalmetric',0)
    316         bamg_options['random']=options.getfieldvalue('rand',True)
    317410        bamg_options['verbose']=options.getfieldvalue('verbose',1)
    318411        #}}}
     
    322415
    323416        # plug results onto model
     417        if options.getfieldvalue('vertical',0):
     418                md.mesh=mesh2dvertical()
     419                md.mesh.x=bamgmesh_out['Vertices'][:,0].copy()
     420                md.mesh.y=bamgmesh_out['Vertices'][:,1].copy()
     421                md.mesh.elements=bamgmesh_out['Triangles'][:,0:3].astype(int)
     422                md.mesh.edges=bamgmesh_out['IssmEdges'].astype(int)
     423                md.mesh.segments=bamgmesh_out['IssmSegments'][:,0:3].astype(int)
     424                md.mesh.segmentmarkers=bamgmesh_out['IssmSegments'][:,3].astype(int)
     425
     426                #Fill in rest of fields:
     427                md.mesh.numberofelements=np.size(md.mesh.elements,axis=0)
     428                md.mesh.numberofvertices=np.size(md.mesh.x)
     429                md.mesh.numberofedges=np.size(md.mesh.edges,axis=0)
     430                md.mesh.vertexonboundary=np.zeros(md.mesh.numberofvertices,bool)
     431                md.mesh.vertexonboundary[md.mesh.segments[:,0:2]-1]=True
     432
     433                #Now, build the connectivity tables for this mesh. Doubled in matlab for some reason
     434                md.mesh.vertexonboundary=np.zeros(md.mesh.numberofvertices,)
     435                md.mesh.vertexonboundary[md.mesh.segments[:,0:2]-1]=1
     436
     437        elif options.getfieldvalue('3dsurface',0):
     438                md.mesh=mesh3dsurface()
     439                md.mesh.x=bamgmesh_out['Vertices'][:,0].copy()
     440                md.mesh.y=bamgmesh_out['Vertices'][:,1].copy()
     441                md.mesh.z=md.mesh.x
     442                md.mesh.z[:]=0
     443                md.mesh.elements=bamgmesh_out['Triangles'][:,0:3].astype(int)
     444                md.mesh.edges=bamgmesh_out['IssmEdges'].astype(int)
     445                md.mesh.segments=bamgmesh_out['IssmSegments'][:,0:3].astype(int)
     446                md.mesh.segmentmarkers=bamgmesh_out['IssmSegments'][:,3].astype(int)
     447
     448                #Fill in rest of fields:
     449                md.mesh.numberofelements=np.size(md.mesh.elements,axis=0)
     450                md.mesh.numberofvertices=np.size(md.mesh.x)
     451                md.mesh.numberofedges=np.size(md.mesh.edges,axis=0)
     452                md.mesh.vertexonboundary=np.zeros(md.mesh.numberofvertices,bool)
     453                md.mesh.vertexonboundary[md.mesh.segments[:,0:2]-1]=True
     454
     455        else:
     456                md.mesh=mesh2d()
     457                md.mesh.x=bamgmesh_out['Vertices'][:,0].copy()
     458                md.mesh.y=bamgmesh_out['Vertices'][:,1].copy()
     459                md.mesh.elements=bamgmesh_out['Triangles'][:,0:3].astype(int)
     460                md.mesh.edges=bamgmesh_out['IssmEdges'].astype(int)
     461                md.mesh.segments=bamgmesh_out['IssmSegments'][:,0:3].astype(int)
     462                md.mesh.segmentmarkers=bamgmesh_out['IssmSegments'][:,3].astype(int)
     463
     464                #Fill in rest of fields:
     465                md.mesh.numberofelements=np.size(md.mesh.elements,axis=0)
     466                md.mesh.numberofvertices=np.size(md.mesh.x)
     467                md.mesh.numberofedges=np.size(md.mesh.edges,axis=0)
     468                md.mesh.vertexonboundary=np.zeros(md.mesh.numberofvertices,bool)
     469                md.mesh.vertexonboundary[md.mesh.segments[:,0:2]-1]=True
     470
     471        #Bamg private fields
    324472        md.private.bamg=OrderedDict()
    325473        md.private.bamg['mesh']=bamgmesh(bamgmesh_out)
    326474        md.private.bamg['geometry']=bamggeom(bamggeom_out)
    327         md.mesh = mesh2d()
    328         md.mesh.x=bamgmesh_out['Vertices'][:,0].copy()
    329         md.mesh.y=bamgmesh_out['Vertices'][:,1].copy()
    330         md.mesh.elements=bamgmesh_out['Triangles'][:,0:3].astype(int)
    331         md.mesh.edges=bamgmesh_out['IssmEdges'].astype(int)
    332         md.mesh.segments=bamgmesh_out['IssmSegments'][:,0:3].astype(int)
    333         md.mesh.segmentmarkers=bamgmesh_out['IssmSegments'][:,3].astype(int)
    334 
    335         #Fill in rest of fields:
    336         md.mesh.numberofelements=np.size(md.mesh.elements,axis=0)
    337         md.mesh.numberofvertices=np.size(md.mesh.x)
    338         md.mesh.numberofedges=np.size(md.mesh.edges,axis=0)
    339         md.mesh.vertexonboundary=np.zeros(md.mesh.numberofvertices,bool)
    340         md.mesh.vertexonboundary[md.mesh.segments[:,0:2]-1]=True
    341475        md.mesh.elementconnectivity=md.private.bamg['mesh'].ElementConnectivity
    342476        md.mesh.elementconnectivity[np.nonzero(np.isnan(md.mesh.elementconnectivity))]=0
     
    345479        #Check for orphan
    346480        if np.any(np.logical_not(np.in1d(np.arange(1,md.mesh.numberofvertices+1),md.mesh.elements.flat))):
    347                 raise RuntimeError("Output mesh has orphans. Decrease MaxCornerAngle to prevent outside points (ex: 0.01)")
     481                raise RuntimeError("Output mesh has orphans. Check your Domain and/or RequiredVertices")
    348482
    349483        return md
Note: See TracChangeset for help on using the changeset viewer.