LCOV - code coverage report
Current view: top level - include/chaincontrols - ChainControl.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 21 21 100.0 %
Date: 2025-07-17 01:28:37 Functions: 9 9 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             : #pragma once
      11             : 
      12             : #include "Control.h"
      13             : #include "ChainControlData.h"
      14             : #include "ChainControlDataSystem.h"
      15             : #include "MooseUtils.h"
      16             : 
      17             : /**
      18             :  * Control that additionally provides the capability to produce/consume data values,
      19             :  * to allow control operations to be chained together.
      20             :  */
      21             : class ChainControl : public Control
      22             : {
      23             : public:
      24             :   static InputParameters validParams();
      25             : 
      26             :   ChainControl(const InputParameters & parameters);
      27             : 
      28             :   /**
      29             :    * Initialization that occurs in \c ChainControlSetupAction, right before the dependencies
      30             :    * are added.
      31             :    *
      32             :    * Note that the \c initialSetup() method is executed after all
      33             :    * actions are completed, so it is too late to add control data dependencies.
      34             :    */
      35         386 :   virtual void init() {}
      36             : 
      37             :   /**
      38             :    * Returns the ChainControls that must run before this one
      39             :    */
      40         410 :   const std::vector<std::string> & getChainControlDataDependencies() const
      41             :   {
      42         410 :     return _control_data_depends_on;
      43             :   }
      44             : 
      45             : protected:
      46             :   /**
      47             :    * Declares chain control data with the given name and type
      48             :    *
      49             :    * @tparam T          Type of chain control data
      50             :    * @param data_name   Chain control data name
      51             :    * @param apply_object_prefix   If true, apply the object name as a prefix to the data name
      52             :    */
      53             :   template <typename T>
      54             :   T & declareChainControlData(const std::string & data_name, bool apply_object_prefix = true);
      55             : 
      56             :   /**
      57             :    * Get a reference to control data that are specified in the input parameter 'param_name'
      58             :    *
      59             :    * @tparam T      Type of chain control data
      60             :    * @param param   Parameter containing chain control data name
      61             :    */
      62             :   template <typename T>
      63             :   const T & getChainControlData(const std::string & param);
      64             : 
      65             :   /**
      66             :    * Get a reference to control data value from a previous time step that is specified in the input
      67             :    * parameter 'param_name'
      68             :    *
      69             :    * @tparam T      Type of chain control data
      70             :    * @param param   Parameter containing chain control data name
      71             :    */
      72             :   template <typename T>
      73             :   const T & getChainControlDataOld(const std::string & param);
      74             : 
      75             :   /**
      76             :    * Get a reference to control data that are specified by 'data_name' name
      77             :    *
      78             :    * @tparam T          Type of chain control data
      79             :    * @param data_name   Chain control data name
      80             :    */
      81             :   template <typename T>
      82             :   const T & getChainControlDataByName(const std::string & data_name);
      83             : 
      84             :   /**
      85             :    * Get a reference to control data value from previous time step that is specified by 'data_name'
      86             :    * name
      87             :    *
      88             :    * @tparam T          Type of chain control data
      89             :    * @param data_name   Chain control data name
      90             :    */
      91             :   template <typename T>
      92             :   const T & getChainControlDataOldByName(const std::string & data_name);
      93             : 
      94             :   /**
      95             :    * Adds a chain control data dependency into the list
      96             :    *
      97             :    * @param data_name   Name of chain control data that this control depends upon
      98             :    */
      99             :   void addChainControlDataDependency(const std::string & data_name);
     100             : 
     101             :   /**
     102             :    * Gets the full control data name, including object name prefix (if any)
     103             :    *
     104             :    * @param data_name   Chain control data name
     105             :    * @param apply_object_prefix   If true, apply the object name as a prefix to the data name
     106             :    */
     107             :   std::string fullControlDataName(const std::string & data_name,
     108             :                                   bool apply_object_prefix = true) const;
     109             : 
     110             :   /// List of chain control data that this control depends upon
     111             :   std::vector<std::string> _control_data_depends_on;
     112             : 
     113             : private:
     114             :   /**
     115             :    * Retrieve the chain control data system from the \p MooseApp
     116             :    */
     117             :   ChainControlDataSystem & getChainControlDataSystem();
     118             : };
     119             : 
     120             : template <typename T>
     121             : T &
     122         448 : ChainControl::declareChainControlData(const std::string & data_name, bool apply_object_prefix)
     123             : {
     124         448 :   const std::string full_data_name = fullControlDataName(data_name, apply_object_prefix);
     125         448 :   auto & data = getChainControlDataSystem().declareChainControlData<T>(full_data_name, *this);
     126         896 :   return data.set();
     127         448 : }
     128             : 
     129             : template <typename T>
     130             : const T &
     131         196 : ChainControl::getChainControlData(const std::string & param)
     132             : {
     133         196 :   return getChainControlDataByName<T>(getParam<std::string>(param));
     134             : }
     135             : 
     136             : template <typename T>
     137             : const T &
     138             : ChainControl::getChainControlDataOld(const std::string & param)
     139             : {
     140             :   return getChainControlDataOldByName<T>(getParam<std::string>(param));
     141             : }
     142             : 
     143             : template <typename T>
     144             : const T &
     145         196 : ChainControl::getChainControlDataByName(const std::string & data_name)
     146             : {
     147         196 :   auto & system = getChainControlDataSystem();
     148             : 
     149         196 :   if (system.hasChainControlData(data_name) && !system.hasChainControlDataOfType<T>(data_name))
     150           8 :     mooseError("The chain control data '",
     151             :                data_name,
     152             :                "' has the type '",
     153           4 :                system.getChainControlDataMap().at(data_name)->type(),
     154             :                "', but this chain control requires its type to be '",
     155             :                MooseUtils::prettyCppType<T>(),
     156             :                "'.");
     157             : 
     158         192 :   auto & data = system.getChainControlData<T>(data_name);
     159             : 
     160         192 :   addChainControlDataDependency(data_name);
     161             : 
     162         192 :   return data.get();
     163             : }
     164             : 
     165             : template <typename T>
     166             : const T &
     167          36 : ChainControl::getChainControlDataOldByName(const std::string & data_name)
     168             : {
     169          36 :   auto & data = getChainControlDataSystem().getChainControlData<T>(data_name);
     170             : 
     171          36 :   return data.getOld();
     172             : }

Generated by: LCOV version 1.14