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.
Files changed (50) hide show
  1. loop_common/__init__.py +6 -0
  2. loop_common/base.py +98 -0
  3. loop_common/geometry/__init__.py +19 -0
  4. loop_common/geometry/_aabb.py +37 -0
  5. loop_common/geometry/_bounding_box.py +792 -0
  6. loop_common/geometry/_face_table.py +42 -0
  7. loop_common/geometry/_point.py +238 -0
  8. loop_common/geometry/_structured_grid.py +114 -0
  9. loop_common/geometry/_structured_grid_2d.py +146 -0
  10. loop_common/geometry/_structured_grid_3d.py +243 -0
  11. loop_common/geometry/_surface.py +288 -0
  12. loop_common/geometry/_unstructured_mesh.py +208 -0
  13. loop_common/interfaces/__init__.py +0 -0
  14. loop_common/interfaces/representation.py +45 -0
  15. loop_common/io/__init__.py +0 -0
  16. loop_common/logging/__init__.py +14 -0
  17. loop_common/logging/logger.py +123 -0
  18. loop_common/logging/sinks.py +221 -0
  19. loop_common/logging/timing.py +113 -0
  20. loop_common/math/__init__.py +5 -0
  21. loop_common/math/_maths.py +441 -0
  22. loop_common/math/_transformation.py +178 -0
  23. loop_common/math/finite_difference_stencil.py +38 -0
  24. loop_common/observations/__init__.py +3 -0
  25. loop_common/observations/lineset.py +41 -0
  26. loop_common/observations/orientation.py +122 -0
  27. loop_common/observations/pointset.py +8 -0
  28. loop_common/observer.py +284 -0
  29. loop_common/supports/_2d_base_unstructured.py +361 -0
  30. loop_common/supports/_2d_p1_unstructured.py +131 -0
  31. loop_common/supports/_2d_p2_unstructured.py +349 -0
  32. loop_common/supports/_2d_structured_grid.py +545 -0
  33. loop_common/supports/_3d_base_structured.py +546 -0
  34. loop_common/supports/_3d_p2_tetra.py +374 -0
  35. loop_common/supports/_3d_rectilinear_grid.py +315 -0
  36. loop_common/supports/_3d_structured_grid.py +510 -0
  37. loop_common/supports/_3d_structured_tetra.py +764 -0
  38. loop_common/supports/_3d_unstructured_tetra.py +656 -0
  39. loop_common/supports/__init__.py +69 -0
  40. loop_common/supports/_aabb.py +77 -0
  41. loop_common/supports/_base_support.py +118 -0
  42. loop_common/supports/_face_table.py +70 -0
  43. loop_common/supports/_p2_structured_tetra.py +627 -0
  44. loop_common/supports/_support_factory.py +78 -0
  45. loop_common/utils.py +74 -0
  46. loop_common-0.0.1.dist-info/METADATA +35 -0
  47. loop_common-0.0.1.dist-info/RECORD +50 -0
  48. loop_common-0.0.1.dist-info/WHEEL +5 -0
  49. loop_common-0.0.1.dist-info/licenses/LICENSE +21 -0
  50. loop_common-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,510 @@
1
+ """
2
+ Cartesian grid for fold interpolator
3
+
4
+ """
5
+
6
+
7
+ import numpy as np
8
+
9
+ from loop_common.logging import get_logger as getLogger
10
+
11
+ from ..math.finite_difference_stencil import Operator
12
+ from . import SupportType
13
+ from ._3d_base_structured import BaseStructuredSupport
14
+
15
+ logger = getLogger(__name__)
16
+
17
+
18
+ class StructuredGrid(BaseStructuredSupport):
19
+ """ """
20
+
21
+ def __init__(
22
+ self,
23
+ origin=None,
24
+ nsteps_cells=None,
25
+ step_vector=None,
26
+ nsteps=None,
27
+ rotation_xy=None,
28
+ properties=None,
29
+ cell_properties=None,
30
+ name="StructuredGrid",
31
+ ):
32
+ """
33
+
34
+ Parameters
35
+ ----------
36
+ origin : array-like, optional
37
+ Grid origin.
38
+ nsteps : array-like, optional
39
+ Legacy node-count alias. Treated as cell counts for compatibility.
40
+ nsteps_cells : array-like, optional
41
+ Number of cells in each direction.
42
+ step_vector : array-like, optional
43
+ Cell size in each direction.
44
+ """
45
+ if origin is None:
46
+ origin = np.zeros(3)
47
+ if step_vector is None:
48
+ step_vector = np.ones(3)
49
+ if nsteps_cells is None:
50
+ nsteps_cells = np.array([10, 10, 10]) if nsteps is None else np.array(nsteps)
51
+ BaseStructuredSupport.__init__(
52
+ self,
53
+ origin,
54
+ nsteps_cells,
55
+ step_vector,
56
+ rotation_xy=rotation_xy,
57
+ )
58
+ self.type = SupportType.StructuredGrid
59
+ self.regions = {}
60
+ self.regions["everywhere"] = np.ones(self.n_nodes).astype(bool)
61
+
62
+ def onGeometryChange(self):
63
+ if self.interpolator is not None:
64
+ self.interpolator.reset()
65
+
66
+ @property
67
+ def barycentre(self):
68
+ return self.cell_centres(np.arange(self.n_elements))
69
+
70
+ def cell_centres(self, global_index):
71
+ """Get the centre of specified cells
72
+
73
+ Parameters
74
+ ----------
75
+ global_index : array/list
76
+ container of integer global indexes to cells
77
+
78
+ Returns
79
+ -------
80
+ numpy array
81
+ Nx3 array of cell centres
82
+ """
83
+ cell_indexes = self.global_index_to_cell_index(global_index)
84
+ return (
85
+ self.origin[None, :]
86
+ + self.step_vector[None, :] * (cell_indexes)
87
+ + self.step_vector[None, :] * 0.5
88
+ )
89
+
90
+ def trilinear(self, local_coords):
91
+ """
92
+ Returns the trilinear interpolation for the local coordinates
93
+
94
+ Parameters
95
+ ----------
96
+ x - double, array of doubles
97
+ y - double, array of doubles
98
+ z - double, array of doubles
99
+
100
+ Returns
101
+ -------
102
+ array of interpolation coefficients
103
+
104
+ """
105
+ interpolant = np.zeros((local_coords.shape[0], 8), dtype=np.float64)
106
+ # interpolant[:,0] = (1 - local_coords[:, 0])* (1 - local_coords[:, 1])* (1 - local_coords[:, 2])
107
+ # interpolant[:,1] = local_coords[:, 0]* (1 - local_coords[:, 1])* (1 - local_coords[:, 2])
108
+ # interpolant[:,2] = (1 - local_coords[:, 0])* local_coords[:, 1]* (1 - local_coords[:, 2])
109
+ # interpolant[:,3] = (1 - local_coords[:, 0])* (1 - local_coords[:, 1])* local_coords[:, 2]
110
+ # interpolant[:,4] = local_coords[:, 0] * (1 - local_coords[:, 1]) * local_coords[:, 2]
111
+ # interpolant[:,5] = (1 - local_coords[:, 0]) * local_coords[:, 1] * local_coords[:, 2]
112
+ # interpolant[:,6] = local_coords[:, 0] * local_coords[:, 1] * (1 - local_coords[:, 2])
113
+ # interpolant[:,7] = local_coords[:, 0] * local_coords[:, 1] * local_coords[:, 2]
114
+ interpolant[:, 0] = (
115
+ (1 - local_coords[:, 0]) * (1 - local_coords[:, 1]) * (1 - local_coords[:, 2])
116
+ )
117
+ interpolant[:, 1] = local_coords[:, 0] * (1 - local_coords[:, 1]) * (1 - local_coords[:, 2])
118
+ interpolant[:, 2] = (1 - local_coords[:, 0]) * local_coords[:, 1] * (1 - local_coords[:, 2])
119
+ interpolant[:, 4] = (1 - local_coords[:, 0]) * (1 - local_coords[:, 1]) * local_coords[:, 2]
120
+ interpolant[:, 5] = local_coords[:, 0] * (1 - local_coords[:, 1]) * local_coords[:, 2]
121
+ interpolant[:, 6] = (1 - local_coords[:, 0]) * local_coords[:, 1] * local_coords[:, 2]
122
+ interpolant[:, 3] = local_coords[:, 0] * local_coords[:, 1] * (1 - local_coords[:, 2])
123
+ interpolant[:, 7] = local_coords[:, 0] * local_coords[:, 1] * local_coords[:, 2]
124
+ return interpolant
125
+
126
+ def position_to_local_coordinates(self, pos):
127
+ """
128
+ Convert from global to local coordinates within a cel
129
+
130
+ Parameters
131
+ ----------
132
+ pos - array of positions inside
133
+
134
+ Returns
135
+ -------
136
+ localx, localy, localz
137
+
138
+ """
139
+ pos = self.check_position(pos)
140
+ cell_indexes, _inside = self.position_to_cell_index(pos)
141
+
142
+ # Use cell origin (not modulo) so points on max faces/vertices map to local=1.
143
+ cell_origin = self.origin[None, :] + self.step_vector[None, :] * cell_indexes
144
+ local_coords = (pos - cell_origin) / self.step_vector[None, :]
145
+ local_coords = np.clip(local_coords, 0.0, 1.0)
146
+ return local_coords
147
+
148
+ def position_to_dof_coefs(self, pos):
149
+ """
150
+ Global posotion to interpolation coefficients
151
+
152
+ Parameters
153
+ ----------
154
+ pos
155
+
156
+ Returns
157
+ -------
158
+
159
+ """
160
+ local_coords = self.position_to_local_coordinates(pos)
161
+ weights = self.trilinear(local_coords)
162
+ return weights
163
+
164
+ def neighbour_global_indexes(self, mask=None, **kwargs):
165
+ """
166
+ Get neighbour indexes
167
+
168
+ Parameters
169
+ ----------
170
+ kwargs - indexes array specifying the cells to return neighbours
171
+
172
+ Returns
173
+ -------
174
+
175
+ """
176
+ indexes = None
177
+ if "indexes" in kwargs:
178
+ indexes = kwargs["indexes"]
179
+ if "indexes" not in kwargs:
180
+ gi = np.arange(self.n_nodes)
181
+ indexes = self.global_index_to_node_index(gi)
182
+ edge_mask = (
183
+ (indexes[:, 0] > 0)
184
+ & (indexes[:, 0] < self.nsteps[0] - 1)
185
+ & (indexes[:, 1] > 0)
186
+ & (indexes[:, 1] < self.nsteps[1] - 1)
187
+ & (indexes[:, 2] > 0)
188
+ & (indexes[:, 2] < self.nsteps[2] - 1)
189
+ )
190
+ indexes = indexes[edge_mask, :].T
191
+
192
+ # indexes = np.array(indexes).T
193
+ if indexes.ndim != 2:
194
+ return
195
+ # determine which neighbours to return default is diagonals included.
196
+ if mask is None:
197
+ mask = np.array(
198
+ [
199
+ [
200
+ -1,
201
+ 0,
202
+ 1,
203
+ -1,
204
+ 0,
205
+ 1,
206
+ -1,
207
+ 0,
208
+ 1,
209
+ -1,
210
+ 0,
211
+ 1,
212
+ -1,
213
+ 0,
214
+ 1,
215
+ -1,
216
+ 0,
217
+ 1,
218
+ -1,
219
+ 0,
220
+ 1,
221
+ -1,
222
+ 0,
223
+ 1,
224
+ -1,
225
+ 0,
226
+ 1,
227
+ ],
228
+ [
229
+ -1,
230
+ -1,
231
+ -1,
232
+ 0,
233
+ 0,
234
+ 0,
235
+ 1,
236
+ 1,
237
+ 1,
238
+ -1,
239
+ -1,
240
+ -1,
241
+ 0,
242
+ 0,
243
+ 0,
244
+ 1,
245
+ 1,
246
+ 1,
247
+ -1,
248
+ -1,
249
+ -1,
250
+ 0,
251
+ 0,
252
+ 0,
253
+ 1,
254
+ 1,
255
+ 1,
256
+ ],
257
+ [
258
+ -1,
259
+ -1,
260
+ -1,
261
+ -1,
262
+ -1,
263
+ -1,
264
+ -1,
265
+ -1,
266
+ -1,
267
+ 0,
268
+ 0,
269
+ 0,
270
+ 0,
271
+ 0,
272
+ 0,
273
+ 0,
274
+ 0,
275
+ 0,
276
+ 1,
277
+ 1,
278
+ 1,
279
+ 1,
280
+ 1,
281
+ 1,
282
+ 1,
283
+ 1,
284
+ 1,
285
+ ],
286
+ ]
287
+ )
288
+ neighbours = indexes[:, None, :] + mask[:, :, None]
289
+ return (
290
+ neighbours[0, :, :]
291
+ + self.nsteps[0, None, None] * neighbours[1, :, :]
292
+ + self.nsteps[0, None, None] * self.nsteps[1, None, None] * neighbours[2, :, :]
293
+ ).astype(np.int64)
294
+
295
+ def evaluate_value(self, evaluation_points, property_array):
296
+ """
297
+ Evaluate the value of of the property at the locations.
298
+ Trilinear interpolation dot corner values
299
+
300
+ Parameters
301
+ ----------
302
+ evaluation_points np array of locations
303
+ property_name string of property name
304
+
305
+ Returns
306
+ -------
307
+
308
+ """
309
+ if property_array.shape[0] != self.n_nodes:
310
+ logger.error("Property array does not match grid")
311
+ raise ValueError(
312
+ f"cannot assign {property_array.shape[0]} vlaues to array of shape {self.n_nodes}"
313
+ )
314
+ idc, inside = self.position_to_cell_corners(evaluation_points)
315
+ # print(idc[inside,:], self.n_nodes,inside)
316
+ if idc.shape[0] != inside.shape[0]:
317
+ raise ValueError("index does not match number of nodes")
318
+ v = np.zeros(idc.shape)
319
+ v[:, :] = np.nan
320
+ v[inside, :] = self.position_to_dof_coefs(evaluation_points[inside, :])
321
+ v[inside, :] *= property_array[idc[inside, :]]
322
+ return np.sum(v, axis=1)
323
+
324
+ def evaluate_gradient(self, evaluation_points, property_array) -> np.ndarray:
325
+ """Evaluate the gradient at a location given node values
326
+
327
+ Parameters
328
+ ----------
329
+ evaluation_points : np.array((N,3))
330
+ locations
331
+ property_array : np.array((self.nx))
332
+ value node, has to be the same length as the number of nodes
333
+
334
+ Returns
335
+ -------
336
+ np.array((N,3),dtype=float)
337
+ gradient of the implicit function at the locations
338
+
339
+ Raises
340
+ ------
341
+ ValueError
342
+ if the array is not the same shape as the number of nodes
343
+
344
+ Notes
345
+ -----
346
+ The implicit function gradient is not normalised, to convert to
347
+ a unit vector normalise using vector/=np.linalg.norm(vector,axis=1)[:,None]
348
+ """
349
+ if property_array.shape[0] != self.n_nodes:
350
+ logger.error("Property array does not match grid")
351
+ raise ValueError(
352
+ f"cannot assign {property_array.shape[0]} vlaues to array of shape {self.n_nodes}"
353
+ )
354
+
355
+ idc, inside = self.position_to_cell_corners(evaluation_points)
356
+ T = np.zeros((idc.shape[0], 3, 8))
357
+ T[inside, :, :] = self.get_element_gradient_for_location(evaluation_points[inside, :])[1]
358
+ if np.any(inside) and np.max(idc[inside, :]) > property_array.shape[0]:
359
+ cix, ciy, ciz = self.position_to_cell_index(evaluation_points)
360
+ if not np.all(cix[inside] < self.nsteps_cells[0]):
361
+ print(
362
+ evaluation_points[inside, :][cix[inside] < self.nsteps_cells[0], 0],
363
+ self.origin[0],
364
+ self.maximum[0],
365
+ )
366
+ if not np.all(ciy[inside] < self.nsteps_cells[1]):
367
+ print(
368
+ evaluation_points[inside, :][ciy[inside] < self.nsteps_cells[1], 1],
369
+ self.origin[1],
370
+ self.maximum[1],
371
+ )
372
+ if not np.all(ciz[inside] < self.nsteps_cells[2]):
373
+ print(ciz[inside], self.nsteps_cells[2])
374
+ print(self.step_vector, self.nsteps_cells, self.nsteps)
375
+ print(
376
+ evaluation_points[inside, :][~(ciz[inside] < self.nsteps_cells[2]), 2],
377
+ self.origin[2],
378
+ self.maximum[2],
379
+ )
380
+
381
+ raise ValueError("index does not match number of nodes")
382
+ T[inside, 0, :] *= property_array[idc[inside, :]]
383
+ T[inside, 1, :] *= property_array[idc[inside, :]]
384
+ T[inside, 2, :] *= property_array[idc[inside, :]]
385
+ return np.array(
386
+ [
387
+ np.sum(T[:, 0, :], axis=1),
388
+ np.sum(T[:, 1, :], axis=1),
389
+ np.sum(T[:, 2, :], axis=1),
390
+ ]
391
+ ).T
392
+
393
+ def get_element_gradient_for_location(self, pos: np.ndarray):
394
+ """
395
+ Get the gradient of the element at the locations.
396
+
397
+ Parameters
398
+ ----------
399
+ pos : np.array((N,3),dtype=float)
400
+ locations
401
+
402
+ Returns
403
+ -------
404
+ vertices, gradient, element, inside
405
+ [description]
406
+ """
407
+ # 6_ _ _ _ 8
408
+ # /| /|
409
+ # 4 /_| 5/ |
410
+ # | 2|_ _|_| 7
411
+ # | / | /
412
+ # |/_ _ _|/
413
+ # 0 1
414
+ #
415
+ # xindex, yindex, zindex = self.position_to_cell_index(pos)
416
+ # cellx, celly, cellz = self.cell_corner_indexes(xindex, yindex,zindex)
417
+ # x, y, z = self.node_indexes_to_position(cellx, celly, cellz)
418
+ pos = np.asarray(pos)
419
+ T = np.zeros((pos.shape[0], 3, 8))
420
+ local_coords = self.position_to_local_coordinates(pos)
421
+ vertices, inside = self.position_to_cell_vertices(pos)
422
+ elements, inside = self.position_to_cell_index(pos)
423
+ elements = self.global_cell_indices(elements)
424
+
425
+ T[:, 0, 0] = (1 - local_coords[:, 2]) * (local_coords[:, 1] - 1) # v000
426
+ T[:, 0, 1] = (1 - local_coords[:, 1]) * (1 - local_coords[:, 2])
427
+ T[:, 0, 2] = -local_coords[:, 1] * (1 - local_coords[:, 2])
428
+ T[:, 0, 4] = -(1 - local_coords[:, 1]) * local_coords[:, 2]
429
+ T[:, 0, 5] = (1 - local_coords[:, 1]) * local_coords[:, 2]
430
+ T[:, 0, 6] = -local_coords[:, 1] * local_coords[:, 2]
431
+ T[:, 0, 3] = local_coords[:, 1] * (1 - local_coords[:, 2])
432
+ T[:, 0, 7] = local_coords[:, 1] * local_coords[:, 2]
433
+
434
+ T[:, 1, 0] = (local_coords[:, 0] - 1) * (1 - local_coords[:, 2])
435
+ T[:, 1, 1] = -local_coords[:, 0] * (1 - local_coords[:, 2])
436
+ T[:, 1, 2] = (1 - local_coords[:, 0]) * (1 - local_coords[:, 2])
437
+ T[:, 1, 4] = -(1 - local_coords[:, 0]) * local_coords[:, 2]
438
+ T[:, 1, 5] = -local_coords[:, 0] * local_coords[:, 2]
439
+ T[:, 1, 6] = (1 - local_coords[:, 0]) * local_coords[:, 2]
440
+ T[:, 1, 3] = local_coords[:, 0] * (1 - local_coords[:, 2])
441
+ T[:, 1, 7] = local_coords[:, 0] * local_coords[:, 2]
442
+
443
+ T[:, 2, 0] = -(1 - local_coords[:, 0]) * (1 - local_coords[:, 1])
444
+ T[:, 2, 1] = -local_coords[:, 0] * (1 - local_coords[:, 1])
445
+ T[:, 2, 2] = -(1 - local_coords[:, 0]) * local_coords[:, 1]
446
+ T[:, 2, 4] = (1 - local_coords[:, 0]) * (1 - local_coords[:, 1])
447
+ T[:, 2, 5] = local_coords[:, 0] * (1 - local_coords[:, 1])
448
+ T[:, 2, 6] = (1 - local_coords[:, 0]) * local_coords[:, 1]
449
+ T[:, 2, 3] = -local_coords[:, 0] * local_coords[:, 1]
450
+ T[:, 2, 7] = local_coords[:, 0] * local_coords[:, 1]
451
+ T[:, 0, :] /= self.step_vector[None, 0]
452
+ T[:, 1, :] /= self.step_vector[None, 1]
453
+ T[:, 2, :] /= self.step_vector[None, 2]
454
+ return vertices, T, elements, inside
455
+
456
+ def get_element_for_location(self, pos: np.ndarray):
457
+ """Calculate the shape function of elements
458
+ for a location
459
+
460
+ Parameters
461
+ ----------
462
+ pos : np.array((N,3))
463
+ location of points to calculate the shape function
464
+
465
+ Returns
466
+ -------
467
+ [type]
468
+ [description]
469
+ """
470
+ vertices, inside = self.position_to_cell_vertices(pos)
471
+ vertices = np.array(vertices)
472
+ # print("ver", vertices.shape)
473
+ # vertices = vertices.reshape((vertices.shape[1], 8, 3))
474
+ elements, inside = self.position_to_cell_corners(pos)
475
+ elements, inside = self.position_to_cell_index(pos)
476
+ elements = self.global_cell_indices(elements)
477
+ a = self.position_to_dof_coefs(pos)
478
+ return vertices, a, elements, inside
479
+
480
+ def get_elements(self):
481
+ return
482
+
483
+ def to_dict(self):
484
+ return {
485
+ "type": self.type.numerator,
486
+ **super().to_dict(),
487
+ }
488
+
489
+ def get_operators(self, weights: dict[str, float]) -> dict[str, tuple[np.ndarray, float]]:
490
+ """Gets the operators specific to this support
491
+
492
+ Parameters
493
+ ----------
494
+ weights : Dict[str, float]
495
+ weight value per operator
496
+
497
+ Returns
498
+ -------
499
+ operators
500
+ A dictionary with a numpy array and float weight
501
+ """
502
+ operators = {
503
+ "dxy": (Operator.Dxy_mask, weights["dxy"] / 4),
504
+ "dyz": (Operator.Dyz_mask, weights["dyz"] / 4),
505
+ "dxz": (Operator.Dxz_mask, weights["dxz"] / 4),
506
+ "dxx": (Operator.Dxx_mask, weights["dxx"] / 1),
507
+ "dyy": (Operator.Dyy_mask, weights["dyy"] / 1),
508
+ "dzz": (Operator.Dzz_mask, weights["dzz"] / 1),
509
+ }
510
+ return operators