Skip to content

Commit

Permalink
Merge pull request #25272 from lindsayad/ad-vector-preset-25271
Browse files Browse the repository at this point in the history
Add ability to preset AD vector Dirichlet BCs
  • Loading branch information
lindsayad authored Aug 29, 2023
2 parents 516ad12 + 6a69f37 commit e5367e4
Show file tree
Hide file tree
Showing 14 changed files with 200 additions and 137 deletions.
4 changes: 2 additions & 2 deletions framework/include/bcs/ADDirichletBC.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

#pragma once

#include "ADDirichletBCBase.h"
#include "ADDirichletBCBaseTempl.h"

/**
* Boundary condition of a Dirichlet type
*
* Sets the values of a nodal variable at nodes
*/
class ADDirichletBC : public ADDirichletBCBase
class ADDirichletBC : public ADDirichletBCBaseTempl<Real>
{
public:
static InputParameters validParams();
Expand Down
13 changes: 3 additions & 10 deletions framework/include/bcs/ADDirichletBCBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,26 @@

#pragma once

#include "ADNodalBC.h"
#include "NodalBCBase.h"

/**
* Base class for automatic differentiation Dirichlet BCs
*/
class ADDirichletBCBase : public ADNodalBC
class ADDirichletBCBase : public NodalBCBase
{
public:
ADDirichletBCBase(const InputParameters & parameters);

/**
* Method to preset the nodal value if applicable
*/
void computeValue(NumericVector<Number> & current_solution);
virtual void computeValue(NumericVector<Number> & current_solution) = 0;

static InputParameters validParams();

bool preset() const { return _preset; }

protected:
virtual ADReal computeQpResidual() override;

/**
* Compute the value of the Dirichlet BC at the current quadrature point
*/
virtual ADReal computeQpValue() = 0;

/// Whether or not the value is to be preset
const bool _preset;
};
45 changes: 45 additions & 0 deletions framework/include/bcs/ADDirichletBCBaseTempl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//* 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 "ADDirichletBCBase.h"
#include "ADNodalBC.h"

/**
* Base class for automatic differentiation Dirichlet BCs
*/
template <typename T>
class ADDirichletBCBaseTempl : public ADNodalBCTempl<T, ADDirichletBCBase>
{
public:
ADDirichletBCBaseTempl(const InputParameters & parameters);

/**
* Method to preset the nodal value if applicable
*/
virtual void computeValue(NumericVector<Number> & current_solution) override;

static InputParameters validParams();

protected:
virtual typename Moose::ADType<T>::type computeQpResidual() override;

/**
* Compute the value of the Dirichlet BC at the current quadrature point
*/
virtual typename Moose::ADType<T>::type computeQpValue() = 0;

using ADNodalBCTempl<T, ADDirichletBCBase>::_var;
using ADNodalBCTempl<T, ADDirichletBCBase>::_sys;
using ADNodalBCTempl<T, ADDirichletBCBase>::_current_node;
using ADNodalBCTempl<T, ADDirichletBCBase>::_u;
using ADNodalBCTempl<T, ADDirichletBCBase>::_t;
using ADNodalBCTempl<T, ADDirichletBCBase>::shouldSetComp;
};
4 changes: 2 additions & 2 deletions framework/include/bcs/ADFunctionDirichletBC.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

#pragma once

#include "ADDirichletBCBase.h"
#include "ADDirichletBCBaseTempl.h"

/**
* Boundary condition of a Dirichlet type
*
* Sets the values of a nodal variable at nodes to values specified by a function
*/
class ADFunctionDirichletBC : public ADDirichletBCBase
class ADFunctionDirichletBC : public ADDirichletBCBaseTempl<Real>
{
public:
static InputParameters validParams();
Expand Down
28 changes: 22 additions & 6 deletions framework/include/bcs/ADNodalBC.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

#pragma once

#include "NodalBCBase.h"
#include "MooseVariableInterface.h"
#include "NodalBCBase.h"
#include "ADDirichletBCBase.h"

/**
* Base class for deriving any automatic differentiation boundary condition of a integrated type
*/
template <typename T>
class ADNodalBCTempl : public NodalBCBase, public MooseVariableInterface<T>
template <typename T, typename Base>
class ADNodalBCTempl : public Base, public MooseVariableInterface<T>
{
public:
static InputParameters validParams();
Expand All @@ -25,6 +26,8 @@ class ADNodalBCTempl : public NodalBCBase, public MooseVariableInterface<T>

const MooseVariableFE<T> & variable() const override { return _var; }

bool shouldSetComp(unsigned short i) const { return _set_components[i]; }

protected:
/**
* Compute this NodalBC's contribution to the residual at the current quadrature point
Expand All @@ -45,6 +48,14 @@ class ADNodalBCTempl : public NodalBCBase, public MooseVariableInterface<T>

const std::array<bool, 3> _set_components;

using Base::_fe_problem;
using Base::_subproblem;
using Base::_sys;
using Base::_tid;
using Base::addJacobian;
using Base::addMooseVariableDependency;
using Base::setResidual;

private:
void computeResidual() override final;
void computeJacobian() override final;
Expand All @@ -58,7 +69,6 @@ class ADNodalBCTempl : public NodalBCBase, public MooseVariableInterface<T>
template <typename ADResidual>
void addResidual(const ADResidual & residual, const std::vector<dof_id_type> & dof_indices);

using NodalBCBase::addJacobian;
/**
* process the Jacobian into the global data structures
*/
Expand All @@ -70,5 +80,11 @@ class ADNodalBCTempl : public NodalBCBase, public MooseVariableInterface<T>
Assembly & _undisplaced_assembly;
};

using ADNodalBC = ADNodalBCTempl<Real>;
using ADVectorNodalBC = ADNodalBCTempl<RealVectorValue>;
template <>
InputParameters ADNodalBCTempl<RealVectorValue, NodalBCBase>::validParams();

template <>
InputParameters ADNodalBCTempl<RealVectorValue, ADDirichletBCBase>::validParams();

using ADNodalBC = ADNodalBCTempl<Real, NodalBCBase>;
using ADVectorNodalBC = ADNodalBCTempl<RealVectorValue, NodalBCBase>;
27 changes: 0 additions & 27 deletions framework/include/bcs/ADPresetNodalBC.h

This file was deleted.

6 changes: 3 additions & 3 deletions framework/include/bcs/ADVectorFunctionDirichletBC.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@

#pragma once

#include "ADNodalBC.h"
#include "ADDirichletBCBaseTempl.h"

/**
* Boundary condition of a Dirichlet type
*
* Sets the values of a LAGRANGE_VEC variable at nodes to values specified by functions
*/
class ADVectorFunctionDirichletBC : public ADVectorNodalBC
class ADVectorFunctionDirichletBC : public ADDirichletBCBaseTempl<RealVectorValue>
{
public:
static InputParameters validParams();

ADVectorFunctionDirichletBC(const InputParameters & parameters);

protected:
virtual ADRealVectorValue computeQpResidual() override;
virtual ADRealVectorValue computeQpValue() override;

/// Optional vectorValue function
const Function * const _function;
Expand Down
4 changes: 2 additions & 2 deletions framework/src/bcs/ADDirichletBC.C
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ registerMooseObject("MooseApp", ADDirichletBC);
InputParameters
ADDirichletBC::validParams()
{
InputParameters params = ADDirichletBCBase::validParams();
InputParameters params = ADDirichletBCBaseTempl<Real>::validParams();
params.addRequiredParam<Real>("value", "Value of the BC");
params.declareControllable("value");
params.addClassDescription("Imposes the essential boundary condition $u=g$, where $g$ "
Expand All @@ -23,7 +23,7 @@ ADDirichletBC::validParams()
}

ADDirichletBC::ADDirichletBC(const InputParameters & parameters)
: ADDirichletBCBase(parameters), _value(getParam<Real>("value"))
: ADDirichletBCBaseTempl<Real>(parameters), _value(getParam<Real>("value"))
{
}

Expand Down
22 changes: 2 additions & 20 deletions framework/src/bcs/ADDirichletBCBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,13 @@
InputParameters
ADDirichletBCBase::validParams()
{
InputParameters params = ADNodalBC::validParams();
InputParameters params = NodalBCBase::validParams();
params.addParam<bool>(
"preset", true, "Whether or not to preset the BC (apply the value before the solve begins).");
return params;
}

ADDirichletBCBase::ADDirichletBCBase(const InputParameters & parameters)
: ADNodalBC(parameters), _preset(getParam<bool>("preset"))
: NodalBCBase(parameters), _preset(getParam<bool>("preset"))
{
}

void
ADDirichletBCBase::computeValue(NumericVector<Number> & current_solution)
{
mooseAssert(_preset, "BC is not preset");

if (_var.isNodalDefined())
{
const auto dof_idx = _var.nodalDofIndex();
current_solution.set(dof_idx, MetaPhysicL::raw_value(computeQpValue()));
}
}

ADReal
ADDirichletBCBase::computeQpResidual()
{
return _u - computeQpValue();
}
66 changes: 66 additions & 0 deletions framework/src/bcs/ADDirichletBCBaseTempl.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//* 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 "ADDirichletBCBaseTempl.h"
#include "MooseVariableFE.h"
#include "SystemBase.h"
#include "libmesh/node.h"

template <typename T>
InputParameters
ADDirichletBCBaseTempl<T>::validParams()
{
InputParameters params = ADNodalBCTempl<T, ADDirichletBCBase>::validParams();
params.addParam<bool>(
"preset", true, "Whether or not to preset the BC (apply the value before the solve begins).");
return params;
}

template <typename T>
ADDirichletBCBaseTempl<T>::ADDirichletBCBaseTempl(const InputParameters & parameters)
: ADNodalBCTempl<T, ADDirichletBCBase>(parameters)
{
}

template <typename T>
void
ADDirichletBCBaseTempl<T>::computeValue(NumericVector<Number> & current_solution)
{
mooseAssert(this->_preset, "BC is not preset");

if (_var.isNodalDefined())
{
const auto n_comp = _current_node->n_comp(_sys.number(), _var.number());
const auto value = MetaPhysicL::raw_value(computeQpValue());
for (const auto i : make_range(n_comp))
{
const auto dof_idx = _current_node->dof_number(_sys.number(), _var.number(), i);
if constexpr (std::is_same<T, Real>::value)
{
mooseAssert(n_comp == 1, "This should only be unity");
current_solution.set(dof_idx, value);
}
else
{
if (shouldSetComp(i))
current_solution.set(dof_idx, value(i));
}
}
}
}

template <typename T>
typename Moose::ADType<T>::type
ADDirichletBCBaseTempl<T>::computeQpResidual()
{
return _u - computeQpValue();
}

template class ADDirichletBCBaseTempl<Real>;
template class ADDirichletBCBaseTempl<RealVectorValue>;
4 changes: 2 additions & 2 deletions framework/src/bcs/ADFunctionDirichletBC.C
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ registerMooseObject("MooseApp", ADFunctionDirichletBC);
InputParameters
ADFunctionDirichletBC::validParams()
{
InputParameters params = ADDirichletBCBase::validParams();
InputParameters params = ADDirichletBCBaseTempl<Real>::validParams();
params.addClassDescription("Imposes the essential boundary condition $u=g$, where $g$ "
"is calculated by a function.");
params.addParam<FunctionName>("function", 0, "The function describing the Dirichlet condition");
return params;
}

ADFunctionDirichletBC::ADFunctionDirichletBC(const InputParameters & parameters)
: ADDirichletBCBase(parameters), _function(getFunction("function"))
: ADDirichletBCBaseTempl<Real>(parameters), _function(getFunction("function"))
{
}

Expand Down
Loading

0 comments on commit e5367e4

Please sign in to comment.