forked from idaholab/moose
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test global map generation (idaholab#15450)
- Loading branch information
Showing
6 changed files
with
221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
77
test/tests/materials/ad_material/ad_global_index_mapping.i
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters