https://mooseframework.inl.gov
KokkosMaterialPropertyDecl.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 "KokkosArray.h"
13 
14 #include "MoosePassKey.h"
15 
16 #include <typeindex>
17 
18 class MaterialBase;
19 
20 namespace Moose::Kokkos
21 {
22 
24 
26 
27 template <typename T, unsigned int dimension>
29 
30 template <typename T, unsigned int dimension>
32 
33 class Datum;
34 class Assembly;
35 class Mesh;
36 
41 {
42  NONE,
43  ELEMENT,
44  SUBDOMAIN
45 };
46 
50 struct PropRecord
51 {
55  std::set<const ::MaterialBase *> declarers;
59  std::string name;
63  std::string type;
67  unsigned int id = libMesh::invalid_uint;
71  std::unordered_map<SubdomainID, std::vector<unsigned int>> dims;
75  bool bnd = false;
79  bool on_demand = false;
83  std::unordered_map<SubdomainID, PropertyConstantOption> constant_option;
84 };
85 
86 using PropertyStore = std::function<void(std::ostream &, void *)>;
87 using PropertyLoad = std::function<void(std::istream &, void *)>;
88 
93 {
94 public:
98  MaterialPropertyBase() = default;
103 
108  unsigned int id() const { return _id; }
113  const std::string & name() const;
118  const std::string & type() const;
123  unsigned int dim() const;
130  unsigned int dimSize(SubdomainID subdomain, unsigned int i) const;
131 
136  virtual std::type_index propertyType() = 0;
137 
142  virtual void init(const PropRecord & record, const StorageKey &);
143 
151  virtual void allocate(const Mesh & mesh,
152  const Assembly & assembly,
153  const std::set<SubdomainID> & subdomains,
154  const bool bnd,
155  StorageKey) = 0;
160  virtual void copy(const MaterialPropertyBase & prop, StorageKey) = 0;
165  virtual void swap(MaterialPropertyBase & prop, StorageKey) = 0;
166 
167 protected:
171  const PropRecord * _record = nullptr;
175  unsigned int _id = libMesh::invalid_uint;
179  bool _default = false;
184 };
185 
186 inline const std::string &
188 {
189  if (!_record)
190  mooseError("Cannot get the name of an uninitialized or default material property.");
191  else
192  return _record->name;
193 }
194 
195 inline const std::string &
197 {
198  if (!_record)
199  mooseError("Cannot get the type of an uninitialized or default material property.");
200  else
201  return _record->type;
202 }
203 
204 inline unsigned int
206 {
207  if (!_record || !_record->dims.size())
208  mooseError("Cannot get the dimension of an uninitialized or default material property.");
209  else
210  return _record->dims.begin()->second.size();
211 }
212 
213 inline unsigned int
214 MaterialPropertyBase::dimSize(SubdomainID subdomain, unsigned int i) const
215 {
216  const unsigned int D = dim();
217 
218  if (i >= D)
219  mooseError("Cannot get the size of ",
220  i,
221  "-th dimension for the ",
222  D,
223  "D material property '",
224  name(),
225  "'.");
226 
227  return libmesh_map_find(_record->dims, subdomain)[i];
228 }
229 
230 template <typename T, unsigned int dimension>
231 void propertyStore(std::ostream & stream, void * prop);
232 template <typename T, unsigned int dimension>
233 void propertyLoad(std::istream & stream, void * prop);
234 
238 template <typename T, unsigned int dimension = 0>
240 {
241 public:
245  MaterialProperty() = default;
250  MaterialProperty(const T & value);
266  template <unsigned int D>
267  MaterialProperty(const MaterialProperty<T, D> & other) = delete;
268 
273  auto & operator=(const MaterialProperty<T, dimension> & property);
274 
275 #ifdef MOOSE_KOKKOS_SCOPE
276 
280  KOKKOS_FUNCTION operator bool() const { return _data.isAlloc() || _default; }
281 
288  KOKKOS_FUNCTION MaterialPropertyValue<T, dimension> operator()(const Datum & datum,
289  const unsigned int qp) const;
290 #endif
291 
292  virtual std::type_index propertyType() override
293  {
294  static const std::type_index type = typeid(*this);
295 
296  return type;
297  }
298 
299  virtual void init(const PropRecord & record, const StorageKey & key) override;
300 
301 #ifdef MOOSE_KOKKOS_SCOPE
302  virtual void allocate(const Mesh & mesh,
303  const Assembly & assembly,
304  const std::set<SubdomainID> & subdomains,
305  const bool bnd,
306  StorageKey) override;
307  virtual void copy(const MaterialPropertyBase & prop, StorageKey) override;
308  virtual void swap(MaterialPropertyBase & prop, StorageKey) override;
309 #endif
310 
311 private:
316  void shallowCopy(const MaterialProperty<T, dimension> & property);
317 
330 
331  friend class MaterialPropertyValueBase<T, dimension>;
332 
333  friend void propertyStore<T, dimension>(std::ostream &, void *);
334  friend void propertyLoad<T, dimension>(std::istream &, void *);
335 };
336 
337 // The Kokkos array containing Kokkos material properties requires a deep copy because the copy
338 // constructor of each Kokkos material property should be invoked
339 template <typename T, unsigned int dimension>
340 struct ArrayDeepCopy<MaterialProperty<T, dimension>>
341 {
342  static constexpr bool value = true;
343 };
344 
345 } // namespace Moose::Kokkos
virtual void init(const PropRecord &record, const StorageKey &)
Initialize this property.
The Kokkos assembly class.
A structure storing the metadata of Kokkos material properties.
The Kokkos mesh object.
Definition: KokkosMesh.h:52
The base class for Kokkos material properties.
KOKKOS_FUNCTION MaterialPropertyValue< T, dimension > operator()(const Datum &datum, const unsigned int qp) const
Get the property values of a quadrature point.
The Kokkos object that holds thread-private data in the parallel operations of any Kokkos object...
Definition: KokkosDatum.h:23
const PropRecord * _record
Pointer to the record of this property.
const unsigned int invalid_uint
virtual std::type_index propertyType()=0
Get the property type index for load/store functions.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
void propertyLoad(std::istream &stream, void *prop)
Stores the stateful material properties computed by materials.
virtual void init(const PropRecord &record, const StorageKey &key) override
Initialize this property.
std::function< void(std::istream &, void *)> PropertyLoad
virtual void swap(MaterialPropertyBase &prop, StorageKey) override
Swap with another property.
virtual std::type_index propertyType() override
Get the property type index for load/store functions.
std::string name
Property name.
virtual void swap(MaterialPropertyBase &prop, StorageKey)=0
Swap with another property.
virtual void copy(const MaterialPropertyBase &prop, StorageKey)=0
Deep copy another property.
std::string type
Demangled data type name.
auto & operator=(const MaterialProperty< T, dimension > &property)
Shallow copy another property.
std::unordered_map< SubdomainID, std::vector< unsigned int > > dims
Size of each dimension of each subdomain.
The Kokkos wrapper class for accessing the material property values of a single quadrature point...
virtual void allocate(const Mesh &mesh, const Assembly &assembly, const std::set< SubdomainID > &subdomains, const bool bnd, StorageKey) override
Allocate the data storage.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
PropertyConstantOption
Property constant options.
std::set< const ::MaterialBase * > declarers
List of declaring materials.
MaterialProperty()=default
Default constructor.
const MaterialProperty< T, dimension > * _reference
Pointer to the reference property.
bool on_demand
Flag whether this property is an on-demand property.
std::function< void(std::ostream &, void *)> PropertyStore
Array< Array< T, dimension+1 > > _data
Data storage.
virtual void copy(const MaterialPropertyBase &prop, StorageKey) override
Deep copy another property.
bool _default
Flag whether this property has a default value.
The type trait that determines the default behavior of copy constructor and deepCopy() If this type t...
Definition: KokkosArray.h:90
void shallowCopy(const MaterialProperty< T, dimension > &property)
Shallow copy another property.
MaterialPropertyBase()=default
Default constructor.
virtual void allocate(const Mesh &mesh, const Assembly &assembly, const std::set< SubdomainID > &subdomains, const bool bnd, StorageKey)=0
Allocate the data storage.
Array< PropertyConstantOption > _constant_option
Whether this property is constant over element or subdomain in each subdomain.
bool bnd
Flag whether this property is a face property.
unsigned int dim() const
Get the dimension.
const std::string & name() const
Get the property name.
The Kokkos material property class.
MaterialBases compute MaterialProperties.
Definition: MaterialBase.h:62
const std::string & type() const
Get the data type.
unsigned int dimSize(SubdomainID subdomain, unsigned int i) const
Get the size of a dimension.
std::unordered_map< SubdomainID, PropertyConstantOption > constant_option
Whether this property is constant over element or subdomain in each subdomain.
unsigned int id() const
Get the property ID.
static constexpr bool value
Definition: KokkosArray.h:92
void propertyStore(std::ostream &stream, void *prop)