Skip to content

Commit 7e31acc

Browse files
committed
Merge branch 'master' into feat/property_fields_container
2 parents b257fcf + c5bc360 commit 7e31acc

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
"""
2+
.. _ref_basic_hdf5:
3+
4+
HDF5 export and import operations
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
7+
This example shows you how to use the HDF5 format to export results
8+
and meshed regions in an H5 file.
9+
It also demonstrates how to read results and meshed regions from the
10+
created H5 file.
11+
12+
First, it exports all the results for all time frequencies,
13+
then it exports all the time sets for the results, per time set.
14+
Finally, it reads the results and compares them.
15+
For the example to run correctly, ensure you do not have an existing H5 file.
16+
17+
"""
18+
19+
###############################################################################
20+
# Import modules, instantiate model and create temporary folder
21+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
# Import the ``dpf-core`` module and its examples files.
23+
24+
import ansys.dpf.core as dpf
25+
from ansys.dpf.core import examples
26+
27+
###############################################################################
28+
# Instantiate the model and the provider operators:
29+
30+
model = dpf.Model(examples.download_transient_result())
31+
streams_cont = model.metadata.streams_provider.outputs.streams_container
32+
time_freq_op = dpf.operators.metadata.time_freq_provider(streams_container=streams_cont)
33+
time_freq_support = time_freq_op.outputs.time_freq_support()
34+
time_freqs = time_freq_support.time_frequencies
35+
36+
result_names_on_all_time_steps = []
37+
result_names_time_per_time = []
38+
39+
num_res = len(model.results)
40+
num_sets = len(time_freqs.data)
41+
42+
###############################################################################
43+
# Define a temporary folder for outputs:
44+
tmpdir = dpf.core.make_tmp_dir_server(dpf.SERVER)
45+
files = [
46+
dpf.path_utilities.join(tmpdir, "file_on_all_time_steps.h5"),
47+
dpf.path_utilities.join(tmpdir, "file_time_per_time.h5"),
48+
]
49+
50+
###############################################################################
51+
# Use H5 serialization operator
52+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
# Export all results on all time frequencies:
54+
h5_serialization_op_all_times = dpf.operators.serialization.hdf5dpf_generate_result_file()
55+
h5_serialization_op_all_times.inputs.filename.connect(files[0])
56+
h5_serialization_op_all_times.inputs.mesh_provider_out.connect(model.metadata.meshed_region)
57+
h5_serialization_op_all_times.inputs.time_freq_support_out.connect(time_freq_support)
58+
59+
for i, res in enumerate(model.results):
60+
res_name = "result_" + res().name
61+
result_names_on_all_time_steps.append(res_name)
62+
h5_serialization_op_all_times.connect(2 * i + 4, res_name)
63+
h5_serialization_op_all_times.connect(2 * i + 5, res.on_all_time_freqs())
64+
65+
h5_all_times_ds = h5_serialization_op_all_times.outputs.data_sources()
66+
67+
###############################################################################
68+
# Export all the results, time set per time set:
69+
h5_serialization_op_set_per_set = dpf.operators.serialization.hdf5dpf_generate_result_file()
70+
h5_serialization_op_set_per_set.inputs.filename.connect(files[1])
71+
h5_serialization_op_set_per_set.inputs.mesh_provider_out.connect(model.metadata.meshed_region)
72+
h5_serialization_op_set_per_set.inputs.time_freq_support_out.connect(time_freq_support)
73+
74+
for j, freq in enumerate(time_freqs.data):
75+
for i, res in enumerate(model.results):
76+
res_name = "result_" + res().name + "_time_" + str(freq)
77+
result_names_time_per_time.append(res_name)
78+
h5_serialization_op_set_per_set.connect(2 * (j * num_res + i) + 4, res_name)
79+
h5_serialization_op_set_per_set.connect(
80+
2 * (j * num_res + i) + 5, res.on_time_scoping(j + 1).eval()
81+
)
82+
83+
h5_set_per_set_ds = h5_serialization_op_set_per_set.outputs.data_sources()
84+
85+
###############################################################################
86+
# Use H5 reading operator
87+
# ~~~~~~~~~~~~~~~~~~~~~~~
88+
# Read the results from all time steps files:
89+
h5_stream_prov_op = dpf.operators.metadata.streams_provider()
90+
h5_stream_prov_op.inputs.data_sources.connect(h5_all_times_ds)
91+
res_deser_all_times_list = []
92+
h5_read_op = dpf.operators.serialization.hdf5dpf_custom_read()
93+
h5_read_op.inputs.streams.connect(h5_stream_prov_op.outputs)
94+
for i, res_name in enumerate(result_names_on_all_time_steps):
95+
h5_read_op.inputs.result_name.connect(res_name)
96+
res_deser = h5_read_op.outputs.field_or_fields_container_as_fields_container()
97+
res_deser_all_times_list.append(res_deser)
98+
99+
###############################################################################
100+
# Read the meshed region from all time steps file:
101+
mesh_prov_op = dpf.operators.mesh.mesh_provider()
102+
mesh_prov_op.inputs.streams_container.connect(h5_stream_prov_op.outputs)
103+
mesh_deser_all_times = mesh_prov_op.outputs.mesh()
104+
105+
###############################################################################
106+
# Read the results from the time set per set file:
107+
h5_stream_prov_op_2 = dpf.operators.metadata.streams_provider()
108+
h5_stream_prov_op_2.inputs.data_sources.connect(h5_set_per_set_ds)
109+
res_deser_set_per_set_list = []
110+
h5_read_op_2 = dpf.operators.serialization.hdf5dpf_custom_read()
111+
h5_read_op_2.inputs.streams.connect(h5_stream_prov_op_2.outputs)
112+
for i, res_name in enumerate(result_names_time_per_time):
113+
h5_read_op_2.inputs.result_name.connect(res_name)
114+
res_deser = h5_read_op_2.outputs.field_or_fields_container_as_fields_container()
115+
res_deser_set_per_set_list.append(res_deser)
116+
117+
###############################################################################
118+
# Read the meshed region from all time steps files:
119+
mesh_prov_op_2 = dpf.operators.mesh.mesh_provider()
120+
mesh_prov_op_2.inputs.streams_container.connect(h5_stream_prov_op_2.outputs)
121+
mesh_deser_set_per_set = mesh_prov_op_2.outputs.mesh()
122+
123+
###############################################################################
124+
# Compare results
125+
# ~~~~~~~~~~~~~~~
126+
127+
###############################################################################
128+
# Print global data:
129+
print("Number of results is: " + str(num_res))
130+
print("Number of time sets is: " + str(num_sets))
131+
print("Results names for 'all time steps' file: ")
132+
print(result_names_on_all_time_steps)
133+
print("Results names for 'set per set' file: ")
134+
print(result_names_time_per_time)
135+
136+
###############################################################################
137+
# compare first result at second time set:
138+
fc_all_steps_first_step_first_res = res_deser_all_times_list[0].get_field_by_time_id(2) # set 1
139+
mesh_deser_all_times.plot(fc_all_steps_first_step_first_res)
140+
141+
mesh_deser_set_per_set.plot(res_deser_set_per_set_list[num_res * 1 + 0])
142+
143+
###############################################################################
144+
# compare 4th result at 6 time set:
145+
to_nodal_op = dpf.operators.averaging.to_nodal_fc()
146+
147+
fc_all_steps_first_step_first_res = res_deser_all_times_list[3].get_field_by_time_id(6) # set 6
148+
mesh_deser_all_times.plot(
149+
dpf.operators.averaging.to_nodal(fc_all_steps_first_step_first_res).outputs.field()
150+
)
151+
152+
mesh_deser_set_per_set.plot(
153+
dpf.operators.averaging.to_nodal(res_deser_set_per_set_list[num_res * 5 + 3]).outputs.field()
154+
)

0 commit comments

Comments
 (0)