libMesh
Loading...
Searching...
No Matches
nonlinear_implicit_system.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20// Local includes
21#include "libmesh/nonlinear_implicit_system.h"
22#include "libmesh/diff_solver.h"
23#include "libmesh/equation_systems.h"
24#include "libmesh/libmesh_logging.h"
25#include "libmesh/mesh_tools.h"
26#include "libmesh/nonlinear_solver.h"
27#include "libmesh/sparse_matrix.h"
28#include "libmesh/static_condensation.h"
29#include "libmesh/static_condensation_preconditioner.h"
30
31namespace libMesh
32{
33
35 const std::string & name_in,
36 const unsigned int number_in) :
37
38 Parent (es, name_in, number_in),
39 nonlinear_solver (NonlinearSolver<Number>::build(*this)),
40 diff_solver (),
41 _n_nonlinear_iterations (0),
42 _final_nonlinear_residual (1.e20),
43 _operator_matrix (nullptr)
44{
45 // Set default parameters
46 // These were chosen to match the Petsc defaults
47 es.parameters.set<Real> ("linear solver tolerance") = 1e-5;
48 es.parameters.set<Real> ("linear solver minimum tolerance") = 1e-5;
49 es.parameters.set<unsigned int>("linear solver maximum iterations") = 10000;
50
51 es.parameters.set<unsigned int>("nonlinear solver maximum iterations") = 50;
52 es.parameters.set<unsigned int>("nonlinear solver maximum function evaluations") = 10000;
53
54 es.parameters.set<Real>("nonlinear solver absolute residual tolerance") = 1e-35;
55 es.parameters.set<Real>("nonlinear solver relative residual tolerance") = 1e-8;
56 es.parameters.set<Real>("nonlinear solver divergence tolerance") = 1e+4;
57 es.parameters.set<Real>("nonlinear solver absolute step tolerance") = 1e-8;
58 es.parameters.set<Real>("nonlinear solver relative step tolerance") = 1e-8;
59
60 es.parameters.set<bool>("reuse preconditioner") = false;
61 es.parameters.set<unsigned int>("reuse preconditioner maximum linear iterations") = 1;
62
63 if (this->has_static_condensation())
65}
66
67
68
70
71
72
78
79
80
82{
83 // clear the nonlinear solver
84 nonlinear_solver->clear();
85
86 // FIXME - this is necessary for petsc_auto_fieldsplit
87 // nonlinear_solver->init_names(*this);
88
89 // clear the parent data
91
92 // And restore any StaticCondensation to defaults
93 if (this->has_static_condensation())
95}
96
97
98
100{
101 // re-initialize the nonlinear solver interface
102 nonlinear_solver->clear();
103
104 // force the solver to get a new preconditioner, in
105 // case reuse was set
106 nonlinear_solver->force_new_preconditioner();
107
108 // FIXME - this is necessary for petsc_auto_fieldsplit
109 // nonlinear_solver->init_names(*this);
110
111 if (diff_solver.get())
112 diff_solver->reinit();
113
114 // initialize parent data
116}
117
118
119
121{
122 // Get a reference to the EquationSystems
123 const EquationSystems & es =
124 this->get_equation_systems();
125
126 // Get the user-specified nonlinear solver tolerances
127 const unsigned int maxits = parameters.have_parameter<unsigned int>("nonlinear solver maximum iterations") ?
128 parameters.get<unsigned int>("nonlinear solver maximum iterations") :
129 es.parameters.get<unsigned int>("nonlinear solver maximum iterations");
130
131 const unsigned int maxfuncs = parameters.have_parameter<unsigned int>("nonlinear solver maximum function evaluations") ?
132 parameters.get<unsigned int>("nonlinear solver maximum function evaluations") :
133 es.parameters.get<unsigned int>("nonlinear solver maximum function evaluations");
134
135 const double abs_resid_tol = parameters.have_parameter<Real>("nonlinear solver absolute residual tolerance") ?
136 double(parameters.get<Real>("nonlinear solver absolute residual tolerance")) :
137 double(es.parameters.get<Real>("nonlinear solver absolute residual tolerance"));
138
139 const double rel_resid_tol = parameters.have_parameter<Real>("nonlinear solver relative residual tolerance") ?
140 double(parameters.get<Real>("nonlinear solver relative residual tolerance")) :
141 double(es.parameters.get<Real>("nonlinear solver relative residual tolerance"));
142
143 const double div_tol = parameters.have_parameter<Real>("nonlinear solver divergence tolerance") ?
144 double(parameters.get<Real>("nonlinear solver divergence tolerance")) :
145 double(es.parameters.get<Real>("nonlinear solver divergence tolerance"));
146
147 const double abs_step_tol = parameters.have_parameter<Real>("nonlinear solver absolute step tolerance") ?
148 double(parameters.get<Real>("nonlinear solver absolute step tolerance")) :
149 double(es.parameters.get<Real>("nonlinear solver absolute step tolerance"));
150
151 const double rel_step_tol = parameters.have_parameter<Real>("nonlinear solver relative step tolerance")?
152 double(parameters.get<Real>("nonlinear solver relative step tolerance")) :
153 double(es.parameters.get<Real>("nonlinear solver relative step tolerance"));
154
155 // Get the user-specified linear solver tolerances
156 const auto [maxlinearits, linear_tol] = this->Parent::get_linear_solve_parameters();
157
158 const double linear_min_tol = parameters.have_parameter<Real>("linear solver minimum tolerance") ?
159 double(parameters.get<Real>("linear solver minimum tolerance")) :
160 double(es.parameters.get<Real>("linear solver minimum tolerance"));
161
162 const bool reuse_preconditioner = parameters.have_parameter<unsigned int>("reuse preconditioner") ?
163 parameters.get<unsigned int>("reuse preconditioner") :
164 es.parameters.get<bool>("reuse preconditioner");
165 const unsigned int reuse_preconditioner_max_linear_its =
166 parameters.have_parameter<unsigned int>("reuse preconditioner maximum linear iterations") ?
167 parameters.get<unsigned int>("reuse preconditioner maximum linear iterations") :
168 es.parameters.get<unsigned int>("reuse preconditioner maximum linear iterations");
169
170 // Set all the parameters on the NonlinearSolver
171 nonlinear_solver->max_nonlinear_iterations = maxits;
172 nonlinear_solver->max_function_evaluations = maxfuncs;
173 nonlinear_solver->absolute_residual_tolerance = abs_resid_tol;
174 nonlinear_solver->relative_residual_tolerance = rel_resid_tol;
175 nonlinear_solver->divergence_tolerance = div_tol;
176 nonlinear_solver->absolute_step_tolerance = abs_step_tol;
177 nonlinear_solver->relative_step_tolerance = rel_step_tol;
178 nonlinear_solver->max_linear_iterations = maxlinearits;
179 nonlinear_solver->initial_linear_tolerance = linear_tol;
180 nonlinear_solver->minimum_linear_tolerance = linear_min_tol;
181 nonlinear_solver->set_reuse_preconditioner(reuse_preconditioner);
182 nonlinear_solver->set_reuse_preconditioner_max_linear_its(reuse_preconditioner_max_linear_its);
183
184 if (diff_solver.get())
185 {
186 diff_solver->max_nonlinear_iterations = maxits;
187 diff_solver->absolute_residual_tolerance = abs_resid_tol;
188 diff_solver->relative_residual_tolerance = rel_resid_tol;
189 diff_solver->absolute_step_tolerance = abs_step_tol;
190 diff_solver->relative_step_tolerance = rel_step_tol;
191 diff_solver->max_linear_iterations = maxlinearits;
192 diff_solver->initial_linear_tolerance = linear_tol;
193 diff_solver->minimum_linear_tolerance = linear_min_tol;
194 }
195}
196
197
198
200{
201 // Log how long the nonlinear solve takes.
202 LOG_SCOPE("solve()", "System");
203
204 this->set_solver_parameters();
205
206 if (diff_solver.get())
207 {
208 diff_solver->solve();
209
210 // Store the number of nonlinear iterations required to
211 // solve and the final residual.
212 _n_nonlinear_iterations = diff_solver->total_outer_iterations();
213 _final_nonlinear_residual = 0.; // FIXME - support this!
214 }
215 else
216 {
217 if (this->prefix_with_name())
218 nonlinear_solver->init(this->prefix().c_str());
219 else
220 nonlinear_solver->init();
221
222 // FIXME - this is necessary for petsc_auto_fieldsplit
223 // nonlinear_solver->init_names(*this);
224
225 // Solve the nonlinear system.
226 // Store the number of nonlinear iterations required to
227 // solve and the final residual.
228 //
229 // If a distinct Jacobian operator matrix has been registered (see
230 // set_operator_matrix()), use it as Amat while *matrix remains the preconditioning matrix
231 // (Pmat); otherwise use the ordinary single-matrix solve.
235 nonlinear_solver->relative_residual_tolerance,
236 nonlinear_solver->max_linear_iterations);
237 else
239 nonlinear_solver->solve (*matrix, *solution, *rhs,
240 nonlinear_solver->relative_residual_tolerance,
241 nonlinear_solver->max_linear_iterations);
242 }
243
244 // Update the system after the solve
245 this->update();
246}
247
248
249
250std::pair<unsigned int, Real> NonlinearImplicitSystem::get_linear_solve_parameters() const
251{
252 if (diff_solver.get())
253 return std::make_pair(this->diff_solver->max_linear_iterations,
254 this->diff_solver->relative_residual_tolerance);
255 return std::make_pair(this->nonlinear_solver->max_linear_iterations,
256 this->nonlinear_solver->relative_residual_tolerance);
257}
258
259
260
262 bool get_jacobian,
263 bool /*apply_heterogeneous_constraints*/,
264 bool /*apply_no_constraints*/)
265{
266 libmesh_assert(this->get_mesh().is_prepared());
267#if defined(DEBUG) && !defined(LIBMESH_ENABLE_DEPRECATED)
269#endif
270
271 // Get current_local_solution in sync
272 this->update();
273
274 //-----------------------------------------------------------------------------
275 // if the user has provided both function pointers and objects only the pointer
276 // will be used, so catch that as an error
277 libmesh_error_msg_if(nonlinear_solver->jacobian && nonlinear_solver->jacobian_object,
278 "ERROR: cannot specify both a function and object to compute the Jacobian!");
279
280 libmesh_error_msg_if(nonlinear_solver->residual && nonlinear_solver->residual_object,
281 "ERROR: cannot specify both a function and object to compute the Residual!");
282
283 libmesh_error_msg_if(nonlinear_solver->matvec && nonlinear_solver->residual_and_jacobian_object,
284 "ERROR: cannot specify both a function and object to compute the combined Residual & Jacobian!");
285
286
287 if (get_jacobian)
288 {
289 if (nonlinear_solver->jacobian != nullptr)
290 nonlinear_solver->jacobian (*current_local_solution.get(), *matrix, *this);
291
292 else if (nonlinear_solver->jacobian_object != nullptr)
293 nonlinear_solver->jacobian_object->jacobian (*current_local_solution.get(), *matrix, *this);
294
295 else if (nonlinear_solver->matvec != nullptr)
296 nonlinear_solver->matvec (*current_local_solution.get(), get_residual ? rhs : nullptr, matrix, *this);
297
298 else if (nonlinear_solver->residual_and_jacobian_object != nullptr)
299 nonlinear_solver->residual_and_jacobian_object->residual_and_jacobian (*current_local_solution.get(), get_residual ? rhs : nullptr, matrix, *this);
300
301 else
302 libmesh_error_msg("Error! Unable to compute residual and/or Jacobian!");
303 }
304
305 if (get_residual)
306 {
307 if (nonlinear_solver->residual != nullptr)
308 nonlinear_solver->residual (*current_local_solution.get(), *rhs, *this);
309
310 else if (nonlinear_solver->residual_object != nullptr)
311 nonlinear_solver->residual_object->residual (*current_local_solution.get(), *rhs, *this);
312
313 else if (nonlinear_solver->matvec != nullptr)
314 {
315 // we might have already grabbed the residual and jacobian together
316 if (!get_jacobian)
317 nonlinear_solver->matvec (*current_local_solution.get(), rhs, nullptr, *this);
318 }
319
320 else if (nonlinear_solver->residual_and_jacobian_object != nullptr)
321 {
322 // we might have already grabbed the residual and jacobian together
323 if (!get_jacobian)
324 nonlinear_solver->residual_and_jacobian_object->residual_and_jacobian (*current_local_solution.get(), rhs, nullptr, *this);
325 }
326
327 else
328 libmesh_error_msg("Error! Unable to compute residual and/or Jacobian!");
329 }
330 else
331 libmesh_assert(get_jacobian); // I can't believe you really wanted to assemble *nothing*
332}
333
334
335
336
338{
339 return nonlinear_solver->get_current_nonlinear_iteration_number();
340}
341
342
343
344} // namespace libMesh
This is the EquationSystems class.
Parameters parameters
Data structure holding arbitrary parameters.
NumericVector< Number > * rhs
The system matrix.
Manages consistently variables, degrees of freedom, coefficient vectors, and matrices for implicit sy...
virtual std::pair< unsigned int, Real > get_linear_solve_parameters() const
SparseMatrix< Number > * matrix
The system matrix.
virtual void clear() override
Clear all the data structures associated with the system.
virtual void create_static_condensation() override
Request that static condensation be performed for this system.
void setup_static_condensation_preconditioner(T &solver)
Sets up the static condensation preconditioner for the supplied solver.
void set_solver_parameters()
Copies system parameters into nonlinear solver parameters.
virtual void solve() override
Assembles & solves the nonlinear system R(x) = 0.
virtual std::pair< unsigned int, Real > get_linear_solve_parameters() const override
virtual void reinit() override
Reinitializes the member data fields associated with the system, so that, e.g., assemble() may be use...
unsigned get_current_nonlinear_iteration_number() const
If called during the solve(), for example by the user-specified residual or Jacobian function,...
NonlinearImplicitSystem(EquationSystems &es, const std::string &name, const unsigned int number)
Constructor.
unsigned int _n_nonlinear_iterations
The number of nonlinear iterations required to solve the nonlinear system R(x)=0.
virtual void assembly(bool get_residual, bool get_jacobian, bool apply_heterogeneous_constraints=false, bool apply_no_constraints=false) override
Assembles a residual in rhs and/or a jacobian in matrix, as requested.
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
The NonlinearSolver defines the default interface used to solve the nonlinear_implicit system.
SparseMatrix< Number > * _operator_matrix
An optional matrix to use as the actual Jacobian operator (what is Amat in PETSc lingo for the linear...
std::unique_ptr< DiffSolver > diff_solver
The DiffSolver defines an optional interface used to solve the nonlinear_implicit system.
Real _final_nonlinear_residual
The final residual for the nonlinear system R(x)
virtual void create_static_condensation() override
Request that static condensation be performed for this system.
virtual void clear() override
Clear all the data structures associated with the system.
This base class can be inherited from to provide interfaces to nonlinear solvers from different packa...
bool have_parameter(std::string_view) const
Definition parameters.h:420
T & set(const std::string &)
Definition parameters.h:494
const T & get(std::string_view) const
Definition parameters.h:451
virtual void reinit()
Reinitializes degrees of freedom and other required data on the current mesh.
Definition system.C:449
std::unique_ptr< NumericVector< Number > > current_local_solution
All the values I need to compute my contribution to the simulation at hand.
Definition system.h:1679
bool prefix_with_name() const
Definition system.h:1986
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition system.h:1667
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition system.C:521
bool has_static_condensation() const
Definition system.C:2724
Parameters parameters
Parameters for the system. If a parameter is not provided, it should be retrieved from the EquationSy...
Definition system.h:1600
std::string prefix() const
Definition system.h:1992
const EquationSystems & get_equation_systems() const
Definition system.h:779
const MeshBase & get_mesh() const
Definition system.h:2432
void libmesh_assert_valid_is_prepared(const MeshBase &mesh)
The following functions, no-ops except in builds with NDEBUG undefined, are for asserting internal co...
Definition mesh_tools.h:662
The libMesh namespace provides an interface to certain functionality in the library.
libmesh_assert(ctx)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real