Skip to content

Commit

Permalink
Merge pull request #28440 from dewenyushu/neml2_inverse_opt
Browse files Browse the repository at this point in the history
Material inverse optimization using modular NEML2
  • Loading branch information
hugary1995 authored Sep 5, 2024
2 parents 9a6b276 + e696af1 commit 1926a67
Show file tree
Hide file tree
Showing 51 changed files with 1,539 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# GenericConstantSymmetricRankTwoTensor

!syntax description /Materials/GenericConstantSymmetricRankTwoTensor

## Overview

`GenericConstantSymmetricRankTwoTensor` creates a `SymmetricRankTwoTensor` material property that use
constant values to fill the tensor. The input of the constants follows the way how a `SymmetricRankTwoTensor` is filled.

This object functions similarly to the [GenericConstantRankTwoTensor](GenericConstantRankTwoTensor.md), but it leverages the symmetry property of the tensor for more efficient computation.

## Example Input File Syntax

!listing test/tests/materials/generic_materials/generic_constant_rank_two_tensor.i block=Materials/tensor

!syntax parameters /Materials/GenericConstantSymmetricRankTwoTensor

!syntax inputs /Materials/GenericConstantSymmetricRankTwoTensor

!syntax children /Materials/GenericConstantSymmetricRankTwoTensor
9 changes: 6 additions & 3 deletions framework/include/auxkernels/MaterialRankFourTensorAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
* property, for example stiffness, and output the value for the
* supplied indices.
*/

class MaterialRankFourTensorAux : public MaterialAuxBase<RankFourTensor>
template <bool is_ad>
class MaterialRankFourTensorAuxTempl : public MaterialAuxBaseTempl<RankFourTensor, is_ad>
{
public:
static InputParameters validParams();

MaterialRankFourTensorAux(const InputParameters & parameters);
MaterialRankFourTensorAuxTempl(const InputParameters & parameters);

protected:
virtual Real getRealValue() override;
Expand All @@ -35,3 +35,6 @@ class MaterialRankFourTensorAux : public MaterialAuxBase<RankFourTensor>
const unsigned int _l;
///@}
};

typedef MaterialRankFourTensorAuxTempl<false> MaterialRankFourTensorAux;
typedef MaterialRankFourTensorAuxTempl<true> ADMaterialRankFourTensorAux;
8 changes: 6 additions & 2 deletions framework/include/auxkernels/MaterialRealTensorValueAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
/**
* AuxKernel for outputting a RealTensorValue material property component to an AuxVariable
*/
class MaterialRealTensorValueAux : public MaterialAuxBase<RealTensorValue>
template <bool is_ad>
class MaterialRealTensorValueAuxTempl : public MaterialAuxBaseTempl<RealTensorValue, is_ad>
{
public:
static InputParameters validParams();
Expand All @@ -24,7 +25,7 @@ class MaterialRealTensorValueAux : public MaterialAuxBase<RealTensorValue>
* Class constructor
* @param parameters The input parameters for this AuxKernel
*/
MaterialRealTensorValueAux(const InputParameters & parameters);
MaterialRealTensorValueAuxTempl(const InputParameters & parameters);

protected:
virtual Real getRealValue() override;
Expand All @@ -35,3 +36,6 @@ class MaterialRealTensorValueAux : public MaterialAuxBase<RealTensorValue>
/// The column index to output
unsigned int _col;
};

typedef MaterialRealTensorValueAuxTempl<false> MaterialRealTensorValueAux;
typedef MaterialRealTensorValueAuxTempl<true> ADMaterialRealTensorValueAux;
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 "Material.h"
#include "SymmetricRankTwoTensor.h"

/**
* Declares a constant material property of type RankTwoTensor.
*/
template <bool is_ad>
class GenericConstantSymmetricRankTwoTensorTempl : public Material
{
public:
static InputParameters validParams();

GenericConstantSymmetricRankTwoTensorTempl(const InputParameters & parameters);

protected:
virtual void initQpStatefulProperties() override;
virtual void computeQpProperties() override;

SymmetricRankTwoTensor _tensor;
GenericMaterialProperty<SymmetricRankTwoTensor, is_ad> & _prop;
};

typedef GenericConstantSymmetricRankTwoTensorTempl<false> GenericConstantSymmetricRankTwoTensor;
typedef GenericConstantSymmetricRankTwoTensorTempl<true> ADGenericConstantSymmetricRankTwoTensor;
28 changes: 28 additions & 0 deletions framework/src/actions/MaterialOutputAction.C
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ MaterialOutputAction::materialOutput(const std::string & property_name,
material,
get_names_only);

else if (hasADProperty<RealTensorValue>(property_name))
names = outputHelper({"ADMaterialRealTensorValueAux", "012", {"row", "column"}},
property_name,
property_name + "_",
material,
get_names_only);

else if (hasProperty<RankTwoTensor>(property_name))
names = outputHelper({"MaterialRankTwoTensorAux", "012", {"i", "j"}},
property_name,
Expand All @@ -266,20 +273,41 @@ MaterialOutputAction::materialOutput(const std::string & property_name,
material,
get_names_only);

else if (hasADProperty<RankFourTensor>(property_name))
names = outputHelper({"ADMaterialRankFourTensorAux", "012", {"i", "j", "k", "l"}},
property_name,
property_name + "_",
material,
get_names_only);

else if (hasProperty<SymmetricRankTwoTensor>(property_name))
names = outputHelper({"MaterialSymmetricRankTwoTensorAux", "012345", {"component"}},
property_name,
property_name + "_",
material,
get_names_only);

else if (hasADProperty<SymmetricRankTwoTensor>(property_name))
names = outputHelper({"ADMaterialSymmetricRankTwoTensorAux", "012345", {"component"}},
property_name,
property_name + "_",
material,
get_names_only);

else if (hasProperty<SymmetricRankFourTensor>(property_name))
names = outputHelper({"MaterialSymmetricRankFourTensorAux", "012345", {"i", "j"}},
property_name,
property_name + "_",
material,
get_names_only);

else if (hasADProperty<SymmetricRankFourTensor>(property_name))
names = outputHelper({"ADMaterialSymmetricRankFourTensorAux", "012345", {"i", "j"}},
property_name,
property_name + "_",
material,
get_names_only);

// Functors
else if (hasFunctorProperty<Real>(property_name))
names = outputHelper({"FunctorMaterialRealAux", "", {}},
Expand Down
25 changes: 15 additions & 10 deletions framework/src/auxkernels/MaterialRankFourTensorAux.C
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include "MaterialRankFourTensorAux.h"

registerMooseObject("MooseApp", MaterialRankFourTensorAux);
registerMooseObject("MooseApp", ADMaterialRankFourTensorAux);

template <bool is_ad>
InputParameters
MaterialRankFourTensorAux::validParams()
MaterialRankFourTensorAuxTempl<is_ad>::validParams()
{
InputParameters params = MaterialAuxBase<>::validParams();
InputParameters params = MaterialAuxBaseTempl<RankFourTensor, is_ad>::validParams();
params.addClassDescription(
"Access a component of a RankFourTensor for automatic material property output");
params.addRequiredParam<unsigned int>("i", "The index i of ijkl for the tensor to output");
Expand All @@ -24,21 +26,24 @@ MaterialRankFourTensorAux::validParams()
return params;
}

MaterialRankFourTensorAux::MaterialRankFourTensorAux(const InputParameters & parameters)
: MaterialAuxBase<RankFourTensor>(parameters),
_i(getParam<unsigned int>("i")),
_j(getParam<unsigned int>("j")),
_k(getParam<unsigned int>("k")),
_l(getParam<unsigned int>("l"))
template <bool is_ad>
MaterialRankFourTensorAuxTempl<is_ad>::MaterialRankFourTensorAuxTempl(
const InputParameters & parameters)
: MaterialAuxBaseTempl<RankFourTensor, is_ad>(parameters),
_i(this->template getParam<unsigned int>("i")),
_j(this->template getParam<unsigned int>("j")),
_k(this->template getParam<unsigned int>("k")),
_l(this->template getParam<unsigned int>("l"))
{
mooseAssert(_i < LIBMESH_DIM, "i component out of range for current LIBMESH_DIM");
mooseAssert(_j < LIBMESH_DIM, "j component out of range for current LIBMESH_DIM");
mooseAssert(_k < LIBMESH_DIM, "k component out of range for current LIBMESH_DIM");
mooseAssert(_l < LIBMESH_DIM, "l component out of range for current LIBMESH_DIM");
}

template <bool is_ad>
Real
MaterialRankFourTensorAux::getRealValue()
MaterialRankFourTensorAuxTempl<is_ad>::getRealValue()
{
return _full_value(_i, _j, _k, _l);
return MetaPhysicL::raw_value(this->_full_value(_i, _j, _k, _l));
}
21 changes: 13 additions & 8 deletions framework/src/auxkernels/MaterialRealTensorValueAux.C
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@
#include "MaterialRealTensorValueAux.h"

registerMooseObject("MooseApp", MaterialRealTensorValueAux);
registerMooseObject("MooseApp", ADMaterialRealTensorValueAux);

template <bool is_ad>
InputParameters
MaterialRealTensorValueAux::validParams()
MaterialRealTensorValueAuxTempl<is_ad>::validParams()
{
InputParameters params = MaterialAuxBase<>::validParams();
InputParameters params = MaterialAuxBaseTempl<RealTensorValue, is_ad>::validParams();
params.addClassDescription("Object for extracting a component of a rank two tensor material "
"property to populate an auxiliary variable.");
params.addParam<unsigned int>("row", 0, "The row component to consider for this kernel");
params.addParam<unsigned int>("column", 0, "The column component to consider for this kernel");
return params;
}

MaterialRealTensorValueAux::MaterialRealTensorValueAux(const InputParameters & parameters)
: MaterialAuxBase<RealTensorValue>(parameters),
_row(getParam<unsigned int>("row")),
_col(getParam<unsigned int>("column"))
template <bool is_ad>
MaterialRealTensorValueAuxTempl<is_ad>::MaterialRealTensorValueAuxTempl(
const InputParameters & parameters)
: MaterialAuxBaseTempl<RealTensorValue, is_ad>(parameters),
_row(this->template getParam<unsigned int>("row")),
_col(this->template getParam<unsigned int>("column"))
{
if (_row > LIBMESH_DIM)
mooseError(
Expand All @@ -38,8 +42,9 @@ MaterialRealTensorValueAux::MaterialRealTensorValueAux(const InputParameters & p
" dimensional problems");
}

template <bool is_ad>
Real
MaterialRealTensorValueAux::getRealValue()
MaterialRealTensorValueAuxTempl<is_ad>::getRealValue()
{
return _full_value(_row, _col);
return MetaPhysicL::raw_value(this->_full_value(_row, _col));
}
55 changes: 55 additions & 0 deletions framework/src/materials/GenericConstantSymmetricRankTwoTensor.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//* 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 "GenericConstantSymmetricRankTwoTensor.h"

registerMooseObject("MooseApp", GenericConstantSymmetricRankTwoTensor);
registerMooseObject("MooseApp", ADGenericConstantSymmetricRankTwoTensor);

template <bool is_ad>
InputParameters
GenericConstantSymmetricRankTwoTensorTempl<is_ad>::validParams()
{
InputParameters params = Material::validParams();
params.addClassDescription(
"Object for declaring a constant symmetric rank two tensor as a material property.");
params.addRequiredParam<std::vector<Real>>(
"tensor_values", "Vector of values defining the constant rank two tensor");
params.addRequiredParam<MaterialPropertyName>(
"tensor_name", "Name of the tensor material property to be created");
params.set<MooseEnum>("constant_on") = "SUBDOMAIN";
return params;
}

template <bool is_ad>
GenericConstantSymmetricRankTwoTensorTempl<is_ad>::GenericConstantSymmetricRankTwoTensorTempl(
const InputParameters & parameters)
: Material(parameters),
_prop(declareGenericProperty<SymmetricRankTwoTensor, is_ad>(
getParam<MaterialPropertyName>("tensor_name")))
{
_tensor.fillFromInputVector(getParam<std::vector<Real>>("tensor_values"));
}

template <bool is_ad>
void
GenericConstantSymmetricRankTwoTensorTempl<is_ad>::initQpStatefulProperties()
{
GenericConstantSymmetricRankTwoTensorTempl<is_ad>::computeQpProperties();
}

template <bool is_ad>
void
GenericConstantSymmetricRankTwoTensorTempl<is_ad>::computeQpProperties()
{
_prop[_qp] = _tensor;
}

template class GenericConstantSymmetricRankTwoTensorTempl<false>;
template class GenericConstantSymmetricRankTwoTensorTempl<true>;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[Models]
[adjoint_elasticity_model]
type = LinearIsotropicElasticity
youngs_modulus = 7.5
poisson_ratio = 0.25
youngs_modulus = 5.0
poisson_ratio = 0.3
strain = 'forces/E'
[]
[forward_elasticity_model]
type = LinearIsotropicElasticity
youngs_modulus = 7.5
poisson_ratio = 0.25
youngs_modulus = 5.0
poisson_ratio = 0.3
strain = 'forces/E'
[]
[]
Loading

0 comments on commit 1926a67

Please sign in to comment.