LCOV - code coverage report
Current view: top level - src/materials - DerivativeSumMaterial.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 75 79 94.9 %
Date: 2025-07-17 01:28:37 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "DerivativeSumMaterial.h"
      11             : 
      12             : #include "libmesh/quadrature.h"
      13             : 
      14             : registerMooseObject("MooseApp", DerivativeSumMaterial);
      15             : registerMooseObject("MooseApp", ADDerivativeSumMaterial);
      16             : 
      17             : template <bool is_ad>
      18             : InputParameters
      19       29212 : DerivativeSumMaterialTempl<is_ad>::validParams()
      20             : {
      21       29212 :   InputParameters params = DerivativeFunctionMaterialBaseTempl<is_ad>::validParams();
      22       29212 :   params.addClassDescription("Meta-material to sum up multiple derivative materials");
      23       29212 :   params.addParam<std::vector<std::string>>("sum_materials",
      24             :                                             "Base name of the parsed sum material property");
      25             : 
      26             :   // All arguments of the parsed expression (free energy) being summed
      27       29212 :   params.addRequiredCoupledVar("args", "Vector of names of variables being summed");
      28       29212 :   params.deprecateCoupledVar("args", "coupled_variables", "02/07/2024");
      29             : 
      30       29212 :   params.addCoupledVar("displacement_gradients",
      31             :                        "Vector of displacement gradient variables (see "
      32             :                        "Modules/PhaseField/DisplacementGradients "
      33             :                        "action)");
      34             : 
      35             :   // Advanced arguments to construct a sum of the form \f$ c+\gamma\sum_iF_i \f$
      36       29212 :   params.addParam<std::vector<Real>>("prefactor", {}, "Prefactor to multiply the sum term with.");
      37       29212 :   params.addParam<Real>("constant", 0.0, "Constant to be added to the prefactor multiplied sum.");
      38             : 
      39       87636 :   params.addParam<bool>("validate_coupling",
      40       58424 :                         true,
      41             :                         "Check if all variables the specified materials depend on are listed in "
      42             :                         "the `coupled_variables` parameter.");
      43       29212 :   params.addParamNamesToGroup("prefactor constant", "Advanced");
      44             : 
      45       29212 :   return params;
      46           0 : }
      47             : 
      48             : template <bool is_ad>
      49         522 : DerivativeSumMaterialTempl<is_ad>::DerivativeSumMaterialTempl(const InputParameters & parameters)
      50             :   : DerivativeFunctionMaterialBaseTempl<is_ad>(parameters),
      51         522 :     _sum_materials(this->template getParam<std::vector<std::string>>("sum_materials")),
      52         522 :     _num_materials(_sum_materials.size()),
      53         522 :     _prefactor(_num_materials, 1.0),
      54         522 :     _constant(this->template getParam<Real>("constant")),
      55        1044 :     _validate_coupling(this->template getParam<bool>("validate_coupling"))
      56             : {
      57             :   // we need at least one material in the sum
      58         522 :   if (_num_materials == 0)
      59           0 :     mooseError("Please supply at least one material to sum in DerivativeSumMaterial ", name());
      60             : 
      61             :   // get prefactor values if not 1.0
      62         522 :   std::vector<Real> p = this->template getParam<std::vector<Real>>("prefactor");
      63             : 
      64             :   // if prefactor is used we need the same number of prefactors as sum materials
      65         522 :   if (_num_materials == p.size())
      66           0 :     _prefactor = p;
      67         522 :   else if (p.size() != 0)
      68           0 :     mooseError("Supply the same number of sum materials and prefactors.");
      69             : 
      70             :   // reserve space for summand material properties
      71         522 :   _summand_F.resize(_num_materials);
      72         522 :   _summand_dF.resize(_num_materials);
      73         522 :   _summand_d2F.resize(_num_materials);
      74         522 :   _summand_d3F.resize(_num_materials);
      75             : 
      76        1308 :   for (unsigned int n = 0; n < _num_materials; ++n)
      77             :   {
      78         786 :     _summand_F[n] = &this->template getGenericMaterialProperty<Real, is_ad>(_sum_materials[n]);
      79         786 :     _summand_dF[n].resize(_nargs);
      80         786 :     _summand_d2F[n].resize(_nargs);
      81         786 :     _summand_d3F[n].resize(_nargs);
      82             : 
      83        1572 :     for (unsigned int i = 0; i < _nargs; ++i)
      84             :     {
      85        1572 :       _summand_dF[n][i] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
      86         786 :           _sum_materials[n], _arg_names[i]);
      87         786 :       _summand_d2F[n][i].resize(_nargs);
      88             : 
      89         786 :       if (_third_derivatives)
      90         396 :         _summand_d3F[n][i].resize(_nargs);
      91             : 
      92        1572 :       for (unsigned int j = 0; j < _nargs; ++j)
      93             :       {
      94        1572 :         _summand_d2F[n][i][j] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
      95         786 :             _sum_materials[n], _arg_names[i], _arg_names[j]);
      96             : 
      97         786 :         if (_third_derivatives)
      98             :         {
      99         396 :           _summand_d3F[n][i][j].resize(_nargs);
     100             : 
     101         792 :           for (unsigned int k = 0; k < _nargs; ++k)
     102         396 :             _summand_d3F[n][i][j][k] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
     103         396 :                 _sum_materials[n], _arg_names[i], _arg_names[j], _arg_names[k]);
     104             :         }
     105             :       }
     106             :     }
     107             :   }
     108         522 : }
     109             : 
     110             : template <bool is_ad>
     111             : void
     112         522 : DerivativeSumMaterialTempl<is_ad>::initialSetup()
     113             : {
     114         522 :   if (_validate_coupling)
     115        1044 :     for (unsigned int n = 0; n < _num_materials; ++n)
     116         588 :       this->template validateCoupling<Real>(_sum_materials[n]);
     117         522 : }
     118             : 
     119             : template <bool is_ad>
     120             : void
     121       55100 : DerivativeSumMaterialTempl<is_ad>::computeProperties()
     122             : {
     123             :   unsigned int i, j, k;
     124             : 
     125      275500 :   for (_qp = 0; _qp < _qrule->n_points(); _qp++)
     126             :   {
     127             :     // set function value
     128      220400 :     if (_prop_F)
     129             :     {
     130      220400 :       (*_prop_F)[_qp] = (*_summand_F[0])[_qp] * _prefactor[0];
     131      642800 :       for (unsigned int n = 1; n < _num_materials; ++n)
     132      422400 :         (*_prop_F)[_qp] += (*_summand_F[n])[_qp] * _prefactor[n];
     133             :     }
     134             : 
     135      440800 :     for (i = 0; i < _nargs; ++i)
     136             :     {
     137             :       // set first derivatives
     138      220400 :       if (_prop_dF[i])
     139             :       {
     140      220400 :         (*_prop_dF[i])[_qp] = (*_summand_dF[0][i])[_qp] * _prefactor[0];
     141      642800 :         for (unsigned int n = 1; n < _num_materials; ++n)
     142      422400 :           (*_prop_dF[i])[_qp] += (*_summand_dF[n][i])[_qp] * _prefactor[n];
     143             :       }
     144             : 
     145             :       // second derivatives
     146      440800 :       for (j = i; j < _nargs; ++j)
     147             :       {
     148      220400 :         if (_prop_d2F[i][j])
     149             :         {
     150      220400 :           (*_prop_d2F[i][j])[_qp] = (*_summand_d2F[0][i][j])[_qp] * _prefactor[0];
     151      642800 :           for (unsigned int n = 1; n < _num_materials; ++n)
     152      422400 :             (*_prop_d2F[i][j])[_qp] += (*_summand_d2F[n][i][j])[_qp] * _prefactor[n];
     153             :         }
     154             : 
     155             :         // third derivatives
     156      220400 :         if (_third_derivatives)
     157             :         {
     158      422400 :           for (k = j; k < _nargs; ++k)
     159      211200 :             if (_prop_d3F[i][j][k])
     160             :             {
     161      211200 :               (*_prop_d3F[i][j][k])[_qp] = (*_summand_d3F[0][i][j][k])[_qp] * _prefactor[0];
     162      633600 :               for (unsigned int n = 1; n < _num_materials; ++n)
     163      422400 :                 (*_prop_d3F[i][j][k])[_qp] += (*_summand_d3F[n][i][j][k])[_qp] * _prefactor[n];
     164             :             }
     165             :         }
     166             :       }
     167             :     }
     168             :   }
     169       55100 : }

Generated by: LCOV version 1.14