https://mooseframework.inl.gov
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
22 class MaterialBase;
23 class QpMap;
24 
25 // libMesh forward declarations
26 namespace libMesh
27 {
28 class QBase;
29 class Elem;
30 }
31 
33 
34 void dataStore(std::ostream & stream, MaterialPropertyStorage & storage, void * context);
35 void dataLoad(std::istream & stream, MaterialPropertyStorage & storage, void * context);
36 
43 {
44 public:
46 
50  struct PropRecord
51  {
53  bool stateful() const { return state > 0; }
55  std::set<std::string> declarers;
57  std::string type;
59  unsigned int stateful_id = invalid_uint;
61  unsigned int state = 0;
63  bool restored = false;
64  };
65 
104  const std::vector<std::vector<QpMap>> & refinement_map,
105  const libMesh::QBase & qrule,
106  const libMesh::QBase & qrule_face,
107  MaterialPropertyStorage & parent_material_props,
108  const THREAD_ID tid,
109  const Elem & elem,
110  const int input_parent_side,
111  const int input_child,
112  const int input_child_side);
113 
130  const std::vector<QpMap> & p_refinement_map,
131  const libMesh::QBase & qrule,
132  const libMesh::QBase & qrule_face,
133  const THREAD_ID tid,
134  const Elem & elem,
135  const int input_side);
136 
149  void restrictStatefulProps(const std::vector<std::pair<unsigned int, QpMap>> & coarsening_map,
150  const std::vector<const Elem *> & coarsened_element_children,
151  const libMesh::QBase & qrule,
152  const libMesh::QBase & qrule_face,
153  const THREAD_ID tid,
154  const Elem & elem,
155  int input_side = -1);
156 
168  void initStatefulProps(const THREAD_ID tid,
169  const std::vector<std::shared_ptr<MaterialBase>> & mats,
170  const unsigned int n_qpoints,
171  const Elem & elem,
172  const unsigned int side = 0);
173 
181  void shift();
182 
197  void copy(const THREAD_ID tid,
198  const Elem & elem_to,
199  const Elem & elem_from,
200  unsigned int side,
201  unsigned int n_qpoints);
202 
213  void copy(const THREAD_ID tid,
214  const Elem * elem_to,
215  const Elem * elem_from,
216  unsigned int side,
217  unsigned int n_qpoints);
218 
226  void swap(const THREAD_ID tid, const Elem & elem, unsigned int side);
227 
235  void swapBack(const THREAD_ID tid, const Elem & elem, unsigned int side);
236 
240  bool hasStatefulProperties() const { return maxState() > 0; }
241 
245  bool hasOlderProperties() const { return maxState() > 1; }
246 
255 
257 
260  const PropsType & props(const unsigned int state = 0) const;
261  const MaterialProperties &
262  props(const Elem * elem, unsigned int side, const unsigned int state = 0) const;
263  MaterialProperties & setProps(const Elem * elem, unsigned int side, const unsigned int state = 0);
265 
266  bool hasProperty(const std::string & prop_name) const { return _registry.hasProperty(prop_name); }
267 
277  unsigned int addProperty(const std::string & prop_name,
278  const std::type_info & type,
279  const unsigned int state,
280  const MaterialBase * const declarer);
281 
282  const std::vector<unsigned int> & statefulProps() const { return _stateful_prop_id_to_prop_id; }
283 
285 
289  std::optional<std::string> queryStatefulPropName(const unsigned int id) const;
290 
296  void eraseProperty(const Elem * elem);
297 
301  unsigned int maxState() const
302  {
303  mooseAssert(_max_state < _storage.size(), "Too big");
304  return _max_state;
305  }
309  unsigned int numStates() const { return maxState() + 1; }
314  {
316  }
323  {
325  }
326 
330  const MaterialData & getMaterialData(const THREAD_ID tid) const { return _material_data[tid]; }
331  MaterialData & getMaterialData(const THREAD_ID tid) { return _material_data[tid]; }
332 
339  void setRecovering() { _recovering = true; }
340 
351  void setRestartInPlace();
352 
356  const PropRecord & getPropRecord(const unsigned int id) const;
357 
361  bool isRestoredProperty(const std::string & name) const;
362 
363 protected:
365  std::array<PropsType, MaterialData::max_state + 1> _storage;
366 
368  std::vector<std::optional<PropRecord>> _prop_records;
369 
371  std::vector<unsigned int> _stateful_prop_id_to_prop_id;
372 
373  void sizeProps(MaterialProperties & mp, unsigned int size);
374 
375 private:
378  std::vector<MaterialProperties *>
379  initProps(const THREAD_ID tid, const Elem * elem, unsigned int side, unsigned int n_qpoints);
380 
383  const unsigned int state,
384  const Elem * elem,
385  unsigned int side,
386  unsigned int n_qpoints);
387 
389 
393  static void shallowSwapData(const std::vector<unsigned int> & stateful_prop_ids,
394  MaterialProperties & data,
395  MaterialProperties & data_from);
396  static void shallowSwapDataBack(const std::vector<unsigned int> & stateful_prop_ids,
397  MaterialProperties & data,
398  MaterialProperties & data_from);
400 
404  PropsType & setProps(const unsigned int state);
405 
414  initAndSetProps(const Elem * elem, const unsigned int side, const unsigned int state);
415 
417  unsigned int _max_state;
418 
419  // You'd think a private mutex would work here, so I'll leave this
420  // in the namespace for when that happens, but CI thinks that can
421  // make us crash, so I'll just initialize this to Threads::spin_mtx
423 
426 
428  std::vector<MaterialData> _material_data;
429 
430  typedef std::unordered_map<std::pair<const Elem *, unsigned int>,
431  std::map<unsigned int, std::vector<std::stringstream>>>
433 
436 
441 
442  // Need to be able to eraseProperty from here
444 
445  // Need to be able to initProps from here
447 
448  // Need non-const props from here
449  friend void dataLoad(std::istream &, MaterialPropertyStorage &, void *);
450  friend void dataStore(std::ostream &, MaterialPropertyStorage &, void *);
451 };
452 
454 MaterialPropertyStorage::props(const unsigned int state) const
455 {
456  mooseAssert(state < _storage.size(), "Invalid material property state " + std::to_string(state));
457  return _storage[state];
458 }
459 
460 inline const MaterialProperties &
461 MaterialPropertyStorage::props(const Elem * elem, unsigned int side, const unsigned int state) const
462 {
463  const auto find_elem = props(state).find(elem);
464  mooseAssert(find_elem != props(state).end(), "Material property does not have elem entry");
465  const auto find_side = find_elem->second.find(side);
466  mooseAssert(find_side != find_elem->second.end(), "Material property does not have side entry");
467  return find_side->second;
468 }
469 
470 inline MaterialProperties &
471 MaterialPropertyStorage::setProps(const Elem * elem, unsigned int side, const unsigned int state)
472 {
473  return const_cast<MaterialProperties &>(std::as_const(*this).props(elem, side, state));
474 }
475 
476 inline MaterialProperties &
478  unsigned int side,
479  const unsigned int state)
480 {
481  return setProps(state)[elem][side];
482 }
483 
485 MaterialPropertyStorage::setProps(const unsigned int state)
486 {
487  return const_cast<MaterialPropertyStorage::PropsType &>(std::as_const(*this).props(state));
488 }
489 
490 void dataStore(std::ostream & stream, MaterialPropertyStorage::PropRecord & record, void * context);
491 void dataLoad(std::istream & stream, MaterialPropertyStorage::PropRecord & record, void * context);
MaterialProperties & setProps(const Elem *elem, unsigned int side, const unsigned int state=0)
void shift()
Shift the material properties in time.
friend void dataStore(std::ostream &, MaterialPropertyStorage &, void *)
HashMap< const Elem *, HashMap< unsigned int, MaterialProperties > > PropsType
Accessible type of the stored material property data.
std::vector< MaterialData > _material_data
The threaded material data.
const unsigned int invalid_uint
HashMap is an abstraction for dictionary data type, we make it thread-safe by locking inserts...
Definition: HashMap.h:18
Registry class for material property IDs and names.
void eraseProperty(const Elem *elem)
Remove the property storage and element pointer from internal data structures Use this when elements ...
Stores the stateful material properties computed by materials.
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) ...
const MaterialPropertyRegistry & getMaterialPropertyRegistry() const
Helper object for holding qp mapping info.
Definition: MooseMesh.h:69
void sizeProps(MaterialProperties &mp, unsigned int size)
bool hasProperty(const std::string &name) const
libMesh::Threads::spin_mutex & _spin_mtx
std::vector< std::optional< PropRecord > > _prop_records
Property records indexed by property id (may be null)
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
iterator find(const Key &k)
Definition: HashMap.h:36
bool restored
Whether or not this property was restored (stateful only)
static void shallowSwapData(const std::vector< unsigned int > &stateful_prop_ids, MaterialProperties &data, MaterialProperties &data_from)
Shallow copies of material properties.
static void shallowSwapDataBack(const std::vector< unsigned int > &stateful_prop_ids, MaterialProperties &data, MaterialProperties &data_from)
MaterialPropertyStorage(MaterialPropertyRegistry &registry)
const std::vector< unsigned int > & statefulProps() const
void dataLoad(std::istream &stream, MaterialPropertyStorage &storage, void *context)
uint8_t processor_id_type
void setRestartInPlace()
Sets the loading of stateful material properties in place.
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
void swapBack(const THREAD_ID tid, const Elem &elem, unsigned int side)
Swap (shallow copy) material properties in MaterialPropertyStorage and MaterialDat Thread safe...
std::optional< std::string > queryStatefulPropName(const unsigned int id) const
bool isRestoredProperty(const std::string &name) const
bool _recovering
Whether or not we&#39;re recovering; enforces a one-to-one mapping of stateful properties.
unsigned int numStates() const
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...
unsigned int state
The max state requrested for this property (0 = current, 1 = old, ...)
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...
RestartableMapType _restartable_map
The restartable data to be loaded in initStatefulProps() later.
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) ...
Basic structure for storing information about a property.
const PropsType & props(const unsigned int state=0) const
Access methods to the stored material property data with the given state state.
std::unordered_map< std::pair< const Elem *, unsigned int >, std::map< unsigned int, std::vector< std::stringstream > > > RestartableMapType
bool hasProperty(const std::string &prop_name) const
MaterialData & getMaterialData(const THREAD_ID tid)
Proxy for accessing MaterialPropertyStorage.
Definition: MaterialData.h:33
std::array< PropsType, MaterialData::max_state+1 > _storage
The actual storage.
MaterialPropertyRegistry & _registry
Shared registry (across storage objects) for property names and IDs.
friend void dataLoad(std::istream &, MaterialPropertyStorage &, void *)
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 swap(const THREAD_ID tid, const Elem &elem, unsigned int side)
Swap (shallow copy) material properties in MaterialData and MaterialPropertyStorage Thread safe...
const MaterialData & getMaterialData(const THREAD_ID tid) const
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.
bool stateful() const
Whether or not this property is stateful.
bool _restart_in_place
Whether or not we want to restart stateful properties in place.
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.
const PropRecord & getPropRecord(const unsigned int id) const
Get the property record associated with the material with id id.
void setRecovering()
Sets the loading of stateful material properties to recover.
MaterialBases compute MaterialProperties.
Definition: MaterialBase.h:62
unsigned int _max_state
The maximum state (0 = current, 1 = old, 2 = older)
void dataStore(std::ostream &stream, MaterialPropertyStorage &storage, void *context)
unsigned int stateful_id
The stateful id in _storage used for this property, if any.
RedistributeProperties is used for its redistribute() callback, which ensures that any stateful prope...
unsigned int THREAD_ID
Definition: MooseTypes.h:209
std::set< std::string > declarers
The material (type,name) that have declared this property.
MaterialProperties & initAndSetProps(const Elem *elem, const unsigned int side, const unsigned int state)
libMesh::IntRange< unsigned int > statefulIndexRange() const
std::string type
The type of this property.