.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\05-file-IO\00-hdf5_double_float_comparison.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_05-file-IO_00-hdf5_double_float_comparison.py: .. _ref_basic_hdf5: HDF5 export and compare precision ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This example shows how to use HDF5 format to export and compare simple precision versus double precision. .. GENERATED FROM PYTHON SOURCE LINES 14-16 Import the ``dpf-core`` module and its examples files, and then create a temporary directory. .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: Python import os from ansys.dpf import core as dpf from ansys.dpf.core import examples from ansys.dpf.core import operators as ops .. GENERATED FROM PYTHON SOURCE LINES 24-25 Create the model and get the stresses, displacements, and mesh. .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python transient = examples.download_transient_result() model = dpf.Model(transient) stress = model.results.stress() displacement = model.results.displacement() mesh = model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 34-35 Create the HDF5 export operator. The HDF5 module should already be loaded. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python h5op = ops.serialization.serialize_to_hdf5() print(h5op) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF serialize_to_hdf5 Operator: This operator is deprecated: use 'hdf5::h5dpf::make_result_file' instead. Serialize the inputs in an hdf5 format. Inputs: file_path [string]: output file path with .h5 extension export_floats (optional) [bool]: converts double to float to reduce file size (default is true) export_flat_vectors (optional) [bool]: if true, vectors and matrices data are exported flat (x1,y1,z1,x2,y2,z2..) (default is false) data (ellipsis) []: only the data set explicitly to export is exported Run the operator to get its result .. GENERATED FROM PYTHON SOURCE LINES 40-42 Connect the correct time scoping to the results operators (stress and displacement). .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: Python timeIds = list(range(1, model.metadata.time_freq_support.n_sets + 1)) stress.inputs.time_scoping.connect(timeIds) displacement.inputs.time_scoping.connect(timeIds) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Connect inputs of the HDF5 export operator. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python h5op.inputs.data1.connect(stress.outputs) h5op.inputs.data2.connect(displacement.outputs) h5op.inputs.data3.connect(mesh) .. GENERATED FROM PYTHON SOURCE LINES 56-57 Define a temporary folder for outputs .. GENERATED FROM PYTHON SOURCE LINES 57-62 .. code-block:: Python tmpdir = dpf.core.make_tmp_dir_server(dpf.SERVER) files = [ dpf.path_utilities.join(tmpdir, "dpf_float.h5"), dpf.path_utilities.join(tmpdir, "dpf_double.h5"), ] .. GENERATED FROM PYTHON SOURCE LINES 63-64 Export with simple precision. .. GENERATED FROM PYTHON SOURCE LINES 64-68 .. code-block:: Python h5op.inputs.file_path.connect(files[0]) h5op.run() .. GENERATED FROM PYTHON SOURCE LINES 69-70 Export with double precision. .. GENERATED FROM PYTHON SOURCE LINES 70-75 .. code-block:: Python h5op.inputs.export_floats.connect(False) h5op.inputs.file_path.connect(files[1]) h5op.run() .. GENERATED FROM PYTHON SOURCE LINES 76-77 Download the resulting .h5 files if necessary .. GENERATED FROM PYTHON SOURCE LINES 77-88 .. code-block:: Python if not dpf.SERVER.local_server: float_file_path = os.path.join(os.getcwd(), "dpf_float.h5") double_file_path = os.path.join(os.getcwd(), "dpf_double.h5") dpf.download_file(files[0], float_file_path) dpf.download_file(files[1], double_file_path) else: float_file_path = files[0] double_file_path = files[1] .. GENERATED FROM PYTHON SOURCE LINES 89-90 Compare simple precision versus double precision. .. GENERATED FROM PYTHON SOURCE LINES 90-96 .. code-block:: Python float_precision = os.stat(float_file_path).st_size double_precision = os.stat(double_file_path).st_size print( f"size with float precision: {float_precision}\n" f"size with double precision: {double_precision}" ) .. rst-class:: sphx-glr-script-out .. code-block:: none size with float precision: 6703264 size with double precision: 13112464 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.156 seconds) .. _sphx_glr_download_examples_05-file-IO_00-hdf5_double_float_comparison.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 00-hdf5_double_float_comparison.ipynb <00-hdf5_double_float_comparison.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 00-hdf5_double_float_comparison.py <00-hdf5_double_float_comparison.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_