Line data Source code
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 :
31 : namespace libMesh
32 : {
33 :
34 150 : NonlinearImplicitSystem::NonlinearImplicitSystem (EquationSystems & es,
35 : const std::string & name_in,
36 150 : const unsigned int number_in) :
37 :
38 : Parent (es, name_in, number_in),
39 50 : nonlinear_solver (NonlinearSolver<Number>::build(*this)),
40 50 : diff_solver (),
41 50 : _n_nonlinear_iterations (0),
42 50 : _final_nonlinear_residual (1.e20),
43 150 : _operator_matrix (nullptr)
44 : {
45 : // Set default parameters
46 : // These were chosen to match the Petsc defaults
47 150 : es.parameters.set<Real> ("linear solver tolerance") = 1e-5;
48 150 : es.parameters.set<Real> ("linear solver minimum tolerance") = 1e-5;
49 150 : es.parameters.set<unsigned int>("linear solver maximum iterations") = 10000;
50 :
51 150 : es.parameters.set<unsigned int>("nonlinear solver maximum iterations") = 50;
52 150 : es.parameters.set<unsigned int>("nonlinear solver maximum function evaluations") = 10000;
53 :
54 150 : es.parameters.set<Real>("nonlinear solver absolute residual tolerance") = 1e-35;
55 150 : es.parameters.set<Real>("nonlinear solver relative residual tolerance") = 1e-8;
56 150 : es.parameters.set<Real>("nonlinear solver divergence tolerance") = 1e+4;
57 150 : es.parameters.set<Real>("nonlinear solver absolute step tolerance") = 1e-8;
58 150 : es.parameters.set<Real>("nonlinear solver relative step tolerance") = 1e-8;
59 :
60 150 : es.parameters.set<bool>("reuse preconditioner") = false;
61 150 : es.parameters.set<unsigned int>("reuse preconditioner maximum linear iterations") = 1;
62 :
63 150 : if (this->has_static_condensation())
64 24 : this->setup_static_condensation_preconditioner(*nonlinear_solver);
65 150 : }
66 :
67 :
68 :
69 92 : NonlinearImplicitSystem::~NonlinearImplicitSystem () = default;
70 :
71 :
72 :
73 0 : void NonlinearImplicitSystem::create_static_condensation()
74 : {
75 0 : Parent::create_static_condensation();
76 0 : this->setup_static_condensation_preconditioner(*nonlinear_solver);
77 0 : }
78 :
79 :
80 :
81 24 : void NonlinearImplicitSystem::clear ()
82 : {
83 : // clear the nonlinear solver
84 24 : 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
90 24 : Parent::clear();
91 :
92 : // And restore any StaticCondensation to defaults
93 24 : if (this->has_static_condensation())
94 0 : this->setup_static_condensation_preconditioner(*nonlinear_solver);
95 24 : }
96 :
97 :
98 :
99 0 : void NonlinearImplicitSystem::reinit ()
100 : {
101 : // re-initialize the nonlinear solver interface
102 0 : nonlinear_solver->clear();
103 :
104 : // force the solver to get a new preconditioner, in
105 : // case reuse was set
106 0 : nonlinear_solver->force_new_preconditioner();
107 :
108 : // FIXME - this is necessary for petsc_auto_fieldsplit
109 : // nonlinear_solver->init_names(*this);
110 :
111 0 : if (diff_solver.get())
112 0 : diff_solver->reinit();
113 :
114 : // initialize parent data
115 0 : Parent::reinit();
116 0 : }
117 :
118 :
119 :
120 2544 : void NonlinearImplicitSystem::set_solver_parameters ()
121 : {
122 : // Get a reference to the EquationSystems
123 : const EquationSystems & es =
124 1696 : this->get_equation_systems();
125 :
126 : // Get the user-specified nonlinear solver tolerances
127 4240 : const unsigned int maxits = parameters.have_parameter<unsigned int>("nonlinear solver maximum iterations") ?
128 0 : parameters.get<unsigned int>("nonlinear solver maximum iterations") :
129 4240 : es.parameters.get<unsigned int>("nonlinear solver maximum iterations");
130 :
131 4240 : const unsigned int maxfuncs = parameters.have_parameter<unsigned int>("nonlinear solver maximum function evaluations") ?
132 0 : parameters.get<unsigned int>("nonlinear solver maximum function evaluations") :
133 4240 : es.parameters.get<unsigned int>("nonlinear solver maximum function evaluations");
134 :
135 3392 : const double abs_resid_tol = parameters.have_parameter<Real>("nonlinear solver absolute residual tolerance") ?
136 0 : double(parameters.get<Real>("nonlinear solver absolute residual tolerance")) :
137 4240 : double(es.parameters.get<Real>("nonlinear solver absolute residual tolerance"));
138 :
139 4240 : const double rel_resid_tol = parameters.have_parameter<Real>("nonlinear solver relative residual tolerance") ?
140 0 : double(parameters.get<Real>("nonlinear solver relative residual tolerance")) :
141 4240 : double(es.parameters.get<Real>("nonlinear solver relative residual tolerance"));
142 :
143 4240 : const double div_tol = parameters.have_parameter<Real>("nonlinear solver divergence tolerance") ?
144 0 : double(parameters.get<Real>("nonlinear solver divergence tolerance")) :
145 4240 : double(es.parameters.get<Real>("nonlinear solver divergence tolerance"));
146 :
147 4240 : const double abs_step_tol = parameters.have_parameter<Real>("nonlinear solver absolute step tolerance") ?
148 0 : double(parameters.get<Real>("nonlinear solver absolute step tolerance")) :
149 4240 : double(es.parameters.get<Real>("nonlinear solver absolute step tolerance"));
150 :
151 4240 : const double rel_step_tol = parameters.have_parameter<Real>("nonlinear solver relative step tolerance")?
152 0 : double(parameters.get<Real>("nonlinear solver relative step tolerance")) :
153 2544 : double(es.parameters.get<Real>("nonlinear solver relative step tolerance"));
154 :
155 : // Get the user-specified linear solver tolerances
156 2544 : const auto [maxlinearits, linear_tol] = this->Parent::get_linear_solve_parameters();
157 :
158 4240 : const double linear_min_tol = parameters.have_parameter<Real>("linear solver minimum tolerance") ?
159 0 : double(parameters.get<Real>("linear solver minimum tolerance")) :
160 4240 : double(es.parameters.get<Real>("linear solver minimum tolerance"));
161 :
162 2544 : const bool reuse_preconditioner = parameters.have_parameter<unsigned int>("reuse preconditioner") ?
163 0 : parameters.get<unsigned int>("reuse preconditioner") :
164 2544 : es.parameters.get<bool>("reuse preconditioner");
165 : const unsigned int reuse_preconditioner_max_linear_its =
166 4240 : parameters.have_parameter<unsigned int>("reuse preconditioner maximum linear iterations") ?
167 0 : parameters.get<unsigned int>("reuse preconditioner maximum linear iterations") :
168 4240 : es.parameters.get<unsigned int>("reuse preconditioner maximum linear iterations");
169 :
170 : // Set all the parameters on the NonlinearSolver
171 2544 : nonlinear_solver->max_nonlinear_iterations = maxits;
172 2544 : nonlinear_solver->max_function_evaluations = maxfuncs;
173 2544 : nonlinear_solver->absolute_residual_tolerance = abs_resid_tol;
174 2544 : nonlinear_solver->relative_residual_tolerance = rel_resid_tol;
175 2544 : nonlinear_solver->divergence_tolerance = div_tol;
176 2544 : nonlinear_solver->absolute_step_tolerance = abs_step_tol;
177 2544 : nonlinear_solver->relative_step_tolerance = rel_step_tol;
178 2544 : nonlinear_solver->max_linear_iterations = maxlinearits;
179 2544 : nonlinear_solver->initial_linear_tolerance = linear_tol;
180 2544 : nonlinear_solver->minimum_linear_tolerance = linear_min_tol;
181 2544 : nonlinear_solver->set_reuse_preconditioner(reuse_preconditioner);
182 2544 : nonlinear_solver->set_reuse_preconditioner_max_linear_its(reuse_preconditioner_max_linear_its);
183 :
184 2544 : if (diff_solver.get())
185 : {
186 0 : diff_solver->max_nonlinear_iterations = maxits;
187 0 : diff_solver->absolute_residual_tolerance = abs_resid_tol;
188 0 : diff_solver->relative_residual_tolerance = rel_resid_tol;
189 0 : diff_solver->absolute_step_tolerance = abs_step_tol;
190 0 : diff_solver->relative_step_tolerance = rel_step_tol;
191 0 : diff_solver->max_linear_iterations = maxlinearits;
192 0 : diff_solver->initial_linear_tolerance = linear_tol;
193 0 : diff_solver->minimum_linear_tolerance = linear_min_tol;
194 : }
195 2544 : }
196 :
197 :
198 :
199 2544 : void NonlinearImplicitSystem::solve ()
200 : {
201 : // Log how long the nonlinear solve takes.
202 2542 : LOG_SCOPE("solve()", "System");
203 :
204 2544 : this->set_solver_parameters();
205 :
206 2544 : if (diff_solver.get())
207 : {
208 0 : diff_solver->solve();
209 :
210 : // Store the number of nonlinear iterations required to
211 : // solve and the final residual.
212 0 : _n_nonlinear_iterations = diff_solver->total_outer_iterations();
213 0 : _final_nonlinear_residual = 0.; // FIXME - support this!
214 : }
215 : else
216 : {
217 2544 : if (this->prefix_with_name())
218 2 : nonlinear_solver->init(this->prefix().c_str());
219 : else
220 2544 : 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.
232 2544 : if (_operator_matrix)
233 6 : std::tie(_n_nonlinear_iterations, _final_nonlinear_residual) =
234 12 : nonlinear_solver->solve (*_operator_matrix, *matrix, *solution, *rhs,
235 4 : nonlinear_solver->relative_residual_tolerance,
236 14 : nonlinear_solver->max_linear_iterations);
237 : else
238 2532 : std::tie(_n_nonlinear_iterations, _final_nonlinear_residual) =
239 3376 : nonlinear_solver->solve (*matrix, *solution, *rhs,
240 844 : nonlinear_solver->relative_residual_tolerance,
241 3376 : nonlinear_solver->max_linear_iterations);
242 : }
243 :
244 : // Update the system after the solve
245 2538 : this->update();
246 2538 : }
247 :
248 :
249 :
250 0 : std::pair<unsigned int, Real> NonlinearImplicitSystem::get_linear_solve_parameters() const
251 : {
252 0 : if (diff_solver.get())
253 0 : return std::make_pair(this->diff_solver->max_linear_iterations,
254 0 : this->diff_solver->relative_residual_tolerance);
255 0 : return std::make_pair(this->nonlinear_solver->max_linear_iterations,
256 0 : this->nonlinear_solver->relative_residual_tolerance);
257 : }
258 :
259 :
260 :
261 0 : void NonlinearImplicitSystem::assembly(bool get_residual,
262 : bool get_jacobian,
263 : bool /*apply_heterogeneous_constraints*/,
264 : bool /*apply_no_constraints*/)
265 : {
266 0 : libmesh_assert(this->get_mesh().is_prepared());
267 : #if defined(DEBUG) && !defined(LIBMESH_ENABLE_DEPRECATED)
268 : MeshTools::libmesh_assert_valid_is_prepared(this->get_mesh());
269 : #endif
270 :
271 : // Get current_local_solution in sync
272 0 : 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 0 : 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 0 : 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 0 : 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 0 : if (get_jacobian)
288 : {
289 0 : if (nonlinear_solver->jacobian != nullptr)
290 0 : nonlinear_solver->jacobian (*current_local_solution.get(), *matrix, *this);
291 :
292 0 : else if (nonlinear_solver->jacobian_object != nullptr)
293 0 : nonlinear_solver->jacobian_object->jacobian (*current_local_solution.get(), *matrix, *this);
294 :
295 0 : else if (nonlinear_solver->matvec != nullptr)
296 0 : nonlinear_solver->matvec (*current_local_solution.get(), get_residual ? rhs : nullptr, matrix, *this);
297 :
298 0 : else if (nonlinear_solver->residual_and_jacobian_object != nullptr)
299 0 : nonlinear_solver->residual_and_jacobian_object->residual_and_jacobian (*current_local_solution.get(), get_residual ? rhs : nullptr, matrix, *this);
300 :
301 : else
302 0 : libmesh_error_msg("Error! Unable to compute residual and/or Jacobian!");
303 : }
304 :
305 0 : if (get_residual)
306 : {
307 0 : if (nonlinear_solver->residual != nullptr)
308 0 : nonlinear_solver->residual (*current_local_solution.get(), *rhs, *this);
309 :
310 0 : else if (nonlinear_solver->residual_object != nullptr)
311 0 : nonlinear_solver->residual_object->residual (*current_local_solution.get(), *rhs, *this);
312 :
313 0 : else if (nonlinear_solver->matvec != nullptr)
314 : {
315 : // we might have already grabbed the residual and jacobian together
316 0 : if (!get_jacobian)
317 0 : nonlinear_solver->matvec (*current_local_solution.get(), rhs, nullptr, *this);
318 : }
319 :
320 0 : else if (nonlinear_solver->residual_and_jacobian_object != nullptr)
321 : {
322 : // we might have already grabbed the residual and jacobian together
323 0 : if (!get_jacobian)
324 0 : nonlinear_solver->residual_and_jacobian_object->residual_and_jacobian (*current_local_solution.get(), rhs, nullptr, *this);
325 : }
326 :
327 : else
328 0 : libmesh_error_msg("Error! Unable to compute residual and/or Jacobian!");
329 : }
330 : else
331 0 : libmesh_assert(get_jacobian); // I can't believe you really wanted to assemble *nothing*
332 0 : }
333 :
334 :
335 :
336 :
337 0 : unsigned NonlinearImplicitSystem::get_current_nonlinear_iteration_number() const
338 : {
339 0 : return nonlinear_solver->get_current_nonlinear_iteration_number();
340 : }
341 :
342 :
343 :
344 : } // namespace libMesh
|