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 116 of file TheWarehouse.C.

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

◆ ~TheWarehouse()

TheWarehouse::~TheWarehouse ( )

Definition at line 117 of file TheWarehouse.C.

117{}

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 122 of file TheWarehouse.C.

123{
124 isValid(obj.get());
125
126 std::size_t obj_id = 0;
127 {
128 std::lock_guard<std::mutex> lock(_obj_mutex);
129
130 mooseAssert(!_obj_ids.count(obj.get()), obj->typeAndName() + " has already been added");
131
132 _objects.push_back(obj);
133 obj_id = _objects.size() - 1;
134 _obj_ids[obj.get()] = obj_id;
135
136 // reset/invalidate the query cache since query results may have been affected by this warehouse
137 // insertion.
138 _obj_cache.clear();
139 _query_cache.clear();
140 }
141
142 std::vector<std::unique_ptr<Attribute>> attribs;
143 readAttribs(obj.get(), attribs);
144 _store->add(obj_id, std::move(attribs));
145}
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::addFVGradientMethod(), 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 276 of file TheWarehouse.C.

277{
278 auto query_id = queryID(conds);
279 std::lock_guard<std::mutex> lock(_obj_cache_mutex);
280 auto & objs = query(query_id);
281 std::size_t count = 0;
282 for (auto obj : objs)
283 if (obj->enabled())
284 count++;
285 return count;
286}
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 172 of file TheWarehouse.C.

173{
174 bool sort = false;
175 std::unique_ptr<Attribute> sorted_attrib;
176 if (!conds.empty() && dynamic_cast<AttribSorted *>(conds.back().get()))
177 {
178 sorted_attrib = std::move(conds.back());
179 static const AttribSorted sorted_attrib_true(*this, true);
180 sort = sorted_attrib->isMatch(sorted_attrib_true);
181 // Remove the sorted condition temporarily
182 conds.pop_back();
183 }
184
185#ifdef DEBUG
186 for (auto & cond : conds)
187 mooseAssert(!dynamic_cast<AttribSorted *>(cond.get()),
188 "There should be no sorted attributes in this container.");
189#endif
190
191 auto obj_ids = _store->query(conds);
192 if (sorted_attrib)
193 conds.push_back(std::move(sorted_attrib));
194
195 std::lock_guard<std::mutex> lock(_obj_cache_mutex);
196 auto & vec = _obj_cache.emplace_back(obj_ids.size());
197 const auto query_id = _obj_cache.size() - 1;
198 {
199 std::lock_guard<std::mutex> lock(_query_cache_mutex);
200 _query_cache[std::move(conds)] = query_id;
201 }
202
203 std::lock_guard<std::mutex> o_lock(_obj_mutex);
204 for (const auto i : index_range(obj_ids))
205 {
206 auto obj = _objects[obj_ids[i]].get();
207 mooseAssert(std::find(vec.begin(), vec.end(), obj) == vec.end(), "Duplicate object");
208 vec[i] = obj;
209 }
210
211 if (sort && !vec.empty() && dynamic_cast<DependencyResolverInterface *>(vec[0]))
212 {
213 std::vector<DependencyResolverInterface *> dependers;
214 for (auto obj : vec)
215 {
216 auto d = dynamic_cast<DependencyResolverInterface *>(obj);
217 if (!d)
218 {
219 dependers.clear();
220 break;
221 }
222 dependers.push_back(d);
223 }
224
225 try
226 {
228 }
230 {
231 DependencyResolverInterface::cyclicDependencyError<MooseObject *>(
232 e,
233 "Cyclic dependency detected in object ordering",
235 {
236 auto * moose_obj = dynamic_cast<MooseObject *>(obj);
237 mooseAssert(moose_obj, "Failed to cast dependency object to MooseObject");
238 return moose_obj->name();
239 });
240 }
241
242 mooseAssert(dependers.size() == vec.size(), "Dependency resolution size mismatch");
243 for (unsigned int i = 0; i < dependers.size(); i++)
244 vec[i] = dynamic_cast<MooseObject *>(dependers[i]);
245 }
246
247 return query_id;
248}
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(), EXTERN_C_END::appendFVSetupObjects(), 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::getFVGradientMethod(), 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::hasFVGradientMethod(), 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 251 of file TheWarehouse.C.

252{
253 if (static_cast<std::size_t>(query_id) >= _obj_cache.size())
254 throw std::runtime_error("unknown query id");
255 return _obj_cache[query_id];
256}

◆ queryID()

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

Definition at line 259 of file TheWarehouse.C.

260{
261 {
262 std::lock_guard<std::mutex> lock(_query_cache_mutex);
263 auto it = _query_cache.find(conds);
264 if (it != _query_cache.end())
265 return it->second;
266 }
267
268 std::vector<std::unique_ptr<Attribute>> conds_clone;
269 conds_clone.resize(conds.size());
270 for (std::size_t i = 0; i < conds.size(); i++)
271 conds_clone[i] = conds[i]->clone();
272 return prepare(std::move(conds_clone));
273}
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. 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 289 of file TheWarehouse.C.

291{
292 for (auto & ref : _attrib_list)
293 {
294 attribs.emplace_back(ref->clone());
295 attribs.back()->initFrom(obj);
296 }
297}
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 160 of file TheWarehouse.C.

161{
162 std::vector<std::unique_ptr<Attribute>> attribs;
163 readAttribs(obj, attribs);
164 _store->set(_obj_ids[obj], std::move(attribs));
165 // reset/invalidate the query cache since query results may have been affected by this object
166 // attribute modification.
167 _obj_cache.clear();
168 _query_cache.clear();
169}

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 148 of file TheWarehouse.C.

149{
150 std::vector<std::unique_ptr<Attribute>> attribs;
151 attribs.push_back(extra.clone());
152 _store->set(_obj_ids[obj], std::move(attribs));
153 // reset/invalidate the query cache since query results may have been affected by this object
154 // attribute modification.
155 _obj_cache.clear();
156 _query_cache.clear();
157}
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: