www.mooseframework.org
PetscOutput.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // MOOSE includes
16 #include "PetscOutput.h"
17 #include "FEProblem.h"
18 #include "NonlinearSystem.h"
19 
20 #include "libmesh/libmesh_common.h"
21 #include "libmesh/petsc_nonlinear_solver.h"
22 
23 template <>
26 {
28 
29 // Toggled for outputting nonlinear and linear residuals, only if we have PETSc
30 #ifdef LIBMESH_HAVE_PETSC
31  params.addParam<bool>(
32  "output_linear", false, "Specifies whether output occurs on each linear residual evaluation");
33  params.addParam<bool>("output_nonlinear",
34  false,
35  "Specifies whether output occurs on each nonlinear residual evaluation");
36 
37  // **** DEPRECATED PARAMETERS ****
38  params.addDeprecatedParam<bool>(
39  "linear_residuals",
40  false,
41  "Specifies whether output occurs on each linear residual evaluation",
42  "Please use 'output_linear' to get this behavior.");
43  params.addDeprecatedParam<bool>(
44  "nonlinear_residuals",
45  false,
46  "Specifies whether output occurs on each nonlinear residual evaluation",
47  "Please use 'output_nonlinear' to get this behavior.");
48  // Psuedo time step divisors
49  params.addParam<Real>(
50  "nonlinear_residual_dt_divisor",
51  1000,
52  "Number of divisions applied to time step when outputting non-linear residuals");
53  params.addParam<Real>(
54  "linear_residual_dt_divisor",
55  1000,
56  "Number of divisions applied to time step when outputting linear residuals");
57 
58  // Start times for residual output
59  params.addParam<Real>(
60  "linear_residual_start_time",
61  "Specifies a start time to begin output on each linear residual evaluation");
62  params.addParam<Real>(
63  "nonlinear_residual_start_time",
64  "Specifies a start time to begin output on each nonlinear residual evaluation");
65 
66  // End time for residual output
67  /* Note, No default is given here so that in Peacock giant numbers do not show up by default, the
68  * defaults are set in the initialization list */
69  params.addParam<Real>("linear_residual_end_time",
70  "Specifies an end time to begin output on each linear residual evaluation");
71  params.addParam<Real>(
72  "nonlinear_residual_end_time",
73  "Specifies an end time to begin output on each nonlinear residual evaluation");
74 
75  params.addParamNamesToGroup("linear_residuals nonlinear_residuals linear_residual_start_time "
76  "nonlinear_residual_start_time linear_residual_end_time "
77  "nonlinear_residual_end_time nonlinear_residual_dt_divisor "
78  "linear_residual_dt_divisor",
79  "PETSc");
80 #endif
81 
82  return params;
83 }
84 
86  : Output(parameters),
87  _nonlinear_iter(0),
88  _linear_iter(0),
89  _on_linear_residual(false),
90  _on_nonlinear_residual(false),
91  _nonlinear_dt_divisor(getParam<Real>("nonlinear_residual_dt_divisor")),
92  _linear_dt_divisor(getParam<Real>("linear_residual_dt_divisor")),
93  _nonlinear_start_time(-std::numeric_limits<Real>::max()),
94  _linear_start_time(-std::numeric_limits<Real>::max()),
95  _nonlinear_end_time(std::numeric_limits<Real>::max()),
96  _linear_end_time(std::numeric_limits<Real>::max())
97 {
98  // Output toggle support
99  if (getParam<bool>("output_linear"))
100  _execute_on.push_back("linear");
101  if (getParam<bool>("output_nonlinear"))
102  _execute_on.push_back("nonlinear");
103 
104  // **** DEPRECATED PARAMETER SUPPORT ****
105  if (getParam<bool>("linear_residuals"))
106  _execute_on.push_back("linear");
107  if (getParam<bool>("nonlinear_residuals"))
108  _execute_on.push_back("nonlinear");
109 
110  // Nonlinear residual start-time supplied by user
111  if (isParamValid("nonlinear_residual_start_time"))
112  {
113  _nonlinear_start_time = getParam<Real>("nonlinear_residual_start_time");
114  _execute_on.push_back("nonlinear");
115  }
116 
117  // Nonlinear residual end-time supplied by user
118  if (isParamValid("nonlinear_residual_end_time"))
119  _nonlinear_end_time = getParam<Real>("nonlinear_residual_end_time");
120 
121  // Linear residual start-time supplied by user
122  if (isParamValid("linear_residual_start_time"))
123  {
124  _linear_start_time = getParam<Real>("linear_residual_start_time");
125  _execute_on.push_back("linear");
126  }
127 
128  // Linear residual end-time supplied by user
129  if (isParamValid("linear_residual_end_time"))
130  _linear_end_time = getParam<Real>("linear_residual_end_time");
131 }
132 
133 void
135 {
136 // Only execute if PETSc exists
137 #ifdef LIBMESH_HAVE_PETSC
138 
139  // Extract the non-linear and linear solvers from PETSc
141  PetscNonlinearSolver<Number> * petsc_solver =
142  dynamic_cast<PetscNonlinearSolver<Number> *>(nl.nonlinearSolver());
143  SNES snes = petsc_solver->snes();
144  KSP ksp;
145  SNESGetKSP(snes, &ksp);
146 
147  // Update the pseudo times
148  _nonlinear_time = _time_old; // non-linear time starts with the previous time step
149  if (_dt != 0)
150  _nonlinear_dt = _dt / _nonlinear_dt_divisor; // set the pseudo non-linear timestep as fraction
151  // of real timestep for transient executioners
152  else
153  _nonlinear_dt = 1. / _nonlinear_dt_divisor; // set the pseudo non-linear timestep for steady
154  // executioners (here _dt==0)
155 
156  _linear_dt = _nonlinear_dt / _linear_dt_divisor; // set the pseudo linear timestep
157 
158  // Set the PETSc monitor functions
161  {
162  PetscErrorCode ierr = SNESMonitorSet(snes, petscNonlinearOutput, this, PETSC_NULL);
163  CHKERRABORT(_communicator.get(), ierr);
164  }
165 
167  (_time >= _linear_start_time - _t_tol && _time <= _linear_end_time + _t_tol))
168  {
169  PetscErrorCode ierr = KSPMonitorSet(ksp, petscLinearOutput, this, PETSC_NULL);
170  CHKERRABORT(_communicator.get(), ierr);
171  }
172 #endif
173 }
174 
175 // Only define the monitor functions if PETSc exists
176 #ifdef LIBMESH_HAVE_PETSC
177 PetscErrorCode
178 PetscOutput::petscNonlinearOutput(SNES, PetscInt its, PetscReal norm, void * void_ptr)
179 {
180  // Get the outputter object
181  PetscOutput * ptr = static_cast<PetscOutput *>(void_ptr);
182 
183  // Update the pseudo times
184  ptr->_nonlinear_time += ptr->_nonlinear_dt;
185  ptr->_linear_time = ptr->_nonlinear_time;
186 
187  // Set the current norm and iteration number
188  ptr->_norm = norm;
189  ptr->_nonlinear_iter = its;
190 
191  // Set the flag indicating that output is occurring on the non-linear residual
192  ptr->_on_nonlinear_residual = true;
193 
194  // Perform the output
196 
209 
210  // Reset the non-linear output flag and the simulation time
211  ptr->_on_nonlinear_residual = false;
212 
213  // Done
214  return 0;
215 }
216 
217 PetscErrorCode
218 PetscOutput::petscLinearOutput(KSP, PetscInt its, PetscReal norm, void * void_ptr)
219 {
220  // Get the Outputter object
221  PetscOutput * ptr = static_cast<PetscOutput *>(void_ptr);
222 
223  // Update the pseudo time
224  ptr->_linear_time += ptr->_linear_dt;
225 
226  // Set the current norm and iteration number
227  ptr->_norm = norm;
228  ptr->_linear_iter = its;
229 
230  // Set the flag indicating that output is occurring on the non-linear residual
231  ptr->_on_linear_residual = true;
232 
233  // Perform the output
234  ptr->outputStep(EXEC_LINEAR);
235 
248 
249  // Reset the linear output flag and the simulation time
250  ptr->_on_linear_residual = false;
251 
252  // Done
253  return 0;
254 }
255 #endif
256 
257 Real
259 {
261  return _nonlinear_time;
262  else if (_on_linear_residual)
263  return _linear_time;
264  else
265  return Output::time();
266 }
Object is evaluated in every residual computation.
Definition: MooseTypes.h:96
static PetscErrorCode petscLinearOutput(KSP, PetscInt its, PetscReal fnorm, void *void_ptr)
Performs the output onlinear iterations.
Definition: PetscOutput.C:218
Real & _time_old
The old time.
Definition: Output.h:192
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:25
NonlinearSystemBase & getNonlinearSystemBase()
virtual NonlinearSolver< Number > * nonlinearSolver()=0
MultiMooseEnum _execute_on
The common Execution types; this is used as the default execution type for everything except system i...
Definition: Output.h:186
virtual Real time() override
Get the output time.
Definition: PetscOutput.C:258
void solveSetup() override
Internal setup function that executes at the beginning of the time step.
Definition: PetscOutput.C:134
bool _on_linear_residual
True if current output calls is on the linear residual (used by time())
Definition: PetscOutput.h:94
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:85
Real _linear_time
Psuedo linear time.
Definition: PetscOutput.h:88
bool _on_nonlinear_residual
True if current output call is on the non-linear residual (used by time())
Definition: PetscOutput.h:97
Real _linear_start_time
Linear residual output start time.
Definition: PetscOutput.h:109
bool contains(const std::string &value) const
Contains methods for seeing if a value is in the MultiMooseEnum.
Real _nonlinear_start_time
Non-linear residual output start time.
Definition: PetscOutput.h:106
NonlinearSystemBase * nl
Nonlinear system to be solved.
Based class for output objects.
Definition: Output.h:45
Real _linear_end_time
Linear residual output end time.
Definition: PetscOutput.h:115
PetscInt _linear_iter
Current linear iteration returned from PETSc.
Definition: PetscOutput.h:57
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:144
Real _linear_dt
Psuedo linear time step.
Definition: PetscOutput.h:91
FEProblemBase * _problem_ptr
Pointer the the FEProblemBase object for output object (use this)
Definition: Output.h:171
bool isParamValid(const std::string &name) const
Test if the supplied parameter is valid.
Definition: MooseObject.h:67
Real _nonlinear_time
The psuedo non-linear time.
Definition: PetscOutput.h:82
InputParameters validParams< Output >()
Definition: Output.C:34
Real _norm
Current norm returned from PETSc.
Definition: PetscOutput.h:51
PetscOutput(const InputParameters &parameters)
Class constructor.
Definition: PetscOutput.C:85
virtual Real time()
Get the output time.
Definition: Output.C:197
Real _nonlinear_dt_divisor
Pseudo non-linear timestep divisor.
Definition: PetscOutput.h:100
static PetscErrorCode petscNonlinearOutput(SNES, PetscInt its, PetscReal fnorm, void *void_ptr)
Performs the output on non-linear iterations.
Definition: PetscOutput.C:178
Real _t_tol
Time checking tolerance.
Definition: Output.h:219
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:108
Real _linear_dt_divisor
Pseudo linear timestep divisor.
Definition: PetscOutput.h:103
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
Object is evaluated in every jacobian computation.
Definition: MooseTypes.h:98
Adds the ability to output on every nonlinear and/or linear residual.
Definition: PetscOutput.h:30
Real _nonlinear_end_time
Non-linear residual output end time.
Definition: PetscOutput.h:112
Real & _time
The current time for output purposes.
Definition: Output.h:189
Real & _dt
Time step delta.
Definition: Output.h:198
OutputWarehouse & getOutputWarehouse()
Get the OutputWarehouse objects.
Definition: MooseApp.C:841
void flushConsoleBuffer()
If content exists in the buffer, write it.
PetscInt _nonlinear_iter
Current non-linear iteration returned from PETSc.
Definition: PetscOutput.h:54
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...