BioCMAMC-ST
uptake_dyn.hpp
1#ifndef __MODELS_UPTAKES_DYN_HPP__
2#define __MODELS_UPTAKES_DYN_HPP__
3
4#include "Kokkos_Assert.hpp"
5#include <common/traits.hpp>
6#include <cstddef>
7#include <mc/prng/prng_extension.hpp>
8#include <mc/traits.hpp>
9#include <models/uptake.hpp>
10
11enum Uptakeparticle_var : int
12{
13 ap_1 = 0,
14 ap_2,
15 ap_3,
16 COUNT
17};
18
19// TODO Put ELSEWHERE
20template <FloatingPointType F> consteval F freq(F tau)
21{
22 return F(1) / tau;
23}
24
25namespace Models
26{
27
28 // TODO Put ELSEWHERE
29
30 template <FloatingPointType F>
31 KOKKOS_INLINE_FUNCTION constexpr F f_saturation(const F x, const F k) noexcept
32 {
33 return x / (x + k);
34 }
35
36 template <typename T>
37 concept UptakeModel = requires(T model) {
38 // { T::NPermease_init } -> std::convertible_to<typename T::FloatType>;
39
40 // { T::NPermease_max } -> std::convertible_to<typename T::FloatType>;
41
42 { T::k } -> std::convertible_to<typename T::FloatType>;
43 { T::k_perm } -> std::convertible_to<typename T::FloatType>;
44
45 { T::beta } -> std::convertible_to<typename T::FloatType>;
46 { T::tau_ap_1 } -> std::convertible_to<typename T::FloatType>;
47 { T::tau_ap_2 } -> std::convertible_to<typename T::FloatType>;
48 { T::tau_ap_2 } -> std::convertible_to<typename T::FloatType>;
49
50 // { T::tau_new_permease } -> std::convertible_to<typename T::FloatType>;
51
52 // { T::tau_rm_perm } -> std::convertible_to<typename T::FloatType>;
53
54 // { T::tau_pts } -> std::convertible_to<typename T::FloatType>;
55
56 // { T::tau_Au } -> std::convertible_to<typename T::FloatType>;
57
58 // { T::tau_Ad } -> std::convertible_to<typename T::FloatType>;
59
60 // { T::delta } -> std::convertible_to<typename T::FloatType>;
61 };
62
63 template <UptakeModel U, ModelType M = U> struct Uptake
64 {
65 static constexpr std::size_t n_var =
66 static_cast<std::size_t>(Uptakeparticle_var::COUNT);
67 static constexpr std::string_view name = "uptake";
68 using uniform_weight = std::true_type;
69 using Self = Uptake;
70 using FloatType = M::FloatType;
72 using Config = std::nullopt_t;
73
74 static KOKKOS_INLINE_FUNCTION constexpr FloatType
75 f_G(const FloatType x) noexcept
76 {
77 return f_saturation(x, U::k);
78 }
79
80 static KOKKOS_INLINE_FUNCTION constexpr FloatType
81 phi_pts(const FloatType phi_max, const FloatType a_pts, const FloatType S)
82 {
83 return a_pts * phi_max * f_G(S);
84 }
85
86 static KOKKOS_INLINE_FUNCTION constexpr FloatType
88 {
89 return ap_2 * ap_3 * phi_max * U::beta * f_saturation(S, U::k_perm);
90 }
91
92 // static KOKKOS_INLINE_FUNCTION consexpr FloatType phi(FloatType phi_max,
93 // FloatType s)
94 //{
95 // constexpr FloatType k_perm = U::k / 10;
96 // constexpr FloatType sigma = k_perm / 10.;
97 // const FloatType innerexp = (s - k_perm) / sigma;
98 // return a_permease * a_permease_2 * phi_max * U::beta *
99 // Kokkos::exp(-innerexp * innerexp);
100 //
101 // return phi_max * (a_pts * f_G(s) + )
102 //}
103 //
104 KOKKOS_INLINE_FUNCTION
105 static void init(const MC::KPRNG::pool_type& random_pool,
106 std::size_t idx,
107 const SelfParticle& arr)
108 {
109
110 // static constexpr FloatType half = FloatType(0.5);
111 auto gen = random_pool.get_state();
112 // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
113 GET_PROPERTY(Uptakeparticle_var::ap_1) =
115 gen,
116 FloatType(1e-3),
117 FloatType(1e-4),
118 FloatType(0.),
119 FloatType(1.));
120
121 GET_PROPERTY(Uptakeparticle_var::ap_2) =
123 gen,
124 FloatType(0.8),
125 FloatType(0.1),
126 FloatType(0.),
127 FloatType(1.));
128
129 GET_PROPERTY(Uptakeparticle_var::ap_3) =
131 gen,
132 FloatType(0.8),
133 FloatType(0.1),
134 FloatType(0.),
135 FloatType(1.));
136
137 // NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
138 random_pool.free_state(gen);
139 }
140
141 KOKKOS_INLINE_FUNCTION static FloatType
143 std::size_t idx,
144 const SelfParticle& arr,
145 const MC::LocalConcentration& c,
146 FloatType* r_phi_pts = nullptr,
147 FloatType* r_phi_perm = nullptr)
148 {
149 const auto s = c(0);
150
151 const FloatType phi_s_pts =
152 phi_pts(phi_max, GET_PROPERTY(Uptakeparticle_var::ap_1), s);
153 if (r_phi_pts != nullptr)
154 {
155 *r_phi_pts = phi_s_pts;
156 }
157 const auto phi_s_perm =
158 phi_permease(phi_max,
159 GET_PROPERTY(Uptakeparticle_var::ap_1),
160 GET_PROPERTY(Uptakeparticle_var::ap_2),
161 s);
162 if (r_phi_perm != nullptr)
163 {
164 *r_phi_perm = phi_s_perm;
165 }
166
167 return phi_s_pts + phi_s_perm;
168 }
169
170 KOKKOS_INLINE_FUNCTION static FloatType
172 FloatType d_t,
173 std::size_t idx,
174 const SelfParticle& arr,
175 const MC::LocalConcentration& c,
176 FloatType* r_phi_pts = nullptr,
177 FloatType* r_phi_perm = nullptr)
178 {
179 constexpr FloatType one = FloatType(1);
180 constexpr FloatType f_ap_1 = 1. / U::tau_ap_1;
181 constexpr FloatType f_ap_2 = 1. / U::tau_ap_2;
182 constexpr FloatType f_ap_3 = 1. / U::tau_ap_3;
183
184 auto& ap_1 = GET_PROPERTY(Uptakeparticle_var::ap_1);
185 auto& ap_2 = GET_PROPERTY(Uptakeparticle_var::ap_2);
186 auto& ap_3 = GET_PROPERTY(Uptakeparticle_var::ap_3);
187
188 auto phi_s = uptake(phi_max, idx, arr, c, r_phi_pts, r_phi_perm);
189
190 const auto s = c(0);
191 KOKKOS_ASSERT(s >= 0.F);
192 const auto G = f_G(s);
193
194 // constexpr FloatType k_perm = U::k_perm;
195 // constexpr FloatType sigma = k_perm;
196 // const FloatType innerexp = (s - 10*k_perm) / sigma;
197
198 const FloatType ap1_star = G;
199 // const FloatType ap2_star = Kokkos::exp(-innerexp*innerexp);
200
201 constexpr auto yon = U::k_perm / 10.;
202 constexpr auto yoff = U::k_perm * 5.;
203 constexpr auto alpha = (1. - 0.) / (yoff - yon);
204 constexpr auto beta = -yon * alpha;
205
206 const FloatType ap2_star = (s >= yon) & (s < yoff);
207 const FloatType ap3_star = (one - ap_1 * G);
208
209 ap_1 += d_t * f_ap_1 * (ap1_star - ap_1);
210
211 ap_2 += d_t * f_ap_2 * (ap2_star - ap_2);
212
213 ap_3 += d_t * f_ap_3 * (ap3_star - ap_3);
214
215 return phi_s;
216 }
217
218 KOKKOS_INLINE_FUNCTION static void
219 division(const MC::KPRNG::pool_type& random_pool,
220 std::size_t idx,
221 std::size_t idx2,
222 const SelfParticle& arr,
223 const SelfParticle& buffer_arr)
224 {
225 static constexpr FloatType half = FloatType(0.5);
226 auto generator = random_pool.get_state();
227 GET_PROPERTY_FROM(idx2, buffer_arr, Uptakeparticle_var::ap_1) =
229 generator,
230 GET_PROPERTY(Uptakeparticle_var::ap_1),
231 GET_PROPERTY(Uptakeparticle_var::ap_1) * half,
232 FloatType(0.),
233 FloatType(1.));
234
235 GET_PROPERTY_FROM(idx2, buffer_arr, Uptakeparticle_var::ap_2) =
237 generator,
238 GET_PROPERTY(Uptakeparticle_var::ap_1),
239 GET_PROPERTY(Uptakeparticle_var::ap_2) * half,
240 FloatType(0.),
241 FloatType(1.));
242
243 const auto new_n_permease = GET_PROPERTY(Uptakeparticle_var::ap_3) * half;
244 GET_PROPERTY(Uptakeparticle_var::ap_3) = new_n_permease;
245 GET_PROPERTY_FROM(idx2, buffer_arr, Uptakeparticle_var::ap_3) =
246 new_n_permease;
247 random_pool.free_state(generator);
248 }
249
250 [[maybe_unused]] KOKKOS_INLINE_FUNCTION static void
251 contribution([[maybe_unused]] std::size_t idx,
252 [[maybe_unused]] std::size_t position,
253 [[maybe_unused]] double weight,
254 [[maybe_unused]] const SelfParticle& arr,
255 [[maybe_unused]] const MC::ContributionView& contributions)
256 {
257 }
258
259 KOKKOS_INLINE_FUNCTION static double mass(std::size_t idx,
260 const SelfParticle& arr)
261 {
262 (void)idx;
263 (void)arr;
264 return 0.;
265 }
266
267 inline constexpr static std::array<std::string_view, n_var> names()
268 {
269 constexpr std::array<std::string_view, n_var> _names = {
270 "a_pts", "a_permease_1", "a_permease_2"};
271 static_assert(_names.size() == n_var);
272 return _names;
273 }
274 };
275
276 // CHECK_MODEL(Uptake<DefaultModel>)
277} // namespace Models
278
279#endif
Kokkos::Random_XorShift1024_Pool< Kokkos::DefaultExecutionSpace > pool_type
Definition prng.hpp:33
Definition uptake_dyn.hpp:37
decltype(Kokkos::Experimental::create_scatter_view(kernelContribution())) ContributionView
Definition alias.hpp:64
Kokkos::Subview< KernelConcentrationType, int, decltype(Kokkos::ALL)> LocalConcentration
Definition alias.hpp:72
Kokkos::View< F *[Nd], Kokkos::LayoutRight > ParticlesModel
Definition traits.hpp:34
Models definition.
Definition config_loader.hpp:9
KOKKOS_INLINE_FUNCTION constexpr F f_saturation(const F x, const F k) noexcept
Definition uptake_dyn.hpp:31
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F mu, F sigma, F lower, F upper)
Definition prng_extension.hpp:303
Definition uptake_dyn.hpp:64
M::FloatType FloatType
Definition uptake_dyn.hpp:70
static KOKKOS_INLINE_FUNCTION void contribution(std::size_t idx, std::size_t position, double weight, const SelfParticle &arr, const MC::ContributionView &contributions)
Definition uptake_dyn.hpp:251
MC::ParticlesModel< M::n_var, Self::FloatType > SelfParticle
Definition uptake_dyn.hpp:71
std::nullopt_t Config
Definition uptake_dyn.hpp:72
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition uptake_dyn.hpp:259
static KOKKOS_INLINE_FUNCTION void division(const MC::KPRNG::pool_type &random_pool, std::size_t idx, std::size_t idx2, const SelfParticle &arr, const SelfParticle &buffer_arr)
Definition uptake_dyn.hpp:219
static KOKKOS_INLINE_FUNCTION void init(const MC::KPRNG::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition uptake_dyn.hpp:105
static KOKKOS_INLINE_FUNCTION constexpr FloatType phi_pts(const FloatType phi_max, const FloatType a_pts, const FloatType S)
Definition uptake_dyn.hpp:81
static KOKKOS_INLINE_FUNCTION constexpr FloatType f_G(const FloatType x) noexcept
Definition uptake_dyn.hpp:75
static KOKKOS_INLINE_FUNCTION FloatType uptake(FloatType phi_max, std::size_t idx, const SelfParticle &arr, const MC::LocalConcentration &c, FloatType *r_phi_pts=nullptr, FloatType *r_phi_perm=nullptr)
Definition uptake_dyn.hpp:142
static KOKKOS_INLINE_FUNCTION constexpr FloatType phi_permease(FloatType phi_max, FloatType ap_2, FloatType ap_3, FloatType S)
Definition uptake_dyn.hpp:87
static constexpr std::array< std::string_view, n_var > names()
Definition uptake_dyn.hpp:267
static constexpr std::string_view name
Definition uptake_dyn.hpp:67
static KOKKOS_INLINE_FUNCTION FloatType uptake_step(FloatType phi_max, FloatType d_t, std::size_t idx, const SelfParticle &arr, const MC::LocalConcentration &c, FloatType *r_phi_pts=nullptr, FloatType *r_phi_perm=nullptr)
Definition uptake_dyn.hpp:171
Uptake Self
Definition uptake_dyn.hpp:69
std::true_type uniform_weight
Definition uptake_dyn.hpp:68
static constexpr std::size_t n_var
Definition uptake_dyn.hpp:65