Line data Source code
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 "KokkosTypes.h"
13 : #include "KokkosMaterialPropertyValue.h"
14 : #include "KokkosDispatcher.h"
15 : #include "KokkosFESystem.h"
16 :
17 : #include "MaterialBase.h"
18 :
19 : namespace Moose::Kokkos
20 : {
21 :
22 : /**
23 : * The base class for Kokkos materials
24 : */
25 : class MaterialBase : public ::MaterialBase,
26 : public MeshHolder,
27 : public AssemblyHolder,
28 : public FESystemHolder
29 : {
30 : public:
31 : static InputParameters validParams();
32 :
33 : /**
34 : * Constructor
35 : */
36 : MaterialBase(const InputParameters & parameters);
37 : /**
38 : * Copy constructor for parallel dispatch
39 : */
40 : MaterialBase(const MaterialBase & object);
41 :
42 : // Unused for Kokkos materials because all subdomains are computed in parallel
43 1511149 : virtual void subdomainSetup() override final {}
44 :
45 : /**
46 : * Kokkos function tags
47 : */
48 : ///@{
49 : struct ElementInit
50 : {
51 : };
52 : struct SideInit
53 : {
54 : };
55 : struct NeighborInit
56 : {
57 : };
58 : struct ElementCompute
59 : {
60 : };
61 : struct SideCompute
62 : {
63 : };
64 : struct NeighborCompute
65 : {
66 : };
67 : ///@}
68 :
69 : protected:
70 : /**
71 : * Declare a material property
72 : * @tparam T The property data type
73 : * @tparam dimension The property dimension
74 : * @param name The property name or the parameter name containing the property name
75 : * @param dims The vector containing the size of each dimension
76 : * @returns The material property
77 : */
78 : template <typename T, unsigned int dimension = 0>
79 : MaterialProperty<T, dimension> declareKokkosProperty(const std::string & name,
80 : const std::vector<unsigned int> & dims = {});
81 : /**
82 : * Declare an on-demand material property
83 : * @tparam T The property data type
84 : * @tparam dimension The property dimension
85 : * @param name The property name or the parameter name containing the property name
86 : * @param dims The vector containing the size of each dimension
87 : * @returns The material property
88 : */
89 : template <typename T, unsigned int dimension = 0>
90 : MaterialProperty<T, dimension>
91 : declareKokkosOnDemandProperty(const std::string & name,
92 : const std::vector<unsigned int> & dims = {});
93 : /**
94 : * Declare a material property by property name
95 : * @tparam T The property data type
96 : * @tparam dimension The property dimension
97 : * @param prop_name The property name
98 : * @param dims The vector containing the size of each dimension
99 : * @returns The material property
100 : */
101 : template <typename T, unsigned int dimension = 0>
102 : MaterialProperty<T, dimension>
103 2792 : declareKokkosPropertyByName(const std::string & prop_name,
104 : const std::vector<unsigned int> & dims = {})
105 : {
106 2792 : return declareKokkosPropertyInternal<T, dimension>(prop_name, dims, false);
107 : }
108 : /**
109 : * Declare an on-demand material property by property name
110 : * The on-demand property is only allocated when any object requests it
111 : * @tparam T The property data type
112 : * @tparam dimension The property dimension
113 : * @param prop_name The property name
114 : * @param dims The vector containing the size of each dimension
115 : * @returns The material property
116 : */
117 : template <typename T, unsigned int dimension = 0>
118 : MaterialProperty<T, dimension>
119 204 : declareKokkosOnDemandPropertyByName(const std::string & prop_name,
120 : const std::vector<unsigned int> & dims = {})
121 : {
122 204 : return declareKokkosPropertyInternal<T, dimension>(prop_name, dims, true);
123 : }
124 :
125 : /**
126 : * Get the number of elements this material operates on for element material property evaluation
127 : * @returns The number of elements
128 : */
129 28887 : KOKKOS_FUNCTION dof_id_type numKokkosElements() const { return _element_ids.size(); }
130 : /**
131 : * Get the number of sides this material is operating on for face material property evaluation
132 : * @returns The number of sides
133 : */
134 56456 : KOKKOS_FUNCTION dof_id_type numKokkosElementSides() const { return _element_side_ids.size(); }
135 : /**
136 : * Get the contiguous element ID for a thread
137 : * @param tid The thread ID
138 : * @returns The contiguous element ID
139 : */
140 1264715 : KOKKOS_FUNCTION ContiguousElementID kokkosElementID(ThreadID tid) const
141 : {
142 1264715 : return _element_ids[tid];
143 : }
144 : /**
145 : * Get the contiguous element ID - side index pair for a thread
146 : * @param tid The thread ID
147 : * @returns The contiguous element ID - side index pair
148 : */
149 98160 : KOKKOS_FUNCTION auto kokkosElementSideID(ThreadID tid) const { return _element_side_ids[tid]; }
150 :
151 : /**
152 : * Kokkos functor dispatchers
153 : */
154 : ///@{
155 : std::unique_ptr<DispatcherBase> _init_dispatcher;
156 : std::unique_ptr<DispatcherBase> _compute_dispatcher;
157 : ///@}
158 :
159 : /**
160 : * Whether the properties declared by this material are constant over element or subdomain
161 : */
162 : const PropertyConstantOption _constant_option;
163 :
164 : /**
165 : * TODO: Move to TransientInterface
166 : */
167 : ///@{
168 : /**
169 : * Time
170 : */
171 : Scalar<Real> _t;
172 : /**
173 : * Old time
174 : */
175 : Scalar<const Real> _t_old;
176 : /**
177 : * The number of the time step
178 : */
179 : Scalar<int> _t_step;
180 : /**
181 : * Time step size
182 : */
183 : Scalar<Real> _dt;
184 : /**
185 : * Size of the old time step
186 : */
187 : Scalar<Real> _dt_old;
188 : ///@}
189 :
190 : private:
191 : // Unused for Kokkos materials because they are hidden by Kokkos functions
192 0 : virtual void initQpStatefulProperties() override final {}
193 0 : virtual void computeQpProperties() override final {}
194 :
195 : /**
196 : * Setup block and boundary restrictions for material
197 : */
198 : void initializeMaterialRestrictable();
199 :
200 : /**
201 : * Internal method for declaring a material property
202 : * @tparam T The property data type
203 : * @tparam dimension The property dimension
204 : * @param prop_name The property name
205 : * @param dims The vector containing the size of each dimension
206 : * @param on_demand Whether the property is an on-demand property
207 : */
208 : template <typename T, unsigned int dimension>
209 : MaterialProperty<T, dimension> declareKokkosPropertyInternal(
210 : const std::string & prop_name, const std::vector<unsigned int> & dims, const bool on_demand);
211 :
212 : /**
213 : * Contiguous element IDs this material operates on for element material property evaluation
214 : */
215 : Array<ContiguousElementID> _element_ids;
216 : /**
217 : * Contiguous element ID - side index pairs this material operates on for face material property
218 : * evaluation
219 : */
220 : Array<Pair<ContiguousElementID, unsigned int>> _element_side_ids;
221 : };
222 :
223 : template <typename T, unsigned int dimension>
224 : MaterialProperty<T, dimension>
225 2792 : MaterialBase::declareKokkosProperty(const std::string & name,
226 : const std::vector<unsigned int> & dims)
227 : {
228 2792 : std::string prop_name = name;
229 2792 : if (_pars.have_parameter<MaterialPropertyName>(name))
230 561 : prop_name = _pars.get<MaterialPropertyName>(name);
231 :
232 5576 : return declareKokkosPropertyByName<T, dimension>(prop_name, dims);
233 2784 : }
234 :
235 : template <typename T, unsigned int dimension>
236 : MaterialProperty<T, dimension>
237 204 : MaterialBase::declareKokkosOnDemandProperty(const std::string & name,
238 : const std::vector<unsigned int> & dims)
239 : {
240 204 : std::string prop_name = name;
241 204 : if (_pars.have_parameter<MaterialPropertyName>(name))
242 0 : prop_name = _pars.get<MaterialPropertyName>(name);
243 :
244 408 : return declareKokkosOnDemandPropertyByName<T, dimension>(prop_name, dims);
245 204 : }
246 :
247 : template <typename T, unsigned int dimension>
248 : MaterialProperty<T, dimension>
249 2996 : MaterialBase::declareKokkosPropertyInternal(const std::string & prop_name,
250 : const std::vector<unsigned int> & dims,
251 : const bool on_demand)
252 : {
253 2996 : if (dims.size() != dimension)
254 0 : mooseError("The declared Kokkos material property '",
255 : prop_name,
256 : "'\nhas a different dimension (",
257 : dimension,
258 : ") with the provided dimension (",
259 0 : dims.size(),
260 : ").");
261 :
262 4694 : const auto prop_name_modified =
263 1698 : _declare_suffix.empty()
264 1698 : ? prop_name
265 1698 : : MooseUtils::join(std::vector<std::string>({prop_name, _declare_suffix}), "_");
266 :
267 2996 : auto prop = materialData().declareKokkosProperty<T, dimension>(
268 1698 : prop_name_modified, dims, this, isBoundaryMaterial(), on_demand, _constant_option);
269 :
270 2988 : registerPropName(prop_name_modified, false, 0);
271 :
272 5976 : return prop;
273 2988 : }
274 :
275 : } // namespace Moose::Kokkos
|