-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathmesh.py
413 lines (345 loc) · 14.4 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import warnings
from typing import Dict, Optional, Tuple,\
Type, TypeVar, Union,\
Callable, Any
import numpy as np
from numpy import ndarray
import matplotlib.pyplot as plt
MeshType = TypeVar('MeshType', bound='Mesh')
DimTuple = Union[Tuple[float],
Tuple[float, float],
Tuple[float, float, float]]
class Mesh():
"""A finite element mesh.
This is an abstract superclass. See the following implementations:
- :class:`~skfem.mesh.MeshTri`, triangular mesh
- :class:`~skfem.mesh.MeshTet`, tetrahedral mesh
- :class:`~skfem.mesh.MeshQuad`, quadrilateral mesh
- :class:`~skfem.mesh.MeshHex`, hexahedral mesh
- :class:`~skfem.mesh.MeshLine`, one-dimensional mesh
Attributes
----------
p
The vertices of the mesh (dim x Nvertices). Each column corresponds to
a point.
t
The element connectivity (dim x Nelements). Each column corresponds to
a element and contains four column indices to p.
refdom
A string describing the shape of the reference domain. Used to find
quadrature rules.
brefdom
A string describing the shape of the reference domain for element
boundaries. Used for finding quadrature rules.
meshio_type
A string which is used to convert between scikit-fem and meshio mesh
types.
name
A string which is used in pretty printing the object.
subdomains
Named subsets of elements.
boundaries
Named subsets of boundary facets.
external
If Mesh is loaded from external format (object), the original
representation is kept here.
"""
refdom: str = "none"
brefdom: str = "none"
meshio_type: str = "none"
name: str = "Abstract"
p: ndarray = np.array([])
t: ndarray = np.array([])
subdomains: Optional[Dict[str, ndarray]] = None
boundaries: Optional[Dict[str, ndarray]] = None
external: Any = None
def __init__(self):
"""Check that p and t are C_CONTIGUOUS as this leads
to better performance."""
if self.p is not None:
if not isinstance(self.p, ndarray):
self.p = np.array(self.p, dtype=np.float_)
if self.p.flags['F_CONTIGUOUS']:
if self.p.shape[1] > 1000:
warnings.warn("Mesh.__init__(): Transforming "
"over 100 vertices to C_CONTIGUOUS.")
self.p = np.ascontiguousarray(self.p)
if self.t is not None:
if not isinstance(self.t, ndarray):
self.t = np.array(self.t, dtype=np.intp)
if self.t.flags['F_CONTIGUOUS']:
if self.t.shape[1] > 1000:
warnings.warn("Mesh.__init__(): Transforming "
"over 100 elements to C_CONTIGUOUS.")
self.t = np.ascontiguousarray(self.t)
# transform lists to ndarrays
if self.boundaries is not None:
for k, v in self.boundaries.items():
if not isinstance(v, ndarray):
self.boundaries[k] = np.array(v, dtype=np.intp)
if self.subdomains is not None:
for k, v in self.subdomains.items():
if not isinstance(v, ndarray):
self.subdomains[k] = np.array(v, dtype=np.intp)
def __str__(self):
return self.__repr__()
def __repr__(self):
return (self.name + " mesh "
"with " + str(self.p.shape[1]) + " vertices "
"and " + str(self.t.shape[1]) + " elements.")
def show(self):
"""A wrapper for matplotlib.pyplot.show()."""
plt.show()
def savefig(self, *args, **kwargs):
"""A wrapper for matplotlib.pyplot.savefig()."""
plt.savefig(*args, **kwargs)
def dim(self):
"""Return the spatial dimension of the mesh."""
return int(self.p.shape[0])
def mapping(self):
"""Default local-to-global mapping for the mesh."""
raise NotImplementedError("Default mapping not implemented!")
def _uniform_refine(self):
"""Perform a single uniform mesh refinement."""
raise NotImplementedError("Single refine not implemented "
"for this mesh type!")
def _adaptive_refine(self, marked):
"""Perform adaptive refinement."""
raise NotImplementedError("Adaptive refine not implemented "
"for this mesh type!")
def refine(self, arg: Optional[Union[int, ndarray]] = None):
"""Refine the mesh.
Parameters
----------
arg
Multiple variations:
- If None, refine all elements.
- If integer, perform multiple uniform refinements.
- If array of element indices, adaptively refine.
"""
if arg is None:
self._uniform_refine()
elif isinstance(arg, int):
for itr in range(arg):
self._uniform_refine()
elif isinstance(arg, list):
self._adaptive_refine(np.array(arg))
elif isinstance(arg, ndarray):
self._adaptive_refine(arg)
else:
raise NotImplementedError("The parameter type not supported.")
def _fix_boundaries(self, facets: ndarray):
"""This should be called after each refine to update the indices in
self.boundaries.
Parameters
----------
facets
An array of integers of size no-splitted-elems x no-facets.
"""
if hasattr(self, "boundaries") and self.boundaries is not None:
for name in self.boundaries:
self.boundaries[name] = (facets[:, self.boundaries[name]]
.flatten())
def remove_elements(self, element_indices: ndarray) -> MeshType:
"""Construct new mesh with elements removed
based on their indices.
Parameters
----------
element_indices
List of element indices to remove.
Returns
-------
Mesh
A new mesh object with the requested elements removed.
"""
keep = np.setdiff1d(np.arange(self.t.shape[1]), element_indices)
newt = self.t[:, keep]
ptix = np.unique(newt)
reverse = np.zeros(self.p.shape[1])
reverse[ptix] = np.arange(len(ptix))
newt = reverse[newt]
newp = self.p[:, ptix]
if newp.shape[1] == 0.0:
raise Exception("The new mesh contains no points!")
meshclass = type(self)
return meshclass(newp, newt.astype(np.intp))
def scale(self, scale: Union[float, DimTuple]) -> None:
"""Scale the mesh.
Parameters
----------
scale
Scale each dimension by this factor. If a single float is provided,
same scaling is used for all dimensions. Otherwise, provide a
tuple which has same size as the mesh dimension.
"""
for itr in range(int(self.dim())):
if isinstance(scale, tuple):
self.p[itr, :] *= scale[itr]
else:
self.p[itr, :] *= scale
def translate(self, vec: DimTuple) -> None:
"""Translate the mesh.
Parameters
----------
vec
Translate the mesh by a vector. Must have same size as the mesh
dimension.
"""
for itr in range(int(self.dim())):
self.p[itr, :] += vec[itr]
def _validate(self):
"""Perform mesh validity checks."""
# check that element connectivity contains integers
# NOTE: this is necessary for some plotting functionality
if not np.issubdtype(self.t[0, 0], np.signedinteger):
msg = ("Mesh._validate(): Element connectivity "
"must consist of integers.")
raise Exception(msg)
# check that vertex matrix has "correct" size
if self.p.shape[0] > 3:
msg = ("Mesh._validate(): We do not allow meshes "
"embedded into larger than 3-dimensional "
"Euclidean space! Please check that "
"the given vertex matrix is of size Ndim x Nvertices.")
raise Exception(msg)
# check that element connectivity matrix has correct size
nvertices = {'line': 2, 'tri': 3, 'quad': 4, 'tet': 4, 'hex': 8}
if self.t.shape[0] != nvertices[self.refdom]:
msg = ("Mesh._validate(): The given connectivity "
"matrix has wrong shape!")
raise Exception(msg)
# check that there are no duplicate points
tmp = np.ascontiguousarray(self.p.T)
if self.p.shape[1] != np.unique(tmp.view([('', tmp.dtype)]
* tmp.shape[1])).shape[0]:
msg = "Mesh._validate(): Mesh contains duplicate vertices."
warnings.warn(msg)
# check that all points are at least in some element
if len(np.setdiff1d(np.arange(self.p.shape[1]),
np.unique(self.t))) > 0:
msg = ("Mesh._validate(): Mesh contains a vertex "
"not belonging to any element.")
raise Exception(msg)
def save(self,
filename: str,
point_data: Optional[Dict[str, ndarray]] = None,
cell_data: Optional[Dict[str, ndarray]] = None) -> None:
"""Export the mesh and fields using meshio.
Parameters
----------
filename
The output filename, with suffix determining format;
e.g. .msh, .vtk, .xdmf
point_data
Data related to the vertices of the mesh.
cell_data
Data related to the elements of the mesh.
"""
import meshio
if point_data is not None:
if not isinstance(point_data, dict):
raise ValueError("point_data should be "
"a dictionary of ndarrays.")
if cell_data is not None:
if not isinstance(cell_data, dict):
raise ValueError("cell_data should be "
"a dictionary of ndarrays.")
cell_data = {self.meshio_type: cell_data}
cells = {self.meshio_type: self.t.T}
mesh = meshio.Mesh(self.p.T, cells, point_data, cell_data)
meshio.write(filename, mesh)
@classmethod
def load(cls: Type[MeshType], filename: str) -> MeshType:
"""Import a mesh from file using `meshio
<https://github.com/nschloe/meshio>`_.
Parameters
----------
filename
The filename of the mesh.
"""
from skfem.importers.meshio import from_file
return from_file(filename)
def boundary_nodes(self) -> ndarray:
"""Return an array of boundary node indices."""
return np.unique(self.facets[:, self.boundary_facets()])
def interior_nodes(self) -> ndarray:
"""Return an array of interior node indices."""
return np.setdiff1d(np.arange(0, self.p.shape[1]),
self.boundary_nodes())
def boundary_facets(self) -> ndarray:
"""Return an array of boundary facet indices."""
return np.nonzero(self.f2t[1, :] == -1)[0]
def interior_facets(self) -> ndarray:
"""Return an array of interior facet indices."""
return np.nonzero(self.f2t[1, :] >= 0)[0]
def element_finder(self) -> Callable[[ndarray], ndarray]:
"""Return a function, which returns element
indices corresponding to the input points."""
raise NotImplementedError("element_finder not implemented "
"for the given Mesh type.")
def nodes_satisfying(self, test: Callable[[ndarray], bool]) -> ndarray:
"""Return nodes that satisfy some condition.
Parameters
----------
test
A function which returns True for the set of nodes that are to be
included in the return set.
"""
return np.nonzero(test(self.p))[0]
def facets_satisfying(self, test: Callable[[ndarray], bool]) -> ndarray:
"""Return facets whose midpoints satisfy some condition.
Parameters
----------
test
A function which returns True for the facet midpoints that are to
be included in the return set.
"""
midp = [np.sum(self.p[itr, self.facets], axis=0) / self.facets.shape[0]
for itr in range(self.p.shape[0])]
return np.nonzero(test(np.array(midp)))[0]
def elements_satisfying(self,
test: Callable[[ndarray], bool]) -> ndarray:
"""Return elements whose midpoints satisfy some condition.
Parameters
----------
test
A function which returns True for the element midpoints that are to
be included in the return set.
"""
midp = [np.sum(self.p[itr, self.t], axis=0) / self.t.shape[0]
for itr in range(self.p.shape[0])]
return np.nonzero(test(np.array(midp)))[0]
@classmethod
def from_dict(cls: Type[MeshType], d) -> MeshType:
"""Initialize a mesh from a dictionary."""
if 'p' not in d or 't' not in d:
raise ValueError("Dictionary must contain keys 'p' and 't'")
else:
d['p'] = np.array(d['p']).T
d['t'] = np.array(d['t']).T
if 'boundaries' in d and d['boundaries'] is not None:
d['boundaries'] = {k: np.array(v)
for k, v in d['boundaries'].items()}
if 'subdomains' in d and d['subdomains'] is not None:
d['subdomains'] = {k: np.array(v)
for k, v in d['subdomains'].items()}
return cls(**d)
def to_dict(self) -> Dict[str, ndarray]:
"""Return json serializable dictionary."""
if self.boundaries is not None:
boundaries = {k: v.tolist() for k, v in self.boundaries.items()}
else:
boundaries = None
if self.subdomains is not None:
subdomains = {k: v.tolist() for k, v in self.subdomains.items()}
else:
subdomains = None
return {
'p': self.p.T.tolist(),
't': self.t.T.tolist(),
'boundaries': boundaries,
'subdomains': subdomains,
}
@staticmethod
def strip_extra_coordinates(p: ndarray) -> ndarray:
return p