.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/MeshModification/demo_conversion.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_MeshModification_demo_conversion.py: .. currentmodule:: mymesh Conversion ========== The :mod:`mymesh.converter` module contains many functions to convert between different types of meshes. This example will highlight a few of them. .. GENERATED FROM PYTHON SOURCE LINES 12-14 We'll start with a volumetric sphere mesh, that begins as a mix of hexahedra, tetrahedra, pyramids, and wedges. Most of the sphere is made of hexahedra (gray), the core is made up of pyramids (orange), and the central axis is made up of wedges (blue), except in the core where there are a few small tetrahedra (green). .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: Python from mymesh import converter, primitives, mesh sphere = primitives.Sphere([0,0,0], 1, Type='vol') sphere.ElemData['Element Type'] = [len(elem) for elem in sphere.NodeConn] sphere.verbose = False sphere.Clip().plot(scalars='Element Type', show_edges=True, view='trimetric', color='Accent', show_colorbar=False) print(sphere.ElemType) .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_conversion_001.png :alt: demo conversion :srcset: /examples/MeshModification/images/sphx_glr_demo_conversion_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() ['hex', 'tet', 'pyr', 'wdg'] .. GENERATED FROM PYTHON SOURCE LINES 23-37 .. note:: Most functions in :mod:`~mymesh.converter` take as inputs :code:`NodeCoords`, :code:`NodeConn` and return updated versions of both. .. code:: NewCoords, NewConn = converter.solid2tets(NodeCoords, NodeConn) NewMesh = mesh(NewCoords, NewConn) can be simplified as .. code:: NewMesh = mesh(*converter.solid2tets(NodeCoords, NodeConn)) .. GENERATED FROM PYTHON SOURCE LINES 39-45 Element Type ------------ This mixed-element mesh can be converted to a purely tetrahedral mesh with :func:`~mymesh.converter.solid2tets`. Mixed-element surface meshes can be similarly converted to triangular meshes with :func:`~mymesh.converter.surf2tris`. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python tet_sphere = mesh(*converter.solid2tets(sphere.NodeCoords, sphere.NodeConn), verbose=False) print(tet_sphere.ElemType) .. rst-class:: sphx-glr-script-out .. code-block:: none ['tet'] .. GENERATED FROM PYTHON SOURCE LINES 49-53 Mesh Type --------- This mixed-element mesh can be converted to a purely tetrahedral mesh with :func:`~mymesh.converter.solid2surface`. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python surf_sphere = mesh(sphere.NodeCoords, converter.solid2surface(sphere.NodeCoords, sphere.NodeConn), verbose=False) print(surf_sphere.ElemType) .. rst-class:: sphx-glr-script-out .. code-block:: none ['tri', 'quad'] .. GENERATED FROM PYTHON SOURCE LINES 58-59 Or equivalently, the :attr:`~mymesh.mesh.mesh.Surface` property can be used .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python surf_sphere = sphere.Surface print(surf_sphere.ElemType) .. rst-class:: sphx-glr-script-out .. code-block:: none ['tri', 'quad'] .. GENERATED FROM PYTHON SOURCE LINES 64-73 Element Order ------------- Most meshes and functions in :mod:`mymesh` use first-order (or "linear") elements (e.g. 4 node tetrahedra, 8 node hexahedra) by default, but for some types of simulations, such as finite element methods, second-order (or "quadratic") elements (e.g. 10 node tetrahedra, 20 node hexahedra) are sometimes preferable for improved accuracy in simulations. This conversion can be achieved with :func:`~mymesh.converter.linear2quadratic`. See also :ref:`Element Types`. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: Python quadratic_sphere = mesh(*converter.linear2quadratic(sphere.NodeCoords, sphere.NodeConn), verbose=False) print(quadratic_sphere.ElemType) .. rst-class:: sphx-glr-script-out .. code-block:: none ['tet10', 'hex20', 'pyr13', 'wdg15'] .. GENERATED FROM PYTHON SOURCE LINES 77-83 The reverse operation can be performed with :func:`~mymesh.converter.quadratic2linear`. This function removes the excess nodes from the node connectivity, but keeps them in :code:`NodeCoords`, so any data associate with the nodes will still match with the mesh. To remove the excess nodes, you can use :meth:`~mymesh.mesh.mesh.cleanup` or :func:`mymesh.utils.RemoveNodes`. .. GENERATED FROM PYTHON SOURCE LINES 83-86 .. code-block:: Python linear_sphere = mesh(*converter.quadratic2linear(quadratic_sphere.NodeCoords, quadratic_sphere.NodeConn), verbose=False) print(linear_sphere.ElemType) .. rst-class:: sphx-glr-script-out .. code-block:: none ['hex', 'tet', 'pyr', 'wdg'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.234 seconds) .. _sphx_glr_download_examples_MeshModification_demo_conversion.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_conversion.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_conversion.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_conversion.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_