https://mooseframework.inl.gov
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 
16 using namespace libMesh;
17 
19  FEProblemBase & fe_problem,
20  const std::string & name,
21  Moose::VarKindType var_kind)
22  : SystemBase(subproblem, fe_problem, name, var_kind),
23  _current_solution(nullptr),
24  _pc_side(Moose::PCS_DEFAULT),
25  _ksp_norm(Moose::KSPN_UNPRECONDITIONED)
26 {
27 }
28 
29 SolverSystem::~SolverSystem() = default;
30 
31 void
33 {
35 
37 
38  if (_serialized_solution.get())
39  _serialized_solution->init(system().n_dofs(), false, SERIAL);
40 }
41 
42 void
44 {
45  // call parent
47  // and update _current_solution
49 }
50 
51 void
53 {
54  if (_serialized_solution.get())
55  {
56  if (!_serialized_solution->initialized() || _serialized_solution->size() != system().n_dofs())
57  {
58  _serialized_solution->clear();
59  _serialized_solution->init(system().n_dofs(), false, SERIAL);
60  }
61 
63  }
64 }
65 
66 void
68 {
69  _current_solution = &soln;
70 
72  associateVectorToTag(const_cast<NumericVector<Number> &>(soln), tag);
73 
74  if (_serialized_solution.get())
76 }
77 
78 void
79 SolverSystem::setFixedPointRelaxationFactor(const Real relaxation_factor)
80 {
81  _fixed_point_relaxation_factor = relaxation_factor;
82 }
83 
84 void
86 {
88 }
89 
90 void
92 {
93  if (MooseUtils::absoluteFuzzyEqual(_fixed_point_relaxation_factor, 1.0))
94  return;
95 
98 
99  // Just in case checking if someone already allocated one which does not match
101  solution().type(),
102  "Fixed point relaxation requires the previous fixed point solution state to have "
103  "the same parallel type as the system solution.");
104 
106 }
107 
108 void
110 {
111  if (MooseUtils::absoluteFuzzyEqual(_fixed_point_relaxation_factor, 1.0))
112  return;
113 
115  "Fixed point relaxation was requested but the old fixed point solution was not "
116  "saved.");
117 
118  // This might be paranoid but who knows, maybe someone requests nonghosted
120  solution().type(),
121  "Fixed point relaxation requires the previous fixed point solution state to have "
122  "the same parallel type as the system solution.");
123 
124  auto & sol = solution();
126  sol.add(1.0 - _fixed_point_relaxation_factor,
128  sol.close();
129  update();
130 }
131 
132 void
134 {
135  if (pcs == "left")
137  else if (pcs == "right")
139  else if (pcs == "symmetric")
141  else if (pcs == "default")
143  else
144  mooseError("Unknown PC side specified.");
145 }
146 
147 void
149 {
150  if (kspnorm == "none")
152  else if (kspnorm == "preconditioned")
154  else if (kspnorm == "unpreconditioned")
156  else if (kspnorm == "natural")
158  else if (kspnorm == "default")
160  else
161  mooseError("Unknown ksp norm type specified.");
162 }
163 
164 void
166 {
167  auto & solution_invalidity = _app.solutionInvalidity();
168 
169  // sync all solution invalid counts to rank 0 process
170  solution_invalidity.syncIteration();
171 
172  if (solution_invalidity.hasInvalidSolution())
173  {
176  solution_invalidity.print(_console);
177  else
178  mooseWarning("The Solution Invalidity warnings are detected but silenced! "
179  "Use Problem/show_invalid_solution_console=true to show solution counts");
180  else
181  // output the occurrence of solution invalid in a summary table
183  solution_invalidity.print(_console);
184  }
185 }
186 
187 void
189 {
190  // Let's try not to overcompute
191  bool compute_tds = false;
192  if (type == EXEC_LINEAR)
193  compute_tds = true;
194  else if (type == EXEC_NONLINEAR)
195  {
197  compute_tds = true;
198  }
199  else if ((type == EXEC_TIMESTEP_END) || (type == EXEC_FINAL))
200  {
202  // We likely don't have a final residual evaluation upon which we compute the time derivatives
203  // so we need to do so now
204  compute_tds = true;
205  }
206 
207  // avoid division by dt which might be zero.
208  if (compute_tds && _fe_problem.dt() > 0.)
209  for (auto & ti : _time_integrators)
210  {
211  // Do things like compute integration weights
212  ti->preStep();
213  ti->computeTimeDerivatives();
214  }
215 }
std::string name(const ElemQuality q)
std::vector< std::shared_ptr< TimeIntegrator > > _time_integrators
Time integrator.
Definition: SystemBase.h:1049
virtual TagID getVectorTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition: SubProblem.C:204
SolverSystem(SubProblem &subproblem, FEProblemBase &fe_problem, const std::string &name, Moose::VarKindType var_kind)
Definition: SolverSystem.C:18
Moose::PCSideType _pc_side
Preconditioning side.
Definition: SolverSystem.h:123
void checkInvalidSolution()
Definition: SolverSystem.C:165
NumericVector< Number > & solution()
Definition: SystemBase.h:197
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
virtual ~SolverSystem()
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:345
virtual void associateVectorToTag(NumericVector< Number > &vec, TagID tag)
Associate a vector for a given tag.
Definition: SystemBase.C:982
void serializeSolution()
Definition: SolverSystem.C:52
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
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).
Definition: SystemBase.C:1433
void setSolution(const NumericVector< Number > &soln)
Set the solution to a given vector.
Definition: SolverSystem.C:67
Solving a linear problem.
Definition: MooseTypes.h:897
void setPCSide(MooseEnum pcs)
Set the side on which the preconditioner is applied to.
Definition: SolverSystem.C:133
const ExecFlagType EXEC_TIMESTEP_END
Definition: Moose.C:36
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
void computingScalingJacobian(bool computing_scaling_jacobian)
Setter for whether we&#39;re computing the scaling jacobian.
Base class for a system (of equations)
Definition: SystemBase.h:85
std::unique_ptr< NumericVector< Number > > _serialized_solution
Serialized version of the solution vector, or nullptr if a serialized solution is not needed...
Definition: SystemBase.h:1068
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
dof_id_type n_dofs() const
virtual bool matrixFromColoring() const
Whether a system matrix is formed from coloring.
Definition: SolverSystem.h:117
void update()
Update the system (doing libMesh magic)
Definition: SystemBase.C:1244
void syncIteration()
Sync iteration counts to main processor Sum across all processors.
Use whatever we have in PETSc.
Definition: MooseTypes.h:873
SERIAL
Use whatever we have in PETSc.
Definition: MooseTypes.h:885
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
Definition: MooseApp.h:184
VarKindType
Framework-wide stuff.
Definition: MooseTypes.h:763
SubProblem & _subproblem
The subproblem for whom this class holds variable data, etc; this can either be the governing finite ...
Definition: SystemBase.h:983
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:54
Moose::SolveType _type
Definition: SolverParams.h:19
void saveOldSolutionForFixedPointRelaxation()
Definition: SolverSystem.C:91
virtual void restoreSolutions() override final
Restore current solutions (call after your solve failed)
Definition: SolverSystem.C:43
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, etc).
Definition: SystemBase.h:1087
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1158
const ExecFlagType EXEC_LINEAR
Definition: Moose.C:31
virtual void needSolutionState(const unsigned int state, Moose::SolutionIterationType iteration_type=Moose::SolutionIterationType::Time, libMesh::ParallelType parallel_type=GHOSTED)
Registers that the solution state state is needed.
Definition: SystemBase.C:1452
bool showInvalidSolutionConsole() const
Whether or not to print out the invalid solutions summary table in console.
const NumericVector< Number > * _current_solution
solution vector from solver
Definition: SolverSystem.h:120
Real _fixed_point_relaxation_factor
Used for relaxing entire system solution during fixed point (multi-)system iterations.
Definition: SolverSystem.h:131
const ExecFlagType EXEC_NONLINEAR
Definition: Moose.C:33
MooseApp & _app
Definition: SystemBase.h:988
FEProblemBase & _fe_problem
the governing finite element/volume problem
Definition: SystemBase.h:986
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
bool acceptInvalidSolution() const
Whether or not to accept the solution based on its invalidity.
virtual void preInit()
This is called prior to the libMesh system has been init&#39;d.
Definition: SystemBase.h:157
virtual void compute(ExecFlagType type) override
Compute time derivatives, auxiliary variables, etc.
Definition: SolverSystem.C:188
void applyFixedPointRelaxation()
Definition: SolverSystem.C:109
libMesh::ParallelType solutionStateParallelType(const unsigned int state, const Moose::SolutionIterationType iteration_type) const
Returns the parallel type of the given solution state.
Definition: SystemBase.C:1442
virtual void preInit() override
This is called prior to the libMesh system has been init&#39;d.
Definition: SolverSystem.C:32
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
std::unique_ptr< NumericVector< Number > > current_local_solution
const TagName SOLUTION_TAG
Definition: MooseTypes.C:25
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.
Moose::MooseKSPNormType _ksp_norm
KSP norm type.
Definition: SolverSystem.h:125
virtual void restoreSolutions()
Restore current solutions (call after your solve failed)
Definition: SystemBase.C:1319
virtual Real & dt() const
const ExecFlagType EXEC_FINAL
Definition: Moose.C:46
void clearFixedPointRelaxation()
Definition: SolverSystem.C:85
void setMooseKSPNormType(MooseEnum kspnorm)
Set the norm in which the linear convergence will be measured.
Definition: SolverSystem.C:148
void setFixedPointRelaxationFactor(const Real relaxation_factor)
Enable solution under/over-relaxation for fixed point iterations.
Definition: SolverSystem.C:79
virtual void localize(std::vector< T > &v_local) const=0