Skip to content

Commit

Permalink
Adding VariableResidualNorm postprocessor
Browse files Browse the repository at this point in the history
For doing convergence studies, it is useful to be able to plot the
residual norm of variables of the interest.  MOOSE computes these
values, but we want to be able to store them as postprocessor values, so
we can plot them easily without postprocessing the console output.

Closes #7206
  • Loading branch information
andrsd committed Feb 10, 2017
1 parent dfa6013 commit 25f7c20
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 1 deletion.
43 changes: 43 additions & 0 deletions framework/include/postprocessors/VariableResidual.h
@@ -0,0 +1,43 @@
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/

#ifndef VARIABLERESIDUAL_H
#define VARIABLERESIDUAL_H

#include "GeneralPostprocessor.h"

//Forward Declarations
class VariableResidual;

template<>
InputParameters validParams<VariableResidual>();

class VariableResidual : public GeneralPostprocessor
{
public:
VariableResidual(const InputParameters & parameters);

virtual void initialize() override;
virtual void execute() override;

virtual PostprocessorValue getValue() override;

protected:
/// MOOSE variable we compute the residual for
MooseVariable & _var;
/// The residual of the variable
Real _var_residual;
};

#endif //VARIABLERESIDUAL_H
3 changes: 2 additions & 1 deletion framework/src/base/Moose.C
Expand Up @@ -229,6 +229,7 @@
#include "RelativeSolutionDifferenceNorm.h"
#include "AxisymmetricCenterlineAverageValue.h"
#include "VariableInnerProduct.h"
#include "VariableResidual.h"

// vector PPS
#include "ConstantVectorPostprocessor.h"
Expand Down Expand Up @@ -661,6 +662,7 @@ registerObjects(Factory & factory)
registerPostprocessor(RelativeSolutionDifferenceNorm);
registerPostprocessor(AxisymmetricCenterlineAverageValue);
registerPostprocessor(VariableInnerProduct);
registerPostprocessor(VariableResidual);

// vector PPS
registerVectorPostprocessor(ConstantVectorPostprocessor);
Expand Down Expand Up @@ -1204,4 +1206,3 @@ bool _throw_on_error = false;

// TODO: delete this after migration to new error/warn funcs is complete
OStreamProxy & _console = Moose::out;

50 changes: 50 additions & 0 deletions framework/src/postprocessors/VariableResidual.C
@@ -0,0 +1,50 @@
/****************************************************************/
/* DO NOT MODIFY THIS HEADER */
/* MOOSE - Multiphysics Object Oriented Simulation Environment */
/* */
/* (c) 2010 Battelle Energy Alliance, LLC */
/* ALL RIGHTS RESERVED */
/* */
/* Prepared by Battelle Energy Alliance, LLC */
/* Under Contract No. DE-AC07-05ID14517 */
/* With the U. S. Department of Energy */
/* */
/* See COPYRIGHT for full restrictions */
/****************************************************************/

#include "VariableResidual.h"

template<>
InputParameters validParams<VariableResidual>()
{
InputParameters params = validParams<GeneralPostprocessor>();
params.addRequiredParam<VariableName>("variable", "The name of the variable to compute the residual for");
return params;
}

VariableResidual::VariableResidual(const InputParameters & parameters)
: GeneralPostprocessor(parameters),
_var(_fe_problem.getVariable(_tid, getParam<VariableName>("variable")))
{
if (_var.kind() != Moose::VAR_NONLINEAR)
mooseError2(name(), ": Residual can be computed only for nonlinear variables.");
}

void
VariableResidual::initialize()
{
_var_residual = 0;
}

void
VariableResidual::execute()
{
NonlinearSystemBase & nl = _fe_problem.getNonlinearSystemBase();
_var_residual = nl.system().calculate_norm(nl.RHS(), _var.number(), DISCRETE_L2);
}

PostprocessorValue
VariableResidual::getValue()
{
return _var_residual;
}
@@ -0,0 +1,4 @@
time,u_res_l2,v_res_l2
0,0,0
1,1.8140133821974e-06,0.00058473832580092

8 changes: 8 additions & 0 deletions test/tests/postprocessors/variable_residual_norm/tests
@@ -0,0 +1,8 @@
[Tests]
[./variable_residual_test]
type = 'CSVDiff'
input = 'variable_residual.i'
csvdiff = 'variable_residual_out.csv'
max_threads = 1
[../]
[]
103 changes: 103 additions & 0 deletions test/tests/postprocessors/variable_residual_norm/variable_residual.i
@@ -0,0 +1,103 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 2
ny = 2
xmin = -1
xmax = 1
ymin = 0
ymax = 1
elem_type = QUAD4
[]

[Variables]
[./u]
[../]

[./v]
[../]
[]

[Kernels]
[./diff_u]
type = Diffusion
variable = u
[../]

[./diff_v]
type = Diffusion
variable = v
[../]
[]

[Functions]
[./leg1]
type = ParsedFunction
value = 'x'
[../]

[./leg2]
type = ParsedFunction
value = '0.5*(3.0*x*x-1.0)'
[../]
[]

[BCs]
[./left_u]
type = DirichletBC
variable = u
boundary = 1
value = 0
[../]

[./right_u]
type = DirichletBC
variable = u
boundary = 2
value = 1
[../]

[./left_v]
type = DirichletBC
variable = v
boundary = 1
value = 200
[../]

[./right_v]
type = DirichletBC
variable = v
boundary = 2
value = 100
[../]
[]

[Executioner]
type = Steady
solve_type = 'PJFNK'

# this is large on purpose so we don't reduce the variable residual to machine zero
# and so that we can compare to larger numbers
nl_rel_tol = 1e-4
[]

[Postprocessors]
[./u_res_l2]
type = VariableResidual
variable = u
[../]

[./v_res_l2]
type = VariableResidual
variable = v
[../]
[]

[Outputs]
csv = true
[./console]
type = Console
# turn this on, so we can visually compare the postprocessor value with what is computed by the Console object
all_variable_norms = true
[../]
[]

0 comments on commit 25f7c20

Please sign in to comment.