.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\05-file-IO\04-basic-load-file.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_05-file-IO_04-basic-load-file.py: .. _ref_basic_load_file_example: Working with a result file ~~~~~~~~~~~~~~~~~~~~~~~~~~ This example shows how to write and upload files on the server machine and then download them back on the client side. The resulting fields container is then exported to a CSV file. .. GENERATED FROM PYTHON SOURCE LINES 15-17 Load a model from the DPF-Core examples: ``ansys.dpf.core`` module. .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: default from ansys.dpf import core as dpf from ansys.dpf.core import examples model = dpf.Model(examples.find_simple_bar()) mesh = model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 26-29 Get and plot the fields container for the result ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get the fields container for the result and plot it so you can compare it later: .. GENERATED FROM PYTHON SOURCE LINES 29-34 .. code-block:: default displacement_operator = model.results.displacement() fc_out = displacement_operator.outputs.fields_container() mesh.plot(fc_out) .. image-sg:: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_001.png :alt: 04 basic load file :srcset: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-38 Export result ~~~~~~~~~~~~~ Get the fields container for the result and export it in the CSV format: .. GENERATED FROM PYTHON SOURCE LINES 38-48 .. code-block:: default import os file_path = os.getcwd() + "\\simple_bar_fc.csv" export_csv_operator = dpf.operators.serialization.field_to_csv() export_csv_operator.inputs.field_or_fields_container.connect(fc_out) export_csv_operator.inputs.file_path.connect(file_path) export_csv_operator.run() .. GENERATED FROM PYTHON SOURCE LINES 49-56 Upload CSV result file ~~~~~~~~~~~~~~~~~~~~~~~ Upload the file ``simple_bar_fc.csv`` on the server side. Here, :func:`upload_file_in_tmp_folder` is used because it is assumed that the server machine architecture is unknown. However, when the server file path is known, :func:`upload_file` can be used. .. GENERATED FROM PYTHON SOURCE LINES 56-64 .. code-block:: default if not dpf.SERVER.local_server: server_file_path = dpf.upload_file_in_tmp_folder(file_path) print(server_file_path) # Remove file to avoid polluting. os.remove(file_path) .. GENERATED FROM PYTHON SOURCE LINES 65-68 Download CSV result file ~~~~~~~~~~~~~~~~~~~~~~~~~ Download the file ``simple_bar_fc.csv``: .. GENERATED FROM PYTHON SOURCE LINES 68-75 .. code-block:: default if not dpf.SERVER.local_server: downloaded_client_file_path = os.getcwd() + "\\simple_bar_fc_downloaded.csv" dpf.download_file(server_file_path, downloaded_client_file_path) else: downloaded_client_file_path = file_path .. GENERATED FROM PYTHON SOURCE LINES 76-79 Load CSV result file as operator input ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Load the fields container contained in the CSV file as an operator input: .. GENERATED FROM PYTHON SOURCE LINES 79-89 .. code-block:: default my_data_sources = dpf.DataSources(downloaded_client_file_path) import_csv_operator = dpf.operators.serialization.csv_to_field() import_csv_operator.inputs.data_sources.connect(my_data_sources) downloaded_fc_out = import_csv_operator.outputs.fields_container() mesh.plot(downloaded_fc_out) # Remove file to avoid polluting. os.remove(downloaded_client_file_path) .. image-sg:: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_002.png :alt: 04 basic load file :srcset: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 90-93 Make operations over the imported fields container ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Use this fields container to get the minimum displacement: .. GENERATED FROM PYTHON SOURCE LINES 93-99 .. code-block:: default min_max_op = dpf.operators.min_max.min_max_fc() min_max_op.inputs.fields_container.connect(downloaded_fc_out) min_field = min_max_op.outputs.field_min() min_field.data .. rst-class:: sphx-glr-script-out .. code-block:: none DPFArray([[-8.202171e-07, -6.265107e-06, -2.444680e-05]]) .. GENERATED FROM PYTHON SOURCE LINES 100-103 Compare the original and the downloaded fields container ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Subtract the two fields and plot an error map: .. GENERATED FROM PYTHON SOURCE LINES 103-113 .. code-block:: default abs_error = (fc_out - downloaded_fc_out).eval() divide = dpf.operators.math.component_wise_divide() divide.inputs.fieldA.connect(fc_out - downloaded_fc_out) divide.inputs.fieldB.connect(fc_out) scale = dpf.operators.math.scale() scale.inputs.field.connect(divide) scale.inputs.ponderation.connect(100.0) rel_error = scale.eval() .. GENERATED FROM PYTHON SOURCE LINES 114-122 Plot both absolute and relative error fields ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Note that the absolute error is bigger where the displacements are bigger, at the tip of the geometry. Instead, the relative error is similar across the geometry since we are dividing by the displacements ``fc_out``. Both plots show errors that can be understood as zero due to machine precision (1e-12 mm for the absolute error and 1e-5% for the relative error). .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: default mesh.plot(abs_error, scalar_bar_args={"title": "Absolute error [mm]"}) mesh.plot(rel_error, scalar_bar_args={"title": "Relative error [%]"}) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_003.png :alt: 04 basic load file :srcset: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_004.png :alt: 04 basic load file :srcset: /examples/05-file-IO/images/sphx_glr_04-basic-load-file_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 10.746 seconds) .. _sphx_glr_download_examples_05-file-IO_04-basic-load-file.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04-basic-load-file.py <04-basic-load-file.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04-basic-load-file.ipynb <04-basic-load-file.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_