LCOV - code coverage report
Current view: top level - include/warehouses - MooseObjectTagWarehouse.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 84 84 100.0 %
Date: 2025-08-08 20:01:16 Functions: 52 56 92.9 %
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             : 
      15             : /**
      16             :  * A storage container for MooseObjects that inherit from SetupInterface.
      17             :  *
      18             :  * Objects that inherit from SetupInterface have various functions (e.g., initialSetup). This
      19             :  * class provides convenience functions for looping over all active Objects stored in the warehouse
      20             :  * and calling the setup methods.
      21             :  */
      22             : template <typename T>
      23             : class MooseObjectTagWarehouse : public MooseObjectWarehouse<T>
      24             : {
      25             : public:
      26             :   /**
      27             :    * Constructor.
      28             :    * @param threaded When true (default) threaded storage is enabled.
      29             :    */
      30             :   MooseObjectTagWarehouse(bool threaded = true);
      31             : 
      32             :   /**
      33             :    * Update the active status of Kernels
      34             :    */
      35             :   virtual void updateActive(THREAD_ID tid = 0) override;
      36             : 
      37             :   /**
      38             :    * Retrieve a moose object warehouse in which every moose object has the given vector tag.
      39             :    * If the warehouse is not constructed yet, it will be constructed here and returned. If
      40             :    * the warehouse is already cached (it was queried before), we just directly return the
      41             :    * cached warehouse.
      42             :    */
      43             :   MooseObjectWarehouse<T> & getVectorTagObjectWarehouse(TagID tag_id, THREAD_ID tid);
      44             : 
      45             :   /**
      46             :    * Retrieve a moose object warehouse in which every moose object at least has one of the given
      47             :    * vector tags.
      48             :    * If the warehouse is not constructed yet, it will be constructed here and returned. If
      49             :    * the warehouse is already cached (it was queried before), we just directly return the
      50             :    * cached warehouse.
      51             :    */
      52             :   MooseObjectWarehouse<T> & getVectorTagsObjectWarehouse(const std::set<TagID> & tags,
      53             :                                                          THREAD_ID tid);
      54             :   /**
      55             :    * Retrieve a moose object warehouse in which every moose object has the given matrix tag.
      56             :    * If the warehouse is not constructed yet, it will be constructed here and returned. If
      57             :    * the warehouse is already cached (it was queried before), we just directly return the
      58             :    * cached warehouse.
      59             :    */
      60             :   MooseObjectWarehouse<T> & getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid);
      61             : 
      62             :   /**
      63             :    * const version of the above
      64             :    */
      65             :   const MooseObjectWarehouse<T> & getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid) const;
      66             : 
      67             :   /**
      68             :    * Retrieve a moose object warehouse in which every moose object has one of the given matrix tags.
      69             :    * If the warehouse is not constructed yet, it will be constructed here and returned. If
      70             :    * the warehouse is already cached (it was queried before), we just directly return the
      71             :    * cached warehouse.
      72             :    */
      73             :   MooseObjectWarehouse<T> & getMatrixTagsObjectWarehouse(const std::set<TagID> & tags,
      74             :                                                          THREAD_ID tid);
      75             : 
      76             : protected:
      77             :   const THREAD_ID _num_threads;
      78             : 
      79             :   /// Tag based storage. Map from a tag to a moose object warehouse for vector tags
      80             :   std::vector<std::map<TagID, MooseObjectWarehouse<T>>> _vector_tag_to_object_warehouse;
      81             : 
      82             :   /// std::set<TagID> based storage. Map from a std::set of tags to a moose object warehouse for vector tags.
      83             :   std::vector<std::map<std::set<TagID>, MooseObjectWarehouse<T>>> _vector_tags_to_object_warehouse;
      84             : 
      85             :   /// Tag based storage. Map fro a tag to moose object warehouse for matrix tags.
      86             :   std::vector<std::map<TagID, MooseObjectWarehouse<T>>> _matrix_tag_to_object_warehouse;
      87             : 
      88             :   /// std::set<TagID> based storage. Map from a std::set of tags to moose object warehouse for matrix tags.
      89             :   std::vector<std::map<std::set<TagID>, MooseObjectWarehouse<T>>> _matrix_tags_to_object_warehouse;
      90             : };
      91             : 
      92             : template <typename T>
      93      553077 : MooseObjectTagWarehouse<T>::MooseObjectTagWarehouse(bool threaded /*=true*/)
      94             :   : MooseObjectWarehouse<T>(threaded),
      95      553077 :     _num_threads(threaded ? libMesh::n_threads() : 1),
      96      553077 :     _vector_tag_to_object_warehouse(_num_threads),
      97      553077 :     _vector_tags_to_object_warehouse(_num_threads),
      98      553077 :     _matrix_tag_to_object_warehouse(_num_threads),
      99     1106154 :     _matrix_tags_to_object_warehouse(_num_threads)
     100             : {
     101      553077 : }
     102             : 
     103             : template <typename T>
     104             : void
     105     2847822 : MooseObjectTagWarehouse<T>::updateActive(THREAD_ID tid)
     106             : {
     107     2847822 :   MooseObjectWarehouse<T>::updateActive(tid);
     108             : 
     109     2882502 :   for (auto & it : _vector_tag_to_object_warehouse[tid])
     110       34680 :     it.second.updateActive(tid);
     111             : 
     112     2872446 :   for (auto & it : _vector_tags_to_object_warehouse[tid])
     113       24624 :     it.second.updateActive(tid);
     114             : 
     115     2876391 :   for (auto & it : _matrix_tag_to_object_warehouse[tid])
     116       28569 :     it.second.updateActive(tid);
     117             : 
     118     2849271 :   for (auto & it : _matrix_tags_to_object_warehouse[tid])
     119        1449 :     it.second.updateActive(tid);
     120     2847822 : }
     121             : 
     122             : template <typename T>
     123             : MooseObjectWarehouse<T> &
     124      217913 : MooseObjectTagWarehouse<T>::getVectorTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
     125             : {
     126      217913 :   auto & vector_tag_to_object_warehouse = _vector_tag_to_object_warehouse[tid];
     127             : 
     128      217913 :   const auto & house_end = vector_tag_to_object_warehouse.end();
     129      217913 :   const auto & tag_warehouse = vector_tag_to_object_warehouse.find(tag_id);
     130             : 
     131      217913 :   if (tag_warehouse != house_end)
     132      183313 :     return tag_warehouse->second;
     133             :   else
     134       34600 :     vector_tag_to_object_warehouse[tag_id];
     135             : 
     136             :   // Now add actual moose objects into warehouse
     137       34600 :   const auto & objects = MooseObjectWarehouse<T>::getObjects(tid);
     138       75214 :   for (auto & object : objects)
     139             :   {
     140       40614 :     auto & tags = object->getVectorTags({});
     141       85060 :     for (auto & tag : tags)
     142             :     {
     143       47122 :       if (tag == tag_id)
     144             :       {
     145             :         // Tag based storage
     146        2676 :         vector_tag_to_object_warehouse[tag_id].addObject(object, tid);
     147        2676 :         break;
     148             :       }
     149             :     }
     150             :   }
     151             : 
     152       34600 :   return vector_tag_to_object_warehouse[tag_id];
     153             : }
     154             : 
     155             : template <typename T>
     156             : MooseObjectWarehouse<T> &
     157       86338 : MooseObjectTagWarehouse<T>::getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
     158             : {
     159       86338 :   auto & matrix_tag_to_object_warehouse = _matrix_tag_to_object_warehouse[tid];
     160             : 
     161       86338 :   const auto & house_end = matrix_tag_to_object_warehouse.end();
     162       86338 :   const auto & tag_warehouse = matrix_tag_to_object_warehouse.find(tag_id);
     163             : 
     164       86338 :   if (tag_warehouse != house_end)
     165       81604 :     return tag_warehouse->second;
     166             :   else
     167        4734 :     matrix_tag_to_object_warehouse[tag_id];
     168             : 
     169             :   // Add moose objects to matrix-tag warehouse
     170        4734 :   const auto & objects = MooseObjectWarehouse<T>::getObjects(tid);
     171        8050 :   for (auto & object : objects)
     172             :   {
     173        3316 :     auto & tags = object->getMatrixTags({});
     174        8566 :     for (auto & tag : tags)
     175             :     {
     176        7188 :       if (tag == tag_id)
     177             :       {
     178             :         // Tag based storage
     179        1938 :         matrix_tag_to_object_warehouse[tag_id].addObject(object, tid);
     180             : 
     181        1938 :         break;
     182             :       }
     183             :     }
     184             :   }
     185             : 
     186        4734 :   return matrix_tag_to_object_warehouse[tag_id];
     187             : }
     188             : 
     189             : template <typename T>
     190             : const MooseObjectWarehouse<T> &
     191          60 : MooseObjectTagWarehouse<T>::getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid) const
     192             : {
     193          60 :   return const_cast<MooseObjectTagWarehouse<T> *>(this)->getMatrixTagObjectWarehouse(tag_id, tid);
     194             : }
     195             : 
     196             : template <typename T>
     197             : MooseObjectWarehouse<T> &
     198      148163 : MooseObjectTagWarehouse<T>::getVectorTagsObjectWarehouse(const std::set<TagID> & v_tags,
     199             :                                                          THREAD_ID tid)
     200             : {
     201             :   // std::map is not thread-safe for writes
     202      148163 :   auto & vector_tags_to_object_warehouse = _vector_tags_to_object_warehouse[tid];
     203             : 
     204      148163 :   const auto & house_end = vector_tags_to_object_warehouse.end();
     205      148163 :   const auto & tags_warehouse = vector_tags_to_object_warehouse.find(v_tags);
     206             : 
     207      148163 :   if (tags_warehouse != house_end)
     208      143865 :     return tags_warehouse->second;
     209             :   else
     210        4298 :     vector_tags_to_object_warehouse[v_tags];
     211             : 
     212             :   // Add moose objects to vector-tags warehouse
     213        4298 :   const auto & objects = MooseObjectWarehouse<T>::getObjects(tid);
     214        8190 :   for (auto & object : objects)
     215             :   {
     216        3892 :     auto & tags = object->getVectorTags({});
     217        3892 :     const auto & tags_end = tags.end();
     218        5922 :     for (auto & v_tag : v_tags)
     219             :     {
     220        5584 :       const auto & tag_found = tags.find(v_tag);
     221             :       // Object contains at least one of required tags
     222        5584 :       if (tag_found != tags_end)
     223             :       {
     224             :         // std::vector<Tag> based storage
     225        3554 :         vector_tags_to_object_warehouse[v_tags].addObject(object, tid);
     226             :         // Then we should work for next object
     227        3554 :         break;
     228             :       }
     229             :     }
     230             :   }
     231             : 
     232        4298 :   return vector_tags_to_object_warehouse[v_tags];
     233             : }
     234             : 
     235             : template <typename T>
     236             : MooseObjectWarehouse<T> &
     237        9438 : MooseObjectTagWarehouse<T>::getMatrixTagsObjectWarehouse(const std::set<TagID> & m_tags,
     238             :                                                          THREAD_ID tid)
     239             : {
     240        9438 :   auto & matrix_tags_to_object_warehouse = _matrix_tags_to_object_warehouse[tid];
     241             : 
     242        9438 :   const auto & house_end = matrix_tags_to_object_warehouse.end();
     243        9438 :   const auto & tags_warehouse = matrix_tags_to_object_warehouse.find(m_tags);
     244             : 
     245        9438 :   if (tags_warehouse != house_end)
     246        7879 :     return tags_warehouse->second;
     247             :   else
     248        1559 :     matrix_tags_to_object_warehouse[m_tags];
     249             : 
     250        1559 :   const auto & objects = MooseObjectWarehouse<T>::getObjects(tid);
     251        2980 :   for (auto & object : objects)
     252             :   {
     253        1421 :     auto & tags = object->getMatrixTags({});
     254        1421 :     const auto & tags_end = tags.end();
     255        1903 :     for (auto & m_tag : m_tags)
     256             :     {
     257        1864 :       const auto & tag_found = tags.find(m_tag);
     258             :       // Object contains at least one of required tags
     259        1864 :       if (tag_found != tags_end)
     260             :       {
     261             :         // std::vector<Tag> based storage
     262        1382 :         matrix_tags_to_object_warehouse[m_tags].addObject(object, tid);
     263             :         // Then we should work for next object
     264        1382 :         break;
     265             :       }
     266             :     }
     267             :   }
     268             : 
     269        1559 :   return matrix_tags_to_object_warehouse[m_tags];
     270             : }

Generated by: LCOV version 1.14