forked from mdelorme/fv2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOManager.h
288 lines (235 loc) · 9.97 KB
/
IOManager.h
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#pragma once
#include <highfive/H5Easy.hpp>
#include <ostream>
#include <iomanip>
#include "SimInfo.h"
using namespace H5Easy;
namespace fv2d {
// xdmf strings
namespace {
char str_xdmf_header[] = R"xml(<?xml version="1.0" ?>
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
<Xdmf Version="2.0">
<Domain CollectionType="Temporal">
<Grid Name="MainTimeSeries" GridType="Collection" CollectionType="Temporal">
<Topology Name="Main Topology" TopologyType="2DSMesh" NumberOfElements="%d %d"/>
<Geometry Name="Main Geometry" GeometryType="X_Y">
<DataItem Dimensions="%d %d" NumberType="Float" Precision="8" Format="HDF">%s:/x</DataItem>
<DataItem Dimensions="%d %d" NumberType="Float" Precision="8" Format="HDF">%s:/y</DataItem>
</Geometry>
)xml";
#define format_xdmf_header(params, path) \
params.Ny + 1, params.Nx + 1, \
params.Ny + 1, params.Nx + 1, (path + ".h5").c_str(), \
params.Ny + 1, params.Nx + 1, (path + ".h5").c_str()
char str_xdmf_footer[] =
R"xml(
</Grid>
</Domain>
</Xdmf>)xml";
char str_xdmf_ite_header[] =
R"xml(
<Grid Name="Cells" GridType="Uniform">
<Time TimeType="Single" Value="%lf" />
<Topology Reference="//Topology[@Name='Main Topology']" />
<Geometry Reference="//Geometry[@Name='Main Geometry']" />)xml";
char str_xdmf_scalar_field[] =
R"xml(
<Attribute Name="%s" AttributeType="Scalar" Center="Cell">
<DataItem Dimensions="%d %d" NumberType="Float" Precision="8" Format="HDF">%s:/%s/%s</DataItem>
</Attribute>)xml";
#define format_xdmf_scalar_field(params, path, group, field) \
field, \
params.Ny, params.Nx, \
(path + ".h5").c_str(), group.c_str(), field
char str_xdmf_vector_field[] =
R"xml(
<Attribute Name="%s" AttributeType="Vector" Center="Cell">
<DataItem Dimensions="%d %d 2" ItemType="Function" Function="JOIN($0, $1)">
<DataItem Dimensions="%d %d" NumberType="Float" Precision="8" Format="HDF">%s:/%s/%s</DataItem>
<DataItem Dimensions="%d %d" NumberType="Float" Precision="8" Format="HDF">%s:/%s/%s</DataItem>
</DataItem>
</Attribute>)xml";
#define format_xdmf_vector_field(params, path, group, name, field_x, field_y) \
name, \
params.Ny, params.Nx, \
params.Ny, params.Nx, \
(path + ".h5").c_str(), group.c_str(), field_x, \
params.Ny, params.Nx, \
(path + ".h5").c_str(), group.c_str(), field_y
char str_xdmf_ite_footer[] =
R"xml(
</Grid>
)xml";
} // anonymous namespace
class IOManager {
public:
Params params;
IOManager(Params ¶ms)
: params(params) {};
~IOManager() = default;
void saveSolution(const Array &Q, int iteration, real_t t, real_t dt) {
if (params.multiple_outputs)
saveSolutionMultiple(Q, iteration, t, dt);
else
saveSolutionUnique(Q, iteration, t, dt);
}
void saveSolutionMultiple(const Array &Q, int iteration, real_t t, real_t dt) {
std::ostringstream oss;
oss << params.filename_out << "_" << std::setw(4) << std::setfill('0') << iteration;
std::string path = oss.str();
std::string h5_filename = oss.str() + ".h5";
std::string xmf_filename = oss.str() + ".xmf";
File file(h5_filename, File::Truncate);
FILE* xdmf_fd = fopen(xmf_filename.c_str(), "w+");
file.createAttribute("Ntx", params.Ntx);
file.createAttribute("Nty", params.Nty);
file.createAttribute("Nx", params.Nx);
file.createAttribute("Ny", params.Ny);
file.createAttribute("ibeg", params.ibeg);
file.createAttribute("iend", params.iend);
file.createAttribute("jbeg", params.jbeg);
file.createAttribute("jend", params.jend);
file.createAttribute("problem", params.problem);
file.createAttribute("iteration", iteration);
std::vector<real_t> x, y;
// -- vertex pos
for (int j=params.jbeg; j <= params.jend; ++j) {
for (int i=params.ibeg; i <= params.iend; ++i) {
x.push_back((i-params.ibeg) * params.dx);
y.push_back((j-params.jbeg) * params.dy);
}
}
file.createDataSet("x", x);
file.createDataSet("y", y);
using Table = std::vector<real_t>;
auto Qhost = Kokkos::create_mirror(Q);
Kokkos::deep_copy(Qhost, Q);
Table trho, tu, tv, tprs;
for (int j=params.jbeg; j<params.jend; ++j) {
for (int i=params.ibeg; i<params.iend; ++i) {
real_t rho = Qhost(j, i, IR);
real_t u = Qhost(j, i, IU);
real_t v = Qhost(j, i, IV);
real_t p = Qhost(j, i, IP);
trho.push_back(rho);
tu.push_back(u);
tv.push_back(v);
tprs.push_back(p);
}
}
file.createDataSet("rho", trho);
file.createDataSet("u", tu);
file.createDataSet("v", tv);
file.createDataSet("prs", tprs);
file.createAttribute("time", t);
std::string empty_string = "";
fprintf(xdmf_fd, str_xdmf_header, format_xdmf_header(params, path));
fprintf(xdmf_fd, str_xdmf_ite_header, t);
fprintf(xdmf_fd, str_xdmf_scalar_field, format_xdmf_scalar_field(params, path, empty_string, "rho"));
fprintf(xdmf_fd, str_xdmf_vector_field, format_xdmf_vector_field(params, path, empty_string, "velocity", "u", "v"));
fprintf(xdmf_fd, str_xdmf_scalar_field, format_xdmf_scalar_field(params, path, empty_string, "prs"));
fprintf(xdmf_fd, "%s", str_xdmf_ite_footer);
fprintf(xdmf_fd, "%s", str_xdmf_footer);
fclose(xdmf_fd);
}
void saveSolutionUnique(const Array &Q, int iteration, real_t t, real_t dt) {
std::ostringstream oss;
oss << "ite_" << std::setw(4) << std::setfill('0') << iteration;
std::string path = oss.str();
auto flag_h5 = (iteration == 0 ? File::Truncate : File::ReadWrite);
auto flag_xdmf = (iteration == 0 ? "w+" : "r+");
File file(params.filename_out + ".h5", flag_h5);
FILE* xdmf_fd = fopen((params.filename_out + ".xdmf").c_str(), flag_xdmf);
if (iteration == 0) {
file.createAttribute("Ntx", params.Ntx);
file.createAttribute("Nty", params.Nty);
file.createAttribute("Nx", params.Nx);
file.createAttribute("Ny", params.Ny);
file.createAttribute("ibeg", params.ibeg);
file.createAttribute("iend", params.iend);
file.createAttribute("jbeg", params.jbeg);
file.createAttribute("jend", params.jend);
file.createAttribute("problem", params.problem);
std::vector<real_t> x, y;
// -- vertex pos
for (int j=params.jbeg; j <= params.jend; ++j)
for (int i=params.ibeg; i <= params.iend; ++i)
{
x.push_back((i-params.ibeg) * params.dx);
y.push_back((j-params.jbeg) * params.dy);
}
file.createDataSet("x", x);
file.createDataSet("y", y);
fprintf(xdmf_fd, str_xdmf_header, format_xdmf_header(params, params.filename_out));
fprintf(xdmf_fd, "%s", str_xdmf_footer);
}
using Table = std::vector<std::vector<real_t>>;
auto Qhost = Kokkos::create_mirror(Q);
Kokkos::deep_copy(Qhost, Q);
Table trho, tu, tv, tprs;
for (int j=params.jbeg; j<params.jend; ++j) {
std::vector<real_t> rrho, ru, rv, rprs;
for (int i=params.ibeg; i<params.iend; ++i) {
real_t rho = Qhost(j, i, IR);
real_t u = Qhost(j, i, IU);
real_t v = Qhost(j, i, IV);
real_t p = Qhost(j, i, IP);
rrho.push_back(rho);
ru.push_back(u);
rv.push_back(v);
rprs.push_back(p);
}
trho.push_back(rrho);
tu.push_back(ru);
tv.push_back(rv);
tprs.push_back(rprs);
}
auto group = file.createGroup(path);
group.createDataSet("rho", trho);
group.createDataSet("u", tu);
group.createDataSet("v", tv);
group.createDataSet("prs", tprs);
group.createAttribute("time", t);
fseek(xdmf_fd, -sizeof(str_xdmf_footer), SEEK_END);
fprintf(xdmf_fd, str_xdmf_ite_header, t);
fprintf(xdmf_fd, str_xdmf_scalar_field, format_xdmf_scalar_field(params, params.filename_out, path, "rho"));
fprintf(xdmf_fd, str_xdmf_vector_field, format_xdmf_vector_field(params, params.filename_out, path, "velocity", "u", "v"));
fprintf(xdmf_fd, str_xdmf_scalar_field, format_xdmf_scalar_field(params, params.filename_out, path, "prs"));
fprintf(xdmf_fd, "%s", str_xdmf_ite_footer);
fprintf(xdmf_fd, "%s", str_xdmf_footer);
fclose(xdmf_fd);
}
RestartInfo loadSnapshot(Array &Q) {
File file(params.restart_file, File::ReadOnly);
auto Nt = getShape(file, "rho")[0];
if (Nt != params.Nx*params.Ny) {
std::cerr << "Attempting to restart with a different resolution ! Ncells (restart) = " << Nt << "; Run resolution = "
<< params.Nx << "x" << params.Ny << "=" << params.Nx*params.Ny << std::endl;
throw std::runtime_error("ERROR : Trying to restart from a file with a different resolution !");
}
auto Qhost = Kokkos::create_mirror(Q);
using Table = std::vector<real_t>;
std::cout << "Loading restart data from hdf5" << std::endl;
auto load_and_copy = [&](std::string var_name, IVar var_id) {
auto table = load<Table>(file, var_name);
// Parallel for here ?
int lid = 0;
for (int y=0; y < params.Ny; ++y) {
for (int x=0; x < params.Nx; ++x) {
Qhost(y+params.jbeg, x+params.ibeg, var_id) = table[lid++];
}
}
};
load_and_copy("rho", IR);
load_and_copy("u", IU);
load_and_copy("v", IV);
load_and_copy("prs", IP);
Kokkos::deep_copy(Q, Qhost);
std::cout << "Restart finished !" << std::endl;
real_t time = loadAttribute<real_t>(file, "", "time");
int iteration = loadAttribute<int>(file, "", "iteration");
return {time, iteration};
}
};
}