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.
- loop_common/__init__.py +6 -0
- loop_common/base.py +98 -0
- loop_common/geometry/__init__.py +19 -0
- loop_common/geometry/_aabb.py +37 -0
- loop_common/geometry/_bounding_box.py +792 -0
- loop_common/geometry/_face_table.py +42 -0
- loop_common/geometry/_point.py +238 -0
- loop_common/geometry/_structured_grid.py +114 -0
- loop_common/geometry/_structured_grid_2d.py +146 -0
- loop_common/geometry/_structured_grid_3d.py +243 -0
- loop_common/geometry/_surface.py +288 -0
- loop_common/geometry/_unstructured_mesh.py +208 -0
- loop_common/interfaces/__init__.py +0 -0
- loop_common/interfaces/representation.py +45 -0
- loop_common/io/__init__.py +0 -0
- loop_common/logging/__init__.py +14 -0
- loop_common/logging/logger.py +123 -0
- loop_common/logging/sinks.py +221 -0
- loop_common/logging/timing.py +113 -0
- loop_common/math/__init__.py +5 -0
- loop_common/math/_maths.py +441 -0
- loop_common/math/_transformation.py +178 -0
- loop_common/math/finite_difference_stencil.py +38 -0
- loop_common/observations/__init__.py +3 -0
- loop_common/observations/lineset.py +41 -0
- loop_common/observations/orientation.py +122 -0
- loop_common/observations/pointset.py +8 -0
- loop_common/observer.py +284 -0
- loop_common/supports/_2d_base_unstructured.py +361 -0
- loop_common/supports/_2d_p1_unstructured.py +131 -0
- loop_common/supports/_2d_p2_unstructured.py +349 -0
- loop_common/supports/_2d_structured_grid.py +545 -0
- loop_common/supports/_3d_base_structured.py +546 -0
- loop_common/supports/_3d_p2_tetra.py +374 -0
- loop_common/supports/_3d_rectilinear_grid.py +315 -0
- loop_common/supports/_3d_structured_grid.py +510 -0
- loop_common/supports/_3d_structured_tetra.py +764 -0
- loop_common/supports/_3d_unstructured_tetra.py +656 -0
- loop_common/supports/__init__.py +69 -0
- loop_common/supports/_aabb.py +77 -0
- loop_common/supports/_base_support.py +118 -0
- loop_common/supports/_face_table.py +70 -0
- loop_common/supports/_p2_structured_tetra.py +627 -0
- loop_common/supports/_support_factory.py +78 -0
- loop_common/utils.py +74 -0
- loop_common-0.0.1.dist-info/METADATA +35 -0
- loop_common-0.0.1.dist-info/RECORD +50 -0
- loop_common-0.0.1.dist-info/WHEEL +5 -0
- loop_common-0.0.1.dist-info/licenses/LICENSE +21 -0
- loop_common-0.0.1.dist-info/top_level.txt +1 -0
loop_common/__init__.py
ADDED
loop_common/base.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import uuid
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Annotated, Any
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
from pydantic import BaseModel, BeforeValidator, ConfigDict, Field, PlainSerializer
|
|
10
|
+
|
|
11
|
+
from loop_common.logging import get_logger as getLogger
|
|
12
|
+
|
|
13
|
+
logger = getLogger(__name__)
|
|
14
|
+
# --- 1. The NumPy Type Logic ---
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def validate_numpy(v: Any) -> np.ndarray:
|
|
18
|
+
"""Ensures input is converted to a numpy array."""
|
|
19
|
+
if isinstance(v, np.ndarray):
|
|
20
|
+
return v
|
|
21
|
+
try:
|
|
22
|
+
return np.array(v)
|
|
23
|
+
except Exception as e:
|
|
24
|
+
raise ValueError(f"Could not convert {type(v)} to numpy array") from e
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Define a 'NumpyArray' type that:
|
|
28
|
+
# - Converts lists/tuples to arrays during input (BeforeValidator)
|
|
29
|
+
# - Converts arrays to lists during JSON export (PlainSerializer)
|
|
30
|
+
NumpyArray = Annotated[
|
|
31
|
+
np.ndarray,
|
|
32
|
+
BeforeValidator(validate_numpy),
|
|
33
|
+
PlainSerializer(lambda x: x.tolist(), return_type=list),
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
# --- 2. The Base Entity ---
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class LoopEntity(BaseModel):
|
|
40
|
+
"""
|
|
41
|
+
The atomic building block for all Loop objects.
|
|
42
|
+
Provides identity, validation, and serialization.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
# Allow Pydantic to handle non-pydantic types (like numpy arrays)
|
|
46
|
+
model_config = ConfigDict(
|
|
47
|
+
arbitrary_types_allowed=True,
|
|
48
|
+
validate_assignment=True, # Validate if user changes a value later
|
|
49
|
+
extra="forbid", # Prevent accidental typos from creating new fields
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
uuid: str = Field(
|
|
53
|
+
default_factory=lambda: str(uuid.uuid4()), description="Permanent unique identifier"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
name: str | None = Field(default=None, description="Human-readable label")
|
|
57
|
+
|
|
58
|
+
last_modified: str = Field(
|
|
59
|
+
default_factory=lambda: datetime.now(tz=timezone.utc).isoformat(),
|
|
60
|
+
description="ISO timestamp of last change",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
def mark_modified(self):
|
|
64
|
+
"""Manually trigger a timestamp update."""
|
|
65
|
+
self.last_modified = datetime.now(tz=timezone.utc).isoformat()
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def from_json(cls, json_str: str):
|
|
69
|
+
"""Helper to reconstruct the object from a JSON string."""
|
|
70
|
+
return cls.model_validate_json(json_str)
|
|
71
|
+
|
|
72
|
+
def to_json(self, indent: int = 2) -> str:
|
|
73
|
+
"""Helper to export to JSON string."""
|
|
74
|
+
return self.model_dump_json(indent=indent)
|
|
75
|
+
|
|
76
|
+
def to_yaml(self) -> str:
|
|
77
|
+
"""Export to YAML string."""
|
|
78
|
+
try:
|
|
79
|
+
import yaml
|
|
80
|
+
except ImportError as exc:
|
|
81
|
+
raise ImportError("PyYAML is required for YAML export: pip install pyyaml") from exc
|
|
82
|
+
return yaml.dump(self.model_dump(mode="json"), sort_keys=False, allow_unicode=True)
|
|
83
|
+
|
|
84
|
+
def to_dict(self) -> dict:
|
|
85
|
+
"""Export to a plain dict (JSON-safe: numpy arrays converted to lists)."""
|
|
86
|
+
return self.model_dump(mode="json")
|
|
87
|
+
|
|
88
|
+
def save(self, filename: Path):
|
|
89
|
+
filename = Path(filename)
|
|
90
|
+
filetype = filename.suffix.lstrip(".")
|
|
91
|
+
if filetype == "json":
|
|
92
|
+
with open(filename,'w') as f:
|
|
93
|
+
f.write(self.to_json())
|
|
94
|
+
elif filetype == 'yaml':
|
|
95
|
+
with open(filename,'w') as f:
|
|
96
|
+
f.write(self.to_yaml())
|
|
97
|
+
else:
|
|
98
|
+
logger.warning(f"Unknown filetype {filetype}. Not saving.")
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from ._bounding_box import BoundingBox
|
|
2
|
+
from ._point import ValuePoints, VectorPoints
|
|
3
|
+
from ._structured_grid import StructuredGrid
|
|
4
|
+
from ._structured_grid_2d import StructuredGrid2DGeometry
|
|
5
|
+
from ._structured_grid_3d import StructuredGrid3DGeometry
|
|
6
|
+
from ._surface import Surface
|
|
7
|
+
from ._unstructured_mesh import UnstructuredMesh2DGeometry, UnstructuredMeshGeometry
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"BoundingBox",
|
|
11
|
+
"StructuredGrid",
|
|
12
|
+
"StructuredGrid2DGeometry",
|
|
13
|
+
"StructuredGrid3DGeometry",
|
|
14
|
+
"Surface",
|
|
15
|
+
"UnstructuredMesh2DGeometry",
|
|
16
|
+
"UnstructuredMeshGeometry",
|
|
17
|
+
"ValuePoints",
|
|
18
|
+
"VectorPoints",
|
|
19
|
+
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from scipy import sparse
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def _initialise_aabb(grid):
|
|
6
|
+
minx = np.min(grid.nodes[grid.elements[:, :4], 0], axis=1)
|
|
7
|
+
maxx = np.max(grid.nodes[grid.elements[:, :4], 0], axis=1)
|
|
8
|
+
miny = np.min(grid.nodes[grid.elements[:, :4], 1], axis=1)
|
|
9
|
+
maxy = np.max(grid.nodes[grid.elements[:, :4], 1], axis=1)
|
|
10
|
+
|
|
11
|
+
cell_indexes = grid.aabb_grid.global_index_to_cell_index(np.arange(grid.aabb_grid.n_elements))
|
|
12
|
+
corners = grid.aabb_grid.cell_corner_indexes(cell_indexes)
|
|
13
|
+
positions = grid.aabb_grid.node_indexes_to_position(corners)
|
|
14
|
+
x_boundary = positions[:, [0, 1], 0]
|
|
15
|
+
y_boundary = positions[:, [0, 2], 1]
|
|
16
|
+
a = np.logical_and(minx[None, :] > x_boundary[:, None, 0], minx[None, :] < x_boundary[:, None, 1])
|
|
17
|
+
b = np.logical_and(maxx[None, :] < x_boundary[:, None, 1], maxx[None, :] > x_boundary[:, None, 0])
|
|
18
|
+
c = np.logical_and(minx[None, :] < x_boundary[:, None, 0], maxx[None, :] > x_boundary[:, None, 0])
|
|
19
|
+
x_logic = np.logical_or(np.logical_or(a, b), c)
|
|
20
|
+
|
|
21
|
+
a = np.logical_and(miny[None, :] > y_boundary[:, None, 0], miny[None, :] < y_boundary[:, None, 1])
|
|
22
|
+
b = np.logical_and(maxy[None, :] < y_boundary[:, None, 1], maxy[None, :] > y_boundary[:, None, 0])
|
|
23
|
+
c = np.logical_and(miny[None, :] < y_boundary[:, None, 0], maxy[None, :] > y_boundary[:, None, 0])
|
|
24
|
+
y_logic = np.logical_or(np.logical_or(a, b), c)
|
|
25
|
+
logic = np.logical_and(x_logic, y_logic)
|
|
26
|
+
|
|
27
|
+
if grid.dimension == 3:
|
|
28
|
+
z_boundary = positions[:, [0, 6], 2]
|
|
29
|
+
minz = np.min(grid.nodes[grid.elements[:, :4], 2], axis=1)
|
|
30
|
+
maxz = np.max(grid.nodes[grid.elements[:, :4], 2], axis=1)
|
|
31
|
+
a = np.logical_and(minz[None, :] > z_boundary[:, None, 0], minz[None, :] < z_boundary[:, None, 1])
|
|
32
|
+
b = np.logical_and(maxz[None, :] < z_boundary[:, None, 1], maxz[None, :] > z_boundary[:, None, 0])
|
|
33
|
+
c = np.logical_and(minz[None, :] < z_boundary[:, None, 0], maxz[None, :] > z_boundary[:, None, 0])
|
|
34
|
+
z_logic = np.logical_or(np.logical_or(a, b), c)
|
|
35
|
+
logic = np.logical_and(logic, z_logic)
|
|
36
|
+
|
|
37
|
+
grid._aabb_table = sparse.csr_matrix(logic)
|