23 #include <petscsnes.h> 34 "Check the convergence of a problem with respect to a user-supplied reference solution." 35 " Replaces ReferenceResidualProblem, currently still used in conjunction with it.");
43 _norm_type_enum(getParam<
MooseEnum>(
"normalization_type")),
44 _accept_mult(getParam<
Real>(
"acceptable_multiplier")),
45 _accept_iters(getParam<unsigned
int>(
"acceptable_iterations")),
46 _residual_vector(nullptr),
47 _reference_vector(nullptr),
50 _unscale_the_residual(getParam<bool>(
"unscale_the_residual")),
55 "Reference residual problem does not currently support multiple nonlinear systems " 56 "in a single Convergence object. Multiple Convergence objects can be used, one for " 57 "each nonlinear system, via the 'solver_sys' parameter.");
59 const auto solver_sys = getParam<SolverSystemName>(
"solver_sys");
63 const auto residual_vector_tag_id =
78 "No `reference_vector` is provided, thus the Reference Residual convergence method will " 79 "revert to default tolerance checking. `reference_vector` will become a required parameter " 80 "on June 1st, 2027. If you are using `ReferenceResidualProblem`, either provide a " 81 "reference_vector or use a standard problem type (e.g., remove " 82 "Problem/type=ReferenceResidualProblem from your input file). If you are using " 83 "`ReferenceResidualConvergence`, either provide a reference_vector or utilize " 84 "`DefaultNonlinearConvergence` instead.");
107 mooseAssert(
false,
"This point should not be reached.");
110 paramError(
"reference_vector",
"If local norm is used, a reference_vector must be provided.");
121 const auto solver_sys = getParam<SolverSystemName>(
"solver_sys");
125 auto & s = nonlinear_sys.
system();
131 for (
const auto var_num :
make_range(s.n_vars()))
133 const auto n_soln_vars = nonlinear_sys.nVariables();
135 const auto converge_on = getParam<std::vector<NonlinearVariableName>>(
"converge_on");
136 if (!converge_on.empty())
139 for (std::size_t i = 0; i < n_soln_vars; ++i)
140 for (
const auto & c : converge_on)
150 unsigned int num_variables_in_groups = 0;
158 " is not grouped with other variables.");
162 unsigned int n_groups = n_soln_vars - num_variables_in_groups +
_group_variables.size();
172 std::set<std::string> check_duplicate;
177 if (check_duplicate.size() != num_variables_in_groups)
178 paramError(
"group_variables",
"A variable cannot be included in multiple groups.");
184 bool found_match =
false;
185 for (
const auto var_num :
make_range(s.n_vars()))
194 mooseError(
"Could not find solution variable '",
201 unsigned int ungroup_index = 0;
219 "You added variable '",
221 "' to a group but excluded it from the convergence check. This is not " 242 unsigned int num_scalar_vars = 0;
243 unsigned int num_field_vars = 0;
247 for (
const auto var_num :
make_range(s.n_vars()))
250 if (nonlinear_sys.isScalarVariable(
_soln_vars[var_num]))
257 if (num_scalar_vars > 0 && num_field_vars > 0)
259 "standard variables and scalar variables are grouped together in group ",
266 std::vector<NonlinearVariableName> names;
273 if (names.size() == 0)
274 mooseError(
"Internal error, something is wrong with variable grouping");
275 else if (names.size() == 1)
283 if (j != names.size() - 1)
297 auto & s = current_nl_sys.
system();
300 if (current_nl_sys.isScalarVariable(
_soln_vars[i]))
328 mooseAssert((*_residual_vector).size() == (*_reference_vector).size(),
329 "Sizes of nonlinear RHS and reference vector should be the same.");
330 mooseAssert((*_reference_vector).size(),
"Reference vector must be provided.");
334 auto div = (*_residual_vector).clone();
365 std::ostringstream
out;
371 out << std::setprecision(2) << std::scientific;
372 unsigned int var_space = 0;
382 out <<
" " << std::setw(var_space + 8) << std::right <<
_group_names[i] +
"-> res: " 388 out <<
" local res/ref: " 425 bool convergedRelative =
true;
434 return convergedRelative;
443 std::ostringstream & oss)
448 it, fnorm, ref_norm, rel_tol, abs_tol, oss);
452 oss <<
"Converged normally";
459 oss <<
" Converged due a larger acceptable tolerance due to `acceptible_multiplier` after " 460 "`acceptible_iterations`.";
461 _console <<
" Converged due to ACCEPTABLE tolerances" << std::endl;
virtual TagID getVectorTagID(const TagName &tag_name) const
Get a TagID from a TagName.
virtual void nonlinearConvergenceSetup() override
Performs setup necessary for each call to checkConvergence.
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
virtual bool checkResidualConvergence(const unsigned int it, const Real fnorm, const Real ref_norm, const Real rel_tol, const Real abs_tol, std::ostringstream &oss) override
Check the absolute and relative convergence of the nonlinear solution.
const std::string & _name
The name of this class.
Interface class shared between ReferenceResidualProblem and ReferenceResidualConvergence.
PetscReal _abs_tol
Nonlinear absolute tolerance.
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
void paramError(const std::string ¶m, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
virtual std::size_t numNonlinearSystems() const override
std::vector< NonlinearVariableName > _group_names
std::vector< NonlinearVariableName > _soln_var_names
std::vector< std::vector< NonlinearVariableName > > _group_variables
Name of variables that are grouped together to check convergence.
const unsigned int _accept_iters
ReferenceResidualConvergence(const InputParameters ¶meters)
const MooseEnum _norm_type_enum
Enum holding the normalization type.
const InputParameters & parameters() const
Get the parameters of the object.
std::vector< bool > _converge_on_var
Flag for each solution variable or group being in 'converge_on'.
virtual std::unique_ptr< NumericVector< Number > > clone() const =0
const NumericVector< Number > * _reference_vector
The vector storing the reference residual values.
std::vector< Real > _group_resid
void updateReferenceResidual()
Computes the reference residuals for each group.
static InputParameters validParams()
static InputParameters validParams()
registerMooseObject("MooseApp", ReferenceResidualConvergence)
bool _use_group_variables
True if any variables are grouped.
std::vector< unsigned int > _soln_vars
TagID _reference_vector_tag_id
The reference vector tag id.
NonlinearSystemBase & currentNonlinearSystem()
const bool _unscale_the_residual
Bool to unscale the residual before convergence checks and screen output.
void mooseDeprecated(Args &&... args) const
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
bool _local_norm
Flag to optionally perform normalization of residual by reference residual before or after L2 norm is...
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
bool checkConvergenceIndividVars(const Real fnorm, const Real abs_tol, const Real rel_tol, const Real initial_residual_before_preset_bcs)
Check the convergence by comparing the norm of each variable's residual separately against its refere...
std::vector< Real > _group_ref_resid
virtual void initialSetup() override
Gets called at the beginning of the simulation before this object is asked to do its job...
Uses a reference residual to define relative convergence criteria.
const NumericVector< Number > * _residual_vector
The optional vector storing the reference residual values.
virtual NumericVector< Number > & RHS()=0
libMesh::FEMNormType _norm_type
Container for normalization type.
static InputParameters validParams()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
unsigned int solverSysNum(const SolverSystemName &solver_sys_name) const override
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template * sqrt(_arg)) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(tanh
enum ReferenceResidualConvergence::ZeroReferenceType _zero_ref_type
const TagID INVALID_TAG_ID
bool globCompare(const std::string &candidate, const std::string &pattern, std::size_t c, std::size_t p)
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
std::vector< unsigned int > _group_index
Group number index for each variable.
std::vector< bool > _is_var_grouped
Vector of bools to signify if variable is in a group.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
std::vector< Real > _scaling_factors
Local storage for the scaling factors applied to each of the variables to apply to _ref_resid_vars...
void paramWarning(const std::string ¶m, Args... args) const
virtual bool checkResidualConvergence(const unsigned int iter, const Real fnorm, const Real ref_norm, const Real rel_tol, const Real abs_tol, std::ostringstream &oss)
Check the absolute and relative convergence of the nonlinear solution.
auto min(const L &left, const R &right)
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector by name.
FEProblemBase & _fe_problem
bool isParamSetByUser(const std::string &name) const
Test if the supplied parameter is set by a user, as opposed to not set or set to default.
Default nonlinear convergence criteria for FEProblem.
std::vector< bool > _converge_on_group
void ErrorVector unsigned int
auto index_range(const T &sizable)
CTSub CT_OPERATOR_BINARY CTMul CTCompareLess CTCompareGreater CTCompareEqual _arg template pow< 2 >(tan(_arg))+1.0) *_arg.template D< dtag >()) CT_SIMPLE_UNARY_FUNCTION(sqrt
PetscReal _rel_tol
Nonlinear relative tolerance.
ZeroReferenceType
Container for convergence treatment when the reference residual is zero.
virtual libMesh::System & system() override
Get the reference to the libMesh system.