Skip to content

Commit

Permalink
move to/from_dict to mesh.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kinnala committed Jun 17, 2024
1 parent baf82f2 commit fe6c7f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
37 changes: 2 additions & 35 deletions skfem/io/json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Import mesh from JSON."""
"""Import mesh from JSON file."""

import json
from os import PathLike
Expand All @@ -9,39 +9,6 @@
from skfem.mesh import MeshLine1, MeshTri, MeshQuad, MeshTet, MeshHex, Mesh


def to_dict(m):
boundaries = None
subdomains = None
if m.boundaries is not None:
boundaries = {k: v.tolist() for k, v in m.boundaries.items()}
if m.subdomains is not None:
subdomains = {k: v.tolist() for k, v in m.subdomains.items()}
return {
'p': m.p.T.tolist(),
't': m.t.T.tolist(),
'boundaries': boundaries,
'subdomains': subdomains,
}


def from_dict(cls, data):
if 'p' not in data or 't' not in data:
raise ValueError("Dictionary must contain keys 'p' and 't'.")
else:
data['p'] = np.ascontiguousarray(np.array(data['p']).T)
data['t'] = np.ascontiguousarray(np.array(data['t']).T)
if 'boundaries' in data and data['boundaries'] is not None:
data['boundaries'] = {k: np.array(v)
for k, v in data['boundaries'].items()}
if 'subdomains' in data and data['subdomains'] is not None:
data['subdomains'] = {k: np.array(v)
for k, v in data['subdomains'].items()}
data['doflocs'] = data.pop('p')
data['_subdomains'] = data.pop('subdomains')
data['_boundaries'] = data.pop('boundaries')
return cls(**data)


def from_file(filename: PathLike) -> Mesh:
with open(filename, 'r') as handle:
d = json.load(handle)
Expand All @@ -65,7 +32,7 @@ def from_file(filename: PathLike) -> Mesh:
else:
raise NotImplementedError("The given mesh is not supported.")

return from_dict(mesh_type, d)
return mesh_type.from_dict(d)


def to_file(mesh: Mesh, filename: str):
Expand Down
33 changes: 29 additions & 4 deletions skfem/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,12 +761,37 @@ def load(cls,

@classmethod
def from_dict(cls, d):
from skfem.io.json import from_dict
return from_dict(cls, d)

if 'p' not in data or 't' not in data:
raise ValueError("Dictionary must contain keys 'p' and 't'.")
else:
data['p'] = np.ascontiguousarray(np.array(data['p']).T)
data['t'] = np.ascontiguousarray(np.array(data['t']).T)
if 'boundaries' in data and data['boundaries'] is not None:
data['boundaries'] = {k: np.array(v)
for k, v in data['boundaries'].items()}
if 'subdomains' in data and data['subdomains'] is not None:
data['subdomains'] = {k: np.array(v)
for k, v in data['subdomains'].items()}
data['doflocs'] = data.pop('p')
data['_subdomains'] = data.pop('subdomains')
data['_boundaries'] = data.pop('boundaries')
return cls(**data)

def to_dict(self):
from skfem.io.json import to_dict
return to_dict(self)

boundaries = None
subdomains = None
if self.boundaries is not None:
boundaries = {k: v.tolist() for k, v in self.boundaries.items()}
if self.subdomains is not None:
subdomains = {k: v.tolist() for k, v in self.subdomains.items()}
return {
'p': self.p.T.tolist(),
't': self.t.T.tolist(),
'boundaries': boundaries,
'subdomains': subdomains,
}

@classmethod
def from_mesh(cls, mesh, t: Optional[ndarray] = None):
Expand Down

0 comments on commit fe6c7f3

Please sign in to comment.