From f244fb277e9fe6743687d99982e102300dd1684b Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Thu, 22 Mar 2018 18:27:52 -0500 Subject: [PATCH] Add function to write VTK file for vertex-based mesh --- doc/mesh.rst | 5 ++ meshmode/mesh/refinement/__init__.py | 2 + meshmode/mesh/visualization.py | 71 ++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/doc/mesh.rst b/doc/mesh.rst index 60de797e..ccfba1e7 100644 --- a/doc/mesh.rst +++ b/doc/mesh.rst @@ -70,4 +70,9 @@ Mesh refinement .. automodule:: meshmode.mesh.refinement +Mesh visualization +--------------- + +.. automodule:: meshmode.mesh.visualization + .. vim: sw=4 diff --git a/meshmode/mesh/refinement/__init__.py b/meshmode/mesh/refinement/__init__.py index 7e63a164..6cfaa101 100644 --- a/meshmode/mesh/refinement/__init__.py +++ b/meshmode/mesh/refinement/__init__.py @@ -9,8 +9,10 @@ in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE diff --git a/meshmode/mesh/visualization.py b/meshmode/mesh/visualization.py index a1b14707..2c97d2f6 100644 --- a/meshmode/mesh/visualization.py +++ b/meshmode/mesh/visualization.py @@ -25,6 +25,12 @@ THE SOFTWARE. import numpy as np from six.moves import range +__doc__ = """ +.. autofunction:: draw_2d_mesh +.. autofunction:: draw_curve +.. autofunction:: write_vertex_vtk_file +""" + # {{{ draw_2d_mesh @@ -162,4 +168,69 @@ def draw_curve(mesh, # }}} + +# {{{ write_vtk_file + +def write_vertex_vtk_file(mesh, file_name, compressor=None): + from pyvisfile.vtk import ( + UnstructuredGrid, DataArray, + AppendedDataXMLGenerator, + VF_LIST_OF_COMPONENTS) + + # {{{ create cell_types + + from pyvisfile.vtk import ( + VTK_LINE, VTK_TRIANGLE, VTK_TETRA, + VTK_QUAD, VTK_HEXAHEDRON) + + from meshmode.mesh import TensorProductElementGroup, SimplexElementGroup + + cell_types = np.empty(mesh.nelements, dtype=np.uint8) + cell_types.fill(255) + for egrp in mesh.groups: + if isinstance(egrp, SimplexElementGroup): + vtk_cell_type = { + 1: VTK_LINE, + 2: VTK_TRIANGLE, + 3: VTK_TETRA, + }[egrp.dim] + elif isinstance(egrp, TensorProductElementGroup): + vtk_cell_type = { + 1: VTK_LINE, + 2: VTK_QUAD, + 3: VTK_HEXAHEDRON, + }[egrp.dim] + else: + raise NotImplementedError("mesh vtk file writing for " + "element group of type '%s'" % type(egrp).__name__) + + cell_types[ + egrp.element_nr_base: + egrp.element_nr_base + egrp.nelements] = \ + vtk_cell_type + + assert (cell_types != 255).all() + + # }}} + + grid = UnstructuredGrid( + (mesh.nvertices, + DataArray("points", + mesh.vertices, + vector_format=VF_LIST_OF_COMPONENTS)), + cells=np.hstack([ + vgrp.vertex_indices.reshape(-1) + for vgrp in mesh.groups]), + cell_types=cell_types) + + from os.path import exists + if exists(file_name): + raise RuntimeError("output file '%s' already exists" + % file_name) + + with open(file_name, "w") as outf: + AppendedDataXMLGenerator(compressor)(grid).write(outf) + +# }}} + # vim: foldmethod=marker -- GitLab