https://mooseframework.inl.gov
PicardSolve.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 "PicardSolve.h"
11 
12 #include "Executioner.h"
13 #include "FEProblemBase.h"
14 #include "NonlinearSystem.h"
16 #include "Console.h"
17 
20 {
22  return params;
23 }
24 
26 
27 void
28 PicardSolve::allocateStorage(const bool primary)
29 {
30  if (!performingRelaxation(primary))
31  return;
32  findTransformedSystem(primary);
33 
34  const std::vector<PostprocessorName> * transformed_pps;
35  std::vector<std::vector<PostprocessorValue>> * transformed_pps_values;
36  if (primary)
37  {
38  if (_transformed_sys)
39  {
40  _old_tag_id =
43  }
44  transformed_pps = &_transformed_pps;
45  transformed_pps_values = &_transformed_pps_values;
46  }
47  else
48  {
49  if (_transformed_sys)
50  {
53  }
54 
55  transformed_pps = &_secondary_transformed_pps;
56  transformed_pps_values = &_secondary_transformed_pps_values;
57  }
58 
59  // Allocate storage for the previous postprocessor values
60  (*transformed_pps_values).resize((*transformed_pps).size());
61  for (const auto i : index_range(*transformed_pps))
62  (*transformed_pps_values)[i].resize(1);
63 }
64 
65 void
67 {
68  // Primary is copied back by _transformed_sys->copyPreviousFixedPointSolutions()
69  if (!performingRelaxation(primary) || primary)
70  return;
71 
72  // Check to make sure allocateStorage has been called
74  "allocateStorage has not been called with primary = " + Moose::stringify(primary));
75 
76  // Save variable previous values
79  transformed_old = solution;
80 }
81 
82 void
84 {
85  if (!performingRelaxation(primary))
86  return;
87 
88  const std::vector<PostprocessorName> * transformed_pps;
89  std::vector<std::vector<PostprocessorValue>> * transformed_pps_values;
90  if (primary)
91  {
92  transformed_pps = &_transformed_pps;
93  transformed_pps_values = &_transformed_pps_values;
94  }
95  else
96  {
97  transformed_pps = &_secondary_transformed_pps;
98  transformed_pps_values = &_secondary_transformed_pps_values;
99  }
100 
101  // Save postprocessor previous values
102  for (const auto i : index_range(*transformed_pps))
103  (*transformed_pps_values)[i][0] = getPostprocessorValueByName((*transformed_pps)[i]);
104 }
105 
106 bool
108 {
109  // unrelaxed Picard is the default update for fixed point iterations
110  // old values are required for relaxation
111  const auto fixed_point_it = primary ? _fixed_point_it : _main_fixed_point_it;
112  return performingRelaxation(primary) && fixed_point_it > 0;
113 }
114 
115 void
117 {
118  Real relaxation_factor;
119  const std::vector<PostprocessorName> * transformed_pps;
120  std::vector<std::vector<PostprocessorValue>> * transformed_pps_values;
121  if (primary)
122  {
123  relaxation_factor = _relax_factor;
124  transformed_pps = &_transformed_pps;
125  transformed_pps_values = &_transformed_pps_values;
126  }
127  else
128  {
129  relaxation_factor = _secondary_relaxation_factor;
130  transformed_pps = &_secondary_transformed_pps;
131  transformed_pps_values = &_secondary_transformed_pps_values;
132  }
133 
134  // Relax the postprocessors
135  for (size_t i = 0; i < (*transformed_pps).size(); i++)
136  {
137  // Get new postprocessor value
138  const Real current_value = getPostprocessorValueByName((*transformed_pps)[i]);
139  const Real old_value = (*transformed_pps_values)[i][0];
140 
141  // Compute and set relaxed value
142  Real new_value = current_value;
143  new_value = relaxation_factor * current_value + (1 - relaxation_factor) * old_value;
144  _problem.setPostprocessorValueByName((*transformed_pps)[i], new_value);
145  }
146 }
147 
148 void
149 PicardSolve::transformVariables(const std::set<dof_id_type> & transformed_dofs, const bool primary)
150 {
151  Real relaxation_factor;
152  TagID old_tag_id;
153  if (primary)
154  {
155  relaxation_factor = _relax_factor;
156  old_tag_id = _old_tag_id;
157  }
158  else
159  {
160  relaxation_factor = _secondary_relaxation_factor;
161  old_tag_id = _secondary_old_tag_id;
162  }
163 
165  NumericVector<Number> & transformed_old = _transformed_sys->getVector(old_tag_id);
166 
167  for (const auto & dof : transformed_dofs)
168  solution.set(dof,
169  (transformed_old(dof) * (1.0 - relaxation_factor)) +
170  (solution(dof) * relaxation_factor));
171 
172  solution.close();
174 }
175 
176 void
178  const std::vector<Real> & timestep_begin_norms,
179  const std::vector<Real> & timestep_end_norms) const
180 {
181  _console << "\n 0 Picard |R| = "
182  << Console::outputNorm(std::numeric_limits<Real>::max(), initial_norm) << '\n';
183 
184  Real max_norm_old = initial_norm;
185  for (unsigned int i = 0; i <= _fixed_point_it; ++i)
186  {
187  Real max_norm = std::max(timestep_begin_norms[i], timestep_end_norms[i]);
188  _console << std::setw(2) << i + 1
189  << " Picard |R| = " << Console::outputNorm(max_norm_old, max_norm) << '\n';
190  max_norm_old = max_norm;
191  }
192 
193  _console << std::endl;
194 }
std::vector< std::vector< PostprocessorValue > > _transformed_pps_values
Previous values of the relaxed postprocessors.
std::vector< std::vector< PostprocessorValue > > _secondary_transformed_pps_values
Previous values of the postprocessors relaxed outside of the fixed point iteration (used as a subapp)...
static InputParameters validParams()
Definition: PicardSolve.C:19
FEProblemBase & _problem
Reference to FEProblem.
Definition: SolveObject.h:47
virtual void savePostprocessorValues(const bool primary) override final
Saves the current values of the postprocessors, and update the old(er) vectors.
Definition: PicardSolve.C:83
virtual void transformPostprocessors(const bool primary) override final
Use the fixed point algorithm to transform the postprocessors.
Definition: PicardSolve.C:116
unsigned int TagID
Definition: MooseTypes.h:238
NumericVector< Number > & solution()
Definition: SystemBase.h:197
PARALLEL
void setPostprocessorValueByName(const PostprocessorName &name, const PostprocessorValue &value, std::size_t t_index=0)
Set the value of a PostprocessorValue.
virtual TagID addVectorTag(const TagName &tag_name, const Moose::VectorTagType type=Moose::VECTOR_TAG_RESIDUAL)
Create a Tag.
Definition: SubProblem.C:93
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const TagName PREVIOUS_FP_SOLUTION_TAG
Definition: MooseTypes.C:29
virtual void printFixedPointConvergenceHistory(Real initial_norm, const std::vector< Real > &timestep_begin_norms, const std::vector< Real > &timestep_end_norms) const override final
Print the convergence history of the coupling, at every fixed point iteration.
Definition: PicardSolve.C:177
std::vector< PostprocessorName > _secondary_transformed_pps
Postprocessors to be relaxed outside of fixed point iteration (used as a subapp)
auto max(const L &left, const R &right)
const std::vector< PostprocessorName > _transformed_pps
The postprocessors (transferred or not) that are going to be relaxed.
NumericVector< Number > & addVector(const std::string &vector_name, const bool project, const libMesh::ParallelType type)
Adds a solution length vector to the system.
virtual bool useFixedPointAlgorithmUpdateInsteadOfPicard(const bool primary) override final
Use the fixed point algorithm transform instead of simply using the Picard update.
Definition: PicardSolve.C:107
void update()
Update the system (doing libMesh magic)
Definition: SystemBase.C:1244
const Real _relax_factor
Relaxation factor for fixed point Iteration.
Executioners are objects that do the actual work of solving your problem.
Definition: Executioner.h:30
TagID _old_tag_id
Vector tag id for the previous solution variable, as a main app.
Definition: PicardSolve.h:88
Real _secondary_relaxation_factor
Relaxation factor outside of fixed point iteration (used as a subapp)
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
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
virtual void close()=0
virtual const PostprocessorValue & getPostprocessorValueByName(const PostprocessorName &name) const
Retrieve the value of the Postprocessor.
unsigned int _fixed_point_it
TagID _secondary_old_tag_id
Vector tag id for the previous solution variable, as a sub app.
Definition: PicardSolve.h:91
static InputParameters validParams()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static std::string outputNorm(const Real &old_norm, const Real &norm, const unsigned int precision=6)
A helper function for outputting norms in color.
Definition: Console.C:619
const TagID INVALID_TAG_ID
Definition: MooseTypes.C:23
SystemBase * _transformed_sys
System holding the transformed variables.
virtual void allocateStorage(const bool primary) override final
Allocate storage for the fixed point algorithm.
Definition: PicardSolve.C:28
virtual void set(const numeric_index_type i, const Number value)=0
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
bool performingRelaxation(const bool primary) const
Returns true if there is relaxation.
virtual void saveVariableValues(const bool primary) override final
Saves the current values of the variables, and update the old(er) vectors.
Definition: PicardSolve.C:66
PicardSolve(Executioner &ex)
Definition: PicardSolve.C:25
void findTransformedSystem(const bool primary)
Find the system holding the variables to be transformed (accelerated or relaxed)
virtual void transformVariables(const std::set< dof_id_type > &transformed_dofs, const bool primary) override final
Use the fixed point algorithm to transform the variables.
Definition: PicardSolve.C:149
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector by name.
Definition: SystemBase.C:934
auto index_range(const T &sizable)
unsigned int _main_fixed_point_it
fixed point iteration counter for the main app