Skip to content

Commit

Permalink
Framework bits of hybrid CG-DG work
Browse files Browse the repository at this point in the history
From pull request idaholab#23986. Refs issue idaholab#24055
  • Loading branch information
lindsayad authored and oanaoana committed Oct 19, 2023
1 parent f8e4d51 commit f96c395
Show file tree
Hide file tree
Showing 18 changed files with 610 additions and 79 deletions.
31 changes: 31 additions & 0 deletions framework/doc/content/source/bcs/ADConservativeAdvectionBC.md
@@ -0,0 +1,31 @@
# ADConservativeAdvectionBC

## Description

The `ADConservativeAdvectionBC` boundary condition pairs with the
[ADConservativeAdvection.md] kernel. It can be used for both Dirichlet and
"implicit" boundary conditions which use only information from the equation
systems solution vectors. Velocity must be provided, either through the
`velocity_mat_prop` parameter (implicit information) or through the
`velocity_function` parameter (Dirichlet information). Similarly, for Dirichlet
conditions, a `primal_dirichlet_value` should be supplied. Otherwise the
advected quantity will be determined from implicit information either through
the supplied `advected_quantity` or if that is not supplied, the `variable`
solution. If `primal_dirichlet_value` is supplied, then a `primal_coefficient`
material property name may be supplied which will multiply the
`primal_dirichlet_value`.

An example of this boundary condition's use is shown in the listing below for
both an inlet and outlet condition. At the inlet (`boundary = left`) both the velocity and primal
value (the `variable` `u` in this case) are prescribed. At the outlet
(`boundary= right`) due to the absence of `primal_dirichlet_value`, the current
solution value of `u` is used. Additionally, the velocity is also determined
implicitly through `velocity_mat_prop`.

!listing test/tests/dgkernels/passive-scalar-channel-flow/test.i block=BCs

!syntax parameters /BCs/ADConservativeAdvectionBC

!syntax inputs /BCs/ADConservativeAdvectionBC

!syntax children /BCs/ADConservativeAdvectionBC
31 changes: 31 additions & 0 deletions framework/doc/content/source/dgkernels/ADDGConvection.md
@@ -0,0 +1,31 @@
# ADDGConvection

!syntax description /DGKernels/ADDGConvection

This is the automatic differentiation analog of [DGConvection.md] but with a
couple distinctions:

- The [!param](/DGKernels/ADDGConvection/velocity) parameter is a material
property instead of a constant. This allows this object's use in a
simulation where the velocity is being solved for
and/or changing spatially. Additionally, the use of a material property as
opposed to a coupled variable allows more straightforward
propagation of derivatives for automatic differentiation.
- A [!param](/DGKernels/ADDGConvection/advected_quantity) paramter is
available which allows for advecting different quantities than the `variable`
this object is acting upon

## Example input syntax

In this example, a field `u` is advected from a boundary condition on its left to the right boundary
with a `1 0 0` velocity. In addition to advection, the simulation is governed by
loss of `u` through diffusion out of the top
and bottom boundaries of the domain.

!listing test/tests/dgkernels/passive-scalar-channel-flow/test.i block=DGKernels

!syntax parameters /DGKernels/ADDGConvection

!syntax inputs /DGKernels/ADDGConvection

!syntax children /DGKernels/ADDGConvection
21 changes: 21 additions & 0 deletions framework/doc/content/source/kernels/ADConservativeAdvection.md
@@ -0,0 +1,21 @@
# ADConservativeAdvection

## Description

The `ADConservativeAdvection` kernel implements the same advection term as
[ConservativeAdvection.md]. A few differences from that object are:

- The [!param](/Kernels/ADConservativeAdvection/velocity) parameter is a material
property instead of a coupled variable. This allows more straightforward
propagation of derivatives for automatic differentiation
- No upwinding option is currently implemented. In that vein this object may be
best used within a discontinuous Galerkin scheme with [ADDGConvection.md].
- A [!param](/Kernels/ADConservativeAdvection/advected_quantity) paramter is
available which allows for advecting different quantities than the `variable`
this object is acting upon

!syntax parameters /Kernels/ADConservativeAdvection

!syntax inputs /Kernels/ADConservativeAdvection

!syntax children /Kernels/ADConservativeAdvection
@@ -0,0 +1,24 @@
# VectorFromVariableComponentsMaterial

## Description

`VectorFromVariableComponentsMaterial` computes a vector material property with
name specified by the `vector_prop_name` parameter from coupled variable
components. The x-component is computed through the `u` coupled
variable. Optional coupled variables `v` and `w` compute the y- and z-components
respectively.

An example of this object's use is shown in the listing below where in this case
a velocity material property is being declared. The ability to pass constants to
the coupled variables is leveraged in this example. Actual coupled variable
instances would be used in, for example, a Navier-Stokes simulation in which the
nonlinear system solves for velocity components and the vector velocity needs to
be constructed.

!listing test/tests/dgkernels/passive-scalar-channel-flow/test.i block=Materials

!syntax parameters /Materials/VectorFromVariableComponentsMaterial

!syntax inputs /Materials/VectorFromVariableComponentsMaterial

!syntax children /Materials/VectorFromVariableComponentsMaterial
45 changes: 45 additions & 0 deletions framework/include/bcs/ADConservativeAdvectionBC.h
@@ -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 "ADIntegratedBC.h"

class Function;

/**
* A boundary condition for when the advection term is integrated by parts. This can be used at both
* inlet and outlet boundaries for imposing both "explicit" (e.g. Dirichlet) and "implicit" (use
* interior information) boundary conditions
*/
class ADConservativeAdvectionBC : public ADIntegratedBC
{
public:
static InputParameters validParams();

ADConservativeAdvectionBC(const InputParameters & parameters);

protected:
virtual ADReal computeQpResidual() override;

/// The velocity as a material property
const ADMaterialProperty<RealVectorValue> * const _velocity_mat_prop;

/// The velocity as a function
const Function * const _velocity_function;

/// The advected quantity
const MooseArray<ADReal> & _adv_quant;

/// Dirichlet value for the primal variable
const Function * const _primal_dirichlet;

/// Coefficient for multiplying the primal Dirichlet value
const ADMaterialProperty<Real> & _primal_coeff;
};
37 changes: 37 additions & 0 deletions framework/include/dgkernels/ADDGConvection.h
@@ -0,0 +1,37 @@
//* 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 "ADDGKernel.h"

/**
* Adds residual/Jacobian contributions for a convection term from internal faces for a
* discontinuous Galerkin formulation
*/
class ADDGConvection : public ADDGKernel
{
public:
static InputParameters validParams();

ADDGConvection(const InputParameters & parameters);

protected:
virtual ADReal computeQpResidual(Moose::DGResidualType type) override;

/// The velocity on the element
const ADMaterialProperty<RealVectorValue> & _velocity;
/// The velocity on the neighbor
const ADMaterialProperty<RealVectorValue> & _velocity_neighbor;

/// The advected quantity value on the element side of the face
const MooseArray<ADReal> & _adv_quant_elem;
/// The advected quantity value on the neighbor side of the face
const MooseArray<ADReal> & _adv_quant_neighbor;
};
33 changes: 33 additions & 0 deletions framework/include/kernels/ADConservativeAdvection.h
@@ -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

#pragma once

#include "ADKernel.h"

/**
* A conservative formulation of advection, e.g. no assumptions are made about whether the velocity
* is divergence-free and the convection term is integrated by parts
*/
class ADConservativeAdvection : public ADKernel
{
public:
static InputParameters validParams();

ADConservativeAdvection(const InputParameters & parameters);

protected:
virtual ADReal computeQpResidual() override;

/// advection velocity
const ADMaterialProperty<RealVectorValue> & _velocity;

/// advected quantity
const MooseArray<ADReal> & _adv_quant;
};
39 changes: 39 additions & 0 deletions framework/include/materials/VectorFromVariableComponentsMaterial.h
@@ -0,0 +1,39 @@
//* 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 "Material.h"

/**
* Computes a vector material property from coupled variables
*/
template <bool is_ad>
class VectorFromVariableComponentsMaterialTempl : public Material
{
public:
static InputParameters validParams();

VectorFromVariableComponentsMaterialTempl(const InputParameters & parameters);

protected:
virtual void computeQpProperties() override;

/// The velocity
GenericMaterialProperty<RealVectorValue, is_ad> & _vector;
/// The x-component
const GenericVariableValue<is_ad> & _u;
/// The y-component
const GenericVariableValue<is_ad> & _v;
/// The z-component
const GenericVariableValue<is_ad> & _w;
};

typedef VectorFromVariableComponentsMaterialTempl<false> VectorFromVariableComponentsMaterial;
typedef VectorFromVariableComponentsMaterialTempl<true> ADVectorFromVariableComponentsMaterial;
75 changes: 75 additions & 0 deletions framework/src/bcs/ADConservativeAdvectionBC.C
@@ -0,0 +1,75 @@
//* 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 "ADConservativeAdvectionBC.h"
#include "Function.h"

registerMooseObject("MooseApp", ADConservativeAdvectionBC);

InputParameters
ADConservativeAdvectionBC::validParams()
{
InputParameters params = ADIntegratedBC::validParams();
params.addParam<MaterialPropertyName>(
"velocity_mat_prop",
"Velocity vector as a material property. Should be provided when we want the velocity value "
"to be determined implicitly (e.g. we don't have a Dirichlet condition)");
params.addParam<FunctionName>("velocity_function",
"Function describing the values of velocity on the boundary.");
params.addClassDescription("DG for convection");
params.addParam<MaterialPropertyName>("advected_quantity",
"An optional material property to be advected. If not "
"supplied, then the variable will be used.");
params.addParam<FunctionName>("primal_dirichlet_value",
"The value of the primal variable on the boundary.");
params.addParam<MaterialPropertyName>(
"primal_coefficient",
1,
"If a primal Dirichlet value is supplied, then a coefficient may be optionally multiplied "
"that multiples the Dirichlet value");
return params;
}

ADConservativeAdvectionBC::ADConservativeAdvectionBC(const InputParameters & parameters)
: ADIntegratedBC(parameters),
_velocity_mat_prop(isParamValid("velocity_mat_prop")
? &getADMaterialProperty<RealVectorValue>("velocity_mat_prop")
: nullptr),
_velocity_function(isParamValid("velocity_function") ? &getFunction("velocity_function")
: nullptr),
_adv_quant(isParamValid("advected_quantity")
? getADMaterialProperty<Real>("advected_quantity").get()
: _u),
_primal_dirichlet(
isParamValid("primal_dirichlet_value") ? &getFunction("primal_dirichlet_value") : nullptr),
_primal_coeff(getADMaterialProperty<Real>("primal_coefficient"))
{
if (isParamSetByUser("primal_coefficient") && !_primal_dirichlet)
paramError("primal_coefficient",
"This parameter should only be provided when 'primal_dirichlet_value' is provided");
if (static_cast<bool>(_primal_dirichlet) + isParamValid("advected_quantity") > 1)
mooseError("Only one of 'primal_dirichlet_value' or 'advected_quantity' should be provided");
if (static_cast<bool>(_velocity_mat_prop) + static_cast<bool>(_velocity_function) != 1)
mooseError("Exactly one of 'velocity_mat_prop' or 'velocity_function' should be provided");
}

ADReal
ADConservativeAdvectionBC::computeQpResidual()
{
const auto vdotn =
(_velocity_mat_prop ? (*_velocity_mat_prop)[_qp]
: ADRealVectorValue(_velocity_function->vectorValue(_t, _q_point[_qp]))) *
_normals[_qp];

if (_primal_dirichlet)
return _test[_i][_qp] * vdotn * _primal_dirichlet->value(_t, _q_point[_qp]) *
_primal_coeff[_qp];
else
return _test[_i][_qp] * vdotn * _adv_quant[_qp];
}

0 comments on commit f96c395

Please sign in to comment.