Skip to content

Commit

Permalink
Adding AD version of MatchedValueBC
Browse files Browse the repository at this point in the history
  • Loading branch information
andrsd committed Jul 1, 2021
1 parent ccd0f14 commit 91ceefe
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 0 deletions.
36 changes: 36 additions & 0 deletions framework/doc/content/source/bcs/ADMatchedValueBC.md
@@ -0,0 +1,36 @@
# ADMatchedValueBC

!syntax description /BCs/ADMatchedValueBC

## Description

`ADMatchedValueBC` is a `ADNodalBC` which applies to systems of two or more variables,
and can be used to impose equality of two solutions along a given `boundary`.
This class is appropriate for systems of partial differential equations (PDEs) of
the form
\begin{equation}
\begin{aligned}
-\nabla^2 u &= f_1 && \quad \in \Omega \\
-\nabla^2 v &= f_2 && \quad \in \Omega \\
\frac{\partial u}{\partial n} &= h_1 && \quad \in \partial \Omega_N \\
\frac{\partial v}{\partial n} &= h_2 && \quad \in \partial \Omega_N \\
u &= v && \quad \in \partial \Omega_D,
\end{aligned}
\end{equation}
where $\Omega \subset \mathbb{R}^n$ is the domain, and $\partial
\Omega = \partial \Omega_D \cup \partial \Omega_N$ is its boundary,
$u$, $v$ are the unknowns, $f_1$, $f_2$ are forcing functions (which
may depend on both $u$ and $v$), and $h_1$ and $h_2$ are given
fluxes. The `v` parameter is used to specify the variable whose value
is tied to $u$. In the example below, the other variable's name
happens to be `v` as well.

## Example Input Syntax

!listing test/tests/bcs/ad_matched_value_bc/test.i start=[left_u] end=[] include-end=true

!syntax parameters /BCs/ADMatchedValueBC

!syntax inputs /BCs/ADMatchedValueBC

!syntax children /BCs/ADMatchedValueBC
28 changes: 28 additions & 0 deletions framework/include/bcs/ADMatchedValueBC.h
@@ -0,0 +1,28 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "ADNodalBC.h"

/**
* Implements a simple coupled boundary condition where u=v on the boundary.
*/
class ADMatchedValueBC : public ADNodalBC
{
public:
static InputParameters validParams();

ADMatchedValueBC(const InputParameters & parameters);

protected:
virtual ADReal computeQpResidual() override;

const ADVariableValue & _v;
};
33 changes: 33 additions & 0 deletions framework/src/bcs/ADMatchedValueBC.C
@@ -0,0 +1,33 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "ADMatchedValueBC.h"

registerMooseObject("MooseApp", ADMatchedValueBC);

InputParameters
ADMatchedValueBC::validParams()
{
InputParameters params = ADNodalBC::validParams();
params.addRequiredCoupledVar("v", "The variable whose value we are to match.");
params.addClassDescription("Implements a nodal BC which equates two different Variables' values "
"on a specified boundary.");
return params;
}

ADMatchedValueBC::ADMatchedValueBC(const InputParameters & parameters)
: ADNodalBC(parameters), _v(adCoupledValue("v"))
{
}

ADReal
ADMatchedValueBC::computeQpResidual()
{
return _u - _v[_qp];
}
Binary file added test/tests/bcs/ad_matched_value_bc/gold/out.e
Binary file not shown.
65 changes: 65 additions & 0 deletions test/tests/bcs/ad_matched_value_bc/test.i
@@ -0,0 +1,65 @@
[Mesh]
[square]
type = GeneratedMeshGenerator
nx = 2
ny = 2
dim = 2
[]
[]

# Solves a pair of coupled diffusion equations where u=v on the boundary

[Variables]
[u]
order = FIRST
family = LAGRANGE
initial_condition = 3
[]

[v]
order = FIRST
family = LAGRANGE
initial_condition = 2
[]
[]

[Kernels]
[diff_u]
type = ADDiffusion
variable = u
[]

[diff_v]
type = ADDiffusion
variable = v
[]
[]

[BCs]
[right_v]
type = ADDirichletBC
variable = v
boundary = 1
value = 3
[]

[left_u]
type = ADMatchedValueBC
variable = u
boundary = 3
v = v
[]
[]

[Executioner]
type = Steady
solve_type = 'NEWTON'

nl_rel_tol = 1e-10
l_tol = 1e-12
[]

[Outputs]
file_base = out
exodus = true
[]
18 changes: 18 additions & 0 deletions test/tests/bcs/ad_matched_value_bc/tests
@@ -0,0 +1,18 @@
[Tests]
requirement = "MOOSE shall support matching variable values on a boundary using automatic differentiation"
design = 'ADMatchedValueBC.md'
issues = "#18212"
[test]
type = 'Exodiff'
input = 'test.i'
exodiff = 'out.e'
detail = ''
[]
[jacobian]
type = 'PetscJacobianTester'
input = 'test.i'
ratio_tol = 1e-8
difference_tol = 1e-7
detail = 'and shall be able to produce the exact Jacobian'
[]
[]

0 comments on commit 91ceefe

Please sign in to comment.