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,69 @@
1
+ from enum import IntEnum
2
+
3
+
4
+ class SupportType(IntEnum):
5
+ """
6
+ Enum for the different interpolator types
7
+
8
+ 1-9 should cover interpolators with supports
9
+ 9+ are data supported
10
+ """
11
+
12
+ StructuredGrid2D = 0
13
+ StructuredGrid = 1
14
+ UnStructuredTetMesh = 2
15
+ P1Unstructured2d = 3
16
+ P2Unstructured2d = 4
17
+ BaseUnstructured2d = 5
18
+ BaseStructured = 6
19
+ TetMesh = 10
20
+ P2UnstructuredTetMesh = 11
21
+ P2StructuredTetMesh = 12
22
+ DataSupported = 13
23
+ RectilinearGrid = 14
24
+
25
+
26
+ from ._2d_base_unstructured import BaseUnstructured2d
27
+ from ._2d_p1_unstructured import P1Unstructured2d
28
+ from ._2d_p2_unstructured import P2Unstructured2d
29
+ from ._2d_structured_grid import StructuredGrid2D
30
+ from ._3d_p2_tetra import P2UnstructuredTetMesh
31
+ from ._3d_rectilinear_grid import RectilinearGrid
32
+ from ._3d_structured_grid import StructuredGrid
33
+ from ._3d_structured_tetra import TetMesh
34
+ from ._3d_unstructured_tetra import UnStructuredTetMesh
35
+ from ._p2_structured_tetra import P2TetMesh
36
+
37
+
38
+ def no_support(*args, **kwargs):
39
+ return None
40
+
41
+
42
+ support_map = {
43
+ SupportType.StructuredGrid2D: StructuredGrid2D,
44
+ SupportType.StructuredGrid: StructuredGrid,
45
+ SupportType.RectilinearGrid: RectilinearGrid,
46
+ SupportType.UnStructuredTetMesh: UnStructuredTetMesh,
47
+ SupportType.P1Unstructured2d: P1Unstructured2d,
48
+ SupportType.P2Unstructured2d: P2Unstructured2d,
49
+ SupportType.TetMesh: TetMesh,
50
+ SupportType.P2UnstructuredTetMesh: P2UnstructuredTetMesh,
51
+ SupportType.P2StructuredTetMesh: P2TetMesh,
52
+ SupportType.DataSupported: no_support,
53
+ }
54
+
55
+ from ._support_factory import SupportFactory
56
+
57
+ __all__ = [
58
+ "BaseUnstructured2d",
59
+ "P1Unstructured2d",
60
+ "P2Unstructured2d",
61
+ "P2UnstructuredTetMesh",
62
+ "RectilinearGrid",
63
+ "StructuredGrid",
64
+ "StructuredGrid2D",
65
+ "SupportType",
66
+ "TetMesh",
67
+ "UnStructuredTetMesh",
68
+ "support_map",
69
+ ]
@@ -0,0 +1,77 @@
1
+ import numpy as np
2
+ from scipy import sparse
3
+
4
+
5
+ def _initialise_aabb(grid):
6
+ """assigns the tetras to the grid cells where the bounding box
7
+ of the tetra element overlaps the grid cell.
8
+ It could be changed to use the separating axis theorem, however this would require
9
+ significantly more calculations. (12 more I think).. #TODO test timing
10
+ """
11
+ # calculate the bounding box for all tetraherdon in the mesh
12
+ # find the min/max extents for xyz
13
+ # tetra_bb = np.zeros((grid.elements.shape[0], 19, 3))
14
+ minx = np.min(grid.nodes[grid.elements[:, :4], 0], axis=1)
15
+ maxx = np.max(grid.nodes[grid.elements[:, :4], 0], axis=1)
16
+ miny = np.min(grid.nodes[grid.elements[:, :4], 1], axis=1)
17
+ maxy = np.max(grid.nodes[grid.elements[:, :4], 1], axis=1)
18
+
19
+ cell_indexes = grid.aabb_grid.global_index_to_cell_index(np.arange(grid.aabb_grid.n_elements))
20
+ corners = grid.aabb_grid.cell_corner_indexes(cell_indexes)
21
+ positions = grid.aabb_grid.node_indexes_to_position(corners)
22
+ ## Because we known the node orders just select min/max from each
23
+ # coordinate. Use these to check whether the tetra is in the cell
24
+ x_boundary = positions[:, [0, 1], 0]
25
+ y_boundary = positions[:, [0, 2], 1]
26
+ a = np.logical_and(
27
+ minx[None, :] > x_boundary[:, None, 0],
28
+ minx[None, :] < x_boundary[:, None, 1],
29
+ ) # min point between cell
30
+ b = np.logical_and(
31
+ maxx[None, :] < x_boundary[:, None, 1],
32
+ maxx[None, :] > x_boundary[:, None, 0],
33
+ ) # max point between cell
34
+ c = np.logical_and(
35
+ minx[None, :] < x_boundary[:, None, 0],
36
+ maxx[None, :] > x_boundary[:, None, 0],
37
+ ) # min point < than cell & max point > cell
38
+
39
+ x_logic = np.logical_or(np.logical_or(a, b), c)
40
+
41
+ a = np.logical_and(
42
+ miny[None, :] > y_boundary[:, None, 0],
43
+ miny[None, :] < y_boundary[:, None, 1],
44
+ ) # min point between cell
45
+ b = np.logical_and(
46
+ maxy[None, :] < y_boundary[:, None, 1],
47
+ maxy[None, :] > y_boundary[:, None, 0],
48
+ ) # max point between cell
49
+ c = np.logical_and(
50
+ miny[None, :] < y_boundary[:, None, 0],
51
+ maxy[None, :] > y_boundary[:, None, 0],
52
+ ) # min point < than cell & max point > cell
53
+
54
+ y_logic = np.logical_or(np.logical_or(a, b), c)
55
+ logic = np.logical_and(x_logic, y_logic)
56
+
57
+ if grid.dimension == 3:
58
+ z_boundary = positions[:, [0, 6], 2]
59
+ minz = np.min(grid.nodes[grid.elements[:, :4], 2], axis=1)
60
+ maxz = np.max(grid.nodes[grid.elements[:, :4], 2], axis=1)
61
+ a = np.logical_and(
62
+ minz[None, :] > z_boundary[:, None, 0],
63
+ minz[None, :] < z_boundary[:, None, 1],
64
+ ) # min point between cell
65
+ b = np.logical_and(
66
+ maxz[None, :] < z_boundary[:, None, 1],
67
+ maxz[None, :] > z_boundary[:, None, 0],
68
+ ) # max point between cell
69
+ c = np.logical_and(
70
+ minz[None, :] < z_boundary[:, None, 0],
71
+ maxz[None, :] > z_boundary[:, None, 0],
72
+ ) # min point < than cell & max point > cell
73
+
74
+ z_logic = np.logical_or(np.logical_or(a, b), c)
75
+ logic = np.logical_and(logic, z_logic)
76
+
77
+ grid._aabb_table = sparse.csr_matrix(logic)
@@ -0,0 +1,118 @@
1
+ from abc import ABCMeta, abstractmethod
2
+
3
+ import numpy as np
4
+
5
+
6
+ class BaseSupport(metaclass=ABCMeta):
7
+ """
8
+ Base support class
9
+ """
10
+
11
+ @abstractmethod
12
+ def __init__(self):
13
+ """
14
+ This class is the base
15
+ """
16
+
17
+ def is_valid(self) -> bool:
18
+ """
19
+ Check if the support is valid
20
+ """
21
+ return True
22
+
23
+ @abstractmethod
24
+ def evaluate_value(self, evaluation_points: np.ndarray, property_array: np.ndarray):
25
+ """
26
+ Evaluate the value of the support at the evaluation points
27
+ """
28
+
29
+ @abstractmethod
30
+ def evaluate_gradient(self, evaluation_points: np.ndarray, property_array: np.ndarray):
31
+ """
32
+ Evaluate the gradient of the support at the evaluation points
33
+ """
34
+
35
+ @abstractmethod
36
+ def inside(self, pos):
37
+ """
38
+ Check if a position is inside the support
39
+ """
40
+
41
+ @abstractmethod
42
+ def onGeometryChange(self):
43
+ """
44
+ Called when the geometry changes
45
+ """
46
+
47
+ @abstractmethod
48
+ def get_element_for_location(
49
+ self, pos: np.ndarray
50
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
51
+ """
52
+ Get the element for a location
53
+ """
54
+
55
+ @abstractmethod
56
+ def get_element_gradient_for_location(
57
+ self, pos: np.ndarray
58
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
59
+ pass
60
+
61
+ @property
62
+ @abstractmethod
63
+ def elements(self):
64
+ """
65
+ Return the elements
66
+ """
67
+
68
+ @property
69
+ @abstractmethod
70
+ def n_elements(self):
71
+ """
72
+ Return the number of elements
73
+ """
74
+
75
+ @property
76
+ @abstractmethod
77
+ def n_nodes(self):
78
+ """
79
+ Return the number of points
80
+ """
81
+
82
+ @property
83
+ @abstractmethod
84
+ def nodes(self):
85
+ """
86
+ Return the nodes
87
+ """
88
+
89
+ @property
90
+ @abstractmethod
91
+ def barycentre(self):
92
+ """
93
+ Return the number of dimensions
94
+ """
95
+
96
+ @property
97
+ @abstractmethod
98
+ def dimension(self):
99
+ """
100
+ Return the number of dimensions
101
+ """
102
+
103
+ @property
104
+ @abstractmethod
105
+ def element_size(self):
106
+ """
107
+ Return the element size
108
+ """
109
+
110
+ @abstractmethod
111
+ def vtk(self, node_properties=None, cell_properties=None):
112
+ """
113
+ Return a vtk object
114
+ """
115
+
116
+ @abstractmethod
117
+ def set_nelements(self, nelements) -> int:
118
+ pass
@@ -0,0 +1,70 @@
1
+ import numpy as np
2
+ from scipy import sparse
3
+
4
+
5
+ def _init_face_table(grid):
6
+ """
7
+ Fill table containing elements that share a face, and another
8
+ table that contains the nodes for a face.
9
+ """
10
+ # need to identify the shared nodes for pairs of elements
11
+ # we do this by creating a sparse matrix that has N rows (number of elements)
12
+ # and M columns (number of nodes).
13
+ # We then fill the location where a node is in an element with true
14
+ # Then we create a table for the pairs of elements in the mesh
15
+ # we have the neighbour relationships, which are the 4 neighbours for each element
16
+ # create a new table that shows the element index repeated four times
17
+ # flatten both of these arrays so we effectively have a table with pairs of neighbours
18
+ # disgard the negative neighbours because these are border neighbours
19
+ rows = np.tile(np.arange(grid.n_elements)[:, None], (1, grid.dimension + 1))
20
+ elements = grid.elements
21
+ neighbours = grid.neighbours
22
+ # add array of bool to the location where there are elements for each node
23
+
24
+ # use this to determine shared faces
25
+
26
+ element_nodes = sparse.coo_matrix(
27
+ (
28
+ np.ones(elements.shape[0] * (grid.dimension + 1)),
29
+ (rows.ravel(), elements[:, : grid.dimension + 1].ravel()),
30
+ ),
31
+ shape=(grid.n_elements, grid.n_nodes),
32
+ dtype=bool,
33
+ ).tocsr()
34
+ n1 = np.tile(np.arange(neighbours.shape[0], dtype=int)[:, None], (1, grid.dimension + 1))
35
+ n1 = n1.flatten()
36
+ n2 = neighbours.flatten()
37
+ n1 = n1[n2 >= 0]
38
+ n2 = n2[n2 >= 0]
39
+ el_rel = np.zeros((grid.neighbours.flatten().shape[0], 2), dtype=int)
40
+ el_rel[:] = -1
41
+ el_rel[np.arange(n1.shape[0]), 0] = n1
42
+ el_rel[np.arange(n1.shape[0]), 1] = n2
43
+ el_rel = el_rel[el_rel[:, 0] >= 0, :]
44
+
45
+ # el_rel2 = np.zeros((grid.neighbours.flatten().shape[0], 2), dtype=int)
46
+ grid._shared_element_relationships[:] = -1
47
+ el_pairs = sparse.coo_matrix((np.ones(el_rel.shape[0]), (el_rel[:, 0], el_rel[:, 1]))).tocsr()
48
+ i, j = sparse.tril(el_pairs).nonzero()
49
+ grid._shared_element_relationships[: len(i), 0] = i
50
+ grid._shared_element_relationships[: len(i), 1] = j
51
+
52
+ grid._shared_element_relationships = grid.shared_element_relationships[
53
+ grid.shared_element_relationships[:, 0] >= 0, :
54
+ ]
55
+
56
+ faces = element_nodes[grid.shared_element_relationships[:, 0], :].multiply(
57
+ element_nodes[grid.shared_element_relationships[:, 1], :]
58
+ )
59
+ shared_faces = faces[np.array(np.sum(faces, axis=1) == grid.dimension).flatten(), :]
60
+ row, col = shared_faces.nonzero()
61
+ row = row[row.argsort()]
62
+ col = col[row.argsort()]
63
+ shared_face_index = np.zeros((shared_faces.shape[0], grid.dimension), dtype=int)
64
+ shared_face_index[:] = -1
65
+ shared_face_index[row.reshape(-1, grid.dimension)[:, 0], :] = col.reshape(-1, grid.dimension)
66
+ grid._shared_elements[np.arange(grid.shared_element_relationships.shape[0]), :] = (
67
+ shared_face_index
68
+ )
69
+ # resize
70
+ grid._shared_elements = grid.shared_elements[: len(grid.shared_element_relationships), :]