loop-common 0.0.1__tar.gz → 0.1.0__tar.gz

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 (67) hide show
  1. {loop_common-0.0.1 → loop_common-0.1.0}/PKG-INFO +1 -1
  2. {loop_common-0.0.1 → loop_common-0.1.0}/pyproject.toml +1 -1
  3. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_2d_p2_unstructured.py +27 -4
  4. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_2d_structured_grid.py +2 -0
  5. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_3d_p2_tetra.py +52 -3
  6. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_3d_unstructured_tetra.py +5 -2
  7. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_p2_structured_tetra.py +25 -2
  8. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common.egg-info/PKG-INFO +1 -1
  9. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common.egg-info/SOURCES.txt +1 -0
  10. loop_common-0.1.0/tests/test_p2_shape_derivatives_outside.py +39 -0
  11. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_p2_structured_tetra.py +1 -1
  12. {loop_common-0.0.1 → loop_common-0.1.0}/LICENSE +0 -0
  13. {loop_common-0.0.1 → loop_common-0.1.0}/README.md +0 -0
  14. {loop_common-0.0.1 → loop_common-0.1.0}/setup.cfg +0 -0
  15. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/__init__.py +0 -0
  16. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/base.py +0 -0
  17. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/__init__.py +0 -0
  18. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_aabb.py +0 -0
  19. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_bounding_box.py +0 -0
  20. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_face_table.py +0 -0
  21. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_point.py +0 -0
  22. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_structured_grid.py +0 -0
  23. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_structured_grid_2d.py +0 -0
  24. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_structured_grid_3d.py +0 -0
  25. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_surface.py +0 -0
  26. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/geometry/_unstructured_mesh.py +0 -0
  27. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/interfaces/__init__.py +0 -0
  28. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/interfaces/representation.py +0 -0
  29. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/io/__init__.py +0 -0
  30. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/logging/__init__.py +0 -0
  31. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/logging/logger.py +0 -0
  32. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/logging/sinks.py +0 -0
  33. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/logging/timing.py +0 -0
  34. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/math/__init__.py +0 -0
  35. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/math/_maths.py +0 -0
  36. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/math/_transformation.py +0 -0
  37. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/math/finite_difference_stencil.py +0 -0
  38. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/observations/__init__.py +0 -0
  39. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/observations/lineset.py +0 -0
  40. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/observations/orientation.py +0 -0
  41. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/observations/pointset.py +0 -0
  42. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/observer.py +0 -0
  43. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_2d_base_unstructured.py +0 -0
  44. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_2d_p1_unstructured.py +0 -0
  45. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_3d_base_structured.py +0 -0
  46. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_3d_rectilinear_grid.py +0 -0
  47. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_3d_structured_grid.py +0 -0
  48. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_3d_structured_tetra.py +0 -0
  49. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/__init__.py +0 -0
  50. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_aabb.py +0 -0
  51. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_base_support.py +0 -0
  52. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_face_table.py +0 -0
  53. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/supports/_support_factory.py +0 -0
  54. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common/utils.py +0 -0
  55. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common.egg-info/dependency_links.txt +0 -0
  56. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common.egg-info/requires.txt +0 -0
  57. {loop_common-0.0.1 → loop_common-0.1.0}/src/loop_common.egg-info/top_level.txt +0 -0
  58. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_2d_discrete_support.py +0 -0
  59. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_base.py +0 -0
  60. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_bounding_box.py +0 -0
  61. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_discrete_supports.py +0 -0
  62. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_imports.py +0 -0
  63. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_observations.py +0 -0
  64. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_p0_pointset_serialization.py +0 -0
  65. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_rectilinear_grid.py +0 -0
  66. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_structured_grid_boundary_eval.py +0 -0
  67. {loop_common-0.0.1 → loop_common-0.1.0}/tests/test_unstructured_supports.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: loop-common
3
- Version: 0.0.1
3
+ Version: 0.1.0
4
4
  Summary: Common utilities for LoopStructural
5
5
  Author-email: Lachlan Grose <lachlan.grose@monash.edu>
6
6
  License: MIT
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
  [project]
6
6
  name = "loop-common"
7
7
  description = "Common utilities for LoopStructural"
8
- version = "0.0.1"
8
+ version = "0.1.0"
9
9
  requires-python = ">=3.9"
10
10
  authors = [{ name = "Lachlan Grose", email = "lachlan.grose@monash.edu" }]
11
11
  readme = "README.md"
@@ -211,7 +211,31 @@ class P2Unstructured2d(BaseUnstructured2d):
211
211
  ii += 1
212
212
  return d2
213
213
 
214
- def evaluate_shape_derivatives(self, locations, elements=None):
214
+ def evaluate_shape_derivatives(self, locations: np.ndarray, elements=None):
215
+ """Evaluate P2 shape derivatives in global coordinates.
216
+
217
+ Returns
218
+ -------
219
+ dN, elements, inside
220
+ dN has shape (n, 2, n_nodes_per_element). Points outside the mesh
221
+ have element index -1, ``inside`` False and NaN rows in dN. They do
222
+ not affect other points in the batch.
223
+ """
224
+ locations = np.asarray(locations)
225
+ if elements is None:
226
+ _verts, _c, elements, inside = self.get_element_for_location(locations)
227
+ elements = np.asarray(elements)
228
+ valid = (elements >= 0) & np.asarray(inside, dtype=bool)
229
+ else:
230
+ elements = np.asarray(elements)
231
+ valid = elements >= 0
232
+ out_elements = np.where(valid, elements, -1)
233
+ dN = np.full((locations.shape[0], 2, self.elements.shape[1]), np.nan)
234
+ if np.any(valid):
235
+ dN[valid], _ = self._evaluate_shape_derivatives_valid(locations[valid], elements[valid])
236
+ return dN, out_elements, valid
237
+
238
+ def _evaluate_shape_derivatives_valid(self, locations, elements=None):
215
239
  """
216
240
  Compute dN/ds (1st row), dN/dt(2nd row)
217
241
  """
@@ -221,7 +245,7 @@ class P2Unstructured2d(BaseUnstructured2d):
221
245
  else:
222
246
  tri = elements
223
247
  M = np.ones((elements.shape[0], 3, 3))
224
- M[:, :, 1:] = self.vertices[self.elements[elements], :][:, :3, :]
248
+ M[:, :, 1:] = self.nodes[self.elements[elements], :][:, :3, :]
225
249
  points_ = np.ones((locations.shape[0], 3))
226
250
  points_[:, 1:] = locations
227
251
  minv = np.linalg.inv(M)
@@ -334,8 +358,7 @@ class P2Unstructured2d(BaseUnstructured2d):
334
358
  raise ValueError("property array must have same length as nodes")
335
359
  values = np.zeros(pos.shape)
336
360
  values[:] = np.nan
337
- element_gradients, tri = self.evaluate_shape_derivatives(pos[:, :2])
338
- inside = tri >= 0
361
+ element_gradients, tri, inside = self.evaluate_shape_derivatives(pos[:, :2])
339
362
  values[inside, :] = (
340
363
  element_gradients[inside, :, :] * property_array[self.elements[tri[inside], None, :]]
341
364
  ).sum(2)
@@ -460,6 +460,8 @@ class StructuredGrid2D(BaseSupport):
460
460
  T[:, 1, 2] = 1 - local_coords[:, 0]
461
461
  T[:, 1, 3] = local_coords[:, 0]
462
462
 
463
+ T[:, 0, :] /= self.step_vector[None, 0]
464
+ T[:, 1, :] /= self.step_vector[None, 1]
463
465
  return vertices, T, elements, inside
464
466
 
465
467
  def get_element_for_location(
@@ -25,7 +25,19 @@ class P2UnstructuredTetMesh(UnStructuredTetMesh):
25
25
  "neighbours arrays, or origin/step_vector/nsteps_cells to build a "
26
26
  "quadratic tetrahedral mesh over a bounding box"
27
27
  )
28
+ origin = np.asarray(origin, dtype=float)
29
+ step_vector = np.asarray(step_vector, dtype=float)
30
+ nsteps_cells = np.asarray(nsteps_cells)
28
31
  nodes, elements, neighbours = self._build_from_bbox(origin, step_vector, nsteps_cells)
32
+ # kept so that inside() and the setup diagnostics work on a bbox-built mesh
33
+ self.origin = origin
34
+ self.step_vector = step_vector
35
+ self.nsteps_cells = nsteps_cells
36
+ elif origin is not None and step_vector is not None and nsteps_cells is not None:
37
+ # explicit arrays (e.g. from to_dict) keep the grid description
38
+ self.origin = np.asarray(origin, dtype=float)
39
+ self.step_vector = np.asarray(step_vector, dtype=float)
40
+ self.nsteps_cells = np.asarray(nsteps_cells)
29
41
  UnStructuredTetMesh.__init__(self, nodes, elements, neighbours, aabb_nsteps)
30
42
  self.type = SupportType.P2UnstructuredTetMesh
31
43
  if self.elements.shape[1] != 10:
@@ -50,6 +62,20 @@ class P2UnstructuredTetMesh(UnStructuredTetMesh):
50
62
  ]
51
63
  )
52
64
 
65
+ def to_dict(self):
66
+ """Serialise as explicit arrays, SupportFactory.from_dict reads this back."""
67
+ d = {
68
+ "type": self.type.numerator,
69
+ "nodes": self.nodes,
70
+ "elements": self.elements,
71
+ "neighbours": self.neighbours,
72
+ }
73
+ if hasattr(self, "origin"):
74
+ d["origin"] = self.origin
75
+ d["step_vector"] = self.step_vector
76
+ d["nsteps_cells"] = self.nsteps_cells
77
+ return d
78
+
53
79
  @staticmethod
54
80
  def _build_from_bbox(origin: np.ndarray, step_vector: np.ndarray, nsteps_cells: np.ndarray):
55
81
  """Build a quadratic (10-node) tetrahedral mesh over a structured grid.
@@ -184,7 +210,31 @@ class P2UnstructuredTetMesh(UnStructuredTetMesh):
184
210
  ii += 1
185
211
  return d2
186
212
 
187
- def evaluate_shape_derivatives(
213
+ def evaluate_shape_derivatives(self, locations: np.ndarray, elements=None):
214
+ """Evaluate P2 shape derivatives in global coordinates.
215
+
216
+ Returns
217
+ -------
218
+ dN, elements, inside
219
+ dN has shape (n, 3, n_nodes_per_element). Points outside the mesh
220
+ have element index -1, ``inside`` False and NaN rows in dN. They do
221
+ not affect other points in the batch.
222
+ """
223
+ locations = np.asarray(locations)
224
+ if elements is None:
225
+ _verts, _c, elements, inside = self.get_element_for_location(locations)
226
+ elements = np.asarray(elements)
227
+ valid = (elements >= 0) & np.asarray(inside, dtype=bool)
228
+ else:
229
+ elements = np.asarray(elements)
230
+ valid = elements >= 0
231
+ out_elements = np.where(valid, elements, -1)
232
+ dN = np.full((locations.shape[0], 3, self.elements.shape[1]), np.nan)
233
+ if np.any(valid):
234
+ dN[valid], _ = self._evaluate_shape_derivatives_valid(locations[valid], elements[valid])
235
+ return dN, out_elements, valid
236
+
237
+ def _evaluate_shape_derivatives_valid(
188
238
  self, locations: np.ndarray, elements: np.ndarray = None
189
239
  ) -> np.ndarray:
190
240
  """
@@ -365,8 +415,7 @@ class P2UnstructuredTetMesh(UnStructuredTetMesh):
365
415
  raise ValueError("property array must have same length as nodes")
366
416
  values = np.zeros(pos.shape)
367
417
  values[:] = np.nan
368
- element_gradients, tetra = self.evaluate_shape_derivatives(pos[:, :3])
369
- inside = tetra >= 0
418
+ element_gradients, tetra, inside = self.evaluate_shape_derivatives(pos[:, :3])
370
419
  values[inside, :] = (
371
420
  element_gradients[:, :, :] * property_array[self.elements[tetra[inside], None, :]]
372
421
  ).sum(2)
@@ -186,8 +186,11 @@ class UnStructuredTetMesh(BaseSupport):
186
186
  )
187
187
  shared_faces = faces[np.array(np.sum(faces, axis=1) == 3).flatten(), :]
188
188
  row, col = shared_faces.nonzero()
189
- row = row[row.argsort()]
190
- col = col[row.argsort()]
189
+ # one stable sort for both arrays: the default sort is not stable, so ties
190
+ # (three nodes per face) came out in a numpy/CPU dependent order
191
+ order = row.argsort(kind="stable")
192
+ row = row[order]
193
+ col = col[order]
191
194
  shared_face_index = np.zeros((shared_faces.shape[0], 3), dtype=int)
192
195
  shared_face_index[:] = -1
193
196
  shared_face_index[row.reshape(-1, 3)[:, 0], :] = col.reshape(-1, 3)
@@ -260,6 +260,30 @@ class P2TetMesh(BaseStructuredSupport):
260
260
  return N, elements, inside
261
261
 
262
262
  def evaluate_shape_derivatives(self, locations: np.ndarray, elements=None):
263
+ """Evaluate P2 shape derivatives in global coordinates.
264
+
265
+ Returns
266
+ -------
267
+ dN, elements, inside
268
+ dN has shape (n, 3, n_nodes_per_element). Points outside the mesh
269
+ have element index -1, ``inside`` False and NaN rows in dN. They do
270
+ not affect other points in the batch.
271
+ """
272
+ locations = np.asarray(locations)
273
+ if elements is None:
274
+ _verts, _c, elements, inside = self.get_element_for_location(locations)
275
+ elements = np.asarray(elements)
276
+ valid = (elements >= 0) & np.asarray(inside, dtype=bool)
277
+ else:
278
+ elements = np.asarray(elements)
279
+ valid = elements >= 0
280
+ out_elements = np.where(valid, elements, -1)
281
+ dN = np.full((locations.shape[0], 3, self.elements.shape[1]), np.nan)
282
+ if np.any(valid):
283
+ dN[valid], _ = self._evaluate_shape_derivatives_valid(locations[valid], elements[valid])
284
+ return dN, out_elements, valid
285
+
286
+ def _evaluate_shape_derivatives_valid(self, locations: np.ndarray, elements=None):
263
287
  """Evaluate quadratic tetrahedral shape derivatives at locations."""
264
288
  locations = np.array(locations)
265
289
  if elements is None:
@@ -480,8 +504,7 @@ class P2TetMesh(BaseStructuredSupport):
480
504
  values = np.zeros(pos.shape)
481
505
  values[:] = np.nan
482
506
 
483
- element_gradients, tetras = self.evaluate_shape_derivatives(pos)
484
- _, _, inside = self.evaluate_shape(pos)
507
+ element_gradients, tetras, inside = self.evaluate_shape_derivatives(pos)
485
508
  values[inside, :] = np.einsum(
486
509
  "ijk,ik->ij",
487
510
  element_gradients[inside, :, :],
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: loop-common
3
- Version: 0.0.1
3
+ Version: 0.1.0
4
4
  Summary: Common utilities for LoopStructural
5
5
  Author-email: Lachlan Grose <lachlan.grose@monash.edu>
6
6
  License: MIT
@@ -58,6 +58,7 @@ tests/test_discrete_supports.py
58
58
  tests/test_imports.py
59
59
  tests/test_observations.py
60
60
  tests/test_p0_pointset_serialization.py
61
+ tests/test_p2_shape_derivatives_outside.py
61
62
  tests/test_p2_structured_tetra.py
62
63
  tests/test_rectilinear_grid.py
63
64
  tests/test_structured_grid_boundary_eval.py
@@ -0,0 +1,39 @@
1
+ """evaluate_shape_derivatives must handle points outside the mesh."""
2
+
3
+ import numpy as np
4
+ import pytest
5
+ from loop_common.supports._3d_p2_tetra import P2UnstructuredTetMesh
6
+ from loop_common.supports._p2_structured_tetra import P2TetMesh
7
+
8
+
9
+ def _unstructured():
10
+ return P2UnstructuredTetMesh(
11
+ origin=np.zeros(3), step_vector=np.ones(3) / 4, nsteps_cells=np.array([4, 4, 4])
12
+ )
13
+
14
+
15
+ def _structured():
16
+ return P2TetMesh(origin=np.zeros(3), nsteps=np.array([3, 3, 3]), step_vector=np.ones(3))
17
+
18
+
19
+ @pytest.mark.parametrize(
20
+ "make,inside_pt,outside_pt",
21
+ [
22
+ (_unstructured, [0.41, 0.52, 0.33], [5.0, 5.0, 5.0]),
23
+ (_structured, [1.1, 1.2, 1.3], [50.0, 50.0, 50.0]),
24
+ ],
25
+ )
26
+ def test_outside_point_gives_nan_and_does_not_poison_batch(make, inside_pt, outside_pt):
27
+ mesh = make()
28
+ ref = mesh.evaluate_shape_derivatives(np.array([inside_pt]))
29
+ pts = np.array([inside_pt, outside_pt, inside_pt])
30
+ res = mesh.evaluate_shape_derivatives(pts)
31
+ assert len(res) == 3
32
+ dN, elements, inside = res
33
+ assert dN.shape == (3, 3, 10)
34
+ assert elements[1] == -1
35
+ assert not inside[1]
36
+ assert np.all(np.isnan(dN[1]))
37
+ assert inside[0] and inside[2]
38
+ assert np.all(np.isfinite(dN[0])) and np.all(np.isfinite(dN[2]))
39
+ np.testing.assert_allclose(dN[0], ref[0][0])
@@ -183,7 +183,7 @@ class TestP2TetMeshShapeFunctions:
183
183
  mesh = P2TetMesh(nsteps=np.array([3, 3, 3]))
184
184
 
185
185
  centroid = np.array([[1.0, 1.0, 1.0]])
186
- dN, _elem_ids = mesh.evaluate_shape_derivatives(centroid)
186
+ dN, _elem_ids, _inside = mesh.evaluate_shape_derivatives(centroid)
187
187
 
188
188
  # dN should have shape (n_points, 3, 10)
189
189
  assert dN.shape == (1, 3, 10)
File without changes
File without changes
File without changes