-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathConcurrentProgramsHandler.cpp
More file actions
347 lines (281 loc) · 9.87 KB
/
Copy pathConcurrentProgramsHandler.cpp
File metadata and controls
347 lines (281 loc) · 9.87 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
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
/************************************************************************
* Copyright © 2020 The Multiphysics Modeling and Computation (M2C) Lab
* <kevin.wgy@gmail.com> <kevinw3@vt.edu>
************************************************************************/
#include <ConcurrentProgramsHandler.h>
#include <cassert>
//---------------------------------------------------------
ConcurrentProgramsHandler::ConcurrentProgramsHandler(IoData &iod_, MPI_Comm global_comm_, MPI_Comm &comm_)
: iod(iod_), global_comm(global_comm_),
m2c_comm(global_comm_), aeros_comm(), aeros(NULL),
aerof_comm(), aerof(NULL), m2c_twin_comm(), m2c_twin(NULL)
{
coupled = false; //default
// check if M2C is coupled with any other programs
int aeros_color = -1;
int aerof_color = -1;
int m2c_twin_color = -1;
// If coupled with another instantiation of M2C (through overset grids), figure out my role
twinning_status = NONE; //0~non-existent, 1~I am the ``leader'', 2~I am the ``follower''
if(iod.concurrent.m2c_twin.type == M2CTwinningData::OVERSET_GRIDS) {
// IMPORTANT: If mesh has "OVERSET" boundaries --> "leader"; otherwise --> follower
if(iod_.mesh.bc_x0 == MeshData::OVERSET || iod_.mesh.bc_xmax == MeshData::OVERSET ||
iod_.mesh.bc_y0 == MeshData::OVERSET || iod_.mesh.bc_ymax == MeshData::OVERSET ||
iod_.mesh.bc_z0 == MeshData::OVERSET || iod_.mesh.bc_zmax == MeshData::OVERSET)
twinning_status = LEADER;
else
twinning_status = FOLLOWER;
}
if(twinning_status == FOLLOWER) {
coupled = true;
m2c_color = 2; // my color
m2c_twin_color = 0; // the leader's color
maxcolor = 4;
// Because M2C cannot be coupled with M2C-follower and AERO-F at the same time, AERO-F and the
// M2C-follower share the same ``color'' (2).
//A follower to another M2C instantiation cannot be coupled with others
if(iod.concurrent.aeros.fsi_algo != AerosCouplingData::NONE ||
iod.concurrent.aerof.type != AerofCouplingData::NONE) {
fprintf(stdout,"\033[0;31m*** Error: A follower M2C instantiation cannot be coupled with "
"other software.\033[0m\n");
exit(-1);
}
}
// Either not coupled with anything, or if coupled, this is the leader M2C instantiation
else if(iod.concurrent.aeros.fsi_algo != AerosCouplingData::NONE ||
iod.concurrent.aerof.type != AerofCouplingData::NONE ||
iod.concurrent.m2c_twin.type != M2CTwinningData::NONE) {
// Within the family... Common codes allow multiple (more than 2) solvers coupled together
coupled = true;
//The following parameters are the same as "FLUID_ID" and "MAX_CODES" in AERO-S and AERO-F
m2c_color = 0; // my color
maxcolor = 4;
if(iod.concurrent.aeros.fsi_algo != AerosCouplingData::NONE)
aeros_color = 1; //"STRUCT_ID" in AERO-S
if(iod.concurrent.aerof.type != AerofCouplingData::NONE)
aerof_color = 2; //"AEROF_ID_FOR_M2C" in AERO-F
if(iod.concurrent.m2c_twin.type != M2CTwinningData::NONE) {
assert(twinning_status == LEADER);
m2c_twin_color = 2;
}
if(iod.concurrent.m2c_twin.type != M2CTwinningData::NONE &&
iod.concurrent.aerof.type != AerofCouplingData::NONE) {
fprintf(stdout,"\033[0;31m*** Error: Cannot be coupled with AERO-F and another instantiation of M2C"
"at the same time.\033[0m\n");
exit(-1);
}
}
// simultaneous operations w/ other programs
if(coupled)
SetupCommunicators();
// create inter-communicators
if(iod.concurrent.aeros.fsi_algo != AerosCouplingData::NONE) {
aeros_comm = c[aeros_color];
int aeros_size(-1);
MPI_Comm_size(aeros_comm, &aeros_size);
assert(aeros_size>0);
}
if(iod.concurrent.aerof.type != AerofCouplingData::NONE) {
aerof_comm = c[aerof_color];
int aerof_size(-1);
MPI_Comm_size(aerof_comm, &aerof_size);
assert(aerof_size>0);
}
if(iod.concurrent.m2c_twin.type != M2CTwinningData::NONE) {
m2c_twin_comm = c[m2c_twin_color];
int m2c_twin_size(-1);
MPI_Comm_size(m2c_twin_comm, &m2c_twin_size);
assert(m2c_twin_size>0);
}
// time-step size suggested by other solvers, will be updated
dt = -1.0;
tmax = -1.0;
// outputs the m2c communicator
comm_ = m2c_comm;
}
//---------------------------------------------------------
ConcurrentProgramsHandler::~ConcurrentProgramsHandler()
{
if(aeros) delete aeros;
if(aerof) delete aerof;
if(m2c_twin) delete m2c_twin;
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::InitializeMessengers(TriangulatedSurface *surf_, vector<Vec3D> *F_) //for AERO-S messengers
{
if(iod.concurrent.aeros.fsi_algo != AerosCouplingData::NONE) {
assert(surf_); //cannot be NULL
assert(F_); //cannot be NULL
aeros = new AerosMessenger(iod.concurrent.aeros, m2c_comm, aeros_comm, *surf_, *F_);
dt = aeros->GetTimeStepSize();
tmax = aeros->GetMaxTime();
}
if(iod.concurrent.aerof.type != AerofCouplingData::NONE) {
aerof = new AerofMessenger(iod, m2c_comm, aerof_comm);
}
if(iod.concurrent.m2c_twin.type != M2CTwinningData::NONE) {
m2c_twin = new M2CTwinMessenger(iod, m2c_comm, m2c_twin_comm, twinning_status);
}
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::SetupCommunicators()
{
MPI_Comm_rank(global_comm, &global_rank);
MPI_Comm_size(global_comm, &global_size);
MPI_Comm_split(global_comm, m2c_color + 1, global_rank, &m2c_comm);
MPI_Comm_rank(m2c_comm, &m2c_rank);
MPI_Comm_size(m2c_comm, &m2c_size);
assert(m2c_rank<m2c_size); //rank must be 0 -- (size-1)
c.resize(maxcolor);
c[m2c_color] = m2c_comm;
vector<int> leaders(maxcolor, -1);
vector<int> newleaders(maxcolor, -1);
if(m2c_rank == 0) {
leaders[m2c_color] = global_rank;
}
MPI_Allreduce(leaders.data(), newleaders.data(), maxcolor, MPI_INTEGER, MPI_MAX, global_comm);
for(int i=0; i<maxcolor; i++) {
if(i != m2c_color && newleaders[i] >= 0) {
// create a communicator between m2c and program i
int tag;
if(m2c_color < i)
tag = maxcolor * (m2c_color + 1) + i + 1;
else
tag = maxcolor * (i + 1) + m2c_color + 1;
MPI_Intercomm_create(m2c_comm, 0, global_comm, newleaders[i], tag, &c[i]);
}
}
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::Destroy()
{
if(aeros)
aeros->Destroy();
if(aerof)
aerof->Destroy();
if(m2c_twin)
m2c_twin->Destroy();
for(int i=0; i<(int)c.size(); i++)
MPI_Comm_free(&c[i]);
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::CommunicateBeforeTimeStepping(SpaceVariable3D *coordinates_,
DataManagers3D *dms_,
std::vector<GhostPoint> *ghost_nodes_inner_,
std::vector<GhostPoint> *ghost_nodes_outer_,
GlobalMeshInfo *global_mesh_, SpaceVariable3D *V,
SpaceVariable3D *ID, std::set<Int3> *spo_frozen_nodes)
{
if(aeros) {
aeros->CommunicateBeforeTimeStepping();
dt = aeros->GetTimeStepSize();
tmax = aeros->GetMaxTime();
}
if(aerof) {
assert(coordinates_);
assert(dms_);
assert(ghost_nodes_inner_);
assert(ghost_nodes_outer_);
assert(global_mesh_);
assert(ID);
assert(spo_frozen_nodes);
aerof->CommunicateBeforeTimeStepping();
}
if(m2c_twin) {
assert(coordinates_);
assert(dms_);
assert(ghost_nodes_inner_);
assert(ghost_nodes_outer_);
assert(global_mesh_);
assert(V);
assert(ID);
assert(spo_frozen_nodes);
m2c_twin->CommunicateBeforeTimeStepping(*coordinates_, *dms_, *ghost_nodes_inner_,
*ghost_nodes_outer_, *global_mesh_,
*V, *ID, *spo_frozen_nodes);
if(twinning_status==FOLLOWER) {
// in terms of time-stepping, the follower is one-step behind the leader --> O(dt) error
dt = m2c_twin->GetTimeStepSize();
tmax = m2c_twin->GetMaxTime();
}
}
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::FirstExchange(SpaceVariable3D *V, double dt0, double tmax0)
{
if(aeros) {
aeros->FirstExchange();
dt = aeros->GetTimeStepSize();
tmax = aeros->GetMaxTime();
}
if(aerof) {
assert(V);
assert(dt0>=0.0);
assert(tmax0>=0.0);
aerof->FirstExchange();
}
if(m2c_twin) {
if(twinning_status==LEADER) {
assert(dt0>=0.0);
assert(tmax0>=0.0);
}
assert(V);
m2c_twin->FirstExchange(*V, dt0, tmax0);
if(twinning_status==FOLLOWER) {
dt = m2c_twin->GetTimeStepSize();
tmax = m2c_twin->GetMaxTime();
}
}
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::Exchange(SpaceVariable3D *V, double dt0, double tmax0)
{
if(aeros) {
aeros->Exchange();
dt = aeros->GetTimeStepSize();
tmax = aeros->GetMaxTime();
}
if(aerof) {
assert(V);
assert(dt0>=0.0);
assert(tmax0>=0.0);
aerof->Exchange();
}
if(m2c_twin) {
if(twinning_status==LEADER) {
assert(dt0>=0.0);
assert(tmax0>=0.0);
}
assert(V);
m2c_twin->Exchange(*V, dt0, tmax0);
if(twinning_status==FOLLOWER) {
dt = m2c_twin->GetTimeStepSize();
tmax = m2c_twin->GetMaxTime();
}
}
}
//---------------------------------------------------------
void
ConcurrentProgramsHandler::FinalExchange(SpaceVariable3D *V)
{
if(aeros) {
aeros->FinalExchange();
dt = aeros->GetTimeStepSize();
tmax = aeros->GetMaxTime();
}
if(aerof) {
aerof->FinalExchange();
}
if(m2c_twin) {
assert(V);
m2c_twin->FinalExchange(*V);
}
}
//---------------------------------------------------------
//---------------------------------------------------------