Skip to content

Commit

Permalink
Test global map generation (idaholab#15450)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayad committed Jun 11, 2020
1 parent 0b8daa6 commit 6ee2830
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
2 changes: 1 addition & 1 deletion framework/include/utils/ADUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ globalDofIndexToDerivative(const T & ad_real_container,
std::vector<std::unordered_map<dof_id_type, Real>> ret_val(ad_real_container.size());

for (MooseIndex(ad_real_container) i = 0; i < ad_real_container.size(); ++i)
ret_val[i] = globalDofIndexToDerivative(ad_real_container[i], sys, elem);
ret_val[i] = globalDofIndexToDerivative(ad_real_container[i], sys, elem, elem_type);

return ret_val;
}
Expand Down
35 changes: 35 additions & 0 deletions test/include/materials/ADCheckGlobalToDerivativeMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//* 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 "ADMaterial.h"

/**
* A material that couples a material property
*/
class ADCheckGlobalToDerivativeMap : public ADMaterial
{
public:
static InputParameters validParams();

ADCheckGlobalToDerivativeMap(const InputParameters & parameters);

void computeProperties() override;

protected:
virtual void computeQpProperties() override;

ADMaterialProperty<Real> & _mat_prop;

const ADVariableValue & _u;
const ADVariableValue & _v;
const MooseVariable * _u_var;
const MooseVariable * _v_var;
};
101 changes: 101 additions & 0 deletions test/src/materials/ADCheckGlobalToDerivativeMap.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//* 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 "ADCheckGlobalToDerivativeMap.h"
#include "ADUtils.h"
#include "FEProblemBase.h"
#include "NonlinearSystemBase.h"
#include "libmesh/system.h"
#include "libmesh/dof_map.h"

registerMooseObject("MooseTestApp", ADCheckGlobalToDerivativeMap);

InputParameters
ADCheckGlobalToDerivativeMap::validParams()
{
InputParameters params = Material::validParams();
params.addRequiredCoupledVar("u", "A variable u");
params.addRequiredCoupledVar("v", "A variable v");
params.addRequiredParam<MaterialPropertyName>("mat_prop",
"Name of the ad property this material defines");
return params;
}

ADCheckGlobalToDerivativeMap::ADCheckGlobalToDerivativeMap(const InputParameters & parameters)
: Material(parameters),
_mat_prop(declareADProperty<Real>(getParam<MaterialPropertyName>("mat_prop"))),
_u(adCoupledValue("u")),
_v(adCoupledValue("v")),
_u_var(getVar("u", 0)),
_v_var(getVar("v", 0))
{
}

void
ADCheckGlobalToDerivativeMap::computeProperties()
{
Material::computeProperties();

if (_fe_problem.currentlyComputingJacobian())
{
const auto & moose_nl_system = _fe_problem.getNonlinearSystemBase();

const auto global_index_to_deriv_map =
Moose::globalDofIndexToDerivative(_mat_prop, moose_nl_system, _current_elem);

const DofMap & dof_map = moose_nl_system.system().get_dof_map();

std::vector<dof_id_type> u_dof_indices, v_dof_indices;
dof_map.dof_indices(_current_elem, u_dof_indices, _u_var->number());
dof_map.dof_indices(_current_elem, v_dof_indices, _v_var->number());

const auto nqp = _qrule->n_points();
mooseAssert(global_index_to_deriv_map.size() == nqp,
"The map should have the same size as the number of quadrature points");

auto max_dofs_per_elem = moose_nl_system.getMaxVarNDofsPerElem();

const auto u_offset = Moose::adOffset(_u_var->number(), max_dofs_per_elem);
const auto v_offset = Moose::adOffset(_v_var->number(), max_dofs_per_elem);

for (MooseIndex(nqp) qp = 0; qp < nqp; ++qp)
{
const auto & map_at_qp = global_index_to_deriv_map[qp];
const ADReal & qp_ad_real = _mat_prop[qp];

for (MooseIndex(u_dof_indices) u_local_index = 0; u_local_index < u_dof_indices.size();
++u_local_index)
{
auto it = map_at_qp.find(u_dof_indices[u_local_index]);
mooseAssert(it != map_at_qp.end(), "The global index key was not found!");

mooseAssert(MooseUtils::absoluteFuzzyEqual(
it->second, qp_ad_real.derivatives()[u_offset + u_local_index]),
"The derivative values don't match!");
}

for (MooseIndex(v_dof_indices) v_local_index = 0; v_local_index < v_dof_indices.size();
++v_local_index)
{
auto it = map_at_qp.find(v_dof_indices[v_local_index]);
mooseAssert(it != map_at_qp.end(), "The global index key was not found!");

mooseAssert(MooseUtils::absoluteFuzzyEqual(
it->second, qp_ad_real.derivatives()[v_offset + v_local_index]),
"The derivative values don't match!");
}
}
}
}

void
ADCheckGlobalToDerivativeMap::computeQpProperties()
{
_mat_prop[_qp] = 1. + std::pow(_u[_qp], 2) * std::pow(_v[_qp], 3);
}
77 changes: 77 additions & 0 deletions test/tests/materials/ad_material/ad_global_index_mapping.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 2
ny = 2
second_order = true
[]

[Variables]
[u]
initial_condition = 1
[]
[v]
initial_condition = 1
order = SECOND
[]
[]

[Kernels]
[u_diff]
type = ADMatDiffusion
variable = u
diffusivity = diffusivity
[]
[v_diff]
type = ADMatDiffusion
variable = v
diffusivity = diffusivity
[]
[]

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

[Materials]
[ad_coupled_mat]
type = ADCheckGlobalToDerivativeMap
u = u
v = v
mat_prop = diffusivity
[]
[]

[Executioner]
type = Steady
solve_type = 'Newton'
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
[]
Binary file not shown.
7 changes: 7 additions & 0 deletions test/tests/materials/ad_material/tests
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@
ratio_tol = 1e-7
difference_tol = 1e-5
[../]
[global_index_derivative_map]
type = 'Exodiff'
input = 'ad_global_index_mapping.i'
exodiff = 'ad_global_index_mapping_out.e'
requirement = 'For a given dual number, the system shall be able to construct a map from global degree of freedom index to the corresponding derivative'
issues = '#15450'
[]
[]

0 comments on commit 6ee2830

Please sign in to comment.