diff --git a/framework/doc/content/source/bcs/ADMatchedValueBC.md b/framework/doc/content/source/bcs/ADMatchedValueBC.md new file mode 100644 index 000000000000..44c6a6b5c0d2 --- /dev/null +++ b/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 diff --git a/framework/include/bcs/ADMatchedValueBC.h b/framework/include/bcs/ADMatchedValueBC.h new file mode 100644 index 000000000000..b5672627172b --- /dev/null +++ b/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; +}; diff --git a/framework/src/bcs/ADMatchedValueBC.C b/framework/src/bcs/ADMatchedValueBC.C new file mode 100644 index 000000000000..83c51ad701c7 --- /dev/null +++ b/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]; +} diff --git a/test/tests/bcs/ad_matched_value_bc/gold/out.e b/test/tests/bcs/ad_matched_value_bc/gold/out.e new file mode 100644 index 000000000000..d0ed5cf81a3d Binary files /dev/null and b/test/tests/bcs/ad_matched_value_bc/gold/out.e differ diff --git a/test/tests/bcs/ad_matched_value_bc/test.i b/test/tests/bcs/ad_matched_value_bc/test.i new file mode 100644 index 000000000000..03fc0fa404c1 --- /dev/null +++ b/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 +[] diff --git a/test/tests/bcs/ad_matched_value_bc/tests b/test/tests/bcs/ad_matched_value_bc/tests new file mode 100644 index 000000000000..4a9272a1b25f --- /dev/null +++ b/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' + [] +[]