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,546 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from loop_common.logging import get_logger as getLogger
|
|
6
|
+
|
|
7
|
+
from . import SupportType
|
|
8
|
+
|
|
9
|
+
logger = getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
from ._base_support import BaseSupport
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class LoopException(Exception):
|
|
15
|
+
"""Custom exception for LoopStructural errors."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BaseStructuredSupport(BaseSupport):
|
|
20
|
+
""" """
|
|
21
|
+
|
|
22
|
+
dimension = 3
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
origin=None,
|
|
27
|
+
nsteps=None,
|
|
28
|
+
step_vector=None,
|
|
29
|
+
rotation_xy=None,
|
|
30
|
+
):
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
Parameters
|
|
34
|
+
----------
|
|
35
|
+
origin - 3d list or numpy array
|
|
36
|
+
nsteps - 3d list or numpy array of ints
|
|
37
|
+
step_vector - 3d list or numpy array of int
|
|
38
|
+
"""
|
|
39
|
+
# the geometry in the mesh can be calculated from the
|
|
40
|
+
# nsteps, step vector and origin
|
|
41
|
+
# we use property decorators to update these when different parts of
|
|
42
|
+
# the geometry need to change
|
|
43
|
+
# inisialise the private attributes
|
|
44
|
+
# cast to numpy array, to allow list like input
|
|
45
|
+
if origin is None:
|
|
46
|
+
origin = np.zeros(3)
|
|
47
|
+
if nsteps is None:
|
|
48
|
+
nsteps = np.array([10, 10, 10])
|
|
49
|
+
if step_vector is None:
|
|
50
|
+
step_vector = np.ones(3)
|
|
51
|
+
origin = np.array(origin)
|
|
52
|
+
nsteps = np.array(nsteps)
|
|
53
|
+
step_vector = np.array(step_vector)
|
|
54
|
+
|
|
55
|
+
self.type = SupportType.BaseStructured
|
|
56
|
+
if np.any(step_vector == 0):
|
|
57
|
+
logger.warning(f"Step vector {step_vector} has zero values")
|
|
58
|
+
if np.any(nsteps == 0):
|
|
59
|
+
raise LoopException("nsteps cannot be zero")
|
|
60
|
+
if np.any(nsteps < 0):
|
|
61
|
+
raise LoopException("nsteps cannot be negative")
|
|
62
|
+
# if np.any(nsteps < 3):
|
|
63
|
+
# raise LoopException(
|
|
64
|
+
# "step vector cannot be less than 3. Try increasing the resolution of the interpolator"
|
|
65
|
+
# )
|
|
66
|
+
self._nsteps = np.array(nsteps, dtype=int) + 1
|
|
67
|
+
self._step_vector = np.array(step_vector)
|
|
68
|
+
self._origin = np.array(origin)
|
|
69
|
+
self.supporttype = "Base"
|
|
70
|
+
self._rotation_xy = np.zeros((3, 3))
|
|
71
|
+
self._rotation_xy[0, 0] = 1
|
|
72
|
+
self._rotation_xy[1, 1] = 1
|
|
73
|
+
self._rotation_xy[2, 2] = 1
|
|
74
|
+
self.rotation_xy = rotation_xy
|
|
75
|
+
self.interpolator = None
|
|
76
|
+
|
|
77
|
+
@property
|
|
78
|
+
def volume(self):
|
|
79
|
+
return np.prod(self.maximum - self.origin)
|
|
80
|
+
|
|
81
|
+
def set_nelements(self, nelements) -> int:
|
|
82
|
+
box_vol = self.volume
|
|
83
|
+
ele_vol = box_vol / nelements
|
|
84
|
+
# calculate the step vector of a regular cube
|
|
85
|
+
step_vector = np.zeros(3)
|
|
86
|
+
|
|
87
|
+
step_vector[:] = ele_vol ** (1.0 / 3.0)
|
|
88
|
+
|
|
89
|
+
# number of steps is the length of the box / step vector
|
|
90
|
+
nsteps = np.ceil((self.maximum - self.origin) / step_vector).astype(int)
|
|
91
|
+
self.nsteps = nsteps
|
|
92
|
+
return self.n_elements
|
|
93
|
+
|
|
94
|
+
def to_dict(self):
|
|
95
|
+
return {
|
|
96
|
+
"origin": self.origin,
|
|
97
|
+
"nsteps": self.nsteps,
|
|
98
|
+
"step_vector": self.step_vector,
|
|
99
|
+
"rotation_xy": self.rotation_xy,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@abstractmethod
|
|
103
|
+
def onGeometryChange(self):
|
|
104
|
+
"""Function to be called when the geometry of the support changes"""
|
|
105
|
+
|
|
106
|
+
def associateInterpolator(self, interpolator):
|
|
107
|
+
self.interpolator = interpolator
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def nsteps(self):
|
|
111
|
+
return self._nsteps
|
|
112
|
+
|
|
113
|
+
@nsteps.setter
|
|
114
|
+
def nsteps(self, nsteps):
|
|
115
|
+
# if nsteps changes we need to change the step vector
|
|
116
|
+
change_factor = nsteps / self.nsteps
|
|
117
|
+
self._step_vector /= change_factor
|
|
118
|
+
self._nsteps = nsteps
|
|
119
|
+
self.onGeometryChange()
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def nsteps_cells(self):
|
|
123
|
+
return self.nsteps - 1
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def rotation_xy(self):
|
|
127
|
+
return self._rotation_xy
|
|
128
|
+
|
|
129
|
+
@rotation_xy.setter
|
|
130
|
+
def rotation_xy(self, rotation_xy):
|
|
131
|
+
if rotation_xy is None:
|
|
132
|
+
return
|
|
133
|
+
if isinstance(rotation_xy, (float, int)):
|
|
134
|
+
rotation_xy = np.array(
|
|
135
|
+
[
|
|
136
|
+
[
|
|
137
|
+
np.cos(np.deg2rad(rotation_xy)),
|
|
138
|
+
-np.sin(np.deg2rad(rotation_xy)),
|
|
139
|
+
0,
|
|
140
|
+
],
|
|
141
|
+
[
|
|
142
|
+
np.sin(np.deg2rad(rotation_xy)),
|
|
143
|
+
np.cos(np.deg2rad(rotation_xy)),
|
|
144
|
+
0,
|
|
145
|
+
],
|
|
146
|
+
[0, 0, 1],
|
|
147
|
+
]
|
|
148
|
+
)
|
|
149
|
+
rotation_xy = np.array(rotation_xy)
|
|
150
|
+
if rotation_xy.shape != (3, 3):
|
|
151
|
+
raise ValueError(f"Rotation matrix should be 3x3, not {rotation_xy.shape}")
|
|
152
|
+
self._rotation_xy = rotation_xy
|
|
153
|
+
|
|
154
|
+
@property
|
|
155
|
+
def step_vector(self):
|
|
156
|
+
return self._step_vector
|
|
157
|
+
|
|
158
|
+
@step_vector.setter
|
|
159
|
+
def step_vector(self, step_vector):
|
|
160
|
+
change_factor = step_vector / self._step_vector
|
|
161
|
+
newsteps = self._nsteps / change_factor
|
|
162
|
+
self._nsteps = np.ceil(newsteps).astype(int)
|
|
163
|
+
self._step_vector = step_vector
|
|
164
|
+
self.onGeometryChange()
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def origin(self):
|
|
168
|
+
return self._origin
|
|
169
|
+
|
|
170
|
+
@origin.setter
|
|
171
|
+
def origin(self, origin):
|
|
172
|
+
origin = np.array(origin)
|
|
173
|
+
length = self.maximum - origin
|
|
174
|
+
length /= self.step_vector
|
|
175
|
+
self._nsteps = np.ceil(length).astype(np.int64)
|
|
176
|
+
self._nsteps[self._nsteps == 0] = (
|
|
177
|
+
3 # need to have a minimum of 3 elements to apply the finite difference mask
|
|
178
|
+
)
|
|
179
|
+
if np.any(~(self._nsteps > 0)):
|
|
180
|
+
logger.error(
|
|
181
|
+
f"Cannot resize the interpolation support. The proposed number of steps is {self._nsteps}, these must be all > 0"
|
|
182
|
+
)
|
|
183
|
+
raise ValueError("Cannot resize the interpolation support.")
|
|
184
|
+
self._origin = origin
|
|
185
|
+
self.onGeometryChange()
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def maximum(self):
|
|
189
|
+
return self.origin + self.nsteps_cells * self.step_vector
|
|
190
|
+
|
|
191
|
+
@maximum.setter
|
|
192
|
+
def maximum(self, maximum):
|
|
193
|
+
"""
|
|
194
|
+
update the number of steps to fit new boundary
|
|
195
|
+
"""
|
|
196
|
+
maximum = np.array(maximum, dtype=float)
|
|
197
|
+
length = maximum - self.origin
|
|
198
|
+
length /= self.step_vector
|
|
199
|
+
self._nsteps = np.ceil(length).astype(np.int64)
|
|
200
|
+
self._nsteps[self._nsteps == 0] = 3
|
|
201
|
+
if np.any(~(self._nsteps > 0)):
|
|
202
|
+
logger.error(
|
|
203
|
+
f"Cannot resize the interpolation support. The proposed number of steps is {self._nsteps}, these must be all > 0"
|
|
204
|
+
)
|
|
205
|
+
raise ValueError("Cannot resize the interpolation support.")
|
|
206
|
+
self.onGeometryChange()
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def n_nodes(self):
|
|
210
|
+
return np.prod(self.nsteps)
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def n_elements(self):
|
|
214
|
+
return np.prod(self.nsteps_cells)
|
|
215
|
+
|
|
216
|
+
@property
|
|
217
|
+
def elements(self):
|
|
218
|
+
global_index = np.arange(self.n_elements)
|
|
219
|
+
cell_indexes = self.global_index_to_cell_index(global_index)
|
|
220
|
+
|
|
221
|
+
return self.global_node_indices(self.cell_corner_indexes(cell_indexes))
|
|
222
|
+
|
|
223
|
+
def __str__(self):
|
|
224
|
+
return (
|
|
225
|
+
f"LoopStructural interpolation support: {self.supporttype} \n"
|
|
226
|
+
f"Origin: {self.origin[0]} {self.origin[1]} {self.origin[2]} \n"
|
|
227
|
+
f"Maximum: {self.maximum[0]} {self.maximum[1]} {self.maximum[2]} \n"
|
|
228
|
+
f"Step Vector: {self.step_vector[0]} {self.step_vector[1]} {self.step_vector[2]} \n"
|
|
229
|
+
f"Number of Steps: {self.nsteps[0]} {self.nsteps[1]} {self.nsteps[2]} \n"
|
|
230
|
+
f"Degrees of freedon {self.n_nodes}"
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
def nodes(self):
|
|
235
|
+
max = self.origin + self.nsteps_cells * self.step_vector
|
|
236
|
+
if np.any(np.isnan(self.nsteps)):
|
|
237
|
+
raise ValueError("Cannot resize mesh nsteps is NaN")
|
|
238
|
+
if np.any(np.isnan(self.origin)):
|
|
239
|
+
raise ValueError("Cannot resize mesh origin is NaN")
|
|
240
|
+
|
|
241
|
+
x = np.linspace(self.origin[0], max[0], self.nsteps[0])
|
|
242
|
+
y = np.linspace(self.origin[1], max[1], self.nsteps[1])
|
|
243
|
+
z = np.linspace(self.origin[2], max[2], self.nsteps[2])
|
|
244
|
+
xx, yy, zz = np.meshgrid(x, y, z, indexing="ij")
|
|
245
|
+
return np.array([xx.flatten(order="F"), yy.flatten(order="F"), zz.flatten(order="F")]).T
|
|
246
|
+
|
|
247
|
+
def rotate(self, pos):
|
|
248
|
+
""" """
|
|
249
|
+
return np.einsum("ijk,ik->ij", self.rotation_xy[None, :, :], pos)
|
|
250
|
+
|
|
251
|
+
def position_to_cell_index(self, pos: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
|
252
|
+
"""Get the indexes (i,j,k) of a cell
|
|
253
|
+
that a point is inside
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
Parameters
|
|
257
|
+
----------
|
|
258
|
+
pos : np.array
|
|
259
|
+
Nx3 array of xyz locations
|
|
260
|
+
|
|
261
|
+
Returns
|
|
262
|
+
-------
|
|
263
|
+
np.ndarray
|
|
264
|
+
N,3 i,j,k indexes of the cell that the point is in
|
|
265
|
+
"""
|
|
266
|
+
pos = self.check_position(pos)
|
|
267
|
+
inside = self.inside(pos)
|
|
268
|
+
cell_indexes = np.zeros((pos.shape[0], 3), dtype=int)
|
|
269
|
+
|
|
270
|
+
x = pos[:, 0] - self.origin[None, 0]
|
|
271
|
+
y = pos[:, 1] - self.origin[None, 1]
|
|
272
|
+
z = pos[:, 2] - self.origin[None, 2]
|
|
273
|
+
cell_indexes[inside, 0] = np.floor(x[inside] / self.step_vector[None, 0]).astype(int)
|
|
274
|
+
cell_indexes[inside, 1] = np.floor(y[inside] / self.step_vector[None, 1]).astype(int)
|
|
275
|
+
cell_indexes[inside, 2] = np.floor(z[inside] / self.step_vector[None, 2]).astype(int)
|
|
276
|
+
|
|
277
|
+
# Boundary points at maximum extents belong to the last cell.
|
|
278
|
+
cell_indexes[inside, 0] = np.clip(cell_indexes[inside, 0], 0, self.nsteps_cells[0] - 1)
|
|
279
|
+
cell_indexes[inside, 1] = np.clip(cell_indexes[inside, 1], 0, self.nsteps_cells[1] - 1)
|
|
280
|
+
cell_indexes[inside, 2] = np.clip(cell_indexes[inside, 2], 0, self.nsteps_cells[2] - 1)
|
|
281
|
+
|
|
282
|
+
return cell_indexes, inside
|
|
283
|
+
|
|
284
|
+
def position_to_cell_global_index(self, pos):
|
|
285
|
+
_ix, _iy, _iz = self.position_to_cell_index(pos)
|
|
286
|
+
|
|
287
|
+
def inside(self, pos):
|
|
288
|
+
# check whether point is inside box
|
|
289
|
+
pos = self.check_position(pos)
|
|
290
|
+
extent = np.maximum(np.abs(self.maximum - self.origin), 1.0)
|
|
291
|
+
tol = np.finfo(float).eps * 32.0 * extent
|
|
292
|
+
inside = np.all((pos >= (self.origin - tol)) & (pos <= (self.maximum + tol)), axis=1)
|
|
293
|
+
return inside
|
|
294
|
+
|
|
295
|
+
def check_position(self, pos: np.ndarray) -> np.ndarray:
|
|
296
|
+
"""[summary]
|
|
297
|
+
|
|
298
|
+
[extended_summary]
|
|
299
|
+
|
|
300
|
+
Parameters
|
|
301
|
+
----------
|
|
302
|
+
pos : [type]
|
|
303
|
+
[description]
|
|
304
|
+
|
|
305
|
+
Returns
|
|
306
|
+
-------
|
|
307
|
+
[type]
|
|
308
|
+
[description]
|
|
309
|
+
"""
|
|
310
|
+
if not isinstance(pos, np.ndarray):
|
|
311
|
+
try:
|
|
312
|
+
pos = np.array(pos, dtype=float)
|
|
313
|
+
except Exception as e:
|
|
314
|
+
logger.error(
|
|
315
|
+
f"Position array should be a numpy array or list of points, not {type(pos)}"
|
|
316
|
+
)
|
|
317
|
+
raise ValueError(
|
|
318
|
+
f"Position array should be a numpy array or list of points, not {type(pos)}"
|
|
319
|
+
) from e
|
|
320
|
+
|
|
321
|
+
if len(pos.shape) == 1:
|
|
322
|
+
pos = np.array([pos])
|
|
323
|
+
if len(pos.shape) != 2:
|
|
324
|
+
logger.error("Position array needs to be a list of points or a point")
|
|
325
|
+
raise ValueError("Position array needs to be a list of points or a point")
|
|
326
|
+
return pos
|
|
327
|
+
|
|
328
|
+
def _global_indicies(self, indexes: np.ndarray, nsteps: np.ndarray) -> np.ndarray:
|
|
329
|
+
"""
|
|
330
|
+
Convert from cell indexes to global cell index
|
|
331
|
+
|
|
332
|
+
Parameters
|
|
333
|
+
----------
|
|
334
|
+
indexes
|
|
335
|
+
|
|
336
|
+
Returns
|
|
337
|
+
-------
|
|
338
|
+
|
|
339
|
+
"""
|
|
340
|
+
if len(indexes.shape) == 1:
|
|
341
|
+
raise ValueError("Cell indexes needs to be Nx3")
|
|
342
|
+
if indexes.shape[-1] != 3:
|
|
343
|
+
raise ValueError("Last dimensions should be ijk indexing")
|
|
344
|
+
original_shape = indexes.shape
|
|
345
|
+
indexes = indexes.reshape(-1, 3)
|
|
346
|
+
gi = (
|
|
347
|
+
indexes[:, 0]
|
|
348
|
+
+ nsteps[None, 0] * indexes[:, 1]
|
|
349
|
+
+ nsteps[None, 0] * nsteps[None, 1] * indexes[:, 2]
|
|
350
|
+
)
|
|
351
|
+
return gi.reshape(original_shape[:-1])
|
|
352
|
+
|
|
353
|
+
def cell_corner_indexes(self, cell_indexes: np.ndarray) -> np.ndarray:
|
|
354
|
+
"""
|
|
355
|
+
Returns the indexes of the corners of a cell given its location xi,
|
|
356
|
+
yi, zi
|
|
357
|
+
|
|
358
|
+
Parameters
|
|
359
|
+
----------
|
|
360
|
+
x_cell_index
|
|
361
|
+
y_cell_index
|
|
362
|
+
z_cell_index
|
|
363
|
+
|
|
364
|
+
Returns
|
|
365
|
+
-------
|
|
366
|
+
|
|
367
|
+
"""
|
|
368
|
+
|
|
369
|
+
corner_indexes = np.zeros((cell_indexes.shape[0], 8, 3), dtype=int)
|
|
370
|
+
|
|
371
|
+
xcorner = np.array([0, 1, 0, 1, 0, 1, 0, 1])
|
|
372
|
+
ycorner = np.array([0, 0, 1, 1, 0, 0, 1, 1])
|
|
373
|
+
zcorner = np.array([0, 0, 0, 0, 1, 1, 1, 1])
|
|
374
|
+
corner_indexes[:, :, 0] = (
|
|
375
|
+
cell_indexes[:, None, 0] + corner_indexes[:, :, 0] + xcorner[None, :]
|
|
376
|
+
)
|
|
377
|
+
corner_indexes[:, :, 1] = (
|
|
378
|
+
cell_indexes[:, None, 1] + corner_indexes[:, :, 1] + ycorner[None, :]
|
|
379
|
+
)
|
|
380
|
+
corner_indexes[:, :, 2] = (
|
|
381
|
+
cell_indexes[:, None, 2] + corner_indexes[:, :, 2] + zcorner[None, :]
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
return corner_indexes
|
|
385
|
+
|
|
386
|
+
def position_to_cell_corners(self, pos):
|
|
387
|
+
"""Get the global indices of the vertices (corners) of the cell containing each point.
|
|
388
|
+
|
|
389
|
+
Parameters
|
|
390
|
+
----------
|
|
391
|
+
pos : np.array
|
|
392
|
+
(N, 3) array of xyz coordinates representing the positions of N points.
|
|
393
|
+
|
|
394
|
+
Returns
|
|
395
|
+
-------
|
|
396
|
+
globalidx : np.array
|
|
397
|
+
(N, 8) array of global indices corresponding to the 8 corner nodes of the cell
|
|
398
|
+
each point lies in. If a point lies outside the support, its corresponding entry
|
|
399
|
+
will be set to -1.
|
|
400
|
+
inside : np.array
|
|
401
|
+
(N,) boolean array indicating whether each point is inside the support domain.
|
|
402
|
+
"""
|
|
403
|
+
cell_indexes, inside = self.position_to_cell_index(pos)
|
|
404
|
+
nx, ny = self.nsteps[0], self.nsteps[1]
|
|
405
|
+
offsets = np.array(
|
|
406
|
+
[0, 1, nx, nx + 1, nx * ny, nx * ny + 1, nx * ny + nx, nx * ny + nx + 1],
|
|
407
|
+
dtype=np.intp,
|
|
408
|
+
)
|
|
409
|
+
g = cell_indexes[:, 0] + nx * cell_indexes[:, 1] + nx * ny * cell_indexes[:, 2]
|
|
410
|
+
globalidx = g[:, None] + offsets[None, :] # (N, 8)
|
|
411
|
+
globalidx[~inside] = -1
|
|
412
|
+
return globalidx, inside
|
|
413
|
+
|
|
414
|
+
def position_to_cell_vertices(self, pos):
|
|
415
|
+
"""Get the vertices of the cell a point is in
|
|
416
|
+
|
|
417
|
+
Parameters
|
|
418
|
+
----------
|
|
419
|
+
pos : np.array
|
|
420
|
+
Nx3 array of xyz locations
|
|
421
|
+
|
|
422
|
+
Returns
|
|
423
|
+
-------
|
|
424
|
+
np.array((N,3),dtype=float), np.array(N,dtype=int)
|
|
425
|
+
vertices, inside
|
|
426
|
+
"""
|
|
427
|
+
gi, inside = self.position_to_cell_corners(pos)
|
|
428
|
+
node_indexes = self.global_index_to_node_index(gi)
|
|
429
|
+
return self.node_indexes_to_position(node_indexes), inside
|
|
430
|
+
|
|
431
|
+
def node_indexes_to_position(self, node_indexes: np.ndarray) -> np.ndarray:
|
|
432
|
+
original_shape = node_indexes.shape
|
|
433
|
+
node_indexes = node_indexes.reshape((-1, 3))
|
|
434
|
+
xyz = np.zeros((node_indexes.shape[0], 3), dtype=float)
|
|
435
|
+
xyz[:, 0] = self.origin[0] + self.step_vector[0] * node_indexes[:, 0]
|
|
436
|
+
xyz[:, 1] = self.origin[1] + self.step_vector[1] * node_indexes[:, 1]
|
|
437
|
+
xyz[:, 2] = self.origin[2] + self.step_vector[2] * node_indexes[:, 2]
|
|
438
|
+
xyz = xyz.reshape(original_shape)
|
|
439
|
+
return xyz
|
|
440
|
+
|
|
441
|
+
def global_index_to_cell_index(self, global_index):
|
|
442
|
+
"""
|
|
443
|
+
Convert from global indexes to xi,yi,zi
|
|
444
|
+
|
|
445
|
+
Parameters
|
|
446
|
+
----------
|
|
447
|
+
global_index
|
|
448
|
+
|
|
449
|
+
Returns
|
|
450
|
+
-------
|
|
451
|
+
|
|
452
|
+
"""
|
|
453
|
+
# determine the ijk indices for the global index.
|
|
454
|
+
# remainder when dividing by nx = i
|
|
455
|
+
# remained when dividing modulus of nx by ny is j
|
|
456
|
+
cell_indexes = np.zeros((global_index.shape[0], 3), dtype=int)
|
|
457
|
+
cell_indexes[:, 0] = global_index % self.nsteps_cells[0, None]
|
|
458
|
+
cell_indexes[:, 1] = global_index // self.nsteps_cells[0, None] % self.nsteps_cells[1, None]
|
|
459
|
+
cell_indexes[:, 2] = (
|
|
460
|
+
global_index // self.nsteps_cells[0, None] // self.nsteps_cells[1, None]
|
|
461
|
+
)
|
|
462
|
+
return cell_indexes
|
|
463
|
+
|
|
464
|
+
def global_index_to_node_index(self, global_index):
|
|
465
|
+
"""
|
|
466
|
+
Convert from global indexes to xi,yi,zi
|
|
467
|
+
|
|
468
|
+
Parameters
|
|
469
|
+
----------
|
|
470
|
+
global_index
|
|
471
|
+
|
|
472
|
+
Returns
|
|
473
|
+
-------
|
|
474
|
+
|
|
475
|
+
"""
|
|
476
|
+
# determine the ijk indices for the global index.
|
|
477
|
+
# remainder when dividing by nx = i
|
|
478
|
+
# remained when dividing modulus of nx by ny is j
|
|
479
|
+
original_shape = global_index.shape
|
|
480
|
+
global_index = global_index.reshape(-1)
|
|
481
|
+
local_indexes = np.zeros((global_index.shape[0], 3), dtype=int)
|
|
482
|
+
local_indexes[:, 0] = global_index % self.nsteps[0, None]
|
|
483
|
+
local_indexes[:, 1] = global_index // self.nsteps[0, None] % self.nsteps[1, None]
|
|
484
|
+
local_indexes[:, 2] = global_index // self.nsteps[0, None] // self.nsteps[1, None]
|
|
485
|
+
return local_indexes.reshape(*original_shape, 3)
|
|
486
|
+
|
|
487
|
+
def global_node_indices(self, indexes) -> np.ndarray:
|
|
488
|
+
"""
|
|
489
|
+
Convert from node indexes to global node index
|
|
490
|
+
|
|
491
|
+
Parameters
|
|
492
|
+
----------
|
|
493
|
+
indexes
|
|
494
|
+
|
|
495
|
+
Returns
|
|
496
|
+
-------
|
|
497
|
+
|
|
498
|
+
"""
|
|
499
|
+
return self._global_indicies(indexes, self.nsteps)
|
|
500
|
+
|
|
501
|
+
def global_cell_indices(self, indexes) -> np.ndarray:
|
|
502
|
+
"""
|
|
503
|
+
Convert from cell indexes to global cell index
|
|
504
|
+
|
|
505
|
+
Parameters
|
|
506
|
+
----------
|
|
507
|
+
indexes
|
|
508
|
+
|
|
509
|
+
Returns
|
|
510
|
+
-------
|
|
511
|
+
|
|
512
|
+
"""
|
|
513
|
+
return self._global_indicies(indexes, self.nsteps_cells)
|
|
514
|
+
|
|
515
|
+
@property
|
|
516
|
+
def element_size(self):
|
|
517
|
+
return np.prod(self.step_vector)
|
|
518
|
+
|
|
519
|
+
@property
|
|
520
|
+
def element_scale(self):
|
|
521
|
+
# all elements are the same size
|
|
522
|
+
return 1.0
|
|
523
|
+
|
|
524
|
+
def vtk(self, node_properties=None, cell_properties=None):
|
|
525
|
+
try:
|
|
526
|
+
import pyvista as pv
|
|
527
|
+
except ImportError:
|
|
528
|
+
raise ImportError("pyvista is required for vtk support")
|
|
529
|
+
|
|
530
|
+
if node_properties is None:
|
|
531
|
+
node_properties = {}
|
|
532
|
+
if cell_properties is None:
|
|
533
|
+
cell_properties = {}
|
|
534
|
+
from pyvista import CellType
|
|
535
|
+
|
|
536
|
+
celltype = np.full(self.n_elements, CellType.VOXEL, dtype=np.uint8)
|
|
537
|
+
elements = np.hstack(
|
|
538
|
+
[np.zeros(self.elements.shape[0], dtype=int)[:, None] + 8, self.elements]
|
|
539
|
+
)
|
|
540
|
+
elements = elements.flatten()
|
|
541
|
+
grid = pv.UnstructuredGrid(elements, celltype, self.nodes)
|
|
542
|
+
for key, value in node_properties.items():
|
|
543
|
+
grid[key] = value
|
|
544
|
+
for key, value in cell_properties.items():
|
|
545
|
+
grid.cell_arrays[key] = value
|
|
546
|
+
return grid
|