Line data Source code
1 : //* This file is part of the MOOSE framework
2 : //* https://mooseframework.inl.gov
3 : //*
4 : //* All rights reserved, see COPYRIGHT for full restrictions
5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 : //*
7 : //* Licensed under LGPL 2.1, please see LICENSE for details
8 : //* https://www.gnu.org/licenses/lgpl-2.1.html
9 :
10 : #include "FEProblemSolve.h"
11 :
12 : #include "FEProblem.h"
13 : #include "NonlinearSystemBase.h"
14 : #include "LinearSystem.h"
15 : #include "Convergence.h"
16 : #include "Executioner.h"
17 : #include "ConvergenceIterationTypes.h"
18 : #include "MooseUtils.h"
19 :
20 : std::set<std::string> const FEProblemSolve::_moose_line_searches = {"contact", "project"};
21 :
22 : const std::set<std::string> &
23 158206 : FEProblemSolve::mooseLineSearches()
24 : {
25 158206 : return _moose_line_searches;
26 : }
27 :
28 : InputParameters
29 290821 : FEProblemSolve::feProblemDefaultConvergenceParams()
30 : {
31 290821 : InputParameters params = emptyInputParameters();
32 :
33 1163284 : params.addParam<unsigned int>("nl_max_its", 50, "Max Nonlinear Iterations");
34 1163284 : params.addParam<unsigned int>("nl_forced_its", 0, "The Number of Forced Nonlinear Iterations");
35 1163284 : params.addParam<unsigned int>("nl_max_funcs", 10000, "Max Nonlinear solver function evaluations");
36 1163284 : params.addParam<Real>("nl_abs_tol", 1.0e-50, "Nonlinear Absolute Tolerance");
37 1163284 : params.addParam<Real>("nl_rel_tol", 1.0e-8, "Nonlinear Relative Tolerance");
38 872463 : params.addParam<Real>(
39 : "nl_div_tol",
40 581642 : 1.0e10,
41 : "Nonlinear Relative Divergence Tolerance. A negative value disables this check.");
42 872463 : params.addParam<Real>(
43 : "nl_abs_div_tol",
44 581642 : 1.0e50,
45 : "Nonlinear Absolute Divergence Tolerance. A negative value disables this check.");
46 1163284 : params.addParam<Real>("nl_abs_step_tol", 0., "Nonlinear Absolute step Tolerance");
47 1163284 : params.addParam<Real>("nl_rel_step_tol", 0., "Nonlinear Relative step Tolerance");
48 872463 : params.addParam<unsigned int>("n_max_nonlinear_pingpong",
49 581642 : 100,
50 : "The maximum number of times the nonlinear residual can ping pong "
51 : "before requesting halting the current evaluation and requesting "
52 : "timestep cut for transient simulations");
53 :
54 872463 : params.addParamNamesToGroup(
55 : "nl_max_its nl_forced_its nl_max_funcs nl_abs_tol nl_rel_tol "
56 : "nl_rel_step_tol nl_abs_step_tol nl_div_tol nl_abs_div_tol n_max_nonlinear_pingpong",
57 : "Nonlinear Solver");
58 :
59 290821 : return params;
60 0 : }
61 :
62 : InputParameters
63 158206 : FEProblemSolve::validParams()
64 : {
65 158206 : InputParameters params = MultiSystemSolveObject::validParams();
66 158206 : params += FEProblemSolve::feProblemDefaultConvergenceParams();
67 :
68 158206 : std::set<std::string> line_searches = mooseLineSearches();
69 :
70 158206 : std::set<std::string> alias_line_searches = {"default", "none", "basic"};
71 158206 : line_searches.insert(alias_line_searches.begin(), alias_line_searches.end());
72 158206 : std::set<std::string> petsc_line_searches = Moose::PetscSupport::getPetscValidLineSearches();
73 158206 : line_searches.insert(petsc_line_searches.begin(), petsc_line_searches.end());
74 632824 : std::string line_search_string = Moose::stringify(line_searches, " ");
75 316412 : MooseEnum line_search(line_search_string, "default");
76 158206 : std::string addtl_doc_str(" (Note: none = basic)");
77 158206 : params.addParam<MooseEnum>(
78 316412 : "line_search", line_search, "Specifies the line search type" + addtl_doc_str);
79 632824 : MooseEnum line_search_package("petsc moose", "petsc");
80 632824 : params.addParam<MooseEnum>("line_search_package",
81 : line_search_package,
82 : "The solver package to use to conduct the line-search");
83 :
84 474618 : params.addParam<unsigned>("contact_line_search_allowed_lambda_cuts",
85 316412 : 2,
86 : "The number of times lambda is allowed to be cut in half in the "
87 : "contact line search. We recommend this number be roughly bounded by 0 "
88 : "<= allowed_lambda_cuts <= 3");
89 474618 : params.addParam<Real>("contact_line_search_ltol",
90 : "The linear relative tolerance to be used while the contact state is "
91 : "changing between non-linear iterations. We recommend that this tolerance "
92 : "be looser than the standard linear tolerance");
93 :
94 158206 : params += Moose::PetscSupport::getPetscValidParams();
95 632824 : params.addParam<Real>("l_tol", 1.0e-5, "Linear Relative Tolerance");
96 632824 : params.addParam<Real>("l_abs_tol", 1.0e-50, "Linear Absolute Tolerance");
97 632824 : params.addParam<unsigned int>("l_max_its", 10000, "Max Linear Iterations");
98 632824 : params.addParam<std::vector<ConvergenceName>>(
99 : "nonlinear_convergence",
100 : "Name of the Convergence object(s) to use to assess convergence of the "
101 : "nonlinear system(s) solve. If not provided, the default Convergence "
102 : "associated with the Problem will be constructed internally.");
103 632824 : params.addParam<std::vector<ConvergenceName>>(
104 : "linear_convergence",
105 : "Name of the Convergence object(s) to use to assess convergence of the "
106 : "linear system(s) solve. If not provided, the linear solver tolerance parameters are used");
107 474618 : params.addParam<bool>(
108 : "snesmf_reuse_base",
109 316412 : true,
110 : "Specifies whether or not to reuse the base vector for matrix-free calculation");
111 474618 : params.addParam<bool>(
112 316412 : "skip_exception_check", false, "Specifies whether or not to skip exception check");
113 474618 : params.addParam<bool>(
114 : "use_pre_SMO_residual",
115 316412 : false,
116 : "Compute the pre-SMO residual norm and use it in the relative convergence check. The "
117 : "pre-SMO residual is computed at the begining of the time step before solution-modifying "
118 : "objects are executed. Solution-modifying objects include preset BCs, constraints, "
119 : "predictors, etc.");
120 632824 : params.addParam<bool>("automatic_scaling", "Whether to use automatic scaling for the variables.");
121 632824 : params.addParam<std::vector<bool>>(
122 : "compute_scaling_once",
123 : {true},
124 : "Whether the scaling factors should only be computed once at the beginning of the simulation "
125 : "through an extra Jacobian evaluation. If this is set to false, then the scaling factors "
126 : "will be computed during an extra Jacobian evaluation at the beginning of every time step. "
127 : "Vector entries correspond to each nonlinear system.");
128 632824 : params.addParam<std::vector<bool>>(
129 : "off_diagonals_in_auto_scaling",
130 : {false},
131 : "Whether to consider off-diagonals when determining automatic scaling factors. Vector "
132 : "entries correspond to each nonlinear system.");
133 1265648 : params.addRangeCheckedParam<std::vector<Real>>(
134 : "resid_vs_jac_scaling_param",
135 : {0},
136 : "0<=resid_vs_jac_scaling_param<=1",
137 : "A parameter that indicates the weighting of the residual vs the Jacobian in determining "
138 : "variable scaling parameters. A value of 1 indicates pure residual-based scaling. A value of "
139 : "0 indicates pure Jacobian-based scaling. Vector entries correspond to each nonlinear "
140 : "system.");
141 632824 : params.addParam<std::vector<std::vector<std::vector<std::string>>>>(
142 : "scaling_group_variables",
143 : "Name of variables that are grouped together for determining scale factors. (Multiple "
144 : "groups can be provided, separated by semicolon). Vector entries correspond to each "
145 : "nonlinear system.");
146 632824 : params.addParam<std::vector<std::vector<std::string>>>(
147 : "ignore_variables_for_autoscaling",
148 : "List of variables that do not participate in autoscaling. Vector entries correspond to each "
149 : "nonlinear system.");
150 791030 : params.addRangeCheckedParam<unsigned int>(
151 : "num_grids",
152 316412 : 1,
153 : "num_grids>0",
154 : "The number of grids to use for a grid sequencing algorithm. This includes the final grid, "
155 : "so num_grids = 1 indicates just one solve in a time-step");
156 632824 : params.addParam<std::vector<bool>>("residual_and_jacobian_together",
157 : {false},
158 : "Whether to compute the residual and Jacobian together. "
159 : "Vector entries correspond to each nonlinear system.");
160 :
161 474618 : params.addParam<bool>("reuse_preconditioner",
162 316412 : false,
163 : "If true reuse the previously calculated "
164 : "preconditioner for the linearized "
165 : "system across multiple solves "
166 : "spanning nonlinear iterations and time steps. "
167 : "The preconditioner resets as controlled by "
168 : "reuse_preconditioner_max_linear_its");
169 474618 : params.addParam<unsigned int>("reuse_preconditioner_max_linear_its",
170 316412 : 25,
171 : "Reuse the previously calculated "
172 : "preconditioner for the linear system "
173 : "until the number of linear iterations "
174 : "exceeds this number");
175 :
176 : // Multi-system fixed point
177 : // Defaults to false because of the difficulty of defining a good multi-system convergence
178 : // criterion, unless we add a default one to the simulation?
179 474618 : params.addParam<bool>(
180 : "multi_system_fixed_point",
181 316412 : false,
182 : "Whether to perform fixed point (Picard) iterations between the nonlinear systems.");
183 1265648 : params.addRangeCheckedParam<std::vector<Real>>(
184 : "multi_system_fixed_point_relaxation_factor",
185 : {1.0},
186 : "multi_system_fixed_point_relaxation_factor>0 & multi_system_fixed_point_relaxation_factor<2",
187 : "Relaxation factor(s) applied to system solution updates during multi-system fixed point "
188 : "iterations; 1 disables relaxation. If one value is provided it is applied to every system; "
189 : "otherwise the vector must match the number/order of systems being solved.");
190 632824 : params.addParam<ConvergenceName>(
191 : "multi_system_fixed_point_convergence",
192 : "Convergence object to determine the convergence of the multi-system fixed point iteration.");
193 :
194 632824 : params.addParamNamesToGroup("l_tol l_abs_tol l_max_its reuse_preconditioner "
195 : "reuse_preconditioner_max_linear_its",
196 : "Linear Solver");
197 632824 : params.addParamNamesToGroup(
198 : "solve_type snesmf_reuse_base use_pre_SMO_residual "
199 : "num_grids residual_and_jacobian_together nonlinear_convergence linear_convergence",
200 : "Nonlinear Solver");
201 632824 : params.addParamNamesToGroup(
202 : "automatic_scaling compute_scaling_once off_diagonals_in_auto_scaling "
203 : "scaling_group_variables resid_vs_jac_scaling_param ignore_variables_for_autoscaling",
204 : "Solver variable scaling");
205 632824 : params.addParamNamesToGroup("line_search line_search_package contact_line_search_ltol "
206 : "contact_line_search_allowed_lambda_cuts",
207 : "Solver line search");
208 632824 : params.addParamNamesToGroup("multi_system_fixed_point multi_system_fixed_point_convergence "
209 : "multi_system_fixed_point_relaxation_factor",
210 : "Multiple solver system");
211 474618 : params.addParamNamesToGroup("skip_exception_check", "Advanced");
212 :
213 316412 : return params;
214 158206 : }
215 :
216 60385 : FEProblemSolve::FEProblemSolve(Executioner & ex)
217 : : MultiSystemSolveObject(ex),
218 60385 : _num_grid_steps(cast_int<unsigned int>(getParam<unsigned int>("num_grids") - 1)),
219 120770 : _using_multi_sys_fp_iterations(getParam<bool>("multi_system_fixed_point")),
220 120770 : _multi_sys_fp_convergence(nullptr) // has not been created yet
221 : {
222 181440 : if (_pars.isParamSetByUser("multi_system_fixed_point_relaxation_factor") &&
223 285 : !_using_multi_sys_fp_iterations)
224 0 : paramError("Can't use relaxation factors because multisystem fixed point iteration hasn't been "
225 : "enabled!");
226 :
227 60385 : setupMultiSystemFixedPointRelaxationFactors();
228 :
229 120770 : if (_moose_line_searches.find(getParam<MooseEnum>("line_search").operator std::string()) !=
230 120770 : _moose_line_searches.end())
231 0 : _problem.addLineSearch(_pars);
232 :
233 60686 : auto set_solver_params = [this, &ex](const SolverSystem & sys)
234 : {
235 60686 : const auto prefix = sys.prefix();
236 60686 : if (dynamic_cast<const LinearSystem *>(&sys))
237 1032 : Moose::PetscSupport::dontAddCommonSNESOptions(_problem, prefix);
238 60686 : Moose::PetscSupport::storePetscOptions(_problem, prefix, ex);
239 60686 : Moose::PetscSupport::setConvergedReasonFlags(_problem, prefix);
240 :
241 : // Set solver parameter prefix and system number
242 60686 : auto & solver_params = _problem.solverParams(sys.number());
243 60686 : solver_params._prefix = prefix;
244 60686 : solver_params._solver_sys_num = sys.number();
245 60686 : };
246 :
247 : // Extract and store PETSc related settings on FEProblemBase
248 121071 : for (const auto * const sys : _systems)
249 60686 : set_solver_params(*sys);
250 :
251 : // Set linear solve parameters in the equation system
252 : // Nonlinear solve parameters are added in the DefaultNonlinearConvergence
253 60385 : EquationSystems & es = _problem.es();
254 241540 : es.parameters.set<Real>("linear solver tolerance") = getParam<Real>("l_tol");
255 241540 : es.parameters.set<Real>("linear solver absolute tolerance") = getParam<Real>("l_abs_tol");
256 60385 : es.parameters.set<unsigned int>("linear solver maximum iterations") =
257 181155 : getParam<unsigned int>("l_max_its");
258 241540 : es.parameters.set<bool>("reuse preconditioner") = getParam<bool>("reuse_preconditioner");
259 60385 : es.parameters.set<unsigned int>("reuse preconditioner maximum linear iterations") =
260 181155 : getParam<unsigned int>("reuse_preconditioner_max_linear_its");
261 :
262 : // Transfer to the Problem misc nonlinear solve optimization parameters
263 60385 : _problem.setSNESMFReuseBase(getParam<bool>("snesmf_reuse_base"),
264 181155 : _pars.isParamSetByUser("snesmf_reuse_base"));
265 120770 : _problem.skipExceptionCheck(getParam<bool>("skip_exception_check"));
266 :
267 181155 : if (isParamValid("nonlinear_convergence"))
268 : {
269 369 : if (_problem.onlyAllowDefaultNonlinearConvergence())
270 0 : mooseError("The selected problem does not allow 'nonlinear_convergence' to be set.");
271 1107 : _problem.setNonlinearConvergenceNames(
272 : getParam<std::vector<ConvergenceName>>("nonlinear_convergence"));
273 : }
274 : else
275 60016 : _problem.setNeedToAddDefaultNonlinearConvergence();
276 181155 : if (isParamValid("linear_convergence"))
277 : {
278 134 : if (_problem.numLinearSystems() == 0)
279 0 : paramError(
280 : "linear_convergence",
281 : "Setting 'linear_convergence' is currently only possible for solving linear systems");
282 402 : _problem.setLinearConvergenceNames(
283 : getParam<std::vector<ConvergenceName>>("linear_convergence"));
284 : }
285 :
286 : // Check whether the user has explicitly requested automatic scaling and is using a solve type
287 : // without a matrix. If so, then we warn them
288 182526 : if ((_pars.isParamSetByUser("automatic_scaling") && getParam<bool>("automatic_scaling")) &&
289 413 : std::all_of(_systems.begin(),
290 : _systems.end(),
291 413 : [this](const auto & solver_sys)
292 413 : { return _problem.solverParams(solver_sys->number())._type == Moose::ST_JFNK; }))
293 : {
294 0 : paramWarning("automatic_scaling",
295 : "Automatic scaling isn't implemented for the case where you do not have a "
296 : "preconditioning matrix. No scaling will be applied");
297 0 : _problem.automaticScaling(false);
298 : }
299 : else
300 : // Check to see whether automatic_scaling has been specified anywhere, including at the
301 : // application level. No matter what: if we don't have a matrix, we don't do scaling
302 60385 : _problem.automaticScaling(
303 181155 : isParamValid("automatic_scaling")
304 61822 : ? getParam<bool>("automatic_scaling")
305 59906 : : (getMooseApp().defaultAutomaticScaling() &&
306 0 : std::any_of(_systems.begin(),
307 : _systems.end(),
308 0 : [this](const auto & solver_sys)
309 : {
310 0 : return _problem.solverParams(solver_sys->number())._type !=
311 0 : Moose::ST_JFNK;
312 : })));
313 :
314 179567 : if (!_using_multi_sys_fp_iterations && isParamValid("multi_system_fixed_point_convergence"))
315 6 : paramError("multi_system_fixed_point_convergence",
316 : "Cannot set a convergence object for multi-system fixed point iterations if "
317 : "'multi_system_fixed_point' is set to false");
318 61970 : if (_using_multi_sys_fp_iterations && !isParamValid("multi_system_fixed_point_convergence"))
319 6 : paramError("multi_system_fixed_point_convergence",
320 : "Must set a convergence object for multi-system fixed point iterations if using "
321 : "multi-system fixed point iterations");
322 :
323 : // Set the same parameters to every nonlinear system by default
324 60379 : int i_nl_sys = -1;
325 121038 : for (const auto i_sys : index_range(_systems))
326 : {
327 60668 : auto nl_ptr = dynamic_cast<NonlinearSystemBase *>(_systems[i_sys]);
328 : // Linear systems have very different parameters at the moment
329 60668 : if (!nl_ptr)
330 1032 : continue;
331 59636 : auto & nl = *nl_ptr;
332 59636 : i_nl_sys++;
333 :
334 119272 : nl.setPreSMOResidual(getParam<bool>("use_pre_SMO_residual"));
335 :
336 : const auto res_and_jac =
337 119272 : getParamFromNonlinearSystemVectorParam<bool>("residual_and_jacobian_together", i_nl_sys);
338 59630 : if (res_and_jac)
339 481 : nl.residualAndJacobianTogether();
340 :
341 : // Automatic scaling parameters
342 59630 : nl.computeScalingOnce(
343 119260 : getParamFromNonlinearSystemVectorParam<bool>("compute_scaling_once", i_nl_sys));
344 119260 : nl.autoScalingParam(
345 : getParamFromNonlinearSystemVectorParam<Real>("resid_vs_jac_scaling_param", i_nl_sys));
346 59630 : nl.offDiagonalsInAutoScaling(
347 119260 : getParamFromNonlinearSystemVectorParam<bool>("off_diagonals_in_auto_scaling", i_nl_sys));
348 178890 : if (isParamValid("scaling_group_variables"))
349 15 : nl.scalingGroupVariables(
350 60 : getParamFromNonlinearSystemVectorParam<std::vector<std::vector<std::string>>>(
351 : "scaling_group_variables", i_nl_sys));
352 178890 : if (isParamValid("ignore_variables_for_autoscaling"))
353 : {
354 : // Before setting ignore_variables_for_autoscaling, check that they are not present in
355 : // scaling_group_variables
356 36 : if (isParamValid("scaling_group_variables"))
357 : {
358 : const auto & ignore_variables_for_autoscaling =
359 : getParamFromNonlinearSystemVectorParam<std::vector<std::string>>(
360 6 : "ignore_variables_for_autoscaling", i_nl_sys);
361 : const auto & scaling_group_variables =
362 : getParamFromNonlinearSystemVectorParam<std::vector<std::vector<std::string>>>(
363 6 : "scaling_group_variables", i_nl_sys);
364 3 : for (const auto & group : scaling_group_variables)
365 6 : for (const auto & var_name : group)
366 6 : if (std::find(ignore_variables_for_autoscaling.begin(),
367 : ignore_variables_for_autoscaling.end(),
368 12 : var_name) != ignore_variables_for_autoscaling.end())
369 6 : paramError("ignore_variables_for_autoscaling",
370 : "Variables cannot be in a scaling grouping and also be ignored");
371 0 : }
372 9 : nl.ignoreVariablesForAutoscaling(
373 36 : getParamFromNonlinearSystemVectorParam<std::vector<std::string>>(
374 : "ignore_variables_for_autoscaling", i_nl_sys));
375 : }
376 : }
377 :
378 : // Multi-grid options
379 60370 : _problem.numGridSteps(_num_grid_steps);
380 60370 : }
381 :
382 : template <typename T>
383 : T
384 238556 : FEProblemSolve::getParamFromNonlinearSystemVectorParam(const std::string & param_name,
385 : unsigned int index) const
386 : {
387 238556 : const auto & param_vec = getParam<std::vector<T>>(param_name);
388 238556 : if (index > _num_nl_systems)
389 0 : paramError(param_name,
390 : "Vector parameter is requested at index (" + std::to_string(index) +
391 : ") which is larger than number of nonlinear systems (" +
392 0 : std::to_string(_num_nl_systems) + ").");
393 238556 : if (param_vec.size() == 0)
394 3 : paramError(
395 : param_name,
396 : "This parameter was passed to a routine which cannot handle empty vector parameters");
397 238553 : if (param_vec.size() != 1 && param_vec.size() != _num_nl_systems)
398 3 : paramError(param_name,
399 : "Vector parameter size (" + std::to_string(param_vec.size()) +
400 : ") is different than the number of nonlinear systems (" +
401 3 : std::to_string(_num_nl_systems) + ").");
402 :
403 : // User passed only one parameter, assume it applies to all nonlinear systems
404 238550 : if (param_vec.size() == 1)
405 238550 : return param_vec[0];
406 : else
407 0 : return param_vec[index];
408 : }
409 :
410 : void
411 57428 : FEProblemSolve::initialSetup()
412 : {
413 57428 : MultiSystemSolveObject::initialSetup();
414 57428 : convergenceSetup();
415 : // Keep track of the solution warnings from the setup
416 : // before a count reset at the beginning of the time step
417 57422 : if (!_app.isRecovering())
418 : {
419 53453 : _app.solutionInvalidity().syncIteration();
420 53453 : _app.solutionInvalidity().accumulateIterationIntoTimeStepOccurences();
421 53453 : _app.solutionInvalidity().accumulateTimeStepIntoTotalOccurences(0);
422 : }
423 57422 : }
424 :
425 : void
426 57428 : FEProblemSolve::convergenceSetup()
427 : {
428 : // nonlinear
429 57428 : const auto conv_names = _problem.getNonlinearConvergenceNames();
430 113977 : for (const auto & conv_name : conv_names)
431 : {
432 56555 : auto & conv = _problem.getConvergence(conv_name);
433 56555 : conv.checkIterationType(ConvergenceIterationTypes::NONLINEAR);
434 : }
435 :
436 : // linear
437 172266 : if (isParamValid("linear_convergence"))
438 : {
439 268 : const auto conv_names = getParam<std::vector<ConvergenceName>>("linear_convergence");
440 268 : for (const auto & conv_name : conv_names)
441 : {
442 134 : auto & conv = _problem.getConvergence(conv_name);
443 134 : conv.checkIterationType(ConvergenceIterationTypes::LINEAR);
444 : }
445 134 : }
446 :
447 : // multisystem fixed point
448 172266 : if (isParamValid("multi_system_fixed_point_convergence"))
449 : {
450 785 : _multi_sys_fp_convergence =
451 1570 : &_problem.getConvergence(getParam<ConvergenceName>("multi_system_fixed_point_convergence"));
452 785 : _multi_sys_fp_convergence->checkIterationType(
453 : ConvergenceIterationTypes::MULTISYSTEM_FIXED_POINT);
454 : }
455 57422 : }
456 :
457 : void
458 60385 : FEProblemSolve::setupMultiSystemFixedPointRelaxationFactors()
459 : {
460 : _multi_sys_fp_relax_factors =
461 120770 : getParam<std::vector<Real>>("multi_system_fixed_point_relaxation_factor");
462 60385 : if (_multi_sys_fp_relax_factors.size() == 1)
463 60385 : _multi_sys_fp_relax_factors.resize(_systems.size(), _multi_sys_fp_relax_factors[0]);
464 0 : else if (_multi_sys_fp_relax_factors.size() != _systems.size())
465 0 : paramError("multi_system_fixed_point_relaxation_factor",
466 0 : "Must provide either 1 value or " + Moose::stringify(_systems.size()) +
467 : " values (one per system in the solve order).");
468 60385 : }
469 :
470 : bool
471 312607 : FEProblemSolve::solve()
472 : {
473 : // Outer loop for multi-grid convergence
474 312607 : bool converged = false;
475 312607 : unsigned int num_fp_multisys_iters = 0;
476 :
477 623301 : for (MooseIndex(_num_grid_steps) grid_step = 0; grid_step <= _num_grid_steps; ++grid_step)
478 : {
479 : // Multi-system fixed point loop
480 312657 : num_fp_multisys_iters = 0;
481 312657 : converged = false;
482 648621 : while (!converged)
483 : {
484 337927 : if (_using_multi_sys_fp_iterations)
485 26053 : _console << COLOR_MAGENTA << "Multi-system fixed point iteration " << num_fp_multisys_iters
486 26053 : << ":" << COLOR_DEFAULT << "\n"
487 26053 : << std::endl;
488 :
489 : // Loop over each system
490 677998 : for (const auto sys_i : index_range(_systems))
491 : {
492 342034 : auto * const sys = _systems[sys_i];
493 342034 : const bool is_nonlinear = (dynamic_cast<NonlinearSystemBase *>(sys) != nullptr);
494 : const Real fp_relax =
495 342034 : _using_multi_sys_fp_iterations ? _multi_sys_fp_relax_factors[sys_i] : 1.0;
496 : const bool apply_fp_relax =
497 342034 : _using_multi_sys_fp_iterations && !MooseUtils::absoluteFuzzyEqual(fp_relax, 1.0);
498 342034 : if (apply_fp_relax)
499 : {
500 21100 : sys->setFixedPointRelaxationFactor(fp_relax);
501 21100 : sys->saveOldSolutionForFixedPointRelaxation();
502 : }
503 :
504 : // Call solve on the problem for that system
505 342034 : if (is_nonlinear)
506 315948 : _problem.solve(sys->number());
507 : else
508 : {
509 : const auto linear_sys_number =
510 26086 : cast_int<unsigned int>(sys->number() - _problem.numNonlinearSystems());
511 26086 : _problem.solveLinearSystem(linear_sys_number, &_problem.getPetscOptions());
512 : }
513 :
514 : // Check convergence
515 : const auto solve_name =
516 675738 : _systems.size() == 1 ? " Solve" : "System " + sys->name() + ": Solve";
517 341976 : if (_problem.shouldSolve())
518 : {
519 308284 : if (_problem.converged(sys->number()))
520 : {
521 306379 : if (apply_fp_relax)
522 21100 : sys->applyFixedPointRelaxation();
523 306379 : _console << COLOR_GREEN << solve_name << " Converged!" << COLOR_DEFAULT << "\n"
524 306379 : << std::endl;
525 : }
526 : else
527 : {
528 1902 : _console << COLOR_RED << solve_name << " Did NOT Converge!" << COLOR_DEFAULT << "\n"
529 1902 : << std::endl;
530 1902 : if (apply_fp_relax)
531 0 : sys->clearFixedPointRelaxation();
532 1902 : return false;
533 : }
534 : }
535 : else
536 33692 : _console << COLOR_GREEN << solve_name << " Skipped!" << COLOR_DEFAULT << "\n"
537 33692 : << std::endl;
538 :
539 340071 : if (!is_nonlinear)
540 : {
541 : const auto linear_sys_number =
542 26077 : cast_int<unsigned int>(sys->number() - _problem.numNonlinearSystems());
543 26077 : auto & linear_sys = _problem.getLinearSystem(linear_sys_number);
544 :
545 : // This is for postprocessing purposes in case none of the objects request the gradients.
546 : // TODO: Somehow collect information if the postprocessors need gradients and if nothing
547 : // needs this, just skip it
548 26077 : linear_sys.computeGradients();
549 : }
550 :
551 340071 : if (apply_fp_relax)
552 21100 : sys->clearFixedPointRelaxation();
553 341973 : }
554 :
555 : // Assess convergence of the multi-system fixed point iteration
556 335964 : if (!_using_multi_sys_fp_iterations)
557 309920 : converged = true;
558 : else
559 : {
560 26044 : _problem.execute(EXEC_MULTISYSTEM_FIXED_POINT_CONVERGENCE);
561 :
562 : const auto convergence_status =
563 26044 : _multi_sys_fp_convergence->checkConvergence(num_fp_multisys_iters);
564 26044 : converged = convergence_status == Convergence::MooseConvergenceStatus::CONVERGED;
565 26044 : if (convergence_status == Convergence::MooseConvergenceStatus::DIVERGED)
566 0 : break;
567 : }
568 335964 : num_fp_multisys_iters++;
569 : }
570 :
571 310694 : if (grid_step != _num_grid_steps)
572 50 : _problem.uniformRefine();
573 : }
574 :
575 310644 : return converged;
576 : }
|