www.mooseframework.org
MaterialProperty.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef MATERIALPROPERTY_H
16 #define MATERIALPROPERTY_H
17 
18 #include <vector>
19 
20 #include "MooseArray.h"
21 #include "DataIO.h"
22 
23 #include "libmesh/libmesh_common.h"
24 #include "libmesh/tensor_value.h"
25 #include "libmesh/vector_value.h"
26 
27 class PropertyValue;
28 
33 template <typename P>
34 PropertyValue * _init_helper(int size, PropertyValue * prop, const P * the_type);
35 
40 template <typename P>
41 PropertyValue * _init_helper(int size, PropertyValue * prop, const std::vector<P> * the_type);
42 
47 {
48 public:
49  virtual ~PropertyValue(){};
50 
54  virtual std::string type() = 0;
55 
59  virtual PropertyValue * init(int size) = 0;
60 
61  virtual unsigned int size() const = 0;
62 
66  virtual void resize(int n) = 0;
67 
68  virtual void swap(PropertyValue * rhs) = 0;
69 
77  virtual void
78  qpCopy(const unsigned int to_qp, PropertyValue * rhs, const unsigned int from_qp) = 0;
79 
80  // save/restore in a file
81  virtual void store(std::ostream & stream) = 0;
82  virtual void load(std::istream & stream) = 0;
83 };
84 
85 template <>
86 inline void
87 dataStore(std::ostream & stream, PropertyValue *& p, void * /*context*/)
88 {
89  p->store(stream);
90 }
91 
92 template <>
93 inline void
94 dataLoad(std::istream & stream, PropertyValue *& p, void * /*context*/)
95 {
96  p->load(stream);
97 }
98 
103 template <typename T>
105 {
106 public:
109 
110  virtual ~MaterialProperty() { _value.release(); }
111 
115  MooseArray<T> & get() { return _value; }
116 
120  MooseArray<T> & set() { return _value; }
121 
125  virtual std::string type();
126 
130  virtual PropertyValue * init(int size);
131 
135  virtual void resize(int n);
136 
140  T & operator[](const unsigned int i) { return _value[i]; }
141 
142  unsigned int size() const { return _value.size(); }
143 
147  const T & operator[](const unsigned int i) const { return _value[i]; }
148 
152  virtual void swap(PropertyValue * rhs);
153 
161  virtual void qpCopy(const unsigned int to_qp, PropertyValue * rhs, const unsigned int from_qp);
162 
166  virtual void store(std::ostream & stream);
167 
171  virtual void load(std::istream & stream);
172 
180  template <typename P>
181  friend PropertyValue * _init_helper(int size, PropertyValue * prop, const P * the_type);
182 
187  template <typename P>
188  friend PropertyValue *
189  _init_helper(int size, PropertyValue * prop, const std::vector<P> * the_type);
190 
191 private:
194  {
195  mooseError("Material properties must be assigned to references (missing '&')");
196  }
197 
200  {
201  mooseError("Material properties must be assigned to references (missing '&')");
202  }
203 
206 };
207 
208 // ------------------------------------------------------------
209 // Material::Property<> class inline methods
210 template <typename T>
211 inline std::string
213 {
214  return typeid(T).name();
215 }
216 
217 template <typename T>
218 inline PropertyValue *
220 {
221  return _init_helper(size, this, static_cast<T *>(0));
222 }
223 
224 template <typename T>
225 inline void
227 {
228  _value.resize(n);
229 }
230 
231 template <typename T>
232 inline void
234 {
235  mooseAssert(rhs != NULL, "Assigning NULL?");
236  _value.swap(cast_ptr<MaterialProperty<T> *>(rhs)->_value);
237 }
238 
239 template <typename T>
240 inline void
241 MaterialProperty<T>::qpCopy(const unsigned int to_qp,
242  PropertyValue * rhs,
243  const unsigned int from_qp)
244 {
245  mooseAssert(rhs != NULL, "Assigning NULL?");
246  _value[to_qp] = cast_ptr<const MaterialProperty<T> *>(rhs)->_value[from_qp];
247 }
248 
249 template <typename T>
250 inline void
251 MaterialProperty<T>::store(std::ostream & stream)
252 {
253  for (unsigned int i = 0; i < _value.size(); i++)
254  storeHelper(stream, _value[i], NULL);
255 }
256 
257 template <typename T>
258 inline void
259 MaterialProperty<T>::load(std::istream & stream)
260 {
261  for (unsigned int i = 0; i < _value.size(); i++)
262  loadHelper(stream, _value[i], NULL);
263 }
264 
268 class MaterialProperties : public std::vector<PropertyValue *>
269 {
270 public:
272 
273  virtual ~MaterialProperties() {}
274 
278  typedef std::vector<PropertyValue *>::iterator iterator;
279 
283  typedef std::vector<PropertyValue *>::const_iterator const_iterator;
284 
288  void destroy()
289  {
290  for (iterator k = begin(); k != end(); ++k)
291  delete *k;
292  }
293 
299  void resizeItems(unsigned int n_qpoints)
300  {
301  for (iterator k = begin(); k != end(); ++k)
302  if (*k != NULL)
303  (*k)->resize(n_qpoints);
304  }
305 };
306 
307 template <>
308 inline void
309 dataStore(std::ostream & stream, MaterialProperties & v, void * context)
310 {
311  // Cast this to a vector so we can just piggy back on the vector store capability
312  std::vector<PropertyValue *> & mat_props = static_cast<std::vector<PropertyValue *> &>(v);
313 
314  storeHelper(stream, mat_props, context);
315 }
316 
317 template <>
318 inline void
319 dataLoad(std::istream & stream, MaterialProperties & v, void * context)
320 {
321  // Cast this to a vector so we can just piggy back on the vector store capability
322  std::vector<PropertyValue *> & mat_props = static_cast<std::vector<PropertyValue *> &>(v);
323 
324  loadHelper(stream, mat_props, context);
325 }
326 
327 // Scalar Init Helper Function
328 template <typename P>
330 _init_helper(int size, PropertyValue * /*prop*/, const P *)
331 {
333  copy->_value.resize(size, P{});
334  return copy;
335 }
336 
337 // Vector Init Helper Function
338 template <typename P>
340 _init_helper(int size, PropertyValue * /*prop*/, const std::vector<P> *)
341 {
342  typedef MaterialProperty<std::vector<P>> PropType;
343  PropType * copy = new PropType;
344  copy->_value.resize(size, std::vector<P>{});
345 
346  // We don't know the size of the underlying vector at each
347  // quadrature point, the user will be responsible for resizing it
348  // and filling in the entries...
349 
350  // Return the copy we allocated
351  return copy;
352 }
353 
354 #endif
virtual unsigned int size() const =0
MaterialProperty()
Explicitly declare a public constructor because we made the copy constructor private.
std::vector< PropertyValue * >::iterator iterator
Parameter map iterator.
virtual void swap(PropertyValue *rhs)
virtual void swap(PropertyValue *rhs)=0
MaterialProperty(const MaterialProperty< T > &)
private copy constructor to avoid shallow copying of material properties
Container for storing material properties.
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
const T & operator[](const unsigned int i) const
Get element i out of the array.
virtual void resize(int n)
Resizes the property to the size n.
virtual std::string type()
String identifying the type of parameter stored.
virtual void load(std::istream &stream)
Load the property from a binary stream.
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. ...
virtual void store(std::ostream &stream)
Store the property into a binary stream.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:528
virtual ~MaterialProperty()
void dataStore(std::ostream &stream, PropertyValue *&p, void *)
virtual void resize(int n)=0
Resizes the property to the size n.
void dataLoad(std::istream &stream, PropertyValue *&p, void *)
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.
virtual PropertyValue * init(int size)
Clone this value.
Abstract definition of a property value.
virtual ~PropertyValue()
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)
Copy the value of a Property from one specific to a specific qp in this Property. ...
virtual void store(std::ostream &stream)=0
virtual ~MaterialProperties()
PetscInt n
void destroy()
Deallocates the memory.
Concrete definition of a parameter value for a specified type.
virtual void load(std::istream &stream)=0
unsigned int size() const
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.
PropertyValue * _init_helper(int size, PropertyValue *prop, const P *the_type)
Scalar Init helper routine so that specialization isn&#39;t needed for basic scalar MaterialProperty type...
MooseArray< T > _value
Stored parameter value.
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:592
T & operator[](const unsigned int i)
Get element i out of the array.