www.mooseframework.org
PetscOutput.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 // MOOSE includes
11 #include "PetscOutput.h"
12 #include "FEProblem.h"
13 #include "NonlinearSystem.h"
14 
15 #include "libmesh/libmesh_common.h"
16 #include "libmesh/petsc_nonlinear_solver.h"
17 
18 template <>
21 {
23 
24 // Toggled for outputting nonlinear and linear residuals, only if we have PETSc
25 #ifdef LIBMESH_HAVE_PETSC
26  params.addParam<bool>(
27  "output_linear", false, "Specifies whether output occurs on each linear residual evaluation");
28  params.addParam<bool>("output_nonlinear",
29  false,
30  "Specifies whether output occurs on each nonlinear residual evaluation");
31 
32  // **** DEPRECATED PARAMETERS ****
33  params.addDeprecatedParam<bool>(
34  "linear_residuals",
35  false,
36  "Specifies whether output occurs on each linear residual evaluation",
37  "Please use 'output_linear' to get this behavior.");
38  params.addDeprecatedParam<bool>(
39  "nonlinear_residuals",
40  false,
41  "Specifies whether output occurs on each nonlinear residual evaluation",
42  "Please use 'output_nonlinear' to get this behavior.");
43  // Psuedo time step divisors
44  params.addParam<Real>(
45  "nonlinear_residual_dt_divisor",
46  1000,
47  "Number of divisions applied to time step when outputting non-linear residuals");
48  params.addParam<Real>(
49  "linear_residual_dt_divisor",
50  1000,
51  "Number of divisions applied to time step when outputting linear residuals");
52 
53  // Start times for residual output
54  params.addParam<Real>(
55  "linear_residual_start_time",
56  "Specifies a start time to begin output on each linear residual evaluation");
57  params.addParam<Real>(
58  "nonlinear_residual_start_time",
59  "Specifies a start time to begin output on each nonlinear residual evaluation");
60 
61  // End time for residual output
62  /* Note, No default is given here so that in Peacock giant numbers do not show up by default, the
63  * defaults are set in the initialization list */
64  params.addParam<Real>("linear_residual_end_time",
65  "Specifies an end time to begin output on each linear residual evaluation");
66  params.addParam<Real>(
67  "nonlinear_residual_end_time",
68  "Specifies an end time to begin output on each nonlinear residual evaluation");
69 
70  params.addParamNamesToGroup("linear_residuals nonlinear_residuals linear_residual_start_time "
71  "nonlinear_residual_start_time linear_residual_end_time "
72  "nonlinear_residual_end_time nonlinear_residual_dt_divisor "
73  "linear_residual_dt_divisor",
74  "PETSc");
75 #endif
76 
77  return params;
78 }
79 
81  : Output(parameters),
82  _nonlinear_iter(0),
83  _linear_iter(0),
84  _on_linear_residual(false),
85  _on_nonlinear_residual(false),
86  _nonlinear_dt_divisor(getParam<Real>("nonlinear_residual_dt_divisor")),
87  _linear_dt_divisor(getParam<Real>("linear_residual_dt_divisor")),
88  _nonlinear_start_time(-std::numeric_limits<Real>::max()),
89  _linear_start_time(-std::numeric_limits<Real>::max()),
90  _nonlinear_end_time(std::numeric_limits<Real>::max()),
91  _linear_end_time(std::numeric_limits<Real>::max())
92 {
93  // Output toggle support
94  if (getParam<bool>("output_linear"))
95  _execute_on.push_back("linear");
96  if (getParam<bool>("output_nonlinear"))
97  _execute_on.push_back("nonlinear");
98 
99  // **** DEPRECATED PARAMETER SUPPORT ****
100  if (getParam<bool>("linear_residuals"))
101  _execute_on.push_back("linear");
102  if (getParam<bool>("nonlinear_residuals"))
103  _execute_on.push_back("nonlinear");
104 
105  // Nonlinear residual start-time supplied by user
106  if (isParamValid("nonlinear_residual_start_time"))
107  {
108  _nonlinear_start_time = getParam<Real>("nonlinear_residual_start_time");
109  _execute_on.push_back("nonlinear");
110  }
111 
112  // Nonlinear residual end-time supplied by user
113  if (isParamValid("nonlinear_residual_end_time"))
114  _nonlinear_end_time = getParam<Real>("nonlinear_residual_end_time");
115 
116  // Linear residual start-time supplied by user
117  if (isParamValid("linear_residual_start_time"))
118  {
119  _linear_start_time = getParam<Real>("linear_residual_start_time");
120  _execute_on.push_back("linear");
121  }
122 
123  // Linear residual end-time supplied by user
124  if (isParamValid("linear_residual_end_time"))
125  _linear_end_time = getParam<Real>("linear_residual_end_time");
126 }
127 
128 void
130 {
131 // Only execute if PETSc exists
132 #ifdef LIBMESH_HAVE_PETSC
133 
134  // Extract the non-linear and linear solvers from PETSc
136  PetscNonlinearSolver<Number> * petsc_solver =
137  dynamic_cast<PetscNonlinearSolver<Number> *>(nl.nonlinearSolver());
138  SNES snes = petsc_solver->snes();
139  KSP ksp;
140  SNESGetKSP(snes, &ksp);
141 
142  // Update the pseudo times
143  _nonlinear_time = _time_old; // non-linear time starts with the previous time step
144  if (_dt != 0)
145  _nonlinear_dt = _dt / _nonlinear_dt_divisor; // set the pseudo non-linear timestep as fraction
146  // of real timestep for transient executioners
147  else
148  _nonlinear_dt = 1. / _nonlinear_dt_divisor; // set the pseudo non-linear timestep for steady
149  // executioners (here _dt==0)
150 
151  _linear_dt = _nonlinear_dt / _linear_dt_divisor; // set the pseudo linear timestep
152 
153  // Set the PETSc monitor functions
156  {
157  PetscErrorCode ierr = SNESMonitorSet(snes, petscNonlinearOutput, this, PETSC_NULL);
158  CHKERRABORT(_communicator.get(), ierr);
159  }
160 
163  {
164  PetscErrorCode ierr = KSPMonitorSet(ksp, petscLinearOutput, this, PETSC_NULL);
165  CHKERRABORT(_communicator.get(), ierr);
166  }
167 #endif
168 }
169 
170 // Only define the monitor functions if PETSc exists
171 #ifdef LIBMESH_HAVE_PETSC
172 PetscErrorCode
173 PetscOutput::petscNonlinearOutput(SNES, PetscInt its, PetscReal norm, void * void_ptr)
174 {
175  // Get the outputter object
176  PetscOutput * ptr = static_cast<PetscOutput *>(void_ptr);
177 
178  // Update the pseudo times
179  ptr->_nonlinear_time += ptr->_nonlinear_dt;
180  ptr->_linear_time = ptr->_nonlinear_time;
181 
182  // Set the current norm and iteration number
183  ptr->_norm = norm;
184  ptr->_nonlinear_iter = its;
185 
186  // Set the flag indicating that output is occurring on the non-linear residual
187  ptr->_on_nonlinear_residual = true;
188 
189  // Perform the output
191 
204 
205  // Reset the non-linear output flag and the simulation time
206  ptr->_on_nonlinear_residual = false;
207 
208  // Done
209  return 0;
210 }
211 
212 PetscErrorCode
213 PetscOutput::petscLinearOutput(KSP, PetscInt its, PetscReal norm, void * void_ptr)
214 {
215  // Get the Outputter object
216  PetscOutput * ptr = static_cast<PetscOutput *>(void_ptr);
217 
218  // Update the pseudo time
219  ptr->_linear_time += ptr->_linear_dt;
220 
221  // Set the current norm and iteration number
222  ptr->_norm = norm;
223  ptr->_linear_iter = its;
224 
225  // Set the flag indicating that output is occurring on the non-linear residual
226  ptr->_on_linear_residual = true;
227 
228  // Perform the output
229  ptr->outputStep(EXEC_LINEAR);
230 
243 
244  // Reset the linear output flag and the simulation time
245  ptr->_on_linear_residual = false;
246 
247  // Done
248  return 0;
249 }
250 #endif
251 
252 Real
254 {
256  return _nonlinear_time;
257  else if (_on_linear_residual)
258  return _linear_time;
259  else
260  return Output::time();
261 }
static PetscErrorCode petscLinearOutput(KSP, PetscInt its, PetscReal fnorm, void *void_ptr)
Performs the output onlinear iterations.
Definition: PetscOutput.C:213
Real & _time_old
The old time.
Definition: Output.h:191
ExecFlagEnum _execute_on
The common Execution types; this is used as the default execution type for everything except system i...
Definition: Output.h:185
void addDeprecatedParam(const std::string &name, const T &value, const std::string &doc_string, const std::string &deprecation_message)
InputParameters validParams< PetscOutput >()
Definition: PetscOutput.C:20
NonlinearSystemBase & getNonlinearSystemBase()
virtual NonlinearSolver< Number > * nonlinearSolver()=0
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:253
void solveSetup() override
Internal setup function that executes at the beginning of the time step.
Definition: PetscOutput.C:129
bool _on_linear_residual
True if current output calls is on the linear residual (used by time())
Definition: PetscOutput.h:88
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Real _nonlinear_dt
The pseuedo non-linear time step.
Definition: PetscOutput.h:79
Real _linear_time
Psuedo linear time.
Definition: PetscOutput.h:82
bool _on_nonlinear_residual
True if current output call is on the non-linear residual (used by time())
Definition: PetscOutput.h:91
Real _linear_start_time
Linear residual output start time.
Definition: PetscOutput.h:103
Real _nonlinear_start_time
Non-linear residual output start time.
Definition: PetscOutput.h:100
NonlinearSystemBase * nl
Nonlinear system to be solved.
Based class for output objects.
Definition: Output.h:41
Real _linear_end_time
Linear residual output end time.
Definition: PetscOutput.h:109
bool contains(const std::string &value) const
Contains methods for seeing if a value is in the MultiMooseEnum.
PetscInt _linear_iter
Current linear iteration returned from PETSc.
Definition: PetscOutput.h:51
virtual void outputStep(const ExecFlagType &type)
A single call to this function should output all the necessary data for a single timestep.
Definition: Output.C:165
Real _linear_dt
Psuedo linear time step.
Definition: PetscOutput.h:85
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:167
Real _nonlinear_time
The psuedo non-linear time.
Definition: PetscOutput.h:76
InputParameters validParams< Output >()
Definition: Output.C:29
const ExecFlagType EXEC_LINEAR
Real _norm
Current norm returned from PETSc.
Definition: PetscOutput.h:45
PetscOutput(const InputParameters &parameters)
Class constructor.
Definition: PetscOutput.C:80
virtual Real time()
Get the output time.
Definition: Output.C:223
const ExecFlagType EXEC_NONLINEAR
Real _nonlinear_dt_divisor
Pseudo non-linear timestep divisor.
Definition: PetscOutput.h:94
static PetscErrorCode petscNonlinearOutput(SNES, PetscInt its, PetscReal fnorm, void *void_ptr)
Performs the output on non-linear iterations.
Definition: PetscOutput.C:173
Real _t_tol
Time checking tolerance.
Definition: Output.h:224
void push_back(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
MooseApp & _app
The MooseApp this object is associated with.
Definition: MooseObject.h:177
Real _linear_dt_divisor
Pseudo linear timestep divisor.
Definition: PetscOutput.h:97
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an option parameter and a documentation string to the InputParameters object...
ierr
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:89
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:24
Real _nonlinear_end_time
Non-linear residual output end time.
Definition: PetscOutput.h:106
Real & _time
The current time for output purposes.
Definition: Output.h:188
Real & _dt
Time step delta.
Definition: Output.h:197
OutputWarehouse & getOutputWarehouse()
Get the OutputWarehouse objects.
Definition: MooseApp.C:1055
void flushConsoleBuffer()
If content exists in the buffer, write it.
PetscInt _nonlinear_iter
Current non-linear iteration returned from PETSc.
Definition: PetscOutput.h:48
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...