Changeset 22758 for issm/trunk/src/m/mesh/bamg.py
- Timestamp:
- 05/10/18 10:24:27 (7 years ago)
- Location:
- issm/trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk
- Property svn:mergeinfo changed
-
issm/trunk/src
- Property svn:mergeinfo changed
-
issm/trunk/src/m/mesh/bamg.py
r21341 r22758 1 1 import os.path 2 2 import numpy as np 3 from mesh2d import mesh2d 3 from mesh2d import * 4 from mesh2dvertical import * 5 from mesh3dsurface import * 4 6 from collections import OrderedDict 5 7 from pairoptions import pairoptions … … 20 22 21 23 - 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 22 28 - hmin : minimum edge length (default is 10^-100) 23 29 - hmax : maximum edge length (default is 10^100) … … 37 43 1 -> use Green formula 38 44 - 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)40 45 - maxnbv : maximum number of vertices used to allocate memory (default is 10^6) 41 46 - maxsubdiv : maximum subdivision of exisiting elements (default is 10) … … 49 54 - power : power applied to the metric (default is 1) 50 55 - 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)52 56 - verbose : level of verbosity (default is 1) 53 57 … … 74 78 bamg_mesh=bamgmesh() 75 79 80 subdomain_ref = 1 81 hole_ref = 1 82 76 83 # Bamg Geometry parameters {{{ 77 84 if options.exist('domain'): 78 79 85 #Check that file exists 80 86 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 84 93 85 94 #Build geometry 86 95 count=0 87 96 for i,domaini in enumerate(domain): 88 89 97 #Check that the domain is closed 90 98 if (domaini['x'][0]!=domaini['x'][-1] or domaini['y'][0]!=domaini['y'][-1]): 91 99 raise RuntimeError("bamg error message: all contours provided in ''domain'' should be closed") 92 100 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 94 102 if i: 95 103 flags=ContourToNodes(domaini['x'],domaini['y'],domainfile,0)[0] 96 104 if np.any(np.logical_not(flags)): 97 105 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 98 116 99 117 #Add all points to bamg_geometry … … 102 120 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)) 103 121 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 111 127 #update counter 112 128 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') 113 209 114 210 #take care of rifts … … 303 399 bamg_options['hVertices']=options.getfieldvalue('hVertices',np.empty((0,1))) 304 400 bamg_options['KeepVertices']=options.getfieldvalue('KeepVertices',1) 305 bamg_options['MaxCornerAngle']=options.getfieldvalue('MaxCornerAngle',10.)306 401 bamg_options['maxnbv']=options.getfieldvalue('maxnbv',10**6) 307 402 bamg_options['maxsubdiv']=options.getfieldvalue('maxsubdiv',10.) … … 313 408 bamg_options['power']=options.getfieldvalue('power',1.) 314 409 bamg_options['splitcorners']=options.getfieldvalue('splitcorners',1) 315 bamg_options['geometricalmetric']=options.getfieldvalue('geometricalmetric',0)316 bamg_options['random']=options.getfieldvalue('rand',True)317 410 bamg_options['verbose']=options.getfieldvalue('verbose',1) 318 411 #}}} … … 322 415 323 416 # 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 324 472 md.private.bamg=OrderedDict() 325 473 md.private.bamg['mesh']=bamgmesh(bamgmesh_out) 326 474 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]=True341 475 md.mesh.elementconnectivity=md.private.bamg['mesh'].ElementConnectivity 342 476 md.mesh.elementconnectivity[np.nonzero(np.isnan(md.mesh.elementconnectivity))]=0 … … 345 479 #Check for orphan 346 480 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") 348 482 349 483 return md
Note:
See TracChangeset
for help on using the changeset viewer.