.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/geometry/example_surface_geometry.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__gallery_geometry_example_surface_geometry.py: .. _example_surface_geometry: Areas, centers and normals of surface meshes ============================================ .. contents:: Table of Contents :local: :depth: 1 :backlinks: top The methods of :class:`pysdic.Mesh` compute the geometry of 3D surface meshes: the areas, centers and normals of the elements, the normals at the vertices and at integration points. They are computed from the Jacobian matrices of the elements, so they work for all the surface element types (``triangle_3``, ``triangle_6``, ``quadrangle_4``, ``quadrangle_8``). The orientation of the normals follows the order of the vertices of the elements (the ``direct`` argument of the ``create_3d_*`` functions). .. GENERATED FROM PYTHON SOURCE LINES 24-29 Areas of the elements --------------------- The area of an element is integrated with the Gauss points of its type. For a cylinder of radius 1 and height 2, the total area tends to :math:`4\pi` when the mesh is refined. .. GENERATED FROM PYTHON SOURCE LINES 29-45 .. code-block:: Python import numpy from pysdic import Plotter3D, create_3d_quadrangle_4_cylindrical n_t = 20 cylinder = create_3d_quadrangle_4_cylindrical( t_bounds=(0.0, 2 * numpy.pi * (1 - 1 / n_t)), h_bounds=(0.0, 2.0), n_t=n_t, n_h=6, close=True ) areas = cylinder.compute_elements_areas() # (N_e,) print(f"Total area: {areas.sum():.4f} (cylinder: {4 * numpy.pi:.4f})") plotter = Plotter3D() plotter.add_mesh(cylinder, element_property=areas, show_bar=True, bar={"title": "Area"}, show_vertices=False) plotter.set_title("Areas of the elements") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_surface_geometry_001.png :alt: example surface geometry :srcset: /_gallery/geometry/images/sphx_glr_example_surface_geometry_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Total area: 12.5148 (cylinder: 12.5664) .. GENERATED FROM PYTHON SOURCE LINES 46-52 Normals of the elements ----------------------- The normal of an element is computed at its center (see :meth:`pysdic.Mesh.compute_elements_centers`). Here it points outward, along the radial direction. .. GENERATED FROM PYTHON SOURCE LINES 52-63 .. code-block:: Python centers = cylinder.compute_elements_centers() # (N_e, 3) normals = cylinder.compute_elements_normals() # (N_e, 3) radial = centers[:, :2] / numpy.linalg.norm(centers[:, :2], axis=1, keepdims=True) print(f"Max deviation from the radial direction: {numpy.abs(normals[:, :2] - radial).max():.1e}") plotter = Plotter3D() plotter.add_mesh(cylinder, face_color="lightgray", show_vertices=False, element_vectors=normals, vector_scale=0.3, vector_color="red") plotter.set_title("Normals of the elements") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_surface_geometry_002.png :alt: example surface geometry :srcset: /_gallery/geometry/images/sphx_glr_example_surface_geometry_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Max deviation from the radial direction: 3.3e-16 .. GENERATED FROM PYTHON SOURCE LINES 64-69 Normals at the vertices ----------------------- The normal at a vertex is the mean of the normals of its elements, weighted by their areas. With ``direct=False``, the normals of the sphere point outward. .. GENERATED FROM PYTHON SOURCE LINES 69-87 .. code-block:: Python from pysdic import create_3d_triangle_3_spherical n_p = 24 sphere = create_3d_triangle_3_spherical( t_bounds=(0.15, numpy.pi - 0.15), p_bounds=(0.0, 2 * numpy.pi * (1 - 1 / n_p)), n_t=12, n_p=n_p, close=True, direct=False, ) vertex_normals = sphere.compute_vertices_normals() # (N_v, 3) radial = sphere.coordinates / numpy.linalg.norm(sphere.coordinates, axis=1, keepdims=True) angles = numpy.degrees(numpy.arccos(numpy.clip((vertex_normals * radial).sum(axis=1), -1.0, 1.0))) print(f"Mean angle with the radial direction: {angles.mean():.2f} deg") plotter = Plotter3D() plotter.add_mesh(sphere, face_color="lightblue", vertex_size=3, vertex_vectors=vertex_normals, vector_scale=0.2, vector_color="navy") plotter.set_title("Normals at the vertices") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_surface_geometry_003.png :alt: example surface geometry :srcset: /_gallery/geometry/images/sphx_glr_example_surface_geometry_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mean angle with the radial direction: 1.55 deg .. GENERATED FROM PYTHON SOURCE LINES 88-93 Normals at integration points ----------------------------- On a curved element (here the bilinear quadrangles of the surface :math:`z = 0.4\,xy`), the normal varies inside the element: it can be computed at any integration point. .. GENERATED FROM PYTHON SOURCE LINES 93-105 .. code-block:: Python from pysdic import IntegrationPoints, create_3d_quadrangle_4_heightmap heightmap = create_3d_quadrangle_4_heightmap(lambda x, y: 0.4 * x * y, x_bounds=(-1.0, 1.0), y_bounds=(-1.0, 1.0), n_x=4, n_y=4) gauss_points = IntegrationPoints.from_gauss_points(heightmap.element_type, heightmap.n_elements) gauss_points["normal"] = heightmap.compute_integration_points_normals(gauss_points) # (N_p, 3) print(f"Normals at the Gauss points of element 0:\n{gauss_points['normal'][gauss_points.element_indices == 0]}") plotter = Plotter3D() plotter.add_mesh(heightmap, face_color="wheat", show_vertices=False) plotter.add_integration_points(gauss_points, heightmap, color="black", size=6, vectors="normal", vector_scale=0.25, vector_color="darkgreen") plotter.set_title("Normals at the Gauss points") plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_surface_geometry_004.png :alt: example surface geometry :srcset: /_gallery/geometry/images/sphx_glr_example_surface_geometry_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Normals at the Gauss points of element 0: [[0.30907954 0.30907954 0.89941074] [0.31988608 0.1765712 0.9308574 ] [0.18320909 0.18320909 0.96585136] [0.1765712 0.31988608 0.9308574 ]] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.755 seconds) .. _sphx_glr_download__gallery_geometry_example_surface_geometry.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_surface_geometry.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_surface_geometry.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_surface_geometry.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_