Skip to content

Commit 82c0bd5

Browse files
committed
fix: ✨ adding support type enum
1 parent 7f1f62a commit 82c0bd5

File tree

10 files changed

+109
-39
lines changed

10 files changed

+109
-39
lines changed

LoopStructural/interpolators/supports/_2d_base_unstructured.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
import numpy as np
77

8+
from . import SupportType
9+
810
logger = logging.getLogger(__name__)
911

1012

1113
class BaseUnstructured2d:
1214
""" """
1315

1416
def __init__(self, elements, vertices, neighbours):
17+
self.type = SupportType.BaseUnstructured2d
1518
self.elements = elements
1619
self.vertices = vertices
1720
if self.elements.shape[1] == 3:
@@ -23,7 +26,6 @@ def __init__(self, elements, vertices, neighbours):
2326
self.n_nodes = self.nx
2427
self.neighbours = neighbours
2528

26-
self.properties = {}
2729
# build an array of edges and edge relationships
2830
self.edges = np.zeros((self.nelements * 3, 2), dtype=int)
2931
self.edge_relationships = np.zeros((self.nelements * 3, 2), dtype=int)
@@ -94,7 +96,7 @@ def element_area(self, elements):
9496
area = np.abs(np.linalg.det(M_t)) * 0.5
9597
return area
9698

97-
def evaluate_value(self, pos, prop):
99+
def evaluate_value(self, pos, values):
98100
"""
99101
Evaluate value of interpolant
100102
@@ -115,7 +117,7 @@ def evaluate_value(self, pos, prop):
115117
inside = tri >= 0
116118
# vertices, c, elements, inside = self.get_elements_for_location(pos)
117119
values[inside] = np.sum(
118-
c[inside, :] * self.properties[prop][self.elements[tri[inside], :]], axis=1
120+
c[inside, :] * values[self.elements[tri[inside], :]], axis=1
119121
)
120122
return values
121123

LoopStructural/interpolators/supports/_2d_p1_unstructured.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77
from ._2d_base_unstructured import BaseUnstructured2d
8+
from . import SupportType
89

910
logger = logging.getLogger(__name__)
1011

@@ -14,6 +15,7 @@ class P1Unstructured2d(BaseUnstructured2d):
1415

1516
def __init__(self, elements, vertices, neighbours):
1617
BaseUnstructured2d.__init__(self, elements, vertices, neighbours)
18+
self.type = SupportType.P1Unstructured2d
1719

1820
def evaluate_shape_derivatives(self, locations, elements=None):
1921
"""

LoopStructural/interpolators/supports/_2d_p2_unstructured.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
Tetmesh based on cartesian grid for piecewise linear interpolation
33
"""
44
import logging
5+
from re import S
56

67
import numpy as np
78
from ._2d_base_unstructured import BaseUnstructured2d
9+
from . import SupportType
810

911
logger = logging.getLogger(__name__)
1012

@@ -14,6 +16,7 @@ class P2Unstructured2d(BaseUnstructured2d):
1416

1517
def __init__(self, elements, vertices, neighbours):
1618
BaseUnstructured2d.__init__(self, elements, vertices, neighbours)
19+
self.type = SupportType.P2Unstructured2d
1720
# hessian of shape functions
1821
self.H = np.array(
1922
[
@@ -271,7 +274,6 @@ def evaluate_d2(self, pos, prop):
271274

272275
def get_quadrature_points(self, npts=2):
273276
if npts == 2:
274-
275277
v1 = self.nodes[self.edges][:, 0, :]
276278
v2 = self.nodes[self.edges][:, 1, :]
277279
cp = np.zeros((v1.shape[0], self.ncps, 2))

LoopStructural/interpolators/supports/_2d_structured_grid.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66

77
import numpy as np
8+
from . import SupportType
89

910
logger = logging.getLogger(__name__)
1011

@@ -26,7 +27,7 @@ def __init__(
2627
nsteps - 2d list or numpy array of ints
2728
step_vector - 2d list or numpy array of int
2829
"""
29-
30+
self.type = SupportType.StructuredGrid2D
3031
self.nsteps = np.array(nsteps)
3132
self.step_vector = np.array(step_vector)
3233
self.origin = np.array(origin)
@@ -74,23 +75,6 @@ def print_geometry(self):
7475
max = self.origin + self.nsteps_cells * self.step_vector
7576
print("Max extent: %f %f %f" % (max[0], max[1], max[2]))
7677

77-
def update_property(self, propertyname, values):
78-
"""[summary]
79-
80-
[extended_summary]
81-
82-
Parameters
83-
----------
84-
propertyname : [type]
85-
[description]
86-
values : [type]
87-
[description]
88-
"""
89-
if values.shape[0] == self.n_nodes:
90-
self.properties[propertyname] = values
91-
if values.shape[0] == self.n_elements:
92-
self.cell_properties[propertyname] = values
93-
9478
def cell_centres(self, global_index):
9579
"""[summary]
9680
@@ -144,7 +128,6 @@ def position_to_cell_index(self, pos):
144128
return ix.astype(int), iy.astype(int)
145129

146130
def inside(self, pos):
147-
148131
# check whether point is inside box
149132
inside = np.ones(pos.shape[0]).astype(bool)
150133
for i in range(self.dim):
@@ -330,14 +313,12 @@ def global_index_to_cell_index(self, global_index):
330313
return x_index, y_index
331314

332315
def node_indexes_to_position(self, xindex, yindex):
333-
334316
x = self.origin[0] + self.step_vector[0] * xindex
335317
y = self.origin[1] + self.step_vector[1] * yindex
336318

337319
return x, y
338320

339321
def position_to_cell_corners(self, pos):
340-
341322
inside = self.inside(pos)
342323
ix, iy = self.position_to_cell_index(pos)
343324
cornersx, cornersy = self.cell_corner_indexes(ix, iy)

LoopStructural/interpolators/supports/_3d_base_structured.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
from LoopStructural.utils import getLogger
3+
from . import SupportType
34

45
logger = getLogger(__name__)
56

@@ -27,6 +28,7 @@ def __init__(
2728
# we use property decorators to update these when different parts of
2829
# the geometry need to change
2930
# inisialise the private attributes
31+
self.type = SupportType.BaseStructured
3032
if np.any(step_vector == 0):
3133
logger.warning(f"Step vector {step_vector} has zero values")
3234
self._nsteps = np.array(nsteps, dtype=int) + 1
@@ -39,6 +41,14 @@ def __init__(
3941
self._rotation_xy[2, 2] = 1
4042
self.rotation_xy = rotation_xy
4143

44+
def to_dict(self):
45+
return {
46+
"origin": self.origin,
47+
"nsteps": self.nsteps,
48+
"step_vector": self.step_vector,
49+
"rotation_xy": self.rotation_xy,
50+
}
51+
4252
@property
4353
def nsteps(self):
4454
return self._nsteps
@@ -60,13 +70,30 @@ def rotation_xy(self):
6070

6171
@rotation_xy.setter
6272
def rotation_xy(self, rotation_xy):
63-
if rotation_xy is not None:
64-
self._rotation_xy[:, :] = 0
65-
self._rotation_xy[0, 0] = np.cos(np.deg2rad(rotation_xy))
66-
self._rotation_xy[0, 1] = -np.sin(np.deg2rad(rotation_xy))
67-
self._rotation_xy[1, 0] = np.sin(np.deg2rad(rotation_xy))
68-
self._rotation_xy[1, 1] = np.cos(np.deg2rad(rotation_xy))
69-
self._rotation_xy[2, 2] = 1.0 # make sure rotation around z vector
73+
if rotation_xy is None:
74+
return
75+
if isinstance(rotation_xy, (float, int)):
76+
rotation_xy = np.array(
77+
[
78+
[
79+
np.cos(np.deg2rad(rotation_xy)),
80+
-np.sin(np.deg2rad(rotation_xy)),
81+
0,
82+
],
83+
[
84+
np.sin(np.deg2rad(rotation_xy)),
85+
np.cos(np.deg2rad(rotation_xy)),
86+
0,
87+
],
88+
[0, 0, 1],
89+
]
90+
)
91+
rotation_xy = np.array(rotation_xy)
92+
if rotation_xy.shape != (3, 3):
93+
raise ValueError(
94+
"Rotation matrix should be 3x3, not {}".format(rotation_xy.shape)
95+
)
96+
self._rotation_xy = rotation_xy
7097

7198
@property
7299
def step_vector(self):
@@ -304,7 +331,6 @@ def cell_corner_indexes(self, cell_indexes):
304331
return corner_indexes
305332

306333
def position_to_cell_corners(self, pos):
307-
308334
inside = self.inside(pos)
309335

310336
cell_indexes = self.position_to_cell_index(pos)

LoopStructural/interpolators/supports/_3d_p2_tetra.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ._3d_unstructured_tetra import UnStructuredTetMesh
22

33
import numpy as np
4+
from . import SupportType
45

56

67
class P2UnstructuredTetMesh(UnStructuredTetMesh):
@@ -12,6 +13,7 @@ def __init__(
1213
aabb_nsteps=None,
1314
):
1415
UnStructuredTetMesh.__init__(self, nodes, elements, neighbours, aabb_nsteps)
16+
self.type = SupportType.P2UnstructuredTetMesh
1517
if self.elements.shape[1] != 10:
1618
raise ValueError(
1719
f"P2 tetrahedron must have 8 nodes, has {self.elements.shape[1]}"

LoopStructural/interpolators/supports/_3d_structured_grid.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ._3d_base_structured import BaseStructuredSupport
1010

11+
from . import SupportType
1112

1213
from LoopStructural.utils import getLogger
1314

@@ -22,7 +23,7 @@ def __init__(
2223
origin=np.zeros(3),
2324
nsteps=np.array([10, 10, 10]),
2425
step_vector=np.ones(3),
25-
name=None,
26+
rotation_xy=None,
2627
):
2728
"""
2829
@@ -32,10 +33,12 @@ def __init__(
3233
nsteps - 3d list or numpy array of ints
3334
step_vector - 3d list or numpy array of int
3435
"""
35-
BaseStructuredSupport.__init__(self, origin, nsteps, step_vector)
36+
BaseStructuredSupport.__init__(
37+
self, origin, nsteps, step_vector, rotation_xy=rotation_xy
38+
)
39+
self.type = SupportType.StructuredGrid
3640
self.regions = {}
3741
self.regions["everywhere"] = np.ones(self.n_nodes).astype(bool)
38-
self.name = name
3942

4043
@property
4144
def barycentre(self):
@@ -475,3 +478,9 @@ def get_element_for_location(self, pos):
475478

476479
def get_elements(self):
477480
return
481+
482+
def to_dict(self):
483+
return {
484+
"type": self.type.numerator,
485+
**super().to_dict(),
486+
}

LoopStructural/interpolators/supports/_3d_structured_tetra.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77
from ._3d_base_structured import BaseStructuredSupport
8+
from . import SupportType
89

910
from LoopStructural.utils import getLogger
1011

@@ -18,7 +19,7 @@ def __init__(
1819
self, origin=np.zeros(3), nsteps=np.ones(3) * 10, step_vector=np.ones(3)
1920
):
2021
BaseStructuredSupport.__init__(self, origin, nsteps, step_vector)
21-
22+
self.type = SupportType.TetMesh
2223
self.tetra_mask_even = np.array(
2324
[[7, 1, 2, 4], [6, 2, 4, 7], [5, 1, 4, 7], [0, 1, 2, 4], [3, 1, 2, 7]]
2425
)
@@ -115,7 +116,6 @@ def evaluate_gradient(
115116
return values
116117

117118
def inside(self, pos: np.ndarray):
118-
119119
inside = np.ones(pos.shape[0]).astype(bool)
120120
for i in range(3):
121121
inside *= pos[:, i] > self.origin[None, i]

LoopStructural/interpolators/supports/_3d_unstructured_tetra.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from ._3d_base_structured import BaseStructuredSupport
1010
from LoopStructural.utils import getLogger
11+
from . import SupportType
1112

1213
logger = getLogger(__name__)
1314

@@ -39,6 +40,7 @@ def __init__(
3940
aabb_nsteps : list, optional
4041
force nsteps for aabb, by default None
4142
"""
43+
self.type = SupportType.UnStructuredTetMesh
4244
self.nodes = np.array(nodes)
4345
if self.nodes.shape[1] != 3:
4446
raise ValueError("Nodes must be 3D")
@@ -395,7 +397,6 @@ def get_element_for_location(self, points):
395397
npts_step = int(1e4)
396398
# break into blocks of 10k points
397399
while npts < points.shape[0]:
398-
399400
cell_index = np.array(
400401
self.aabb_grid.position_to_cell_index(points[: npts + npts_step, :])
401402
)

LoopStructural/interpolators/supports/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
22+
123
from ._2d_base_unstructured import BaseUnstructured2d
224
from ._2d_p1_unstructured import P1Unstructured2d
325
from ._2d_p2_unstructured import P2Unstructured2d
@@ -6,3 +28,26 @@
628
from ._3d_unstructured_tetra import UnStructuredTetMesh
729
from ._3d_structured_tetra import TetMesh
830
from ._3d_p2_tetra import P2UnstructuredTetMesh
31+
32+
support_map = {
33+
SupportType.StructuredGrid2D: StructuredGrid2D,
34+
SupportType.StructuredGrid: StructuredGrid,
35+
SupportType.UnStructuredTetMesh: UnStructuredTetMesh,
36+
SupportType.P1Unstructured2d: P1Unstructured2d,
37+
SupportType.P2Unstructured2d: P2Unstructured2d,
38+
SupportType.TetMesh: TetMesh,
39+
SupportType.P2UnstructuredTetMesh: P2UnstructuredTetMesh,
40+
}
41+
42+
__all__ = [
43+
"BaseUnstructured2d",
44+
"P1Unstructured2d",
45+
"P2Unstructured2d",
46+
"StructuredGrid2D",
47+
"StructuredGrid",
48+
"UnStructuredTetMesh",
49+
"TetMesh",
50+
"P2UnstructuredTetMesh",
51+
"support_map",
52+
"SupportType",
53+
]

0 commit comments

Comments
 (0)