.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/MeshModification/demo_improvement.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_improvement.py: Mesh quality improvement ======================== See also: :mod:`~mymesh.improvement`, :mod:`~mymesh.quality`, :ref:`theory_improvement` High quality meshes are essential for accuracy in simulations (e.g. finite element, finite volume methods) and other mesh-based computation. There are different approaches to improving mesh quality, including smoothing and modifications to the mesh connectivity (see also: :ref:`theory_improvement`). Often, the best approach is to use a combination of these methods :cite:p:`Klingner2008`. The :func:`mymesh.improvement.Improve` function combines edge contraction for coarsening (:func:`~mymesh.improvement.Contract`), edge splitting for refinement (:func:`~mymesh.improvement.Split`), edge flipping (:func:`~mymesh.improvement.Flip`), and smoothing to improve mesh quality while also achieving a target edge length for triangular or tetrahedral meshes. :func:`~mymesh.improvement.Improve` repeats a schedule of operations, which is by default splitting ('s'), contraction ('c'), flipping ('f'), and smoothing ('S') defined by a string (``'scfS'``), however the schedule can be customized to remove or rearrange the order of operations (e.g. ``'Sfc'``). .. GENERATED FROM PYTHON SOURCE LINES 18-22 2D Surface Improvement ---------------------- :func:`~mymesh.improvement.Improve` can be used on a two dimensional triangular surface while preserving the original boundaries of the mesh .. GENERATED FROM PYTHON SOURCE LINES 22-42 .. code-block:: Python from mymesh import implicit, improvement, primitives, quality, visualize import numpy as np C = primitives.Circle([0,0,0], 1, ElemType='tri', theta_resolution=40, radial_resolution=20) C.ElemData['skew'] = quality.Skewness(*C) target_edge_length = 0.1 C2 = improvement.Improve(C, target_edge_length, schedule = 'scfS', repeat=10, verbose=False) C2.ElemData['skew'] = quality.Skewness(*C2) means = [np.mean(C.ElemData['skew']), np.mean(C2.ElemData['skew'])] C.plot(scalars='skew', clim=(0,1), show_edges=True, view='xy', title=f'Original\nMean Skewness={means[0]:.2f}') C2.plot(scalars='skew', clim=(0,1), show_edges=True, view='xy', title=f'Improved\nMean Skewness={means[1]:.2f}') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_improvement_001.png :alt: Original Mean Skewness=0.40 :srcset: /examples/MeshModification/images/sphx_glr_demo_improvement_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_improvement_002.png :alt: Improved Mean Skewness=0.02 :srcset: /examples/MeshModification/images/sphx_glr_demo_improvement_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() RFBOutputContext() .. GENERATED FROM PYTHON SOURCE LINES 43-49 3D Surface Improvement ---------------------- Similarly, three dimensional triangular surfaces can be improved. Implicit or image-based surfaces often contain some highly skewed elements as a result of the contouring process, which can be improved by :func:`~mymesh.improvement.Improve`. .. GENERATED FROM PYTHON SOURCE LINES 49-63 .. code-block:: Python S = implicit.SurfaceMesh(implicit.sphere([0,0,0], 1), [-1,1,-1,1,-1,1], .1) S.ElemData['skew'] = quality.Skewness(*S) target_edge_length = 0.2 S2 = improvement.Improve(S, target_edge_length, schedule='scfS', repeat=10, verbose=False) S2.ElemData['skew'] = quality.Skewness(*S2) means = [np.mean(S.ElemData['skew']), np.mean(S2.ElemData['skew'])] S.plot(scalars='skew', clim=(0,1), show_edges=True, view='trimetric', title=f'Original\nMean Skewness={means[0]:.2f}') S2.plot(scalars='skew', clim=(0,1), show_edges=True, view='trimetric', title=f'Improved\nMean Skewness={means[1]:.2f}') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_improvement_003.png :alt: Original Mean Skewness=0.40 :srcset: /examples/MeshModification/images/sphx_glr_demo_improvement_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_improvement_004.png :alt: Improved Mean Skewness=0.02 :srcset: /examples/MeshModification/images/sphx_glr_demo_improvement_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() RFBOutputContext() .. GENERATED FROM PYTHON SOURCE LINES 64-70 3D Volume Improvement --------------------- The tetrahedral meshes generated by :func:`~mymesh.contour.MarchingTetrahedra` (:func:`mymesh.implicit.TetMesh`, :func:`mymesh.image.TetMesh`) generally structured interiors with some low quality elements on the surface, however, even the structured interiors have elements of only 'average' quality. .. GENERATED FROM PYTHON SOURCE LINES 70-85 .. code-block:: Python S = implicit.TetMesh(implicit.sphere([0,0,0], 1), [-1,1,-1,1,-1,1], .1) S.ElemData['skew'] = quality.Skewness(*S) target_edge_length = 0.25 S2 = improvement.Improve(S, target_edge_length, schedule='scfS', repeat=20, verbose=False) S2.ElemData['skew'] = quality.Skewness(*S2) means = [np.mean(S.ElemData['skew']), np.mean(S2.ElemData['skew'])] S.Clip().plot(scalars='skew', clim=(0,1), show_edges=True, view='trimetric', title=f'Original\nMean Skewness={means[0]:.2f}') S2.Clip().plot(scalars='skew', clim=(0,1), show_edges=True, view='trimetric', title=f'Improved\nMean Skewness={means[1]:.2f}') .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_improvement_005.png :alt: Original Mean Skewness=0.59 :srcset: /examples/MeshModification/images/sphx_glr_demo_improvement_005.png :class: sphx-glr-multi-img * .. image-sg:: /examples/MeshModification/images/sphx_glr_demo_improvement_006.png :alt: Improved Mean Skewness=0.43 :srcset: /examples/MeshModification/images/sphx_glr_demo_improvement_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none RFBOutputContext() RFBOutputContext() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 34.806 seconds) .. _sphx_glr_download_examples_MeshModification_demo_improvement.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_improvement.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_improvement.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_improvement.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_