From fe6c7f388a2e09cd8471bcea6c99f6fa1dca585e Mon Sep 17 00:00:00 2001 From: Tom Gustafsson Date: Mon, 17 Jun 2024 23:22:19 +0300 Subject: [PATCH] move to/from_dict to mesh.py --- skfem/io/json.py | 37 ++----------------------------------- skfem/mesh/mesh.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/skfem/io/json.py b/skfem/io/json.py index a762bf0fb..9f3d9b483 100644 --- a/skfem/io/json.py +++ b/skfem/io/json.py @@ -1,4 +1,4 @@ -"""Import mesh from JSON.""" +"""Import mesh from JSON file.""" import json from os import PathLike @@ -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) @@ -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): diff --git a/skfem/mesh/mesh.py b/skfem/mesh/mesh.py index 2d6cc6e0e..bd8aea0df 100644 --- a/skfem/mesh/mesh.py +++ b/skfem/mesh/mesh.py @@ -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):