.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\00-basic\01-basic_operators.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_00-basic_01-basic_operators.py: .. _ref_basic_operators_example: Operators overview ~~~~~~~~~~~~~~~~~~ In DPF, operators provide the primary method for interacting with and extracting results. Within DPF-Core, operators are directly exposed with the ``Operators`` class as well as wrapped within several other convenience classes. For a list of all operators, see :ref:`ref_dpf_operators_reference`. This example demonstrates how to work directly with operators and compares this method to a wrapped approach. .. GENERATED FROM PYTHON SOURCE LINES 41-46 .. code-block:: Python # Import the necessary modules from ansys.dpf import core as dpf from ansys.dpf.core import examples .. GENERATED FROM PYTHON SOURCE LINES 47-49 Create a model object to establish a connection with an example result file: .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python model = dpf.Model(examples.find_static_rst()) print(model) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Model ------------------------------ Static 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 - element_euler_angles: ElementalNodal Element Euler Angles - structural_temperature: ElementalNodal Structural temperature ------------------------------ DPF Meshed Region: 81 nodes 8 elements Unit: m 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 53-60 Next, create a raw displacement operator ``"U"``. Each operator contains ``input`` and ``output`` pins that can be connected to various sources to include other operators. This allows operators to be "chained" to allow for highly efficient operations. To print out the available inputs and outputs of the displacement operator: .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: Python disp_op = dpf.Operator("U") print(disp_op.inputs) print(disp_op.outputs) .. rst-class:: sphx-glr-script-out .. code-block:: none Available inputs: - time_scoping : Scoping, int, list, float, Field, optional Time/freq values (use doubles or field), time/freq set ids (use ints or scoping) or time/freq step ids (use scoping with timefreq_steps location) required in output. to specify time/freq values at specific load steps, put a field (and not a list) in input with a scoping located on "timefreq_steps". linear time freq intrapolation is performed if the values are not in the result files and the data at the max time or freq is taken when time/freqs are higher than available time/freqs in result files. - mesh_scoping : ScopingsContainer, Scoping, optional Nodes or elements scoping required in output. the output fields will be scoped on these node or element ids. to figure out the ordering of the fields data, look at their scoping ids as they might not be ordered as the input scoping was. the scoping's location indicates whether nodes or elements are asked for. using scopings container allows you to split the result fields container into domains - fields_container : FieldsContainer, optional Fields container already allocated modified inplace - streams_container : StreamsContainer, optional Result file container allowed to be kept open to cache data - data_sources : DataSources Result file path container, used if no streams are set - bool_rotate_to_global : bool, optional If true the field is rotated to global coordinate system (default true) - mesh : MeshedRegion, MeshesContainer, optional Prevents from reading the mesh in the result files - read_cyclic : Enum Dataprocessing::Ecyclicreading, int, optional If 0 cyclic symmetry is ignored, if 1 cyclic sector is read, if 2 cyclic expansion is done, if 3 cyclic expansion is done and stages are merged (default is 1) Available outputs: - fields_container .. GENERATED FROM PYTHON SOURCE LINES 65-70 Compute the maximum normalized displacement ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This example demonstrate how to chain various operators. It connects the input of the operator to the data sources contained within the ``model`` object and then the maximum of the norm of the operator. .. GENERATED FROM PYTHON SOURCE LINES 70-89 .. code-block:: Python # Connect to the data sources of the model. disp_op.inputs.data_sources.connect(model.metadata.data_sources) # Create a fields container norm operator and connect it to the # displacement operator to chain the operators. norm_op = dpf.Operator("norm_fc") norm_op.inputs.connect(disp_op.outputs) # Create a fields container min/max operator and connect it to the # output of the norm operator. mm_op = dpf.Operator("min_max_fc") mm_op.inputs.connect(norm_op.outputs) # Finally, get the value of the maximum displacement. field_max = mm_op.outputs.field_max() print(field_max) print(field_max.data) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF displacement_1.s Field Location: Nodal Unit: m 1 entities Data: 1 components and 1 elementary data IDs data(m) ------------ ---------- 0 1.481537e-08 [1.48153706e-08] .. GENERATED FROM PYTHON SOURCE LINES 90-96 Wrapped operators ~~~~~~~~~~~~~~~~~ The ``model.results`` property contains all the wrapped operators available for a given result. This is provided out of convenience because all operators may not be available for a given result. Consequently, it is much easier to reference available operators by first running: .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: Python print(model.results) .. rst-class:: sphx-glr-script-out .. code-block:: none Static 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 - element_euler_angles: ElementalNodal Element Euler Angles - structural_temperature: ElementalNodal Structural temperature .. GENERATED FROM PYTHON SOURCE LINES 99-100 Create the displacement operator directly from the ``results`` property: .. GENERATED FROM PYTHON SOURCE LINES 100-113 .. code-block:: Python disp_op = model.results.displacement() # Out of convenience, the ``operators`` module contains available operators. # These operators can be chained to create a workflow in one line. from ansys.dpf.core import operators mm_op = operators.min_max.min_max_fc(operators.math.norm_fc(disp_op)) # Finally, get the value of the maximum displacement. field_max = mm_op.outputs.field_max() print(field_max) print(field_max.data) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF displacement_1.s Field Location: Nodal Unit: m 1 entities Data: 1 components and 1 elementary data IDs data(m) ------------ ---------- 0 1.481537e-08 [1.48153706e-08] .. GENERATED FROM PYTHON SOURCE LINES 114-115 Plot the displacement: .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python print(model.metadata.meshed_region.plot(disp_op.outputs.fields_container())) .. image-sg:: /examples/00-basic/images/sphx_glr_01-basic_operators_001.png :alt: 01 basic operators :srcset: /examples/00-basic/images/sphx_glr_01-basic_operators_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none None .. GENERATED FROM PYTHON SOURCE LINES 118-124 Scripting operator syntax ~~~~~~~~~~~~~~~~~~~~~~~~~~ Because DPF provides a scripting syntax, knowing an operator's "string name" is not mandatory. While this example is similar to the above script, it uses the DPF scripting syntax. .. GENERATED FROM PYTHON SOURCE LINES 126-128 Instead of using a ``model`` class instance, use a ``DdataSources`` object directly. The ``DataSources`` constructor input is a path. .. GENERATED FROM PYTHON SOURCE LINES 128-131 .. code-block:: Python ds = dpf.DataSources(examples.find_static_rst()) print(examples.find_static_rst()) .. rst-class:: sphx-glr-script-out .. code-block:: none C:\hostedtoolcache\windows\Python\3.9.13\x64\lib\site-packages\ansys\dpf\core\examples\result_files\static.rst .. GENERATED FROM PYTHON SOURCE LINES 132-133 Instantiate the operators and connect them: .. GENERATED FROM PYTHON SOURCE LINES 133-141 .. code-block:: Python disp_op = dpf.operators.result.displacement() disp_op.inputs.data_sources.connect(ds) norm_op = dpf.operators.math.norm_fc() norm_op.inputs.connect(disp_op.outputs) mm_op = dpf.operators.min_max.min_max_fc() mm_op.inputs.connect(norm_op.outputs) .. GENERATED FROM PYTHON SOURCE LINES 142-143 Get the output and print the result data: .. GENERATED FROM PYTHON SOURCE LINES 143-146 .. code-block:: Python field_max = mm_op.outputs.field_max() print(field_max.data) .. rst-class:: sphx-glr-script-out .. code-block:: none [1.48153706e-08] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.658 seconds) .. _sphx_glr_download_examples_00-basic_01-basic_operators.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01-basic_operators.ipynb <01-basic_operators.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01-basic_operators.py <01-basic_operators.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 01-basic_operators.zip <01-basic_operators.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_