LCOV - code coverage report
Current view: top level - src/actions - CommonSolidMechanicsAction.C (source / functions) Hit Total Coverage
Test: idaholab/moose solid_mechanics: #31405 (292dce) with base fef103 Lines: 46 55 83.6 %
Date: 2025-09-04 07:57:23 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        5455 : CommonSolidMechanicsAction::validParams()
      22             : {
      23        5455 :   InputParameters params = QuasiStaticSolidMechanicsPhysicsBase::validParams();
      24        5455 :   params.addClassDescription("Store common solid mechanics parameters");
      25        5455 :   return params;
      26           0 : }
      27             : 
      28        5455 : CommonSolidMechanicsAction::CommonSolidMechanicsAction(const InputParameters & parameters)
      29        5455 :   : Action(parameters)
      30             : {
      31        5455 : }
      32             : 
      33             : void
      34       10888 : CommonSolidMechanicsAction::act()
      35             : {
      36             : 
      37             :   // check if sub-blocks block are found which will use the common parameters
      38       10888 :   auto actions = _awh.getActions<QuasiStaticSolidMechanicsPhysicsBase>();
      39       10888 :   if (actions.size() == 0)
      40           2 :     mooseWarning("Common parameters are supplied, but not used in ", parameters().blockLocation());
      41             : 
      42             :   // Add disp-variables
      43       10886 :   if (_current_task == "add_variable")
      44             :   {
      45             :     // We set these variables first with the "common" (not nested) level parameters
      46       10870 :     bool do_add_variables = getParam<bool>("add_variables");
      47       16305 :     auto displacement_variables = getParam<std::vector<VariableName>>("displacements");
      48             :     bool add_variables_block_restricted = true;
      49             :     std::set<SubdomainName> add_variables_blocks;
      50        5435 :     bool scaling_set = isParamValid("scaling");
      51        5435 :     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       10976 :     for (const auto & action : actions)
      55             :     {
      56       11082 :       if (action->getParam<bool>("add_variables"))
      57             :       {
      58             :         // this sub-action wants the disp-variables added.
      59             :         do_add_variables = true;
      60       15312 :         const auto v = action->getParam<std::vector<VariableName>>("displacements");
      61        5104 :         if (v.size() > 0)
      62             :         {
      63        5104 :           if (displacement_variables.size() == 0)
      64           0 :             displacement_variables.insert(displacement_variables.end(), v.begin(), v.end());
      65        5104 :           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       20416 :         if (add_variables_block_restricted && action->isParamValid("block") &&
      72       15312 :             action->getParam<std::vector<SubdomainName>>("block").size())
      73             :         {
      74        1038 :           auto action_blocks = action->getParam<std::vector<SubdomainName>>("block");
      75             :           mooseAssert(action_blocks.size(), "Block restriction must not be empty");
      76         346 :           add_variables_blocks.insert(action_blocks.cbegin(), action_blocks.cend());
      77         346 :         }
      78             :         else
      79             :           add_variables_block_restricted = false;
      80             : 
      81             :         // scaling?
      82       20416 :         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        5104 :       }
      95             :     }
      96             : 
      97             :     // add the disp-variables, if desired
      98        5435 :     if (do_add_variables)
      99             :     {
     100        4998 :       auto params = _factory.getValidParams("MooseVariable");
     101             :       // determine necessary order
     102        4998 :       const bool second = _problem->mesh().hasSecondOrderElements();
     103             : 
     104       14664 :       params.set<MooseEnum>("order") = second ? "SECOND" : "FIRST";
     105        9996 :       params.set<MooseEnum>("family") = "LAGRANGE";
     106        4998 :       if (add_variables_block_restricted && add_variables_blocks.size() > 0)
     107             :       {
     108         240 :         std::vector<SubdomainName> blks(add_variables_blocks.begin(), add_variables_blocks.end());
     109         240 :         params.set<std::vector<SubdomainName>>("block") = blks;
     110         240 :       }
     111        4998 :       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        4998 :       if (n == 0)
     117           0 :         paramError("displacements", "The vector of displacement variables is not set.");
     118        4998 :       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       18472 :       for (const auto & disp : displacement_variables)
     124             :         // Create displacement variables
     125       26948 :         _problem->addVariable("MooseVariable", disp, params);
     126        4998 :     }
     127        5435 :   }
     128       10886 : }

Generated by: LCOV version 1.14