.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\04-advanced\06-stress_gradient_path.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_04-advanced_06-stress_gradient_path.py: .. _stress_gradient_path: Stress gradient normal to a defined node ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This example shows how to plot a stress gradient normal to a selected node. Because the example is based on creating a path along the normal, the selected node must be on the surface of the geometry. A path is created of a defined length. .. GENERATED FROM PYTHON SOURCE LINES 38-41 Import the DPF-Core module as ``dpf`` and import the included examples file and ``DpfPlotter``. .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. code-block:: Python import matplotlib.pyplot as plt from ansys.dpf import core as dpf from ansys.dpf.core import examples from ansys.dpf.core import operators as ops from ansys.dpf.core.plotter import DpfPlotter .. GENERATED FROM PYTHON SOURCE LINES 50-63 Open an example and print out the ``Model`` object. The :class:`Model ` class helps to organize access methods for the result by keeping track of the operators and data sources used by the result file. Printing the model displays: - Analysis type - Available results - Size of the mesh - Number of results - Unit .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python path = examples.download_hemisphere() model = dpf.Model(path) print(model) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Model ------------------------------ Static analysis Unit system: NMM: mm, ton, N, s, mV, mA, degC Physics Type: Mechanical Available results: - displacement: Nodal Displacement - reaction_force: Nodal Force - stress: ElementalNodal Stress - elemental_volume: Elemental Volume - stiffness_matrix_energy: Elemental Energy-stiffness matrix - artificial_hourglass_energy: Elemental Hourglass Energy - thermal_dissipation_energy: Elemental thermal dissipation energy - kinetic_energy: Elemental Kinetic Energy - co_energy: Elemental co-energy - incremental_energy: Elemental incremental energy - elastic_strain: ElementalNodal Strain - element_euler_angles: ElementalNodal Element Euler Angles - structural_temperature: ElementalNodal Structural temperature ------------------------------ DPF Meshed Region: 10741 nodes 3011 elements Unit: mm With solid (3D) elements ------------------------------ DPF Time/Freq Support: Number of sets: 1 Cumulative Time (s) LoadStep Substep 1 1.000000 1 1 .. GENERATED FROM PYTHON SOURCE LINES 67-69 Define the node ID normal to plot the a stress gradient .. GENERATED FROM PYTHON SOURCE LINES 69-70 .. code-block:: Python node_id = 1928 .. GENERATED FROM PYTHON SOURCE LINES 71-73 Print the mesh unit .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python unit = model.metadata.meshed_region.unit print("Unit: %s" % unit) .. rst-class:: sphx-glr-script-out .. code-block:: none Unit: mm .. GENERATED FROM PYTHON SOURCE LINES 76-79 ``depth`` defines the length/depth that the path penetrates to. While defining ``depth`` make sure you use the correct mesh unit. ``delta`` defines distance between consecutive points on the path. .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python depth = 10 # in mm delta = 0.1 # in mm .. GENERATED FROM PYTHON SOURCE LINES 82-84 Get the meshed region .. GENERATED FROM PYTHON SOURCE LINES 84-85 .. code-block:: Python mesh = model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 86-88 Get Equivalent stress fields container. .. GENERATED FROM PYTHON SOURCE LINES 88-89 .. code-block:: Python stress_fc = model.results.stress().eqv().eval() .. GENERATED FROM PYTHON SOURCE LINES 90-94 Define Nodal scoping. Make sure to define ``"Nodal"`` as the requested location, important for the :class:`normals ` operator. .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: Python nodal_scoping = dpf.Scoping(location=dpf.locations.nodal) nodal_scoping.ids = [node_id] .. GENERATED FROM PYTHON SOURCE LINES 97-100 Get Skin Mesh because :class:`normals ` operator requires Shells as input. .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python skin_mesh = ops.mesh.skin(mesh=mesh) skin_meshed_region = skin_mesh.outputs.mesh.get_data() .. GENERATED FROM PYTHON SOURCE LINES 103-106 Get normal at a node using :class:`normals ` operator. .. GENERATED FROM PYTHON SOURCE LINES 106-110 .. code-block:: Python normal = ops.geo.normals() normal.inputs.mesh.connect(skin_meshed_region) normal.inputs.mesh_scoping.connect(nodal_scoping) normal_vec_out_field = normal.outputs.field.get_data() .. GENERATED FROM PYTHON SOURCE LINES 111-115 The normal vector is along the surface normal. You need to invert the vector using :class:`scale ` operator inwards in the geometry, to get the path direction. .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python normal_vec_in_field = ops.math.scale(field=normal_vec_out_field, ponderation=-1.0) normal_vec_in = normal_vec_in_field.outputs.field.get_data().data[0] .. GENERATED FROM PYTHON SOURCE LINES 118-120 Get nodal coordinates, they serve as the first point on the line. .. GENERATED FROM PYTHON SOURCE LINES 120-122 .. code-block:: Python node = mesh.nodes.node_by_id(node_id) line_fp = node.coordinates .. GENERATED FROM PYTHON SOURCE LINES 123-125 Create 3D line equation. .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: Python fx = lambda t: line_fp[0] + normal_vec_in[0] * t fy = lambda t: line_fp[1] + normal_vec_in[1] * t fz = lambda t: line_fp[2] + normal_vec_in[2] * t .. GENERATED FROM PYTHON SOURCE LINES 129-131 Create coordinates using 3D line equation. .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: Python coordinates = [[fx(t * delta), fy(t * delta), fz(t * delta)] for t in range(int(depth / delta))] flat_coordinates = [entry for data in coordinates for entry in data] .. GENERATED FROM PYTHON SOURCE LINES 134-136 Create field for coordinates of the path. .. GENERATED FROM PYTHON SOURCE LINES 136-139 .. code-block:: Python field_coord = dpf.fields_factory.create_3d_vector_field(len(coordinates)) field_coord.data = flat_coordinates field_coord.scoping.ids = list(range(1, len(coordinates) + 1)) .. GENERATED FROM PYTHON SOURCE LINES 140-141 Map results on the path. .. GENERATED FROM PYTHON SOURCE LINES 141-145 .. code-block:: Python mapping_operator = ops.mapping.on_coordinates( fields_container=stress_fc, coordinates=field_coord, create_support=True, mesh=mesh ) fields_mapped = mapping_operator.outputs.fields_container() .. GENERATED FROM PYTHON SOURCE LINES 146-147 Request the mapped field data and its mesh. .. GENERATED FROM PYTHON SOURCE LINES 147-149 .. code-block:: Python field_m = fields_mapped[0] mesh_m = field_m.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 150-152 Create stress vs length chart. .. GENERATED FROM PYTHON SOURCE LINES 152-158 .. code-block:: Python x_initial = 0.0 length = [x_initial + delta * index for index in range(len(field_m.data))] plt.plot(length, field_m.data, "r") plt.xlabel("Length (%s)" % mesh.unit) plt.ylabel("Stress (%s)" % field_m.unit) plt.show() .. image-sg:: /examples/04-advanced/images/sphx_glr_06-stress_gradient_path_001.png :alt: 06 stress gradient path :srcset: /examples/04-advanced/images/sphx_glr_06-stress_gradient_path_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 159-161 Create a plot to add both meshes, ``mesh_m`` (the mapped mesh) and ``mesh`` (the original mesh) .. GENERATED FROM PYTHON SOURCE LINES 161-168 .. code-block:: Python pl = DpfPlotter() pl.add_field(field_m, mesh_m) pl.add_mesh(mesh, style="surface", show_edges=True, color="w", opacity=0.3) pl.show_figure( show_axes=True, cpos=[(62.687, 50.119, 67.247), (5.135, 6.458, -0.355), (-0.286, 0.897, -0.336)], ) .. image-sg:: /examples/04-advanced/images/sphx_glr_06-stress_gradient_path_002.png :alt: 06 stress gradient path :srcset: /examples/04-advanced/images/sphx_glr_06-stress_gradient_path_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.382 seconds) .. _sphx_glr_download_examples_04-advanced_06-stress_gradient_path.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06-stress_gradient_path.ipynb <06-stress_gradient_path.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06-stress_gradient_path.py <06-stress_gradient_path.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 06-stress_gradient_path.zip <06-stress_gradient_path.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_