Skip to content

Commit

Permalink
Merge pull request #12 from gerlero/dimensioned
Browse files Browse the repository at this point in the history
Update FoamDimensioned type
  • Loading branch information
gerlero authored Mar 19, 2024
2 parents fb42790 + 6fa85ad commit 20b845a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
39 changes: 28 additions & 11 deletions foamlib/_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MutableMapping,
)
from collections import namedtuple
from dataclasses import dataclass
from contextlib import suppress

from ._subprocesses import run_process, CalledProcessError
Expand All @@ -19,8 +20,6 @@
except ModuleNotFoundError:
np = None

FoamValue = Union[str, int, float, bool, Sequence["FoamValue"]]

FoamDimensionSet = namedtuple(
"FoamDimensionSet",
[
Expand All @@ -35,7 +34,30 @@
defaults=(0, 0, 0, 0, 0, 0, 0),
)

FoamDimensioned = namedtuple("FoamDimensioned", ["name", "dimensions", "value"])

@dataclass
class FoamDimensioned:
name: Optional[str] = None
dimensions: Union[FoamDimensionSet, Sequence[Union[int, float]]] = (
FoamDimensionSet()
)
value: Union["FoamValue", Sequence["FoamValue"]] = 0

def __post_init__(self) -> None:
if self.name is not None and not isinstance(self.name, str) and self.value == 0:
self.value = self.name
self.name = None

if not isinstance(self.dimensions, FoamDimensionSet):
self.dimensions = FoamDimensionSet(*self.dimensions)


FoamValue = Union[
str, int, float, bool, FoamDimensioned, FoamDimensionSet, Sequence["FoamValue"]
]
"""
A value that can be stored in an OpenFOAM dictionary.
"""


def _parse_bool(value: str) -> bool:
Expand Down Expand Up @@ -215,19 +237,14 @@ def _serialize_dimensions(value: Any) -> str:


def _serialize_dimensioned(value: Any) -> str:
if (
isinstance(value, Sequence)
and not isinstance(value, str)
and len(value) == 3
and isinstance(value[0], str)
):
return f"{value[0]} {_serialize_dimensions(value[1])} {_serialize(value[2])}"
if isinstance(value, FoamDimensioned):
return f"{value.name or 'unnamed'} {_serialize_dimensions(value.dimensions)} {_serialize(value.value)}"
else:
raise TypeError(f"Not a valid dimensioned value: {type(value)}")


def _serialize(
value: Any, assume_field: bool = False, assume_dimensions: bool = False
value: Any, *, assume_field: bool = False, assume_dimensions: bool = False
) -> str:
with suppress(TypeError):
return _serialize_mapping(value)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_write_read(tmp_path: Path) -> None:
sd["nestedList"] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
assert sd["nestedList"] == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

sd["g"] = ("g", [1, 1, -2, 0, 0, 0, 0], [0, 0, -9.81])
sd["g"] = FoamDimensioned("g", [1, 1, -2, 0, 0, 0, 0], [0, 0, -9.81])
assert sd["g"] == FoamDimensioned(
"g", FoamDimensionSet(mass=1, length=1, time=-2), [0, 0, -9.81]
)
Expand Down

0 comments on commit 20b845a

Please sign in to comment.