https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MaterialProperty.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 <vector>
13#include <memory>
14#include <typeinfo>
15
16#include "MooseArray.h"
17#include "MooseTypes.h"
18#include "DataIO.h"
19#include "MooseError.h"
20#include "UniqueStorage.h"
21#include "MooseUtils.h"
22
23#include "libmesh/libmesh_common.h"
24#include "libmesh/tensor_value.h"
25#include "libmesh/vector_value.h"
26#include "libmesh/int_range.h"
27
28#include "metaphysicl/raw_type.h"
29
30class PropertyValue;
31class Material;
33
38{
39public:
41 typedef unsigned int id_type;
42
43 PropertyValue(const id_type id) : _id(id) {}
44
45 virtual ~PropertyValue() {}
46
50 static constexpr id_type invalid_property_id = std::numeric_limits<id_type>::max() - 1;
51
55 id_type id() const { return _id; }
56
60 virtual const std::string & type() const = 0;
61
65 virtual std::unique_ptr<PropertyValue> clone(const std::size_t) const = 0;
66
67 virtual unsigned int size() const = 0;
68
72 virtual void resize(const std::size_t size) = 0;
73
74 virtual void swap(PropertyValue & rhs) = 0;
75
76 virtual bool isAD() const = 0;
77
87 virtual void
88 qpCopy(const unsigned int to_qp, const PropertyValue & rhs, const unsigned int from_qp) = 0;
89
90 // save/restore in a file
91 virtual void store(std::ostream & stream) = 0;
92 virtual void load(std::istream & stream) = 0;
93
97 virtual const std::type_info & typeID() const = 0;
98
99protected:
102};
103
108template <typename T, bool is_ad>
110{
111public:
113
115
116 bool isAD() const override final { return is_ad; }
117
122
127
131 virtual const std::string & type() const override final;
132
136 virtual void resize(const std::size_t size) override final;
137
138 virtual unsigned int size() const override final { return _value.size(); }
139
143 Moose::GenericType<T, is_ad> & operator[](const unsigned int i) { return _value[i]; }
144
148 const Moose::GenericType<T, is_ad> & operator[](const unsigned int i) const { return _value[i]; }
149
157 virtual void qpCopy(const unsigned int to_qp,
158 const PropertyValue & rhs,
159 const unsigned int from_qp) override final;
160
164 virtual void store(std::ostream & stream) override final;
165
169 virtual void load(std::istream & stream) override final;
170
171 virtual void swap(PropertyValue & rhs) override final;
172
173 const std::type_info & typeID() const override final;
174
182 virtual std::unique_ptr<PropertyValue> clone(const std::size_t size) const override final;
183
184private:
187 {
188 mooseError("Material properties must be assigned to references (missing '&')");
189 }
190
193 {
194 mooseError("Material properties must be assigned to references (missing '&')");
195 }
196
197protected:
200};
201
202template <typename T>
203class MaterialProperty;
204template <typename T>
206
207// ------------------------------------------------------------
208// Material::Property<> class inline methods
209
210namespace moose
211{
212namespace internal
213{
214template <typename T1, typename T2>
215void
216rawValueEqualityHelper(T1 & out, const T2 & in)
217{
218 out = MetaPhysicL::raw_value(in);
219}
220
221template <typename T1, typename T2>
222void
223rawValueEqualityHelper(std::vector<T1> & out, const std::vector<T2> & in)
224{
225 out.resize(in.size());
226 for (MooseIndex(in) i = 0; i < in.size(); ++i)
227 rawValueEqualityHelper(out[i], in[i]);
228}
229
230template <typename T1, typename T2, std::size_t N>
231void
232rawValueEqualityHelper(std::array<T1, N> & out, const std::array<T2, N> & in)
233{
234 for (MooseIndex(in) i = 0; i < in.size(); ++i)
235 rawValueEqualityHelper(out[i], in[i]);
236}
237}
238}
239
240template <typename T, bool is_ad>
241inline const std::string &
243{
244 static const std::string type_name = MooseUtils::prettyCppType<T>();
245 return type_name;
246}
247
248template <typename T, bool is_ad>
249inline void
251{
252 _value.template resize</*value_initalize=*/true>(size);
253}
254
255template <typename T, bool is_ad>
256inline void
258 const PropertyValue & rhs,
259 const unsigned int from_qp)
260{
261 // If we're the same
262 if (rhs.isAD() == is_ad)
263 _value[to_qp] =
264 libMesh::cast_ptr<const MaterialPropertyBase<T, is_ad> *>(&rhs)->_value[from_qp];
265 else
267 _value[to_qp],
268 (*libMesh::cast_ptr<const MaterialPropertyBase<T, !is_ad> *>(&rhs))[from_qp]);
269}
270
271template <typename T, bool is_ad>
272inline void
274{
275 for (const auto i : index_range(_value))
276 storeHelper(stream, _value[i], nullptr);
277}
278
279template <typename T, bool is_ad>
280inline void
282{
283 for (const auto i : index_range(_value))
284 loadHelper(stream, _value[i], nullptr);
285}
286
287template <typename T, bool is_ad>
288inline void
290{
291 mooseAssert(this->id() == rhs.id(), "Inconsistent properties");
292 mooseAssert(this->typeID() == rhs.typeID(), "Inconsistent types");
293
294 // If we're the same
295 if (rhs.isAD() == is_ad)
296 {
297 mooseAssert(dynamic_cast<decltype(this)>(&rhs), "Expected same type is not the same");
298 this->_value.swap(libMesh::cast_ptr<decltype(this)>(&rhs)->_value);
299 return;
300 }
301
302 // We may call this function when doing swap between MaterialData material properties (you can
303 // think of these as the current element properties) and MaterialPropertyStorage material
304 // properties (these are the stateful material properties that we store for *every* element). We
305 // never store ADMaterialProperty in stateful storage (e.g. MaterialPropertyStorage) for memory
306 // resource reasons; instead we keep a regular MaterialProperty version of it. Hence we do have a
307 // need to exchange data between the AD and regular copies which we implement below. The below
308 // is obviously not a swap, for which you cannot identify a giver and receiver. Instead the below
309 // has a clear giver and receiver. The giver is the object passed in as the rhs. The receiver is
310 // *this* object. This directionality, although not conceptually appropriate given the method
311 // name, *is* appropriate to how this method is used in practice. See shallowCopyData and
312 // shallowCopyDataBack in MaterialPropertyStorage.C
313
314 auto * different_type_prop = libMesh::cast_ptr<MaterialPropertyBase<T, !is_ad> *>(&rhs);
315
316 this->resize(different_type_prop->size());
317 for (const auto qp : make_range(this->size()))
318 moose::internal::rawValueEqualityHelper(this->_value[qp], (*different_type_prop)[qp]);
319}
320
321template <typename T, bool is_ad>
322inline const std::type_info &
324{
325 static const auto & info = typeid(T);
326 return info;
327}
328
329template <typename T, bool is_ad>
330std::unique_ptr<PropertyValue>
331MaterialPropertyBase<T, is_ad>::clone(const std::size_t size) const
332{
333 auto prop = std::make_unique<MaterialProperty<T>>(this->id());
334 if (size)
335 prop->resize(size);
336 return prop;
337}
338
339template <typename T>
341{
342public:
347
348private:
351 {
352 mooseError("Material properties must be assigned to references (missing '&')");
353 }
354
357 {
358 mooseError("Material properties must be assigned to references (missing '&')");
359 }
360};
361
362template <typename T>
364{
365public:
370
372
373private:
376 {
377 mooseError("Material properties must be assigned to references (missing '&')");
378 }
379
382 {
383 mooseError("Material properties must be assigned to references (missing '&')");
384 }
385};
386
387class MaterialData;
389
390class MaterialProperties : public UniqueStorage<PropertyValue>
391{
392public:
394 {
395 friend class MaterialData;
397 friend void dataLoad(std::istream &, MaterialPropertyStorage &, void *);
398
400 WriteKey(const WriteKey &) {}
401 };
402
408 void resizeItems(const std::size_t n_qpoints, const WriteKey)
409 {
410 for (const auto i : index_range(*this))
411 if (auto value = queryValue(i))
412 value->resize(n_qpoints);
413 }
414
415 void resize(const std::size_t size, const WriteKey)
416 {
418 }
419
420 void setPointer(const std::size_t i, std::unique_ptr<PropertyValue> && ptr, const WriteKey)
421 {
422 return UniqueStorage<PropertyValue>::setPointer(i, std::move(ptr));
423 }
424};
425
426template <typename T, bool is_ad>
431
432template <typename T>
437
438template <typename T, bool is_ad>
440
449
450template <class M, typename T, bool is_ad>
452
458template <typename T, bool is_ad>
460{
462
463public:
464 GenericOptionalMaterialProperty(const P * pointer) : _pointer(pointer) {}
465
471
473 const Moose::GenericType<T, is_ad> & operator[](const unsigned int i) const
474 {
475 // check if the optional property is valid in debug mode
476 mooseAssert(
477 _pointer,
478 "Attempting to access an optional material property that was not provided by any material "
479 "class. Make sure to check optional material properties before using them.");
480 return (*_pointer)[i];
481 }
482
484 unsigned int size() const { return (*_pointer).size(); }
485
487 operator bool() const { return _pointer; }
488
490 const P * get() const { return _pointer; }
491
492private:
495
497 void set(const P * pointer) { _pointer = pointer; }
498 const P * _pointer;
499
500 friend class OptionalMaterialPropertyProxy<Material, T, is_ad>;
502};
503
504void dataStore(std::ostream & stream, PropertyValue & p, void * context);
505void dataLoad(std::istream & stream, PropertyValue & p, void * context);
506
507void dataStore(std::ostream & stream, MaterialProperties & v, void * context);
508void dataLoad(std::istream & stream, MaterialProperties & v, void * context);
509
510template <typename T>
512template <typename T>
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:989
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition DataIO.h:1081
void dataLoad(std::istream &stream, PropertyValue &p, void *context)
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
void dataStore(std::ostream &stream, PropertyValue &p, void *context)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
ADMaterialProperty< T > & operator=(const ADMaterialProperty< T > &)
private assignment operator to avoid shallow copying of material properties
ADMaterialProperty(const ADMaterialProperty< T > &)
private copy constructor to avoid shallow copying of material properties
ADMaterialProperty(const PropertyValue::id_type id=PropertyValue::invalid_property_id)
Base class to facilitate storage using unique pointers.
Wrapper around a material property pointer.
GenericMaterialProperty< T, is_ad > P
const P * get() const
get a pointer to the underlying property (only do this in initialSetup or later)
unsigned int size() const
pass through size calls
void set(const P *pointer)
setting the pointer is only permitted through the optional material proxy system
GenericOptionalMaterialProperty & operator=(const GenericOptionalMaterialProperty< T, is_ad > &)=delete
no copy assignment is permitted
const Moose::GenericType< T, is_ad > & operator[](const unsigned int i) const
pass through operator[] to provide a similar API as MaterialProperty
GenericOptionalMaterialProperty(const GenericOptionalMaterialProperty< T, is_ad > &)=delete
no copy construction is permitted
GenericOptionalMaterialProperty(const P *pointer)
GenericOptionalMaterialProperty()
the default constructor is only called from the friend class
Proxy for accessing MaterialPropertyStorage.
friend void dataLoad(std::istream &, MaterialPropertyStorage &, void *)
void resizeItems(const std::size_t n_qpoints, const WriteKey)
Resize items in this array, i.e.
void setPointer(const std::size_t i, std::unique_ptr< PropertyValue > &&ptr, const WriteKey)
void resize(const std::size_t size, const WriteKey)
Concrete definition of a parameter value for a specified type.
const MooseArray< Moose::GenericType< T, is_ad > > & get() const
virtual unsigned int size() const override final
Moose::GenericType< T, is_ad > & operator[](const unsigned int i)
Get element i out of the array as a writeable reference.
MooseArray< Moose::GenericType< T, is_ad > > _value
Stored parameter value.
MooseArray< Moose::GenericType< T, is_ad > > & set()
const std::type_info & typeID() const override final
virtual void qpCopy(const unsigned int to_qp, const PropertyValue &rhs, const unsigned int from_qp) override final
Copy the value of a Property from one specific to a specific qp in this Property.
bool isAD() const override final
virtual void load(std::istream &stream) override final
Load the property from a binary stream.
const Moose::GenericType< T, is_ad > & operator[](const unsigned int i) const
Get element i out of the array as a ready-only reference.
virtual const std::string & type() const override final
String identifying the type of parameter stored.
MaterialPropertyBase(const PropertyValue::id_type id)
virtual void store(std::ostream &stream) override final
Store the property into a binary stream.
virtual std::unique_ptr< PropertyValue > clone(const std::size_t size) const override final
MaterialPropertyBase< T, is_ad > & operator=(const MaterialPropertyBase< T, is_ad > &)
private assignment operator to avoid shallow copying of material properties
Moose::GenericType< T, is_ad > value_type
virtual void swap(PropertyValue &rhs) override final
virtual void resize(const std::size_t size) override final
Resizes the property to the size n.
An interface for accessing Materials.
Stores the stateful material properties computed by materials.
MaterialProperty< T > & operator=(const MaterialProperty< T > &)
private assignment operator to avoid shallow copying of material properties
MaterialProperty(const MaterialProperty< T > &)
private copy constructor to avoid shallow copying of material properties
MaterialProperty(const PropertyValue::id_type id=PropertyValue::invalid_property_id)
Materials compute MaterialProperties.
Definition Material.h:36
forward declarations
Definition MooseArray.h:18
unsigned int size() const
The number of elements that can currently be stored in the array.
Definition MooseArray.h:259
Abstract definition of a property value.
virtual bool isAD() const =0
virtual void store(std::ostream &stream)=0
static constexpr id_type invalid_property_id
The material property ID for an invalid property We only have this because there are a few cases wher...
unsigned int id_type
The type for a material property ID.
const id_type _id
The material property ID.
id_type id() const
virtual const std::string & type() const =0
String identifying the type of parameter stored.
PropertyValue(const id_type id)
virtual void load(std::istream &stream)=0
virtual ~PropertyValue()
virtual const std::type_info & typeID() const =0
virtual unsigned int size() const =0
virtual std::unique_ptr< PropertyValue > clone(const std::size_t) const =0
Clone this value.
virtual void qpCopy(const unsigned int to_qp, const 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 resize(const std::size_t size)=0
Resizes the property to the size n.
virtual void swap(PropertyValue &rhs)=0
Storage container that stores a vector of unique pointers of T, but represents most of the public fac...
const PropertyValue * queryValue(const std::size_t i) const
void resize(const std::size_t size)
Resizes the underlying vector.
void setPointer(const std::size_t i, std::unique_ptr< T > &&ptr)
Sets the underlying unique_ptr at index i to ptr.
auto raw_value(const Eigen::Map< T > &in)
typename std::conditional< is_ad, typename ADType< T >::type, T >::type GenericType
Definition MooseTypes.h:694
Tnew cast_ptr(Told *oldvar)
void rawValueEqualityHelper(T1 &out, const T2 &in)