LCOV - code coverage report
Current view: top level - include/controls - ControllableParameter.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 27 28 96.4 %
Date: 2025-07-17 01:28:37 Functions: 20 24 83.3 %
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             : #pragma once
      11             : 
      12             : // MOOSE includes
      13             : #include "InputParameters.h"
      14             : #include "MooseObjectName.h"
      15             : #include "MooseObjectParameterName.h"
      16             : #include "ControllableItem.h"
      17             : 
      18             : /**
      19             :  * The ControllableParameter class is simply a set of ControllableItem objects. This object
      20             :  * is what is used from within a Control for setting input parameter values. These objects are
      21             :  * made on demand by the Control objects.
      22             :  *
      23             :  * This class is needed to allow for multiple parameters to be set with a single interface.
      24             :  */
      25             : 
      26             : class ControllableParameter
      27             : {
      28             : public:
      29       10346 :   ControllableParameter() = default;
      30       10330 :   virtual ~ControllableParameter() = default;
      31             : 
      32             :   ControllableParameter(ControllableParameter &&) = default;
      33             :   ControllableParameter(const ControllableParameter &) = delete;
      34             :   ControllableParameter & operator=(const ControllableParameter &) = delete;
      35             :   ControllableParameter & operator=(ControllableParameter &&) = delete;
      36             : 
      37             :   /**
      38             :    * Return true if the container is empty.
      39             :    */
      40       10335 :   bool empty() { return _items.size() == 0; }
      41             : 
      42             :   /**
      43             :    * Return a string that lists the parameters stored by this object.
      44             :    *
      45             :    * This is used by ControlInterface::getControlParamByName for error reporting.
      46             :    */
      47             :   std::string dump() const;
      48             : 
      49             :   /**
      50             :    * Set the value(s) of the controlled parameters stored in this class.
      51             :    * @param value The value to change the parameters to.
      52             :    */
      53             :   template <typename T>
      54             :   void set(const T & value, bool type_check = true);
      55             : 
      56             :   /**
      57             :    * Return a copy of the values of the given type.
      58             :    */
      59             :   template <typename T>
      60             :   std::vector<T> get(bool type_check = true, bool warn_when_values_difffer = false) const;
      61             : 
      62             :   /**
      63             :    * Check size() and the type of the stored items, i.e., there must be items with the given type.
      64             :    */
      65             :   template <typename T>
      66             :   bool check();
      67             : 
      68             :   /**
      69             :    * Check the execute flags.
      70             :    */
      71             :   void checkExecuteOnType(const ExecFlagType & current) const;
      72             : 
      73             :   /**
      74             :    * Adds the supplied item with the other items within this object.
      75             :    */
      76             :   void add(ControllableItem * item);
      77             : 
      78             :   /// Allows this to be used with std:: cout
      79             :   friend std::ostream & operator<<(std::ostream & stream, const ControllableParameter & obj);
      80             : 
      81             : private:
      82             :   /// Storage for the ControllableItems, these are stored as pointers to avoid copies.
      83             :   std::vector<ControllableItem *> _items;
      84             : };
      85             : 
      86             : template <typename T>
      87             : void
      88        9246 : ControllableParameter::set(const T & value, bool type_check /*=true*/)
      89             : {
      90       21771 :   for (ControllableItem * item : _items)
      91       12526 :     item->set<T>(value, type_check);
      92        9245 : }
      93             : 
      94             : template <typename T>
      95             : std::vector<T>
      96        1065 : ControllableParameter::get(bool type_check /*=true*/, bool warn_when_values_differ) const
      97             : {
      98        1065 :   std::vector<T> output;
      99        2182 :   for (const ControllableItem * const item : _items)
     100             :   {
     101        1118 :     std::vector<T> local = item->get<T>(type_check);
     102        1117 :     output.insert(output.end(), local.begin(), local.end());
     103             :   }
     104             : 
     105             :   // Produce a warning, if the flag is true, when multiple parameters have different values
     106        1064 :   if (warn_when_values_differ && _items.size() > 1)
     107             :   {
     108             :     // The first parameter to test against
     109          14 :     const T value0 = output[0];
     110             : 
     111             :     // Loop over all other parameter values
     112          65 :     for (T value : output)
     113             :     {
     114          52 :       if (value0 != value)
     115             :       {
     116          23 :         std::ostringstream oss;
     117          23 :         oss << "The following controlled parameters are being retrieved, but the values differ:\n";
     118          23 :         oss << dump();
     119          24 :         mooseWarning(oss.str());
     120          23 :       }
     121             :     }
     122           0 :   }
     123             : 
     124        1063 :   return output;
     125           2 : }
     126             : 
     127             : template <typename T>
     128             : bool
     129           7 : ControllableParameter::check()
     130             : {
     131           7 :   bool type = std::all_of(
     132           6 :       _items.begin(), _items.end(), [](ControllableItem * item) { return item->check<T>(); });
     133           7 :   return type && !empty();
     134             : }

Generated by: LCOV version 1.14