loop-common 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loop_common/__init__.py +6 -0
- loop_common/base.py +98 -0
- loop_common/geometry/__init__.py +19 -0
- loop_common/geometry/_aabb.py +37 -0
- loop_common/geometry/_bounding_box.py +792 -0
- loop_common/geometry/_face_table.py +42 -0
- loop_common/geometry/_point.py +238 -0
- loop_common/geometry/_structured_grid.py +114 -0
- loop_common/geometry/_structured_grid_2d.py +146 -0
- loop_common/geometry/_structured_grid_3d.py +243 -0
- loop_common/geometry/_surface.py +288 -0
- loop_common/geometry/_unstructured_mesh.py +208 -0
- loop_common/interfaces/__init__.py +0 -0
- loop_common/interfaces/representation.py +45 -0
- loop_common/io/__init__.py +0 -0
- loop_common/logging/__init__.py +14 -0
- loop_common/logging/logger.py +123 -0
- loop_common/logging/sinks.py +221 -0
- loop_common/logging/timing.py +113 -0
- loop_common/math/__init__.py +5 -0
- loop_common/math/_maths.py +441 -0
- loop_common/math/_transformation.py +178 -0
- loop_common/math/finite_difference_stencil.py +38 -0
- loop_common/observations/__init__.py +3 -0
- loop_common/observations/lineset.py +41 -0
- loop_common/observations/orientation.py +122 -0
- loop_common/observations/pointset.py +8 -0
- loop_common/observer.py +284 -0
- loop_common/supports/_2d_base_unstructured.py +361 -0
- loop_common/supports/_2d_p1_unstructured.py +131 -0
- loop_common/supports/_2d_p2_unstructured.py +349 -0
- loop_common/supports/_2d_structured_grid.py +545 -0
- loop_common/supports/_3d_base_structured.py +546 -0
- loop_common/supports/_3d_p2_tetra.py +374 -0
- loop_common/supports/_3d_rectilinear_grid.py +315 -0
- loop_common/supports/_3d_structured_grid.py +510 -0
- loop_common/supports/_3d_structured_tetra.py +764 -0
- loop_common/supports/_3d_unstructured_tetra.py +656 -0
- loop_common/supports/__init__.py +69 -0
- loop_common/supports/_aabb.py +77 -0
- loop_common/supports/_base_support.py +118 -0
- loop_common/supports/_face_table.py +70 -0
- loop_common/supports/_p2_structured_tetra.py +627 -0
- loop_common/supports/_support_factory.py +78 -0
- loop_common/utils.py +74 -0
- loop_common-0.0.1.dist-info/METADATA +35 -0
- loop_common-0.0.1.dist-info/RECORD +50 -0
- loop_common-0.0.1.dist-info/WHEEL +5 -0
- loop_common-0.0.1.dist-info/licenses/LICENSE +21 -0
- loop_common-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
"""
|
|
2
|
+
P2TetMesh based on cartesian grid for piecewise quadratic interpolation.
|
|
3
|
+
|
|
4
|
+
P2 tetrahedra have 10 nodes:
|
|
5
|
+
- 4 corner nodes (vertices of the tetrahedron)
|
|
6
|
+
- 6 edge midpoint nodes (one at the center of each edge)
|
|
7
|
+
|
|
8
|
+
This mesh adds mid-edge nodes to a structured cartesian grid.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
from loop_common.logging import get_logger as getLogger
|
|
14
|
+
|
|
15
|
+
from . import SupportType
|
|
16
|
+
from ._3d_base_structured import BaseStructuredSupport
|
|
17
|
+
|
|
18
|
+
logger = getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class P2TetMesh(BaseStructuredSupport):
|
|
22
|
+
"""P2 (piecewise quadratic) tetrahedral mesh from a structured cartesian grid.
|
|
23
|
+
|
|
24
|
+
P2 elements have 10 nodes per tetrahedron (4 vertices + 6 edge midpoints).
|
|
25
|
+
This class builds a mesh with both vertex and edge midpoint nodes.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, origin=None, nsteps=None, step_vector=None):
|
|
29
|
+
if origin is None:
|
|
30
|
+
origin = np.zeros(3)
|
|
31
|
+
if nsteps is None:
|
|
32
|
+
nsteps = np.ones(3) * 10
|
|
33
|
+
if step_vector is None:
|
|
34
|
+
step_vector = np.ones(3)
|
|
35
|
+
|
|
36
|
+
BaseStructuredSupport.__init__(self, origin, nsteps, step_vector)
|
|
37
|
+
self.type = SupportType.P2StructuredTetMesh
|
|
38
|
+
|
|
39
|
+
# P1 tetra masks (same as TetMesh - used for vertex connectivity)
|
|
40
|
+
self.tetra_mask_even = np.array(
|
|
41
|
+
[[7, 1, 2, 4], [6, 2, 4, 7], [5, 1, 4, 7], [0, 1, 2, 4], [3, 1, 2, 7]]
|
|
42
|
+
)
|
|
43
|
+
self.tetra_mask = np.array(
|
|
44
|
+
[[0, 6, 5, 3], [7, 3, 5, 6], [4, 0, 5, 6], [2, 0, 3, 6], [1, 0, 3, 5]]
|
|
45
|
+
)
|
|
46
|
+
self.hessian = np.array(
|
|
47
|
+
[
|
|
48
|
+
[
|
|
49
|
+
[4, 4, 0, 0, 0, 0, -8, 0, 0, 0],
|
|
50
|
+
[4, 0, 0, 0, 0, 0, -4, 4, 0, -4],
|
|
51
|
+
[4, 0, 0, 0, 0, -4, -4, 0, 4, 0],
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
[4, 0, 0, 0, 0, 0, -4, 4, 0, -4],
|
|
55
|
+
[4, 0, 4, 0, 0, 0, 0, 0, 0, -8],
|
|
56
|
+
[4, 0, 0, 0, 4, -4, 0, 0, 0, -4],
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
[4, 0, 0, 0, 0, -4, -4, 0, 4, 0],
|
|
60
|
+
[4, 0, 0, 0, 4, -4, 0, 0, 0, -4],
|
|
61
|
+
[4, 0, 0, 4, 0, -8, 0, 0, 0, 0],
|
|
62
|
+
],
|
|
63
|
+
]
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
self.cg = None
|
|
67
|
+
self._elements = None
|
|
68
|
+
self._nodes = None
|
|
69
|
+
self._edge_node_indices = None
|
|
70
|
+
self._p1_mesh = None
|
|
71
|
+
|
|
72
|
+
def onGeometryChange(self):
|
|
73
|
+
"""Reset cached properties when geometry changes."""
|
|
74
|
+
self._elements = None
|
|
75
|
+
self._nodes = None
|
|
76
|
+
self._edge_node_indices = None
|
|
77
|
+
self._p1_mesh = None
|
|
78
|
+
if self.interpolator is not None:
|
|
79
|
+
self.interpolator.reset()
|
|
80
|
+
|
|
81
|
+
def _get_p1_mesh(self):
|
|
82
|
+
"""Return a structured P1 mesh with the same geometry."""
|
|
83
|
+
if self._p1_mesh is None:
|
|
84
|
+
from ._3d_structured_tetra import TetMesh
|
|
85
|
+
|
|
86
|
+
self._p1_mesh = TetMesh(self.origin, self.nsteps - 1, self.step_vector)
|
|
87
|
+
return self._p1_mesh
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def n_nodes(self) -> int:
|
|
91
|
+
"""Total number of nodes: vertices + edge midpoints."""
|
|
92
|
+
return self.n_vertices + self.n_edge_nodes
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def ntetra(self) -> int:
|
|
96
|
+
"""Number of tetrahedra: 5 per cell."""
|
|
97
|
+
return np.prod(self.nsteps_cells) * 5
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def n_elements(self) -> int:
|
|
101
|
+
"""Number of elements (same as ntetra)."""
|
|
102
|
+
return self.ntetra
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def n_cells(self) -> int:
|
|
106
|
+
"""Number of cells in the grid."""
|
|
107
|
+
return np.prod(self.nsteps_cells)
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def n_vertices(self) -> int:
|
|
111
|
+
"""Number of vertex nodes (corners of the grid)."""
|
|
112
|
+
return np.prod(self.nsteps)
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def n_edge_nodes(self) -> int:
|
|
116
|
+
"""Number of edge midpoint nodes."""
|
|
117
|
+
if self._edge_node_indices is None:
|
|
118
|
+
self._build_edge_node_map()
|
|
119
|
+
return len(self._edge_node_indices)
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def nodes(self):
|
|
123
|
+
"""All nodes: vertices followed by edge midpoints."""
|
|
124
|
+
if self._nodes is None:
|
|
125
|
+
self._nodes = self._compute_all_nodes()
|
|
126
|
+
return self._nodes
|
|
127
|
+
|
|
128
|
+
def _compute_all_nodes(self) -> np.ndarray:
|
|
129
|
+
"""Compute vertex and edge midpoint node coordinates."""
|
|
130
|
+
x = np.arange(self.nsteps[0])
|
|
131
|
+
y = np.arange(self.nsteps[1])
|
|
132
|
+
z = np.arange(self.nsteps[2])
|
|
133
|
+
zz, yy, xx = np.meshgrid(z, y, x, indexing="ij")
|
|
134
|
+
vertex_indexes = np.array([xx.flatten(), yy.flatten(), zz.flatten()]).T
|
|
135
|
+
vertex_nodes = self.node_indexes_to_position(vertex_indexes)
|
|
136
|
+
|
|
137
|
+
# Compute edge midpoint nodes
|
|
138
|
+
edge_nodes = self._compute_edge_nodes()
|
|
139
|
+
|
|
140
|
+
# Concatenate vertices and edge nodes
|
|
141
|
+
all_nodes = np.vstack([vertex_nodes, edge_nodes])
|
|
142
|
+
return all_nodes
|
|
143
|
+
|
|
144
|
+
def _compute_edge_nodes(self) -> np.ndarray:
|
|
145
|
+
"""Compute coordinates of all edge midpoint nodes."""
|
|
146
|
+
if self._edge_node_indices is None:
|
|
147
|
+
self._build_edge_node_map()
|
|
148
|
+
|
|
149
|
+
x = np.arange(self.nsteps[0])
|
|
150
|
+
y = np.arange(self.nsteps[1])
|
|
151
|
+
z = np.arange(self.nsteps[2])
|
|
152
|
+
zz, yy, xx = np.meshgrid(z, y, x, indexing="ij")
|
|
153
|
+
vertex_indexes = np.array([xx.flatten(), yy.flatten(), zz.flatten()]).T
|
|
154
|
+
vertex_nodes = self.node_indexes_to_position(vertex_indexes)
|
|
155
|
+
edge_nodes_list = [None] * len(self._edge_node_indices)
|
|
156
|
+
for (v1_idx, v2_idx), edge_index in self._edge_node_indices.items():
|
|
157
|
+
pos1 = vertex_nodes[v1_idx]
|
|
158
|
+
pos2 = vertex_nodes[v2_idx]
|
|
159
|
+
edge_nodes_list[edge_index - self.n_vertices] = 0.5 * (pos1 + pos2)
|
|
160
|
+
|
|
161
|
+
return np.array(edge_nodes_list)
|
|
162
|
+
|
|
163
|
+
def _build_edge_node_map(self):
|
|
164
|
+
"""Build a mapping from vertex pairs to edge node indices."""
|
|
165
|
+
if self._edge_node_indices is not None:
|
|
166
|
+
return self._edge_node_indices
|
|
167
|
+
|
|
168
|
+
edge_map = {}
|
|
169
|
+
n_vertices = self.n_vertices
|
|
170
|
+
|
|
171
|
+
from ._3d_structured_tetra import TetMesh
|
|
172
|
+
dummy_mesh = TetMesh(self.origin, self.nsteps - 1, self.step_vector)
|
|
173
|
+
p1_tetras = dummy_mesh.get_elements()
|
|
174
|
+
|
|
175
|
+
for tetra in p1_tetras:
|
|
176
|
+
tetra = np.asarray(tetra, dtype=int)
|
|
177
|
+
tetra_edges = (
|
|
178
|
+
(tetra[0], tetra[1]),
|
|
179
|
+
(tetra[0], tetra[2]),
|
|
180
|
+
(tetra[0], tetra[3]),
|
|
181
|
+
(tetra[1], tetra[2]),
|
|
182
|
+
(tetra[1], tetra[3]),
|
|
183
|
+
(tetra[2], tetra[3]),
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
for edge in tetra_edges:
|
|
187
|
+
key = tuple(sorted((int(edge[0]), int(edge[1]))))
|
|
188
|
+
if key not in edge_map:
|
|
189
|
+
edge_map[key] = n_vertices + len(edge_map)
|
|
190
|
+
|
|
191
|
+
self._edge_node_indices = edge_map
|
|
192
|
+
return edge_map
|
|
193
|
+
|
|
194
|
+
@property
|
|
195
|
+
def elements(self):
|
|
196
|
+
"""Get all P2 elements (10-node tetrahedra)."""
|
|
197
|
+
if self._elements is None:
|
|
198
|
+
self._elements = self.get_elements()
|
|
199
|
+
return self._elements
|
|
200
|
+
|
|
201
|
+
def get_elements(self):
|
|
202
|
+
"""Get all P2 tetrahedra with 10 nodes each.
|
|
203
|
+
|
|
204
|
+
Returns
|
|
205
|
+
-------
|
|
206
|
+
np.ndarray
|
|
207
|
+
Array of shape (n_tetras, 10) with node indices for each P2 tetrahedron.
|
|
208
|
+
"""
|
|
209
|
+
edge_map = self._build_edge_node_map()
|
|
210
|
+
tetras_list = []
|
|
211
|
+
|
|
212
|
+
# Get P1 tetrahedra (vertex connectivity)
|
|
213
|
+
from ._3d_structured_tetra import TetMesh
|
|
214
|
+
# TetMesh.__init__ adds 1 to nsteps, so pass nsteps - 1 here.
|
|
215
|
+
dummy_mesh = TetMesh(self.origin, self.nsteps - 1, self.step_vector)
|
|
216
|
+
p1_tetras = dummy_mesh.get_elements()
|
|
217
|
+
# For each P1 tetrahedron, add the 6 edge midpoint nodes
|
|
218
|
+
for p1_tetra_vertices in p1_tetras:
|
|
219
|
+
# p1_tetra_vertices is a list of 4 vertex indices
|
|
220
|
+
p2_tetra = list(p1_tetra_vertices)
|
|
221
|
+
|
|
222
|
+
# Add the 6 edge midpoint nodes
|
|
223
|
+
edges = [
|
|
224
|
+
(p1_tetra_vertices[2], p1_tetra_vertices[3]),
|
|
225
|
+
(p1_tetra_vertices[0], p1_tetra_vertices[3]),
|
|
226
|
+
(p1_tetra_vertices[0], p1_tetra_vertices[1]),
|
|
227
|
+
(p1_tetra_vertices[1], p1_tetra_vertices[2]),
|
|
228
|
+
(p1_tetra_vertices[1], p1_tetra_vertices[3]),
|
|
229
|
+
(p1_tetra_vertices[0], p1_tetra_vertices[2]),
|
|
230
|
+
]
|
|
231
|
+
|
|
232
|
+
for v1, v2 in edges:
|
|
233
|
+
edge_key = tuple(sorted([v1, v2]))
|
|
234
|
+
if edge_key in edge_map:
|
|
235
|
+
p2_tetra.append(edge_map[edge_key])
|
|
236
|
+
else:
|
|
237
|
+
# This shouldn't happen in a well-formed mesh
|
|
238
|
+
raise ValueError(f"Edge {edge_key} not found in edge map")
|
|
239
|
+
|
|
240
|
+
tetras_list.append(p2_tetra)
|
|
241
|
+
|
|
242
|
+
return np.array(tetras_list, dtype=np.int64)
|
|
243
|
+
|
|
244
|
+
def evaluate_shape(self, locations: np.ndarray):
|
|
245
|
+
"""Evaluate quadratic tetrahedral shape functions at locations."""
|
|
246
|
+
locations = np.array(locations)
|
|
247
|
+
_verts, c, elements, inside = self.get_element_for_location(locations)
|
|
248
|
+
N = np.zeros((c.shape[0], 10))
|
|
249
|
+
|
|
250
|
+
for i in range(c.shape[1]):
|
|
251
|
+
N[:, i] = (2 * c[:, i] - 1) * c[:, i]
|
|
252
|
+
|
|
253
|
+
N[:, 4] = 4 * c[:, 3] * c[:, 2]
|
|
254
|
+
N[:, 5] = 4 * c[:, 0] * c[:, 3]
|
|
255
|
+
N[:, 6] = 4 * c[:, 0] * c[:, 1]
|
|
256
|
+
N[:, 7] = 4 * c[:, 1] * c[:, 2]
|
|
257
|
+
N[:, 8] = 4 * c[:, 1] * c[:, 3]
|
|
258
|
+
N[:, 9] = 4 * c[:, 0] * c[:, 2]
|
|
259
|
+
|
|
260
|
+
return N, elements, inside
|
|
261
|
+
|
|
262
|
+
def evaluate_shape_derivatives(self, locations: np.ndarray, elements=None):
|
|
263
|
+
"""Evaluate quadratic tetrahedral shape derivatives at locations."""
|
|
264
|
+
locations = np.array(locations)
|
|
265
|
+
if elements is None:
|
|
266
|
+
verts, c, elements, _inside = self.get_element_for_location(locations)
|
|
267
|
+
else:
|
|
268
|
+
M = np.ones((elements.shape[0], 4, 4))
|
|
269
|
+
M[:, :, 1:] = self.nodes[self.elements[elements], :][:, :4, :]
|
|
270
|
+
points_ = np.ones((locations.shape[0], 4))
|
|
271
|
+
points_[:, 1:] = locations
|
|
272
|
+
minv = np.linalg.inv(M)
|
|
273
|
+
c = np.einsum("lij,li->lj", minv, points_)
|
|
274
|
+
verts = self.nodes[self.elements[elements][:, :4]]
|
|
275
|
+
|
|
276
|
+
jac = np.array(
|
|
277
|
+
[
|
|
278
|
+
[
|
|
279
|
+
(verts[:, 1, 0] - verts[:, 0, 0]),
|
|
280
|
+
(verts[:, 1, 1] - verts[:, 0, 1]),
|
|
281
|
+
(verts[:, 1, 2] - verts[:, 0, 2]),
|
|
282
|
+
],
|
|
283
|
+
[
|
|
284
|
+
(verts[:, 2, 0] - verts[:, 0, 0]),
|
|
285
|
+
(verts[:, 2, 1] - verts[:, 0, 1]),
|
|
286
|
+
(verts[:, 2, 2] - verts[:, 0, 2]),
|
|
287
|
+
],
|
|
288
|
+
[
|
|
289
|
+
(verts[:, 3, 0] - verts[:, 0, 0]),
|
|
290
|
+
(verts[:, 3, 1] - verts[:, 0, 1]),
|
|
291
|
+
(verts[:, 3, 2] - verts[:, 0, 2]),
|
|
292
|
+
],
|
|
293
|
+
]
|
|
294
|
+
)
|
|
295
|
+
r = c[:, 1]
|
|
296
|
+
s = c[:, 2]
|
|
297
|
+
t = c[:, 3]
|
|
298
|
+
jac = np.swapaxes(jac, 0, 2)
|
|
299
|
+
dN = np.zeros((elements.shape[0], 3, 10))
|
|
300
|
+
|
|
301
|
+
dN[:, 0, 0] = 4 * r + 4 * s + 4 * t - 3
|
|
302
|
+
dN[:, 0, 1] = 4 * r - 1
|
|
303
|
+
dN[:, 0, 2] = 0
|
|
304
|
+
dN[:, 0, 3] = 0
|
|
305
|
+
dN[:, 0, 4] = 0
|
|
306
|
+
dN[:, 0, 5] = -4 * t
|
|
307
|
+
dN[:, 0, 6] = -8 * r - 4 * s - 4 * t + 4
|
|
308
|
+
dN[:, 0, 7] = 4 * s
|
|
309
|
+
dN[:, 0, 8] = 4 * t
|
|
310
|
+
dN[:, 0, 9] = -4 * s
|
|
311
|
+
|
|
312
|
+
dN[:, 1, 0] = 4 * r + 4 * s + 4 * t - 3
|
|
313
|
+
dN[:, 1, 1] = 0
|
|
314
|
+
dN[:, 1, 2] = 4 * s - 1
|
|
315
|
+
dN[:, 1, 3] = 0
|
|
316
|
+
dN[:, 1, 4] = 4 * t
|
|
317
|
+
dN[:, 1, 5] = -4 * t
|
|
318
|
+
dN[:, 1, 6] = -4 * r
|
|
319
|
+
dN[:, 1, 7] = 4 * r
|
|
320
|
+
dN[:, 1, 8] = 0
|
|
321
|
+
dN[:, 1, 9] = -4 * r - 8 * s - 4 * t + 4
|
|
322
|
+
|
|
323
|
+
dN[:, 2, 0] = 4 * r + 4 * s + 4 * t - 3
|
|
324
|
+
dN[:, 2, 1] = 0
|
|
325
|
+
dN[:, 2, 2] = 0
|
|
326
|
+
dN[:, 2, 3] = 4 * t - 1
|
|
327
|
+
dN[:, 2, 4] = 4 * s
|
|
328
|
+
dN[:, 2, 5] = -4 * r - 4 * s - 8 * t + 4
|
|
329
|
+
dN[:, 2, 6] = -4 * r
|
|
330
|
+
dN[:, 2, 7] = 0
|
|
331
|
+
dN[:, 2, 8] = 4 * r
|
|
332
|
+
dN[:, 2, 9] = -4 * s
|
|
333
|
+
|
|
334
|
+
d_n = np.linalg.inv(jac)
|
|
335
|
+
d_n = d_n.swapaxes(1, 2)
|
|
336
|
+
d_n = d_n @ dN
|
|
337
|
+
return d_n, elements
|
|
338
|
+
|
|
339
|
+
def evaluate_shape_d2(self, indexes: np.ndarray) -> np.ndarray:
|
|
340
|
+
"""Evaluate second derivatives of the P2 tetrahedral shape functions."""
|
|
341
|
+
vertices = self.nodes[self.elements[indexes], :]
|
|
342
|
+
|
|
343
|
+
jac = np.array(
|
|
344
|
+
[
|
|
345
|
+
[
|
|
346
|
+
(vertices[:, 1, 0] - vertices[:, 0, 0]),
|
|
347
|
+
(vertices[:, 1, 1] - vertices[:, 0, 1]),
|
|
348
|
+
(vertices[:, 1, 2] - vertices[:, 0, 2]),
|
|
349
|
+
],
|
|
350
|
+
[
|
|
351
|
+
(vertices[:, 2, 0] - vertices[:, 0, 0]),
|
|
352
|
+
(vertices[:, 2, 1] - vertices[:, 0, 1]),
|
|
353
|
+
(vertices[:, 2, 2] - vertices[:, 0, 2]),
|
|
354
|
+
],
|
|
355
|
+
[
|
|
356
|
+
(vertices[:, 3, 0] - vertices[:, 0, 0]),
|
|
357
|
+
(vertices[:, 3, 1] - vertices[:, 0, 1]),
|
|
358
|
+
(vertices[:, 3, 2] - vertices[:, 0, 2]),
|
|
359
|
+
],
|
|
360
|
+
]
|
|
361
|
+
)
|
|
362
|
+
jac = jac.swapaxes(0, 2)
|
|
363
|
+
jac = jac.swapaxes(1, 2)
|
|
364
|
+
jac = np.linalg.inv(jac)
|
|
365
|
+
|
|
366
|
+
d2 = np.zeros((vertices.shape[0], 6, self.elements.shape[1]))
|
|
367
|
+
ii = 0
|
|
368
|
+
for i in range(3):
|
|
369
|
+
for j in range(i, 3):
|
|
370
|
+
for k in range(3):
|
|
371
|
+
for l in range(3):
|
|
372
|
+
d2[:, ii, :] += (
|
|
373
|
+
jac[:, i, k, None] * jac[:, j, l, None] * self.hessian[None, k, l, :]
|
|
374
|
+
)
|
|
375
|
+
ii += 1
|
|
376
|
+
return d2
|
|
377
|
+
|
|
378
|
+
def get_quadrature_points(self, npts: int = 3):
|
|
379
|
+
"""Return face quadrature points for shared tetra faces."""
|
|
380
|
+
if npts not in (1, 3):
|
|
381
|
+
raise ValueError("Only 1-point and 3-point quadrature are supported")
|
|
382
|
+
|
|
383
|
+
vertices = self.nodes[self.shared_elements]
|
|
384
|
+
if npts == 3:
|
|
385
|
+
cp = np.zeros((vertices.shape[0], 3, 3))
|
|
386
|
+
reference_points = np.array([[1 / 6, 2 / 3, 1 / 6], [1 / 6, 1 / 6, 2 / 3]])
|
|
387
|
+
|
|
388
|
+
cp[:, 0, :] = (
|
|
389
|
+
vertices[:, 0, :] * (1 - reference_points[0, 0] - reference_points[1, 0])
|
|
390
|
+
+ vertices[:, 1, :] * (reference_points[0, 0])
|
|
391
|
+
+ vertices[:, 2, :] * (reference_points[1, 0])
|
|
392
|
+
)
|
|
393
|
+
cp[:, 1, :] = (
|
|
394
|
+
vertices[:, 0, :] * (1 - reference_points[0, 1] - reference_points[1, 1])
|
|
395
|
+
+ vertices[:, 1, :] * (reference_points[0, 1])
|
|
396
|
+
+ vertices[:, 2, :] * (reference_points[1, 1])
|
|
397
|
+
)
|
|
398
|
+
cp[:, 2, :] = (
|
|
399
|
+
vertices[:, 0, :] * (1 - reference_points[0, 2] - reference_points[1, 2])
|
|
400
|
+
+ vertices[:, 1, :] * (reference_points[0, 2])
|
|
401
|
+
+ vertices[:, 2, :] * (reference_points[1, 2])
|
|
402
|
+
)
|
|
403
|
+
weights = np.zeros((vertices.shape[0], 3))
|
|
404
|
+
weights[:, :] = 1 / 6
|
|
405
|
+
return cp, weights
|
|
406
|
+
|
|
407
|
+
cp = np.zeros((vertices.shape[0], 1, 3))
|
|
408
|
+
reference_points = np.array([[1 / 3], [1 / 3]])
|
|
409
|
+
cp[:, 0, :] = (
|
|
410
|
+
vertices[:, 0, :] * (1 - reference_points[0, 0] - reference_points[1, 0])
|
|
411
|
+
+ vertices[:, 1, :] * (reference_points[0, 0])
|
|
412
|
+
+ vertices[:, 2, :] * (reference_points[1, 0])
|
|
413
|
+
)
|
|
414
|
+
weights = np.zeros((vertices.shape[0], 1))
|
|
415
|
+
weights[:, :] = 1 / 2
|
|
416
|
+
return cp, weights
|
|
417
|
+
|
|
418
|
+
@property
|
|
419
|
+
def shared_element_relationships(self):
|
|
420
|
+
"""Shared face relationships from the underlying P1 tetra mesh."""
|
|
421
|
+
return self._get_p1_mesh().shared_element_relationships
|
|
422
|
+
|
|
423
|
+
@property
|
|
424
|
+
def shared_elements(self):
|
|
425
|
+
"""Shared face node indices from the underlying P1 tetra mesh."""
|
|
426
|
+
return self._get_p1_mesh().shared_elements
|
|
427
|
+
|
|
428
|
+
@property
|
|
429
|
+
def shared_element_norm(self):
|
|
430
|
+
"""Face normals from the underlying P1 tetra mesh."""
|
|
431
|
+
return self._get_p1_mesh().shared_element_norm
|
|
432
|
+
|
|
433
|
+
@property
|
|
434
|
+
def shared_element_size(self):
|
|
435
|
+
"""Face areas from the underlying P1 tetra mesh."""
|
|
436
|
+
return self._get_p1_mesh().shared_element_size
|
|
437
|
+
|
|
438
|
+
@property
|
|
439
|
+
def shared_element_scale(self):
|
|
440
|
+
"""Scaled face areas from the underlying P1 tetra mesh."""
|
|
441
|
+
return self._get_p1_mesh().shared_element_scale
|
|
442
|
+
|
|
443
|
+
@property
|
|
444
|
+
def neighbours(self):
|
|
445
|
+
"""Neighbour relationships from the underlying P1 tetra mesh."""
|
|
446
|
+
return self._get_p1_mesh().neighbours
|
|
447
|
+
|
|
448
|
+
@property
|
|
449
|
+
def element_size(self):
|
|
450
|
+
"""Calculate the volume of tetrahedra using the 4 corner vertices."""
|
|
451
|
+
vecs = (
|
|
452
|
+
self.nodes[self.elements[:, 1:4], :]
|
|
453
|
+
- self.nodes[self.elements[:, 0, None], :]
|
|
454
|
+
)
|
|
455
|
+
return np.abs(np.linalg.det(vecs)) / 6
|
|
456
|
+
|
|
457
|
+
@property
|
|
458
|
+
def barycentre(self) -> np.ndarray:
|
|
459
|
+
"""Return the barycentres of all tetrahedra."""
|
|
460
|
+
tetra_vertices = self.elements[:, :4]
|
|
461
|
+
barycentre = np.sum(self.nodes[tetra_vertices][:, :, :], axis=1) / 4.0
|
|
462
|
+
return barycentre
|
|
463
|
+
|
|
464
|
+
def evaluate_value(self, pos: np.ndarray, property_array: np.ndarray) -> np.ndarray:
|
|
465
|
+
"""Evaluate value at given positions using P2 interpolation."""
|
|
466
|
+
values = np.zeros(pos.shape[0])
|
|
467
|
+
values[:] = np.nan
|
|
468
|
+
|
|
469
|
+
N, tetras, inside = self.evaluate_shape(pos)
|
|
470
|
+
values[inside] = np.sum(
|
|
471
|
+
N[inside, :] * property_array[self.elements[tetras[inside], :]], axis=1
|
|
472
|
+
)
|
|
473
|
+
return values
|
|
474
|
+
|
|
475
|
+
def evaluate_gradient(self, pos: np.ndarray, property_array: np.ndarray) -> np.ndarray:
|
|
476
|
+
"""Evaluate the gradient of an interpolant at the locations.
|
|
477
|
+
|
|
478
|
+
This uses the quadratic tetrahedral basis on the 10 P2 nodes.
|
|
479
|
+
"""
|
|
480
|
+
values = np.zeros(pos.shape)
|
|
481
|
+
values[:] = np.nan
|
|
482
|
+
|
|
483
|
+
element_gradients, tetras = self.evaluate_shape_derivatives(pos)
|
|
484
|
+
_, _, inside = self.evaluate_shape(pos)
|
|
485
|
+
values[inside, :] = np.einsum(
|
|
486
|
+
"ijk,ik->ij",
|
|
487
|
+
element_gradients[inside, :, :],
|
|
488
|
+
property_array[self.elements[tetras[inside], :]],
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
return values
|
|
492
|
+
|
|
493
|
+
def get_element_for_location(self, pos: np.ndarray):
|
|
494
|
+
"""Determine the tetrahedron from a numpy array of points.
|
|
495
|
+
|
|
496
|
+
This uses only the vertex nodes for locating points.
|
|
497
|
+
"""
|
|
498
|
+
pos = np.array(pos)
|
|
499
|
+
pos = pos[:, : self.dimension]
|
|
500
|
+
inside = self.inside(pos)
|
|
501
|
+
|
|
502
|
+
# Create vertex-only elements for point location (P1 mesh)
|
|
503
|
+
vertices = np.zeros((pos.shape[0], 5, 4, 3))
|
|
504
|
+
vertices[:] = np.nan
|
|
505
|
+
|
|
506
|
+
cell_indexes, inside = self.position_to_cell_index(pos)
|
|
507
|
+
even_mask = np.sum(cell_indexes, axis=1) % 2 == 0
|
|
508
|
+
|
|
509
|
+
corner_indexes = self.cell_corner_indexes(cell_indexes)
|
|
510
|
+
vert_positions = self.node_indexes_to_position(corner_indexes)
|
|
511
|
+
|
|
512
|
+
vertices[even_mask, :, :, :] = vert_positions[even_mask, :, :][:, self.tetra_mask_even, :]
|
|
513
|
+
vertices[~even_mask, :, :, :] = vert_positions[~even_mask, :, :][:, self.tetra_mask, :]
|
|
514
|
+
|
|
515
|
+
vap = pos[:, None, :] - vertices[:, :, 0, :]
|
|
516
|
+
vbp = pos[:, None, :] - vertices[:, :, 1, :]
|
|
517
|
+
vab = vertices[:, :, 1, :] - vertices[:, :, 0, :]
|
|
518
|
+
vac = vertices[:, :, 2, :] - vertices[:, :, 0, :]
|
|
519
|
+
vad = vertices[:, :, 3, :] - vertices[:, :, 0, :]
|
|
520
|
+
vbc = vertices[:, :, 2, :] - vertices[:, :, 1, :]
|
|
521
|
+
vbd = vertices[:, :, 3, :] - vertices[:, :, 1, :]
|
|
522
|
+
|
|
523
|
+
va = np.einsum("ikj, ikj->ik", vbp, np.cross(vbd, vbc, axisa=2, axisb=2)) / 6.0
|
|
524
|
+
vb = np.einsum("ikj, ikj->ik", vap, np.cross(vac, vad, axisa=2, axisb=2)) / 6.0
|
|
525
|
+
vc = np.einsum("ikj, ikj->ik", vap, np.cross(vad, vab, axisa=2, axisb=2)) / 6.0
|
|
526
|
+
vd = np.einsum("ikj, ikj->ik", vap, np.cross(vab, vac, axisa=2, axisb=2)) / 6.0
|
|
527
|
+
v = np.einsum("ikj, ikj->ik", vab, np.cross(vac, vad, axisa=2, axisb=2)) / 6.0
|
|
528
|
+
|
|
529
|
+
c = np.zeros((va.shape[0], va.shape[1], 4))
|
|
530
|
+
c[:, :, 0] = va / v
|
|
531
|
+
c[:, :, 1] = vb / v
|
|
532
|
+
c[:, :, 2] = vc / v
|
|
533
|
+
c[:, :, 3] = vd / v
|
|
534
|
+
|
|
535
|
+
mask = np.all(c >= 0, axis=2)
|
|
536
|
+
i, j = np.where(mask)
|
|
537
|
+
pairs = dict(zip(i, j))
|
|
538
|
+
mask[:] = False
|
|
539
|
+
mask[list(pairs.keys()), list(pairs.values())] = True
|
|
540
|
+
|
|
541
|
+
inside = np.logical_and(inside, np.any(mask, axis=1))
|
|
542
|
+
|
|
543
|
+
even_mask = np.sum(cell_indexes, axis=1) % 2 == 0
|
|
544
|
+
gi = self.global_node_indices(corner_indexes)
|
|
545
|
+
|
|
546
|
+
tetras = np.zeros((corner_indexes.shape[0], 5, 4)).astype(int)
|
|
547
|
+
tetras[even_mask, :, :] = gi[even_mask, :][:, self.tetra_mask_even]
|
|
548
|
+
tetras[~even_mask, :, :] = gi[~even_mask, :][:, self.tetra_mask]
|
|
549
|
+
|
|
550
|
+
inside = np.logical_and(inside, self.inside(pos))
|
|
551
|
+
|
|
552
|
+
vertices_return = np.zeros((pos.shape[0], 4, 3))
|
|
553
|
+
vertices_return[:] = np.nan
|
|
554
|
+
mask[~inside, :] = False
|
|
555
|
+
vertices_return[inside, :, :] = vertices[mask, :, :]
|
|
556
|
+
|
|
557
|
+
c_return = np.zeros((pos.shape[0], 4))
|
|
558
|
+
c_return[:] = np.nan
|
|
559
|
+
c_return[inside] = c[mask]
|
|
560
|
+
|
|
561
|
+
tetra_return = np.zeros(pos.shape[0]).astype(int)
|
|
562
|
+
tetra_return[:] = -1
|
|
563
|
+
|
|
564
|
+
local_tetra_index = np.tile(np.arange(0, 5)[None, :], (mask.shape[0], 1))
|
|
565
|
+
local_tetra_index = local_tetra_index[mask]
|
|
566
|
+
|
|
567
|
+
tetra_global_index = self.tetra_global_index(cell_indexes[inside, :], local_tetra_index)
|
|
568
|
+
tetra_return[inside] = tetra_global_index
|
|
569
|
+
|
|
570
|
+
return vertices_return, c_return, tetra_return, inside
|
|
571
|
+
|
|
572
|
+
def get_element_gradient_for_location(self, pos: np.ndarray):
|
|
573
|
+
"""Get the gradient of the tetra for a location.
|
|
574
|
+
|
|
575
|
+
Uses vertex-only evaluation (P1 gradients).
|
|
576
|
+
"""
|
|
577
|
+
vertices, _bc, tetras, inside = self.get_element_for_location(pos)
|
|
578
|
+
ps = vertices
|
|
579
|
+
|
|
580
|
+
m = np.array([
|
|
581
|
+
[
|
|
582
|
+
(ps[:, 1, 0] - ps[:, 0, 0]),
|
|
583
|
+
(ps[:, 1, 1] - ps[:, 0, 1]),
|
|
584
|
+
(ps[:, 1, 2] - ps[:, 0, 2]),
|
|
585
|
+
],
|
|
586
|
+
[
|
|
587
|
+
(ps[:, 2, 0] - ps[:, 0, 0]),
|
|
588
|
+
(ps[:, 2, 1] - ps[:, 0, 1]),
|
|
589
|
+
(ps[:, 2, 2] - ps[:, 0, 2]),
|
|
590
|
+
],
|
|
591
|
+
[
|
|
592
|
+
(ps[:, 3, 0] - ps[:, 0, 0]),
|
|
593
|
+
(ps[:, 3, 1] - ps[:, 0, 1]),
|
|
594
|
+
(ps[:, 3, 2] - ps[:, 0, 2]),
|
|
595
|
+
],
|
|
596
|
+
])
|
|
597
|
+
|
|
598
|
+
I = np.array([[-1.0, 1.0, 0.0, 0.0], [-1.0, 0.0, 1.0, 0.0], [-1.0, 0.0, 0.0, 1.0]])
|
|
599
|
+
m = np.swapaxes(m, 0, 2)
|
|
600
|
+
element_gradients = np.zeros_like(m)
|
|
601
|
+
element_gradients[:] = np.nan
|
|
602
|
+
element_gradients[inside, :, :] = np.linalg.inv(m[inside, :, :])
|
|
603
|
+
|
|
604
|
+
element_gradients = element_gradients.swapaxes(1, 2)
|
|
605
|
+
element_gradients = element_gradients @ I
|
|
606
|
+
|
|
607
|
+
return vertices, element_gradients, tetras, inside
|
|
608
|
+
|
|
609
|
+
def tetra_global_index(self, indices, tetra_index):
|
|
610
|
+
"""Get the global index of a tetra from cell index and local tetra index."""
|
|
611
|
+
return (
|
|
612
|
+
tetra_index
|
|
613
|
+
+ indices[:, 0] * 5
|
|
614
|
+
+ self.nsteps_cells[0] * indices[:, 1] * 5
|
|
615
|
+
+ self.nsteps_cells[0] * self.nsteps_cells[1] * indices[:, 2] * 5
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
def inside(self, pos: np.ndarray):
|
|
619
|
+
"""Check if points are inside the mesh domain."""
|
|
620
|
+
inside = np.ones(pos.shape[0]).astype(bool)
|
|
621
|
+
for i in range(3):
|
|
622
|
+
inside *= pos[:, i] > self.origin[None, i]
|
|
623
|
+
inside *= (
|
|
624
|
+
pos[:, i]
|
|
625
|
+
< self.origin[None, i] + self.step_vector[None, i] * self.nsteps_cells[None, i]
|
|
626
|
+
)
|
|
627
|
+
return inside
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from loop_common.supports import SupportType, support_map
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SupportFactory:
|
|
11
|
+
@staticmethod
|
|
12
|
+
def create_support(support_type, **kwargs):
|
|
13
|
+
if support_type is None:
|
|
14
|
+
raise ValueError("No support type specified")
|
|
15
|
+
if isinstance(support_type, str):
|
|
16
|
+
support_type = SupportType._member_map_[support_type].numerator
|
|
17
|
+
return support_map[support_type](**kwargs)
|
|
18
|
+
|
|
19
|
+
@staticmethod
|
|
20
|
+
def from_dict(d):
|
|
21
|
+
d = d.copy()
|
|
22
|
+
support_type = d.pop("type", None)
|
|
23
|
+
if support_type is None:
|
|
24
|
+
raise ValueError("No support type specified")
|
|
25
|
+
return SupportFactory.create_support(support_type, **d)
|
|
26
|
+
|
|
27
|
+
# Support types whose constructor takes nsteps as a *cell* count
|
|
28
|
+
# (translated internally to a node count via BaseStructuredSupport).
|
|
29
|
+
_CELL_COUNT_SUPPORT_TYPES: ClassVar[frozenset[SupportType]] = frozenset(
|
|
30
|
+
{
|
|
31
|
+
SupportType.StructuredGrid,
|
|
32
|
+
SupportType.TetMesh,
|
|
33
|
+
SupportType.P2UnstructuredTetMesh,
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def create_support_from_bbox(
|
|
39
|
+
support_type,
|
|
40
|
+
bounding_box,
|
|
41
|
+
nelements,
|
|
42
|
+
element_volume=None,
|
|
43
|
+
buffer: float | None = None,
|
|
44
|
+
local_coordinates: bool = True,
|
|
45
|
+
):
|
|
46
|
+
if isinstance(support_type, str):
|
|
47
|
+
support_type = SupportType._member_map_[support_type].numerator
|
|
48
|
+
if buffer is not None:
|
|
49
|
+
bounding_box = bounding_box.with_buffer(buffer=buffer)
|
|
50
|
+
if element_volume is not None:
|
|
51
|
+
nelements = int(np.prod(bounding_box.length) / element_volume)
|
|
52
|
+
if nelements is not None:
|
|
53
|
+
bounding_box.nelements = nelements
|
|
54
|
+
|
|
55
|
+
if local_coordinates:
|
|
56
|
+
# Build the mesh in the bounding box's local (interpolation) frame
|
|
57
|
+
# rather than raw world coordinates -- keeps node coordinates
|
|
58
|
+
# numerically well-conditioned. Project origin/maximum directly
|
|
59
|
+
# (rather than all corners, which BoundingBox.corners only
|
|
60
|
+
# supports in 3D) -- exact for translation-only transforms, which
|
|
61
|
+
# is the only kind in use today (no code sets a non-identity
|
|
62
|
+
# rotation on a bounding box).
|
|
63
|
+
local_points = bounding_box.project(np.array([bounding_box.origin, bounding_box.maximum]))
|
|
64
|
+
origin = np.min(local_points, axis=0)
|
|
65
|
+
local_maximum = np.max(local_points, axis=0)
|
|
66
|
+
step_vector = (local_maximum - origin) / bounding_box.nsteps
|
|
67
|
+
else:
|
|
68
|
+
origin = bounding_box.origin
|
|
69
|
+
step_vector = bounding_box.step_vector
|
|
70
|
+
|
|
71
|
+
nsteps_kwarg = (
|
|
72
|
+
"nsteps_cells" if support_type in SupportFactory._CELL_COUNT_SUPPORT_TYPES else "nsteps"
|
|
73
|
+
)
|
|
74
|
+
return support_map[support_type](
|
|
75
|
+
origin=origin,
|
|
76
|
+
step_vector=step_vector,
|
|
77
|
+
**{nsteps_kwarg: bounding_box.nsteps},
|
|
78
|
+
)
|