Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move save to io #350

Merged
merged 4 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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