https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MaterialPropertyStorage.h
Go to the documentation of this file.
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 "Moose.h"
13#include "MooseHashing.h"
14#include "HashMap.h"
15#include "MaterialProperty.h"
17#include "MaterialData.h"
18
19#include <variant>
20
21// Forward declarations
22class FEProblemBase;
23class MaterialBase;
24class QpMap;
25
26// libMesh forward declarations
27namespace libMesh
28{
29class QBase;
30class Elem;
31}
32
34
35void dataStore(std::ostream & stream, MaterialPropertyStorage & storage, void * context);
36void dataLoad(std::istream & stream, MaterialPropertyStorage & storage, void * context);
37
44{
45public:
47
52 {
54 bool stateful() const { return state > 0; }
56 std::set<std::string> declarers;
58 std::string type;
60 unsigned int stateful_id = invalid_uint;
62 unsigned int state = 0;
64 bool restored = false;
65 };
66
104 void prolongStatefulProps(processor_id_type pid,
105 const std::vector<std::vector<QpMap>> & refinement_map,
106 const libMesh::QBase & qrule,
107 const libMesh::QBase & qrule_face,
108 MaterialPropertyStorage & parent_material_props,
109 const THREAD_ID tid,
110 const Elem & elem,
111 const int input_parent_side,
112 const int input_child,
113 const int input_child_side);
114
130 void updateStatefulPropsForPRefinement(const processor_id_type pid,
131 const std::vector<QpMap> & p_refinement_map,
132 const libMesh::QBase & qrule,
133 const libMesh::QBase & qrule_face,
134 const THREAD_ID tid,
135 const Elem & elem,
136 const int input_side);
137
150 void restrictStatefulProps(const std::vector<std::pair<unsigned int, QpMap>> & coarsening_map,
151 const std::vector<const Elem *> & coarsened_element_children,
152 const libMesh::QBase & qrule,
153 const libMesh::QBase & qrule_face,
154 const THREAD_ID tid,
155 const Elem & elem,
156 int input_side = -1);
157
169 void initStatefulProps(const THREAD_ID tid,
170 const std::vector<std::shared_ptr<MaterialBase>> & mats,
171 const unsigned int n_qpoints,
172 const Elem & elem,
173 const unsigned int side = 0);
174
182 void shift();
183
198 void copy(const THREAD_ID tid,
199 const Elem & elem_to,
200 const Elem & elem_from,
201 unsigned int side,
202 unsigned int n_qpoints);
203
214 void copy(const THREAD_ID tid,
215 const Elem * elem_to,
216 const Elem * elem_from,
217 unsigned int side,
218 unsigned int n_qpoints);
219
227 void swap(const THREAD_ID tid, const Elem & elem, unsigned int side);
228
236 void swapBack(const THREAD_ID tid, const Elem & elem, unsigned int side);
237
241 bool hasStatefulProperties() const { return maxState() > 0; }
242
246 bool hasOlderProperties() const { return maxState() > 1; }
247
256
258
261 const PropsType & props(const unsigned int state = 0) const;
262 const MaterialProperties &
263 props(const Elem * elem, unsigned int side, const unsigned int state = 0) const;
264 MaterialProperties & setProps(const Elem * elem, unsigned int side, const unsigned int state = 0);
266
267 bool hasProperty(const std::string & prop_name) const { return _registry.hasProperty(prop_name); }
268
278 unsigned int addProperty(const std::string & prop_name,
279 const std::type_info & type,
280 const unsigned int state,
281 const MaterialBase * const declarer);
282
283 const std::vector<unsigned int> & statefulProps() const { return _stateful_prop_id_to_prop_id; }
284
286
290 std::optional<std::string> queryStatefulPropName(const unsigned int id) const;
291
297 void eraseProperty(const Elem * elem);
298
302 unsigned int maxState() const
303 {
304 mooseAssert(_max_state < _storage.size(), "Too big");
305 return _max_state;
306 }
310 unsigned int numStates() const { return maxState() + 1; }
327
331 const MaterialData & getMaterialData(const THREAD_ID tid) const { return _material_data[tid]; }
333
337 const std::set<const MooseObject *> & getConsumers(Moose::MaterialDataType type) const;
338
343 {
344 _consumers[type].insert(object);
345 }
346
353 void setRecovering() { _recovering = true; }
354
365 void setRestartInPlace();
366
370 const PropRecord & getPropRecord(const unsigned int id) const;
371
375 bool isRestoredProperty(const std::string & name) const;
376
377protected:
380
382 std::array<PropsType, MaterialData::max_state + 1> _storage;
383
385 std::vector<std::optional<PropRecord>> _prop_records;
386
388 std::vector<unsigned int> _stateful_prop_id_to_prop_id;
389
391 std::map<Moose::MaterialDataType, std::set<const MooseObject *>> _consumers;
392
393 void sizeProps(MaterialProperties & mp, unsigned int size);
394
395private:
398 std::vector<MaterialProperties *>
399 initProps(const THREAD_ID tid, const Elem * elem, unsigned int side, unsigned int n_qpoints);
400
403 const unsigned int state,
404 const Elem * elem,
405 unsigned int side,
406 unsigned int n_qpoints);
407
409
413 static void shallowSwapData(const std::vector<unsigned int> & stateful_prop_ids,
414 MaterialProperties & data,
415 MaterialProperties & data_from);
416 static void shallowSwapDataBack(const std::vector<unsigned int> & stateful_prop_ids,
417 MaterialProperties & data,
418 MaterialProperties & data_from);
420
424 PropsType & setProps(const unsigned int state);
425
434 initAndSetProps(const Elem * elem, const unsigned int side, const unsigned int state);
435
437 unsigned int _max_state;
438
439 // You'd think a private mutex would work here, so I'll leave this
440 // in the namespace for when that happens, but CI thinks that can
441 // make us crash, so I'll just initialize this to Threads::spin_mtx
443
446
448 std::vector<MaterialData> _material_data;
449
450 typedef std::unordered_map<std::pair<const Elem *, unsigned int>,
451 std::map<unsigned int, std::vector<std::stringstream>>>
453
456
461
462 // Need to be able to eraseProperty from here
464
465 // Need to be able to initProps from here
467
468 // Need non-const props from here
469 friend void dataLoad(std::istream &, MaterialPropertyStorage &, void *);
470 friend void dataStore(std::ostream &, MaterialPropertyStorage &, void *);
471};
472
474MaterialPropertyStorage::props(const unsigned int state) const
475{
476 mooseAssert(state < _storage.size(), "Invalid material property state " + std::to_string(state));
477 return _storage[state];
478}
479
480inline const MaterialProperties &
481MaterialPropertyStorage::props(const Elem * elem, unsigned int side, const unsigned int state) const
482{
483 const auto find_elem = props(state).find(elem);
484 mooseAssert(find_elem != props(state).end(), "Material property does not have elem entry");
485 const auto find_side = find_elem->second.find(side);
486 mooseAssert(find_side != find_elem->second.end(), "Material property does not have side entry");
487 return find_side->second;
488}
489
490inline MaterialProperties &
491MaterialPropertyStorage::setProps(const Elem * elem, unsigned int side, const unsigned int state)
492{
493 return const_cast<MaterialProperties &>(std::as_const(*this).props(elem, side, state));
494}
495
496inline MaterialProperties &
498 unsigned int side,
499 const unsigned int state)
500{
501 return setProps(state)[elem][side];
502}
503
505MaterialPropertyStorage::setProps(const unsigned int state)
506{
507 return const_cast<MaterialPropertyStorage::PropsType &>(std::as_const(*this).props(state));
508}
509
510void dataStore(std::ostream & stream, MaterialPropertyStorage::PropRecord & record, void * context);
511void dataLoad(std::istream & stream, MaterialPropertyStorage::PropRecord & record, void * context);
void dataLoad(std::istream &stream, MaterialPropertyStorage &storage, void *context)
void dataStore(std::ostream &stream, MaterialPropertyStorage &storage, void *context)
unsigned int THREAD_ID
Definition MooseTypes.h:237
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts.
Definition HashMap.h:19
iterator find(const Key &k)
Definition HashMap.h:36
MaterialBases compute MaterialProperties.
Proxy for accessing MaterialPropertyStorage.
Registry class for material property IDs and names.
bool hasProperty(const std::string &name) const
Stores the stateful material properties computed by materials.
void setRestartInPlace()
Sets the loading of stateful material properties in place.
friend void dataLoad(std::istream &, MaterialPropertyStorage &, void *)
bool _recovering
Whether or not we're recovering; enforces a one-to-one mapping of stateful properties.
void initStatefulProps(const THREAD_ID tid, const std::vector< std::shared_ptr< MaterialBase > > &mats, const unsigned int n_qpoints, const Elem &elem, const unsigned int side=0)
Initialize stateful material properties.
friend void dataStore(std::ostream &, MaterialPropertyStorage &, void *)
void copy(const THREAD_ID tid, const Elem &elem_to, const Elem &elem_from, unsigned int side, unsigned int n_qpoints)
Copy material properties from elem_from to elem_to.
bool _restart_in_place
Whether or not we want to restart stateful properties in place.
MaterialPropertyRegistry & _registry
Shared registry (across storage objects) for property names and IDs.
void updateStatefulPropsForPRefinement(const processor_id_type pid, const std::vector< QpMap > &p_refinement_map, const libMesh::QBase &qrule, const libMesh::QBase &qrule_face, const THREAD_ID tid, const Elem &elem, const int input_side)
Based on the p-refinement flag of elem, either prolong (for refinement) or restrict (for coarsening) ...
const std::set< const MooseObject * > & getConsumers(Moose::MaterialDataType type) const
void swapBack(const THREAD_ID tid, const Elem &elem, unsigned int side)
Swap (shallow copy) material properties in MaterialPropertyStorage and MaterialDat Thread safe.
std::vector< unsigned int > _stateful_prop_id_to_prop_id
the vector of stateful property ids (the vector index is the map to stateful prop_id)
void swap(const THREAD_ID tid, const Elem &elem, unsigned int side)
Swap (shallow copy) material properties in MaterialData and MaterialPropertyStorage Thread safe.
std::map< Moose::MaterialDataType, std::set< const MooseObject * > > _consumers
The consumers of this storage.
void restrictStatefulProps(const std::vector< std::pair< unsigned int, QpMap > > &coarsening_map, const std::vector< const Elem * > &coarsened_element_children, const libMesh::QBase &qrule, const libMesh::QBase &qrule_face, const THREAD_ID tid, const Elem &elem, int input_side=-1)
Creates storage for newly created elements from mesh Adaptivity.
void sizeProps(MaterialProperties &mp, unsigned int size)
const PropsType & props(const unsigned int state=0) const
Access methods to the stored material property data with the given state state.
bool hasProperty(const std::string &prop_name) const
std::array< PropsType, MaterialData::max_state+1 > _storage
The actual storage.
std::vector< std::optional< PropRecord > > _prop_records
Property records indexed by property id (may be null)
libMesh::Threads::spin_mutex & _spin_mtx
std::vector< MaterialData > _material_data
The threaded material data.
std::vector< MaterialProperties * > initProps(const THREAD_ID tid, const Elem *elem, unsigned int side, unsigned int n_qpoints)
Initializes hashmap entries for element and side to proper qpoint and property count sizes.
void eraseProperty(const Elem *elem)
Remove the property storage and element pointer from internal data structures Use this when elements ...
static void shallowSwapDataBack(const std::vector< unsigned int > &stateful_prop_ids, MaterialProperties &data, MaterialProperties &data_from)
void shift()
Shift the material properties in time.
MaterialData & getMaterialData(const THREAD_ID tid)
libMesh::IntRange< unsigned int > statefulIndexRange() const
unsigned int _max_state
The maximum state (0 = current, 1 = old, 2 = older)
const std::vector< unsigned int > & statefulProps() const
bool isRestoredProperty(const std::string &name) const
HashMap< const Elem *, HashMap< unsigned int, MaterialProperties > > PropsType
Accessible type of the stored material property data.
FEProblemBase & _problem
Reference to the problem.
std::unordered_map< std::pair< const Elem *, unsigned int >, std::map< unsigned int, std::vector< std::stringstream > > > RestartableMapType
unsigned int addProperty(const std::string &prop_name, const std::type_info &type, const unsigned int state, const MaterialBase *const declarer)
Adds a property with the name prop_name, type type, and state state (0 = current, 1 = old,...
MaterialProperties & setProps(const Elem *elem, unsigned int side, const unsigned int state=0)
const MaterialPropertyRegistry & getMaterialPropertyRegistry() const
const PropRecord & getPropRecord(const unsigned int id) const
Get the property record associated with the material with id id.
RestartableMapType _restartable_map
The restartable data to be loaded in initStatefulProps() later.
void addConsumer(Moose::MaterialDataType type, const MooseObject *object)
Add object as the consumer of storage of type type.
std::optional< std::string > queryStatefulPropName(const unsigned int id) const
void prolongStatefulProps(processor_id_type pid, const std::vector< std::vector< QpMap > > &refinement_map, const libMesh::QBase &qrule, const libMesh::QBase &qrule_face, MaterialPropertyStorage &parent_material_props, const THREAD_ID tid, const Elem &elem, const int input_parent_side, const int input_child, const int input_child_side)
Creates storage for newly created elements from mesh Adaptivity.
libMesh::IntRange< unsigned int > stateIndexRange() const
static void shallowSwapData(const std::vector< unsigned int > &stateful_prop_ids, MaterialProperties &data, MaterialProperties &data_from)
Shallow copies of material properties.
const MaterialData & getMaterialData(const THREAD_ID tid) const
void setRecovering()
Sets the loading of stateful material properties to recover.
MaterialProperties & initAndSetProps(const Elem *elem, const unsigned int side, const unsigned int state)
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
Helper object for holding qp mapping info.
Definition MooseMesh.h:76
RedistributeProperties is used for its redistribute() callback, which ensures that any stateful prope...
MaterialDataType
MaterialData types.
Definition MooseTypes.h:746
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
Basic structure for storing information about a property.
std::string type
The type of this property.
unsigned int stateful_id
The stateful id in _storage used for this property, if any.
std::set< std::string > declarers
The material (type,name) that have declared this property.
bool restored
Whether or not this property was restored (stateful only)
unsigned int state
The max state requrested for this property (0 = current, 1 = old, ...)
bool stateful() const
Whether or not this property is stateful.