-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path2_read_serial.cpp
More file actions
134 lines (121 loc) · 4.74 KB
/
2_read_serial.cpp
File metadata and controls
134 lines (121 loc) · 4.74 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Copyright 2017-2025 Fabian Koller, Axel Huebl, Franz Poeschel,
* L. Diana Amorim
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <openPMD/openPMD.hpp>
#include <cstddef>
#include <iostream>
#include <memory>
using std::cout;
using namespace openPMD;
int main()
{
Series series = Series(
// "../samples/git-sample/data%T.h5",
"data.h5",
Access::READ_ONLY,
R"({"defer_iteration_parsing": true})");
cout << "Read a Series with openPMD standard version " << series.openPMD()
<< '\n';
cout << "The Series contains " << series.snapshots().size()
<< " iterations:";
for (auto const &i : series.snapshots())
cout << "\n\t" << i.first;
cout << '\n';
// with defer_iteration_parsing, open() must be called explicitly
Iteration i = series.snapshots()[100].open();
cout << "Iteration 100 contains " << i.meshes.size() << " meshes:";
for (auto const &m : i.meshes)
cout << "\n\t" << m.first;
cout << '\n';
cout << "Iteration 100 contains " << i.particles.size()
<< " particle species:";
for (auto const &ps : i.particles)
{
cout << "\n\t" << ps.first;
for (auto const &r : ps.second)
{
cout << "\n\t" << r.first;
cout << '\n';
}
}
openPMD::ParticleSpecies electrons = i.particles["electrons"];
std::shared_ptr<double> charge = electrons["charge"].loadChunk<double>();
series.flush();
cout << "And the first electron particle has a charge = "
<< charge.get()[0];
cout << '\n';
MeshRecordComponent E_x = i.meshes["E"]["x"];
Extent extent = E_x.getExtent();
cout << "Field E/x has shape (";
for (auto const &dim : extent)
cout << dim << ',';
cout << ") and has datatype " << E_x.getDatatype() << '\n';
Offset chunk_offset = {1, 1, 1};
Extent chunk_extent = {2, 2, 1};
// Loading without explicit datatype here
auto chunk_data = E_x.loadChunkVariant(chunk_offset, chunk_extent);
cout << "Queued the loading of a single chunk from disk, "
"ready to execute\n";
series.flush();
cout << "Chunk has been read from disk\n"
<< "Read chunk contains:\n";
std::visit(
[&chunk_offset, &chunk_extent](auto &shared_ptr) {
for (size_t row = 0; row < chunk_extent[0]; ++row)
{
for (size_t col = 0; col < chunk_extent[1]; ++col)
cout << "\t" << '(' << row + chunk_offset[0] << '|'
<< col + chunk_offset[1] << '|' << 1 << ")\t"
<< shared_ptr.get()[row * chunk_extent[1] + col];
cout << '\n';
}
},
chunk_data);
auto all_data = E_x.loadChunk<double>();
auto ch = series.customHierarchies();
ch.printRecursively();
std::cout << "READING 200/fields" << std::endl;
ch["data"]["200"]["fields"].read();
std::cout << "READING 200/particles" << std::endl;
ch["data"]["200"]["particles"].read();
std::cout << "READING 300" << std::endl;
ch["data"]["300"].read(0);
ch.printRecursively();
// The iteration can be closed in order to help free up resources.
// The iteration's content will be flushed automatically.
i.close();
std::cout << "OPENING 200" << std::endl;
i = series.snapshots()[200].open();
ch.printRecursively();
series.snapshots()[300].open();
cout << "Full E/x starts with:\n\t{";
for (size_t col = 0; col < extent[1] && col < 5; ++col)
cout << all_data.get()[col] << ", ";
cout << "...}\n";
/* 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();
return 0;
}