www.mooseframework.org
MaterialProperty.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 <vector>
13 
14 #include "MooseADWrapper.h"
15 #include "MooseArray.h"
16 #include "DataIO.h"
17 
18 #include "libmesh/libmesh_common.h"
19 #include "libmesh/tensor_value.h"
20 #include "libmesh/vector_value.h"
21 
22 class PropertyValue;
23 
28 template <typename P>
29 PropertyValue * _init_helper(int size, PropertyValue * prop, const P * the_type, bool use_ad);
30 
35 {
36 public:
37  virtual ~PropertyValue(){};
38 
42  virtual std::string type() = 0;
43 
47  virtual PropertyValue * init(int size) = 0;
48 
49  virtual unsigned int size() const = 0;
50 
54  virtual void resize(int n) = 0;
55 
56  virtual void swap(PropertyValue * rhs) = 0;
57 
65  virtual void
66  qpCopy(const unsigned int to_qp, PropertyValue * rhs, const unsigned int from_qp) = 0;
67 
68  // save/restore in a file
69  virtual void store(std::ostream & stream) = 0;
70  virtual void load(std::istream & stream) = 0;
71 
73  virtual void markAD(bool use_ad) = 0;
74 
79  virtual void copyDualNumberToValue(const unsigned int i) = 0;
80 };
81 
82 template <>
83 inline void
84 dataStore(std::ostream & stream, PropertyValue *& p, void * /*context*/)
85 {
86  p->store(stream);
87 }
88 
89 template <>
90 inline void
91 dataLoad(std::istream & stream, PropertyValue *& p, void * /*context*/)
92 {
93  p->load(stream);
94 }
95 
100 template <typename T>
101 class MaterialProperty : public PropertyValue
102 {
103 public:
105  MaterialProperty(bool use_ad = false) : PropertyValue(), _use_ad(use_ad)
106  { /* */
107  }
108 
109  virtual ~MaterialProperty() {}
110 
114  const std::vector<MooseADWrapper<T>> & get() const { return _value; }
115 
119  std::vector<MooseADWrapper<T>> & set() { return _value; }
120 
124  virtual std::string type() override;
125 
129  virtual PropertyValue * init(int size) override;
130 
134  virtual void resize(int n) override;
135 
136  virtual unsigned int size() const override { return _value.size(); }
137 
141  T & operator[](const unsigned int i) { return _value[i].value(); }
142 
146  const T & operator[](const unsigned int i) const { return _value[i].value(); }
147 
151  virtual void swap(PropertyValue * rhs) override;
152 
160  virtual void
161  qpCopy(const unsigned int to_qp, PropertyValue * rhs, const unsigned int from_qp) override;
162 
166  virtual void store(std::ostream & stream) override;
167 
171  virtual void load(std::istream & stream) override;
172 
177  void copyDualNumberToValue(const unsigned int i) override { _value[i].copyDualNumberToValue(); }
178 
179  void markAD(bool use_ad) override;
180 
181 private:
184  {
185  mooseError("Material properties must be assigned to references (missing '&')");
186  }
187 
190  {
191  mooseError("Material properties must be assigned to references (missing '&')");
192  }
193 
194 protected:
196  bool _use_ad;
198  std::vector<MooseADWrapper<T>> _value;
199 };
200 
201 // ------------------------------------------------------------
202 // Material::Property<> class inline methods
203 
204 template <typename T>
205 inline void
207 {
208  _use_ad = use_ad;
209  for (auto && moose_wrapper : _value)
210  moose_wrapper.markAD(use_ad);
211 }
212 
213 template <typename T>
214 inline std::string
216 {
217  return typeid(T).name();
218 }
219 
220 template <typename T>
221 inline PropertyValue *
223 {
224  return _init_helper(size, this, static_cast<T *>(0), _use_ad);
225 }
226 
227 template <typename T>
228 inline void
230 {
231  auto diff = n - static_cast<int>(_value.size());
232  if (diff < 0)
233  _value.erase(_value.end() + diff, _value.end());
234  else
235  for (decltype(diff) i = 0; i < diff; ++i)
236  _value.emplace_back(MooseADWrapper<T>(_use_ad));
237 }
238 
239 template <typename T>
240 inline void
242 {
243  mooseAssert(rhs != NULL, "Assigning NULL?");
244  _value.swap(cast_ptr<MaterialProperty<T> *>(rhs)->_value);
245 }
246 
247 template <typename T>
248 inline void
249 MaterialProperty<T>::qpCopy(const unsigned int to_qp,
250  PropertyValue * rhs,
251  const unsigned int from_qp)
252 {
253  mooseAssert(rhs != NULL, "Assigning NULL?");
254  _value[to_qp] = cast_ptr<const MaterialProperty<T> *>(rhs)->_value[from_qp];
255 }
256 
257 template <typename T>
258 inline void
259 MaterialProperty<T>::store(std::ostream & stream)
260 {
261  for (unsigned int i = 0; i < size(); i++)
262  storeHelper(stream, _value[i], NULL);
263 }
264 
265 template <typename T>
266 inline void
267 MaterialProperty<T>::load(std::istream & stream)
268 {
269  for (unsigned int i = 0; i < size(); i++)
270  loadHelper(stream, _value[i], NULL);
271 }
272 
273 template <typename T>
275 {
276 public:
277  ADMaterialPropertyObject(bool use_ad = false) : MaterialProperty<T>(use_ad) {}
278 
282  typename MooseADWrapper<T>::DNType & operator[](const unsigned int i)
283  {
284  return this->_value[i].dn();
285  }
289  const typename MooseADWrapper<T>::DNType & operator[](const unsigned int i) const
290  {
291  return this->_value[i].dn();
292  }
293 };
294 
298 class MaterialProperties : public std::vector<PropertyValue *>
299 {
300 public:
302 
303  virtual ~MaterialProperties() {}
304 
308  typedef std::vector<PropertyValue *>::iterator iterator;
309 
313  typedef std::vector<PropertyValue *>::const_iterator const_iterator;
314 
318  void destroy()
319  {
320  for (iterator k = begin(); k != end(); ++k)
321  delete *k;
322  }
323 
329  void resizeItems(unsigned int n_qpoints)
330  {
331  for (iterator k = begin(); k != end(); ++k)
332  if (*k != NULL)
333  (*k)->resize(n_qpoints);
334  }
335 };
336 
337 template <>
338 inline void
339 dataStore(std::ostream & stream, MaterialProperties & v, void * context)
340 {
341  // Cast this to a vector so we can just piggy back on the vector store capability
342  std::vector<PropertyValue *> & mat_props = static_cast<std::vector<PropertyValue *> &>(v);
343 
344  storeHelper(stream, mat_props, context);
345 }
346 
347 template <>
348 inline void
349 dataLoad(std::istream & stream, MaterialProperties & v, void * context)
350 {
351  // Cast this to a vector so we can just piggy back on the vector store capability
352  std::vector<PropertyValue *> & mat_props = static_cast<std::vector<PropertyValue *> &>(v);
353 
354  loadHelper(stream, mat_props, context);
355 }
356 
357 // Scalar Init Helper Function
358 template <typename P>
360 _init_helper(int size, PropertyValue * /*prop*/, const P *, bool use_ad)
361 {
363  copy->resize(size);
364  return copy;
365 }
366 
std::vector< PropertyValue * >::iterator iterator
Parameter map iterator.
virtual void swap(PropertyValue *rhs)=0
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
virtual std::string type() override
String identifying the type of parameter stored.
MaterialProperty(const MaterialProperty< T > &)
private copy constructor to avoid shallow copying of material properties
bool _use_ad
Whether this property was declared as AD.
Container for storing material properties.
virtual void markAD(bool use_ad)=0
Mark whether this property is in AD mode. This method is necessary for switching the state after swap...
MooseADWrapper< T >::DNType & operator[](const unsigned int i)
Get element i out of the array as a writeable reference.
ADMaterialPropertyObject(bool use_ad=false)
const MooseADWrapper< T >::DNType & operator[](const unsigned int i) const
Get element i out of the array as a read-only reference.
virtual void qpCopy(const unsigned int to_qp, PropertyValue *rhs, const unsigned int from_qp)=0
Copy the value of a Property from one specific to a specific qp in this Property. ...
void markAD(bool use_ad) override
Mark whether this property is in AD mode. This method is necessary for switching the state after swap...
const T & operator[](const unsigned int i) const
Get element i out of the array as a ready-only reference.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:677
virtual ~MaterialProperty()
void dataStore(std::ostream &stream, PropertyValue *&p, void *)
std::vector< MooseADWrapper< T > > _value
Stored parameter value.
virtual void copyDualNumberToValue(const unsigned int i)=0
copy the value portion (not the derivatives) of the DualNumber<Real> version of the material property...
virtual void swap(PropertyValue *rhs) override
virtual void resize(int n)=0
Resizes the property to the size n.
void dataLoad(std::istream &stream, PropertyValue *&p, void *)
virtual unsigned int size() const =0
MaterialProperty(bool use_ad=false)
Explicitly declare a public constructor because we made the copy constructor private.
virtual std::string type()=0
String identifying the type of parameter stored.
void resizeItems(unsigned int n_qpoints)
Resize items in this array, i.e.
Abstract definition of a property value.
virtual PropertyValue * init(int size) override
Clone this value.
virtual ~PropertyValue()
void copyDualNumberToValue(const unsigned int i) override
copy the value portion (not the derivatives) of the DualNumber<Real> version of the material property...
std::vector< PropertyValue * >::const_iterator const_iterator
Constant parameter map iterator.
virtual void qpCopy(const unsigned int to_qp, PropertyValue *rhs, const unsigned int from_qp) override
Copy the value of a Property from one specific to a specific qp in this Property. ...
virtual void resize(int n) override
Resizes the property to the size n.
virtual void store(std::ostream &stream)=0
virtual ~MaterialProperties()
PetscInt n
void destroy()
Deallocates the memory.
virtual void load(std::istream &stream) override
Load the property from a binary stream.
Concrete definition of a parameter value for a specified type.
virtual void load(std::istream &stream)=0
MaterialProperty< T > & operator=(const MaterialProperty< T > &)
private assignment operator to avoid shallow copying of material properties
virtual PropertyValue * init(int size)=0
Clone this value.
virtual void store(std::ostream &stream) override
Store the property into a binary stream.
virtual unsigned int size() const override
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:741
PropertyValue * _init_helper(int size, PropertyValue *prop, const P *the_type, bool use_ad)
Scalar Init helper routine so that specialization isn&#39;t needed for basic scalar MaterialProperty type...
T & operator[](const unsigned int i)
Get element i out of the array as a writeable reference.