https://mooseframework.inl.gov
Loading...
Searching...
No Matches
SolverSystem.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#include "SolverSystem.h"
11#include "SolutionInvalidity.h"
12#include "FEProblemBase.h"
13#include "TimeIntegrator.h"
14#include "MooseUtils.h"
15
17 FEProblemBase & fe_problem,
18 const std::string & name,
19 Moose::VarKindType var_kind)
20 : SystemBase(subproblem, fe_problem, name, var_kind),
21 _current_solution(nullptr),
22 _pc_side(Moose::PCS_DEFAULT),
23 _ksp_norm(Moose::KSPN_UNPRECONDITIONED)
24{
25}
26
28
29void
31{
33
35
36 if (_serialized_solution.get())
37 _serialized_solution->init(system().n_dofs(), false, SERIAL);
38}
39
40void
42{
43 // call parent
45 // and update _current_solution
47}
48
49void
51{
52 if (_serialized_solution.get())
53 {
54 if (!_serialized_solution->initialized() || _serialized_solution->size() != system().n_dofs())
55 {
56 _serialized_solution->clear();
57 _serialized_solution->init(system().n_dofs(), false, SERIAL);
58 }
59
61 }
62}
63
64void
65SolverSystem::setSolution(const NumericVector<Number> & soln)
66{
67 _current_solution = &soln;
68
70 associateVectorToTag(const_cast<NumericVector<Number> &>(soln), tag);
71
72 if (_serialized_solution.get())
74}
75
76void
77SolverSystem::applyFixedPointRelaxation(const Real relaxation_factor,
78 const Moose::SolutionIterationType iteration_type)
79{
80 if (MooseUtils::absoluteFuzzyEqual(relaxation_factor, 1.0))
81 return;
82
83 mooseAssert(hasSolutionState(1, iteration_type),
84 "Fixed point relaxation was requested but the old fixed point solution was not "
85 "saved.");
86
87 // This might be paranoid but who knows, maybe someone requests nonghosted
88 mooseAssert(solutionStateParallelType(1, iteration_type) == solution().type(),
89 "Fixed point relaxation requires the previous fixed point solution state to have "
90 "the same parallel type as the system solution.");
91
92 auto & sol = solution();
93 sol.scale(relaxation_factor);
94 sol.add(1.0 - relaxation_factor, solutionState(1, iteration_type));
95 sol.close();
96 update();
97}
98
99void
101{
102 if (pcs == "left")
104 else if (pcs == "right")
106 else if (pcs == "symmetric")
108 else if (pcs == "default")
110 else
111 mooseError("Unknown PC side specified.");
112}
113
114void
116{
117 if (kspnorm == "none")
119 else if (kspnorm == "preconditioned")
121 else if (kspnorm == "unpreconditioned")
123 else if (kspnorm == "natural")
125 else if (kspnorm == "default")
127 else
128 mooseError("Unknown ksp norm type specified.");
129}
130
131void
133{
134 auto & solution_invalidity = _app.solutionInvalidity();
135
136 // sync all solution invalid counts to rank 0 process
137 solution_invalidity.syncIteration();
138
139 if (solution_invalidity.hasInvalidSolution())
140 {
143 solution_invalidity.print(_console);
144 else
145 mooseWarning("The Solution Invalidity warnings are detected but silenced! "
146 "Use Problem/show_invalid_solution_console=true to show solution counts");
147 else
148 // output the occurrence of solution invalid in a summary table
150 solution_invalidity.print(_console);
151 }
152}
153
154void
156{
157 // Let's try not to overcompute
158 bool compute_tds = false;
159 if (type == EXEC_LINEAR)
160 compute_tds = true;
161 else if (type == EXEC_NONLINEAR)
162 {
164 compute_tds = true;
165 }
166 else if ((type == EXEC_TIMESTEP_END) || (type == EXEC_FINAL))
167 {
169 // We likely don't have a final residual evaluation upon which we compute the time derivatives
170 // so we need to do so now
171 compute_tds = true;
172 }
173
174 // avoid division by dt which might be zero.
175 if (compute_tds && _fe_problem.dt() > 0.)
176 for (auto & ti : _time_integrators)
177 {
178 // Do things like compute integration weights
179 ti->preStep();
180 ti->computeTimeDerivatives();
181 }
182}
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
const ExecFlagType EXEC_TIMESTEP_END
Definition Moose.C:37
const ExecFlagType EXEC_LINEAR
Definition Moose.C:32
const ExecFlagType EXEC_NONLINEAR
Definition Moose.C:34
const ExecFlagType EXEC_FINAL
Definition Moose.C:49
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
bool acceptInvalidSolution() const
Whether or not to accept the solution based on its invalidity.
void computingScalingJacobian(bool computing_scaling_jacobian)
Setter for whether we're computing the scaling jacobian.
virtual Real & dt() const
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
bool showInvalidSolutionConsole() const
Whether or not to print out the invalid solutions summary table in console.
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
Definition MooseApp.h:185
Class for containing MooseEnum item information.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
void syncIteration()
Sync iteration counts to main processor Sum across all processors.
void setPCSide(MooseEnum pcs)
Set the side on which the preconditioner is applied to.
void setSolution(const NumericVector< Number > &soln)
Set the solution to a given vector.
virtual void compute(ExecFlagType type) override
Compute time derivatives, auxiliary variables, etc.
virtual void preInit() override
This is called prior to the libMesh system has been init'd.
void checkInvalidSolution()
SolverSystem(SubProblem &subproblem, FEProblemBase &fe_problem, const std::string &name, Moose::VarKindType var_kind)
virtual bool matrixFromColoring() const
Whether a system matrix is formed from coloring.
const NumericVector< Number > * _current_solution
solution vector from solver
void serializeSolution()
void applyFixedPointRelaxation(const Real relaxation_factor, const Moose::SolutionIterationType iteration_type)
Apply solution under/over-relaxation for fixed point iterations.
virtual ~SolverSystem()
void setMooseKSPNormType(MooseEnum kspnorm)
Set the norm in which the linear convergence will be measured.
Moose::PCSideType _pc_side
Preconditioning side.
virtual void restoreSolutions() override final
Restore current solutions (call after your solve failed)
Moose::MooseKSPNormType _ksp_norm
KSP norm type.
Generic class for solving transient nonlinear problems.
Definition SubProblem.h:79
virtual TagID getVectorTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition SubProblem.C:202
Base class for a system (of equations)
Definition SystemBase.h:87
std::unique_ptr< NumericVector< Number > > _serialized_solution
Serialized version of the solution vector, or nullptr if a serialized solution is not needed.
MooseApp & _app
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.
virtual void associateVectorToTag(NumericVector< Number > &vec, TagID tag)
Associate a vector for a given tag.
Definition SystemBase.C:980
virtual void preInit()
This is called prior to the libMesh system has been init'd.
Definition SystemBase.h:157
virtual void restoreSolutions()
Restore current solutions (call after your solve failed)
libMesh::ParallelType solutionStateParallelType(const unsigned int state, const Moose::SolutionIterationType iteration_type) const
Returns the parallel type of the given solution state.
SubProblem & _subproblem
The subproblem for whom this class holds variable data, etc; this can either be the governing finite ...
virtual NumericVector< Number > & solutionState(const unsigned int state, Moose::SolutionIterationType iteration_type=Moose::SolutionIterationType::Time)
Get a state of the solution (0 = current, 1 = old, 2 = older, etc).
virtual bool hasSolutionState(const unsigned int state, Moose::SolutionIterationType iteration_type=Moose::SolutionIterationType::Time) const
Whether or not the system has the solution state (0 = current, 1 = old, 2 = older,...
NumericVector< Number > & solution()
Definition SystemBase.h:203
void update()
Update the system (doing libMesh magic)
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
virtual void localize(std::vector< T > &v_local) const=0
std::unique_ptr< NumericVector< Number > > current_local_solution
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
@ PCS_LEFT
Definition MooseTypes.h:875
@ PCS_DEFAULT
Use whatever we have in PETSc.
Definition MooseTypes.h:878
@ PCS_SYMMETRIC
Definition MooseTypes.h:877
@ PCS_RIGHT
Definition MooseTypes.h:876
@ ST_LINEAR
Solving a linear problem.
Definition MooseTypes.h:902
@ KSPN_NONE
Definition MooseTypes.h:886
@ KSPN_PRECONDITIONED
Definition MooseTypes.h:887
@ KSPN_UNPRECONDITIONED
Definition MooseTypes.h:888
@ KSPN_DEFAULT
Use whatever we have in PETSc.
Definition MooseTypes.h:890
@ KSPN_NATURAL
Definition MooseTypes.h:889
SolutionIterationType
Definition MooseTypes.h:270
VarKindType
Framework-wide stuff.
Definition MooseTypes.h:769
const TagName SOLUTION_TAG
Definition MooseTypes.C:25