LCOV - code coverage report
Current view: top level - src/utils - InputParameterWarehouse.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 3501bd Lines: 111 137 81.0 %
Date: 2025-09-04 20:01:23 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       68470 : InputParameterWarehouse::InputParameterWarehouse()
      15      273880 :   : _input_parameters(libMesh::n_threads()), _controllable_items(libMesh::n_threads())
      16             : {
      17       68470 : }
      18             : 
      19             : InputParameters &
      20     5467224 : 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     5467224 :   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     5467224 :   std::shared_ptr<InputParameters> ptr = std::make_shared<InputParameters>(parameters);
      31             : 
      32     5467224 :   const auto & base = ptr->getBase();
      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     5467224 :   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     5467224 :   if (_input_parameters[tid].find(unique_name) != _input_parameters[tid].end() &&
      47     5467224 :       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    10934436 :   _input_parameters[tid].insert(
      56    10934436 :       std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(unique_name, ptr));
      57             : 
      58             :   // Build a list of object names
      59     5467218 :   std::vector<MooseObjectName> object_names;
      60     5467218 :   object_names.push_back(unique_name);
      61             : 
      62             :   // Store the object according to the control tags
      63    16401654 :   if (ptr->isParamValid("control_tags"))
      64             :   {
      65     2007528 :     const std::vector<std::string> & tags = ptr->get<std::vector<std::string>>("control_tags");
      66     4015163 :     for (const auto & tag : tags)
      67             :     {
      68     2007635 :       if (!tag.empty())
      69             :       {
      70      928811 :         auto short_name = MooseUtils::shortName(name);
      71     1857622 :         _input_parameters[tid].insert(std::pair<MooseObjectName, std::shared_ptr<InputParameters>>(
      72     1857622 :             MooseObjectName(tag, short_name), ptr));
      73      928811 :         object_names.emplace_back(tag, short_name);
      74      928811 :       }
      75             :     }
      76             :   }
      77             : 
      78             :   // Store controllable parameters using all possible names
      79   134959029 :   for (libMesh::Parameters::iterator map_iter = ptr->begin(); map_iter != ptr->end(); ++map_iter)
      80             :   {
      81   129491811 :     const std::string & pname = map_iter->first;
      82   129491811 :     libMesh::Parameters::Value * value = MooseUtils::get(map_iter->second);
      83             : 
      84   129491811 :     if (ptr->isControllable(pname))
      85     2371707 :       for (const auto & object_name : object_names)
      86             :       {
      87     1440618 :         MooseObjectParameterName param_name(object_name, pname);
      88     1440618 :         _controllable_items[tid].emplace_back(std::make_shared<ControllableItem>(
      89             :             param_name, value, ptr->getControllableExecuteOnTypes(pname)));
      90     1440618 :       }
      91             :   }
      92             : 
      93             :   // Set the name and tid parameters, and unique_name
      94     5467218 :   std::stringstream oss;
      95     5467218 :   oss << unique_name;
      96             : 
      97     5467218 :   ptr->addPrivateParam<std::string>(MooseBase::unique_name_param, oss.str());
      98     5467218 :   ptr->addPrivateParam<std::string>(MooseBase::name_param, name);
      99    10934436 :   ptr->addPrivateParam<THREAD_ID>("_tid", tid);
     100             : 
     101             :   // no more copies allowed
     102     5467218 :   ptr->allowCopy(false);
     103             : 
     104             :   // Return a reference to the InputParameters object
     105    10934436 :   return *ptr;
     106     5467230 : }
     107             : 
     108             : void
     109      491687 : InputParameterWarehouse::removeInputParameters(const MooseObject & moose_object,
     110             :                                                THREAD_ID tid,
     111             :                                                const AddRemoveParamsKey)
     112             : {
     113             :   auto moose_object_name_string =
     114      491687 :       moose_object.parameters().get<std::string>(MooseBase::unique_name_param);
     115      491687 :   MooseObjectName moose_object_name(moose_object_name_string);
     116      491687 :   _input_parameters[tid].erase(moose_object_name);
     117      491687 : }
     118             : 
     119             : const InputParameters &
     120           0 : InputParameterWarehouse::getInputParametersObject(const std::string & name, THREAD_ID tid) const
     121             : {
     122           0 :   return getInputParameters(MooseObjectName(name), tid);
     123             : }
     124             : 
     125             : const InputParameters &
     126          60 : InputParameterWarehouse::getInputParametersObject(const std::string & tag,
     127             :                                                   const std::string & name,
     128             :                                                   THREAD_ID tid) const
     129             : {
     130         116 :   return getInputParameters(MooseObjectName(tag, name), tid);
     131             : }
     132             : 
     133             : const InputParameters &
     134           0 : InputParameterWarehouse::getInputParametersObject(const MooseObjectName & object_name,
     135             :                                                   THREAD_ID tid) const
     136             : {
     137           0 :   return getInputParameters(object_name, tid);
     138             : }
     139             : 
     140             : InputParameters &
     141           0 : InputParameterWarehouse::getInputParameters(const std::string & name, THREAD_ID tid) const
     142             : {
     143           0 :   return getInputParameters(MooseObjectName(name), tid);
     144             : }
     145             : 
     146             : InputParameters &
     147           0 : InputParameterWarehouse::getInputParameters(const std::string & tag,
     148             :                                             const std::string & name,
     149             :                                             THREAD_ID tid) const
     150             : {
     151           0 :   return getInputParameters(MooseObjectName(tag, name), tid);
     152             : }
     153             : 
     154             : InputParameters &
     155          60 : InputParameterWarehouse::getInputParameters(const MooseObjectName & object_name,
     156             :                                             THREAD_ID tid) const
     157             : {
     158             :   // Locate the InputParameters object and error if it was not located
     159          60 :   const auto iter = _input_parameters[tid].find(object_name);
     160          60 :   if (iter == _input_parameters[tid].end())
     161           4 :     mooseError("Unknown InputParameters object ", object_name);
     162             : 
     163             :   // Return a reference to the parameter
     164         112 :   return *(iter->second.get());
     165             : }
     166             : 
     167             : const std::multimap<MooseObjectName, std::shared_ptr<InputParameters>> &
     168     1451897 : InputParameterWarehouse::getInputParameters(THREAD_ID tid) const
     169             : {
     170     1451897 :   return _input_parameters[tid];
     171             : }
     172             : 
     173             : void
     174       34349 : InputParameterWarehouse::addControllableParameterConnection(
     175             :     const MooseObjectParameterName & primary,
     176             :     const MooseObjectParameterName & secondary,
     177             :     bool error_on_empty /*=true*/)
     178             : {
     179       73758 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     180             :   {
     181       41938 :     std::vector<ControllableItem *> primaries = getControllableItems(primary, tid);
     182       41938 :     if (primaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
     183           0 :       mooseError("Unable to locate primary parameter with name ", primary);
     184       41938 :     else if (primaries.empty())
     185             :     {
     186        2532 :       if (tid == 0)
     187           0 :         return;
     188             :       else
     189             :         // try to connect non-threaded primary control to secondary controls of all threads
     190        2532 :         primaries = getControllableItems(primary, 0);
     191             :     }
     192             : 
     193       41938 :     std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
     194       41938 :     if (secondaries.empty() && error_on_empty && tid == 0) // some objects only exist on tid 0
     195           0 :       mooseError("Unable to locate secondary parameter with name ", secondary);
     196       41938 :     else if (secondaries.empty())
     197        2529 :       return;
     198             : 
     199       78820 :     for (auto primary_ptr : primaries)
     200       78825 :       for (auto secondary_ptr : secondaries)
     201       39414 :         if (primary_ptr != secondary_ptr)
     202       39414 :           primary_ptr->connect(secondary_ptr);
     203       44467 :   }
     204             : }
     205             : 
     206             : void
     207           0 : InputParameterWarehouse::addControllableObjectAlias(const MooseObjectName & alias,
     208             :                                                     const MooseObjectName & secondary)
     209             : {
     210           0 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     211             :   {
     212             :     std::vector<ControllableItem *> secondaries =
     213           0 :         getControllableItems(MooseObjectParameterName(secondary, "*"), tid);
     214           0 :     for (auto secondary_ptr : secondaries)
     215             :     {
     216           0 :       MooseObjectParameterName alias_param(alias, secondary_ptr->name().parameter());
     217           0 :       MooseObjectParameterName secondary_param(secondary, secondary_ptr->name().parameter());
     218           0 :       addControllableParameterAlias(alias_param, secondary_param);
     219           0 :     }
     220           0 :   }
     221           0 : }
     222             : 
     223             : void
     224          14 : InputParameterWarehouse::addControllableParameterAlias(const MooseObjectParameterName & alias,
     225             :                                                        const MooseObjectParameterName & secondary)
     226             : {
     227          29 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     228             :   {
     229          15 :     std::vector<ControllableItem *> secondaries = getControllableItems(secondary, tid);
     230          15 :     if (secondaries.empty() && tid == 0) // some objects only exist on tid 0
     231           0 :       mooseError("Unable to locate secondary parameter with name ", secondary);
     232             : 
     233          29 :     for (auto secondary_ptr : secondaries)
     234          28 :       _controllable_items[tid].emplace_back(
     235          28 :           std::make_unique<ControllableAlias>(alias, secondary_ptr));
     236          15 :   }
     237          14 : }
     238             : 
     239             : std::vector<ControllableItem *>
     240       86425 : InputParameterWarehouse::getControllableItems(const MooseObjectParameterName & input,
     241             :                                               THREAD_ID tid /*=0*/) const
     242             : {
     243       86425 :   std::vector<ControllableItem *> output;
     244     1446065 :   for (auto & ptr : _controllable_items[tid])
     245     1359640 :     if (ptr->name() == input)
     246       81368 :       output.push_back(ptr.get());
     247       86425 :   return output;
     248           0 : }
     249             : 
     250             : ControllableParameter
     251       11368 : InputParameterWarehouse::getControllableParameter(const MooseObjectParameterName & input) const
     252             : {
     253       11368 :   ControllableParameter cparam;
     254       23695 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     255      306944 :     for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
     256      294617 :       if ((*it)->name() == input)
     257       15028 :         cparam.add(it->get());
     258       11368 :   return cparam;
     259           0 : }
     260             : 
     261             : std::string
     262         180 : InputParameterWarehouse::dumpChangedControls(bool reset_changed) const
     263             : {
     264         180 :   std::stringstream oss;
     265         180 :   oss << std::left;
     266             : 
     267        8460 :   for (const auto & item : _controllable_items[0])
     268        8280 :     if (item->isChanged())
     269             :     {
     270         180 :       oss << item->dump(4);
     271         180 :       if (reset_changed)
     272          60 :         item->resetChanged();
     273             :     }
     274         360 :   return oss.str();
     275         180 : }
     276             : 
     277             : std::vector<MooseObjectParameterName>
     278       11056 : InputParameterWarehouse::getControllableParameterNames(const MooseObjectParameterName & input) const
     279             : {
     280       11056 :   std::vector<MooseObjectParameterName> names;
     281       23872 :   for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
     282      204955 :     for (auto it = _controllable_items[tid].begin(); it != _controllable_items[tid].end(); ++it)
     283      192139 :       if ((*it)->name() == input)
     284       17140 :         names.push_back((*it)->name());
     285       11056 :   return names;
     286           0 : }

Generated by: LCOV version 1.14