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

Last change on this file was 24313, checked in by Mathieu Morlighem, 5 years ago

merged trunk-jpl and trunk for revision 24310

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