Skip to content
Permalink
Browse files

Merge pull request #13173 from lindsayad/non-transient-vars-in-transi…

…ent-sim

More AD checking
  • Loading branch information...
aeslaughter committed Apr 4, 2019
2 parents d79007a + 2afed21 commit 5af125403874c98120e05d91ea57bed94620a779
@@ -16,6 +16,7 @@
#include "metaphysicl/dualnumber.h"

#define usingDGKernelMembers \
usingDGKernelBaseMembers; \
using ADDGKernel<compute_stage>::_test; \
using ADDGKernel<compute_stage>::_qp; \
using ADDGKernel<compute_stage>::_i; \
@@ -32,6 +32,12 @@ class Assembly;

class DGKernelBase;

#define usingDGKernelBaseMembers \
usingNeighborCoupleableMembers; \
usingFunctionInterfaceMembers; \
usingBlockRestrictableMembers; \
usingTransientInterfaceMembers

template <>
InputParameters validParams<DGKernelBase>();

@@ -27,7 +27,8 @@
using Coupleable::coupledValueOlder; \
using Coupleable::coupledGradient; \
using Coupleable::coupledGradientOld; \
using Coupleable::coupledGradientOlder
using Coupleable::coupledGradientOlder; \
using Coupleable::getVar

// Forward declarations
class MooseVariableScalar;
@@ -13,6 +13,8 @@
#include "MooseVariableBase.h"
#include "Coupleable.h"

#define usingNeighborCoupleableMembers usingCoupleableMembers

/**
* Enhances Coupleable interface to also couple the values from neighbor elements
*
@@ -337,7 +337,7 @@ class MooseVariableFE : public MooseVariableFEBase
template <ComputeStage compute_stage>
const typename VariableValueType<OutputType, compute_stage>::type & adUDot()
{
_need_ad = true;
_need_ad = _need_ad_u_dot = true;
return _ad_u_dot;
}

@@ -429,7 +429,7 @@ class MooseVariableFE : public MooseVariableFEBase
template <ComputeStage compute_stage>
const typename VariableValueType<OutputType, compute_stage>::type & adUDotNeighbor()
{
_need_neighbor_ad = true;
_need_neighbor_ad = _need_neighbor_ad_u_dot = true;
return _neighbor_ad_u_dot;
}

@@ -804,6 +804,7 @@ class MooseVariableFE : public MooseVariableFEBase
bool _need_u_previous_nl;

bool _need_u_dot;
bool _need_ad_u_dot;
bool _need_u_dotdot;
bool _need_u_dot_old;
bool _need_u_dotdot_old;
@@ -839,6 +840,7 @@ class MooseVariableFE : public MooseVariableFEBase
bool _need_u_previous_nl_neighbor;

bool _need_u_dot_neighbor;
bool _need_neighbor_ad_u_dot;
bool _need_u_dotdot_neighbor;
bool _need_u_dot_old_neighbor;
bool _need_u_dotdot_old_neighbor;
@@ -181,9 +181,9 @@ ADDGKernel<compute_stage>::computeOffDiagElemNeighJacobian(Moose::DGJacobianType

size_t ad_offset = 0;
if (type == Moose::ElementElement || type == Moose::NeighborElement)
ad_offset = _var.number() * _sys.getMaxVarNDofsPerElem();
ad_offset = jvar * _sys.getMaxVarNDofsPerElem();
else
ad_offset = _var.number() * _sys.getMaxVarNDofsPerElem() +
ad_offset = jvar * _sys.getMaxVarNDofsPerElem() +
(_sys.system().n_vars() * _sys.getMaxVarNDofsPerElem());

for (_qp = 0; _qp < _qrule->n_points(); _qp++)
@@ -34,6 +34,7 @@ MooseVariableFE<OutputType>::MooseVariableFE(unsigned int var_num,
_need_u_older(false),
_need_u_previous_nl(false),
_need_u_dot(false),
_need_ad_u_dot(false),
_need_u_dotdot(false),
_need_u_dot_old(false),
_need_u_dotdot_old(false),
@@ -63,6 +64,7 @@ MooseVariableFE<OutputType>::MooseVariableFE(unsigned int var_num,
_need_u_older_neighbor(false),
_need_u_previous_nl_neighbor(false),
_need_u_dot_neighbor(false),
_need_neighbor_ad_u_dot(false),
_need_u_dotdot_neighbor(false),
_need_u_dot_old_neighbor(false),
_need_u_dotdot_old_neighbor(false),
@@ -1324,7 +1326,7 @@ void
MooseVariableFE<OutputType>::computeAD(
const unsigned int num_dofs,
const unsigned int nqp,
const bool is_transient,
const bool /*is_transient*/,
const FieldVariablePhiValue & phi,
const FieldVariablePhiGradient & grad_phi,
const FieldVariablePhiSecond *& second_phi,
@@ -1340,7 +1342,7 @@ MooseVariableFE<OutputType>::computeAD(
if (_need_ad_second_u)
_ad_second_u.resize(nqp);

if (is_transient)
if (_need_ad_u_dot)
{
_ad_dofs_dot.resize(num_dofs);
_ad_u_dot.resize(nqp);
@@ -1365,7 +1367,7 @@ MooseVariableFE<OutputType>::computeAD(
if (_need_ad_second_u)
_ad_second_u[qp] = _ad_zero;

if (is_transient)
if (_need_ad_u_dot)
_ad_u_dot[qp] = _ad_zero;
}

@@ -1377,7 +1379,7 @@ MooseVariableFE<OutputType>::computeAD(
if (_var_kind == Moose::VAR_NONLINEAR)
_ad_dof_values[i].derivatives()[ad_offset + i] = 1.0;

if (is_transient && _time_integrator)
if (_need_ad_u_dot && _time_integrator)
{
_ad_dofs_dot[i] = _ad_dof_values[i];
_time_integrator->computeADTimeDerivatives(_ad_dofs_dot[i], _dof_indices[i]);
@@ -1409,12 +1411,12 @@ MooseVariableFE<OutputType>::computeAD(
_ad_second_u[qp] += _ad_dof_values[i] * (*second_phi)[i][qp];
}

if (is_transient && _time_integrator)
if (_need_ad_u_dot && _time_integrator)
_ad_u_dot[qp] += phi[i][qp] * _ad_dofs_dot[i];
}
}

if (is_transient && !_time_integrator)
if (_need_ad_u_dot && !_time_integrator)
for (MooseIndex(nqp) qp = 0; qp < nqp; ++qp)
_ad_u_dot[qp] = _u_dot[qp];
}
@@ -1423,7 +1425,7 @@ template <typename OutputType>
void
MooseVariableFE<OutputType>::computeADNeighbor(const unsigned int num_dofs,
const unsigned int nqp,
const bool is_transient,
const bool /*is_transient*/,
const FieldVariablePhiValue & phi,
const FieldVariablePhiGradient & grad_phi,
const FieldVariablePhiSecond *& second_phi)
@@ -1439,6 +1441,9 @@ MooseVariableFE<OutputType>::computeADNeighbor(const unsigned int num_dofs,
if (_need_neighbor_ad_second_u)
_neighbor_ad_second_u.resize(nqp);

if (_need_neighbor_ad_u_dot)
_neighbor_ad_u_dot.resize(nqp);

// Derivatives are offset by the variable number
size_t ad_offset = _var_num * _sys.getMaxVarNDofsPerElem() +
(_sys.system().n_vars() * _sys.getMaxVarNDofsPerElem());
@@ -1459,7 +1464,7 @@ MooseVariableFE<OutputType>::computeADNeighbor(const unsigned int num_dofs,
if (_need_neighbor_ad_second_u)
_neighbor_ad_second_u[qp] = _ad_zero;

if (is_transient)
if (_need_neighbor_ad_u_dot)
_neighbor_ad_u_dot[qp] = _ad_zero;
}

@@ -1471,7 +1476,7 @@ MooseVariableFE<OutputType>::computeADNeighbor(const unsigned int num_dofs,
if (_var_kind == Moose::VAR_NONLINEAR)
_neighbor_ad_dof_values[i].derivatives()[ad_offset + i] = 1.0;

if (is_transient && _time_integrator)
if (_need_neighbor_ad_u_dot && _time_integrator)
{
_neighbor_ad_dofs_dot[i] = _neighbor_ad_dof_values[i];
_time_integrator->computeADTimeDerivatives(_neighbor_ad_dofs_dot[i],
@@ -1493,12 +1498,12 @@ MooseVariableFE<OutputType>::computeADNeighbor(const unsigned int num_dofs,
if (_need_neighbor_ad_second_u)
_neighbor_ad_second_u[qp] += _neighbor_ad_dof_values[i] * (*second_phi)[i][qp];

if (is_transient && _time_integrator)
if (_need_neighbor_ad_u_dot && _time_integrator)
_neighbor_ad_u_dot[qp] += phi[i][qp] * _neighbor_ad_dofs_dot[i];
}
}

if (is_transient && !_time_integrator)
if (_need_neighbor_ad_u_dot && !_time_integrator)
for (MooseIndex(nqp) qp = 0; qp < nqp; ++qp)
_neighbor_ad_u_dot[qp] = _u_dot_neighbor[qp];
}
@@ -0,0 +1,41 @@
//* 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

#ifndef ADDGCOUPLEDTEST_H
#define ADDGCOUPLEDTEST_H

#include "ADDGKernel.h"

// Forward Declarations
template <ComputeStage>
class ADDGCoupledTest;

declareADValidParams(ADDGCoupledTest);

/**
* This class is only currently used to test whether we can request neighbor AD calculations and not
* have anything go horribly wrong
*/
template <ComputeStage compute_stage>
class ADDGCoupledTest : public ADDGKernel<compute_stage>
{
public:
ADDGCoupledTest(const InputParameters & parameters);

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

MooseVariable & _v_var;
const ADVariableValue & _v;
const ADVariableValue & _v_neighbor;

usingDGKernelMembers;
};

#endif
@@ -0,0 +1,42 @@
//* 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 "ADDGCoupledTest.h"

// MOOSE includes
#include "MooseVariableFE.h"

#include "libmesh/utility.h"

registerADMooseObject("MooseTestApp", ADDGCoupledTest);

defineADValidParams(ADDGCoupledTest,
ADDGKernel,
params.addRequiredCoupledVar("v", "The coupling variable"););

template <ComputeStage compute_stage>
ADDGCoupledTest<compute_stage>::ADDGCoupledTest(const InputParameters & parameters)
: ADDGKernel<compute_stage>(parameters),
_v_var(dynamic_cast<MooseVariable &>(*getVar("v", 0))),
_v(_v_var.adSln<compute_stage>()),
_v_neighbor(_v_var.adSlnNeighbor<compute_stage>())
{
}

template <ComputeStage compute_stage>
ADResidual
ADDGCoupledTest<compute_stage>::computeQpResidual(Moose::DGResidualType type)
{
auto fake_flux = 5 * _u[_qp] - 4 * _u_neighbor[_qp] + 3 * _v[_qp] - 2 * _v_neighbor[_qp];

if (type == Moose::DGResidualType::Element)
return _test[_i][_qp] * fake_flux;
else
return _test_neighbor[_i][_qp] * -fake_flux;
}
@@ -0,0 +1,85 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 10
ny = 10
[]

[Variables]
[./u]
[../]
[v][]
[]

[Kernels]
[./diff]
type = CoefDiffusion
variable = u
coef = 0.1
[../]
[./time]
type = ADTimeDerivative
variable = u
[../]
[coupled]
type = ADCoupledValueTest
variable = u
v = v
[]
[v_diff]
type = Diffusion
variable = v
[]
[]

[DGKernels]
[dummy]
type = ADDGCoupledTest
variable = u
v = v
[]
[]

[BCs]
[./left]
type = DirichletBC
variable = u
boundary = left
value = 0
[../]
[./right]
type = DirichletBC
variable = u
boundary = right
value = 1
[../]
[./left_v]
type = DirichletBC
variable = v
boundary = left
value = 0
[../]
[./right_v]
type = DirichletBC
variable = v
boundary = right
value = 1
[../]
[]

[Executioner]
type = Transient
num_steps = 2
dt = 0.1
solve_type = NEWTON
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
[dof_map]
type = DOFMap
execute_on = 'initial'
[]
[]
Oops, something went wrong.

0 comments on commit 5af1254

Please sign in to comment.
You can’t perform that action at this time.