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,349 @@
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_p1_unstructured import P1Unstructured2d
13
+
14
+ logger = logging.getLogger(__name__)
15
+
16
+
17
+ class P2Unstructured2d(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
+ "P2Unstructured2d requires either explicit elements/vertices/"
34
+ "neighbours arrays, or origin/step_vector/nsteps to build a "
35
+ "quadratic 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.P2Unstructured2d
40
+ # hessian of shape functions
41
+ self.hessian = np.array(
42
+ [
43
+ [[4, 4, 0, 0, 0, -8], [4, 0, 0, 4, -4, -4]],
44
+ [[4, 0, 0, 4, -4, -4], [4, 0, 4, 0, -8, 0]],
45
+ ]
46
+ )
47
+
48
+ @staticmethod
49
+ def _build_from_bbox(origin: np.ndarray, step_vector: np.ndarray, nsteps: np.ndarray):
50
+ """Build a quadratic (6-node) triangular mesh over a structured grid."""
51
+ p1_vertices, p1_elements, p1_neighbours = P1Unstructured2d._build_from_bbox(
52
+ origin, step_vector, nsteps
53
+ )
54
+
55
+ local_edges = np.array([[1, 2], [0, 2], [0, 1]])
56
+ local_index_for_edge = [3, 4, 5]
57
+
58
+ n_tris = p1_elements.shape[0]
59
+ edge_nodes = p1_elements[:, local_edges]
60
+ edge_nodes_sorted = np.sort(edge_nodes, axis=2)
61
+ flat_edges = edge_nodes_sorted.reshape(-1, 2)
62
+
63
+ unique_edges, inverse = np.unique(flat_edges, axis=0, return_inverse=True)
64
+ midpoint_nodes = (p1_vertices[unique_edges[:, 0]] + p1_vertices[unique_edges[:, 1]]) / 2.0
65
+
66
+ all_vertices = np.vstack([p1_vertices, midpoint_nodes])
67
+ edge_node_index = p1_vertices.shape[0] + inverse.reshape(n_tris, 3)
68
+
69
+ p2_elements = np.zeros((n_tris, 6), dtype=p1_elements.dtype)
70
+ p2_elements[:, :3] = p1_elements
71
+ for edge_i, local_idx in enumerate(local_index_for_edge):
72
+ p2_elements[:, local_idx] = edge_node_index[:, edge_i]
73
+
74
+ return all_vertices, p2_elements, p1_neighbours
75
+
76
+ def evaluate_d2_shape(self, indexes):
77
+ vertices = self.nodes[self.elements[indexes], :]
78
+ jac = np.array(
79
+ [
80
+ [
81
+ (vertices[:, 1, 0] - vertices[:, 0, 0]),
82
+ (vertices[:, 1, 1] - vertices[:, 0, 1]),
83
+ ],
84
+ [
85
+ vertices[:, 2, 0] - vertices[:, 0, 0],
86
+ vertices[:, 2, 1] - vertices[:, 0, 1],
87
+ ],
88
+ ]
89
+ )
90
+ jac = np.linalg.inv(jac)
91
+ dxy = (
92
+ self.hessian[None, 0, 1, :] * jac[:, 0, 0] * jac[:, 1, 1]
93
+ + self.hessian[None, 0, 1, :] * jac[:, 1, 0] * jac[:, 0, 1]
94
+ + self.hessian[None, 0, 0, :] * jac[:, 0, 0] * jac[:, 0, 1]
95
+ + self.hessian[None, 1, 1, :] * jac[:, 1, 0] * jac[:, 1, 1]
96
+ )
97
+ dxx = (
98
+ self.hessian[None, 0, 0, :] * jac[:, 0, 0] * jac[:, 0, 0]
99
+ + jac[:, 0, 0] * jac[:, 1, 0] * self.hessian[None, 0, 1, :]
100
+ + jac[:, 1, 0] * jac[:, 1, 0] * self.hessian[None, 1, 1]
101
+ )
102
+ dyy = (
103
+ self.hessian[None, 0, 0, :] * jac[:, 1, 0] * jac[:, 1, 0]
104
+ + jac[:, 1, 0] * jac[:, 1, 1] * self.hessian[None, 0, 1, :]
105
+ + jac[:, 1, 1] * jac[:, 1, 1] * self.hessian[None, 1, 1]
106
+ )
107
+ return dxx, dyy, dxy
108
+
109
+ # vertices = np.zeros((3,2))
110
+ # vertices[0,:] = [M[0,1],M[0,2]]
111
+ # vertices[1,:] = [M[1,1],M[1,2]]
112
+ # vertices[2,:] = [M[2,1],M[2,2]]
113
+ # jac = np.array([[(vertices[1,0]-vertices[0,0]),(vertices[1,1]-vertices[0,1])],
114
+ # [vertices[2,0]-vertices[0,0],vertices[2,1]-vertices[0,1]]])
115
+ # Nst_coeff = jac[0,0]*jac[1,1]+jac[0,1]*jac[1,0]
116
+
117
+ # #N_st
118
+ # Nst = np.zeros(6)
119
+ # Nst[0] = 4
120
+ # Nst[1] = 0
121
+ # Nst[2] = 0
122
+ # Nst[3] = 4
123
+ # Nst[4] = -4
124
+ # Nst[5] = -4
125
+
126
+ # hN = np.zeros((2,6))
127
+
128
+ # #N_ss
129
+ # hN[0,0] = 4
130
+ # hN[0,1] = 4
131
+ # hN[0,2] = 0
132
+ # hN[0,3] = 0
133
+ # hN[0,4] = 0
134
+ # hN[0,5] = -8
135
+
136
+ # #N_tt
137
+ # hN[1,0] = 4
138
+ # hN[1,1] = 0
139
+ # hN[1,2] = 4
140
+ # hN[1,3] = 0
141
+ # hN[1,4] = -8
142
+ # hN[1,5] = 0
143
+
144
+ # xyConst = Nst*Nst_coeff + hN[0] * jac[0,0]*jac[1,0] + hN[1] * jac[1,0]*jac[1,1]
145
+ # jac = np.linalg.inv(jac)
146
+ # jac = jac*jac
147
+
148
+ # d2_prod = np.dot(jac,hN)
149
+ # d2Const = d2_prod[0] + d2_prod[1]
150
+ # xxConst = d2_prod[0]
151
+ # yyConst = d2_prod[1]
152
+
153
+ # return xxConst,yyConst,xyConstz
154
+ # def evaluate_mixed_derivative(self, indexes):
155
+ # """
156
+ # evaluate partial of N with respect to st (to set u_xy=0)
157
+ # """
158
+
159
+ # vertices = self.nodes[self.elements[indexes], :]
160
+ # jac = np.array(
161
+ # [
162
+ # [
163
+ # (vertices[:, 1, 0] - vertices[:, 0, 0]),
164
+ # (vertices[:, 1, 1] - vertices[:, 0, 1]),
165
+ # ],
166
+ # [
167
+ # vertices[:, 2, 0] - vertices[:, 0, 0],
168
+ # vertices[:, 2, 1] - vertices[:, 0, 1],
169
+ # ],
170
+ # ]
171
+ # ).T
172
+ # Nst_coeff = jac[:, 0, 0] * jac[:, 1, 1] + jac[:, 0, 1] * jac[:, 1, 0]
173
+
174
+ # Nst = self.Nst[None, :] * Nst_coeff[:, None]
175
+ # return (
176
+ # Nst
177
+ # + self.hN[None, 0, :] * (jac[:, 0, 0] * jac[:, 1, 0])[:, None]
178
+ # + self.hN[None, 1, :] * (jac[:, 1, 0] * jac[:, 1, 1])[:, None]
179
+ # )
180
+
181
+ def evaluate_shape_d2(self, indexes):
182
+ """Evaluate physical second derivatives of quadratic shape functions."""
183
+ vertices = self.nodes[self.elements[indexes], :]
184
+
185
+ jac = np.array(
186
+ [
187
+ [
188
+ (vertices[:, 1, 0] - vertices[:, 0, 0]),
189
+ (vertices[:, 1, 1] - vertices[:, 0, 1]),
190
+ ],
191
+ [
192
+ (vertices[:, 2, 0] - vertices[:, 0, 0]),
193
+ (vertices[:, 2, 1] - vertices[:, 0, 1]),
194
+ ],
195
+ ]
196
+ )
197
+ jac = jac.swapaxes(0, 2)
198
+ jac = jac.swapaxes(1, 2)
199
+ jac = np.linalg.inv(jac)
200
+ # calculate derivative by summation, using the reference-space
201
+ # hessian of the shape functions (self.hessian) and the chain rule
202
+ d2 = np.zeros((vertices.shape[0], 3, self.elements.shape[1]))
203
+ ii = 0
204
+ for i in range(2):
205
+ for j in range(i, 2):
206
+ for k in range(2):
207
+ for l in range(2):
208
+ d2[:, ii, :] += (
209
+ jac[:, i, k, None] * jac[:, j, l, None] * self.hessian[None, k, l, :]
210
+ )
211
+ ii += 1
212
+ return d2
213
+
214
+ def evaluate_shape_derivatives(self, locations, elements=None):
215
+ """
216
+ Compute dN/ds (1st row), dN/dt(2nd row)
217
+ """
218
+ locations = np.array(locations)
219
+ if elements is None:
220
+ _verts, c, tri, _inside = self.get_element_for_location(locations)
221
+ else:
222
+ tri = elements
223
+ M = np.ones((elements.shape[0], 3, 3))
224
+ M[:, :, 1:] = self.vertices[self.elements[elements], :][:, :3, :]
225
+ points_ = np.ones((locations.shape[0], 3))
226
+ points_[:, 1:] = locations
227
+ minv = np.linalg.inv(M)
228
+ c = np.einsum("lij,li->lj", minv, points_)
229
+
230
+ vertices = self.nodes[self.elements[tri][:, :3]]
231
+ jac = np.zeros((tri.shape[0], 2, 2))
232
+ jac[:, 0, 0] = vertices[:, 1, 0] - vertices[:, 0, 0]
233
+ jac[:, 0, 1] = vertices[:, 1, 1] - vertices[:, 0, 1]
234
+ jac[:, 1, 0] = vertices[:, 2, 0] - vertices[:, 0, 0]
235
+ jac[:, 1, 1] = vertices[:, 2, 1] - vertices[:, 0, 1]
236
+ # N = np.zeros((tri.shape[0], 6))
237
+
238
+ # dN containts the derivatives of the shape functions
239
+ dN = np.zeros((tri.shape[0], 2, 6))
240
+ dN[:, 0, 0] = 4 * c[:, 1] + 4 * c[:, 2] - 3 # diff(N1,s).evalf(subs=vmap)
241
+ dN[:, 0, 1] = 4 * c[:, 1] - 1 # diff(N2,s).evalf(subs=vmap)
242
+ dN[:, 0, 2] = 0 # diff(N3,s).evalf(subs=vmap)
243
+ dN[:, 0, 3] = 4 * c[:, 2] # diff(N4,s).evalf(subs=vmap)
244
+ dN[:, 0, 4] = -4 * c[:, 2] # diff(N5,s).evalf(subs=vmap)
245
+ dN[:, 0, 5] = -8 * c[:, 1] - 4 * c[:, 2] + 4 # diff(N6,s).evalf(subs=vmap)
246
+
247
+ dN[:, 1, 0] = 4 * c[:, 1] + 4 * c[:, 2] - 3 # diff(N1,t).evalf(subs=vmap)
248
+ dN[:, 1, 1] = 0 # diff(N2,t).evalf(subs=vmap)
249
+ dN[:, 1, 2] = 4 * c[:, 2] - 1 # diff(N3,t).evalf(subs=vmap)
250
+ dN[:, 1, 3] = 4 * c[:, 1] # diff(N4,t).evalf(subs=vmap)
251
+ dN[:, 1, 4] = -4 * c[:, 1] - 8 * c[:, 2] + 4 # diff(N5,t).evalf(subs=vmap)
252
+ dN[:, 1, 5] = -4 * c[:, 1] # diff(N6,t).evalf(subs=vmap)
253
+
254
+ # find the derivatives in x and y by calculating the dot product between the jacobian^-1 and the
255
+ # derivative matrix
256
+ # d_n = np.einsum('ijk,ijl->ilk',np.linalg.inv(jac),dN)
257
+ d_n = np.linalg.inv(jac)
258
+ # d_n = d_n.swapaxes(1,2)
259
+ d_n = d_n @ dN
260
+ # d_n = d_n.swapaxes(2, 1)
261
+ # d_n = np.dot(np.linalg.inv(jac),dN)
262
+ return d_n, tri
263
+
264
+ def evaluate_shape(self, locations):
265
+ locations = np.array(locations)
266
+ _verts, c, tri, inside = self.get_element_for_location(locations)
267
+ # c = np.dot(np.array([1,x,y]),np.linalg.inv(M)) # convert to barycentric coordinates
268
+ # order of bary coord is (1-s-t,s,t)
269
+ N = np.zeros((c.shape[0], 6)) # evaluate shape functions at barycentric coordinates
270
+ N[:, 0] = c[:, 0] * (2 * c[:, 0] - 1) # (1-s-t)(1-2s-2t)
271
+ N[:, 1] = c[:, 1] * (2 * c[:, 1] - 1) # s(2s-1)
272
+ N[:, 2] = c[:, 2] * (2 * c[:, 2] - 1) # t(2t-1)
273
+ N[:, 3] = 4 * c[:, 1] * c[:, 2] # 4st
274
+ N[:, 4] = 4 * c[:, 2] * c[:, 0] # 4t(1-s-t)
275
+ N[:, 5] = 4 * c[:, 1] * c[:, 0] # 4s(1-s-t)
276
+
277
+ return N, tri, inside
278
+
279
+ def evaluate_d2(self, pos, property_array):
280
+ """
281
+ Evaluate value of interpolant
282
+
283
+ Parameters
284
+ ----------
285
+ pos - numpy array
286
+ locations
287
+ prop - numpy array
288
+ property values at nodes
289
+
290
+ Returns
291
+ -------
292
+
293
+ """
294
+ _c, tri, inside = self.evaluate_shape(pos[:, :2])
295
+ d2 = self.evaluate_shape_d2(tri)
296
+ values = np.zeros((pos.shape[0], d2.shape[1]))
297
+ values[:] = np.nan
298
+ for i in range(d2.shape[1]):
299
+ values[inside, i] = np.sum(
300
+ d2[inside, i, :] * property_array[self.elements[tri[inside], :]],
301
+ axis=1,
302
+ )
303
+
304
+ return values
305
+
306
+ def get_quadrature_points(self, npts=2):
307
+ if npts == 2:
308
+ v1 = self.nodes[self.shared_elements][:, 0, :]
309
+ v2 = self.nodes[self.shared_elements][:, 1, :]
310
+ cp = np.zeros((v1.shape[0], 2, 2))
311
+ cp[:, 0] = 0.25 * v1 + 0.75 * v2
312
+ cp[:, 1] = 0.75 * v1 + 0.25 * v2
313
+ weight = np.ones((v1.shape[0], 2))
314
+ return cp, weight
315
+ raise NotImplementedError("Only 2 point quadrature is implemented")
316
+
317
+ def evaluate_value(self, pos: np.ndarray, property_array: np.ndarray) -> np.ndarray:
318
+ """Evaluate value of interpolant using quadratic shape functions."""
319
+ pos = np.asarray(pos)
320
+ if property_array.shape[0] != self.n_nodes:
321
+ raise ValueError("property array must have same length as nodes")
322
+ values = np.zeros(pos.shape[0])
323
+ values[:] = np.nan
324
+ N, tri, inside = self.evaluate_shape(pos[:, :2])
325
+ values[inside] = np.sum(
326
+ N[inside, :] * property_array[self.elements[tri[inside], :]], axis=1
327
+ )
328
+ return values
329
+
330
+ def evaluate_gradient(self, pos: np.ndarray, property_array: np.ndarray) -> np.ndarray:
331
+ """Evaluate gradient of interpolant using quadratic shape derivatives."""
332
+ pos = np.asarray(pos)
333
+ if property_array.shape[0] != self.n_nodes:
334
+ raise ValueError("property array must have same length as nodes")
335
+ values = np.zeros(pos.shape)
336
+ values[:] = np.nan
337
+ element_gradients, tri = self.evaluate_shape_derivatives(pos[:, :2])
338
+ inside = tri >= 0
339
+ values[inside, :] = (
340
+ element_gradients[inside, :, :] * property_array[self.elements[tri[inside], None, :]]
341
+ ).sum(2)
342
+ return values
343
+
344
+ def get_edge_normal(self, e):
345
+ v = self.nodes[self.shared_elements][:, 0, :] - self.nodes[self.shared_elements][:, 1, :]
346
+ # e_len = np.linalg.norm(v, axis=1)
347
+ normal = np.array([v[:, 1], -v[:, 0]]).T
348
+ normal /= np.linalg.norm(normal, axis=1)[:, None]
349
+ return normal