.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/geometry/example_integration_points.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_integration_points.py: .. _example_integration_points: Integration points: interpolation, derivatives and projection ============================================================= .. contents:: Table of Contents :local: :depth: 1 :backlinks: top An :class:`pysdic.IntegrationPoints` object locates points in the elements of a mesh by their element index and natural coordinates :math:`\Xi`. The methods of :class:`pysdic.Mesh` use them to interpolate a property given at the vertices with the shape functions, .. math:: u(\Xi) = \sum_{i=1}^{N_{vpe}} N_i(\Xi) \, u_i, to compute its derivatives and strains, and to project values given at the points back to the vertices. .. seealso:: :mod:`pysdic.fem` for the same operations on raw arrays. .. GENERATED FROM PYTHON SOURCE LINES 31-38 Mesh and Gauss points --------------------- :meth:`pysdic.IntegrationPoints.from_gauss_points` creates the Gauss points of all the elements (4 per ``quadrangle_4``), and :meth:`pysdic.Mesh.compute_integration_points_coordinates` gives their global coordinates. .. GENERATED FROM PYTHON SOURCE LINES 38-56 .. code-block:: Python import numpy from pysdic import IntegrationPoints, Plotter3D, create_2d_quadrangle_4_rectangle mesh = create_2d_quadrangle_4_rectangle(x_bounds=(0.0, 2.0), y_bounds=(0.0, 1.0), n_x=9, n_y=5) gauss_points = IntegrationPoints.from_gauss_points(mesh.element_type, mesh.n_elements) print(gauss_points) coordinates = mesh.compute_integration_points_coordinates(gauss_points).coordinates # (N_p, 2) print(f"First Gauss point (element {gauss_points.element_indices[0]}): {coordinates[0]}") plotter = Plotter3D() plotter.add_mesh(mesh, face_color="lightgray", show_vertices=False) plotter.add_integration_points(gauss_points, mesh, color="red", size=6) plotter.set_title("Gauss points of a quadrangle_4 mesh") plotter.plotter.view_xy() plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_integration_points_001.png :alt: example integration points :srcset: /_gallery/geometry/images/sphx_glr_example_integration_points_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none IntegrationPoints(n_points=128, n_located=128, n_topological_dimensions=2) First Gauss point (element 0): [0.05283122 0.05283122] .. GENERATED FROM PYTHON SOURCE LINES 57-63 Interpolation of a property --------------------------- A property of the vertices (here a displacement field) is interpolated at the points with :meth:`pysdic.Mesh.compute_property_interpolation`. The property is given by its name or as an array, and the result always has shape :math:`(N_p, P)`. .. GENERATED FROM PYTHON SOURCE LINES 63-78 .. code-block:: Python x, y = mesh.coordinates[:, 0], mesh.coordinates[:, 1] mesh.vertices["displacement"] = numpy.stack((0.05 * x * y, 0.02 * x**2), axis=1) # (N_v, 2) gauss_points["displacement"] = mesh.compute_property_interpolation("displacement", gauss_points) # (N_p, 2) plotter = Plotter3D() plotter.add_mesh(mesh, warp_by="displacement", face_color="lightgray", show_vertices=False) plotter.add_integration_points( gauss_points, mesh, warp_by="displacement", property="displacement", size=8, show_bar=True, bar={"title": "|u|"}, vectors="displacement", vector_scale=3.0, ) plotter.set_title("Displacement at the Gauss points (arrows x3), on the deformed mesh") plotter.plotter.view_xy() plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_integration_points_002.png :alt: example integration points :srcset: /_gallery/geometry/images/sphx_glr_example_integration_points_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 79-81 The interpolation is linear in the values of the vertices: it is the product with the shape function matrix :math:`\mathcal{N}` of shape :math:`(N_p, N_v)`. .. GENERATED FROM PYTHON SOURCE LINES 81-86 .. code-block:: Python N = mesh.compute_shape_function_matrix(gauss_points, sparse=True) difference = numpy.abs(N @ mesh.vertices["displacement"] - gauss_points["displacement"]).max() print(f"Shape function matrix {N.shape}: max difference with the interpolation {difference:.1e}") .. rst-class:: sphx-glr-script-out .. code-block:: none Shape function matrix (128, 45): max difference with the interpolation 1.4e-17 .. GENERATED FROM PYTHON SOURCE LINES 87-95 Derivatives and strains ----------------------- :meth:`pysdic.Mesh.compute_strains` gives the linear strain tensor :math:`\varepsilon = \frac{1}{2}(\nabla u + \nabla u^T)` at the points, of shape :math:`(N_p, 2, 2)` (see :meth:`pysdic.Mesh.compute_property_derivative` for the gradient itself). Here :math:`u_x = 0.05\,xy` is bilinear, so :math:`\varepsilon_{xx} = 0.05\,y` is exact. .. GENERATED FROM PYTHON SOURCE LINES 95-107 .. code-block:: Python strains = mesh.compute_strains("displacement", gauss_points) gauss_points["strain_xx"] = strains[:, 0, 0] print(f"Max difference with 0.05 y: {numpy.abs(strains[:, 0, 0] - 0.05 * coordinates[:, 1]).max():.1e}") plotter = Plotter3D() plotter.add_mesh(mesh, face_color="lightgray", show_vertices=False) plotter.add_integration_points(gauss_points, mesh, property="strain_xx", size=10, show_bar=True, bar={"title": "eps_xx"}) plotter.set_title("Strain eps_xx at the Gauss points") plotter.plotter.view_xy() plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_integration_points_003.png :alt: example integration points :srcset: /_gallery/geometry/images/sphx_glr_example_integration_points_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Max difference with 0.05 y: 9.0e-17 .. GENERATED FROM PYTHON SOURCE LINES 108-116 Projection to the vertices -------------------------- :meth:`pysdic.Mesh.compute_property_projection` does the reverse operation: the values at the vertices are fitted by least squares on the values at the points (weighted by ``integration_points.weights``). A property interpolated from the vertices is recovered, as long as the points are enough to determine the values of the vertices (here 4 points per element). .. GENERATED FROM PYTHON SOURCE LINES 116-120 .. code-block:: Python projected = mesh.compute_property_projection("displacement", gauss_points) # (N_v, 2) print(f"Max projection error: {numpy.abs(projected - mesh.vertices['displacement']).max():.1e}") .. rst-class:: sphx-glr-script-out .. code-block:: none Max projection error: 1.7e-16 .. GENERATED FROM PYTHON SOURCE LINES 121-127 Neighborhoods ------------- :meth:`pysdic.Mesh.compute_integration_points_neighborhood` gives, for each element, the points located in the elements at a distance :math:`\leq` ``max_distance`` (two elements are at distance 1 if they share a vertex). .. GENERATED FROM PYTHON SOURCE LINES 127-142 .. code-block:: Python neighborhood = mesh.compute_integration_points_neighborhood(gauss_points, max_distance=1) # (N_e, N_p) element = 13 selected = numpy.zeros(gauss_points.n_points, dtype=bool) selected[neighborhood[element].indices] = True print(f"Points in the neighborhood of element {element}: {selected.sum()}") plotter = Plotter3D() plotter.add_mesh(mesh, face_color="lightgray", show_vertices=False) plotter.add_integration_points(gauss_points, mesh, mask=~selected, color="black", size=5) plotter.add_integration_points(gauss_points, mesh, mask=selected, color="red", size=9) plotter.set_title(f"Gauss points in the neighborhood of element {element}") plotter.plotter.view_xy() plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_integration_points_004.png :alt: example integration points :srcset: /_gallery/geometry/images/sphx_glr_example_integration_points_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Points in the neighborhood of element 13: 36 .. GENERATED FROM PYTHON SOURCE LINES 143-149 Removing elements ----------------- When elements are removed from a mesh, :meth:`pysdic.IntegrationPoints.filter_elements` updates the points with the same mask: the points of the removed elements are unlocated (element index ``-1``, not displayed) and the other element indices are renumbered. .. GENERATED FROM PYTHON SOURCE LINES 149-161 .. code-block:: Python keep = mesh.compute_elements_centers()[:, 0] < 1.0 # The elements with x < 1 filtered_mesh = mesh.filter_connectivity(keep).remove_unused_vertices() filtered_points = gauss_points.filter_elements(keep) filtered_mesh.validate_integration_points(filtered_points) print(f"Located points: {filtered_points.n_located} / {filtered_points.n_points}") plotter = Plotter3D() plotter.add_mesh(filtered_mesh, face_color="lightgray", show_vertices=False) plotter.add_integration_points(filtered_points, filtered_mesh, color="red", size=6) plotter.set_title("Gauss points after removing the elements with x > 1") plotter.plotter.view_xy() plotter.show() .. image-sg:: /_gallery/geometry/images/sphx_glr_example_integration_points_005.png :alt: example integration points :srcset: /_gallery/geometry/images/sphx_glr_example_integration_points_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Located points: 64 / 128 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.552 seconds) .. _sphx_glr_download__gallery_geometry_example_integration_points.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_integration_points.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_integration_points.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_integration_points.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_