www.mooseframework.org
MaterialData.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "MaterialProperty.h"
13 #include "Moose.h"
14 #include "MooseUtils.h"
15 
16 // libMesh
17 #include "libmesh/elem.h"
18 
19 #include <vector>
20 #include <memory>
21 #include <typeinfo>
22 
24 class MooseObject;
25 class Material;
26 class XFEM;
27 class MaterialBase;
28 
34 {
35 public:
36  MaterialData(MaterialPropertyStorage & storage, const THREAD_ID tid);
37 
39  static constexpr unsigned int max_state = 2;
40 
44  void resize(unsigned int n_qpoints);
45 
50  unsigned int nQPoints() const { return _n_qpoints; }
51 
53  void copy(const Elem & elem_to, const Elem & elem_from, unsigned int side);
54 
56  void swap(const Elem & elem, unsigned int side = 0);
57 
65  template <typename MatContainer>
66  void reinit(const MatContainer & mats);
67 
69  void reset(const std::vector<std::shared_ptr<MaterialBase>> & mats);
70 
72  void swapBack(const Elem & elem, unsigned int side = 0);
73 
79  const MaterialProperties & props(const unsigned int state = 0) const;
81  MaterialProperties & props(const unsigned int state = 0);
83 
84  template <typename T, bool is_ad>
85  bool haveGenericProperty(const std::string & prop_name) const;
86 
88  template <typename T>
89  bool haveProperty(const std::string & prop_name) const
90  {
91  return haveGenericProperty<T, false>(prop_name);
92  }
93 
95  template <typename T>
96  bool haveADProperty(const std::string & prop_name) const
97  {
98  return haveGenericProperty<T, true>(prop_name);
99  }
100 
110  template <typename T, bool is_ad = false>
111  GenericMaterialProperty<T, is_ad> & getProperty(const std::string & prop_name,
112  const unsigned int state,
113  const MooseObject & requestor)
114  {
115  return getPropertyHelper<T, is_ad, false>(prop_name, state, requestor);
116  }
125  template <typename T, bool is_ad>
126  GenericMaterialProperty<T, is_ad> & declareProperty(const std::string & prop_name,
127  const MooseObject & requestor)
128  {
129  return getPropertyHelper<T, is_ad, true>(prop_name, 0, requestor);
130  }
131 
135  bool isSwapped() const { return _swapped; }
136 
141 
145  class XFEMKey
146  {
147  friend class XFEM;
148  XFEMKey() {}
149  XFEMKey(const XFEM &) {}
150  };
151 
162 
166  bool hasProperty(const std::string & prop_name) const;
167 
175  unsigned int getPropertyId(const std::string & prop_name) const;
176 
182  void onlyResizeIfSmaller(bool flag) { _resize_only_if_smaller = flag; };
183 
188 
194  void eraseProperty(const Elem * elem);
195 
196 private:
199 
202 
204  unsigned int _n_qpoints;
205 
207  std::array<MaterialProperties, max_state + 1> _props;
208 
209  unsigned int addPropertyHelper(const std::string & prop_name,
210  const std::type_info & type,
211  const unsigned int state,
212  const MaterialBase * const declarer);
213 
214  template <typename T, bool is_ad, bool declare>
215  GenericMaterialProperty<T, is_ad> & getPropertyHelper(const std::string & prop_name,
216  const unsigned int state,
217  const MooseObject & requestor);
218 
219  static void mooseErrorHelper(const MooseObject & object, const std::string_view & error);
220 
224  const MaterialBase & castRequestorToDeclarer(const MooseObject & requestor) const;
225 
227  bool _swapped;
228 
232 
234  unsigned int getMaxStateRequested(const unsigned int prop_id) const;
235 };
236 
237 inline const MaterialProperties &
238 MaterialData::props(const unsigned int state) const
239 {
240  mooseAssert(_props.size() > state, "Invalid state");
241  return _props[state];
242 }
243 
244 inline MaterialProperties &
245 MaterialData::props(const unsigned int state)
246 {
247  mooseAssert(_props.size() > state, "Invalid state");
248  return _props[state];
249 }
250 
251 template <typename T, bool is_ad>
252 inline bool
253 MaterialData::haveGenericProperty(const std::string & prop_name) const
254 {
255  if (!hasProperty(prop_name))
256  return false;
257 
258  const auto prop_id = getPropertyId(prop_name);
259  // the property id exists, but the property was not created in this instance of the material type
260  if (prop_id >= props(0).size())
261  return false;
262 
263  const PropertyValue * const base_prop = props(0).queryValue(prop_id);
264  return dynamic_cast<const GenericMaterialProperty<T, is_ad> *>(base_prop) != nullptr;
265 }
266 
267 template <typename T, bool is_ad, bool declare>
269 MaterialData::getPropertyHelper(const std::string & prop_name,
270  const unsigned int state,
271  const MooseObject & requestor)
272 {
273  if constexpr (is_ad)
274  mooseAssert(state == 0, "Cannot request/declare AD properties for states other than zero");
275  if constexpr (declare)
276  mooseAssert(state == 0, "Cannot declare properties for states other than zero");
277 
278  // Register/get the ID of the property
279  const auto prop_id = addPropertyHelper(
280  prop_name, typeid(T), state, declare ? &castRequestorToDeclarer(requestor) : nullptr);
281  const auto size = prop_id + 1;
282 
283  // Initialize the states that we need
284  for (const auto state_i : make_range(getMaxStateRequested(prop_id) + 1))
285  {
286  auto & entry = props(state_i);
287  if (entry.size() < size)
288  entry.resize(size, {});
289  // if we are not declaring the property we initialize only what we need (the requested state)
290  if (!entry.hasValue(prop_id) && (declare || state_i == state))
291  {
292  if (state_i == 0)
293  entry.setPointer(
294  prop_id, std::move(std::make_unique<GenericMaterialProperty<T, is_ad>>(prop_id)), {});
295  else
296  entry.setPointer(prop_id, std::move(std::make_unique<MaterialProperty<T>>(prop_id)), {});
297  }
298  }
299 
300  // Should be available now
301  auto & base_prop = props(state)[prop_id];
302 
303  // In the event that this property was already declared/requested, make sure
304  // that the types are consistent
305  auto prop = dynamic_cast<GenericMaterialProperty<T, is_ad> *>(&base_prop);
306  if (!prop)
307  {
308  constexpr std::string_view action = declare ? "declared" : "requested";
309  constexpr auto is_ad_to_str = [](const bool is_ad_bool)
310  { return std::string_view(is_ad_bool ? "AD" : "non-AD"); };
311  constexpr std::string_view ad_type = is_ad_to_str(is_ad);
312 
313  std::stringstream error;
314  error << "The " << action << " " << ad_type << " "
315  << "material property '" + prop_name + "' of type '" << MooseUtils::prettyCppType<T>()
316  << "'\nis already retrieved or declared as a " << is_ad_to_str(base_prop.isAD())
317  << " property of type '" << base_prop.type() << "'.";
318  mooseErrorHelper(requestor, error.str());
319  }
320 
321  return *prop;
322 }
323 
324 template <typename MatContainer>
325 void
326 MaterialData::reinit(const MatContainer & mats)
327 {
328  for (const auto & mat : mats)
329  mat->computeProperties();
330 }
const MaterialPropertyStorage & getMaterialPropertyStorage() const
Provide read-only access to the underlying MaterialPropertyStorage object.
Definition: MaterialData.h:140
bool isSwapped() const
Returns true if the stateful material is in a swapped state.
Definition: MaterialData.h:135
bool hasProperty(const std::string &prop_name) const
Definition: MaterialData.C:74
bool haveGenericProperty(const std::string &prop_name) const
Definition: MaterialData.h:253
void onlyResizeIfSmaller(bool flag)
Set _resize_only_if_smaller to perform a non-destructive resize.
Definition: MaterialData.h:182
const MaterialBase & castRequestorToDeclarer(const MooseObject &requestor) const
Helper for casting requestor to a MaterialBase in addPropertyHelper() (templated) ...
Definition: MaterialData.C:101
bool haveProperty(const std::string &prop_name) const
Returns true if the regular material property exists - defined by any material.
Definition: MaterialData.h:89
void swapBack(const Elem &elem, unsigned int side=0)
material properties for given element (and possible side)
Definition: MaterialData.C:58
MaterialPropertyStorage & _storage
Reference to the MaterialStorage class.
Definition: MaterialData.h:198
void reinit(const MatContainer &mats)
Reinit material properties for given element (and possible side)
Definition: MaterialData.h:326
bool _resize_only_if_smaller
Use non-destructive resize of material data (calling resize() will not reduce size).
Definition: MaterialData.h:231
std::array< MaterialProperties, max_state+1 > _props
The underlying property data.
Definition: MaterialData.h:207
Stores the stateful material properties computed by materials.
unsigned int getPropertyId(const std::string &prop_name) const
Wrapper for MaterialStorage::getPropertyId.
Definition: MaterialData.C:80
bool isOnlyResizeIfSmaller() const
Check value of _resize_only_if_smaller.
Definition: MaterialData.h:187
void eraseProperty(const Elem *elem)
Remove the property storage and element pointer from MaterialPropertyStorage data structures Use this...
Definition: MaterialData.C:86
MaterialData(MaterialPropertyStorage &storage, const THREAD_ID tid)
Definition: MaterialData.C:15
unsigned int _n_qpoints
Number of quadrature points.
Definition: MaterialData.h:204
unsigned int addPropertyHelper(const std::string &prop_name, const std::type_info &type, const unsigned int state, const MaterialBase *const declarer)
Definition: MaterialData.C:92
const T * queryValue(const std::size_t i) const
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:33
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
void copy(const Elem &elem_to, const Elem &elem_from, unsigned int side)
copy material properties from one element to another
Definition: MaterialData.C:35
Abstract definition of a property value.
const MaterialProperties & props(const unsigned int state=0) const
Definition: MaterialData.h:238
static constexpr unsigned int max_state
The max time state supported (2 = older)
Definition: MaterialData.h:39
Materials compute MaterialProperties.
Definition: Material.h:34
unsigned int getMaxStateRequested(const unsigned int prop_id) const
maximum state id requested for a property
Definition: MaterialData.C:109
MaterialPropertyStorage & getMaterialPropertyStorageForXFEM(const XFEMKey)
Provide write-only access to the underlying MaterialPropertyStorage object JUST FOR XFEM...
Definition: MaterialData.h:161
bool haveADProperty(const std::string &prop_name) const
Returns true if the AD material property exists - defined by any material.
Definition: MaterialData.h:96
void reset(const std::vector< std::shared_ptr< MaterialBase >> &mats)
Calls the reset method of Materials to ensure that they are in a proper state.
Definition: MaterialData.C:51
void swap(const Elem &elem, unsigned int side=0)
material properties for given element (and possible side)
Definition: MaterialData.C:41
GenericMaterialProperty< T, is_ad > & getProperty(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
Retrieves a material property.
Definition: MaterialData.h:111
bool _swapped
Status of storage swapping (calling swap sets this to true; swapBack sets it to false) ...
Definition: MaterialData.h:227
IntRange< T > make_range(T beg, T end)
static void mooseErrorHelper(const MooseObject &object, const std::string_view &error)
Definition: MaterialData.C:68
unsigned int nQPoints() const
Returns the number of quadrature points the material properties support/hold.
Definition: MaterialData.h:50
Proxy for accessing MaterialPropertyStorage.
Definition: MaterialData.h:33
Key that provides access to only the XFEM class.
Definition: MaterialData.h:145
MaterialBases compute MaterialProperties.
Definition: MaterialBase.h:60
GenericMaterialProperty< T, is_ad > & declareProperty(const std::string &prop_name, const MooseObject &requestor)
Declares a material property.
Definition: MaterialData.h:126
void resize(unsigned int n_qpoints)
Resize the data to hold properties for n_qpoints quadrature points.
Definition: MaterialData.C:21
unsigned int THREAD_ID
Definition: MooseTypes.h:198
XFEMKey(const XFEM &)
Definition: MaterialData.h:149
GenericMaterialProperty< T, is_ad > & getPropertyHelper(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
Definition: MaterialData.h:269
const THREAD_ID _tid
The thread id.
Definition: MaterialData.h:201