Line data Source code
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 "KokkosTypes.h"
13 : #include "KokkosFunctionWrapper.h"
14 : #include "KokkosFunctorRegistry.h"
15 :
16 : #include "FunctionBase.h"
17 :
18 : namespace Moose::Kokkos
19 : {
20 :
21 : /**
22 : * The base class for a user to derive their own Kokkos functions.
23 : *
24 : * The user should define the hook methods in their derived class as inlined public methods (not
25 : * virtual override) with the same signature. If they are defined in the derived class, they will
26 : * hide the default definitions in the base class. However, the default definitions are not to be
27 : * actually called. If a hook method was not defined in the derived class, it should not be called.
28 : */
29 : class FunctionBase : public Moose::FunctionBase
30 : {
31 : public:
32 : static InputParameters validParams();
33 :
34 : /**
35 : * Constructor
36 : */
37 : FunctionBase(const InputParameters & parameters);
38 : /**
39 : * Copy constructor for parallel dispatch
40 : */
41 : FunctionBase(const FunctionBase & object);
42 :
43 : /**
44 : * Evaluate a scalar value at point (t,x,y,z)
45 : * @param t The time
46 : * @param p The location in space (x,y,z)
47 : * @returns The scalar value evaluated at the time and location
48 : */
49 : KOKKOS_FUNCTION Real value(Real /* t */, Real3 /* p */) const
50 : {
51 : KOKKOS_ASSERT(false);
52 : return 0;
53 : }
54 : /**
55 : * Evaluate a vector value at point (t,x,y,z)
56 : * @param t The time
57 : * @param p The location in space (x,y,z)
58 : * @returns The vector value evaluated at the time and location
59 : */
60 0 : KOKKOS_FUNCTION Real3 vectorValue(Real /* t */, Real3 /* p */) const
61 : {
62 : KOKKOS_ASSERT(false);
63 0 : return Real3(0);
64 : }
65 : /**
66 : * Evaluate a gradient at point (t,x,y,z)
67 : * @param t The time
68 : * @param p The location in space (x,y,z)
69 : * @returns The gradient evaluated at the time and location
70 : */
71 0 : KOKKOS_FUNCTION Real3 gradient(Real /* t */, Real3 /* p */) const
72 : {
73 : KOKKOS_ASSERT(false);
74 0 : return Real3(0);
75 : }
76 : /**
77 : * Evaluate a curl at point (t,x,y,z)
78 : * @param t The time
79 : * @param p The location in space (x,y,z)
80 : * @returns The curl evaluated at the time and location
81 : */
82 0 : KOKKOS_FUNCTION Real3 curl(Real /* t */, Real3 /* p */) const
83 : {
84 : KOKKOS_ASSERT(false);
85 0 : return Real3(0);
86 : }
87 : /**
88 : * Evaluate a divergence at point (t,x,y,z)
89 : * @param t The time
90 : * @param p The location in space (x,y,z)
91 : * @returns The divergence evaluated at the time and location
92 : */
93 0 : KOKKOS_FUNCTION Real div(Real /* t */, Real3 /* p */) const
94 : {
95 : KOKKOS_ASSERT(false);
96 0 : return 0;
97 : }
98 : /**
99 : * Evaluate a time derivative at point (t,x,y,z)
100 : * @param t The time
101 : * @param p The location in space (x,y,z)
102 : * @returns The time derivative evaluated at the time and location
103 : */
104 0 : KOKKOS_FUNCTION Real timeDerivative(Real /* t */, Real3 /* p */) const
105 : {
106 : KOKKOS_ASSERT(false);
107 0 : return 0;
108 : }
109 : /**
110 : * Evaluate a time integral at point (x,y,z) between time \p t1 and \p t2
111 : * @param t1 The beginning time
112 : * @param t2 The end time
113 : * @param p The location in space (x,y,z)
114 : * @returns The time integral evaluated at the location between the times
115 : */
116 0 : KOKKOS_FUNCTION Real timeIntegral(Real /* t1 */, Real /* t2 */, Real3 /* p */) const
117 : {
118 : KOKKOS_ASSERT(false);
119 0 : return 0;
120 : }
121 : /**
122 : * Evaluate the integral over the domain
123 : * @returns The integral over the domain
124 : */
125 0 : KOKKOS_FUNCTION Real integral() const
126 : {
127 : KOKKOS_ASSERT(false);
128 0 : return 0;
129 : }
130 : /**
131 : * Evaluate the average over the domain
132 : * @returns The average over the domain
133 : */
134 0 : KOKKOS_FUNCTION Real average() const
135 : {
136 : KOKKOS_ASSERT(false);
137 0 : return 0;
138 : }
139 : };
140 :
141 : /**
142 : * The abstract class that provides polymorphic interfaces for a function.
143 : *
144 : * NOTE: This class is not the base class for a Kokkos function derivation. The user should derive
145 : * their own function from Moose::Kokkos::FunctionBase.
146 : */
147 : class Function final
148 : {
149 : public:
150 : /**
151 : * Default constructor
152 : */
153 : Function() = default;
154 : /**
155 : * Constructor
156 : * @param wrapper The host function wrapper
157 : */
158 : Function(std::shared_ptr<FunctionWrapperHostBase> wrapper);
159 : /**
160 : * Copy constructor for parallel dispatch
161 : */
162 : Function(const Function & function);
163 : /**
164 : * Destructor
165 : */
166 : ~Function();
167 :
168 : /**
169 : * Get whether the function wrapper is valid
170 : * @returns Whether the function wrapper is valid
171 : */
172 : KOKKOS_FUNCTION operator bool() const
173 : {
174 : KOKKOS_IF_ON_HOST(return static_cast<bool>(_wrapper_host);)
175 :
176 : return _wrapper_device != nullptr;
177 : }
178 :
179 13123379 : KOKKOS_FUNCTION Real value(Real t, Real3 p) const
180 : {
181 : KOKKOS_ASSERT(_wrapper_device);
182 :
183 13123379 : return _wrapper_device->value(t, p);
184 : }
185 : KOKKOS_FUNCTION Real3 vectorValue(Real t, Real3 p) const
186 : {
187 : KOKKOS_ASSERT(_wrapper_device);
188 :
189 : return _wrapper_device->vectorValue(t, p);
190 : }
191 : KOKKOS_FUNCTION Real3 gradient(Real t, Real3 p) const
192 : {
193 : KOKKOS_ASSERT(_wrapper_device);
194 :
195 : return _wrapper_device->gradient(t, p);
196 : }
197 : KOKKOS_FUNCTION Real3 curl(Real t, Real3 p) const
198 : {
199 : KOKKOS_ASSERT(_wrapper_device);
200 :
201 : return _wrapper_device->curl(t, p);
202 : }
203 : KOKKOS_FUNCTION Real div(Real t, Real3 p) const
204 : {
205 : KOKKOS_ASSERT(_wrapper_device);
206 :
207 : return _wrapper_device->div(t, p);
208 : }
209 : KOKKOS_FUNCTION Real timeDerivative(Real t, Real3 p) const
210 : {
211 : KOKKOS_ASSERT(_wrapper_device);
212 :
213 : return _wrapper_device->timeDerivative(t, p);
214 : }
215 : KOKKOS_FUNCTION Real timeIntegral(Real t1, Real t2, Real3 p) const
216 : {
217 : KOKKOS_ASSERT(_wrapper_device);
218 :
219 : return _wrapper_device->timeIntegral(t1, t2, p);
220 : }
221 : KOKKOS_FUNCTION Real integral() const
222 : {
223 : KOKKOS_ASSERT(_wrapper_device);
224 :
225 : return _wrapper_device->integral();
226 : }
227 : KOKKOS_FUNCTION Real average() const
228 : {
229 : KOKKOS_ASSERT(_wrapper_device);
230 :
231 : return _wrapper_device->average();
232 : }
233 :
234 : private:
235 : /**
236 : * Pointer to the host function wrapper
237 : */
238 : std::shared_ptr<FunctionWrapperHostBase> _wrapper_host;
239 : /**
240 : * Pointer to the device function wrapper
241 : */
242 : FunctionWrapperDeviceBase * _wrapper_device = nullptr;
243 : };
244 :
245 : } // namespace Moose::Kokkos
|