https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
TheWarehouse Class Reference

TheWarehouse is a container for MooseObjects that allows querying/filtering over various customizeable attributes. More...

#include <TheWarehouse.h>

Classes

class  QueryCache
 QueryCache is a convenient way to construct and pass around (possible partially constructed) warehouse queries. More...
 

Public Types

template<typename T >
using KeyType = typename T::Key
 
template<typename T >
using AttribType = T *
 
using Query = QueryCache<>
 

Public Member Functions

 TheWarehouse ()
 
 ~TheWarehouse ()
 
template<typename T , typename... Args>
unsigned int registerAttribute (const std::string &name, Args... dummy_args)
 registers a new "tracked" attribute of type T for the warehouse.
 
unsigned int attribID (const std::string &name)
 Returns a unique ID associated with the given attribute name - i.e.
 
void add (std::shared_ptr< MooseObject > obj)
 add adds a new object to the warehouse and stores attributes/metadata about it for running queries/filtering.
 
void update (MooseObject *obj)
 update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse.
 
void update (MooseObject *obj, const Attribute &extra)
 update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse.
 
Query query ()
 query creates and returns an initialized a query object for querying objects from the warehouse.
 
std::size_t count (const std::vector< std::unique_ptr< Attribute > > &conds)
 count returns the number of objects that match the provided query conditions.
 
template<typename T >
std::vector< T * > & queryInto (const std::vector< std::unique_ptr< Attribute > > &conds, std::vector< T * > &results)
 queryInto takes the given conditions (i.e.
 
std::size_t queryID (const std::vector< std::unique_ptr< Attribute > > &conds)
 
template<typename T >
std::vector< T * > & queryInto (int query_id, std::vector< T * > &results, bool show_all=false)
 

Private Member Functions

int prepare (std::vector< std::unique_ptr< Attribute > > conds)
 prepares a query and returns an associated query_id (i.e. for use with the query function).
 
const std::vector< MooseObject * > & query (int query_id)
 callers of this function must lock _obj_cache_mutex as long as a reference to the returned vector is being used.
 
void readAttribs (const MooseObject *obj, std::vector< std::unique_ptr< Attribute > > &attribs)
 

Private Attributes

std::unique_ptr< WarehouseStorage_store
 
std::vector< std::shared_ptr< MooseObject > > _objects
 
std::unordered_map< MooseObject *, std::size_t > _obj_ids
 
std::vector< std::vector< MooseObject * > > _obj_cache
 
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int_query_cache
 
std::unordered_map< std::string, unsigned int_attrib_ids
 
std::vector< std::unique_ptr< Attribute > > _attrib_list
 
std::mutex _obj_mutex
 
std::mutex _query_cache_mutex
 
std::mutex _obj_cache_mutex
 

Detailed Description

TheWarehouse is a container for MooseObjects that allows querying/filtering over various customizeable attributes.

The meta-data about the objects is read/stored when the objects are added to the warehouse - updates to objects' state will not be reflected in query results unless the object is explicitly updated through the warehouse interface. The warehouse object can safely be queried concurrently from multiple threads.

Once Query and Attribute objects have been constructed, they are tied to the specific warehouse they were created with. They must not be used for different warehouses or the attribute ID they store internally will be wrong and that is bad.

Definition at line 186 of file TheWarehouse.h.

Member Typedef Documentation

◆ AttribType

template<typename T >
using TheWarehouse::AttribType = T *

Definition at line 192 of file TheWarehouse.h.

◆ KeyType

template<typename T >
using TheWarehouse::KeyType = typename T::Key

Definition at line 190 of file TheWarehouse.h.

◆ Query

Definition at line 410 of file TheWarehouse.h.

Constructor & Destructor Documentation

◆ TheWarehouse()

TheWarehouse::TheWarehouse ( )

Definition at line 110 of file TheWarehouse.C.

110: _store(std::make_unique<VecStore>()) {}
std::unique_ptr< WarehouseStorage > _store

◆ ~TheWarehouse()

TheWarehouse::~TheWarehouse ( )

Definition at line 111 of file TheWarehouse.C.

111{}

Member Function Documentation

◆ add()

void TheWarehouse::add ( std::shared_ptr< MooseObject obj)

add adds a new object to the warehouse and stores attributes/metadata about it for running queries/filtering.

The warehouse will maintain a pointer to the object indefinitely.

Definition at line 116 of file TheWarehouse.C.

117{
118 isValid(obj.get());
119
120 std::size_t obj_id = 0;
121 {
122 std::lock_guard<std::mutex> lock(_obj_mutex);
123
124 mooseAssert(!_obj_ids.count(obj.get()), obj->typeAndName() + " has already been added");
125
126 _objects.push_back(obj);
127 obj_id = _objects.size() - 1;
128 _obj_ids[obj.get()] = obj_id;
129
130 // reset/invalidate the query cache since query results may have been affected by this warehouse
131 // insertion.
132 _obj_cache.clear();
133 _query_cache.clear();
134 }
135
136 std::vector<std::unique_ptr<Attribute>> attribs;
137 readAttribs(obj.get(), attribs);
138 _store->add(obj_id, std::move(attribs));
139}
void isValid(MooseObject *obj)
std::mutex _obj_mutex
std::vector< std::shared_ptr< MooseObject > > _objects
std::unordered_map< MooseObject *, std::size_t > _obj_ids
void readAttribs(const MooseObject *obj, std::vector< std::unique_ptr< Attribute > > &attribs)
std::vector< std::vector< MooseObject * > > _obj_cache
std::unordered_map< std::vector< std::unique_ptr< Attribute > >, int > _query_cache

Referenced by NonlinearSystemBase::addBoundaryCondition(), NonlinearSystemBase::addDGKernel(), NonlinearSystemBase::addDiracKernel(), FEProblemBase::addFVInterpolationMethod(), NonlinearSystemBase::addHDGKernel(), NonlinearSystemBase::addInterfaceKernel(), NonlinearSystemBase::addKernel(), NonlinearSystemBase::addNodalKernel(), FEProblemBase::addObject(), NonlinearSystemBase::addScalarKernel(), NonlinearSystemBase::addSplit(), and FEProblemBase::addUserObject().

◆ attribID()

unsigned int TheWarehouse::attribID ( const std::string &  name)
inline

Returns a unique ID associated with the given attribute name - i.e.

an attribute and name that were previously registered via calls to registerAttribute. Users should generally not need to use this function.

Definition at line 444 of file TheWarehouse.h.

445 {
446 auto it = _attrib_ids.find(name);
447 if (it != _attrib_ids.end())
448 return it->second;
449 mooseError("no ID exists for unregistered attribute '", name, "'");
450 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
std::unordered_map< std::string, unsigned int > _attrib_ids

◆ count()

std::size_t TheWarehouse::count ( const std::vector< std::unique_ptr< Attribute > > &  conds)

count returns the number of objects that match the provided query conditions.

This requires executing a full query operation (i.e. as if calling queryInto). A Query object should generally be used via the query() member function instead.

Definition at line 270 of file TheWarehouse.C.

271{
272 auto query_id = queryID(conds);
273 std::lock_guard<std::mutex> lock(_obj_cache_mutex);
274 auto & objs = query(query_id);
275 std::size_t count = 0;
276 for (auto obj : objs)
277 if (obj->enabled())
278 count++;
279 return count;
280}
unsigned int count
Definition MortarUtils.C:53
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse.
std::mutex _obj_cache_mutex
std::size_t queryID(const std::vector< std::unique_ptr< Attribute > > &conds)

Referenced by TheWarehouse::QueryCache< Attribs >::count().

◆ prepare()

int TheWarehouse::prepare ( std::vector< std::unique_ptr< Attribute > >  conds)
private

prepares a query and returns an associated query_id (i.e. for use with the query function).

Definition at line 166 of file TheWarehouse.C.

167{
168 bool sort = false;
169 std::unique_ptr<Attribute> sorted_attrib;
170 if (!conds.empty() && dynamic_cast<AttribSorted *>(conds.back().get()))
171 {
172 sorted_attrib = std::move(conds.back());
173 static const AttribSorted sorted_attrib_true(*this, true);
174 sort = sorted_attrib->isMatch(sorted_attrib_true);
175 // Remove the sorted condition temporarily
176 conds.pop_back();
177 }
178
179#ifdef DEBUG
180 for (auto & cond : conds)
181 mooseAssert(!dynamic_cast<AttribSorted *>(cond.get()),
182 "There should be no sorted attributes in this container.");
183#endif
184
185 auto obj_ids = _store->query(conds);
186 if (sorted_attrib)
187 conds.push_back(std::move(sorted_attrib));
188
189 std::lock_guard<std::mutex> lock(_obj_cache_mutex);
190 auto & vec = _obj_cache.emplace_back(obj_ids.size());
191 const auto query_id = _obj_cache.size() - 1;
192 {
193 std::lock_guard<std::mutex> lock(_query_cache_mutex);
194 _query_cache[std::move(conds)] = query_id;
195 }
196
197 std::lock_guard<std::mutex> o_lock(_obj_mutex);
198 for (const auto i : index_range(obj_ids))
199 {
200 auto obj = _objects[obj_ids[i]].get();
201 mooseAssert(std::find(vec.begin(), vec.end(), obj) == vec.end(), "Duplicate object");
202 vec[i] = obj;
203 }
204
205 if (sort && !vec.empty() && dynamic_cast<DependencyResolverInterface *>(vec[0]))
206 {
207 std::vector<DependencyResolverInterface *> dependers;
208 for (auto obj : vec)
209 {
210 auto d = dynamic_cast<DependencyResolverInterface *>(obj);
211 if (!d)
212 {
213 dependers.clear();
214 break;
215 }
216 dependers.push_back(d);
217 }
218
219 try
220 {
222 }
224 {
225 DependencyResolverInterface::cyclicDependencyError<MooseObject *>(
226 e,
227 "Cyclic dependency detected in object ordering",
229 {
230 auto * moose_obj = dynamic_cast<MooseObject *>(obj);
231 mooseAssert(moose_obj, "Failed to cast dependency object to MooseObject");
232 return moose_obj->name();
233 });
234 }
235
236 mooseAssert(dependers.size() == vec.size(), "Dependency resolution size mismatch");
237 for (unsigned int i = 0; i < dependers.size(); i++)
238 vec[i] = dynamic_cast<MooseObject *>(dependers[i]);
239 }
240
241 return query_id;
242}
This attribute describes sorting state.
Interface for sorting dependent vectors of objects.
static void sort(typename std::vector< T > &vector)
Given a vector, sort using the getRequested/SuppliedItems sets.
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
std::mutex _query_cache_mutex
const Elem & get(const ElemType type_in)
auto index_range(const T &sizable)

Referenced by queryID().

◆ query() [1/2]

Query TheWarehouse::query ( )
inline

query creates and returns an initialized a query object for querying objects from the warehouse.

Definition at line 467 of file TheWarehouse.h.

467{ return Query(*this); }
QueryCache<> Query

Referenced by CheckFVBCAction::act(), NonlinearSystemBase::checkKernelCoverage(), FEProblemBase::checkUserObjectJacobianRequirement(), FEProblemBase::checkUserObjects(), ComputeResidualAndJacobianThread::compute(), NonlinearSystemBase::computeJacobianInternal(), NonlinearSystemBase::computeResidualAndJacobianInternal(), NonlinearSystemBase::computeResidualInternal(), LinearSystem::containsTimeKernel(), count(), FEProblemBase::customSetup(), ComputeResidualAndJacobianThread::determineObjectWarehouses(), ComputeResidualThread::determineObjectWarehouses(), MFEMProblem::executeMFEMObjects(), FEProblemBase::executeSamplers(), ComputeLinearFVElementalThread::fetchBlockSystemContributionObjects(), ComputeLinearFVFaceThread::fetchBlockSystemContributionObjects(), FEProblemBase::getDistribution(), FEProblemBase::getFVInterpolationMethod(), FEProblemBase::getKokkosUserObject(), MFEMProblem::getMFEMObject(), FEProblemBase::getMortarUserObjects(), FEProblemBase::getPositionsObject(), FEProblemBase::getPostprocessorObjectByName(), FEProblemBase::getSampler(), CompositionDT::getTimeSteppers(), FEProblemBase::getUOQuery(), FEProblemBase::getUserObject(), FEProblemBase::getUserObjectBase(), FEProblemBase::getVectorPostprocessorObjectByName(), FEProblemBase::hasDistribution(), FEProblemBase::hasFVInterpolationMethod(), MFEMProblem::hasMFEMObject(), FEProblemBase::hasUserObject(), VectorPostprocessorInterface::hasVectorPostprocessorByName(), MFEMProblem::initialSetup(), BoundaryLinearFVFluxIntegral::initialSetup(), SideFVFluxBCIntegral::initialSetup(), LinearSystem::initialSetup(), ExplicitTimeIntegrator::initialSetup(), AdvancedOutput::initPostprocessorOrVectorPostprocessorLists(), MooseVariableDataFV< OutputType >::MooseVariableDataFV(), FEProblemBase::needBoundaryMaterialOnSide(), FEProblemBase::needInterfaceMaterialOnSide(), FEProblemBase::needInternalNeighborSideMaterial(), JSONOutput::outputReporters(), BlockRestrictionDebugOutput::printBlockRestrictionGroups(), BlockRestrictionDebugOutput::printBlockRestrictionMap(), BlockRestrictionDebugOutput::printBoundaryRestrictionGroups(), queryInto(), ComputeLinearFVElementalThread::setupSystemContributionObjects(), ComputeLinearFVFaceThread::setupSystemContributionObjects(), and FEProblemBase::timestepSetup().

◆ query() [2/2]

const std::vector< MooseObject * > & TheWarehouse::query ( int  query_id)
private

callers of this function must lock _obj_cache_mutex as long as a reference to the returned vector is being used.

Definition at line 245 of file TheWarehouse.C.

246{
247 if (static_cast<std::size_t>(query_id) >= _obj_cache.size())
248 throw std::runtime_error("unknown query id");
249 return _obj_cache[query_id];
250}

◆ queryID()

std::size_t TheWarehouse::queryID ( const std::vector< std::unique_ptr< Attribute > > &  conds)

Definition at line 253 of file TheWarehouse.C.

254{
255 {
256 std::lock_guard<std::mutex> lock(_query_cache_mutex);
257 auto it = _query_cache.find(conds);
258 if (it != _query_cache.end())
259 return it->second;
260 }
261
262 std::vector<std::unique_ptr<Attribute>> conds_clone;
263 conds_clone.resize(conds.size());
264 for (std::size_t i = 0; i < conds.size(); i++)
265 conds_clone[i] = conds[i]->clone();
266 return prepare(std::move(conds_clone));
267}
int prepare(std::vector< std::unique_ptr< Attribute > > conds)
prepares a query and returns an associated query_id (i.e. for use with the query function).

Referenced by count(), JSONOutput::outputReporters(), queryInto(), and TheWarehouse::QueryCache< Attribs >::queryIntoHelper().

◆ queryInto() [1/2]

template<typename T >
std::vector< T * > & TheWarehouse::queryInto ( const std::vector< std::unique_ptr< Attribute > > &  conds,
std::vector< T * > &  results 
)
inline

queryInto takes the given conditions (i.e.

Attributes holding the values to filter/match over) and filters all objects in the warehouse that match all conditions (i.e. "and"ing the conditions together) and stores them in the results vector. All result objects must be castable to the templated type T. This function filters out disabled objects on the fly - only returning enabled ones.

Definition at line 478 of file TheWarehouse.h.

480 {
481 return queryInto(queryID(conds), results);
482 }
std::vector< T * > & queryInto(const std::vector< std::unique_ptr< Attribute > > &conds, std::vector< T * > &results)
queryInto takes the given conditions (i.e.

Referenced by JSONOutput::outputReporters(), queryInto(), and TheWarehouse::QueryCache< Attribs >::queryIntoHelper().

◆ queryInto() [2/2]

template<typename T >
std::vector< T * > & TheWarehouse::queryInto ( int  query_id,
std::vector< T * > &  results,
bool  show_all = false 
)
inline

Definition at line 487 of file TheWarehouse.h.

488 {
489 std::lock_guard<std::mutex> lock(_obj_cache_mutex);
490 auto & objs = query(query_id);
491 results.clear();
492 results.reserve(objs.size());
493 for (auto & obj : objs)
494 {
495 mooseAssert(obj, "Null object");
496 T * cast_obj;
497 // We've been using dynamic_cast plus an assert that it
498 // succeeds, but if we know the correct type T then often we can
499 // use a much-cheaper static_cast. libMesh::cast_ptr will still
500 // use a dynamic_cast + assertion in devel/dbg modes.
501 if constexpr (std::is_convertible_v<MooseObject *, std::remove_cv_t<T> *> ||
502 std::is_convertible_v<std::remove_cv_t<T> *, MooseObject *>)
503 {
504 cast_obj = cast_ptr<T *>(obj);
505 }
506 // Side-casts or virtual inheritence are always expensive.
507 // Someone should figure out how to get rid of this code path.
508 else
509 {
510 cast_obj = dynamic_cast<T *>(obj);
511 mooseAssert(cast_obj,
512 "Queried object " + obj->typeAndName() + " has incompatible c++ type with " +
513 MooseUtils::prettyCppType<T>());
514 }
515
516 mooseAssert(std::find(results.begin(), results.end(), cast_obj) == results.end(),
517 "Duplicate object");
518 if (show_all || obj->enabled())
519 results.push_back(cast_obj);
520 }
521 return results;
522 }

◆ readAttribs()

void TheWarehouse::readAttribs ( const MooseObject obj,
std::vector< std::unique_ptr< Attribute > > &  attribs 
)
private

Definition at line 283 of file TheWarehouse.C.

285{
286 for (auto & ref : _attrib_list)
287 {
288 attribs.emplace_back(ref->clone());
289 attribs.back()->initFrom(obj);
290 }
291}
std::vector< std::unique_ptr< Attribute > > _attrib_list

Referenced by add(), and update().

◆ registerAttribute()

template<typename T , typename... Args>
unsigned int TheWarehouse::registerAttribute ( const std::string &  name,
Args...  dummy_args 
)
inline

registers a new "tracked" attribute of type T for the warehouse.

args are all arguments necessary to create an instance of the T class excluding the warehouse reference/pointer which is assumed to be first and automatically inserted. An instance of every registered attribute will be created for and initialized to each object added to the warehouse allowing queries to be executed over specific values the attribute may take on. Attributes must be registered before objects are added to the warehouse. A unique ID associated with the registered attribute is returned - which is generally not needed used by users.

As an example, to register a class with the constructor "YourAttribute(TheWarehouse& w, int foo)", you would call "registerAttribute<YourAttribute>("your_attrib_name", constructor_arg1, ...)". Custom attribute classes are required to pass an attribute name (i.e. "your_attrib_name") to the Attribute base class. The dummy args are forwarded to the attrib class' constructor. The name passed here into registerAttribute must be the same string as the name passed to the Attribute base class's constructor.

Definition at line 430 of file TheWarehouse.h.

431 {
432 auto it = _attrib_ids.find(name);
433 if (it != _attrib_ids.end())
434 return it->second;
435
436 _attrib_ids[name] = _attrib_list.size();
437 _attrib_list.push_back(std::unique_ptr<Attribute>(new T(*this, dummy_args...)));
438 return _attrib_list.size() - 1;
439 }
std::string name(const ElemQuality q)

◆ update() [1/2]

void TheWarehouse::update ( MooseObject obj)

update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse.

Call this if an object's state has changed in such a way that its warehouse attributes have become stale/incorrect.

Definition at line 154 of file TheWarehouse.C.

155{
156 std::vector<std::unique_ptr<Attribute>> attribs;
157 readAttribs(obj, attribs);
158 _store->set(_obj_ids[obj], std::move(attribs));
159 // reset/invalidate the query cache since query results may have been affected by this object
160 // attribute modification.
161 _obj_cache.clear();
162 _query_cache.clear();
163}

Referenced by groupUserObjects().

◆ update() [2/2]

void TheWarehouse::update ( MooseObject obj,
const Attribute extra 
)

update updates the metadata/attribute-info stored for the given object obj that must already exists in the warehouse.

Call this if an object's state has changed in such a way that its warehouse attributes have become stale/incorrect. Any attribute specified in extra overwrites/trumps one read from the object's current state.

Definition at line 142 of file TheWarehouse.C.

143{
144 std::vector<std::unique_ptr<Attribute>> attribs;
145 attribs.push_back(extra.clone());
146 _store->set(_obj_ids[obj], std::move(attribs));
147 // reset/invalidate the query cache since query results may have been affected by this object
148 // attribute modification.
149 _obj_cache.clear();
150 _query_cache.clear();
151}
virtual std::unique_ptr< Attribute > clone() const =0
clone creates and returns and identical (deep) copy of this attribute - i.e.

Member Data Documentation

◆ _attrib_ids

std::unordered_map<std::string, unsigned int> TheWarehouse::_attrib_ids
private

Definition at line 546 of file TheWarehouse.h.

Referenced by attribID(), and registerAttribute().

◆ _attrib_list

std::vector<std::unique_ptr<Attribute> > TheWarehouse::_attrib_list
private

Definition at line 547 of file TheWarehouse.h.

Referenced by readAttribs(), and registerAttribute().

◆ _obj_cache

std::vector<std::vector<MooseObject *> > TheWarehouse::_obj_cache
private

Definition at line 540 of file TheWarehouse.h.

Referenced by add(), prepare(), query(), update(), and update().

◆ _obj_cache_mutex

std::mutex TheWarehouse::_obj_cache_mutex
private

Definition at line 551 of file TheWarehouse.h.

Referenced by count(), prepare(), and queryInto().

◆ _obj_ids

std::unordered_map<MooseObject *, std::size_t> TheWarehouse::_obj_ids
private

Definition at line 536 of file TheWarehouse.h.

Referenced by add(), update(), and update().

◆ _obj_mutex

std::mutex TheWarehouse::_obj_mutex
private

Definition at line 549 of file TheWarehouse.h.

Referenced by add(), and prepare().

◆ _objects

std::vector<std::shared_ptr<MooseObject> > TheWarehouse::_objects
private

Definition at line 535 of file TheWarehouse.h.

Referenced by add(), and prepare().

◆ _query_cache

std::unordered_map<std::vector<std::unique_ptr<Attribute> >, int> TheWarehouse::_query_cache
private

Definition at line 544 of file TheWarehouse.h.

Referenced by add(), prepare(), queryID(), update(), and update().

◆ _query_cache_mutex

std::mutex TheWarehouse::_query_cache_mutex
private

Definition at line 550 of file TheWarehouse.h.

Referenced by prepare(), and queryID().

◆ _store

std::unique_ptr<WarehouseStorage> TheWarehouse::_store
private

Definition at line 534 of file TheWarehouse.h.

Referenced by add(), prepare(), update(), and update().


The documentation for this class was generated from the following files: