https://mooseframework.inl.gov
KokkosMaterial.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 // initQpStatefulProperties() and computeQpProperties() are intentionally hidden
13 // but some compilers generate ugly warnings
14 
15 #if defined(__clang__)
16 #pragma clang diagnostic push
17 #pragma clang diagnostic ignored "-Woverloaded-virtual"
18 #elif defined(__GNUC__) || defined(__GNUG__)
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
21 #endif
22 
23 #include "KokkosMaterialBase.h"
24 #include "KokkosDatum.h"
25 
26 #include "Coupleable.h"
28 
29 namespace Moose
30 {
31 namespace Kokkos
32 {
33 
62 template <typename Derived>
64 {
65 public:
67 
72 
76  Material(const Material & object);
77 
81  virtual void initStatefulProperties(unsigned int) override;
85  virtual void computeProperties() override;
86 
91 
97  KOKKOS_FUNCTION void initQpStatefulProperties(const unsigned int /* qp */,
98  Datum & /* datum */) const
99  {
100  }
102 
106  KOKKOS_FUNCTION void operator()(ElementInit, const ThreadID tid) const;
108  KOKKOS_FUNCTION void operator()(SideInit, const ThreadID tid) const;
109  KOKKOS_FUNCTION void operator()(NeighborInit, const ThreadID tid) const;
110  KOKKOS_FUNCTION void operator()(ElementCompute, const ThreadID tid) const;
111  KOKKOS_FUNCTION void operator()(SideCompute, const ThreadID tid) const;
112  KOKKOS_FUNCTION void operator()(NeighborCompute, const ThreadID tid) const;
114 
115 protected:
124  template <typename T, unsigned int dimension = 0, unsigned int state = 0>
125  MaterialProperty<T, dimension> getKokkosMaterialPropertyByName(const std::string & prop_name_in);
133  template <typename T, unsigned int dimension = 0>
135  {
136  return getKokkosMaterialPropertyByName<T, dimension, 1>(prop_name);
137  }
145  template <typename T, unsigned int dimension = 0>
147  {
148  return getKokkosMaterialPropertyByName<T, dimension, 2>(prop_name);
149  }
158  template <typename T, unsigned int dimension = 0, unsigned int state = 0>
160  {
161  return getKokkosMaterialPropertyByName<T, dimension, state>(getMaterialPropertyName(name));
162  }
170  template <typename T, unsigned int dimension = 0>
172  {
173  return getKokkosMaterialPropertyByName<T, dimension, 1>(getMaterialPropertyName(name));
174  }
182  template <typename T, unsigned int dimension = 0>
184  {
185  return getKokkosMaterialPropertyByName<T, dimension, 2>(getMaterialPropertyName(name));
186  }
187 
188  virtual void checkMaterialProperty(const std::string & name, const unsigned int state) override
189  {
190  // Avoid performing duplicate checks for triple block/face/neighbor materials
191  if (boundaryRestricted() || !_bnd)
193  }
194 
195  virtual bool isBoundaryMaterial() const override { return _bnd; }
196 
197  virtual const std::unordered_set<unsigned int> & getMatPropDependencies() const override
198  {
200  }
201 
202  virtual const MaterialData & materialData() const override { return _material_data; }
203  virtual MaterialData & materialData() override { return _material_data; }
205 
209  const bool _bnd;
213  const bool _neighbor;
214 
215 private:
219  const bool _default_init;
220 
224  const QBase * const & _qrule;
226  virtual const QBase & qRule() const override { return *_qrule; }
228 };
229 
230 template <typename Derived>
233 {
236  params.addParamNamesToGroup("use_displaced_mesh", "Advanced");
237  return params;
238 }
239 
240 template <typename Derived>
242  : MaterialBase(parameters),
243  Coupleable(this, false),
244  MaterialPropertyInterface(this, blockIDs(), boundaryIDs()),
245  _bnd(_material_data_type != Moose::BLOCK_MATERIAL_DATA),
246  _neighbor(_material_data_type == Moose::NEIGHBOR_MATERIAL_DATA),
247  _default_init(&Derived::initQpStatefulProperties == &Material::initQpStatefulProperties),
248  _qrule(_bnd ? (_neighbor ? _subproblem.assembly(_tid, 0).qRuleNeighbor()
249  : _subproblem.assembly(_tid, 0).qRuleFace())
250  : _subproblem.assembly(_tid, 0).qRule())
251 {
252  for (auto coupled_var : getCoupledMooseVars())
253  addMooseVariableDependency(coupled_var);
254 }
255 
256 template <typename Derived>
258  : MaterialBase(object),
259  Coupleable(object, {}),
260  MaterialPropertyInterface(object, {}),
261  _bnd(object._bnd),
262  _neighbor(object._neighbor),
263  _default_init(object._default_init),
264  _qrule(object._qrule)
265 {
266 }
267 
268 template <typename Derived>
269 void
271 {
272  if (_default_init)
273  return;
274 
275  if (!_bnd && !_neighbor)
276  ::Kokkos::parallel_for(
277  ::Kokkos::RangePolicy<ElementInit, ExecSpace, ::Kokkos::IndexType<ThreadID>>(
278  0, numKokkosElements()),
279  *static_cast<Derived *>(this));
280  else if (_bnd && !_neighbor)
281  ::Kokkos::parallel_for(
282  ::Kokkos::RangePolicy<SideInit, ExecSpace, ::Kokkos::IndexType<ThreadID>>(
283  0, numKokkosElementSides()),
284  *static_cast<Derived *>(this));
285  else
286  ::Kokkos::parallel_for(
287  ::Kokkos::RangePolicy<NeighborInit, ExecSpace, ::Kokkos::IndexType<ThreadID>>(
288  0, numKokkosElementSides()),
289  *static_cast<Derived *>(this));
290 
291  ::Kokkos::fence();
292 }
293 
294 template <typename Derived>
295 void
297 {
298  if (!_bnd && !_neighbor)
299  ::Kokkos::parallel_for(
300  ::Kokkos::RangePolicy<ElementCompute, ExecSpace, ::Kokkos::IndexType<ThreadID>>(
301  0, numKokkosElements()),
302  *static_cast<Derived *>(this));
303  else if (_bnd && !_neighbor)
304  ::Kokkos::parallel_for(
305  ::Kokkos::RangePolicy<SideCompute, ExecSpace, ::Kokkos::IndexType<ThreadID>>(
306  0, numKokkosElementSides()),
307  *static_cast<Derived *>(this));
308  else
309  ::Kokkos::parallel_for(
310  ::Kokkos::RangePolicy<NeighborCompute, ExecSpace, ::Kokkos::IndexType<ThreadID>>(
311  0, numKokkosElementSides()),
312  *static_cast<Derived *>(this));
313 
314  ::Kokkos::fence();
315 }
316 
317 template <typename Derived>
318 KOKKOS_FUNCTION void
320 {
321  auto material = static_cast<const Derived *>(this);
322  auto elem = kokkosElementID(tid);
323 
324  Datum datum(elem, kokkosAssembly(), kokkosSystems());
325 
326  for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
327  {
328  datum.reinit();
329  material->initQpStatefulProperties(qp, datum);
330  }
331 }
332 
333 template <typename Derived>
334 KOKKOS_FUNCTION void
336 {
337  auto material = static_cast<const Derived *>(this);
338  auto [elem, side] = kokkosElementSideID(tid);
339 
340  Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
341 
342  for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
343  {
344  datum.reinit();
345  material->initQpStatefulProperties(qp, datum);
346  }
347 }
348 
349 template <typename Derived>
350 KOKKOS_FUNCTION void
352 {
353  auto material = static_cast<const Derived *>(this);
354  auto [elem, side] = kokkosElementSideID(tid);
355 
356  Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
357 
358  for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
359  {
360  datum.reinit();
361  material->initQpStatefulProperties(qp, datum);
362  }
363 }
364 
365 template <typename Derived>
366 KOKKOS_FUNCTION void
368 {
369  auto material = static_cast<const Derived *>(this);
370  auto elem = kokkosElementID(tid);
371 
372  Datum datum(elem, kokkosAssembly(), kokkosSystems());
373 
374  for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
375  {
376  datum.reinit();
377  material->computeQpProperties(qp, datum);
378  }
379 }
380 
381 template <typename Derived>
382 KOKKOS_FUNCTION void
384 {
385  auto material = static_cast<const Derived *>(this);
386  auto [elem, side] = kokkosElementSideID(tid);
387 
388  Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
389 
390  for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
391  {
392  datum.reinit();
393  material->computeQpProperties(qp, datum);
394  }
395 }
396 
397 template <typename Derived>
398 KOKKOS_FUNCTION void
400 {
401  auto material = static_cast<const Derived *>(this);
402  auto [elem, side] = kokkosElementSideID(tid);
403 
404  Datum datum(elem, side, kokkosAssembly(), kokkosSystems());
405 
406  for (unsigned int qp = 0; qp < datum.n_qps(); qp++)
407  {
408  datum.reinit();
409  material->computeQpProperties(qp, datum);
410  }
411 }
412 
413 template <typename Derived>
414 template <typename T, unsigned int dimension, unsigned int state>
416 Material<Derived>::getKokkosMaterialPropertyByName(const std::string & prop_name_in)
417 {
419 
420  const auto prop_name =
421  _get_suffix.empty()
422  ? prop_name_in
423  : MooseUtils::join(std::vector<std::string>({prop_name_in, _get_suffix}), "_");
424 
425  if constexpr (state == 0)
426  _requested_props.insert(prop_name);
427 
428  auto prop =
429  MaterialPropertyInterface::getKokkosMaterialPropertyByName<T, dimension, state>(prop_name);
430 
431  registerPropName(prop_name, true, state);
432 
433  return prop;
434 }
435 
436 } // namespace Kokkos
437 } // namespace Moose
virtual const std::unordered_set< unsigned int > & getMatPropDependencies() const
Retrieve the set of material properties that this object depends on.
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
Python-like join function for strings over an iterator range.
Definition: MooseUtils.h:142
virtual void checkMaterialProperty(const std::string &name, const unsigned int state)
A helper method for checking material properties This method was required to avoid a compiler problem...
virtual const std::unordered_set< unsigned int > & getMatPropDependencies() const override
Retrieve the set of material properties that this object depends on.
The Kokkos object that holds thread-private data in the parallel operations of any Kokkos object...
Definition: KokkosDatum.h:25
virtual bool boundaryRestricted() const
Returns true if this object has been restricted to a boundary.
dof_id_type ThreadID
Definition: KokkosThread.h:18
KOKKOS_FUNCTION void initQpStatefulProperties(const unsigned int, Datum &) const
Default methods to prevent compile errors even when these methods were not defined in the derived cla...
virtual bool isBoundaryMaterial() const override
Returns true of the MaterialData type is not associated with volume data.
const bool _bnd
Flag whether the material is on faces.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
MaterialDataType
MaterialData types.
Definition: MooseTypes.h:692
virtual void computeProperties() override
Performs the quadrature point loop, calling computeQpProperties.
Definition: Material.C:111
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
static InputParameters validParams()
MaterialPropertyName getMaterialPropertyName(const std::string &name) const
virtual void initStatefulProperties(unsigned int) override
Dispatch stateful material property initialization.
virtual void checkMaterialProperty(const std::string &name, const unsigned int state) override
A helper method for checking material properties This method was required to avoid a compiler problem...
MaterialData & _material_data
The material data class that stores properties.
MaterialProperty< T, dimension > getKokkosMaterialPropertyOlder(const std::string &name)
Get an older material property.
MaterialProperty< T, dimension > getKokkosMaterialPropertyOldByName(const std::string &prop_name)
Get an old material property by property name.
const std::string & name() const
Get the name of the class.
Definition: MooseBase.h:103
const QBase *const & _qrule
Dummy members unused for Kokkos materials.
void checkExecutionStage()
Check and throw an error if the execution has progressed past the construction stage.
Definition: MaterialBase.C:292
static InputParameters validParams()
KOKKOS_FUNCTION unsigned int n_qps() const
Get the number of local quadrature points.
Definition: KokkosDatum.h:95
virtual MaterialDataType materialDataType() override
virtual void initStatefulProperties(unsigned int n_points)
Initialize stateful properties (if material has some)
Definition: MaterialBase.C:159
const std::vector< MooseVariableFieldBase * > & getCoupledMooseVars() const
Get the list of all coupled variables.
Definition: Coupleable.h:81
virtual void computeProperties() override
Dispatch material property evaluation.
MaterialProperty< T, dimension > getKokkosMaterialPropertyOld(const std::string &name)
Get an old material property.
Interface for objects that needs coupling capabilities.
Definition: Coupleable.h:49
static InputParameters validParams()
void addMooseVariableDependency(MooseVariableFieldBase *var)
Call this function to add the passed in MooseVariableFieldBase as a variable that this object depends...
The base class for a user to derive their own Kokkos materials.
MaterialProperty< T, dimension > getKokkosMaterialPropertyOlderByName(const std::string &prop_name)
Get an older material property by property name.
An interface for accessing Materials.
virtual MaterialData & materialData() override
KOKKOS_FUNCTION void operator()(ElementInit, const ThreadID tid) const
The parallel computation entry functions called by Kokkos.
Moose::Kokkos::MaterialProperty< T, dimension > getKokkosMaterialPropertyByName(const std::string &prop_name)
Get a Kokkos material property by property name for any state.
const bool _default_init
Flag whether initQpStatefulProperties() was not defined in the derived class.
Proxy for accessing MaterialPropertyStorage.
Definition: MaterialData.h:37
virtual const MaterialData & materialData() const override
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
virtual const QBase & qRule() const override
const Moose::MaterialDataType _material_data_type
The type of data.
The Kokkos material property class.
MaterialProperty< T, dimension > getKokkosMaterialPropertyByName(const std::string &prop_name_in)
Get a material property by property name for any state.
The base class for Kokkos materials.
Material(const InputParameters &parameters)
Constructor.
const bool _neighbor
Flag whether the material is on neighbor faces.
MaterialProperty< T, dimension > getKokkosMaterialProperty(const std::string &name)
Get a material property for any state.
KOKKOS_FUNCTION void reinit()
Reset the reinit flag.
Definition: KokkosDatum.h:147
Material(const InputParameters &parameters)
Definition: Material.C:32
void addParamNamesToGroup(const std::string &space_delim_names, const std::string group_name)
This method takes a space delimited list of parameter names and adds them to the specified group name...