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"
15 
16 // libMesh
17 #include "libmesh/elem.h"
18 
19 #include <vector>
20 
21 class Material;
22 
28 {
29 public:
31  virtual ~MaterialData();
32 
36  void release();
37 
41  void resize(unsigned int n_qpoints);
42 
47  unsigned int nQPoints();
48 
55  template <typename T>
56  MaterialProperty<T> & declareProperty(const std::string & prop_name);
57 
61  template <typename T>
62  MaterialProperty<T> & declarePropertyOld(const std::string & prop_name);
63 
67  template <typename T>
68  MaterialProperty<T> & declarePropertyOlder(const std::string & prop_name);
69 
76  template <typename T>
77  ADMaterialPropertyObject<T> & declareADProperty(const std::string & prop_name);
78 
80  void copy(const Elem & elem_to, const Elem & elem_from, unsigned int side);
81 
83  void swap(const Elem & elem, unsigned int side = 0);
84 
86  void reinit(const std::vector<std::shared_ptr<Material>> & mats);
87 
89  void reset(const std::vector<std::shared_ptr<Material>> & mats);
90 
92  void swapBack(const Elem & elem, unsigned int side = 0);
93 
95 
104 
107  template <typename T>
108  bool haveProperty(const std::string & prop_name) const;
109 
111 
117  template <typename T>
118  MaterialProperty<T> & getProperty(const std::string & prop_name);
119  template <typename T>
120  ADMaterialPropertyObject<T> & getADProperty(const std::string & prop_name);
121  template <typename T>
122  MaterialProperty<T> & getPropertyOld(const std::string & prop_name);
123  template <typename T>
124  MaterialProperty<T> & getPropertyOlder(const std::string & prop_name);
126 
130  bool isSwapped();
131 
136 
144  unsigned int getPropertyId(const std::string & prop_name) const
145  {
146  return _storage.getPropertyId(prop_name);
147  }
148 
149 protected:
152 
154  unsigned int _n_qpoints;
155 
162 
166  template <typename T>
167  void resizeProps(unsigned int size, bool declared_ad = false);
168 
170  bool _swapped;
171 
172 private:
173  template <typename T>
175  declareHelper(MaterialProperties & props, const std::string & prop_name, unsigned int prop_id);
176 
177  template <typename T>
179  declareADHelper(MaterialProperties & props, const std::string & prop_name, unsigned int prop_id);
180 };
181 
182 template <typename T>
183 inline bool
184 MaterialData::haveProperty(const std::string & prop_name) const
185 {
186  if (!_storage.hasProperty(prop_name))
187  return false;
188 
189  unsigned int prop_id = getPropertyId(prop_name);
190  if (prop_id >= _props.size())
191  return false; // the property id exists, but the property was not created in this instance of
192  // the material type
193 
194  return dynamic_cast<const MaterialProperty<T> *>(_props[prop_id]) != nullptr;
195 }
196 
197 template <typename T>
198 void
199 MaterialData::resizeProps(unsigned int size, bool declared_ad)
200 {
201  auto n = size + 1;
202  if (_props.size() < n)
203  _props.resize(n, nullptr);
204  if (_props_old.size() < n)
205  _props_old.resize(n, nullptr);
206  if (_props_older.size() < n)
207  _props_older.resize(n, nullptr);
208 
209  if (_props[size] == nullptr)
210  _props[size] = new ADMaterialPropertyObject<T>(declared_ad);
211  // This branch is necessary in case the frist call to resizeProps for this property id was
212  // initiated through a getMaterialProperty call, which will have declared_ad = false
213  else if (declared_ad)
214  _props[size]->markAD(true);
215  if (_props_old[size] == nullptr)
217  if (_props_older[size] == nullptr)
219 }
220 
221 template <typename T>
223 MaterialData::declareProperty(const std::string & prop_name)
224 {
225  return declareHelper<T>(_props, prop_name, _storage.addProperty(prop_name));
226 }
227 
228 template <typename T>
230 MaterialData::declareADProperty(const std::string & prop_name)
231 {
232  return declareADHelper<T>(_props, prop_name, _storage.addProperty(prop_name));
233 }
234 
235 template <typename T>
237 MaterialData::declarePropertyOld(const std::string & prop_name)
238 {
239  // TODO: add mooseDeprecated("'declarePropertyOld' is deprecated an no longer necessary");
240  return getPropertyOld<T>(prop_name);
241 }
242 
243 template <typename T>
245 MaterialData::declarePropertyOlder(const std::string & prop_name)
246 {
247  // TODO: add mooseDeprecated("'declarePropertyOlder' is deprecated an no longer necessary");
248  return getPropertyOlder<T>(prop_name);
249 }
250 
251 template <typename T>
254  const std::string & libmesh_dbg_var(prop_name),
255  unsigned int prop_id)
256 {
257  resizeProps<T>(prop_id);
258  auto prop = dynamic_cast<MaterialProperty<T> *>(props[prop_id]);
259  mooseAssert(prop != nullptr, "Internal error in declaring material property: " + prop_name);
260  return *prop;
261 }
262 
263 template <typename T>
266  const std::string & libmesh_dbg_var(prop_name),
267  unsigned int prop_id)
268 {
269  resizeProps<T>(prop_id, true);
270  auto prop = dynamic_cast<ADMaterialPropertyObject<T> *>(props[prop_id]);
271  mooseAssert(prop != nullptr, "Internal error in declaring material property: " + prop_name);
272  return *prop;
273 }
274 
275 template <typename T>
277 MaterialData::getProperty(const std::string & name)
278 {
279  auto prop_id = getPropertyId(name);
280  resizeProps<T>(prop_id);
281  auto prop = dynamic_cast<MaterialProperty<T> *>(_props[prop_id]);
282  if (!prop)
283  mooseError("Material has no property named: " + name);
284  return *prop;
285 }
286 
287 template <typename T>
289 MaterialData::getADProperty(const std::string & name)
290 {
291  auto prop_id = getPropertyId(name);
292  resizeProps<T>(prop_id);
293  auto prop = dynamic_cast<ADMaterialPropertyObject<T> *>(_props[prop_id]);
294  if (!prop)
295  mooseError("Material has no property named: " + name);
296  return *prop;
297 }
298 
299 template <typename T>
301 MaterialData::getPropertyOld(const std::string & name)
302 {
303  return declareHelper<T>(_props_old, name, _storage.addPropertyOld(name));
304 }
305 
306 template <typename T>
308 MaterialData::getPropertyOlder(const std::string & name)
309 {
310  return declareHelper<T>(_props_older, name, _storage.addPropertyOlder(name));
311 }
312 
const MaterialPropertyStorage & getMaterialPropertyStorage() const
Provide read-only access to the underlying MaterialPropertyStorage object.
Definition: MaterialData.h:135
virtual ~MaterialData()
Definition: MaterialData.C:18
void release()
Calls the destroy() methods for the properties currently stored.
Definition: MaterialData.C:21
MaterialProperty< T > & getPropertyOlder(const std::string &prop_name)
Definition: MaterialData.h:308
bool haveProperty(const std::string &prop_name) const
Returns true if the property exists - defined by any material (i.e.
Definition: MaterialData.h:184
void reset(const std::vector< std::shared_ptr< Material >> &mats)
Calls the reset method of Materials to ensure that they are in a proper state.
Definition: MaterialData.C:74
unsigned int addProperty(const std::string &prop_name)
The addProperty functions are idempotent - calling multiple times with the same name will provide the...
void swapBack(const Elem &elem, unsigned int side=0)
material properties for given element (and possible side)
Definition: MaterialData.C:81
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
MaterialPropertyStorage & _storage
Reference to the MaterialStorage class.
Definition: MaterialData.h:151
Container for storing material properties.
unsigned int addPropertyOld(const std::string &prop_name)
ADMaterialPropertyObject< T > & declareADProperty(const std::string &prop_name)
Declare the AD property named "name".
Definition: MaterialData.h:230
Stores the stateful material properties computed by materials.
unsigned int getPropertyId(const std::string &prop_name) const
Wrapper for MaterialStorage::getPropertyId.
Definition: MaterialData.h:144
MaterialProperties _props_old
Definition: MaterialData.h:159
MaterialProperties _props_older
Definition: MaterialData.h:160
MaterialProperty< T > & declareProperty(const std::string &prop_name)
Declare the Real valued property named "name".
Definition: MaterialData.h:223
unsigned int addPropertyOlder(const std::string &prop_name)
ADMaterialPropertyObject< T > & declareADHelper(MaterialProperties &props, const std::string &prop_name, unsigned int prop_id)
MaterialProperty< T > & declareHelper(MaterialProperties &props, const std::string &prop_name, unsigned int prop_id)
void resizeProps(unsigned int size, bool declared_ad=false)
Calls resizeProps helper function for regular material properties.
Definition: MaterialData.h:199
MaterialProperty< T > & declarePropertyOlder(const std::string &prop_name)
Declare the Real valued property named prop_name.
Definition: MaterialData.h:245
unsigned int _n_qpoints
Number of quadrature points.
Definition: MaterialData.h:154
bool isSwapped()
Returns true if the stateful material is in a swapped state.
Definition: MaterialData.C:91
MaterialProperties & propsOld()
Definition: MaterialData.h:101
MaterialProperty< T > & declarePropertyOld(const std::string &prop_name)
Declare the Real valued property prop_name.
Definition: MaterialData.h:237
MaterialProperties & propsOlder()
Definition: MaterialData.h:102
ADMaterialPropertyObject< T > & getADProperty(const std::string &prop_name)
Definition: MaterialData.h:289
void copy(const Elem &elem_to, const Elem &elem_from, unsigned int side)
copy material properties from one element to another
Definition: MaterialData.C:51
unsigned int getPropertyId(const std::string &prop_name)
Returns the property ID for the given prop_name, adding the property and creating a new ID if it hasn...
MaterialProperties _props
Definition: MaterialData.h:158
MaterialProperty< T > & getProperty(const std::string &prop_name)
Methods for retieving a MaterialProperty object.
Definition: MaterialData.h:277
Materials compute MaterialProperties.
Definition: Material.h:46
MaterialData(MaterialPropertyStorage &storage)
Definition: MaterialData.C:13
void swap(const Elem &elem, unsigned int side=0)
material properties for given element (and possible side)
Definition: MaterialData.C:57
PetscInt n
bool _swapped
Status of storage swapping (calling swap sets this to true; swapBack sets it to false) ...
Definition: MaterialData.h:170
bool hasProperty(const std::string &prop_name) const
Concrete definition of a parameter value for a specified type.
unsigned int nQPoints()
Returns the number of quadrature points the material properties support/hold.
Definition: MaterialData.C:45
Proxy for accessing MaterialPropertyStorage.
Definition: MaterialData.h:27
MaterialProperty< T > & getPropertyOld(const std::string &prop_name)
Definition: MaterialData.h:301
MaterialProperties & props()
Methods for retrieving MaterialProperties object.
Definition: MaterialData.h:100
void resize(unsigned int n_qpoints)
Resize the data to hold properties for n_qpoints quadrature points.
Definition: MaterialData.C:29
void reinit(const std::vector< std::shared_ptr< Material >> &mats)
Reinit material properties for given element (and possible side)
Definition: MaterialData.C:67