LCOV - code coverage report
Current view: top level - src/utils - InputParameterWarehouse.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 111 137 81.0 %
Date: 2025-07-17 01:28:37 Functions: 12 17 70.6 %
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             : // MOOSE includes
      11             : #include "InputParameterWarehouse.h"
      12             : #include "InputParameters.h"
      13             : 
      14       62765 : InputParameterWarehouse::InputParameterWarehouse()
      15       62765 :   : _input_parameters(libMesh::n_threads()), _controllable_items(libMesh::n_threads())
      16             : {
      17       62765 : }
      18             : 
      19             : InputParameters &
      20     4951033 : InputParameterWarehouse::addInputParameters(const std::string & name,
      21             :                                             const InputParameters & parameters,
      22             :                                             THREAD_ID tid,
      23             :                                             const AddRemoveParamsKey)
      24             : {
      25             :   // Error if the name contains "::"
      26     4951033 :   if (name.find("::") != std::string::npos)
      27           0 :     mooseError("The object name may not contain '::' in the name: ", name);
      28             : 
      29             :   // Create the actual InputParameters object that will be reference by the objects
      30     4951033 :   std::shared_ptr<InputParameters> ptr = std::make_shared<InputParameters>(parameters);
      31             : 
      32     4951033 :   auto base = ptr->get<std::string>("_moose_base");
      33             : 
      34             :   // The object name defined by the base class name, this method of storing is used for
      35             :   // determining the uniqueness of the name
      36     4951033 :   MooseObjectName unique_name(base, name, "::");
      37             : 
      38             :   // Check that the Parameters do not already exist. We allow duplicate unique_names for
      39             :   // MooseVariableBase objects because we require duplication of the variable for reference and
      40             :   // displaced problems. We must also have std::pair(reference_var, reference_params) AND
      41             :   // std::pair(displaced_var, displaced_params) elements because the two vars will have different
      42             :   // values for _sys. It's a good thing we are using a multi-map as our underlying storage.
      43             :   // We also allow duplicate unique_names for Action objects because it is allowed to have
      44             :   // multiple [Materials] input blocks each of which can add an action but all of these actions
      45             :   // will have the same unique name.
      46     4966070 :   if (_input_parameters[tid].find(unique_name) != _input_parameters[tid].end() &&
      47     4966070 :       base != "MooseVariableBase" && base != "Action")
      48           6 :     mooseError("A '",
      49           6 :                unique_name.tag(),
      50             :                "' object already exists with the name '",
      51           6 :                unique_name.name(),
      52             :                "'.\n");
      53             : 
      54             :   // Store the parameters according to the base name
      55     9902054 :   _input_parameters[tid].insert(
      56     9902054 :       std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(unique_name, ptr));
      57             : 
      58             :   // Build a list of object names
      59     4951027 :   std::vector<MooseObjectName> object_names;
      60     4951027 :   object_names.push_back(unique_name);
      61             : 
      62             :   // Store the object according to the control tags
      63     4951027 :   if (ptr->isParamValid("control_tags"))
      64             :   {
      65     1806691 :     const std::vector<std::string> & tags = ptr->get<std::vector<std::string>>("control_tags");
      66     3613481 :     for (const auto & tag : tags)
      67             :     {
      68     1806790 :       if (!tag.empty())
      69             :       {
      70      849616 :         auto short_name = MooseUtils::shortName(name);
      71     1699232 :         _input_parameters[tid].insert(std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(
      72     1699232 :             MooseObjectName(tag, short_name), ptr));
      73      849616 :         object_names.emplace_back(tag, short_name);
      74      849616 :       }
      75             :     }
      76             :   }
      77             : 
      78             :   // Store controllable parameters using all possible names
      79   116634596 :   for (libMesh::Parameters::iterator map_iter = ptr->begin(); map_iter != ptr->end(); ++map_iter)
      80             :   {
      81   111683569 :     const std::string & pname = map_iter->first;
      82   111683569 :     libMesh::Parameters::Value * value = MooseUtils::get(map_iter->second);
      83             : 
      84   111683569 :     if (ptr->isControllable(pname))
      85     2170078 :       for (const auto & object_name : object_names)
      86             :       {
      87     1317774 :         MooseObjectParameterName param_name(object_name, pname);
      88     1317774 :         _controllable_items[tid].emplace_back(std::make_shared<ControllableItem>(
      89             :             param_name, value, ptr->getControllableExecuteOnTypes(pname)));
      90     1317774 :       }
      91             :   }
      92             : 
      93             :   // Set the name and tid parameters, and unique_name
      94     4951027 :   std::stringstream oss;
      95     4951027 :   oss << unique_name;
      96             : 
      97     4951027 :   ptr->addPrivateParam<std::string>("_unique_name", oss.str());
      98     4951027 :   ptr->addPrivateParam<std::string>("_object_name", name);
      99     4951027 :   ptr->addPrivateParam<THREAD_ID>("_tid", tid);
     100             : 
     101             :   // no more copies allowed
     102     4951027 :   ptr->allowCopy(false);
     103             : 
     104             :   // Return a reference to the InputParameters object
     105     9902054 :   return *ptr;
     106     4951045 : }
     107             : 
     108             : void
     109      451897 : InputParameterWarehouse::removeInputParameters(const MooseObject & moose_object,
     110             :                                                THREAD_ID tid,
     111             :                                                const AddRemoveParamsKey)
     112             : {
     113      451897 :   auto moose_object_name_string = moose_object.parameters().get<std::string>("_unique_name");
     114      451897 :   MooseObjectName moose_object_name(moose_object_name_string);
     115      451897 :   _input_parameters[tid].erase(moose_object_name);
     116      451897 : }
     117             : 
     118             : const InputParameters &
     119           0 : InputParameterWarehouse::getInputParametersObject(const std::string & name, THREAD_ID tid) const
     120             : {
     121           0 :   return getInputParameters(MooseObjectName(name), tid);
     122             : }
     123             : 
     124             : const InputParameters &
     125          56 : InputParameterWarehouse::getInputParametersObject(const std::string & tag,
     126             :                                                   const std::string & name,
     127             :                                                   THREAD_ID tid) const
     128             : {
     129          56 :   return getInputParameters(MooseObjectName(tag, name), tid);
     130             : }
     131             : 
     132             : const InputParameters &
     133           0 : InputParameterWarehouse::getInputParametersObject(const MooseObjectName & object_name,
     134             :                                                   THREAD_ID tid) const
     135             : {
     136           0 :   return getInputParameters(object_name, tid);
     137             : }
     138             : 
     139             : InputParameters &
     140           0 : InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
     141             : {
     142           0 :   return getInputParameters(MooseObjectName(name), tid);
     143             : }
     144             : 
     145             : InputParameters &
     146           0 : InputParameterWarehouse::getInputParameters(const std::string & tag,
     147             :                                             const std::string & name,
     148             :                                             THREAD_ID tid) const
     149             : {
     150           0 :   return getInputParameters(MooseObjectName(tag, name), tid);
     151             : }
     152             : 
     153             : InputParameters &
     154          56 : InputParameterWarehouse::getInputParameters(const MooseObjectName & object_name,
     155             :                                             THREAD_ID tid) const
     156             : {
     157             :   // Locate the InputParameters object and error if it was not located
     158          56 :   const auto iter = _input_parameters[tid].find(object_name);
     159          56 :   if (iter == _input_parameters[tid].end())
     160           4 :     mooseError("Unknown InputParameters object ", object_name);
     161             : 
     162             :   // Return a reference to the parameter
     163         104 :   return *(iter->second.get());
     164             : }
     165             : 
     166             : const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
     167     2431127 : InputParameterWarehouse::getInputParameters(THREAD_ID tid) const
     168             : {
     169     2431127 :   return _input_parameters[tid];
     170             : }
     171             : 
     172             : void
     173       30882 : InputParameterWarehouse::addControllableParameterConnection(
     174             :     const MooseObjectParameterName & primary,
     175             :     const MooseObjectParameterName & secondary,
     176             :     bool error_on_empty /*=true*/)
     177             : {
     178       66552 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     179             :   {
     180       38063 :     std::vector<ControllableItem *> primaries = getControllableItems(primary, tid);
     181       38063 :     if (primaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
     182           0 :       mooseError("Unable to locate primary parameter with name ", primary);
     183       38063 :     else if (primaries.empty())
     184             :     {
     185        2396 :       if (tid == 0)
     186           0 :         return;
     187             :       else
     188             :         // try to connect non-threaded primary control to secondary controls of all threads
     189        2396 :         primaries = getControllableItems(primary, 0);
     190             :     }
     191             : 
     192       38063 :     std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
     193       38063 :     if (secondaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
     194           0 :       mooseError("Unable to locate secondary parameter with name ", secondary);
     195       38063 :     else if (secondaries.empty())
     196        2393 :       return;
     197             : 
     198       71342 :     for (auto primary_ptr : primaries)
     199       71347 :       for (auto secondary_ptr : secondaries)
     200       35675 :         if (primary_ptr != secondary_ptr)
     201       35675 :           primary_ptr->connect(secondary_ptr);
     202       40456 :   }
     203             : }
     204             : 
     205             : void
     206           0 : InputParameterWarehouse::addControllableObjectAlias(const MooseObjectName & alias,
     207             :                                                     const MooseObjectName & secondary)
     208             : {
     209           0 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     210             :   {
     211             :     std::vector<ControllableItem *> secondaries =
     212           0 :         getControllableItems(MooseObjectParameterName(secondary, "*"), tid);
     213           0 :     for (auto secondary_ptr : secondaries)
     214             :     {
     215           0 :       MooseObjectParameterName alias_param(alias, secondary_ptr->name().parameter());
     216           0 :       MooseObjectParameterName secondary_param(secondary, secondary_ptr->name().parameter());
     217           0 :       addControllableParameterAlias(alias_param, secondary_param);
     218           0 :     }
     219           0 :   }
     220           0 : }
     221             : 
     222             : void
     223          13 : InputParameterWarehouse::addControllableParameterAlias(const MooseObjectParameterName & alias,
     224             :                                                        const MooseObjectParameterName & secondary)
     225             : {
     226          27 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     227             :   {
     228          14 :     std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
     229          14 :     if (secondaries.empty() && tid == 0) // some objects only exist on tid 0
     230           0 :       mooseError("Unable to locate secondary parameter with name ", secondary);
     231             : 
     232          27 :     for (auto secondary_ptr : secondaries)
     233          26 :       _controllable_items[tid].emplace_back(
     234          26 :           std::make_unique<ControllableAlias>(alias, secondary_ptr));
     235          14 :   }
     236          13 : }
     237             : 
     238             : std::vector<ControllableItem *>
     239       78538 : InputParameterWarehouse::getControllableItems(const MooseObjectParameterName & input,
     240             :                                               THREAD_ID tid /*=0*/) const
     241             : {
     242       78538 :   std::vector<ControllableItem *> output;
     243     1294446 :   for (auto & ptr : _controllable_items[tid])
     244     1215908 :     if (ptr->name() == input)
     245       73753 :       output.push_back(ptr.get());
     246       78538 :   return output;
     247           0 : }
     248             : 
     249             : ControllableParameter
     250       10340 : InputParameterWarehouse::getControllableParameter(const MooseObjectParameterName & input) const
     251             : {
     252       10340 :   ControllableParameter cparam;
     253       21628 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     254      280413 :     for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
     255      269125 :       if ((*it)->name() == input)
     256       13641 :         cparam.add(it->get());
     257       10340 :   return cparam;
     258           0 : }
     259             : 
     260             : std::string
     261         180 : InputParameterWarehouse::dumpChangedControls(bool reset_changed) const
     262             : {
     263         180 :   std::stringstream oss;
     264         180 :   oss << std::left;
     265             : 
     266        8460 :   for (const auto & item : _controllable_items[0])
     267        8280 :     if (item->isChanged())
     268             :     {
     269         180 :       oss << item->dump(4);
     270         180 :       if (reset_changed)
     271          60 :         item->resetChanged();
     272             :     }
     273         360 :   return oss.str();
     274         180 : }
     275             : 
     276             : std::vector<MooseObjectParameterName>
     277       10025 : InputParameterWarehouse::getControllableParameterNames(const MooseObjectParameterName & input) const
     278             : {
     279       10025 :   std::vector<MooseObjectParameterName> names;
     280       21732 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     281      184551 :     for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
     282      172844 :       if ((*it)->name() == input)
     283       15409 :         names.push_back((*it)->name());
     284       10025 :   return names;
     285           0 : }

Generated by: LCOV version 1.14