Permalink
Browse files

Merge pull request #12539 from rwcarlsen/example-doc

improve examples 3 and 4
  • Loading branch information...
aeslaughter committed Nov 27, 2018
2 parents 49c4df0 + 6eb277f commit fc962ae34a39c188dc7a5ddce42f408c20ad00b4
@@ -9,6 +9,7 @@
#include "ExampleConvection.h"
// Don't forget to register your object with MOOSE
registerMooseObject("ExampleApp", ExampleConvection);
template <>
@@ -17,24 +18,34 @@ validParams<ExampleConvection>()
{
InputParameters params = validParams<Kernel>();
// Here we specify a new parameter for our kernel allowing users to indicate which other
// variable they want to be coupled into this kernel from an input file.
params.addRequiredCoupledVar(
"some_variable", "The gradient of this variable will be used as the velocity vector.");
return params;
}
ExampleConvection::ExampleConvection(const InputParameters & parameters)
: Kernel(parameters), _grad_some_variable(coupledGradient("some_variable"))
: Kernel(parameters),
// using the user-specified name for the coupled variable, retrieve and store a reference to the
// coupled variable.
_grad_some_variable(coupledGradient("some_variable"))
{
}
Real
ExampleConvection::computeQpResidual()
{
// Implement the weak form equations using the coupled variable instead of the constant
// parameter 'velocity' used in example 2.
return _test[_i][_qp] * (_grad_some_variable[_qp] * _grad_u[_qp]);
}
Real
ExampleConvection::computeQpJacobian()
{
// Implement the Jacobian using the coupled variable instead of the 'velocity'
// constant parameter used in example 2.
return _test[_i][_qp] * (_grad_some_variable[_qp] * _grad_phi[_j][_qp]);
}
@@ -12,37 +12,24 @@
#include "NodalBC.h"
// Forward Declarations
class CoupledDirichletBC;
template <>
InputParameters validParams<CoupledDirichletBC>();
/**
* Implements a coupled Dirichlet BC where u = alpha * some_var on the boundary.
*/
/// Implements a coupled Dirichlet BC where u = alpha * some_var on the boundary.
class CoupledDirichletBC : public NodalBC
{
public:
/**
* Factory constructor, takes parameters so that all derived classes can be built using the same
* constructor.
*/
CoupledDirichletBC(const InputParameters & parameters);
protected:
virtual Real computeQpResidual() override;
private:
/**
* Multiplier on the boundary.
*/
/// Multiplier on the boundary.
Real _alpha;
/**
* Holds the values at the quadrature points
* of a coupled variable.
*/
/// reference to a user-specifiable coupled (independent) variable
const VariableValue & _some_var_val;
};
@@ -12,7 +12,6 @@
#include "IntegratedBC.h"
// Forward Declarations
class CoupledNeumannBC;
template <>
@@ -25,25 +24,15 @@ InputParameters validParams<CoupledNeumannBC>();
class CoupledNeumannBC : public IntegratedBC
{
public:
/**
* Factory constructor, takes parameters so that all derived classes can be built using the same
* constructor.
*/
CoupledNeumannBC(const InputParameters & parameters);
protected:
virtual Real computeQpResidual() override;
private:
/**
* Multiplier on the boundary.
*/
/// Multiplier on the boundary.
Real _alpha;
/**
* Holds the values at the quadrature points
* of a coupled variable.
*/
/// reference to a user-specifiable coupled (independent) variable
const VariableValue & _some_var_val;
};
@@ -17,29 +17,23 @@ validParams<CoupledDirichletBC>()
{
InputParameters params = validParams<NodalBC>();
// Here we are adding a parameter that will be extracted from the input file by the Parser
// Specify input parameters that we want users to be able to set:
params.addParam<Real>("alpha", 1.0, "Value multiplied by the coupled value on the boundary");
params.addRequiredCoupledVar("some_var", "Value on the Boundary");
params.addRequiredCoupledVar("some_var", "Value on the boundary");
return params;
}
CoupledDirichletBC::CoupledDirichletBC(const InputParameters & parameters)
: NodalBC(parameters),
/**
* Grab the parameter for the multiplier.
*/
_alpha(getParam<Real>("alpha")),
/**
* Get a reference to the coupled variable's values.
*/
_some_var_val(coupledValue("some_var"))
// store the user-specified parameters from the input file...
_alpha(getParam<Real>("alpha")), // for the multiplier
_some_var_val(coupledValue("some_var")) // for the coupled variable
{
}
Real
CoupledDirichletBC::computeQpResidual()
{
// For dirichlet BCS, u=BC at the boundary, so the residual includes _u and the desired BC value:
return _u[_qp] - (_alpha * _some_var_val[_qp]);
}
@@ -17,21 +17,24 @@ validParams<CoupledNeumannBC>()
{
InputParameters params = validParams<IntegratedBC>();
// Here we are adding a parameter that will be extracted from the input file by the Parser
// Specify input parameters that we want users to be able to set:
params.addParam<Real>("alpha", 1.0, "Value multiplied by the coupled value on the boundary");
params.addRequiredCoupledVar("some_var", "Flux Value at the Boundary");
params.addRequiredCoupledVar("some_var", "Flux value at the boundary");
return params;
}
CoupledNeumannBC::CoupledNeumannBC(const InputParameters & parameters)
: IntegratedBC(parameters),
_alpha(getParam<Real>("alpha")),
_some_var_val(coupledValue("some_var"))
// store the user-specified parameters from the input file...
_alpha(getParam<Real>("alpha")), // for the multiplier
_some_var_val(coupledValue("some_var")) // for the coupled variable
{
}
Real
CoupledNeumannBC::computeQpResidual()
{
// For this Neumann BC grad(u)=alpha * v on the boundary.
// We use the term produced from integrating the diffusion operator by parts.
return -_test[_i][_qp] * _alpha * _some_var_val[_qp];
}
@@ -21,73 +21,31 @@ where $\phi_i$ are the test functions and $u_h$ and $v_h$ are the finite element
## Create Convection Kernel
The convection component of the problem requires the creation of a a new `Kernel`, as described in
Example 02. Here the `Kernel` must utilize a coupled variable rather than a known constant.
The convection component of the problem requires `Kernel` object just as described in Example 2
with one small addition - the `Kernel` will utilize a coupled variable rather than a known
constant. "ExampleConvection.h" needs one new member variable to store the gradient of the
coupled variable:
The header for this object, "ExampleConvection.h" is little changed from the previous example,
with one exception, a member is defined to store the gradient of the coupled variable:
!listing examples/ex03_coupling/include/kernels/ExampleConvection.h start=private end=}
[examples/ex03_coupling/include/kernels/ExampleConvection.h]
The source file "ExampleConvection.C" also includes a new parameter that defines the variable to
couple into its kernel. Additionally, the `computeQpResidual` and `computeQpJacobian` functions in
the source file now utilize the coupled value to compute the desired residuals and jacobians
respectively:
The source filem "ExampleConvection.C", after including the header, defines the parameters for the
kernel. This definition includes adding a parameter that defines the variable to couple into tis
kernel.
[examples/ex03_coupling/src/kernels/ExampleConvection.C]
Finally, the `computeQpResiduals` and `computeQpJacobian` then utilize the coupled value to
compute the desired residuals and jacobians.
```cpp
Real ExampleConvection::computeQpResidual()
{
return _test[_i][_qp]*(_grad_some_variable[_qp]*_grad_u[_qp]);
}
```
```cpp
Real ExampleConvection::computeQpJacobian()
{
return _test[_i][_qp]*(_grad_some_variable[_qp]*_grad_phi[_j][_qp]);
}
```
## Register Kernel
As done in Example 2, the newly created object must be registered. This is accomplished by
including the "Convection.h" file and the following in `ExampleApp::registerObjects` method of
`src/base/ExampleApp.C` within the example 3 directory of MOOSE (`examples/ex03_coupling`).
```cpp
registerKernel(ExampleConvection);
```
!listing examples/ex03_coupling/src/kernels/ExampleConvection.C start=#include end=$ max-height=10000px
## Input File Syntax
First, the mesh is defined by loading a file "mug.e".
```puppet
[Mesh]
file = mug.e
[]
```
!listing examples/ex03_coupling/ex03.i block=Mesh
Then, the two variables are defined: "diffused" and "convected", which refer to $$u$$ and $$v$$
from the problem statement, respectively. Both variables in this case are assigned to utilize
linear Lagrange shape functions, but they could each use different shape functions and/or orders.
```puppet
[Variables]
[./convected]
order = FIRST
family = LAGRANGE
[../]
[./diffused]
order = FIRST
family = LAGRANGE
[../]
[]
```
!listing examples/ex03_coupling/ex03.i block=Variables
The problem requires three `Kernels`, two `Diffusion` `Kernels`, one for each of the variables and
the `ExampleConvection` `Kernel` created above. It is important to point out that for the two
@@ -96,74 +54,17 @@ application of the code to two variables. Additionally, the actual coupling of
takes place in the `ExampleConvection` object. The `some_variable` input parameter was created in
the `ExampleConvection` `Kernel` and here is assigned to utilize the `diffused` variable.
```puppet
[Kernels]
[./diff_convected]
type = Diffusion
variable = convected
[../]
[./conv]
type = ExampleConvection
variable = convected
some_variable = diffused #Couple variable into the convection kernel
[../]
[./diff_diffused]
type = Diffusion
variable = diffused
[../]
[]
```
!listing examples/ex03_coupling/ex03.i block=Kernels
For the given problem, each of the variables has a `DirichletBC` applied at the top and bottom.
This is done in the input file as follows.
```puppet
[BCs]
[./bottom_convected]
type = DirichletBC
variable = convected
boundary = 'bottom'
value = 1
[../]
[./top_convected]
type = DirichletBC
variable = convected
boundary = 'top'
value = 0
[../]
[./bottom_diffused]
type = DirichletBC
variable = diffused
boundary = 'bottom'
value = 2
[../]
[./top_diffused]
type = DirichletBC
variable = diffused
boundary = 'top'
value = 0
[../]
[]
```
!listing examples/ex03_coupling/ex03.i block=BCs max-height=10000px
Finally, the `Executioner` block is setup for solving the problem an the `Outputs` are set for
viewing the results.
```puppet
[Executioner]
type = Steady
solve_type = 'PJFNK'
[]
[Outputs]
file_base = out
exodus = true
[./console]
type = Console
perf_log = true
linear_residuals = true
[../]
[]
```
!listing examples/ex03_coupling/ex03.i start=Executioner end=$
## Running the Problem
Oops, something went wrong.

0 comments on commit fc962ae

Please sign in to comment.