LCOV - code coverage report
Current view: top level - src/materials - DerivativeSumMaterial.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31730 (e8b711) with base e0c998 Lines: 74 78 94.9 %
Date: 2025-10-29 16:49:47 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       30472 : DerivativeSumMaterialTempl<is_ad>::validParams()
      20             : {
      21       30472 :   InputParameters params = DerivativeFunctionMaterialBaseTempl<is_ad>::validParams();
      22       60944 :   params.addClassDescription("Meta-material to sum up multiple derivative materials");
      23      121888 :   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      121888 :   params.addRequiredCoupledVar("coupled_variables", "Vector of names of variables being summed");
      28             : 
      29      121888 :   params.addCoupledVar("displacement_gradients",
      30             :                        "Vector of displacement gradient variables (see "
      31             :                        "Modules/PhaseField/DisplacementGradients "
      32             :                        "action)");
      33             : 
      34             :   // Advanced arguments to construct a sum of the form \f$ c+\gamma\sum_iF_i \f$
      35      121888 :   params.addParam<std::vector<Real>>("prefactor", {}, "Prefactor to multiply the sum term with.");
      36      121888 :   params.addParam<Real>("constant", 0.0, "Constant to be added to the prefactor multiplied sum.");
      37             : 
      38       91416 :   params.addParam<bool>("validate_coupling",
      39       60944 :                         true,
      40             :                         "Check if all variables the specified materials depend on are listed in "
      41             :                         "the `coupled_variables` parameter.");
      42       91416 :   params.addParamNamesToGroup("prefactor constant", "Advanced");
      43             : 
      44       30472 :   return params;
      45           0 : }
      46             : 
      47             : template <bool is_ad>
      48         564 : DerivativeSumMaterialTempl<is_ad>::DerivativeSumMaterialTempl(const InputParameters & parameters)
      49             :   : DerivativeFunctionMaterialBaseTempl<is_ad>(parameters),
      50         564 :     _sum_materials(this->template getParam<std::vector<std::string>>("sum_materials")),
      51         564 :     _num_materials(_sum_materials.size()),
      52        1128 :     _prefactor(_num_materials, 1.0),
      53        1128 :     _constant(this->template getParam<Real>("constant")),
      54        2256 :     _validate_coupling(this->template getParam<bool>("validate_coupling"))
      55             : {
      56             :   // we need at least one material in the sum
      57         564 :   if (_num_materials == 0)
      58           0 :     mooseError("Please supply at least one material to sum in DerivativeSumMaterial ", name());
      59             : 
      60             :   // get prefactor values if not 1.0
      61        1128 :   std::vector<Real> p = this->template getParam<std::vector<Real>>("prefactor");
      62             : 
      63             :   // if prefactor is used we need the same number of prefactors as sum materials
      64         564 :   if (_num_materials == p.size())
      65           0 :     _prefactor = p;
      66         564 :   else if (p.size() != 0)
      67           0 :     mooseError("Supply the same number of sum materials and prefactors.");
      68             : 
      69             :   // reserve space for summand material properties
      70         564 :   _summand_F.resize(_num_materials);
      71         564 :   _summand_dF.resize(_num_materials);
      72         564 :   _summand_d2F.resize(_num_materials);
      73         564 :   _summand_d3F.resize(_num_materials);
      74             : 
      75        1416 :   for (unsigned int n = 0; n < _num_materials; ++n)
      76             :   {
      77         852 :     _summand_F[n] = &this->template getGenericMaterialProperty<Real, is_ad>(_sum_materials[n]);
      78         852 :     _summand_dF[n].resize(_nargs);
      79         852 :     _summand_d2F[n].resize(_nargs);
      80         852 :     _summand_d3F[n].resize(_nargs);
      81             : 
      82        1704 :     for (unsigned int i = 0; i < _nargs; ++i)
      83             :     {
      84        4260 :       _summand_dF[n][i] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
      85         852 :           _sum_materials[n], _arg_names[i]);
      86         852 :       _summand_d2F[n][i].resize(_nargs);
      87             : 
      88         852 :       if (_third_derivatives)
      89         432 :         _summand_d3F[n][i].resize(_nargs);
      90             : 
      91        1704 :       for (unsigned int j = 0; j < _nargs; ++j)
      92             :       {
      93        2556 :         _summand_d2F[n][i][j] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
      94         852 :             _sum_materials[n], _arg_names[i], _arg_names[j]);
      95             : 
      96         852 :         if (_third_derivatives)
      97             :         {
      98         432 :           _summand_d3F[n][i][j].resize(_nargs);
      99             : 
     100         864 :           for (unsigned int k = 0; k < _nargs; ++k)
     101         432 :             _summand_d3F[n][i][j][k] = &this->template getMaterialPropertyDerivative<Real, is_ad>(
     102         432 :                 _sum_materials[n], _arg_names[i], _arg_names[j], _arg_names[k]);
     103             :         }
     104             :       }
     105             :     }
     106             :   }
     107         564 : }
     108             : 
     109             : template <bool is_ad>
     110             : void
     111         564 : DerivativeSumMaterialTempl<is_ad>::initialSetup()
     112             : {
     113         564 :   if (_validate_coupling)
     114        1128 :     for (unsigned int n = 0; n < _num_materials; ++n)
     115         636 :       this->template validateCoupling<Real>(_sum_materials[n]);
     116         564 : }
     117             : 
     118             : template <bool is_ad>
     119             : void
     120       67950 : DerivativeSumMaterialTempl<is_ad>::computeProperties()
     121             : {
     122             :   unsigned int i, j, k;
     123             : 
     124      339750 :   for (_qp = 0; _qp < _qrule->n_points(); _qp++)
     125             :   {
     126             :     // set function value
     127      271800 :     if (_prop_F)
     128             :     {
     129      271800 :       (*_prop_F)[_qp] = (*_summand_F[0])[_qp] * _prefactor[0];
     130      793400 :       for (unsigned int n = 1; n < _num_materials; ++n)
     131      521600 :         (*_prop_F)[_qp] += (*_summand_F[n])[_qp] * _prefactor[n];
     132             :     }
     133             : 
     134      543600 :     for (i = 0; i < _nargs; ++i)
     135             :     {
     136             :       // set first derivatives
     137      271800 :       if (_prop_dF[i])
     138             :       {
     139      271800 :         (*_prop_dF[i])[_qp] = (*_summand_dF[0][i])[_qp] * _prefactor[0];
     140      793400 :         for (unsigned int n = 1; n < _num_materials; ++n)
     141      521600 :           (*_prop_dF[i])[_qp] += (*_summand_dF[n][i])[_qp] * _prefactor[n];
     142             :       }
     143             : 
     144             :       // second derivatives
     145      543600 :       for (j = i; j < _nargs; ++j)
     146             :       {
     147      271800 :         if (_prop_d2F[i][j])
     148             :         {
     149      271800 :           (*_prop_d2F[i][j])[_qp] = (*_summand_d2F[0][i][j])[_qp] * _prefactor[0];
     150      793400 :           for (unsigned int n = 1; n < _num_materials; ++n)
     151      521600 :             (*_prop_d2F[i][j])[_qp] += (*_summand_d2F[n][i][j])[_qp] * _prefactor[n];
     152             :         }
     153             : 
     154             :         // third derivatives
     155      271800 :         if (_third_derivatives)
     156             :         {
     157      521600 :           for (k = j; k < _nargs; ++k)
     158      260800 :             if (_prop_d3F[i][j][k])
     159             :             {
     160      260800 :               (*_prop_d3F[i][j][k])[_qp] = (*_summand_d3F[0][i][j][k])[_qp] * _prefactor[0];
     161      782400 :               for (unsigned int n = 1; n < _num_materials; ++n)
     162      521600 :                 (*_prop_d3F[i][j][k])[_qp] += (*_summand_d3F[n][i][j][k])[_qp] * _prefactor[n];
     163             :             }
     164             :         }
     165             :       }
     166             :     }
     167             :   }
     168       67950 : }

Generated by: LCOV version 1.14