.. 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 16-19 Import the DPF-Core module as ``dpf`` and import the included examples file and ``DpfPlotter``. .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. 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 28-41 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 41-44 .. 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 45-47 Define the node ID normal to plot the a stress gradient .. GENERATED FROM PYTHON SOURCE LINES 47-48 .. code-block:: Python node_id = 1928 .. GENERATED FROM PYTHON SOURCE LINES 49-51 Print the mesh unit .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. 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 54-57 ``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 57-59 .. code-block:: Python depth = 10 # in mm delta = 0.1 # in mm .. GENERATED FROM PYTHON SOURCE LINES 60-62 Get the meshed region .. GENERATED FROM PYTHON SOURCE LINES 62-63 .. code-block:: Python mesh = model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 64-66 Get Equivalent stress fields container. .. GENERATED FROM PYTHON SOURCE LINES 66-67 .. code-block:: Python stress_fc = model.results.stress().eqv().eval() .. GENERATED FROM PYTHON SOURCE LINES 68-72 Define Nodal scoping. Make sure to define ``"Nodal"`` as the requested location, important for the :class:`normals ` operator. .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python nodal_scoping = dpf.Scoping(location=dpf.locations.nodal) nodal_scoping.ids = [node_id] .. GENERATED FROM PYTHON SOURCE LINES 75-78 Get Skin Mesh because :class:`normals ` operator requires Shells as input. .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python skin_mesh = ops.mesh.skin(mesh=mesh) skin_meshed_region = skin_mesh.outputs.mesh.get_data() .. GENERATED FROM PYTHON SOURCE LINES 81-84 Get normal at a node using :class:`normals ` operator. .. GENERATED FROM PYTHON SOURCE LINES 84-88 .. 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 89-93 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 93-95 .. 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 96-98 Get nodal coordinates, they serve as the first point on the line. .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: Python node = mesh.nodes.node_by_id(node_id) line_fp = node.coordinates .. GENERATED FROM PYTHON SOURCE LINES 101-103 Create 3D line equation. .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. 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 107-109 Create coordinates using 3D line equation. .. GENERATED FROM PYTHON SOURCE LINES 109-111 .. 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 112-114 Create field for coordinates of the path. .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. 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 118-119 Map results on the path. .. GENERATED FROM PYTHON SOURCE LINES 119-123 .. 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 124-125 Request the mapped field data and its mesh. .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python field_m = fields_mapped[0] mesh_m = field_m.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 128-130 Create stress vs length chart. .. GENERATED FROM PYTHON SOURCE LINES 130-136 .. 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 137-139 Create a plot to add both meshes, ``mesh_m`` (the mapped mesh) and ``mesh`` (the original mesh) .. GENERATED FROM PYTHON SOURCE LINES 139-146 .. 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.706 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>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_