.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\16-maths-ops\01-matrix-operations.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_16-maths-ops_01-matrix-operations.py: .. _ref_matrix-operations: Matrix Operations ~~~~~~~~~~~~~~~~~ This example shows how to do some matrix operations, including basic mathematical operations (power, add and multiply by a constant, add field containers and invert) and separating and assembling fields and fields containers. .. GENERATED FROM PYTHON SOURCE LINES 35-37 Import the ``ansys.dpf.core`` module, included examples file, and the ``operators.math`` module. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python from ansys.dpf import core as dpf from ansys.dpf.core import examples import ansys.dpf.core.operators.math as maths .. GENERATED FROM PYTHON SOURCE LINES 42-54 Open an example and print 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 this metadata: - Analysis type - Available results - Size of the mesh - Number of results .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python my_model = dpf.Model(examples.find_complex_rst()) my_mesh = my_model.metadata.meshed_region print(my_model) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Model ------------------------------ Harmonic analysis Unit system: MKS: m, kg, N, s, V, A, 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 - nmisc: Elemental Elemental Non Summable Miscellaneous Data - elemental_heat_generation: Elemental Elemental Heat Generation - structural_temperature: ElementalNodal Structural temperature - electric_potential: Nodal Electric Potential - electric_flux_density: ElementalNodal Electric flux density - electric_field: ElementalNodal Electric field ------------------------------ DPF Meshed Region: 4802 nodes 657 elements Unit: m With solid (3D) elements ------------------------------ DPF Time/Freq Support: Number of sets: 1 With complex values Cumulative Frequency (Hz) LoadStep Substep RPM 1 343478.200000 1 1 0.000000 .. GENERATED FROM PYTHON SOURCE LINES 58-60 Get the stress tensor and define its scoping. Only three nodes will be taken into account to facilitate the visualization. .. GENERATED FROM PYTHON SOURCE LINES 60-69 .. code-block:: Python my_nodes_scoping = dpf.Scoping(ids=[38, 37, 36], location=dpf.locations.elemental) my_stress = my_model.results.stress(mesh_scoping=my_nodes_scoping).eval() # We need to average the result from 'elemental_nodal' to an 'elemental' location to plot it. my_avg_stress = dpf.operators.averaging.to_elemental_fc( fields_container=my_stress, mesh=my_mesh ).eval() print(my_avg_stress) print(my_avg_stress[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF stress(s)Fields Container with 2 field(s) defined on labels: complex time with: - field 0 {complex: 0, time: 1} with Elemental location, 6 components and 3 entities. - field 1 {complex: 1, time: 1} with Elemental location, 6 components and 3 entities. DPF stress_343478.2Hz_real Field Location: Elemental Unit: Pa 3 entities Data: 6 components and 3 elementary data Elemental IDs data(Pa) ------------ ---------- 38 -1.708495e+04 -1.180122e+05 -1.239892e+03 -1.071840e+04 6.860520e+01 -5.747203e+02 37 -1.969171e+04 -1.162967e+05 -1.240906e+03 -1.771855e+04 1.132344e+02 -5.604889e+02 36 -2.358518e+04 -1.136281e+05 -1.239959e+03 -2.447057e+04 1.554545e+02 -5.414763e+02 .. GENERATED FROM PYTHON SOURCE LINES 70-72 Separating tensor by component ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 72-85 .. code-block:: Python # If operations need to be done separately in each tensor component, use # :func:'select_component()'. # Here, the stress tensor has 6 components per elementary data (symmetrical tensor XX,YY,ZZ,XY,YZ,XZ). # Separating the results in different fields containers for each stress tensor component stress_1 = my_avg_stress.select_component(0) stress_2 = my_avg_stress.select_component(1) stress_3 = my_avg_stress.select_component(2) stress_4 = my_avg_stress.select_component(3) stress_5 = my_avg_stress.select_component(4) stress_6 = my_avg_stress.select_component(5) .. GENERATED FROM PYTHON SOURCE LINES 86-88 Mathematical operation on each field ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 88-112 .. code-block:: Python # Here we will do some basic mathematical operations on each stress field # Power # Raise each value of the field to power 2 stress_1 = maths.pow_fc(fields_container=stress_1, factor=2.0).eval() # Add a constant # Add 2 to each value in the field stress_2 = maths.add_constant_fc(fields_container=stress_2, weights=2.0).eval() # Multiply by a constant # Multiply each value in the field by 3 stress_3 = maths.scale_fc(fields_container=stress_3, weights=3.0).eval() # Add fields containers # Each value of each field is added by the correspondent component of the others fields stress_4 = maths.add_fc(fields_container1=stress_4, fields_container2=stress_5).eval() stress_5 = maths.add_fc(fields_container1=stress_5, fields_container2=stress_6).eval() # Invert # Compute the invert of each element of each field (1./X) stress_6 = maths.invert_fc(fields_container=stress_6).eval() .. GENERATED FROM PYTHON SOURCE LINES 113-115 Reassembling the stress tensor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 115-125 .. code-block:: Python # There are different methods to re-assemble the components, here we use the # operator :class:'assemble_scalars_to_matrices_fc ' re_assemble = dpf.operators.utility.assemble_scalars_to_matrices_fc( xx=stress_1, yy=stress_2, zz=stress_3, xy=stress_4, yz=stress_5, xz=stress_6, symmetrical=True ).eval() print(re_assemble) print(re_assemble[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Fields Container with 2 field(s) defined on labels: complex time with: - field 0 {complex: 0, time: 1} with Elemental location, 6 components and 3 entities. - field 1 {complex: 1, time: 1} with Elemental location, 6 components and 3 entities. DPF stress_343478.2Hz_real5 Field Location: Elemental Unit: Pa^-1 3 entities Data: 6 components and 3 elementary data Elemental IDs data(Pa^-1) ------------ ---------- 38 2.918956e+08 -1.180102e+05 -3.719676e+03 -1.064980e+04 -5.061151e+02 -1.739977e-03 37 3.877632e+08 -1.162947e+05 -3.722718e+03 -1.760532e+04 -4.472545e+02 -1.784157e-03 36 5.562606e+08 -1.136261e+05 -3.719876e+03 -2.431511e+04 -3.860217e+02 -1.846803e-03 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.234 seconds) .. _sphx_glr_download_examples_16-maths-ops_01-matrix-operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01-matrix-operations.ipynb <01-matrix-operations.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01-matrix-operations.py <01-matrix-operations.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01-matrix-operations.zip <01-matrix-operations.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_