https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MaterialData.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#ifdef MOOSE_KOKKOS_ENABLED
14#endif
15
16#include "MaterialProperty.h"
17#include "Moose.h"
18#include "MooseUtils.h"
19
20// libMesh
21#include "libmesh/elem.h"
22
23#include <vector>
24#include <memory>
25#include <typeinfo>
26
28class MooseObject;
29class Material;
30class XFEM;
31class MaterialBase;
32
38{
39public:
41
43 static constexpr unsigned int max_state = 2;
44
48 void resize(unsigned int n_qpoints);
49
54 unsigned int nQPoints() const { return _n_qpoints; }
55
57 void copy(const Elem & elem_to, const Elem & elem_from, unsigned int side);
58
60 void swap(const Elem & elem, unsigned int side = 0);
61
69 template <typename MatContainer>
70 void reinit(const MatContainer & mats);
71
73 void reset(const std::vector<std::shared_ptr<MaterialBase>> & mats);
74
76 void swapBack(const Elem & elem, unsigned int side = 0);
77
84 const MaterialProperties & props(const unsigned int state = 0) const;
85 MaterialProperties & props(const unsigned int state = 0);
87
88 template <typename T, bool is_ad>
89 bool haveGenericProperty(const std::string & prop_name) const;
90
92 template <typename T>
93 bool haveProperty(const std::string & prop_name) const
94 {
95 return haveGenericProperty<T, false>(prop_name);
96 }
97
99 template <typename T>
100 bool haveADProperty(const std::string & prop_name) const
101 {
102 return haveGenericProperty<T, true>(prop_name);
103 }
104
105#ifdef MOOSE_KOKKOS_SCOPE
113 template <typename T, unsigned int dimension>
114 bool haveKokkosProperty(const std::string & prop_name) const;
115#endif
116
126 template <typename T, bool is_ad = false>
127 GenericMaterialProperty<T, is_ad> & getProperty(const std::string & prop_name,
128 const unsigned int state,
129 const MooseObject & requestor)
130 {
131 return getPropertyHelper<T, is_ad, false>(prop_name, state, requestor);
132 }
141 template <typename T, bool is_ad>
142 GenericMaterialProperty<T, is_ad> & declareProperty(const std::string & prop_name,
143 const MooseObject & requestor)
144 {
145 return getPropertyHelper<T, is_ad, true>(prop_name, 0, requestor);
146 }
147
148#ifdef MOOSE_KOKKOS_SCOPE
157 template <typename T, unsigned int dimension, unsigned int state>
159
172 template <typename T, unsigned int dimension>
174 declareKokkosProperty(const std::string & prop_name,
175 const std::vector<unsigned int> & dims,
176 const MaterialBase * declarer,
177 const bool bnd,
178 const bool on_demand,
179 const Moose::Kokkos::PropertyConstantOption constant_option);
180#endif
181
185 bool isSwapped() const { return _swapped; }
186
191
196 {
197 friend class XFEM;
199 XFEMKey(const XFEM &) {}
200 };
201
212
216 bool hasProperty(const std::string & prop_name) const;
217
225 unsigned int getPropertyId(const std::string & prop_name) const;
226
232 void onlyResizeIfSmaller(bool flag) { _resize_only_if_smaller = flag; };
233
238
244 void eraseProperty(const Elem * elem);
245
246private:
249
252
254 unsigned int _n_qpoints;
255
257 std::array<MaterialProperties, max_state + 1> _props;
258
259 unsigned int addPropertyHelper(const std::string & prop_name,
260 const std::type_info & type,
261 const unsigned int state,
262 const MaterialBase * const declarer);
263
264 template <typename T, bool is_ad, bool declare>
265 GenericMaterialProperty<T, is_ad> & getPropertyHelper(const std::string & prop_name,
266 const unsigned int state,
267 const MooseObject & requestor);
268
269#ifdef MOOSE_KOKKOS_ENABLED
279 addKokkosPropertyHelper(const std::string & prop_name,
280 const std::type_info & type,
281 const unsigned int state,
282 std::shared_ptr<Moose::Kokkos::MaterialPropertyBase> shell);
283
297 declareKokkosPropertyHelper(const std::string & prop_name,
298 const std::type_info & type,
299 const MaterialBase * declarer,
300 const std::vector<unsigned int> & dims,
301 const bool bnd,
302 const bool on_demand,
303 const Moose::Kokkos::PropertyConstantOption constant_option,
304 std::shared_ptr<Moose::Kokkos::MaterialPropertyBase> shell);
305
314 const std::string & prop_name,
315 const unsigned int state = 0,
316 std::shared_ptr<Moose::Kokkos::MaterialPropertyBase> shell = nullptr) const;
317
323 bool haveKokkosPropertyHelper(const std::string & prop_name) const;
331 void kokkosRegisterLoadStoreHelper(std::type_index type,
334#endif
335
336 static void mooseErrorHelper(const MooseObject & object, const std::string_view & error);
337
341 const MaterialBase & castRequestorToDeclarer(const MooseObject & requestor) const;
342
345
349
351 unsigned int getMaxStateRequested(const unsigned int prop_id) const;
352};
353
354inline const MaterialProperties &
355MaterialData::props(const unsigned int state) const
356{
357 mooseAssert(_props.size() > state, "Invalid state");
358 return _props[state];
359}
360
361inline MaterialProperties &
362MaterialData::props(const unsigned int state)
363{
364 mooseAssert(_props.size() > state, "Invalid state");
365 return _props[state];
366}
367
368template <typename T, bool is_ad>
369inline bool
370MaterialData::haveGenericProperty(const std::string & prop_name) const
371{
372 if (!hasProperty(prop_name))
373 return false;
374
375 const auto prop_id = getPropertyId(prop_name);
376 // the property id exists, but the property was not created in this instance of the material type
377 if (prop_id >= props(0).size())
378 return false;
379
380 const PropertyValue * const base_prop = props(0).queryValue(prop_id);
381 return dynamic_cast<const GenericMaterialProperty<T, is_ad> *>(base_prop) != nullptr;
382}
383
384template <typename T, bool is_ad, bool declare>
386MaterialData::getPropertyHelper(const std::string & prop_name,
387 const unsigned int state,
388 const MooseObject & requestor)
389{
390 if constexpr (is_ad)
391 mooseAssert(state == 0, "Cannot request/declare AD properties for states other than zero");
392 if constexpr (declare)
393 mooseAssert(state == 0, "Cannot declare properties for states other than zero");
394
395 // Register/get the ID of the property
396 const auto prop_id = addPropertyHelper(
397 prop_name, typeid(T), state, declare ? &castRequestorToDeclarer(requestor) : nullptr);
398 const auto size = prop_id + 1;
399
400 // Initialize the states that we need
401 for (const auto state_i : make_range(getMaxStateRequested(prop_id) + 1))
402 {
403 auto & entry = props(state_i);
404 if (entry.size() < size)
405 entry.resize(size, {});
406 // if we are not declaring the property we initialize only what we need (the requested state)
407 if (!entry.hasValue(prop_id) && (declare || state_i == state))
408 {
409 if (state_i == 0)
410 entry.setPointer(
411 prop_id, std::move(std::make_unique<GenericMaterialProperty<T, is_ad>>(prop_id)), {});
412 else
413 entry.setPointer(prop_id, std::move(std::make_unique<MaterialProperty<T>>(prop_id)), {});
414 }
415 }
416
417 // Should be available now
418 auto & base_prop = props(state)[prop_id];
419
420 // In the event that this property was already declared/requested, make sure
421 // that the types are consistent
422 auto prop = dynamic_cast<GenericMaterialProperty<T, is_ad> *>(&base_prop);
423 if (!prop)
424 {
425 constexpr std::string_view action = declare ? "declared" : "requested";
426 constexpr auto is_ad_to_str = [](const bool is_ad_bool)
427 { return std::string_view(is_ad_bool ? "AD" : "non-AD"); };
428 constexpr std::string_view ad_type = is_ad_to_str(is_ad);
429
430 std::stringstream error;
431 error << "The " << action << " " << ad_type << " "
432 << "material property '" + prop_name + "' of type '" << MooseUtils::prettyCppType<T>()
433 << "'\nis already retrieved or declared as a " << is_ad_to_str(base_prop.isAD())
434 << " property of type '" << base_prop.type() << "'.";
435 mooseErrorHelper(requestor, error.str());
436 }
437
438 return *prop;
439}
440
441template <typename MatContainer>
442void
443MaterialData::reinit(const MatContainer & mats)
444{
445 for (const auto & mat : mats)
446 mat->computeProperties();
447}
448
449#ifdef MOOSE_KOKKOS_SCOPE
450template <typename T, unsigned int dimension>
451bool
452MaterialData::haveKokkosProperty(const std::string & prop_name) const
453{
454 if (!haveKokkosPropertyHelper(prop_name))
455 return false;
456
457 auto & prop = getKokkosPropertyHelper(prop_name);
458 return dynamic_cast<Moose::Kokkos::MaterialProperty<T, dimension> *>(&prop) != nullptr;
459}
460
461template <typename T, unsigned int dimension, unsigned int state>
463MaterialData::getKokkosProperty(const std::string & prop_name)
464{
465 // Reserve the storages for the property up to the requested state
466 // If the storages were already reserved, it will do nothing
467 for (unsigned int s = 0; s <= state; ++s)
468 {
469 auto shell = std::make_shared<Moose::Kokkos::MaterialProperty<T, dimension>>();
470
471 addKokkosPropertyHelper(prop_name, typeid(T), state, shell);
472
473 // Only instantiate load and store functions for stateful properties to avoid requiring users
474 // to provide custom dataLoad and dataStore for non-trivially-copyable types that are never
475 // used as stateful properties
476 if constexpr (state > 0)
477 kokkosRegisterLoadStoreHelper(shell->propertyType(),
478 Moose::Kokkos::propertyStore<T, dimension>,
479 Moose::Kokkos::propertyLoad<T, dimension>);
480 }
481
482 auto & prop_base = getKokkosPropertyHelper(prop_name, state, nullptr);
483 auto prop_cast = dynamic_cast<Moose::Kokkos::MaterialProperty<T, dimension> *>(&prop_base);
484
485 if (!prop_cast)
486 mooseError("The requested ",
487 dimension,
488 "D Kokkos material property '",
489 prop_name,
490 "' of type '",
491 MooseUtils::prettyCppType<T>(),
492 "' was already declared or requested as a ",
493 prop_base.dim(),
494 "D property of type '",
495 prop_base.type(),
496 "'.");
497
498 return *prop_cast;
499}
500
501template <typename T, unsigned int dimension>
503MaterialData::declareKokkosProperty(const std::string & prop_name,
504 const std::vector<unsigned int> & dims,
505 const MaterialBase * declarer,
506 const bool bnd,
507 const bool on_demand,
508 const Moose::Kokkos::PropertyConstantOption constant_option)
509{
510 auto shell = std::make_shared<Moose::Kokkos::MaterialProperty<T, dimension>>();
511
512 auto & prop_base = declareKokkosPropertyHelper(
513 prop_name, typeid(T), declarer, dims, bnd, on_demand, constant_option, shell);
514 auto prop_cast = dynamic_cast<Moose::Kokkos::MaterialProperty<T, dimension> *>(&prop_base);
515
516 if (!prop_cast)
517 mooseError("The declared ",
518 dimension,
519 "D Kokkos material property '",
520 prop_name,
521 "' of type '",
522 MooseUtils::prettyCppType<T>(),
523 "' was already declared or requested as a ",
524 prop_base.dim(),
525 "D property of type '",
526 prop_base.type(),
527 "'.");
528
529 return *prop_cast;
530}
531#endif
typename GenericMaterialPropertyStruct< T, is_ad >::type GenericMaterialProperty
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
unsigned int THREAD_ID
Definition MooseTypes.h:237
MaterialBases compute MaterialProperties.
Key that provides access to only the XFEM class.
XFEMKey(const XFEM &)
Proxy for accessing MaterialPropertyStorage.
void resize(unsigned int n_qpoints)
Resize the data to hold properties for n_qpoints quadrature points.
void eraseProperty(const Elem *elem)
Remove the property storage and element pointer from MaterialPropertyStorage data structures Use this...
static void mooseErrorHelper(const MooseObject &object, const std::string_view &error)
void swapBack(const Elem &elem, unsigned int side=0)
material properties for given element (and possible side)
bool hasProperty(const std::string &prop_name) const
unsigned int nQPoints() const
Returns the number of quadrature points the material properties support/hold.
bool _resize_only_if_smaller
Use non-destructive resize of material data (calling resize() will not reduce size).
void reinit(const MatContainer &mats)
Reinit material properties for given element (and possible side)
void onlyResizeIfSmaller(bool flag)
Set _resize_only_if_smaller to perform a non-destructive resize.
const MaterialPropertyStorage & getMaterialPropertyStorage() const
Provide read-only access to the underlying MaterialPropertyStorage object.
void reset(const std::vector< std::shared_ptr< MaterialBase > > &mats)
Calls the reset method of Materials to ensure that they are in a proper state.
std::array< MaterialProperties, max_state+1 > _props
The underlying property data.
Moose::Kokkos::MaterialPropertyBase & getKokkosPropertyHelper(const std::string &prop_name, const unsigned int state=0, std::shared_ptr< Moose::Kokkos::MaterialPropertyBase > shell=nullptr) const
Helper function for getting a Kokkos material property.
const THREAD_ID _tid
The thread id.
unsigned int _n_qpoints
Number of quadrature points.
bool haveProperty(const std::string &prop_name) const
Returns true if the regular material property exists - defined by any material.
unsigned int getPropertyId(const std::string &prop_name) const
Wrapper for MaterialStorage::getPropertyId.
GenericMaterialProperty< T, is_ad > & declareProperty(const std::string &prop_name, const MooseObject &requestor)
Declares a material property.
MaterialPropertyStorage & getMaterialPropertyStorageForXFEM(const XFEMKey)
Provide write-only access to the underlying MaterialPropertyStorage object JUST FOR XFEM.
unsigned int getMaxStateRequested(const unsigned int prop_id) const
maximum state id requested for a property
void swap(const Elem &elem, unsigned int side=0)
material properties for given element (and possible side)
const MaterialBase & castRequestorToDeclarer(const MooseObject &requestor) const
Helper for casting requestor to a MaterialBase in addPropertyHelper() (templated)
bool haveKokkosPropertyHelper(const std::string &prop_name) const
Helper function for checking whether a Kokkos material property exists.
bool haveGenericProperty(const std::string &prop_name) const
bool haveKokkosProperty(const std::string &prop_name) const
Get whether a Kokkos material property exists.
void kokkosRegisterLoadStoreHelper(std::type_index type, Moose::Kokkos::PropertyStore store, Moose::Kokkos::PropertyLoad load)
Helper function to register load/store functions of a Kokkos material property to the Kokkos material...
unsigned int addPropertyHelper(const std::string &prop_name, const std::type_info &type, const unsigned int state, const MaterialBase *const declarer)
bool haveADProperty(const std::string &prop_name) const
Returns true if the AD material property exists - defined by any material.
MaterialPropertyStorage & _storage
Reference to the MaterialStorage class.
const MaterialProperties & props(const unsigned int state=0) const
{
Moose::Kokkos::MaterialPropertyBase & declareKokkosPropertyHelper(const std::string &prop_name, const std::type_info &type, const MaterialBase *declarer, const std::vector< unsigned int > &dims, const bool bnd, const bool on_demand, const Moose::Kokkos::PropertyConstantOption constant_option, std::shared_ptr< Moose::Kokkos::MaterialPropertyBase > shell)
Helper function for declaring a Kokkos material property.
Moose::Kokkos::MaterialPropertyBase & addKokkosPropertyHelper(const std::string &prop_name, const std::type_info &type, const unsigned int state, std::shared_ptr< Moose::Kokkos::MaterialPropertyBase > shell)
Helper function for adding a Kokkos material property.
void copy(const Elem &elem_to, const Elem &elem_from, unsigned int side)
copy material properties from one element to another
Moose::Kokkos::MaterialProperty< T, dimension > declareKokkosProperty(const std::string &prop_name, const std::vector< unsigned int > &dims, const MaterialBase *declarer, const bool bnd, const bool on_demand, const Moose::Kokkos::PropertyConstantOption constant_option)
Declare a Kokkos material property.
bool _swapped
Status of storage swapping (calling swap sets this to true; swapBack sets it to false)
bool isSwapped() const
Returns true if the stateful material is in a swapped state.
static constexpr unsigned int max_state
The max time state supported (2 = older)
Moose::Kokkos::MaterialProperty< T, dimension > getKokkosProperty(const std::string &prop_name)
Get a Kokkos material property.
GenericMaterialProperty< T, is_ad > & getProperty(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
Retrieves a material property.
GenericMaterialProperty< T, is_ad > & getPropertyHelper(const std::string &prop_name, const unsigned int state, const MooseObject &requestor)
bool isOnlyResizeIfSmaller() const
Check value of _resize_only_if_smaller.
Stores the stateful material properties computed by materials.
Materials compute MaterialProperties.
Definition Material.h:36
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
The base class for Kokkos material properties.
The Kokkos material property class.
Abstract definition of a property value.
const T * queryValue(const std::size_t i) const
std::function< void(std::istream &, void *)> PropertyLoad
std::function< void(std::ostream &, void *)> PropertyStore
PropertyConstantOption
Property constant options.