LCOV - code coverage report
Current view: top level - src/actions - CommonSolidMechanicsAction.C (source / functions) Hit Total Coverage
Test: idaholab/moose solid_mechanics: #32971 (54bef8) with base c6cf66 Lines: 46 55 83.6 %
Date: 2026-05-29 20:40:07 Functions: 3 3 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 "CommonSolidMechanicsAction.h"
      11             : #include "QuasiStaticSolidMechanicsPhysics.h"
      12             : #include "ActionWarehouse.h"
      13             : #include "FEProblemBase.h"
      14             : #include "Factory.h"
      15             : 
      16             : registerMooseAction("SolidMechanicsApp", CommonSolidMechanicsAction, "meta_action");
      17             : 
      18             : registerMooseAction("SolidMechanicsApp", CommonSolidMechanicsAction, "add_variable");
      19             : 
      20             : InputParameters
      21        3052 : CommonSolidMechanicsAction::validParams()
      22             : {
      23        3052 :   InputParameters params = QuasiStaticSolidMechanicsPhysicsBase::validParams();
      24        3052 :   params.addClassDescription("Store common solid mechanics parameters");
      25        3052 :   return params;
      26           0 : }
      27             : 
      28        3052 : CommonSolidMechanicsAction::CommonSolidMechanicsAction(const InputParameters & parameters)
      29        3052 :   : Action(parameters)
      30             : {
      31        3052 : }
      32             : 
      33             : void
      34        6093 : CommonSolidMechanicsAction::act()
      35             : {
      36             : 
      37             :   // check if sub-blocks block are found which will use the common parameters
      38        6093 :   auto actions = _awh.getActions<QuasiStaticSolidMechanicsPhysicsBase>();
      39        6093 :   if (actions.size() == 0)
      40           1 :     mooseWarning("Common parameters are supplied, but not used in ", parameters().blockLocation());
      41             : 
      42             :   // Add disp-variables
      43        6092 :   if (_current_task == "add_variable")
      44             :   {
      45             :     // We set these variables first with the "common" (not nested) level parameters
      46        6084 :     bool do_add_variables = getParam<bool>("add_variables");
      47        9126 :     auto displacement_variables = getParam<std::vector<VariableName>>("displacements");
      48             :     bool add_variables_block_restricted = true;
      49             :     std::set<SubdomainName> add_variables_blocks;
      50        3042 :     bool scaling_set = isParamValid("scaling");
      51        3042 :     Real scaling_value = (scaling_set) ? getParam<Real>("scaling") : 1.0;
      52             : 
      53             :     // Check all nested sub-actions and update the variables keeping track of each selection
      54        6142 :     for (const auto & action : actions)
      55             :     {
      56        6200 :       if (action->getParam<bool>("add_variables"))
      57             :       {
      58             :         // this sub-action wants the disp-variables added.
      59             :         do_add_variables = true;
      60        8565 :         const auto v = action->getParam<std::vector<VariableName>>("displacements");
      61        2855 :         if (v.size() > 0)
      62             :         {
      63        2855 :           if (displacement_variables.size() == 0)
      64           0 :             displacement_variables.insert(displacement_variables.end(), v.begin(), v.end());
      65        2855 :           else if (displacement_variables != v)
      66           0 :             paramError("displacements",
      67             :                        "The vector of displacement variables of the actions differ.");
      68             :         }
      69             : 
      70             :         // with block-restriction?
      71       11420 :         if (add_variables_block_restricted && action->isParamValid("block") &&
      72        8565 :             action->getParam<std::vector<SubdomainName>>("block").size())
      73             :         {
      74         576 :           auto action_blocks = action->getParam<std::vector<SubdomainName>>("block");
      75             :           mooseAssert(action_blocks.size(), "Block restriction must not be empty");
      76         192 :           add_variables_blocks.insert(action_blocks.cbegin(), action_blocks.cend());
      77         192 :         }
      78             :         else
      79             :           add_variables_block_restricted = false;
      80             : 
      81             :         // scaling?
      82       11420 :         if (action->getParam<bool>("add_variables") && action->isParamValid("scaling"))
      83             :         {
      84           0 :           const auto scaling = action->getParam<Real>("scaling");
      85             : 
      86             :           // sanity-check
      87           0 :           if (scaling_set && scaling_value != scaling)
      88           0 :             paramError("scaling", "The scaling parameter of the actions differ.");
      89             : 
      90             :           // set local scaling variables
      91             :           scaling_set = true;
      92             :           scaling_value = scaling;
      93             :         }
      94        2855 :       }
      95             :     }
      96             : 
      97             :     // add the disp-variables, if desired
      98        3042 :     if (do_add_variables)
      99             :     {
     100        2797 :       auto params = _factory.getValidParams("MooseVariable");
     101             :       // determine necessary order
     102        2797 :       const bool second = _problem->mesh().hasSecondOrderElements();
     103             : 
     104        8212 :       params.set<MooseEnum>("order") = second ? "SECOND" : "FIRST";
     105        5594 :       params.set<MooseEnum>("family") = "LAGRANGE";
     106        2797 :       if (add_variables_block_restricted && add_variables_blocks.size() > 0)
     107             :       {
     108         134 :         std::vector<SubdomainName> blks(add_variables_blocks.begin(), add_variables_blocks.end());
     109         134 :         params.set<std::vector<SubdomainName>>("block") = blks;
     110         134 :       }
     111        2797 :       if (scaling_set && scaling_value != 1)
     112           0 :         params.set<std::vector<Real>>("scaling") = {scaling_value};
     113             : 
     114             :       // Loop through the displacement variables
     115             :       const auto n = displacement_variables.size();
     116        2797 :       if (n == 0)
     117           0 :         paramError("displacements", "The vector of displacement variables is not set.");
     118        2797 :       if (n != _problem->mesh().dimension())
     119           0 :         paramError("displacements",
     120             :                    "The number of displacement variables differs from the number of dimensions of "
     121             :                    "the mesh.");
     122             : 
     123       10347 :       for (const auto & disp : displacement_variables)
     124             :         // Create displacement variables
     125       15100 :         _problem->addVariable("MooseVariable", disp, params);
     126        2797 :     }
     127        3042 :   }
     128        6092 : }

Generated by: LCOV version 1.14