.. 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-47 .. code-block:: Python import matplotlib.pyplot as plt from ansys.dpf import core as dpf from ansys.dpf.core import examples, operators as ops from ansys.dpf.core.plotter import DpfPlotter .. GENERATED FROM PYTHON SOURCE LINES 48-61 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 61-64 .. 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_orientations: 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 65-67 Define the node ID normal to plot the a stress gradient .. GENERATED FROM PYTHON SOURCE LINES 67-68 .. code-block:: Python node_id = 1928 .. GENERATED FROM PYTHON SOURCE LINES 69-71 Print the mesh unit .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. 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 74-77 ``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 77-79 .. code-block:: Python depth = 10 # in mm delta = 0.1 # in mm .. GENERATED FROM PYTHON SOURCE LINES 80-82 Get the meshed region .. GENERATED FROM PYTHON SOURCE LINES 82-83 .. code-block:: Python mesh = model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 84-86 Get Equivalent stress fields container. .. GENERATED FROM PYTHON SOURCE LINES 86-87 .. code-block:: Python stress_fc = model.results.stress().eqv().eval() .. GENERATED FROM PYTHON SOURCE LINES 88-92 Define Nodal scoping. Make sure to define ``"Nodal"`` as the requested location, important for the :class:`normals ` operator. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python nodal_scoping = dpf.Scoping(location=dpf.locations.nodal) nodal_scoping.ids = [node_id] .. GENERATED FROM PYTHON SOURCE LINES 95-98 Get Skin Mesh because :class:`normals ` operator requires Shells as input. .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: Python skin_mesh = ops.mesh.skin(mesh=mesh) skin_meshed_region = skin_mesh.outputs.mesh.get_data() .. GENERATED FROM PYTHON SOURCE LINES 101-104 Get normal at a node using :class:`normals ` operator. .. GENERATED FROM PYTHON SOURCE LINES 104-108 .. 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 109-113 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 113-115 .. code-block:: Python normal_vec_in_field = ops.math.scale(field=normal_vec_out_field, weights=-1.0) normal_vec_in = normal_vec_in_field.outputs.field.get_data().data[0] .. GENERATED FROM PYTHON SOURCE LINES 116-118 Get nodal coordinates, they serve as the first point on the line. .. GENERATED FROM PYTHON SOURCE LINES 118-120 .. code-block:: Python node = mesh.nodes.node_by_id(node_id) line_fp = node.coordinates .. GENERATED FROM PYTHON SOURCE LINES 121-123 Create 3D line equation. .. GENERATED FROM PYTHON SOURCE LINES 123-126 .. 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 127-129 Create coordinates using 3D line equation. .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. 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 132-134 Create field for coordinates of the path. .. GENERATED FROM PYTHON SOURCE LINES 134-137 .. 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 138-139 Map results on the path. .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. 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 144-145 Request the mapped field data and its mesh. .. GENERATED FROM PYTHON SOURCE LINES 145-147 .. code-block:: Python field_m = fields_mapped[0] mesh_m = field_m.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 148-150 Create stress vs length chart. .. GENERATED FROM PYTHON SOURCE LINES 150-156 .. 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 157-159 Create a plot to add both meshes, ``mesh_m`` (the mapped mesh) and ``mesh`` (the original mesh) .. GENERATED FROM PYTHON SOURCE LINES 159-166 .. 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 7.315 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 `_