-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmodes.cpp
396 lines (364 loc) · 18.7 KB
/
modes.cpp
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "modes.hpp"
#include <iostream>
#include "logger.hpp"
#include "utilities.hpp"
void aste::runReplayMode(const aste::ExecutionContext &context, const std::string &asteConfigName)
{
aste::asteConfig asteConfiguration;
asteConfiguration.load(asteConfigName);
const std::string participantName = asteConfiguration.participantName;
addLogIdentity(participantName, context.rank);
ASTE_INFO << "ASTE Running in replay mode";
precice::SolverInterface preciceInterface(participantName, asteConfiguration.preciceConfigFilename, context.rank, context.size);
const int dim = preciceInterface.getDimensions();
size_t minMeshSize{0};
std::vector<int> vertexIDs;
double dt;
for (auto &asteInterface : asteConfiguration.asteInterfaces) {
const std::string meshname = asteInterface.meshFilePrefix;
asteInterface.meshes = aste::BaseName(meshname).findAll(context);
if (asteInterface.meshes.empty()) {
ASTE_ERROR << "ERROR: Could not find meshes for name: " << meshname;
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
asteInterface.meshID = preciceInterface.getMeshID(asteInterface.meshName);
for (const auto &dataname : asteInterface.writeVectorNames) {
const int dataID = preciceInterface.getDataID(dataname, asteInterface.meshID);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::WRITE, dim, dataname, dataID);
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
if (preciceInterface.isGradientDataRequired(dataID)) {
asteInterface.writeVectorNames.push_back(dataname + "_gradient");
asteInterface.mesh.meshdata.emplace_back(aste::datatype::GRADIENT, dim, dataname, dataID, dim);
}
#endif
}
for (const auto &dataname : asteInterface.readVectorNames) {
const int dataID = preciceInterface.getDataID(dataname, asteInterface.meshID);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::READ, dim, dataname, dataID);
}
for (const auto &dataname : asteInterface.writeScalarNames) {
const int dataID = preciceInterface.getDataID(dataname, asteInterface.meshID);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::WRITE, 1, dataname, dataID);
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
if (preciceInterface.isGradientDataRequired(dataID)) {
asteInterface.writeVectorNames.push_back(dataname + "_gradient");
asteInterface.mesh.meshdata.emplace_back(aste::datatype::GRADIENT, 1, dataname, dataID, dim);
}
#endif
}
for (const auto &dataname : asteInterface.readScalarNames) {
const int dataID = preciceInterface.getDataID(dataname, asteInterface.meshID);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::READ, 1, dataname, dataID);
}
ASTE_INFO << "Loading mesh from " << asteInterface.meshes.front().filename();
const bool requireConnectivity = preciceInterface.isMeshConnectivityRequired(asteInterface.meshID);
asteInterface.meshes.front().loadMesh(asteInterface.mesh, dim, requireConnectivity);
ASTE_INFO << "The loaded mesh " << asteInterface.meshes.front().filename() << " contains: " << asteInterface.mesh.summary();
vertexIDs = setupMesh(preciceInterface, asteInterface.mesh, asteInterface.meshID);
ASTE_DEBUG << "Mesh setup completed on Rank " << context.rank;
minMeshSize = std::max(minMeshSize, asteInterface.meshes.size());
}
dt = preciceInterface.initialize();
std::size_t round{0};
ASTE_DEBUG << "Looking for dt = " << asteConfiguration.startdt;
for (const auto &mesh : asteConfiguration.asteInterfaces.front().meshes) {
if (mesh.filename().find(std::to_string(asteConfiguration.startdt)) == std::string::npos)
round++;
else
break;
}
ASTE_DEBUG << "Found in position " << round << "\n";
ASTE_INFO << "ASTE Start mesh is " << asteConfiguration.asteInterfaces.front().meshes[round].filename();
ASTE_INFO << "ASTE Final mesh is " << asteConfiguration.asteInterfaces.front().meshes.back().filename();
if (preciceInterface.isActionRequired(precice::constants::actionWriteInitialData())) {
if (round == 0) {
ASTE_ERROR << "Starting from dt = " << std::to_string(asteConfiguration.startdt) << " but previous timestep \".init\" or " << std::to_string(asteConfiguration.startdt - 1) << " was not found. Please make sure the relevant Mesh exists.";
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
ASTE_INFO << "Write initial data for participant " << participantName;
for (auto &asteInterface : asteConfiguration.asteInterfaces) {
asteInterface.meshes[round - 1].loadData(asteInterface.mesh);
ASTE_INFO << "The mesh contains: " << asteInterface.mesh.summary();
for (const auto &meshdata : asteInterface.mesh.meshdata) {
if (meshdata.type == aste::datatype::WRITE) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size());
preciceInterface.writeBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
preciceInterface.writeBlockVectorData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
}
}
ASTE_DEBUG << "Data written: " << asteInterface.mesh.previewData();
}
preciceInterface.markActionFulfilled(precice::constants::actionWriteInitialData());
}
preciceInterface.initializeData();
const std::string &coric = precice::constants::actionReadIterationCheckpoint();
const std::string &cowic = precice::constants::actionWriteIterationCheckpoint();
while (preciceInterface.isCouplingOngoing() && (round < minMeshSize)) {
if (preciceInterface.isActionRequired(cowic)) {
ASTE_ERROR << "Implicit coupling schemes cannot be used with ASTE";
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
for (auto &asteInterface : asteConfiguration.asteInterfaces) {
ASTE_INFO << "Read mesh for t= " << round << " from " << asteInterface.meshes[round];
asteInterface.meshes[round].resetData(asteInterface.mesh);
asteInterface.meshes[round].loadData(asteInterface.mesh);
ASTE_DEBUG << "This roundmesh contains: " << asteInterface.mesh.summary();
for (const auto &meshdata : asteInterface.mesh.meshdata) {
if (meshdata.type == aste::datatype::WRITE) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size());
preciceInterface.writeBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
preciceInterface.writeBlockVectorData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
}
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
else if (meshdata.type == aste::datatype::GRADIENT) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
// preciceInterface.writeBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
preciceInterface.writeBlockScalarGradientData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim * dim);
preciceInterface.writeBlockVectorGradientData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
}
#endif
ASTE_DEBUG << "Data written: " << asteInterface.mesh.previewData(meshdata);
}
}
dt = preciceInterface.advance(dt);
if (preciceInterface.isActionRequired(coric)) {
ASTE_ERROR << "Implicit coupling schemes cannot be used with ASTE";
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
for (auto &asteInterface : asteConfiguration.asteInterfaces) {
for (auto &meshdata : asteInterface.mesh.meshdata) {
if (meshdata.type == aste::datatype::READ) {
switch (meshdata.numcomp) {
case 1:
meshdata.dataVector.resize(vertexIDs.size());
preciceInterface.readBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
meshdata.dataVector.resize(vertexIDs.size() * dim);
preciceInterface.readBlockVectorData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
ASTE_DEBUG << "Data read: " << asteInterface.mesh.previewData(meshdata);
}
}
}
round++;
}
preciceInterface.finalize();
};
void aste::runMapperMode(const aste::ExecutionContext &context, const OptionMap &options)
{
const std::string meshname = options["mesh"].as<std::string>();
const std::string participantName = options["participant"].as<std::string>();
const std::string dataname = options["data"].as<std::string>();
const bool isVector = options["vector"].as<bool>();
const std::string preciceConfig = options["precice-config"].as<std::string>();
addLogIdentity(participantName, context.rank);
ASTE_INFO << "ASTE Running in mapping test mode";
aste::asteConfig asteConfiguration;
// Create and configure solver interface
precice::SolverInterface preciceInterface(participantName, preciceConfig, context.rank, context.size);
const int dim = preciceInterface.getDimensions();
if (participantName == "A") {
asteConfiguration.participantName = "A";
asteConfiguration.preciceConfigFilename = preciceConfig;
aste::asteInterface asteInterface;
asteInterface.meshName = "A-Mesh";
asteInterface.meshFilePrefix = meshname;
asteInterface.meshID = preciceInterface.getMeshID(asteInterface.meshName);
asteInterface.meshes = aste::BaseName(meshname).findAll(context);
const int dataID = preciceInterface.getDataID("Data", asteInterface.meshID);
if (isVector) {
asteInterface.writeVectorNames.push_back(dataname);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::WRITE, dim, dataname, dataID);
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
if (preciceInterface.isGradientDataRequired(dataID)) {
asteInterface.writeVectorNames.push_back(dataname + "_gradient");
asteInterface.mesh.meshdata.emplace_back(aste::datatype::GRADIENT, dim, dataname, dataID, dim);
}
#endif
} else {
asteInterface.writeScalarNames.push_back(dataname);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::WRITE, 1, dataname, dataID);
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
if (preciceInterface.isGradientDataRequired(dataID)) {
asteInterface.writeVectorNames.push_back(dataname + "_gradient");
asteInterface.mesh.meshdata.emplace_back(aste::datatype::GRADIENT, 1, dataname, dataID, dim);
}
#endif
}
asteConfiguration.asteInterfaces.push_back(asteInterface);
} else if (participantName == "B") {
asteConfiguration.participantName = "B";
asteConfiguration.preciceConfigFilename = preciceConfig;
aste::asteInterface asteInterface;
asteInterface.meshName = "B-Mesh";
asteInterface.meshFilePrefix = meshname;
asteInterface.meshID = preciceInterface.getMeshID(asteInterface.meshName);
asteInterface.meshes = aste::BaseName(meshname).findAll(context);
const int dataID = preciceInterface.getDataID("Data", asteInterface.meshID);
if (isVector) {
asteInterface.writeVectorNames.push_back(dataname);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::READ, dim, dataname, dataID);
} else {
asteInterface.writeScalarNames.push_back(dataname);
asteInterface.mesh.meshdata.emplace_back(aste::datatype::READ, 1, dataname, dataID);
}
asteConfiguration.asteInterfaces.push_back(asteInterface);
}
auto asteInterface = asteConfiguration.asteInterfaces.front();
ASTE_INFO << "Loading mesh from " << asteInterface.meshes.front().filename();
const bool requireConnectivity = preciceInterface.isMeshConnectivityRequired(asteInterface.meshID);
asteInterface.meshes.front().loadMesh(asteInterface.mesh, dim, requireConnectivity);
asteInterface.meshes.front().loadData(asteInterface.mesh);
ASTE_INFO << "The loaded mesh " << asteInterface.meshes.front().filename() << " contains: " << asteInterface.mesh.summary();
auto vertexIDs = aste::setupMesh(preciceInterface, asteInterface.mesh, asteInterface.meshID);
ASTE_DEBUG << "Mesh setup completed on Rank " << context.rank;
double dt = preciceInterface.initialize();
if (preciceInterface.isActionRequired(precice::constants::actionWriteInitialData())) {
ASTE_DEBUG << "Write initial data for participant " << participantName;
for (auto const &asteInterface : asteConfiguration.asteInterfaces) {
for (const auto &meshdata : asteInterface.mesh.meshdata) {
if (meshdata.type == aste::datatype::WRITE) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size());
preciceInterface.writeBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
preciceInterface.writeBlockVectorData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
}
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
else if (meshdata.type == aste::datatype::GRADIENT) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
// preciceInterface.writeBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
preciceInterface.writeBlockScalarGradientData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim * dim);
preciceInterface.writeBlockVectorGradientData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
}
#endif
}
ASTE_DEBUG << "Data written: " << asteInterface.mesh.previewData();
}
preciceInterface.markActionFulfilled(precice::constants::actionWriteInitialData());
}
preciceInterface.initializeData();
const std::string &coric = precice::constants::actionReadIterationCheckpoint();
const std::string &cowic = precice::constants::actionWriteIterationCheckpoint();
size_t round = 0;
while (preciceInterface.isCouplingOngoing() && round < asteInterface.meshes.size()) {
if (preciceInterface.isActionRequired(cowic)) {
ASTE_ERROR << "Implicit coupling schemes cannot be used with ASTE";
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
for (auto &asteInterface : asteConfiguration.asteInterfaces) {
ASTE_INFO << "Read mesh for t=" << round << " from " << asteInterface.meshes[round];
asteInterface.meshes[round].resetData(asteInterface.mesh);
asteInterface.meshes[round].loadData(asteInterface.mesh);
ASTE_DEBUG << "This roundmesh contains: " << asteInterface.mesh.summary();
for (const auto &meshdata : asteInterface.mesh.meshdata) {
if (meshdata.type == aste::datatype::WRITE) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size());
preciceInterface.writeBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
preciceInterface.writeBlockVectorData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
ASTE_DEBUG << "Data written: " << asteInterface.mesh.previewData(meshdata);
}
#if PRECICE_VERSION_GREATER_EQUAL(2, 5, 0)
else if (meshdata.type == aste::datatype::GRADIENT) {
switch (meshdata.numcomp) {
case 1:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim);
preciceInterface.writeBlockScalarGradientData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
assert(meshdata.dataVector.size() == vertexIDs.size() * dim * dim);
preciceInterface.writeBlockVectorGradientData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
ASTE_DEBUG << "Gradient data written: " << asteInterface.mesh.previewData(meshdata);
}
#endif
}
}
dt = preciceInterface.advance(dt);
if (preciceInterface.isActionRequired(coric)) {
ASTE_ERROR << "Implicit coupling schemes cannot be used with ASTE";
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
for (auto &asteInterface : asteConfiguration.asteInterfaces) {
for (auto &meshdata : asteInterface.mesh.meshdata) {
if (meshdata.type == aste::datatype::READ) {
switch (meshdata.numcomp) {
case 1:
meshdata.dataVector.resize(vertexIDs.size());
preciceInterface.readBlockScalarData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
default:
meshdata.dataVector.resize(vertexIDs.size() * dim);
preciceInterface.readBlockVectorData(meshdata.dataID, vertexIDs.size(), vertexIDs.data(), meshdata.dataVector.data());
break;
}
ASTE_DEBUG << "Data read: " << asteInterface.mesh.previewData(meshdata);
}
}
}
round++;
}
// Write out results in same format as data was read
if (asteConfiguration.participantName == "B") {
auto meshname = asteConfiguration.asteInterfaces.front().meshes.front();
auto filename = fs::path(options["output"].as<std::string>());
if (context.rank == 0 && fs::exists(filename)) {
if (context.isParallel() && !filename.parent_path().empty()) {
auto dir = filename.parent_path();
fs::remove_all(dir);
fs::create_directory(dir);
} else if (!context.isParallel()) {
fs::remove(filename);
}
}
MPI_Barrier(MPI_COMM_WORLD);
//
ASTE_INFO << "Writing results to " << options["output"].as<std::string>();
meshname.save(asteConfiguration.asteInterfaces.front().mesh, options["output"].as<std::string>());
}
preciceInterface.finalize();
return;
}