25 #include "libmesh/nonlinear_solver.h" 26 #include "libmesh/petsc_nonlinear_solver.h" 27 #include "libmesh/sparse_matrix.h" 28 #include "libmesh/petsc_matrix.h" 29 #include "libmesh/diagonal_matrix.h" 30 #include "libmesh/default_coupling.h" 31 #include "libmesh/petsc_solver_exception.h" 86 bool & changed_search_direction,
87 bool & changed_new_soln,
93 sys, old_soln, search_direction, new_soln, changed_search_direction, changed_new_soln);
101 _nl_residual_functor(_fe_problem),
102 _fd_residual_functor(_fe_problem),
103 _resid_and_jac_functor(_fe_problem),
104 _use_coloring_finite_difference(false)
158 TIME_SECTION(
"nlPreSMOResidual", 3,
"Computing Pre-SMO Residual");
167 <<
" (Before preset BCs, predictors, correctors, and constraints)\n";
171 const bool presolve_succeeded =
preSolve();
172 if (!presolve_succeeded)
179 [](
auto & ti) {
return ti->overridesSolve(); });
180 if (time_integrator_solve)
182 "If solve is overridden, then there must be only one time integrator");
184 if (time_integrator_solve)
191 if (!ti->overridesSolve())
192 ti->setNumIterationsLastSolve();
217 LibmeshPetscCall(MatFDColoringDestroy(&
_fdcoloring));
222 const std::set<TagID> & vector_tags_to_close)
229 LibmeshPetscCall(SNESSetFunctionDomainError(solver.
snes()));
236 LibmeshPetscCall(SNESSetJacobianDomainError(solver.
snes()));
244 std::shared_ptr<FiniteDifferencePreconditioner> fdp =
247 mooseError(
"Did not setup finite difference preconditioner, and please add a preconditioning " 248 "block with type = fdp");
250 if (fdp->finiteDifferenceType() ==
"coloring")
256 else if (fdp->finiteDifferenceType() ==
"standard")
277 LibmeshPetscCall(SNESSetJacobian(petsc_nonlinear_solver->
snes(),
280 SNESComputeJacobianDefault,
300 mooseError(
"Could not convert to Petsc matrix.");
304 ISColoring iscoloring;
307 MatColoring matcoloring;
308 LibmeshPetscCallA(
_communicator.
get(), MatColoringCreate(petsc_mat->mat(), &matcoloring));
309 LibmeshPetscCallA(
_communicator.
get(), MatColoringSetType(matcoloring, MATCOLORINGLF));
310 LibmeshPetscCallA(
_communicator.
get(), MatColoringSetFromOptions(matcoloring));
311 LibmeshPetscCallA(
_communicator.
get(), MatColoringApply(matcoloring, &iscoloring));
315 MatFDColoringCreate(petsc_mat->mat(), iscoloring, &
_fdcoloring));
319 (PetscErrorCode(*)(
void))(
void (*)(
void)) &
321 &petsc_nonlinear_solver));
324 MatFDColoringSetUp(petsc_mat->mat(), iscoloring,
_fdcoloring));
326 SNESSetJacobian(petsc_nonlinear_solver.
snes(),
329 SNESComputeJacobianDefaultColor,
342 mooseWarning(
"The solution is not converged due to the solution being invalid.");
374 const char * snes_prefix =
nullptr;
375 std::string snes_prefix_str;
379 snes_prefix = snes_prefix_str.c_str();
381 return petsc_solver->
snes(snes_prefix);
384 mooseError(
"It is not a petsc nonlinear solver");
392 "Evaluting the residual and Jacobian together does not make sense for a JFNK solve type in " 393 "which only function evaluations are required, e.g. there is no need to form a matrix");
std::string name(const ElemQuality q)
std::vector< std::shared_ptr< TimeIntegrator > > _time_integrators
Time integrator.
void compute_jacobian(const NumericVector< Number > &soln, SparseMatrix< Number > &jacobian, NonlinearImplicitSystem &sys)
SNES snes(const char *name=nullptr)
virtual libMesh::NonlinearSolver< Number > * nonlinearSolver() override
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
virtual void solve() override
Solve the system (using libMesh magic)
void solutionInvalidAccumulationTimeStep(const unsigned int timestep_index)
Pass the number of solution invalid occurrences from current iteration to cumulative time iteration c...
PetscErrorCode libmesh_petsc_snes_fd_residual(SNES, Vec x, Vec r, void *ctx)
libMesh::NonlinearImplicitSystem & _nl_implicit_sys
void use_default_monitor(bool state)
void checkInvalidSolution()
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
unsigned int _n_linear_iters
void compute_nearnullspace(std::vector< NumericVector< Number > *> &sp, NonlinearImplicitSystem &sys)
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
const EquationSystems & get_equation_systems() const
void resetSolutionInvalidTimeStep()
Reset the number of solution invalid occurrences back to zero for the current time step...
NonlinearSystem(FEProblemBase &problem, const std::string &name)
virtual void setupFiniteDifferencedPreconditioner() override
NumericVector< Number > * rhs
ComputeResidualFunctor _nl_residual_functor
void compute_bounds(NumericVector< Number > &lower, NumericVector< Number > &upper, NonlinearImplicitSystem &sys)
virtual void computeNearNullSpace(libMesh::NonlinearImplicitSystem &sys, std::vector< NumericVector< libMesh::Number > *> &sp)
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
unsigned int n_nonlinear_iterations() const
std::unique_ptr< libMesh::DiagonalMatrix< Number > > _scaling_matrix
A diagonal matrix used for computing scaling.
const Parallel::Communicator & _communicator
virtual void residualAndJacobianTogether() override
Call this method if you want the residual and Jacobian to be computed simultaneously.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Real _pre_smo_residual
The pre-SMO residual, see setPreSMOResidual for a detailed explanation.
bool hasDampers()
Whether or not this system has dampers.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void computeJacobianSys(libMesh::NonlinearImplicitSystem &sys, const NumericVector< libMesh::Number > &soln, libMesh::SparseMatrix< libMesh::Number > &jacobian)
Form a Jacobian matrix.
auto max(const L &left, const R &right)
Nonlinear system to be solved.
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)
virtual void stopSolve(const ExecFlagType &exec_flag, const std::set< TagID > &vector_tags_to_close) override
Quit the current solve as soon as possible.
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.
const T & get(std::string_view) const
Real final_nonlinear_residual() const
virtual Real l2_norm() const=0
ComputeFDResidualFunctor _fd_residual_functor
virtual bool shouldUpdateSolution()
Check to see whether the problem should update the solution.
std::string prefix() const
ComputeResidualAndJacobian _resid_and_jac_functor
MatFDColoring _fdcoloring
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
std::shared_ptr< MoosePreconditioner > _preconditioner
Preconditioner.
Jacobian-Free Newton Krylov.
bool _use_finite_differenced_preconditioner
Whether or not to use a finite differenced preconditioner.
void needsPreviousNewtonIteration(bool state)
Set a flag that indicated that user required values for the previous Newton iterate.
void setupColoringFiniteDifferencedPreconditioner()
According to the nonzero pattern provided in the matrix, a graph is constructed.
bool shouldEvaluatePreSMOResidual() const
We offer the option to check convergence against the pre-SMO residual.
virtual void computeNullSpace(libMesh::NonlinearImplicitSystem &sys, std::vector< NumericVector< libMesh::Number > *> &sp)
void compute_transpose_nullspace(std::vector< NumericVector< Number > *> &sp, NonlinearImplicitSystem &sys)
bool _use_coloring_finite_difference
void computeScalingResidual() override
Compute a "residual" for automatic scaling purposes.
bool _computing_pre_smo_residual
virtual libMesh::NonlinearImplicitSystem & sys()
unsigned int number() const
Gets the number of this system.
const ExecFlagType EXEC_LINEAR
std::string stringify(const T &t)
conversion to string
void closeTaggedVectors(const std::set< TagID > &tags)
Close all vectors for given tags.
virtual NumericVector< Number > & RHS() override
virtual int & timeStep() const
virtual bool converged() override
Returns the convergence state.
const NumericVector< Number > * _current_solution
solution vector from solver
void set_residual_zero_out(bool state)
void set_jacobian_zero_out(bool state)
virtual SNES getSNES() override
const ExecFlagType EXEC_POSTCHECK
const ExecFlagType EXEC_NONLINEAR
FEProblemBase & _fe_problem
the governing finite element/volume problem
Finite difference preconditioner.
virtual void attachPreconditioner(libMesh::Preconditioner< Number > *preconditioner) override
Attach a customized preconditioner that requires physics knowledge.
virtual void potentiallySetupFiniteDifferencing() override
Create finite differencing contexts for assembly of the Jacobian and/or approximating the action of t...
Class for containing MooseEnum item information.
static std::string outputNorm(const Real &old_norm, const Real &norm, const unsigned int precision=6)
A helper function for outputting norms in color.
bool acceptInvalidSolution() const
Whether or not to accept the solution based on its invalidity.
void compute_nullspace(std::vector< NumericVector< Number > *> &sp, NonlinearImplicitSystem &sys)
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
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)
void set_snesmf_reuse_base(bool state)
bool getFailNextNonlinearConvergenceCheck() const
Whether it will skip further residual evaluations and fail the next nonlinear convergence check(s) ...
NonlinearImplicitSystem::ComputeResidual * mffd_residual_object
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
std::unique_ptr< NumericVector< Number > > current_local_solution
virtual void computeTransposeNullSpace(libMesh::NonlinearImplicitSystem &sys, std::vector< NumericVector< libMesh::Number > *> &sp)
void setupStandardFiniteDifferencedPreconditioner()
Form preconditioning matrix via a standard finite difference method column-by-column.
void computeScalingJacobian() override
Compute a "Jacobian" for automatic scaling purposes.
bool useSNESMFReuseBase()
Return a flag that indicates if we are reusing the vector base.
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.
bool preSolve()
Perform some steps to get ready for the solver.
void prefix_with_name(bool value)
virtual bool hasException()
Whether or not an exception has occurred.
const SparseMatrix< Number > & get_system_matrix() const
virtual ~NonlinearSystem()
virtual libMesh::System & system() override
Get the reference to the libMesh system.