-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathproblem_builder_base.cpp
More file actions
386 lines (324 loc) · 10.9 KB
/
problem_builder_base.cpp
File metadata and controls
386 lines (324 loc) · 10.9 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
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
#include "problem_builder.hpp"
namespace hephaestus
{
void
Problem::Update()
{
// 1. Update the fespaces.
_fespaces.Update();
// 2. Update the gridfunctions.
_gridfunctions.Update();
// 3. Update sources.
_sources.Update();
// 4. Update the preprocessors and postprocessors.
_preprocessors.Update();
_postprocessors.Update();
// 5. Rebuild operator (and equation system).
GetOperator()->Update();
}
void
ProblemBuilder::SetMesh(std::shared_ptr<mfem::ParMesh> pmesh)
{
logger.info("Setting Mesh");
GetProblem()->_pmesh = pmesh;
GetProblem()->_comm = pmesh->GetComm();
MPI_Comm_size(pmesh->GetComm(), &(GetProblem()->_num_procs));
MPI_Comm_rank(pmesh->GetComm(), &(GetProblem()->_myid));
}
void
ProblemBuilder::SetFESpaces(hephaestus::FESpaces & fespaces)
{
logger.info("Setting FE Spaces");
GetProblem()->_fespaces = fespaces;
}
void
ProblemBuilder::SetGridFunctions(hephaestus::GridFunctions & gridfunctions)
{
logger.info("Setting GridFunctions");
GetProblem()->_gridfunctions = gridfunctions;
}
void
ProblemBuilder::SetBoundaryConditions(hephaestus::BCMap & bc_map)
{
logger.info("Setting Boundary Conditions");
GetProblem()->_bc_map = bc_map;
}
void
ProblemBuilder::SetAuxSolvers(hephaestus::AuxSolvers & preprocessors)
{
logger.info("Setting AuxSolvers");
GetProblem()->_preprocessors = preprocessors;
}
void
ProblemBuilder::SetPostprocessors(hephaestus::AuxSolvers & postprocessors)
{
logger.info("Setting Postprocessors");
GetProblem()->_postprocessors = postprocessors;
}
void
ProblemBuilder::SetSources(hephaestus::Sources & sources)
{
logger.info("Setting Sources");
GetProblem()->_sources = sources;
}
void
ProblemBuilder::SetOutputs(hephaestus::Outputs & outputs)
{
logger.info("Setting Outputs");
GetProblem()->_outputs = outputs;
}
void
ProblemBuilder::SetSolverOptions(hephaestus::InputParameters & solver_options)
{
logger.info("Setting Solver Options");
GetProblem()->_solver_options = solver_options;
}
void
ProblemBuilder::SetJacobianPreconditioner(std::unique_ptr<mfem::Solver> preconditioner)
{
GetProblem()->GetOperator()->SetJacobianPreconditioner(std::move(preconditioner));
}
void
ProblemBuilder::SetJacobianSolver(std::unique_ptr<mfem::Solver> jacobian_solver)
{
GetProblem()->GetOperator()->SetJacobianSolver(std::move(jacobian_solver));
}
void
ProblemBuilder::SetCoefficients(hephaestus::Coefficients & coefficients)
{
logger.info("Setting Coefficients");
GetProblem()->_coefficients = coefficients;
}
void
ProblemBuilder::AddFESpace(std::string fespace_name, std::string fec_name, int vdim, int ordering)
{
logger.info("Adding {} FE Space to problem", fespace_name);
if (GetProblem()->_fespaces.Has(fespace_name))
{
const std::string error_message = "A fespace with the name " + fespace_name +
" has already been added to the problem fespaces.";
mfem::mfem_error(error_message.c_str());
}
if (!GetProblem()->_fecs.Has(fec_name))
{
auto fec = std::shared_ptr<mfem::FiniteElementCollection>(
mfem::FiniteElementCollection::New(fec_name.c_str()));
GetProblem()->_fecs.Register(fec_name, fec);
}
if (!GetProblem()->_fespaces.Has(fespace_name))
{
mfem::ParMesh * pmesh = GetProblem()->_pmesh.get();
if (pmesh == nullptr)
{
MFEM_ABORT("ParMesh not found when trying to add " << fespace_name << " to fespaces.");
}
auto pfes = std::make_shared<mfem::ParFiniteElementSpace>(
GetProblem()->_pmesh.get(), GetProblem()->_fecs.Get(fec_name), vdim, ordering);
GetProblem()->_fespaces.Register(fespace_name, std::move(pfes));
}
}
void
ProblemBuilder::AddGridFunction(std::string gridfunction_name, std::string fespace_name)
{
if (GetProblem()->_gridfunctions.Has(gridfunction_name))
{
const std::string error_message = "A gridfunction with the name " + gridfunction_name +
" has already been added to the problem gridfunctions.";
mfem::mfem_error(error_message.c_str());
}
if (!GetProblem()->_fespaces.Has(fespace_name))
{
MFEM_ABORT("FESpace " << fespace_name << " not found in fespaces when trying to add "
<< gridfunction_name
<< " associated with it into gridfunctions. Please add " << fespace_name
<< " to fespaces before adding this gridfunction.");
}
auto fespace = GetProblem()->_fespaces.Get(fespace_name);
auto gridfunc = std::make_shared<mfem::ParGridFunction>(fespace);
*gridfunc = 0.0;
GetProblem()->_gridfunctions.Register(gridfunction_name, std::move(gridfunc));
}
void
ProblemBuilder::AddBoundaryCondition(std::string bc_name,
std::shared_ptr<hephaestus::BoundaryCondition> bc)
{
if (GetProblem()->_bc_map.Has(bc_name))
{
const std::string error_message = "A boundary condition with the name " + bc_name +
" has already been added to the problem boundary conditions.";
mfem::mfem_error(error_message.c_str());
}
GetProblem()->_bc_map.Register(bc_name, std::move(bc));
}
void
ProblemBuilder::AddAuxSolver(std::string auxsolver_name, std::shared_ptr<hephaestus::AuxSolver> aux)
{
if (GetProblem()->_preprocessors.Has(auxsolver_name))
{
const std::string error_message = "An auxsolver with the name " + auxsolver_name +
" has already been added to the problem preprocessors.";
mfem::mfem_error(error_message.c_str());
}
GetProblem()->_preprocessors.Register(auxsolver_name, std::move(aux));
}
void
ProblemBuilder::AddPostprocessor(std::string auxsolver_name,
std::shared_ptr<hephaestus::AuxSolver> aux)
{
if (GetProblem()->_postprocessors.Has(auxsolver_name))
{
const std::string error_message = "An auxsolver with the name " + auxsolver_name +
" has already been added to the problem postprocessors.";
mfem::mfem_error(error_message.c_str());
}
GetProblem()->_postprocessors.Register(auxsolver_name, std::move(aux));
}
void
ProblemBuilder::AddSource(std::string source_name, std::shared_ptr<hephaestus::Source> source)
{
if (GetProblem()->_sources.Has(source_name))
{
const std::string error_message =
"A source with the name " + source_name + " has already been added to the problem sources.";
mfem::mfem_error(error_message.c_str());
}
GetProblem()->_sources.Register(source_name, std::move(source));
}
void
ProblemBuilder::ConstructJacobianSolver()
{
auto precond = std::make_unique<mfem::HypreBoomerAMG>();
precond->SetPrintLevel(GetGlobalPrintLevel());
GetProblem()->GetOperator()->SetJacobianPreconditioner(std::move(precond));
ConstructJacobianSolverWithOptions(SolverType::HYPRE_GMRES);
}
void
ProblemBuilder::ConstructJacobianSolverWithOptions(SolverType type, SolverParams default_params)
{
const auto & solver_options = GetProblem()->_solver_options;
const auto tolerance =
solver_options.GetOptionalParam<float>("Tolerance", default_params._tolerance);
const auto abs_tolerance =
solver_options.GetOptionalParam<float>("AbsTolerance", default_params._abs_tolerance);
const auto max_iter =
solver_options.GetOptionalParam<unsigned int>("MaxIter", default_params._max_iteration);
const auto print_level =
solver_options.GetOptionalParam<int>("PrintLevel", default_params._print_level);
const auto k_dim = solver_options.GetOptionalParam<unsigned int>("KDim", default_params._k_dim);
auto preconditioner = GetProblem()->GetOperator()->JacobianPreconditioner<mfem::HypreSolver>();
switch (type)
{
case SolverType::HYPRE_PCG:
{
auto solver = std::make_unique<mfem::HyprePCG>(GetProblem()->_comm);
solver->SetTol(tolerance);
solver->SetAbsTol(abs_tolerance);
solver->SetMaxIter(max_iter);
solver->SetPrintLevel(print_level);
if (preconditioner)
solver->SetPreconditioner(*preconditioner);
GetProblem()->GetOperator()->SetJacobianSolver(std::move(solver));
break;
}
case SolverType::HYPRE_GMRES:
{
auto solver = std::make_unique<mfem::HypreGMRES>(GetProblem()->_comm);
solver->SetTol(tolerance);
solver->SetAbsTol(abs_tolerance);
solver->SetMaxIter(max_iter);
solver->SetKDim(k_dim);
solver->SetPrintLevel(print_level);
if (preconditioner)
solver->SetPreconditioner(*preconditioner);
GetProblem()->GetOperator()->SetJacobianSolver(std::move(solver));
break;
}
case SolverType::HYPRE_FGMRES:
{
auto solver = std::make_unique<mfem::HypreFGMRES>(GetProblem()->_comm);
solver->SetTol(tolerance);
solver->SetMaxIter(max_iter);
solver->SetKDim(k_dim);
solver->SetPrintLevel(print_level);
if (preconditioner)
solver->SetPreconditioner(*preconditioner);
GetProblem()->GetOperator()->SetJacobianSolver(std::move(solver));
break;
}
case SolverType::HYPRE_AMG:
{
auto solver = std::make_unique<mfem::HypreBoomerAMG>();
solver->SetTol(tolerance);
solver->SetMaxIter(max_iter);
solver->SetPrintLevel(print_level);
GetProblem()->GetOperator()->SetJacobianSolver(std::move(solver));
break;
}
case SolverType::SUPER_LU:
{
auto solver = std::make_unique<hephaestus::SuperLUSolver>(GetProblem()->_comm);
GetProblem()->GetOperator()->SetJacobianSolver(std::move(solver));
break;
}
default:
{
MFEM_ABORT("Unsupported solver type specified.");
break;
}
}
}
void
ProblemBuilder::ConstructNonlinearSolver()
{
auto nl_solver = std::make_unique<mfem::NewtonSolver>(GetProblem()->_comm);
// Defaults to one iteration, without further nonlinear iterations
nl_solver->SetRelTol(0.0);
nl_solver->SetAbsTol(0.0);
nl_solver->SetMaxIter(1);
GetProblem()->GetOperator()->SetNonlinearSolver(std::move(nl_solver));
}
void
ProblemBuilder::InitializeSources()
{
GetProblem()->_sources.Init(GetProblem()->_gridfunctions,
GetProblem()->_fespaces,
GetProblem()->_bc_map,
GetProblem()->_coefficients);
}
void
ProblemBuilder::InitializeAuxSolvers()
{
GetProblem()->_preprocessors.Init(GetProblem()->_gridfunctions, GetProblem()->_coefficients);
GetProblem()->_postprocessors.Init(GetProblem()->_gridfunctions, GetProblem()->_coefficients);
}
void
ProblemBuilder::InitializeOutputs()
{
GetProblem()->_outputs.Init(GetProblem()->_gridfunctions);
}
void
ProblemBuilder::InitializeOperator()
{
// Setup initial conditions.
GetProblem()->GetOperator()->Init();
}
void
ProblemBuilder::FinalizeProblem(bool build_operator)
{
RegisterFESpaces();
RegisterGridFunctions();
RegisterAuxSolvers();
RegisterCoefficients();
if (build_operator)
{
ConstructOperator();
}
InitializeAuxSolvers();
InitializeSources();
InitializeOperator();
ConstructJacobianSolver();
ConstructNonlinearSolver();
ConstructTimestepper();
InitializeOutputs();
}
} // namespace hephaestus