-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy path10_streaming_write.py
More file actions
executable file
·81 lines (67 loc) · 3.21 KB
/
Copy path10_streaming_write.py
File metadata and controls
executable file
·81 lines (67 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
import sys
import numpy as np
import openpmd_api as io
# pass-through for ADIOS2 engine parameters
# https://adios2.readthedocs.io/en/latest/engines/engines.html
config = {'adios2': {'engine': {}, 'dataset': {}}}
config['adios2']['engine'] = {'parameters':
{'Threads': '4', 'DataTransport': 'WAN'}}
config['adios2']['dataset'] = {'operators': [{'type': 'bzip2'}]}
if __name__ == "__main__":
# this block is for our CI, SST engine is not present on all systems
backends = io.file_extensions
if "sst" not in backends:
print("SST engine not available in ADIOS2.")
sys.exit(0)
# create a series and specify some global metadata
# change the file extension to .json, .h5 or .bp for regular file writing
series = io.Series("simData.sst", io.Access_Type.create_linear, config)
series.set_author("Franz Poeschel <f.poeschel@hzdr.de>")
series.set_software("openPMD-api-python-examples")
# now, write a number of iterations (or: snapshots, time steps)
for i in range(10):
iteration = series.snapshots()[i]
#######################
# write electron data #
#######################
electronPositions = iteration.particles["e"]["position"]
# openPMD attribute
# (this one would also be set automatically for positions)
electronPositions.unit_dimension = {io.Unit_Dimension.L: 1.0}
# custom attribute
electronPositions.set_attribute("comment", "I'm a comment")
length = 10
local_data = np.arange(i * length, (i + 1) * length,
dtype="double")
for dim in ["x", "y", "z"]:
pos = electronPositions[dim]
pos.reset_dataset(io.Dataset(local_data.dtype, [length]))
pos[()] = local_data
# optionally: flush now to clear buffers
iteration.series_flush() # this is a shortcut for `series.flush()`
###############################
# write some temperature data #
###############################
temperature = iteration.meshes["temperature"]
temperature.unit_dimension = {io.Unit_Dimension.theta: 1.0}
temperature.axis_labels = ["x", "y"]
temperature.grid_spacing = [1., 1.]
# temperature has no x,y,z components, so skip the last layer:
temperature_dataset = temperature
# let's say we are in a 3x3 mesh
temperature_dataset.reset_dataset(
io.Dataset(local_data.dtype, [3, 3]))
# temperature is constant
temperature_dataset.make_constant(273.15)
# After closing the iteration, the readers can see the iteration.
# It can no longer be modified.
# If not closing an iteration explicitly, it will be implicitly closed
# upon creating the next iteration.
iteration.close()
# The files in 'series' are still open until the object is destroyed, on
# which it cleanly flushes and closes all open file handles.
# When running out of scope on return, the 'Series' destructor is called.
# Alternatively, one can call `series.close()` to the same effect as
# calling the destructor, including the release of file handles.
series.close()