Hdf5 export and compare precision

This example shows how to use hdf5 format to export and make a comparison between simple/double precision.

Import dpf module and its examples files, and create a temporary directory

from ansys.dpf import core as dpf
from ansys.dpf.core import examples
from ansys.dpf.core import operators as ops
import os

import tempfile
tmpdir = tempfile.mkdtemp()

Create the model and get stresses, displacements and mesh.

transient = examples.download_transient_result()
model = dpf.Model(transient)

stress = model.results.stress()
displacement = model.results.displacement()
mesh = model.metadata.meshed_region

Create the hdf5 export operator. Hdf5 module should already be loaded.

h5op = ops.serialization.serialize_to_hdf5()
print(h5op)

Out:

DPF serialize_to_hdf5 Operator:
  Serialize the inputs in an hdf5 format.
  Inputs:
         file_path [string]: output file path with .h5 extension
         export_floats [bool]: converts double to float to reduce file size (default is true)
         export_flat_vectors [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

Connect the correct time scoping to the results operators (stress and displacement).

timeIds = list(range(1, model.metadata.time_freq_support.n_sets+1))
timeIds

stress.inputs.time_scoping.connect(timeIds)
displacement.inputs.time_scoping.connect(timeIds)

Connect inputs of the hdf5 export operator.

h5op.inputs.data1.connect(stress.outputs)
h5op.inputs.data2.connect(displacement.outputs)
h5op.inputs.data3.connect(mesh)

Export with simple precision

h5op.inputs.file_path.connect(os.path.join(tmpdir,'c:/temp/dpf_float.h5'))
h5op.run()

Export with simple precision

h5op.inputs.export_floats.connect(False)
h5op.inputs.file_path.connect(os.path.join(tmpdir,'c:/temp/dpf_double.h5'))
h5op.run()

Comparison

print(f"size with float precision: {os.stat(os.path.join(tmpdir,'c:/temp/dpf_float.h5')).st_size} \n size with double precision: {os.stat(os.path.join(tmpdir,'c:/temp/dpf_double.h5')).st_size}")

Out:

size with float precision: 6702616
 size with double precision: 13111816

Total running time of the script: ( 0 minutes 0.594 seconds)

Gallery generated by Sphinx-Gallery