forked from idaholab/moose
/
NonlinearSystem.C
443 lines (387 loc) · 13.8 KB
/
NonlinearSystem.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
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
// moose includes
#include "NonlinearSystem.h"
#include "FEProblem.h"
#include "DisplacedProblem.h"
#include "TimeIntegrator.h"
#include "FiniteDifferencePreconditioner.h"
#include "PetscSupport.h"
#include "ComputeResidualFunctor.h"
#include "ComputeFDResidualFunctor.h"
#include "TimedPrint.h"
#include "MooseVariableScalar.h"
#include "MooseTypes.h"
#include "libmesh/nonlinear_solver.h"
#include "libmesh/petsc_nonlinear_solver.h"
#include "libmesh/sparse_matrix.h"
#include "libmesh/petsc_matrix.h"
#include "libmesh/diagonal_matrix.h"
#include "libmesh/default_coupling.h"
namespace Moose
{
void
compute_jacobian(const NumericVector<Number> & soln,
SparseMatrix<Number> & jacobian,
NonlinearImplicitSystem & sys)
{
FEProblemBase * p =
sys.get_equation_systems().parameters.get<FEProblemBase *>("_fe_problem_base");
p->computeJacobianSys(sys, soln, jacobian);
}
void
compute_bounds(NumericVector<Number> & lower,
NumericVector<Number> & upper,
NonlinearImplicitSystem & sys)
{
FEProblemBase * p =
sys.get_equation_systems().parameters.get<FEProblemBase *>("_fe_problem_base");
p->computeBounds(sys, lower, upper);
}
void
compute_nullspace(std::vector<NumericVector<Number> *> & sp, NonlinearImplicitSystem & sys)
{
FEProblemBase * p =
sys.get_equation_systems().parameters.get<FEProblemBase *>("_fe_problem_base");
p->computeNullSpace(sys, sp);
}
void
compute_transpose_nullspace(std::vector<NumericVector<Number> *> & sp,
NonlinearImplicitSystem & sys)
{
FEProblemBase * p =
sys.get_equation_systems().parameters.get<FEProblemBase *>("_fe_problem_base");
p->computeTransposeNullSpace(sys, sp);
}
void
compute_nearnullspace(std::vector<NumericVector<Number> *> & sp, NonlinearImplicitSystem & sys)
{
FEProblemBase * p =
sys.get_equation_systems().parameters.get<FEProblemBase *>("_fe_problem_base");
p->computeNearNullSpace(sys, sp);
}
void
compute_postcheck(const NumericVector<Number> & old_soln,
NumericVector<Number> & search_direction,
NumericVector<Number> & new_soln,
bool & changed_search_direction,
bool & changed_new_soln,
NonlinearImplicitSystem & sys)
{
FEProblemBase * p =
sys.get_equation_systems().parameters.get<FEProblemBase *>("_fe_problem_base");
p->computePostCheck(
sys, old_soln, search_direction, new_soln, changed_search_direction, changed_new_soln);
}
} // namespace Moose
NonlinearSystem::NonlinearSystem(FEProblemBase & fe_problem, const std::string & name)
: NonlinearSystemBase(
fe_problem, fe_problem.es().add_system<TransientNonlinearImplicitSystem>(name), name),
_transient_sys(fe_problem.es().get_system<TransientNonlinearImplicitSystem>(name)),
_nl_residual_functor(_fe_problem),
_fd_residual_functor(_fe_problem),
_use_coloring_finite_difference(false)
{
nonlinearSolver()->residual_object = &_nl_residual_functor;
nonlinearSolver()->jacobian = Moose::compute_jacobian;
nonlinearSolver()->bounds = Moose::compute_bounds;
nonlinearSolver()->nullspace = Moose::compute_nullspace;
nonlinearSolver()->transpose_nullspace = Moose::compute_transpose_nullspace;
nonlinearSolver()->nearnullspace = Moose::compute_nearnullspace;
#ifdef LIBMESH_HAVE_PETSC
PetscNonlinearSolver<Real> * petsc_solver =
static_cast<PetscNonlinearSolver<Real> *>(_transient_sys.nonlinear_solver.get());
if (petsc_solver)
{
petsc_solver->set_residual_zero_out(false);
petsc_solver->set_jacobian_zero_out(false);
petsc_solver->use_default_monitor(false);
}
#endif
/// Forcefully init the default solution states to match those available in libMesh
/// Must be called here because it would call virtuals in the parent class
solutionState(_default_solution_states);
}
NonlinearSystem::~NonlinearSystem() {}
void
NonlinearSystem::init()
{
NonlinearSystemBase::init();
if (_automatic_scaling && _resid_vs_jac_scaling_param < 1. - TOLERANCE)
// Add diagonal matrix that will be used for computing scaling factors
_transient_sys.add_matrix<DiagonalMatrix>("scaling_matrix");
}
void
NonlinearSystem::solve()
{
// Only attach the postcheck function to the solver if we actually
// have dampers or if the FEProblemBase needs to update the solution,
// which is also done during the linesearch postcheck. It doesn't
// hurt to do this multiple times, it is just setting a pointer.
if (_fe_problem.hasDampers() || _fe_problem.shouldUpdateSolution() ||
_fe_problem.needsPreviousNewtonIteration())
_transient_sys.nonlinear_solver->postcheck = Moose::compute_postcheck;
if (_fe_problem.solverParams()._type != Moose::ST_LINEAR)
{
CONSOLE_TIMED_PRINT("Computing initial residual")
// Calculate the initial residual for use in the convergence criterion.
_computing_initial_residual = true;
_fe_problem.computeResidualSys(_transient_sys, *_current_solution, *_transient_sys.rhs);
_computing_initial_residual = false;
_transient_sys.rhs->close();
_initial_residual_before_preset_bcs = _transient_sys.rhs->l2_norm();
if (_compute_initial_residual_before_preset_bcs)
_console << "Initial residual before setting preset BCs: "
<< _initial_residual_before_preset_bcs << '\n';
}
// Clear the iteration counters
_current_l_its.clear();
_current_nl_its = 0;
// Initialize the solution vector using a predictor and known values from nodal bcs
setInitialSolution();
// Now that the initial solution has ben set, potentially perform a residual/Jacobian evaluation
// to determine variable scaling factors
if (_automatic_scaling)
{
if (_compute_scaling_once)
{
if (!_computed_scaling)
{
computeScaling();
_computed_scaling = true;
}
}
else
computeScaling();
}
#ifdef MOOSE_GLOBAL_AD_INDEXING
// We do not know a priori what variable a global degree of freedom corresponds to, so we need a
// map from global dof to scaling factor. We just use a ghosted NumericVector for that mapping
assembleScalingVector();
#endif
if (_use_finite_differenced_preconditioner)
{
_transient_sys.nonlinear_solver->fd_residual_object = &_fd_residual_functor;
setupFiniteDifferencedPreconditioner();
}
#ifdef LIBMESH_HAVE_PETSC
PetscNonlinearSolver<Real> & solver =
static_cast<PetscNonlinearSolver<Real> &>(*_transient_sys.nonlinear_solver);
solver.mffd_residual_object = &_fd_residual_functor;
solver.set_snesmf_reuse_base(_fe_problem.useSNESMFReuseBase());
#endif
if (_time_integrator)
{
_time_integrator->solve();
_time_integrator->postSolve();
_n_iters = _time_integrator->getNumNonlinearIterations();
_n_linear_iters = _time_integrator->getNumLinearIterations();
}
else
{
system().solve();
_n_iters = _transient_sys.n_nonlinear_iterations();
#ifdef LIBMESH_HAVE_PETSC
_n_linear_iters = solver.get_total_linear_iterations();
#endif
}
// store info about the solve
_final_residual = _transient_sys.final_nonlinear_residual();
#ifdef LIBMESH_HAVE_PETSC
if (_use_coloring_finite_difference)
#if PETSC_VERSION_LESS_THAN(3, 2, 0)
MatFDColoringDestroy(_fdcoloring);
#else
MatFDColoringDestroy(&_fdcoloring);
#endif
#endif
}
void
NonlinearSystem::stopSolve()
{
#ifdef LIBMESH_HAVE_PETSC
#if PETSC_VERSION_LESS_THAN(3, 0, 0)
#else
PetscNonlinearSolver<Real> & solver =
static_cast<PetscNonlinearSolver<Real> &>(*sys().nonlinear_solver);
SNESSetFunctionDomainError(solver.snes());
#endif
#endif
// Insert a NaN into the residual vector. As of PETSc-3.6, this
// should make PETSc return DIVERGED_NANORINF the next time it does
// a reduction. We'll write to the first local dof on every
// processor that has any dofs.
_transient_sys.rhs->close();
if (_transient_sys.rhs->local_size())
_transient_sys.rhs->set(_transient_sys.rhs->first_local_index(),
std::numeric_limits<Real>::quiet_NaN());
_transient_sys.rhs->close();
// Clean up by getting other vectors into a valid state for a
// (possible) subsequent solve. There may be more than just
// these...
if (_Re_time)
_Re_time->close();
_Re_non_time->close();
}
void
NonlinearSystem::setupFiniteDifferencedPreconditioner()
{
std::shared_ptr<FiniteDifferencePreconditioner> fdp =
std::dynamic_pointer_cast<FiniteDifferencePreconditioner>(_preconditioner);
if (!fdp)
mooseError("Did not setup finite difference preconditioner, and please add a preconditioning "
"block with type = fdp");
if (fdp->finiteDifferenceType() == "coloring")
{
setupColoringFiniteDifferencedPreconditioner();
_use_coloring_finite_difference = true;
}
else if (fdp->finiteDifferenceType() == "standard")
{
setupStandardFiniteDifferencedPreconditioner();
_use_coloring_finite_difference = false;
}
else
mooseError("Unknown finite difference type");
}
void
NonlinearSystem::setupStandardFiniteDifferencedPreconditioner()
{
#if LIBMESH_HAVE_PETSC
// Make sure that libMesh isn't going to override our preconditioner
_transient_sys.nonlinear_solver->jacobian = nullptr;
PetscNonlinearSolver<Number> * petsc_nonlinear_solver =
static_cast<PetscNonlinearSolver<Number> *>(_transient_sys.nonlinear_solver.get());
PetscMatrix<Number> * petsc_mat =
static_cast<PetscMatrix<Number> *>(&_transient_sys.get_system_matrix());
SNESSetJacobian(petsc_nonlinear_solver->snes(),
petsc_mat->mat(),
petsc_mat->mat(),
#if PETSC_VERSION_LESS_THAN(3, 4, 0)
SNESDefaultComputeJacobian,
#else
SNESComputeJacobianDefault,
#endif
nullptr);
#endif
}
void
NonlinearSystem::setupColoringFiniteDifferencedPreconditioner()
{
#ifdef LIBMESH_HAVE_PETSC
// Make sure that libMesh isn't going to override our preconditioner
_transient_sys.nonlinear_solver->jacobian = nullptr;
PetscNonlinearSolver<Number> & petsc_nonlinear_solver =
dynamic_cast<PetscNonlinearSolver<Number> &>(*_transient_sys.nonlinear_solver);
// Pointer to underlying PetscMatrix type
PetscMatrix<Number> * petsc_mat =
dynamic_cast<PetscMatrix<Number> *>(&_transient_sys.get_system_matrix());
#if PETSC_VERSION_LESS_THAN(3, 2, 0)
// This variable is only needed for PETSC < 3.2.0
PetscVector<Number> * petsc_vec =
dynamic_cast<PetscVector<Number> *>(_transient_sys.solution.get());
#endif
Moose::compute_jacobian(*_transient_sys.current_local_solution, *petsc_mat, _transient_sys);
if (!petsc_mat)
mooseError("Could not convert to Petsc matrix.");
petsc_mat->close();
PetscErrorCode ierr = 0;
ISColoring iscoloring;
#if PETSC_VERSION_LESS_THAN(3, 2, 0)
// PETSc 3.2.x
ierr = MatGetColoring(petsc_mat->mat(), MATCOLORING_LF, &iscoloring);
CHKERRABORT(libMesh::COMM_WORLD, ierr);
#elif PETSC_VERSION_LESS_THAN(3, 5, 0)
// PETSc 3.3.x, 3.4.x
ierr = MatGetColoring(petsc_mat->mat(), MATCOLORINGLF, &iscoloring);
CHKERRABORT(_communicator.get(), ierr);
#else
// PETSc 3.5.x
MatColoring matcoloring;
ierr = MatColoringCreate(petsc_mat->mat(), &matcoloring);
CHKERRABORT(_communicator.get(), ierr);
ierr = MatColoringSetType(matcoloring, MATCOLORINGLF);
CHKERRABORT(_communicator.get(), ierr);
ierr = MatColoringSetFromOptions(matcoloring);
CHKERRABORT(_communicator.get(), ierr);
ierr = MatColoringApply(matcoloring, &iscoloring);
CHKERRABORT(_communicator.get(), ierr);
ierr = MatColoringDestroy(&matcoloring);
CHKERRABORT(_communicator.get(), ierr);
#endif
MatFDColoringCreate(petsc_mat->mat(), iscoloring, &_fdcoloring);
MatFDColoringSetFromOptions(_fdcoloring);
// clang-format off
MatFDColoringSetFunction(_fdcoloring,
(PetscErrorCode(*)(void))(void (*)(void)) &
libMesh::libmesh_petsc_snes_fd_residual,
&petsc_nonlinear_solver);
// clang-format on
#if !PETSC_RELEASE_LESS_THAN(3, 5, 0)
MatFDColoringSetUp(petsc_mat->mat(), iscoloring, _fdcoloring);
#endif
#if PETSC_VERSION_LESS_THAN(3, 4, 0)
SNESSetJacobian(petsc_nonlinear_solver.snes(),
petsc_mat->mat(),
petsc_mat->mat(),
SNESDefaultComputeJacobianColor,
_fdcoloring);
#else
SNESSetJacobian(petsc_nonlinear_solver.snes(),
petsc_mat->mat(),
petsc_mat->mat(),
SNESComputeJacobianDefaultColor,
_fdcoloring);
#endif
#if PETSC_VERSION_LESS_THAN(3, 2, 0)
Mat my_mat = petsc_mat->mat();
MatStructure my_struct;
SNESComputeJacobian(
petsc_nonlinear_solver.snes(), petsc_vec->vec(), &my_mat, &my_mat, &my_struct);
#endif
#if PETSC_VERSION_LESS_THAN(3, 2, 0)
ISColoringDestroy(iscoloring);
#else
// PETSc 3.3.0
ISColoringDestroy(&iscoloring);
#endif
#endif
}
bool
NonlinearSystem::converged()
{
if (_fe_problem.hasException())
return false;
return _transient_sys.nonlinear_solver->converged;
}
void
NonlinearSystem::attachPreconditioner(Preconditioner<Number> * preconditioner)
{
nonlinearSolver()->attach_preconditioner(preconditioner);
}
void
NonlinearSystem::computeScalingJacobian()
{
_fe_problem.computeJacobianSys(_transient_sys, *_current_solution, _scaling_matrix);
}
void
NonlinearSystem::computeScalingResidual()
{
_fe_problem.computeResidualSys(_transient_sys, *_current_solution, RHS());
}
SNES
NonlinearSystem::getSNES()
{
PetscNonlinearSolver<Number> * petsc_solver =
dynamic_cast<PetscNonlinearSolver<Number> *>(nonlinearSolver());
if (petsc_solver)
return petsc_solver->snes();
else
mooseError("It is not a petsc nonlinear solver");
}