23 #include "libmesh/nonlinear_solver.h" 24 #include "libmesh/petsc_nonlinear_solver.h" 25 #include "libmesh/sparse_matrix.h" 26 #include "libmesh/petsc_matrix.h" 27 #include "libmesh/diagonal_matrix.h" 28 #include "libmesh/default_coupling.h" 35 NonlinearImplicitSystem & sys)
45 NonlinearImplicitSystem & sys)
62 NonlinearImplicitSystem & sys)
81 bool & changed_search_direction,
82 bool & changed_new_soln,
83 NonlinearImplicitSystem & sys)
88 sys, old_soln, search_direction, new_soln, changed_search_direction, changed_new_soln);
94 fe_problem, fe_problem.es().add_system<NonlinearImplicitSystem>(
name),
name),
95 _nl_implicit_sys(fe_problem.es().get_system<NonlinearImplicitSystem>(
name)),
96 _nl_residual_functor(_fe_problem),
97 _fd_residual_functor(_fe_problem),
98 _resid_and_jac_functor(_fe_problem),
99 _use_coloring_finite_difference(false),
100 _solution_is_invalid(false)
109 PetscNonlinearSolver<Real> * petsc_solver =
110 static_cast<PetscNonlinearSolver<Real> *
>(
_nl_implicit_sys.nonlinear_solver.get());
113 petsc_solver->set_residual_zero_out(
false);
114 petsc_solver->set_jacobian_zero_out(
false);
115 petsc_solver->use_default_monitor(
false);
144 TIME_SECTION(
"nlInitialResidual", 3,
"Computing Initial Residual");
152 _console <<
"Initial residual before setting preset BCs: " 156 const bool presolve_succeeded =
preSolve();
157 if (!presolve_succeeded)
166 PetscNonlinearSolver<Real> & solver =
167 static_cast<PetscNonlinearSolver<Real> &
>(*
_nl_implicit_sys.nonlinear_solver);
203 mooseWarning(
"The Solution Invalidity warnings are detected but silenced! " 204 "Use Problem/allow_invalid_solution=false to activate ");
217 PetscNonlinearSolver<Real> & solver =
218 static_cast<PetscNonlinearSolver<Real> &
>(*
sys().nonlinear_solver);
222 SNESSetFunctionDomainError(solver.snes());
233 SNESSetJacobianDomainError(solver.snes());
241 std::shared_ptr<FiniteDifferencePreconditioner> fdp =
244 mooseError(
"Did not setup finite difference preconditioner, and please add a preconditioning " 245 "block with type = fdp");
247 if (fdp->finiteDifferenceType() ==
"coloring")
253 else if (fdp->finiteDifferenceType() ==
"standard")
271 PetscMatrix<Number> * petsc_mat =
274 SNESSetJacobian(petsc_nonlinear_solver->
snes(),
277 SNESComputeJacobianDefault,
291 PetscMatrix<Number> * petsc_mat =
297 mooseError(
"Could not convert to Petsc matrix.");
301 PetscErrorCode
ierr = 0;
302 ISColoring iscoloring;
305 MatColoring matcoloring;
306 ierr = MatColoringCreate(petsc_mat->mat(), &matcoloring);
308 ierr = MatColoringSetType(matcoloring, MATCOLORINGLF);
310 ierr = MatColoringSetFromOptions(matcoloring);
312 ierr = MatColoringApply(matcoloring, &iscoloring);
314 ierr = MatColoringDestroy(&matcoloring);
317 MatFDColoringCreate(petsc_mat->mat(), iscoloring, &
_fdcoloring);
321 (PetscErrorCode(*)(
void))(
void (*)(
void)) &
323 &petsc_nonlinear_solver);
325 MatFDColoringSetUp(petsc_mat->mat(), iscoloring,
_fdcoloring);
326 SNESSetJacobian(petsc_nonlinear_solver.
snes(),
329 SNESComputeJacobianDefaultColor,
332 ISColoringDestroy(&iscoloring);
342 mooseWarning(
"The solution is not converged due to the solution being invalid.");
373 return petsc_solver->
snes();
375 mooseError(
"It is not a petsc nonlinear solver");
383 "Evaluting the residual and Jacobian together does not make sense for a JFNK solve type in " 384 "which only function evaluations are required, e.g. there is no need to form a matrix");
std::string name(const ElemQuality q)
void compute_jacobian(const NumericVector< Number > &soln, SparseMatrix< Number > &jacobian, NonlinearImplicitSystem &sys)
std::unique_ptr< DiagonalMatrix< Number > > _scaling_matrix
A diagonal matrix used for computing scaling.
NumericVector< Number > * _Re_time
residual vector for time contributions
void solutionInvalidAccumulationTimeStep()
Pass the number of solution invalid occurrences from current iteration to cumulative time iteration c...
virtual void computeJacobianSys(NonlinearImplicitSystem &sys, const NumericVector< Number > &soln, SparseMatrix< Number > &jacobian)
Form a Jacobian matrix.
virtual void solve() override
Solve the system (using libMesh magic)
bool _computing_initial_residual
SolverParams & solverParams()
Get the solver parameters.
NonlinearImplicitSystem::ComputeResidualandJacobian * residual_and_jacobian_object
PetscErrorCode libmesh_petsc_snes_fd_residual(SNES, Vec x, Vec r, void *ctx)
NumericVector< Number > * _Re_non_time
residual vector for non-time contributions
virtual void init() override
Initialize the system.
virtual void computeNullSpace(NonlinearImplicitSystem &sys, std::vector< NumericVector< Number > *> &sp)
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.
NonlinearImplicitSystem & _nl_implicit_sys
void resetSolutionInvalidTimeStep()
Reset the number of solution invalid occurrences back to zero for the current time step...
void attach_preconditioner(Preconditioner< Number > *preconditioner)
NonlinearSystem(FEProblemBase &problem, const std::string &name)
virtual void setupFiniteDifferencedPreconditioner() override
ComputeResidualFunctor _nl_residual_functor
virtual void computeBounds(NonlinearImplicitSystem &sys, NumericVector< Number > &lower, NumericVector< Number > &upper)
Solving a linear problem.
void compute_bounds(NumericVector< Number > &lower, NumericVector< Number > &upper, NonlinearImplicitSystem &sys)
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.
const Parallel::Communicator & _communicator
void residualAndJacobianTogether() override
Call this method if you want the residual and Jacobian to be computed simultaneously.
NonlinearImplicitSystem::ComputeResidual * residual_object
void init() override
Initialize the system.
bool hasDampers()
Whether or not this system has dampers.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
void(* jacobian)(const NumericVector< Number > &X, SparseMatrix< Number > &J, sys_type &S)
Real _initial_residual_before_preset_bcs
virtual NonlinearSolver< Number > * nonlinearSolver() override
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)
Real _resid_vs_jac_scaling_param
The param that indicates the weighting of the residual vs the Jacobian in determining variable scalin...
FEProblemBase & _fe_problem
ComputeFDResidualFunctor _fd_residual_functor
virtual void attachPreconditioner(Preconditioner< Number > *preconditioner) override
Attach a customized preconditioner that requires physics knowledge.
virtual bool shouldUpdateSolution()
Check to see whether the problem should update the solution.
ComputeResidualAndJacobian _resid_and_jac_functor
bool _automatic_scaling
Whether to automatically scale the variables.
MatFDColoring _fdcoloring
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
std::shared_ptr< MoosePreconditioner > _preconditioner
Preconditioner.
Jacobian-Free Newton Krylov.
bool _solution_is_invalid
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(* transpose_nullspace)(std::vector< NumericVector< Number > *> &sp, sys_type &S)
void setupColoringFiniteDifferencedPreconditioner()
According to the nonzero pattern provided in the matrix, a graph is constructed.
bool allowInvalidSolution() const
Whether or not the invalid solutions are allowed.
virtual void computePostCheck(NonlinearImplicitSystem &sys, const NumericVector< Number > &old_soln, NumericVector< Number > &search_direction, NumericVector< Number > &new_soln, bool &changed_search_direction, bool &changed_new_soln)
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.
const ExecFlagType EXEC_LINEAR
std::string stringify(const T &t)
conversion to string
void(* nullspace)(std::vector< NumericVector< Number > *> &sp, sys_type &S)
virtual NumericVector< Number > & RHS() override
void(* nearnullspace)(std::vector< NumericVector< Number > *> &sp, sys_type &S)
virtual bool converged() override
Returns the convergence state.
bool solutionInvalid() const
Loop over all the tracked objects and determine whether solution invalid is detected.
virtual SNES getSNES() override
bool _compute_initial_residual_before_preset_bcs
const ExecFlagType EXEC_POSTCHECK
const ExecFlagType EXEC_NONLINEAR
Finite difference preconditioner.
Class for containing MooseEnum item information.
virtual System & system() override
Get the reference to the libMesh system.
void compute_nullspace(std::vector< NumericVector< Number > *> &sp, NonlinearImplicitSystem &sys)
std::shared_ptr< TimeIntegrator > _time_integrator
Time integrator.
const NumericVector< Number > * _current_solution
solution vector from nonlinear solver
void(* bounds)(NumericVector< Number > &XL, NumericVector< Number > &XU, sys_type &S)
bool getFailNextNonlinearConvergenceCheck() const
Whether it will skip further residual evaluations and fail the next nonlinear convergence check...
virtual NonlinearImplicitSystem & sys()
void print(const ConsoleStream &console) const
Print the summary table of Solution Invalid warnings.
const InputParameters & parameters() const
Get the parameters of the object.
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.
virtual void stopSolve(const ExecFlagType &exec_flag) override
Quit the current solve as soon as possible.
virtual void computeResidualSys(NonlinearImplicitSystem &sys, const NumericVector< Number > &soln, NumericVector< Number > &residual)
This function is called by Libmesh to form a residual.
virtual bool hasException()
Whether or not an exception has occurred.
virtual void computeTransposeNullSpace(NonlinearImplicitSystem &sys, std::vector< NumericVector< Number > *> &sp)
virtual void computeNearNullSpace(NonlinearImplicitSystem &sys, std::vector< NumericVector< Number > *> &sp)
virtual ~NonlinearSystem()