forked from idaholab/moose
/
MultiAppProjectionTransfer.C
444 lines (366 loc) · 15.8 KB
/
MultiAppProjectionTransfer.C
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/
#include "MultiAppProjectionTransfer.h"
#include "FEProblem.h"
#include "AddVariableAction.h"
#include "libmesh/quadrature_gauss.h"
#include "libmesh/dof_map.h"
#include "libmesh/mesh_function.h"
#include "libmesh/mesh_tools.h"
#include "libmesh/string_to_enum.h"
void assemble_l2_from(EquationSystems & es, const std::string & system_name)
{
MultiAppProjectionTransfer * transfer = es.parameters.get<MultiAppProjectionTransfer *>("transfer");
transfer->assembleL2From(es, system_name);
}
void assemble_l2_to(EquationSystems & es, const std::string & system_name)
{
MultiAppProjectionTransfer * transfer = es.parameters.get<MultiAppProjectionTransfer *>("transfer");
transfer->assembleL2To(es, system_name);
}
template<>
InputParameters validParams<MultiAppProjectionTransfer>()
{
InputParameters params = validParams<MultiAppTransfer>();
params.addRequiredParam<AuxVariableName>("variable", "The auxiliary variable to store the transferred values in.");
params.addRequiredParam<VariableName>("source_variable", "The variable to transfer from.");
MooseEnum proj_type("l2", "l2");
params.addParam<MooseEnum>("proj_type", proj_type, "The type of the projection.");
MooseEnum families(AddVariableAction::getNonlinearVariableFamilies());
params.addParam<MooseEnum>("family", families, "Specifies the family of FE shape functions to use for this variable");
MooseEnum orders(AddVariableAction::getNonlinearVariableOrders());
params.addParam<MooseEnum>("order", orders, "Specifies the order of the FE shape function to use for this variable (additional orders not listed are allowed)");
return params;
}
MultiAppProjectionTransfer::MultiAppProjectionTransfer(const InputParameters & parameters) :
MultiAppTransfer(parameters),
_to_var_name(getParam<AuxVariableName>("variable")),
_from_var_name(getParam<VariableName>("source_variable")),
_proj_type(getParam<MooseEnum>("proj_type")),
_compute_matrix(true)
{
}
MultiAppProjectionTransfer::~MultiAppProjectionTransfer()
{
}
void
MultiAppProjectionTransfer::initialSetup()
{
switch (_direction)
{
case TO_MULTIAPP:
{
unsigned int n_apps = _multi_app->numGlobalApps();
_proj_sys.resize(n_apps, NULL);
// Keep track of which EquationSystems just had new Systems
// added to them
std::set<EquationSystems *> augmented_es;
for (unsigned int app = 0; app < n_apps; app++)
{
if (_multi_app->hasLocalApp(app))
{
MPI_Comm swapped = Moose::swapLibMeshComm(_multi_app->comm());
FEProblem & to_problem = *_multi_app->appProblem(app);
FEType fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family")));
//to_problem.addAuxVariable(_to_var_name, fe_type, NULL);
EquationSystems & to_es = to_problem.es();
LinearImplicitSystem & proj_sys = to_es.add_system<LinearImplicitSystem>("proj-sys-" + Utility::enum_to_string<FEFamily>(fe_type.family)
+ "-" + Utility::enum_to_string<Order>(fe_type.order)
+ "-" + name());
_proj_var_num = proj_sys.add_variable("var", fe_type);
proj_sys.attach_assemble_function(assemble_l2_to);
_proj_sys[app] = &proj_sys;
// We'll defer to_es.reinit() so we don't do it multiple
// times even if we add multiple new systems
augmented_es.insert(&to_es);
//to_problem.hideVariableFromOutput("var"); // hide the auxiliary projection variable
Moose::swapLibMeshComm(swapped);
}
}
// Make sure all new systems are initialized.
for (std::set<EquationSystems *>::iterator es_iter =
augmented_es.begin();
es_iter != augmented_es.end(); ++es_iter)
{
EquationSystems *es = *es_iter;
es->reinit();
}
}
break;
case FROM_MULTIAPP:
{
_proj_sys.resize(1);
FEProblem & to_problem = *_multi_app->problem();
FEType fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family")));
//to_problem.addAuxVariable(_to_var_name, fe_type, NULL);
EquationSystems & to_es = to_problem.es();
LinearImplicitSystem & proj_sys = to_es.add_system<LinearImplicitSystem>("proj-sys-" + Utility::enum_to_string<FEFamily>(fe_type.family)
+ "-" + Utility::enum_to_string<Order>(fe_type.order)
+ "-" + name());
_proj_var_num = proj_sys.add_variable("var", fe_type);
proj_sys.attach_assemble_function(assemble_l2_from);
_proj_sys[0] = &proj_sys;
// to_problem.hideVariableFromOutput("var"); // hide the auxiliary projection variable
to_es.reinit();
}
break;
}
}
void
MultiAppProjectionTransfer::assembleL2To(EquationSystems & es, const std::string & system_name)
{
unsigned int app = es.parameters.get<unsigned int>("app");
FEProblem & from_problem = *_multi_app->problem();
EquationSystems & from_es = from_problem.es();
MooseVariable & from_var = from_problem.getVariable(0, _from_var_name);
System & from_sys = from_var.sys().system();
unsigned int from_var_num = from_sys.variable_number(from_var.name());
NumericVector<Number> * serialized_from_solution = NumericVector<Number>::build(from_sys.comm()).release();
serialized_from_solution->init(from_sys.n_dofs(), false, SERIAL);
// Need to pull down a full copy of this vector on every processor so we can get values in parallel
from_sys.solution->localize(*serialized_from_solution);
MeshFunction from_func(from_es, *serialized_from_solution, from_sys.get_dof_map(), from_var_num);
from_func.init(Trees::ELEMENTS);
from_func.enable_out_of_mesh_mode(0.);
const MeshBase& mesh = es.get_mesh();
const unsigned int dim = mesh.mesh_dimension();
LinearImplicitSystem & system = es.get_system<LinearImplicitSystem>(system_name);
FEType fe_type = system.variable_type(0);
UniquePtr<FEBase> fe(FEBase::build(dim, fe_type));
QGauss qrule(dim, fe_type.default_quadrature_order());
fe->attach_quadrature_rule(&qrule);
const std::vector<Real> & JxW = fe->get_JxW();
const std::vector<std::vector<Real> > & phi = fe->get_phi();
const std::vector<Point> & xyz = fe->get_xyz();
const DofMap& dof_map = system.get_dof_map();
DenseMatrix<Number> Ke;
DenseVector<Number> Fe;
std::vector<dof_id_type> dof_indices;
MeshBase::const_element_iterator el = mesh.active_local_elements_begin();
const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end();
for ( ; el != end_el; ++el)
{
const Elem* elem = *el;
fe->reinit (elem);
dof_map.dof_indices (elem, dof_indices);
Ke.resize (dof_indices.size(), dof_indices.size());
Fe.resize (dof_indices.size());
for (unsigned int qp = 0; qp < qrule.n_points(); qp++)
{
Point qpt = xyz[qp];
Point pt = qpt + _multi_app->position(app);
Real f = from_func(pt);
// Now compute the element matrix and RHS contributions.
for (unsigned int i=0; i<phi.size(); i++)
{
// RHS
Fe(i) += JxW[qp] * (f * phi[i][qp]);
if (_compute_matrix)
for (unsigned int j = 0; j < phi.size(); j++)
{
// The matrix contribution
Ke(i,j) += JxW[qp] * (phi[i][qp] * phi[j][qp]);
}
}
dof_map.constrain_element_matrix_and_vector(Ke, Fe, dof_indices);
if (_compute_matrix)
system.matrix->add_matrix(Ke, dof_indices);
system.rhs->add_vector(Fe, dof_indices);
}
}
delete serialized_from_solution;
}
void
MultiAppProjectionTransfer::assembleL2From(EquationSystems & es, const std::string & system_name)
{
unsigned int n_apps = _multi_app->numGlobalApps();
std::vector<NumericVector<Number> *> from_slns(n_apps, NULL);
std::vector<MeshFunction *> from_fns(n_apps, NULL);
std::vector<MeshTools::BoundingBox *> from_bbs(n_apps, NULL);
// get bounding box, mesh function and solution for each subapp
for (unsigned int i = 0; i < n_apps; i++)
{
if (!_multi_app->hasLocalApp(i))
continue;
MPI_Comm swapped = Moose::swapLibMeshComm(_multi_app->comm());
FEProblem & from_problem = *_multi_app->appProblem(i);
EquationSystems & from_es = from_problem.es();
MeshBase & from_mesh = from_es.get_mesh();
MeshTools::BoundingBox * app_box = new MeshTools::BoundingBox(MeshTools::processor_bounding_box(from_mesh, from_mesh.processor_id()));
from_bbs[i] = app_box;
MooseVariable & from_var = from_problem.getVariable(0, _from_var_name);
System & from_sys = from_var.sys().system();
unsigned int from_var_num = from_sys.variable_number(from_var.name());
NumericVector<Number> * serialized_from_solution = NumericVector<Number>::build(from_sys.comm()).release();
serialized_from_solution->init(from_sys.n_dofs(), false, SERIAL);
// Need to pull down a full copy of this vector on every processor so we can get values in parallel
from_sys.solution->localize(*serialized_from_solution);
from_slns[i] = serialized_from_solution;
MeshFunction * from_func = new MeshFunction(from_es, *serialized_from_solution, from_sys.get_dof_map(), from_var_num);
from_func->init(Trees::ELEMENTS);
from_func->enable_out_of_mesh_mode(OutOfMeshValue);
from_fns[i] = from_func;
Moose::swapLibMeshComm(swapped);
}
const MeshBase& mesh = es.get_mesh();
const unsigned int dim = mesh.mesh_dimension();
LinearImplicitSystem & system = es.get_system<LinearImplicitSystem>(system_name);
FEType fe_type = system.variable_type(0);
UniquePtr<FEBase> fe(FEBase::build(dim, fe_type));
QGauss qrule(dim, fe_type.default_quadrature_order());
fe->attach_quadrature_rule(&qrule);
const std::vector<Real> & JxW = fe->get_JxW();
const std::vector<std::vector<Real> > & phi = fe->get_phi();
const std::vector<Point> & xyz = fe->get_xyz();
const DofMap& dof_map = system.get_dof_map();
DenseMatrix<Number> Ke;
DenseVector<Number> Fe;
std::vector<dof_id_type> dof_indices;
MeshBase::const_element_iterator el = mesh.active_local_elements_begin();
const MeshBase::const_element_iterator end_el = mesh.active_local_elements_end();
for ( ; el != end_el; ++el)
{
const Elem* elem = *el;
fe->reinit (elem);
dof_map.dof_indices (elem, dof_indices);
Ke.resize (dof_indices.size(), dof_indices.size());
Fe.resize (dof_indices.size());
for (unsigned int qp = 0; qp < qrule.n_points(); qp++)
{
Point qpt = xyz[qp];
Real f = 0.;
for (unsigned int app = 0; app < n_apps; app++)
{
Point pt = qpt - _multi_app->position(app);
if (from_bbs[app] != NULL && from_bbs[app]->contains_point(pt))
{
MPI_Comm swapped = Moose::swapLibMeshComm(_multi_app->comm());
f = (*from_fns[app])(pt);
Moose::swapLibMeshComm(swapped);
break;
}
}
// Now compute the element matrix and RHS contributions.
for (unsigned int i=0; i<phi.size(); i++)
{
// RHS
Fe(i) += JxW[qp] * (f * phi[i][qp]);
if (_compute_matrix)
for (unsigned int j = 0; j < phi.size(); j++)
{
// The matrix contribution
Ke(i,j) += JxW[qp] * (phi[i][qp] * phi[j][qp]);
}
}
dof_map.constrain_element_matrix_and_vector(Ke, Fe, dof_indices);
if (_compute_matrix)
system.matrix->add_matrix(Ke, dof_indices);
system.rhs->add_vector(Fe, dof_indices);
}
}
for (unsigned int i = 0; i < n_apps; i++)
{
delete from_fns[i];
delete from_bbs[i];
delete from_slns[i];
}
}
void
MultiAppProjectionTransfer::execute()
{
_console << "Beginning projection transfer " << name() << std::endl;
switch (_direction)
{
case TO_MULTIAPP:
toMultiApp();
break;
case FROM_MULTIAPP:
fromMultiApp();
break;
}
_console << "Finished projection transfer " << name() << std::endl;
}
void
MultiAppProjectionTransfer::projectSolution(FEProblem & to_problem, unsigned int app)
{
EquationSystems & proj_es = to_problem.es();
LinearImplicitSystem & ls = *_proj_sys[app];
// activate the current transfer
proj_es.parameters.set<MultiAppProjectionTransfer *>("transfer") = this;
proj_es.parameters.set<unsigned int>("app") = app;
// TODO: specify solver params in an input file
// solver tolerance
Real tol = proj_es.parameters.get<Real>("linear solver tolerance");
proj_es.parameters.set<Real>("linear solver tolerance") = 1e-10; // set our tolerance
// solve it
ls.solve();
proj_es.parameters.set<Real>("linear solver tolerance") = tol; // restore the original tolerance
// copy projected solution into target es
MeshBase & to_mesh = proj_es.get_mesh();
MooseVariable & to_var = to_problem.getVariable(0, _to_var_name);
System & to_sys = to_var.sys().system();
NumericVector<Number> * to_solution = to_sys.solution.get();
{
MeshBase::const_node_iterator it = to_mesh.local_nodes_begin();
const MeshBase::const_node_iterator end_it = to_mesh.local_nodes_end();
for ( ; it != end_it; ++it)
{
const Node * node = *it;
if (node->n_comp(to_sys.number(), to_var.number()) > 0)
{
const dof_id_type proj_index = node->dof_number(ls.number(), _proj_var_num, 0);
const dof_id_type to_index = node->dof_number(to_sys.number(), to_var.number(), 0);
to_solution->set(to_index, (*ls.solution)(proj_index));
}
}
}
{
MeshBase::const_element_iterator it = to_mesh.active_local_elements_begin();
const MeshBase::const_element_iterator end_it = to_mesh.active_local_elements_end();
for ( ; it != end_it; ++it)
{
const Elem * elem = *it;
if (elem->n_comp(to_sys.number(), to_var.number()) > 0)
{
const dof_id_type proj_index = elem->dof_number(ls.number(), _proj_var_num, 0);
const dof_id_type to_index = elem->dof_number(to_sys.number(), to_var.number(), 0);
to_solution->set(to_index, (*ls.solution)(proj_index));
}
}
}
to_solution->close();
to_sys.update();
}
void
MultiAppProjectionTransfer::toMultiApp()
{
_console << "Projecting solution" << std::endl;
for (unsigned int app = 0; app < _multi_app->numGlobalApps(); app++)
{
if (_multi_app->hasLocalApp(app))
{
MPI_Comm swapped = Moose::swapLibMeshComm(_multi_app->comm());
projectSolution(*_multi_app->appProblem(app), app);
Moose::swapLibMeshComm(swapped);
}
}
}
void
MultiAppProjectionTransfer::fromMultiApp()
{
_console << "Projecting solution" << std::endl;
projectSolution(*_multi_app->problem(), 0);
}