source: issm/trunk/src/m/geometry/FlagElements.py@ 16137

Last change on this file since 16137 was 16137, checked in by Mathieu Morlighem, 12 years ago

merged trunk-jpl and trunk for revision 16135

File size: 2.0 KB
RevLine 
[12888]1import numpy
2import os
3#from basinzoom import *
[13975]4from ContourToMesh import *
[12888]5from MatlabFuncs import *
[14310]6from PythonFuncs import *
[12888]7
[12112]8def FlagElements(md,region):
[12888]9 """
10 FLAGELEMENTS - flag the elements in an region
[12112]11
[16137]12 The region can be given with an exp file, a list of elements or vertices
[12888]13
[12944]14 Usage:
15 flag=FlagElements(md,region);
[12888]16
[12944]17 Example:
18 flag=FlagElements(md,'all');
19 flag=FlagElements(md,'');
20 flag=FlagElements(md,'Domain.exp');
21 flag=FlagElements(md,'~Domain.exp');
[12888]22 """
23
[13098]24 if isinstance(region,(str,unicode)):
[12888]25 if not region:
[14067]26 flag=numpy.zeros(md.mesh.numberofelements,bool)
[12888]27 invert=0
28 elif strcmpi(region,'all'):
[14067]29 flag=numpy.ones(md.mesh.numberofelements,bool)
[12888]30 invert=0
[12112]31 else:
32 #make sure that we actually don't want the elements outside the domain outline!
[12888]33 if strcmpi(region[0],'~'):
[12112]34 region=region[1:]
[12888]35 invert=1
[12112]36 else:
[12888]37 invert=0
38
[12112]39 #does the region domain outline exist or do we have to look for xlim,ylim in basinzoom?
[12888]40 if not os.path.exists(region):
41 if len(region)>3 and not strcmp(region[-4:],'.exp'):
42 raise IOError("Error: File 'region' not found!" % region)
[13975]43 raise RuntimeError("FlagElements.py calling basinzoom.py is not complete.")
[12888]44 xlim,ylim=basinzoom('basin',region)
[14310]45 flag_nodes=logical_and_n(md.mesh.x<xlim[1],md.mesh.x>xlim[0],md.mesh.y<ylim[1],md.mesh.y>ylim[0])
[14067]46 flag=numpy.prod(flag_nodes[md.mesh.elements],axis=1).astype(bool)
[12112]47 else:
48 #ok, flag elements
[14310]49 [flag,dum]=ContourToMesh(md.mesh.elements[:,0:3].copy(),md.mesh.x,md.mesh.y,region,'element',1)
[14067]50 flag=flag.astype(bool)
[12888]51
[12112]52 if invert:
[12888]53 flag=numpy.logical_not(flag)
54
[13975]55 elif isinstance(region,numpy.ndarray) or isinstance(region,bool):
[16137]56 if numpy.size(region,0)==md.mesh.numberofelements:
57 flag=region
58 elif numpy.size(region,0)==md.mesh.numberofvertices:
59 flag=(numpy.sum(region[md.mesh.elements-1]>0,axis=1)==numpy.size(md.mesh.elements,1))
60 else:
[12888]61 raise TypeError("Flaglist for region must be of same size as number of elements in model.")
62
[12112]63 else:
[12888]64 raise TypeError("Invalid region option")
[12112]65
[12888]66 return flag
67
Note: See TracBrowser for help on using the repository browser.