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,361 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tetmesh based on cartesian grid for piecewise linear interpolation
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from abc import abstractmethod
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
from scipy import sparse
|
|
10
|
+
|
|
11
|
+
from . import SupportType
|
|
12
|
+
from ._2d_structured_grid import StructuredGrid2D
|
|
13
|
+
from ._aabb import _initialise_aabb
|
|
14
|
+
from ._base_support import BaseSupport
|
|
15
|
+
from ._face_table import _init_face_table
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class BaseUnstructured2d(BaseSupport):
|
|
21
|
+
""" """
|
|
22
|
+
|
|
23
|
+
dimension = 2
|
|
24
|
+
|
|
25
|
+
def __init__(self, elements, vertices, neighbours, aabb_nsteps=None):
|
|
26
|
+
self.type = SupportType.BaseUnstructured2d
|
|
27
|
+
self._elements = elements
|
|
28
|
+
self.vertices = vertices
|
|
29
|
+
if self.elements.shape[1] == 3:
|
|
30
|
+
self.order = 1
|
|
31
|
+
elif self.elements.shape[1] == 6:
|
|
32
|
+
self.order = 2
|
|
33
|
+
self.dof = self.vertices.shape[0]
|
|
34
|
+
self.neighbours = neighbours
|
|
35
|
+
self.minimum = np.min(self.nodes, axis=0)
|
|
36
|
+
self.maximum = np.max(self.nodes, axis=0)
|
|
37
|
+
length = self.maximum - self.minimum
|
|
38
|
+
self.minimum -= length * 0.1
|
|
39
|
+
self.maximum += length * 0.1
|
|
40
|
+
if aabb_nsteps is None:
|
|
41
|
+
box_vol = np.prod(self.maximum - self.minimum)
|
|
42
|
+
element_volume = box_vol / (len(self.elements) / 20)
|
|
43
|
+
# calculate the step vector of a regular cube
|
|
44
|
+
step_vector = np.zeros(2)
|
|
45
|
+
step_vector[:] = element_volume ** (1.0 / 2.0)
|
|
46
|
+
# number of steps is the length of the box / step vector
|
|
47
|
+
aabb_nsteps = np.ceil((self.maximum - self.minimum) / step_vector).astype(int)
|
|
48
|
+
# make sure there is at least one cell in every dimension
|
|
49
|
+
aabb_nsteps[aabb_nsteps < 2] = 2
|
|
50
|
+
step_vector = (self.maximum - self.minimum) / (aabb_nsteps - 1)
|
|
51
|
+
self.aabb_grid = StructuredGrid2D(self.minimum, nsteps=aabb_nsteps, step_vector=step_vector)
|
|
52
|
+
# make a big table to store which tetra are in which element.
|
|
53
|
+
# if this takes up too much memory it could be simplified by using sparse matrices or dict but
|
|
54
|
+
# at the expense of speed
|
|
55
|
+
self._aabb_table = sparse.csr_matrix(
|
|
56
|
+
(self.aabb_grid.n_elements, len(self.elements)), dtype=bool
|
|
57
|
+
)
|
|
58
|
+
self._shared_element_relationships = np.zeros(
|
|
59
|
+
(self.neighbours[self.neighbours >= 0].flatten().shape[0], 2), dtype=int
|
|
60
|
+
)
|
|
61
|
+
self._shared_elements = np.zeros(
|
|
62
|
+
(self.neighbours[self.neighbours >= 0].flatten().shape[0], self.dimension), dtype=int
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def aabb_table(self):
|
|
67
|
+
if np.sum(self._aabb_table) == 0:
|
|
68
|
+
_initialise_aabb(self)
|
|
69
|
+
return self._aabb_table
|
|
70
|
+
|
|
71
|
+
def set_nelements(self, nelements) -> int:
|
|
72
|
+
raise NotImplementedError
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def shared_elements(self):
|
|
76
|
+
if np.sum(self._shared_elements) == 0:
|
|
77
|
+
_init_face_table(self)
|
|
78
|
+
return self._shared_elements
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def shared_element_relationships(self):
|
|
82
|
+
if np.sum(self._shared_element_relationships) == 0:
|
|
83
|
+
_init_face_table(self)
|
|
84
|
+
return self._shared_element_relationships
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def elements(self):
|
|
88
|
+
return self._elements
|
|
89
|
+
|
|
90
|
+
def onGeometryChange(self):
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def n_elements(self):
|
|
95
|
+
return self.elements.shape[0]
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def n_nodes(self):
|
|
99
|
+
return self.vertices.shape[0]
|
|
100
|
+
|
|
101
|
+
def inside(self, pos):
|
|
102
|
+
if pos.shape[1] > self.dimension:
|
|
103
|
+
logger.warning(f"Converting {pos.shape[1]} to 3d using first {self.dimension} columns")
|
|
104
|
+
pos = pos[:, : self.dimension]
|
|
105
|
+
|
|
106
|
+
inside = np.ones(pos.shape[0]).astype(bool)
|
|
107
|
+
for i in range(self.dimension):
|
|
108
|
+
inside *= pos[:, i] > self.minimum[None, i]
|
|
109
|
+
inside *= pos[:, i] < self.maximum[None, i]
|
|
110
|
+
return inside
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def ncps(self):
|
|
114
|
+
"""
|
|
115
|
+
Returns the number of nodes for an element in the mesh
|
|
116
|
+
"""
|
|
117
|
+
return self.elements.shape[1]
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def nodes(self):
|
|
121
|
+
"""
|
|
122
|
+
Gets the nodes of the mesh as a property rather than using a function, accessible as a property! Python magic!
|
|
123
|
+
|
|
124
|
+
Returns
|
|
125
|
+
-------
|
|
126
|
+
nodes : np.array((N,3))
|
|
127
|
+
Fortran ordered
|
|
128
|
+
"""
|
|
129
|
+
return self.vertices
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def barycentre(self):
|
|
133
|
+
"""
|
|
134
|
+
Return the barycentres of all tetrahedrons or of specified tetras using
|
|
135
|
+
global index
|
|
136
|
+
|
|
137
|
+
Parameters
|
|
138
|
+
----------
|
|
139
|
+
elements - numpy array
|
|
140
|
+
global index
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
-------
|
|
144
|
+
|
|
145
|
+
"""
|
|
146
|
+
element_idx = np.arange(0, self.n_elements)
|
|
147
|
+
elements = self.elements[element_idx]
|
|
148
|
+
barycentre = np.sum(self.nodes[elements][:, :3, :], axis=1) / 3.0
|
|
149
|
+
return barycentre
|
|
150
|
+
|
|
151
|
+
@property
|
|
152
|
+
def shared_element_norm(self):
|
|
153
|
+
"""
|
|
154
|
+
Get the normal to all of the shared elements
|
|
155
|
+
"""
|
|
156
|
+
elements = self.shared_elements
|
|
157
|
+
v1 = self.nodes[elements[:, 1], :] - self.nodes[elements[:, 0], :]
|
|
158
|
+
norm = np.zeros_like(v1)
|
|
159
|
+
norm[:, 0] = v1[:, 1]
|
|
160
|
+
norm[:, 1] = -v1[:, 0]
|
|
161
|
+
return norm
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def shared_element_size(self):
|
|
165
|
+
"""
|
|
166
|
+
Get the size of the shared elements
|
|
167
|
+
"""
|
|
168
|
+
elements = self.shared_elements
|
|
169
|
+
v1 = self.nodes[elements[:, 1], :] - self.nodes[elements[:, 0], :]
|
|
170
|
+
return np.linalg.norm(v1, axis=1)
|
|
171
|
+
|
|
172
|
+
@property
|
|
173
|
+
def element_size(self):
|
|
174
|
+
v1 = self.nodes[self.elements[:, 1], :] - self.nodes[self.elements[:, 0], :]
|
|
175
|
+
v2 = self.nodes[self.elements[:, 2], :] - self.nodes[self.elements[:, 0], :]
|
|
176
|
+
# cross product isn't defined in 2d, numpy returns the magnitude of the orthogonal vector.
|
|
177
|
+
return 0.5 * np.cross(v1, v2, axisa=1, axisb=1)
|
|
178
|
+
|
|
179
|
+
@abstractmethod
|
|
180
|
+
def evaluate_shape(self, locations) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
181
|
+
"""
|
|
182
|
+
Evaluate the shape functions at the locations
|
|
183
|
+
|
|
184
|
+
Parameters
|
|
185
|
+
----------
|
|
186
|
+
locations - numpy array
|
|
187
|
+
locations to evaluate
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
-------
|
|
191
|
+
|
|
192
|
+
"""
|
|
193
|
+
|
|
194
|
+
def element_area(self, elements):
|
|
195
|
+
tri_points = self.nodes[self.elements[elements, :], :]
|
|
196
|
+
M_t = np.ones((tri_points.shape[0], 3, 3))
|
|
197
|
+
M_t[:, :, 1:] = tri_points[:, :3, :]
|
|
198
|
+
area = np.abs(np.linalg.det(M_t)) * 0.5
|
|
199
|
+
return area
|
|
200
|
+
|
|
201
|
+
def evaluate_value(self, evaluation_points: np.ndarray, property_array: np.ndarray):
|
|
202
|
+
"""
|
|
203
|
+
Evaluate value of interpolant
|
|
204
|
+
|
|
205
|
+
Parameters
|
|
206
|
+
----------
|
|
207
|
+
pos - numpy array
|
|
208
|
+
locations
|
|
209
|
+
prop - numpy array
|
|
210
|
+
property values at nodes
|
|
211
|
+
|
|
212
|
+
Returns
|
|
213
|
+
-------
|
|
214
|
+
|
|
215
|
+
"""
|
|
216
|
+
pos = np.asarray(evaluation_points)
|
|
217
|
+
return_values = np.zeros(pos.shape[0])
|
|
218
|
+
return_values[:] = np.nan
|
|
219
|
+
_verts, c, tri, inside = self.get_element_for_location(pos[:, :2])
|
|
220
|
+
inside = tri >= 0
|
|
221
|
+
# vertices, c, elements, inside = self.get_elements_for_location(pos)
|
|
222
|
+
return_values[inside] = np.sum(
|
|
223
|
+
c[inside, :] * property_array[self.elements[tri[inside], :]], axis=1
|
|
224
|
+
)
|
|
225
|
+
return return_values
|
|
226
|
+
|
|
227
|
+
def evaluate_gradient(self, evaluation_points, property_array):
|
|
228
|
+
"""
|
|
229
|
+
Evaluate the gradient of an interpolant at the locations
|
|
230
|
+
|
|
231
|
+
Parameters
|
|
232
|
+
----------
|
|
233
|
+
pos - numpy array
|
|
234
|
+
locations
|
|
235
|
+
prop - string
|
|
236
|
+
property to evaluate
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
Returns
|
|
240
|
+
-------
|
|
241
|
+
|
|
242
|
+
"""
|
|
243
|
+
values = np.zeros(evaluation_points.shape)
|
|
244
|
+
values[:] = np.nan
|
|
245
|
+
element_gradients, tri, inside = self.evaluate_shape_derivatives(evaluation_points[:, :2])
|
|
246
|
+
inside = tri >= 0
|
|
247
|
+
|
|
248
|
+
values[inside, :] = (
|
|
249
|
+
element_gradients[inside, :, :] * property_array[self.elements[tri[inside], :, None]]
|
|
250
|
+
).sum(1)
|
|
251
|
+
return values
|
|
252
|
+
|
|
253
|
+
def get_element_for_location(
|
|
254
|
+
self,
|
|
255
|
+
points: np.ndarray,
|
|
256
|
+
return_verts=True,
|
|
257
|
+
return_bc=True,
|
|
258
|
+
return_inside=True,
|
|
259
|
+
return_tri=True,
|
|
260
|
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
|
|
261
|
+
"""
|
|
262
|
+
Determine the elements from a numpy array of points
|
|
263
|
+
|
|
264
|
+
Parameters
|
|
265
|
+
----------
|
|
266
|
+
pos : np.array
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
Returns
|
|
271
|
+
-------
|
|
272
|
+
|
|
273
|
+
"""
|
|
274
|
+
if return_verts:
|
|
275
|
+
verts = np.zeros((points.shape[0], self.dimension + 1, self.dimension))
|
|
276
|
+
else:
|
|
277
|
+
verts = np.zeros((0, 0, 0))
|
|
278
|
+
bc = np.zeros((points.shape[0], self.dimension + 1))
|
|
279
|
+
tetras = np.zeros(points.shape[0], dtype="int64")
|
|
280
|
+
inside = np.zeros(points.shape[0], dtype=bool)
|
|
281
|
+
npts = 0
|
|
282
|
+
npts_step = int(1e4)
|
|
283
|
+
# break into blocks of 10k points
|
|
284
|
+
while npts < points.shape[0]:
|
|
285
|
+
chunk = points[npts : npts + npts_step, :]
|
|
286
|
+
cell_index, chunk_inside = self.aabb_grid.position_to_cell_index(chunk)
|
|
287
|
+
global_index = self.aabb_grid.global_cell_indices(cell_index)
|
|
288
|
+
tetra_indices = self.aabb_table[global_index[chunk_inside], :].tocoo()
|
|
289
|
+
# tetra_indices[:] = -1
|
|
290
|
+
row = tetra_indices.row
|
|
291
|
+
col = tetra_indices.col
|
|
292
|
+
# using returned indexes calculate barycentric coords to determine which tetra the points are in
|
|
293
|
+
|
|
294
|
+
vertices = self.nodes[self.elements[col, : self.dimension + 1]]
|
|
295
|
+
pos = chunk[row, : self.dimension]
|
|
296
|
+
row = tetra_indices.row
|
|
297
|
+
col = tetra_indices.col
|
|
298
|
+
# using returned indexes calculate barycentric coords to determine which tetra the points are in
|
|
299
|
+
vpa = pos[:, :] - vertices[:, 0, :]
|
|
300
|
+
vba = vertices[:, 1, :] - vertices[:, 0, :]
|
|
301
|
+
vca = vertices[:, 2, :] - vertices[:, 0, :]
|
|
302
|
+
d00 = np.einsum("ij,ij->i", vba, vba)
|
|
303
|
+
d01 = np.einsum("ij,ij->i", vba, vca)
|
|
304
|
+
d11 = np.einsum("ij,ij->i", vca, vca)
|
|
305
|
+
d20 = np.einsum("ij,ij->i", vpa, vba)
|
|
306
|
+
d21 = np.einsum("ij,ij->i", vpa, vca)
|
|
307
|
+
denom = d00 * d11 - d01 * d01
|
|
308
|
+
c = np.zeros((denom.shape[0], 3))
|
|
309
|
+
c[:, 1] = (d11 * d20 - d01 * d21) / denom
|
|
310
|
+
c[:, 2] = (d00 * d21 - d01 * d20) / denom
|
|
311
|
+
c[:, 0] = 1.0 - c[:, 1] - c[:, 2]
|
|
312
|
+
|
|
313
|
+
mask = np.all(c >= 0, axis=1)
|
|
314
|
+
if return_verts:
|
|
315
|
+
verts[npts : npts + npts_step, :, :][row[mask], :, :] = vertices[mask, :, :]
|
|
316
|
+
bc[npts : npts + npts_step, :][row[mask], :] = c[mask, :]
|
|
317
|
+
tetras[npts : npts + npts_step][row[mask]] = col[mask]
|
|
318
|
+
inside[npts : npts + npts_step][row[mask]] = True
|
|
319
|
+
npts += npts_step
|
|
320
|
+
tetra_return = np.zeros(points.shape[0]).astype(int)
|
|
321
|
+
tetra_return[:] = -1
|
|
322
|
+
tetra_return[inside] = tetras[inside]
|
|
323
|
+
return verts, bc, tetra_return, inside
|
|
324
|
+
|
|
325
|
+
def get_element_gradient_for_location(
|
|
326
|
+
self, pos: np.ndarray
|
|
327
|
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
|
|
328
|
+
"""
|
|
329
|
+
Get the element gradients for a location
|
|
330
|
+
|
|
331
|
+
Parameters
|
|
332
|
+
----------
|
|
333
|
+
pos : np.array
|
|
334
|
+
location to evaluate
|
|
335
|
+
|
|
336
|
+
Returns
|
|
337
|
+
-------
|
|
338
|
+
|
|
339
|
+
"""
|
|
340
|
+
_verts, _c, tri, _inside = self.get_element_for_location(pos, return_verts=False)
|
|
341
|
+
return self.evaluate_shape_derivatives(pos, tri)
|
|
342
|
+
|
|
343
|
+
def vtk(self, node_properties=None, cell_properties=None):
|
|
344
|
+
"""
|
|
345
|
+
Create a vtk unstructured grid from the mesh
|
|
346
|
+
"""
|
|
347
|
+
import pyvista as pv
|
|
348
|
+
|
|
349
|
+
if node_properties is None:
|
|
350
|
+
node_properties = {}
|
|
351
|
+
if cell_properties is None:
|
|
352
|
+
cell_properties = {}
|
|
353
|
+
grid = pv.UnstructuredGrid()
|
|
354
|
+
grid.points = self.nodes
|
|
355
|
+
grid.cell_types = np.ones(self.elements.shape[0]) * pv.vtk.VTK_TRIANGLE
|
|
356
|
+
grid.cells = np.c_[np.ones(self.elements.shape[0]) * 3, self.elements]
|
|
357
|
+
for key, value in node_properties.items():
|
|
358
|
+
grid.point_data[key] = value
|
|
359
|
+
for key, value in cell_properties.items():
|
|
360
|
+
grid.cell_data[key] = value
|
|
361
|
+
return grid
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Tetmesh based on cartesian grid for piecewise linear interpolation
|
|
3
|
+
"""
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
from . import SupportType
|
|
11
|
+
from ._2d_base_unstructured import BaseUnstructured2d
|
|
12
|
+
from ._2d_structured_grid import StructuredGrid2D
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class P1Unstructured2d(BaseUnstructured2d):
|
|
18
|
+
""" """
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
elements: np.ndarray | None = None,
|
|
23
|
+
vertices: np.ndarray | None = None,
|
|
24
|
+
neighbours: np.ndarray | None = None,
|
|
25
|
+
aabb_nsteps=None,
|
|
26
|
+
origin: np.ndarray | None = None,
|
|
27
|
+
step_vector: np.ndarray | None = None,
|
|
28
|
+
nsteps: np.ndarray | None = None,
|
|
29
|
+
):
|
|
30
|
+
if elements is None or vertices is None or neighbours is None:
|
|
31
|
+
if origin is None or step_vector is None or nsteps is None:
|
|
32
|
+
raise ValueError(
|
|
33
|
+
"P1Unstructured2d requires either explicit elements/vertices/"
|
|
34
|
+
"neighbours arrays, or origin/step_vector/nsteps to build a "
|
|
35
|
+
"triangular mesh over a bounding box"
|
|
36
|
+
)
|
|
37
|
+
vertices, elements, neighbours = self._build_from_bbox(origin, step_vector, nsteps)
|
|
38
|
+
BaseUnstructured2d.__init__(self, elements, vertices, neighbours, aabb_nsteps)
|
|
39
|
+
self.type = SupportType.P1Unstructured2d
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def _build_from_bbox(origin: np.ndarray, step_vector: np.ndarray, nsteps: np.ndarray):
|
|
43
|
+
"""Build a triangular mesh over a structured 2D grid by splitting
|
|
44
|
+
every grid cell into two triangles along the (bottom-left, top-right)
|
|
45
|
+
diagonal.
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
tuple of (vertices, elements, neighbours) suitable for
|
|
50
|
+
BaseUnstructured2d.__init__
|
|
51
|
+
"""
|
|
52
|
+
grid = StructuredGrid2D(origin=origin, nsteps=nsteps, step_vector=step_vector)
|
|
53
|
+
vertices = grid.nodes
|
|
54
|
+
quads = grid.elements
|
|
55
|
+
|
|
56
|
+
tri_a = quads[:, [0, 1, 2]]
|
|
57
|
+
tri_b = quads[:, [1, 3, 2]]
|
|
58
|
+
elements = np.vstack([tri_a, tri_b])
|
|
59
|
+
n_tris = elements.shape[0]
|
|
60
|
+
|
|
61
|
+
local_edges = np.array([[0, 1], [1, 2], [2, 0]])
|
|
62
|
+
edge_nodes = elements[:, local_edges]
|
|
63
|
+
edge_nodes_sorted = np.sort(edge_nodes, axis=2)
|
|
64
|
+
flat_edges = edge_nodes_sorted.reshape(-1, 2)
|
|
65
|
+
_unique_edges, inverse = np.unique(flat_edges, axis=0, return_inverse=True)
|
|
66
|
+
|
|
67
|
+
tri_ids = np.repeat(np.arange(n_tris), 3)
|
|
68
|
+
local_edge_ids = np.tile(np.arange(3), n_tris)
|
|
69
|
+
|
|
70
|
+
order = np.argsort(inverse, kind="stable")
|
|
71
|
+
sorted_inverse = inverse[order]
|
|
72
|
+
sorted_tri = tri_ids[order]
|
|
73
|
+
sorted_local = local_edge_ids[order]
|
|
74
|
+
|
|
75
|
+
same_as_next = sorted_inverse[:-1] == sorted_inverse[1:]
|
|
76
|
+
pair_idx = np.where(same_as_next)[0]
|
|
77
|
+
|
|
78
|
+
neighbours = np.full((n_tris, 3), -1, dtype=np.int64)
|
|
79
|
+
neighbours[sorted_tri[pair_idx], sorted_local[pair_idx]] = sorted_tri[pair_idx + 1]
|
|
80
|
+
neighbours[sorted_tri[pair_idx + 1], sorted_local[pair_idx + 1]] = sorted_tri[pair_idx]
|
|
81
|
+
|
|
82
|
+
return vertices, elements, neighbours
|
|
83
|
+
|
|
84
|
+
def evaluate_shape_derivatives(self, locations, elements=None):
|
|
85
|
+
"""
|
|
86
|
+
Compute dN/ds (1st row), dN/dt(2nd row)
|
|
87
|
+
"""
|
|
88
|
+
inside = None
|
|
89
|
+
if elements is not None:
|
|
90
|
+
inside = np.zeros(self.n_elements, dtype=bool)
|
|
91
|
+
inside[elements] = True
|
|
92
|
+
locations = np.array(locations)
|
|
93
|
+
if elements is None:
|
|
94
|
+
vertices, _c, tri, inside = self.get_element_for_location(locations)
|
|
95
|
+
else:
|
|
96
|
+
tri = elements
|
|
97
|
+
M = np.ones((elements.shape[0], 3, 3))
|
|
98
|
+
M[:, :, 1:] = self.vertices[self.elements[elements], :][:, :3, :]
|
|
99
|
+
points_ = np.ones((locations.shape[0], 3))
|
|
100
|
+
points_[:, 1:] = locations
|
|
101
|
+
# minv = np.linalg.inv(M)
|
|
102
|
+
# c = np.einsum("lij,li->lj", minv, points_)
|
|
103
|
+
|
|
104
|
+
vertices = self.nodes[self.elements[tri][:, :3]]
|
|
105
|
+
jac = np.zeros((tri.shape[0], 2, 2))
|
|
106
|
+
jac[:, 0, 0] = vertices[:, 1, 0] - vertices[:, 0, 0]
|
|
107
|
+
jac[:, 0, 1] = vertices[:, 1, 1] - vertices[:, 0, 1]
|
|
108
|
+
jac[:, 1, 0] = vertices[:, 2, 0] - vertices[:, 0, 0]
|
|
109
|
+
jac[:, 1, 1] = vertices[:, 2, 1] - vertices[:, 0, 1]
|
|
110
|
+
# N = np.zeros((tri.shape[0], 6))
|
|
111
|
+
|
|
112
|
+
# dN containts the derivatives of the shape functions
|
|
113
|
+
dN = np.array([[-1.0, 1.0, 0.0], [-1.0, 0.0, 1.0]])
|
|
114
|
+
|
|
115
|
+
# find the derivatives in x and y by calculating the dot product between the jacobian^-1 and the
|
|
116
|
+
# derivative matrix
|
|
117
|
+
# d_n = np.einsum('ijk,ijl->ilk',np.linalg.inv(jac),dN)
|
|
118
|
+
d_n = np.linalg.inv(jac)
|
|
119
|
+
# d_n = d_n.swapaxes(1,2)
|
|
120
|
+
d_n = d_n @ dN
|
|
121
|
+
# d_n = d_n.swapaxes(2, 1)
|
|
122
|
+
# d_n = np.dot(np.linalg.inv(jac),dN)
|
|
123
|
+
return d_n, tri, inside
|
|
124
|
+
|
|
125
|
+
def evaluate_shape(self, locations):
|
|
126
|
+
locations = np.array(locations)
|
|
127
|
+
_vertices, c, tri, inside = self.get_element_for_location(locations, return_verts=False)
|
|
128
|
+
# c = np.dot(np.array([1,x,y]),np.linalg.inv(M)) # convert to barycentric coordinates
|
|
129
|
+
# order of bary coord is (1-s-t,s,t)
|
|
130
|
+
N = c # np.zeros((c.shape[0],3)) #evaluate shape functions at barycentric coordinates
|
|
131
|
+
return N, tri, inside
|