LCOV - code coverage report
Current view: top level - include/warehouses - ExecuteMooseObjectWarehouse.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 56 63 88.9 %
Date: 2025-07-17 01:28:37 Functions: 60 80 75.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             : // MOOSE includes
      13             : #include "MooseObjectWarehouse.h"
      14             : #include "SetupInterface.h"
      15             : 
      16             : /**
      17             :  * A class for storing MooseObjects based on execution flag.
      18             :  *
      19             :  * Note: The global list of objects, those accessed via the "get" methods of this class, are not
      20             :  * sorted when the
      21             :  *       sort method is called. This is because cyclic errors occur even when the execution flags
      22             :  * differ.
      23             :  */
      24             : template <typename T>
      25             : class ExecuteMooseObjectWarehouse : public MooseObjectWarehouse<T>
      26             : {
      27             : public:
      28             :   using MooseObjectWarehouse<T>::checkThreadID;
      29             :   using MooseObjectWarehouse<T>::initialSetup;
      30             :   using MooseObjectWarehouse<T>::timestepSetup;
      31             :   using MooseObjectWarehouse<T>::subdomainSetup;
      32             : 
      33             :   /**
      34             :    * Constructor.
      35             :    * @param threaded True enables threaded object storage (default).
      36             :    */
      37             :   ExecuteMooseObjectWarehouse(const ExecFlagEnum & flags, bool threaded = true);
      38             : 
      39             :   virtual ~ExecuteMooseObjectWarehouse();
      40             : 
      41             :   /**
      42             :    * Adds an object to the storage structure.
      43             :    * @param object A shared pointer to the object being added
      44             :    */
      45             :   void addObject(std::shared_ptr<T> object, THREAD_ID tid = 0, bool recurse = true) override;
      46             : 
      47             :   ///@{
      48             :   /**
      49             :    * Retrieve shared pointers for the given thread and execution type for all/active objects.
      50             :    * @param exec_flag The execution flag to retrieve objects from
      51             :    */
      52             :   const MooseObjectWarehouse<T> & operator[](ExecFlagType exec_flag) const;
      53             :   MooseObjectWarehouse<T> & operator[](ExecFlagType exec_flag);
      54             :   ///@}
      55             : 
      56             :   ///@{
      57             :   /**
      58             :    * Provide access to begin/end iterators of the underlying map of execution flags.
      59             :    */
      60          72 :   typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::const_iterator begin() const
      61             :   {
      62          72 :     return _execute_objects.begin();
      63             :   }
      64          72 :   typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::const_iterator end() const
      65             :   {
      66          72 :     return _execute_objects.end();
      67             :   }
      68             :   ///@}
      69             : 
      70             :   /**
      71             :    * Updates the active objects storage.
      72             :    */
      73             :   void updateActive(THREAD_ID tid = 0) override;
      74             : 
      75             :   ///@{
      76             :   /**
      77             :    * Convenience methods for calling object setup methods.
      78             :    *
      79             :    * Limits call to these methods only to objects being executed on linear/nonlinear iterations.
      80             :    */
      81             :   void jacobianSetup(THREAD_ID tid = 0) const override;
      82             :   void residualSetup(THREAD_ID tid = 0) const override;
      83             :   void setup(const ExecFlagType & exec_flag, THREAD_ID tid = 0) const;
      84             :   ///@}
      85             : 
      86             :   /**
      87             :    * Performs a sort using the DependencyResolver.
      88             :    * @param tid The thread id to access.
      89             :    */
      90             :   void sort(THREAD_ID tid = 0);
      91             : 
      92             :   bool hasExecType(const ExecFlagType & exec_flag) { return _execute_objects.count(exec_flag) > 0; }
      93             : 
      94             : protected:
      95             :   // Map of execute objects to storage containers for MooseObjects
      96             :   std::map<ExecFlagType, MooseObjectWarehouse<T>> _execute_objects;
      97             : 
      98             :   /// A helper method for extracting objects from the various storage containers
      99             :   typename std::map<ExecFlagType, MooseObjectWarehouse<T>>::iterator
     100             :   getStorageHelper(std::map<ExecFlagType, MooseObjectWarehouse<T>> & objects,
     101             :                    ExecFlagType exec_flag) const;
     102             : };
     103             : 
     104             : template <typename T>
     105      928000 : ExecuteMooseObjectWarehouse<T>::ExecuteMooseObjectWarehouse(const ExecFlagEnum & flags,
     106             :                                                             bool threaded)
     107      928000 :   : MooseObjectWarehouse<T>(threaded)
     108             : {
     109             :   // Initialize the active/all data structures with the correct map entries and empty vectors
     110    25053200 :   for (const auto & flag : flags.items())
     111    24125200 :     _execute_objects.insert(std::make_pair(flag, MooseObjectWarehouse<T>(threaded)));
     112      928000 : }
     113             : 
     114             : template <typename T>
     115      859696 : ExecuteMooseObjectWarehouse<T>::~ExecuteMooseObjectWarehouse()
     116             : {
     117      859696 : }
     118             : 
     119             : template <typename T>
     120             : const MooseObjectWarehouse<T> &
     121      111197 : ExecuteMooseObjectWarehouse<T>::operator[](ExecFlagType exec_flag) const
     122             : {
     123             :   // Use find to avoid accidental insertion
     124      111197 :   const auto iter = _execute_objects.find(exec_flag);
     125             : 
     126      111197 :   if (iter == _execute_objects.end())
     127           0 :     mooseError("Unable to locate the desired execute flag (",
     128             :                exec_flag,
     129             :                "), the supplied execution flag was likely "
     130             :                "not registered.");
     131             : 
     132      111197 :   return iter->second;
     133             : }
     134             : 
     135             : template <typename T>
     136             : MooseObjectWarehouse<T> &
     137    65308032 : ExecuteMooseObjectWarehouse<T>::operator[](ExecFlagType exec_flag)
     138             : {
     139             :   // Use find to avoid accidental insertion
     140    65308032 :   const auto iter = _execute_objects.find(exec_flag);
     141             : 
     142    65308032 :   if (iter == _execute_objects.end())
     143           0 :     mooseError("Unable to locate the desired execute flag (",
     144             :                exec_flag,
     145             :                "), the supplied execution flag was likely "
     146             :                "not registered.");
     147             : 
     148    65308032 :   return iter->second;
     149             : }
     150             : 
     151             : template <typename T>
     152             : void
     153     4792710 : ExecuteMooseObjectWarehouse<T>::updateActive(THREAD_ID tid /* = 0 */)
     154             : {
     155             :   // Update all objects active list
     156     4792710 :   MooseObjectWarehouse<T>::updateActive(tid);
     157             : 
     158             :   // Update the execute flag lists of objects
     159   129402885 :   for (auto & object_pair : _execute_objects)
     160   124610175 :     object_pair.second.updateActive(tid);
     161     4792710 : }
     162             : 
     163             : template <typename T>
     164             : void
     165     4239464 : ExecuteMooseObjectWarehouse<T>::jacobianSetup(THREAD_ID tid /* = 0*/) const
     166             : {
     167     4239464 :   checkThreadID(tid);
     168     4239464 :   const auto iter = _execute_objects.find(EXEC_NONLINEAR);
     169     4239464 :   if (iter != _execute_objects.end())
     170     4239464 :     iter->second.jacobianSetup(tid);
     171     4239464 : }
     172             : 
     173             : template <typename T>
     174             : void
     175    26718600 : ExecuteMooseObjectWarehouse<T>::residualSetup(THREAD_ID tid /* = 0*/) const
     176             : {
     177    26718600 :   checkThreadID(tid);
     178    26718600 :   const auto iter = _execute_objects.find(EXEC_LINEAR);
     179    26718600 :   if (iter != _execute_objects.end())
     180    26718600 :     iter->second.residualSetup(tid);
     181    26718600 : }
     182             : 
     183             : template <typename T>
     184             : void
     185        7328 : ExecuteMooseObjectWarehouse<T>::setup(const ExecFlagType & exec_flag, THREAD_ID tid /* = 0*/) const
     186             : {
     187        7328 :   checkThreadID(tid);
     188        7328 :   if (exec_flag == EXEC_INITIAL)
     189         806 :     initialSetup(tid);
     190        6522 :   else if (exec_flag == EXEC_TIMESTEP_BEGIN)
     191        4241 :     timestepSetup(tid);
     192        2281 :   else if (exec_flag == EXEC_SUBDOMAIN)
     193           0 :     subdomainSetup(tid);
     194        2281 :   else if (exec_flag == EXEC_NONLINEAR)
     195           0 :     jacobianSetup(tid);
     196        2281 :   else if (exec_flag == EXEC_LINEAR)
     197           0 :     residualSetup(tid);
     198        7328 : }
     199             : 
     200             : template <typename T>
     201             : void
     202      166030 : ExecuteMooseObjectWarehouse<T>::addObject(std::shared_ptr<T> object,
     203             :                                           THREAD_ID tid,
     204             :                                           bool /*recurse*/)
     205             : {
     206             :   // Update list of all objects
     207      166030 :   MooseObjectWarehouse<T>::addObject(object, tid);
     208             : 
     209             :   // Update the execute flag lists of objects
     210      332036 :   if (const auto ptr = std::dynamic_pointer_cast<SetupInterface>(object))
     211      407874 :     for (const auto & flag : ptr->getExecuteOnEnum())
     212      241856 :       _execute_objects[flag].addObject(object, tid);
     213             :   else
     214           0 :     mooseError("The object being added (",
     215           0 :                object->name(),
     216             :                ") must inherit from SetupInterface to be added to the ExecuteMooseObjectWarehouse "
     217             :                "container.");
     218      166018 : }
     219             : 
     220             : template <typename T>
     221             : void
     222      485240 : ExecuteMooseObjectWarehouse<T>::sort(THREAD_ID tid /* = 0*/)
     223             : {
     224             :   // Sort execute object storage
     225    13101392 :   for (auto & object_pair : _execute_objects)
     226    12616152 :     object_pair.second.sort(tid);
     227      485240 : }

Generated by: LCOV version 1.14