<commit_before>/****************************************************************/
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*          All contents are licensed under LGPL V2.1           */
/*             See LICENSE for full restrictions                */
/****************************************************************/
#include "ElasticEnergyMaterial.h"
#include "RankTwoTensor.h"
#include "ElasticityTensorR4.h"

template<>
InputParameters validParams<ElasticEnergyMaterial>()
{
  InputParameters params = validParams<DerivativeFunctionMaterialBase>();
  params.addClassDescription("Free energy material for the elastic energy contributions.");
  params.addParam<std::string>("base_name", "Material property base name");
  params.addRequiredCoupledVar("args", "Arguments of F() - use vector coupling");
  return params;
}

ElasticEnergyMaterial::ElasticEnergyMaterial(const InputParameters & parameters) :
    DerivativeFunctionMaterialBase(parameters),
    _base_name(isParamValid("base_name") ? getParam<std::string>("base_name") + "_" : "" ),
    _stress(getMaterialPropertyByName<RankTwoTensor>(_base_name + "stress")),
    _elasticity_tensor(getMaterialPropertyByName<ElasticityTensorR4>(_base_name + "elasticity_tensor")),
    _strain(getMaterialPropertyByName<RankTwoTensor>(_base_name + "elastic_strain"))
{
  _dstrain.resize(_nargs);
  _d2strain.resize(_nargs);
  _delasticity_tensor.resize(_nargs);
  _d2elasticity_tensor.resize(_nargs);

  // fetch stress and elasticity tensor derivatives (in simple eigenstrain models this is is only w.r.t. 'c')
  for (unsigned int i = 0; i < _nargs; ++i)
  {
    _dstrain[i]            = &getMaterialPropertyDerivativeByName<RankTwoTensor>(_base_name + "elastic_strain", _arg_names[i]);
    _delasticity_tensor[i] = &getMaterialPropertyDerivativeByName<ElasticityTensorR4>(_base_name + "elasticity_tensor", _arg_names[i]);

    _d2strain[i].resize(_nargs);
    _d2elasticity_tensor[i].resize(_nargs);

    for (unsigned int j = 0; j < _nargs; ++j)
    {
      _d2strain[i][j]            = &getMaterialPropertyDerivativeByName<RankTwoTensor>(_base_name + "elastic_strain", _arg_names[i], _arg_names[j]);
      _d2elasticity_tensor[i][j] = &getMaterialPropertyDerivativeByName<ElasticityTensorR4>(_base_name + "elasticity_tensor", _arg_names[i], _arg_names[j]);
    }
  }
}

Real
ElasticEnergyMaterial::computeF()
{
  return 0.5 * _stress[_qp].doubleContraction(_strain[_qp]);
}

Real
ElasticEnergyMaterial::computeDF(unsigned int i_var)
{
  unsigned int i = argIndex(i_var);

  // product rule d/di computeF
  return 0.5 * (
    ( // dstress/di
        (*_delasticity_tensor[i])[_qp] * _strain[_qp]
      + _elasticity_tensor[_qp] * (*_dstrain[i])[_qp]
    ).doubleContraction(_strain[_qp])

    + _stress[_qp].doubleContraction((*_dstrain[i])[_qp])
  );
}

Real
ElasticEnergyMaterial::computeD2F(unsigned int i_var, unsigned int j_var)
{
  unsigned int i = argIndex(i_var);
  unsigned int j = argIndex(j_var);

  // product rule d/dj computeDF
  return 0.5 * (
    (
        (*_d2elasticity_tensor[i][j])[_qp] * _strain[_qp]
      + (*_delasticity_tensor[i])[_qp] * (*_dstrain[j])[_qp]
      + (*_delasticity_tensor[j])[_qp] * (*_dstrain[i])[_qp]
      + _elasticity_tensor[_qp] * (*_d2strain[i][j])[_qp]
    ).doubleContraction(_strain[_qp])
    + (
        (*_delasticity_tensor[i])[_qp] * _strain[_qp]
      + _elasticity_tensor[_qp] * (*_dstrain[i])[_qp]
    ).doubleContraction((*_dstrain[j])[_qp])

    + ( // dstress/dj
          (*_delasticity_tensor[j])[_qp] * _strain[_qp]
        + _elasticity_tensor[_qp] * (*_dstrain[j])[_qp]
      ).doubleContraction((*_dstrain[i])[_qp])
    + _stress[_qp].doubleContraction((*_d2strain[i][j])[_qp])
  );
}
<commit_msg>Simplify first derivative of elastic free energy (#5631)<commit_after>/****************************************************************/
/* MOOSE - Multiphysics Object Oriented Simulation Environment  */
/*                                                              */
/*          All contents are licensed under LGPL V2.1           */
/*             See LICENSE for full restrictions                */
/****************************************************************/
#include "ElasticEnergyMaterial.h"
#include "RankTwoTensor.h"
#include "ElasticityTensorR4.h"

template<>
InputParameters validParams<ElasticEnergyMaterial>()
{
  InputParameters params = validParams<DerivativeFunctionMaterialBase>();
  params.addClassDescription("Free energy material for the elastic energy contributions.");
  params.addParam<std::string>("base_name", "Material property base name");
  params.addRequiredCoupledVar("args", "Arguments of F() - use vector coupling");
  return params;
}

ElasticEnergyMaterial::ElasticEnergyMaterial(const InputParameters & parameters) :
    DerivativeFunctionMaterialBase(parameters),
    _base_name(isParamValid("base_name") ? getParam<std::string>("base_name") + "_" : "" ),
    _stress(getMaterialPropertyByName<RankTwoTensor>(_base_name + "stress")),
    _elasticity_tensor(getMaterialPropertyByName<ElasticityTensorR4>(_base_name + "elasticity_tensor")),
    _strain(getMaterialPropertyByName<RankTwoTensor>(_base_name + "elastic_strain"))
{
  _dstrain.resize(_nargs);
  _d2strain.resize(_nargs);
  _delasticity_tensor.resize(_nargs);
  _d2elasticity_tensor.resize(_nargs);

  // fetch stress and elasticity tensor derivatives (in simple eigenstrain models this is is only w.r.t. 'c')
  for (unsigned int i = 0; i < _nargs; ++i)
  {
    _dstrain[i]            = &getMaterialPropertyDerivativeByName<RankTwoTensor>(_base_name + "elastic_strain", _arg_names[i]);
    _delasticity_tensor[i] = &getMaterialPropertyDerivativeByName<ElasticityTensorR4>(_base_name + "elasticity_tensor", _arg_names[i]);

    _d2strain[i].resize(_nargs);
    _d2elasticity_tensor[i].resize(_nargs);

    for (unsigned int j = 0; j < _nargs; ++j)
    {
      _d2strain[i][j]            = &getMaterialPropertyDerivativeByName<RankTwoTensor>(_base_name + "elastic_strain", _arg_names[i], _arg_names[j]);
      _d2elasticity_tensor[i][j] = &getMaterialPropertyDerivativeByName<ElasticityTensorR4>(_base_name + "elasticity_tensor", _arg_names[i], _arg_names[j]);
    }
  }
}

Real
ElasticEnergyMaterial::computeF()
{
  return 0.5 * _stress[_qp].doubleContraction(_strain[_qp]);
}

Real
ElasticEnergyMaterial::computeDF(unsigned int i_var)
{
  unsigned int i = argIndex(i_var);

  // product rule d/di computeF (doubleContraction commutes)
  return 0.5 * ((*_delasticity_tensor[i])[_qp] * _strain[_qp]).doubleContraction(_strain[_qp]) +
         (_elasticity_tensor[_qp] * (*_dstrain[i])[_qp]).doubleContraction(_strain[_qp]);
}

Real
ElasticEnergyMaterial::computeD2F(unsigned int i_var, unsigned int j_var)
{
  unsigned int i = argIndex(i_var);
  unsigned int j = argIndex(j_var);

  // product rule d/dj computeDF
  // TODO: simplify because doubleContraction commutes
  return 0.5 * (
    (
        (*_d2elasticity_tensor[i][j])[_qp] * _strain[_qp]
      + (*_delasticity_tensor[i])[_qp] * (*_dstrain[j])[_qp]
      + (*_delasticity_tensor[j])[_qp] * (*_dstrain[i])[_qp]
      + _elasticity_tensor[_qp] * (*_d2strain[i][j])[_qp]
    ).doubleContraction(_strain[_qp])
    + (
        (*_delasticity_tensor[i])[_qp] * _strain[_qp]
      + _elasticity_tensor[_qp] * (*_dstrain[i])[_qp]
    ).doubleContraction((*_dstrain[j])[_qp])

    + ( // dstress/dj
          (*_delasticity_tensor[j])[_qp] * _strain[_qp]
        + _elasticity_tensor[_qp] * (*_dstrain[j])[_qp]
      ).doubleContraction((*_dstrain[i])[_qp])
    + _stress[_qp].doubleContraction((*_d2strain[i][j])[_qp])
  );
}
{'fraction_non_alphanumeric': 0.1690927995840915, 'fraction_numerical': 0.006758513127112035, 'mean_word_length': 4.592296511627907, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7484552', 'n_tokens_mistral': 2624, 'n_tokens_neox': 2461, 'n_words': 389}