Skip to content

Commit

Permalink
add coupled var mat derivative idaholab#97
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnezdyur committed Dec 5, 2024
1 parent c34e31c commit 5879006
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/content/source/auxkernels/ADCoupledVarMaterialDerivativeAux.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ADCoupledVarMaterialDerivativeAux

## Description
The `AuxKernel` calculates the derivative of a material property with respect to a coupled variable using automatic differentiation. This kernel is particularly useful when you need to compute the sensitivity of a material property with respect to a given variable, which can be crucial for optimization, sensitivity analysis, or other numerical studies.

## Example Input File Syntax

!listing test/tests/auxkernels/coupledmatderivative/adcoupled_mat.i

!syntax parameters /UserObjects/ADCoupledVarMaterialDerivativeAux

!syntax inputs /UserObjects/ADCoupledVarMaterialDerivativeAux

!syntax children /UserObjects/ADCoupledVarMaterialDerivativeAux
36 changes: 36 additions & 0 deletions include/auxkernels/ADCoupledVarMaterialDerivativeAux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//* 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 "AuxiliarySystem.h"
#include "AuxKernel.h"
#include "MaterialProperty.h"
#include "MooseTypes.h"

/**
* Computes the derivative of a material property with respect to a coupled variable using
* automatic differentiation.
*/
class ADCoupledVarMaterialDerivativeAux : public AuxKernel
{
public:
static InputParameters validParams();

ADCoupledVarMaterialDerivativeAux(const InputParameters & parameters);

protected:
virtual Real computeValue() override;

/// The coupled variable we are taking the derivative with respect to
const MooseVariable & _coupled_var;

/// AD material for the derivate dM/du
const ADMaterialProperty<Real> & _ad_prop;
};
57 changes: 57 additions & 0 deletions src/auxkernels/ADCoupledVarMaterialDerivativeAux.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//* 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 "ADCoupledVarMaterialDerivativeAux.h"
#include "libmesh/libmesh_common.h"

registerMooseObject("isopodApp", ADCoupledVarMaterialDerivativeAux);

InputParameters
ADCoupledVarMaterialDerivativeAux::validParams()
{
InputParameters params = AuxKernel::validParams();
params.addClassDescription("An auxkernel that calculates the derivative of a material "
"property with respect to a coupled variable.");
params.addRequiredCoupledVar("coupled_var",
"The variable with respect to which the derivative is taken.");
params.addRequiredParam<MaterialPropertyName>(
"ad_prop_name", "The name of the material property to take the derivative of.");

return params;
}

ADCoupledVarMaterialDerivativeAux::ADCoupledVarMaterialDerivativeAux(
const InputParameters & parameters)
: AuxKernel(parameters),
_coupled_var(*getVar("coupled_var", 0)),
_ad_prop(getADMaterialProperty<Real>(getParam<MaterialPropertyName>("ad_prop_name")))
{
if (isNodal())
paramError("variable", "This AuxKernel only supports Elemental fields");
}

Real
ADCoupledVarMaterialDerivativeAux::computeValue()
{
const auto & dofs = _coupled_var.dofIndices();

const auto & phis = _coupled_var.phi();

auto mat_derivatives = _ad_prop[_qp].derivatives();

Real _derivative_value = 0;

for (const auto & dof : dofs)
{
for (const auto & phi : phis[_qp])
_derivative_value += phi * mat_derivatives[dof];
}

return _derivative_value;
}
88 changes: 88 additions & 0 deletions test/tests/auxkernels/coupledmatderivative/adcoupled_mat.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[Mesh]
[generated]
type = GeneratedMeshGenerator
dim = 2
nx = 5
ny = 5
[]
[]

[Variables]
[u]
[]
[]

[AuxVariables]
[dmat_du]
family = MONOMIAL
order = CONSTANT
[]
[]

[Kernels]
[diff]
type = Diffusion
variable = u
[]
[]

[BCs]
[left]
type = DirichletBC
variable = u
value = 0
boundary = left
[]
[right]
type = DirichletBC
variable = u
value = 1
boundary = right
[]
[]

[AuxKernels]
[deriv_aux]
type = ADCoupledVarMaterialDerivativeAux
variable = dmat_du
ad_prop_name = parsed_mat
coupled_var = u
[]
[]

[Materials]
[parsed_mat]
type = ADParsedMaterial
coupled_variables = 'u'
property_name = parsed_mat
extra_symbols = 'x'
expression = 'u*u + u*x'
[]
[parsed_mat_deriv_exact]
type = ADParsedMaterial
coupled_variables = 'u'
property_name = parsed_mat_deriv_exact
expression = '2*u + x'
extra_symbols = 'x'
output_properties = parsed_mat_deriv_exact
outputs = exodus
[]
[]

[Executioner]
type = Steady
[]

[Postprocessors]
[difference]
type = ElementL2Difference
variable = dmat_du
other_variable = parsed_mat_deriv_exact
execute_on = TIMESTEP_END
[]
[]

[Outputs]
exodus = true
execute_on = 'TIMESTEP_END'
[]
Binary file not shown.
8 changes: 8 additions & 0 deletions test/tests/auxkernels/coupledmatderivative/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Tests]
[coupled_mat]
requirement = "This tests that an auxkernel can compute the derivative of a material with respect to a variable."
type = 'Exodiff'
input = 'adcoupled_mat.i'
exodiff = 'adcoupled_mat.e'
[]
[]

0 comments on commit 5879006

Please sign in to comment.