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