Skip to content

Commit

Permalink
Merge pull request #350 from kinnala/move-save-to-io
Browse files Browse the repository at this point in the history
Move save to io
  • Loading branch information
kinnala committed Mar 19, 2020
2 parents bcd1bb0 + f6b249e commit ac61dcd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
11 changes: 11 additions & 0 deletions skfem/io/meshio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
('line', skfem.MeshLine),
])

TYPE_MESH_MAPPING = {v: k for k, v in MESH_TYPE_MAPPING.items()}


def from_meshio(m, force_mesh_type=None):
"""Convert meshio mesh into :class:`skfem.mesh.Mesh`.
Expand Down Expand Up @@ -130,3 +132,12 @@ def find_tagname(tag):

def from_file(filename):
return from_meshio(meshio.read(filename))


def to_meshio(mesh, point_data=None):
cells = {TYPE_MESH_MAPPING[type(mesh)]: mesh.t.T}
return meshio.Mesh(mesh.p.T, cells, point_data)


def to_file(mesh, filename, point_data=None, **kwargs):
meshio.write(filename, to_meshio(mesh, point_data), **kwargs)
23 changes: 3 additions & 20 deletions skfem/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ def _validate(self):

def save(self,
filename: str,
point_data: Optional[Dict[str, ndarray]] = None,
cell_data: Optional[Dict[str, ndarray]] = None,
point_data: Dict[str, ndarray] = None,
**kwargs) -> None:
"""Export the mesh and fields using meshio.
Expand All @@ -256,26 +255,10 @@ def save(self,
e.g. .msh, .vtk, .xdmf
point_data
Data related to the vertices of the mesh.
cell_data
Data related to the elements of the mesh.
"""
import meshio

if point_data is not None:
if not isinstance(point_data, dict):
raise ValueError("point_data should be "
"a dictionary of ndarrays.")

if cell_data is not None:
if not isinstance(cell_data, dict):
raise ValueError("cell_data should be "
"a dictionary of ndarrays.")
cell_data = {self.meshio_type: cell_data}

cells = {self.meshio_type: self.t.T}
mesh = meshio.Mesh(self.p.T, cells, point_data, cell_data)
meshio.write(filename, mesh, **kwargs)
from skfem.io.meshio import to_file
return to_file(self, filename, point_data, **kwargs)

@classmethod
def from_basis(cls: Type[MeshType], basis):
Expand Down
2 changes: 1 addition & 1 deletion skfem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def funv(v, dv, w):

@bilinear_form
def deriv(u, du, v, dv, w):
return du[i] * v
return du[diff] * v

M = asm(mass, basis_to)

Expand Down

0 comments on commit ac61dcd

Please sign in to comment.