LCOV - code coverage report
Current view: top level - src/actions - GrainGrowthAction.C (source / functions) Hit Total Coverage
Test: idaholab/moose phase_field: #31405 (292dce) with base fef103 Lines: 126 127 99.2 %
Date: 2025-09-04 07:55:36 Functions: 5 5 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 "GrainGrowthAction.h"
      11             : 
      12             : // MOOSE includes
      13             : #include "AddVariableAction.h"
      14             : #include "Conversion.h"
      15             : #include "FEProblem.h"
      16             : #include "Factory.h"
      17             : #include "MooseObjectAction.h"
      18             : #include "MooseMesh.h"
      19             : #include "NonlinearSystemBase.h"
      20             : 
      21             : #include "libmesh/string_to_enum.h"
      22             : 
      23             : using namespace libMesh;
      24             : 
      25             : registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_aux_variable");
      26             : registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_aux_kernel");
      27             : registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_variable");
      28             : registerMooseAction("PhaseFieldApp", GrainGrowthAction, "add_kernel");
      29             : registerMooseAction("PhaseFieldApp", GrainGrowthAction, "copy_nodal_vars");
      30             : registerMooseAction("PhaseFieldApp", GrainGrowthAction, "check_copy_nodal_vars");
      31             : 
      32             : InputParameters
      33         682 : GrainGrowthAction::validParams()
      34             : {
      35         682 :   InputParameters params = Action::validParams();
      36         682 :   params.addClassDescription(
      37             :       "Set up the variable and the kernels needed for a grain growth simulation");
      38        1364 :   params.addRequiredParam<unsigned int>("op_num",
      39             :                                         "specifies the number of order parameters to create");
      40        1364 :   params.addRequiredParam<std::string>("var_name_base", "specifies the base name of the variables");
      41        1364 :   params.addParam<Real>(
      42        1364 :       "scaling", 1.0, "Specifies a scaling factor to apply to the order parameters");
      43        1364 :   params.addParam<bool>(
      44             :       "initial_from_file",
      45        1364 :       false,
      46             :       "Take the initial condition of all polycrystal variables from the mesh file");
      47             : 
      48             :   // Get MooseEnums for the possible order/family options for this variable
      49         682 :   MooseEnum families(AddVariableAction::getNonlinearVariableFamilies());
      50         682 :   MooseEnum orders(AddVariableAction::getNonlinearVariableOrders());
      51        1364 :   params.addParam<MooseEnum>("family",
      52             :                              families,
      53             :                              "Specifies the family of FE "
      54             :                              "shape function to use for the order parameters");
      55        1364 :   params.addParam<MooseEnum>("order",
      56             :                              orders,
      57             :                              "Specifies the order of the FE "
      58             :                              "shape function to use for the order parameters");
      59             : 
      60        1364 :   params.addParam<MaterialPropertyName>(
      61             :       "mobility", "L", "The isotropic mobility used with the kernels");
      62        1364 :   params.addParam<MaterialPropertyName>("kappa", "kappa_op", "The kappa used with the kernels");
      63             : 
      64        1364 :   params.addParam<VariableName>("c", "Name of coupled concentration variable");
      65             : 
      66        1364 :   params.addParam<Real>("en_ratio", 1.0, "Ratio of surface to GB energy");
      67        1364 :   params.addParam<unsigned int>("ndef", 0, "Specifies the number of deformed grains to create");
      68        1364 :   params.addParam<bool>("variable_mobility",
      69        1364 :                         true,
      70             :                         "The mobility is a function of any MOOSE variable (if "
      71             :                         "this is set to false, L must be constant over the "
      72             :                         "entire domain!)");
      73        1364 :   params.addCoupledVar("args", "Vector of nonlinear variable arguments that L depends on");
      74        1364 :   params.deprecateCoupledVar("args", "coupled_variables", "02/09/2024");
      75             : 
      76        1364 :   params.addParam<bool>("implicit", true, "Whether kernels are implicit or not");
      77        1364 :   params.addParam<bool>(
      78        1364 :       "use_displaced_mesh", false, "Whether to use displaced mesh in the kernels");
      79        1364 :   params.addParam<bool>("use_automatic_differentiation",
      80        1364 :                         false,
      81             :                         "Flag to use automatic differentiation (AD) objects when possible");
      82        1364 :   params.addParam<std::vector<SubdomainName>>(
      83             :       "block", {}, "Block restriction for the variables and kernels");
      84             : 
      85        1364 :   params.addParamNamesToGroup("scaling implicit use_displaced_mesh", "Advanced");
      86        1364 :   params.addParamNamesToGroup("c en_ratio ndef", "Multiphysics");
      87             : 
      88         682 :   return params;
      89         682 : }
      90             : 
      91         116 : GrainGrowthAction::GrainGrowthAction(const InputParameters & params)
      92             :   : Action(params),
      93         116 :     _op_num(getParam<unsigned int>("op_num")),
      94         232 :     _var_name_base(getParam<std::string>("var_name_base")),
      95         348 :     _fe_type(Utility::string_to_enum<Order>(getParam<MooseEnum>("order")),
      96         116 :              Utility::string_to_enum<FEFamily>(getParam<MooseEnum>("family"))),
      97         232 :     _initial_from_file(getParam<bool>("initial_from_file")),
      98         348 :     _use_ad(getParam<bool>("use_automatic_differentiation"))
      99             : {
     100         116 : }
     101             : 
     102             : void
     103         498 : GrainGrowthAction::act()
     104             : {
     105             :   // Add Variables
     106         498 :   addVariables();
     107             : 
     108             :   // Add Kernels
     109        2922 :   for (unsigned int op = 0; op < _op_num; op++)
     110             :   {
     111             :     // Create variable name
     112        2424 :     std::string var_name = _var_name_base + Moose::stringify(op);
     113             : 
     114             :     // Add the kernels for each grain growth variable
     115        2424 :     if (_current_task == "add_kernel")
     116             :     {
     117             :       //
     118             :       // Add time derivative kernel
     119             :       //
     120             : 
     121             :       {
     122         778 :         std::string kernel_type = _use_ad ? "ADTimeDerivative" : "TimeDerivative";
     123             : 
     124         404 :         std::string kernel_name = var_name + "_" + kernel_type;
     125         404 :         InputParameters params = _factory.getValidParams(kernel_type);
     126         808 :         params.set<NonlinearVariableName>("variable") = var_name;
     127         404 :         params.applyParameters(parameters());
     128             : 
     129         404 :         _problem->addKernel(kernel_type, kernel_name, params);
     130         404 :       }
     131             : 
     132             :       //
     133             :       // Add ACGrGrPoly kernel
     134             :       //
     135             : 
     136             :       {
     137         778 :         std::string kernel_type = _use_ad ? "ADGrainGrowth" : "ACGrGrPoly";
     138             : 
     139             :         // Make vector of order parameter names, excluding this one
     140             :         std::vector<VariableName> v;
     141         404 :         v.resize(_op_num - 1);
     142             : 
     143             :         unsigned int ind = 0;
     144        3884 :         for (unsigned int j = 0; j < _op_num; ++j)
     145        3480 :           if (j != op)
     146        9228 :             v[ind++] = _var_name_base + Moose::stringify(j);
     147             : 
     148         404 :         std::string kernel_name = var_name + "_" + kernel_type;
     149         404 :         InputParameters params = _factory.getValidParams(kernel_type);
     150         808 :         params.set<NonlinearVariableName>("variable") = var_name;
     151         404 :         params.set<std::vector<VariableName>>("v") = v;
     152        1212 :         params.set<MaterialPropertyName>("mob_name") = getParam<MaterialPropertyName>("mobility");
     153         404 :         params.applyParameters(parameters());
     154             : 
     155         404 :         _problem->addKernel(kernel_type, kernel_name, params);
     156         808 :       }
     157             : 
     158             :       //
     159             :       // Add ACInterface kernel
     160             :       //
     161             : 
     162             :       {
     163         778 :         std::string kernel_type = _use_ad ? "ADACInterface" : "ACInterface";
     164             : 
     165         404 :         std::string kernel_name = var_name + "_" + kernel_type;
     166         404 :         InputParameters params = _factory.getValidParams(kernel_type);
     167         808 :         params.set<NonlinearVariableName>("variable") = var_name;
     168        1212 :         params.set<MaterialPropertyName>("mob_name") = getParam<MaterialPropertyName>("mobility");
     169        1212 :         params.set<MaterialPropertyName>("kappa_name") = getParam<MaterialPropertyName>("kappa");
     170         808 :         params.set<bool>("variable_L") = getParam<bool>("variable_mobility");
     171         404 :         params.applyParameters(parameters());
     172             : 
     173         404 :         _problem->addKernel(kernel_type, kernel_name, params);
     174         404 :       }
     175             : 
     176             :       //
     177             :       // Set up optional ACGBPoly bubble interaction kernels
     178             :       //
     179             : 
     180         808 :       if (isParamValid("c"))
     181             :       {
     182          22 :         if (_use_ad)
     183           0 :           mooseError("AD version of ACGBPoly is not implemented");
     184             : 
     185          22 :         std::string kernel_type = "ACGBPoly";
     186             : 
     187          22 :         std::string kernel_name = var_name + "_" + kernel_type;
     188          22 :         InputParameters params = _factory.getValidParams(kernel_type);
     189          44 :         params.set<NonlinearVariableName>("variable") = var_name;
     190          66 :         params.set<std::vector<VariableName>>("c") = {getParam<VariableName>("c")};
     191          22 :         params.applyParameters(parameters());
     192             : 
     193          22 :         _problem->addKernel(kernel_type, kernel_name, params);
     194          22 :       }
     195             :     }
     196             :   }
     197             : 
     198             :   // Add AuxVriable and AuxKernel for Bnds variable
     199         498 :   addBnds(_var_name_base);
     200         542 : }
     201             : 
     202             : void
     203         762 : GrainGrowthAction::addVariables()
     204             : {
     205        4506 :   for (unsigned int op = 0; op < _op_num; op++)
     206             :   {
     207             :     // Create variable name
     208        3744 :     std::string var_name = _var_name_base + Moose::stringify(op);
     209             : 
     210             :     // Setup initial from file if requested
     211        3744 :     if (_initial_from_file)
     212             :     {
     213         264 :       if (_current_task == "check_copy_nodal_vars")
     214          44 :         _app.setExodusFileRestart(true);
     215             : 
     216         264 :       if (_current_task == "copy_nodal_vars")
     217             :       {
     218          44 :         auto * system = &_problem->getNonlinearSystemBase(/*nl_sys_num=*/0);
     219          88 :         system->addVariableToCopy(var_name, var_name, "LATEST");
     220             :       }
     221             :     }
     222             : 
     223             :     // Add each grain growth variable
     224        3744 :     if (_current_task == "add_variable")
     225             :     {
     226         569 :       auto type = AddVariableAction::variableType(_fe_type);
     227         569 :       auto var_params = _factory.getValidParams(type);
     228             : 
     229         569 :       var_params.applySpecificParameters(_pars, {"family", "order", "block"});
     230        1707 :       var_params.set<std::vector<Real>>("scaling") = {getParam<Real>("scaling")};
     231             : 
     232             :       // Add variable
     233         569 :       _problem->addVariable(type, var_name, var_params);
     234         569 :     }
     235             :   }
     236         762 : }
     237             : 
     238             : void
     239         762 : GrainGrowthAction::addBnds(const std::string & name_base)
     240             : {
     241             :   // Create auxvariable
     242         762 :   if (_current_task == "add_aux_variable")
     243             :   {
     244         116 :     auto var_params = _factory.getValidParams("MooseVariable");
     245         232 :     var_params.set<MooseEnum>("family") = "LAGRANGE";
     246         232 :     var_params.set<MooseEnum>("order") = "FIRST";
     247         116 :     var_params.applySpecificParameters(_pars, {"block"});
     248         232 :     _problem->addAuxVariable("MooseVariable", "bnds", var_params);
     249         116 :   }
     250             :   // Create auxkernel
     251         646 :   else if (_current_task == "add_aux_kernel")
     252             :   {
     253             :     // Make vector of order parameter names, excluding this one std::vector<VariableName> v;
     254             :     std::vector<VariableName> v;
     255         116 :     v.resize(_op_num);
     256             : 
     257         685 :     for (unsigned int j = 0; j < _op_num; ++j)
     258        1707 :       v[j] = name_base + Moose::stringify(j);
     259             : 
     260         116 :     std::string aux_kernel_type = "BndsCalcAux";
     261             : 
     262         116 :     std::string aux_kernel_name = "bnds_" + aux_kernel_type;
     263         116 :     InputParameters params = _factory.getValidParams(aux_kernel_type);
     264         232 :     params.set<AuxVariableName>("variable") = "bnds";
     265         116 :     params.set<std::vector<VariableName>>("v") = v;
     266         464 :     params.set<ExecFlagEnum>("execute_on") = {EXEC_INITIAL, EXEC_TIMESTEP_END};
     267         116 :     params.applyParameters(parameters());
     268             : 
     269         116 :     _problem->addAuxKernel(aux_kernel_type, aux_kernel_name, params);
     270         232 :   }
     271         878 : }

Generated by: LCOV version 1.14