.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\00-basic\05-use_local_data.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_05-use_local_data.py: .. _ref_use_local_data_example: Bring a field's data locally to improve performance ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Reducing the number of calls to the server is key to improving performance. Using the ``as_local_field`` option brings the data from the server to your local machine where you can work on it. When finished, you send the updated data back to the server in one transaction. .. GENERATED FROM PYTHON SOURCE LINES 36-41 .. code-block:: Python # Import necessary modules from ansys.dpf import core as dpf from ansys.dpf.core import examples, operators as ops .. GENERATED FROM PYTHON SOURCE LINES 42-44 Create a model object to establish a connection with an example result file and then extract: .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: Python model = dpf.Model(examples.download_multi_stage_cyclic_result()) print(model) mesh = model.metadata.meshed_region .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Model ------------------------------ Modal analysis Unit system: MKS: m, kg, N, s, V, A, degC Physics Type: Mechanical Available results: - displacement: Nodal Displacement - stress: ElementalNodal Stress - elastic_strain: ElementalNodal Strain - element_orientations: ElementalNodal Element Euler Angles - structural_temperature: ElementalNodal Structural temperature ------------------------------ DPF Meshed Region: 3595 nodes 1557 elements Unit: m With solid (3D) elements ------------------------------ DPF Time/Freq Support: Number of sets: 6 Cumulative Frequency (Hz) LoadStep Substep Harmonic index 1 188.385357 1 1 0.000000 2 325.126418 1 2 0.000000 3 595.320548 1 3 0.000000 4 638.189511 1 4 0.000000 5 775.669703 1 5 0.000000 6 928.278013 1 6 0.000000 .. GENERATED FROM PYTHON SOURCE LINES 49-51 Create the workflow ~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 53-54 Compute the stress principal invariants: .. GENERATED FROM PYTHON SOURCE LINES 54-63 .. code-block:: Python stress_op = ops.result.stress(data_sources=model.metadata.data_sources) stress_op.inputs.requested_location.connect(dpf.locations.nodal) stress_op.inputs.mesh_scoping.connect(mesh.nodes.scoping) principal_op = ops.invariant.principal_invariants_fc(stress_op) principal_stress_1 = principal_op.outputs.fields_eig_1()[0] principal_stress_2 = principal_op.outputs.fields_eig_2()[0] principal_stress_3 = principal_op.outputs.fields_eig_3()[0] .. GENERATED FROM PYTHON SOURCE LINES 64-66 Manipulate data locally ~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 69-72 This example goes over the fields, keeping the largest invariant value by node if the averaged value of invariants is large enough. Exploring data allows you to customize it to meet your needs. .. GENERATED FROM PYTHON SOURCE LINES 72-92 .. code-block:: Python node_scoping_ids = principal_stress_1.scoping.ids threshold = 300000.0 field_to_keep = dpf.fields_factory.create_scalar_field( len(node_scoping_ids), location=dpf.locations.nodal ) with field_to_keep.as_local_field() as f: with principal_stress_1.as_local_field() as s1: with principal_stress_2.as_local_field() as s2: with principal_stress_3.as_local_field() as s3: for i, id in enumerate(node_scoping_ids): d1 = abs(s1.get_entity_data_by_id(id)) d2 = abs(s2.get_entity_data_by_id(id)) d3 = abs(s3.get_entity_data_by_id(id)) if (d1 + d2 + d3) / 3.0 > threshold: d = max(d1, d2, d3) f.append(d, id) .. GENERATED FROM PYTHON SOURCE LINES 93-95 Plot result field ~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 98-99 Plot the result field on the skin mesh: .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: Python mesh.plot(field_to_keep) .. image-sg:: /examples/00-basic/images/sphx_glr_05-use_local_data_001.png :alt: 05 use local data :srcset: /examples/00-basic/images/sphx_glr_05-use_local_data_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 102-104 Plot initial invariants ~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 107-108 Plot the initial invariants: .. GENERATED FROM PYTHON SOURCE LINES 108-112 .. code-block:: Python mesh.plot(principal_stress_1) mesh.plot(principal_stress_2) mesh.plot(principal_stress_3) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/00-basic/images/sphx_glr_05-use_local_data_002.png :alt: 05 use local data :srcset: /examples/00-basic/images/sphx_glr_05-use_local_data_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/00-basic/images/sphx_glr_05-use_local_data_003.png :alt: 05 use local data :srcset: /examples/00-basic/images/sphx_glr_05-use_local_data_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/00-basic/images/sphx_glr_05-use_local_data_004.png :alt: 05 use local data :srcset: /examples/00-basic/images/sphx_glr_05-use_local_data_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.118 seconds) .. _sphx_glr_download_examples_00-basic_05-use_local_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 05-use_local_data.ipynb <05-use_local_data.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 05-use_local_data.py <05-use_local_data.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 05-use_local_data.zip <05-use_local_data.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_