24#include "libmesh/nonlinear_solver.h"
25#include "libmesh/petsc_nonlinear_solver.h"
26#include "libmesh/sparse_matrix.h"
27#include "libmesh/petsc_matrix.h"
28#include "libmesh/diagonal_matrix.h"
29#include "libmesh/default_coupling.h"
30#include "libmesh/petsc_solver_exception.h"
85 bool & changed_search_direction,
86 bool & changed_new_soln,
92 sys, old_soln, search_direction, new_soln, changed_search_direction, changed_new_soln);
100 _nl_residual_functor(_fe_problem),
101 _fd_residual_functor(_fe_problem),
102 _resid_and_jac_functor(_fe_problem),
103 _use_coloring_finite_difference(false)
153 TIME_SECTION(
"nlPreSMOResidual", 3,
"Computing Pre-SMO Residual");
162 <<
" (Before preset BCs, predictors, correctors, and constraints)\n";
166 const bool presolve_succeeded =
preSolve();
167 if (!presolve_succeeded)
174 [](
auto & ti) { return ti->overridesSolve(); });
175 if (time_integrator_solve)
177 "If solve is overridden, then there must be only one time integrator");
179 if (time_integrator_solve)
186 if (!ti->overridesSolve())
187 ti->setNumIterationsLastSolve();
209 LibmeshPetscCall(MatFDColoringDestroy(&
_fdcoloring));
214 const std::set<TagID> & vector_tags_to_close)
221 LibmeshPetscCall(SNESSetFunctionDomainError(solver.
snes()));
228 LibmeshPetscCall(SNESSetJacobianDomainError(solver.
snes()));
236 std::shared_ptr<FiniteDifferencePreconditioner> fdp =
237 std::dynamic_pointer_cast<FiniteDifferencePreconditioner>(
_preconditioner);
239 mooseError(
"Did not setup finite difference preconditioner, and please add a preconditioning "
240 "block with type = fdp");
242 if (fdp->finiteDifferenceType() ==
"coloring")
248 else if (fdp->finiteDifferenceType() ==
"standard")
269 LibmeshPetscCall(SNESSetJacobian(petsc_nonlinear_solver->
snes(),
272 SNESComputeJacobianDefault,
292 mooseError(
"Could not convert to Petsc matrix.");
296 ISColoring iscoloring;
299 MatColoring matcoloring;
300 LibmeshPetscCallA(
_communicator.
get(), MatColoringCreate(petsc_mat->mat(), &matcoloring));
301 LibmeshPetscCallA(
_communicator.
get(), MatColoringSetType(matcoloring, MATCOLORINGLF));
302 LibmeshPetscCallA(
_communicator.
get(), MatColoringSetFromOptions(matcoloring));
303 LibmeshPetscCallA(
_communicator.
get(), MatColoringApply(matcoloring, &iscoloring));
307 MatFDColoringCreate(petsc_mat->mat(), iscoloring, &
_fdcoloring));
310#if PETSC_VERSION_LESS_THAN(3, 24, 0)
313 (PetscErrorCode(*)(
void))(
void (*)(
void))
315 &petsc_nonlinear_solver));
321 &petsc_nonlinear_solver));
325 MatFDColoringSetUp(petsc_mat->mat(), iscoloring,
_fdcoloring));
327 SNESSetJacobian(petsc_nonlinear_solver.
snes(),
330 SNESComputeJacobianDefaultColor,
347 mooseWarning(
"The solution is not converged due to the solution being invalid.");
379 const char * snes_prefix =
nullptr;
380 std::string snes_prefix_str;
384 snes_prefix = snes_prefix_str.c_str();
386 return petsc_solver->
snes(snes_prefix);
389 mooseError(
"It is not a petsc nonlinear solver");
397 "Evaluting the residual and Jacobian together does not make sense for a JFNK solve type in "
398 "which only function evaluations are required, e.g. there is no need to form a matrix");
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
const ExecFlagType EXEC_POSTCHECK
const ExecFlagType EXEC_LINEAR
const ExecFlagType EXEC_NONLINEAR
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
static std::string outputNorm(const Real &old_norm, const Real &norm, const unsigned int precision=6)
A helper function for outputting norms in color.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void computeNearNullSpace(libMesh::NonlinearImplicitSystem &sys, std::vector< NumericVector< libMesh::Number > * > &sp)
void needsPreviousNewtonIteration(bool state)
Set a flag that indicated that user required values for the previous Newton iterate.
virtual bool hasException()
Whether or not an exception has occurred.
virtual bool shouldUpdateSolution()
Check to see whether the problem should update the solution.
virtual void computeJacobianSys(libMesh::NonlinearImplicitSystem &sys, const NumericVector< libMesh::Number > &soln, libMesh::SparseMatrix< libMesh::Number > &jacobian)
Form a Jacobian matrix.
bool acceptInvalidSolution() const
Whether or not to accept the solution based on its invalidity.
bool getFailNextNonlinearConvergenceCheck() const
Whether it will skip further residual evaluations and fail the next nonlinear convergence check(s)
bool hasDampers()
Whether or not this system has dampers.
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
virtual void computeNullSpace(libMesh::NonlinearImplicitSystem &sys, std::vector< NumericVector< libMesh::Number > * > &sp)
virtual void computeBounds(libMesh::NonlinearImplicitSystem &sys, NumericVector< libMesh::Number > &lower, NumericVector< libMesh::Number > &upper)
virtual void computePostCheck(libMesh::NonlinearImplicitSystem &sys, const NumericVector< libMesh::Number > &old_soln, NumericVector< libMesh::Number > &search_direction, NumericVector< libMesh::Number > &new_soln, bool &changed_search_direction, bool &changed_new_soln)
bool useSNESMFReuseBase()
Return a flag that indicates if we are reusing the vector base.
virtual void computeTransposeNullSpace(libMesh::NonlinearImplicitSystem &sys, std::vector< NumericVector< libMesh::Number > * > &sp)
virtual void computeResidualSys(libMesh::NonlinearImplicitSystem &sys, const NumericVector< libMesh::Number > &soln, NumericVector< libMesh::Number > &residual)
This function is called by Libmesh to form a residual.
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
Class for containing MooseEnum item information.
Nonlinear system to be solved.
bool preSolve()
Perform some steps to get ready for the solver.
bool _computing_pre_smo_residual
MatFDColoring _fdcoloring
bool _use_finite_differenced_preconditioner
Whether or not to use a finite differenced preconditioner.
Real _pre_smo_residual
The pre-SMO residual, see setPreSMOResidual for a detailed explanation.
bool shouldEvaluatePreSMOResidual() const
We offer the option to check convergence against the pre-SMO residual.
unsigned int _n_linear_iters
std::unique_ptr< libMesh::DiagonalMatrix< Number > > _scaling_matrix
A diagonal matrix used for computing scaling.
virtual libMesh::System & system() override
Get the reference to the libMesh system.
std::shared_ptr< MoosePreconditioner > _preconditioner
Preconditioner.
virtual void potentiallySetupFiniteDifferencing() override
Create finite differencing contexts for assembly of the Jacobian and/or approximating the action of t...
void setupColoringFiniteDifferencedPreconditioner()
According to the nonzero pattern provided in the matrix, a graph is constructed.
ComputeResidualFunctor _nl_residual_functor
virtual libMesh::NonlinearImplicitSystem & sys()
virtual SNES getSNES() override
virtual ~NonlinearSystem()
void setupStandardFiniteDifferencedPreconditioner()
Form preconditioning matrix via a standard finite difference method column-by-column.
virtual void stopSolve(const ExecFlagType &exec_flag, const std::set< TagID > &vector_tags_to_close) override
Quit the current solve as soon as possible.
ComputeResidualAndJacobian _resid_and_jac_functor
virtual void setupFiniteDifferencedPreconditioner() override
ComputeFDResidualFunctor _fd_residual_functor
void computeScalingResidual() override
Compute a "residual" for automatic scaling purposes.
NonlinearSystem(FEProblemBase &problem, const std::string &name)
libMesh::NonlinearImplicitSystem & _nl_implicit_sys
virtual libMesh::NonlinearSolver< Number > * nonlinearSolver() override
virtual void attachPreconditioner(libMesh::Preconditioner< Number > *preconditioner) override
Attach a customized preconditioner that requires physics knowledge.
virtual void solve() override
Solve the system (using libMesh magic)
virtual void residualAndJacobianTogether() override
Call this method if you want the residual and Jacobian to be computed simultaneously.
virtual bool converged() override
Returns the convergence state.
void computeScalingJacobian() override
Compute a "Jacobian" for automatic scaling purposes.
bool _use_coloring_finite_difference
virtual NumericVector< Number > & RHS() override
bool hasSynced() const
Whether the solution invalidity has synchronized iteration counts across MPI processes.
void checkInvalidSolution()
const NumericVector< Number > * _current_solution
solution vector from solver
FEProblemBase & _fe_problem
the governing finite element/volume problem
std::vector< std::shared_ptr< TimeIntegrator > > _time_integrators
Time integrator.
unsigned int number() const
Gets the number of this system.
void closeTaggedVectors(const std::set< TagID > &tags)
Close all vectors for given tags.
NumericVector< Number > * rhs
const SparseMatrix< Number > & get_system_matrix() const
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
Real final_nonlinear_residual() const
unsigned int n_nonlinear_iterations() const
NonlinearImplicitSystem::ComputeResidual * mffd_residual_object
virtual Real l2_norm() const=0
const Parallel::Communicator & _communicator
const T & get(std::string_view) const
void set_residual_zero_out(bool state)
void set_snesmf_reuse_base(bool state)
SNES snes(const char *name=nullptr)
void set_jacobian_zero_out(bool state)
void use_default_monitor(bool state)
std::unique_ptr< NumericVector< Number > > current_local_solution
void prefix_with_name(bool value)
std::string prefix() const
const EquationSystems & get_equation_systems() const
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
void compute_nearnullspace(std::vector< NumericVector< Number > * > &sp, NonlinearImplicitSystem &sys)
@ ST_JFNK
Jacobian-Free Newton Krylov.
void compute_nullspace(std::vector< NumericVector< Number > * > &sp, NonlinearImplicitSystem &sys)
void compute_jacobian(const NumericVector< Number > &soln, SparseMatrix< Number > &jacobian, NonlinearImplicitSystem &sys)
std::string stringify(const T &t)
conversion to string
void compute_postcheck(const NumericVector< Number > &old_soln, NumericVector< Number > &search_direction, NumericVector< Number > &new_soln, bool &changed_search_direction, bool &changed_new_soln, NonlinearImplicitSystem &sys)
void compute_transpose_nullspace(std::vector< NumericVector< Number > * > &sp, NonlinearImplicitSystem &sys)
void compute_bounds(NumericVector< Number > &lower, NumericVector< Number > &upper, NonlinearImplicitSystem &sys)
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
PetscErrorCode libmesh_petsc_snes_fd_residual(SNES, Vec x, Vec r, void *ctx)