# Example 4 : Custom Boundary Conditions

MOOSE provides several basic boundary conditions that can be either used directly in your input files or extended to provide custom behavior. This example builds on the system of equations used in Example 3. In this example, we extend MOOSE's built-in Dirichlet and Neumann boundary condition objects by allowing them to be coupled to independent variables in your equation system. We also provide an example of using MOOSE's built-in periodic boundary conditions. For more detailed information about boundary conditions in MOOSE, see the BCs System Documentation

## Coupled Dirichlet

Dirichlet BCs in MOOSE all inherit from a NodalBC base-class - our CoupledDirichletBC in this example is no exception:


class CoupledDirichletBC : public NodalBC

#ifndef COUPLEDDIRICHLETBC_H
#define COUPLEDDIRICHLETBC_H

#include "NodalBC.h"

class CoupledDirichletBC;

template <>
InputParameters validParams<CoupledDirichletBC>();

/// Implements a coupled Dirichlet BC where u = alpha * some_var on the boundary.
class CoupledDirichletBC : public NodalBC
{
public:
CoupledDirichletBC(const InputParameters & parameters);

protected:
virtual Real computeQpResidual() override;

private:
/// Multiplier on the boundary.
Real _alpha;
/// reference to a user-specifiable coupled (independent) variable
const VariableValue & _some_var_val;
};

#endif // COUPLEDDIRICHLETBC_H

(examples/ex04_bcs/include/bcs/CoupledDirichletBC.h)

The source file creates input parameters and uses them to compute the residual just like the prior examples:

#include "CoupledDirichletBC.h"

registerMooseObject("ExampleApp", CoupledDirichletBC);

template <>
InputParameters
validParams<CoupledDirichletBC>()
{
InputParameters params = validParams<NodalBC>();

// 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");
return params;
}

CoupledDirichletBC::CoupledDirichletBC(const InputParameters & parameters)
: NodalBC(parameters),
// 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]);
}

(examples/ex04_bcs/src/bcs/CoupledDirichletBC.C)

Running the dirichlet_bc.i file should give you results like this for the 'u' and 'v' variables in our coupled system:

## Coupled Neumann

This example is largely the same as the coupled Dirichlet example above, except we inherit from the IntegratedBC class instead:


class CoupledNeumannBC : public IntegratedBC


And the residual calculation is obviously a bit different:

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];
}

(examples/ex04_bcs/src/bcs/CoupledNeumannBC.C)

This boundary condition class is used in the file neumann_bc.i. Running this file will generate results that look like this:

## Periodic

MOOSE provides built-in support for specifying periodic boundary conditions. The periodic_bc.i input file demonstrates this functionality while also taking advantage of MOOSE's ability for parsing user-specified analytical functions from input files. Parsed functions can be used to do many things in input files and are discussed in more detail in Example 13. TODO: briefly explain how to use PeriodicBC.

Here are some results using periodic BCs from running periodic_bc.i and trapezoid.i: