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")),
56 "reference residual problem does not currently support multiple nonlinear systems");
60 const auto residual_vector_tag_id =
74 "No `reference_vector` is provided, thus the Reference Residual convergence method will " 75 "revert to default tolerance checking. `reference_vector` will become a required parameter " 76 "on June 1st, 2027. If you are using `ReferenceResidualProblem`, either provide a " 77 "reference_vector or use a standard problem type (e.g., remove " 78 "Problem/type=ReferenceResidualProblem from your input file). If you are using " 79 "`ReferenceResidualConvergence`, either provide a reference_vector or utilize " 80 "`DefaultNonlinearConvergence` instead.");
103 mooseAssert(
false,
"This point should not be reached.");
106 paramError(
"reference_vector",
"If local norm is used, a reference_vector must be provided.");
118 auto & s = nonlinear_sys.
system();
124 for (
const auto var_num :
make_range(s.n_vars()))
126 const auto n_soln_vars = nonlinear_sys.nVariables();
128 const auto converge_on = getParam<std::vector<NonlinearVariableName>>(
"converge_on");
129 if (!converge_on.empty())
132 for (std::size_t i = 0; i < n_soln_vars; ++i)
133 for (
const auto & c : converge_on)
143 unsigned int num_variables_in_groups = 0;
151 " is not grouped with other variables.");
155 unsigned int n_groups = n_soln_vars - num_variables_in_groups +
_group_variables.size();
165 std::set<std::string> check_duplicate;
170 if (check_duplicate.size() != num_variables_in_groups)
171 paramError(
"group_variables",
"A variable cannot be included in multiple groups.");
177 bool found_match =
false;
178 for (
const auto var_num :
make_range(s.n_vars()))
187 mooseError(
"Could not find solution variable '",
194 unsigned int ungroup_index = 0;
212 "You added variable '",
214 "' to a group but excluded it from the convergence check. This is not " 235 unsigned int num_scalar_vars = 0;
236 unsigned int num_field_vars = 0;
240 for (
const auto var_num :
make_range(s.n_vars()))
243 if (nonlinear_sys.isScalarVariable(
_soln_vars[var_num]))
250 if (num_scalar_vars > 0 && num_field_vars > 0)
252 "standard variables and scalar variables are grouped together in group ",
259 std::vector<NonlinearVariableName> names;
266 if (names.size() == 0)
267 mooseError(
"Internal error, something is wrong with variable grouping");
268 else if (names.size() == 1)
276 if (j != names.size() - 1)
290 auto & s = current_nl_sys.
system();
293 if (current_nl_sys.isScalarVariable(
_soln_vars[i]))
321 mooseAssert((*_residual_vector).size() == (*_reference_vector).size(),
322 "Sizes of nonlinear RHS and reference vector should be the same.");
323 mooseAssert((*_reference_vector).size(),
"Reference vector must be provided.");
327 auto div = (*_residual_vector).clone();
358 std::ostringstream
out;
364 out << std::setprecision(2) << std::scientific;
365 unsigned int var_space = 0;
375 out <<
" " << std::setw(var_space + 8) << std::right
385 out <<
" res/ref: " << std::setw(8)
412 bool convergedRelative =
true;
421 return convergedRelative;
430 std::ostringstream & oss)
435 it, fnorm, ref_norm, rel_tol, abs_tol, oss);
439 oss <<
"Converged normally";
446 oss <<
" Converged due a larger acceptable tolerance due to `acceptible_multiplier` after " 447 "`acceptible_iterations`.";
448 _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.
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
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
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
ZeroReferenceType
Container for convergence treatment when the reference residual is zero.
virtual libMesh::System & system() override
Get the reference to the libMesh system.