BioCMAMC-ST
two_meta_nb.hpp
1#ifndef __TWOMETA_MODEL_NB_HPP__
2#define __TWOMETA_MODEL_NB_HPP__
3
4#include <common/traits.hpp>
5#include <mc/macros.hpp>
6#include <mc/prng/prng_extension.hpp>
7#include <mc/traits.hpp>
8#include <models/uptake_dyn.hpp>
9#include <models/utils.hpp>
10#include <string_view>
11
12namespace Models
13{
14
15 struct TwoMetaNb
16 {
17 using uniform_weight = std::true_type; // Using type alias
18 using Self = TwoMetaNb;
19 using FloatType = float;
20 using Config = std::nullopt_t;
21
22 enum class particle_var : int
23 {
24 age = INDEX_FROM_ENUM(Uptakeparticle_var::COUNT),
29 nu_eff_1, // This is not itself a model property but stored to be exported
30 nu_eff_2, // This is not itself a model property but stored to be exported
31 // TODO FIND BETTER WAY TO STORE/GET CONTRIBUTIONS
37 };
38
39 static constexpr std::size_t n_var = INDEX_FROM_ENUM(particle_var::COUNT);
40 static constexpr std::string_view name = "two_mode_nb";
42
43 // Constants BEGIN
44 MODEL_CONSTANT FloatType MolarMassG
46 MODEL_CONSTANT FloatType MolarMassO2
48
49 MODEL_CONSTANT FloatType l_max_m = 5e-6; // m
50 MODEL_CONSTANT FloatType l_c_m = 3e-6; // m
51 MODEL_CONSTANT FloatType d_m = 0.3e-6; // m
52 MODEL_CONSTANT FloatType l_min_m = 0.9e-6; // m
53
54 MODEL_CONSTANT FloatType dl_max_ms
55 = 10 * 2e-10; // m/s https://doi.org/10.7554/eLife.67495;
56
57 MODEL_CONSTANT FloatType lin_density
58 = c_linear_density(static_cast<FloatType>(1000), d_m);
59
60 MODEL_CONSTANT FloatType y_sx_1
61 = 1. / 2.217737e+00; // Mode 1 S to X yield (mass)
62 MODEL_CONSTANT FloatType y_sx_2 = y_sx_1 / 3.; // Mode 2 S to X yield (mass)
63 MODEL_CONSTANT FloatType y_sa = 0.8; // S to A yield (mass)
64 MODEL_CONSTANT FloatType y_os_molar = 3; // 3 mol o2 per mol for glucose
65
66 MODEL_CONSTANT FloatType tau_1 = 1000.; // s
67 MODEL_CONSTANT FloatType tau_2 = 1000.; // s
68
70 MODEL_CONSTANT FloatType phi_o2_max
71 = 10 * phi_max / MolarMassG * y_os_molar * MolarMassO2; // kgS/s
73
74 MODEL_CONSTANT FloatType k_o
75 = 0.0001; // g/L: Anane et. al 2017 (Biochem. Eng. J) (g/g)
76 MODEL_CONSTANT FloatType k = 1e-2;
77 MODEL_CONSTANT FloatType k_perm = 1e-3;
78 MODEL_CONSTANT FloatType beta = 7;
79 MODEL_CONSTANT FloatType tau_ap_1 = 300;
80 MODEL_CONSTANT FloatType tau_ap_2 = 200;
81 MODEL_CONSTANT FloatType tau_ap_3 = 1000;
82
83 MODEL_CONSTANT auto length_c_dist
85 l_c_m, l_c_m / 2., l_min_m, l_max_m); // use in out_str_l3
86
87 MODEL_CONSTANT FloatType adder_mean = 1.5e-6; // m
88 MODEL_CONSTANT auto adder_dist
91 adder_mean / 2.,
92 adder_mean / 20.,
93 adder_mean * 10); // use in out_str_l3
94 // Constants END
95
97
98 KOKKOS_INLINE_FUNCTION static double
99 mass(std::size_t idx, const SelfParticle& arr)
100 {
101 return GET_PROPERTY(Self::particle_var::length) * lin_density;
102 }
103
104 static std::vector<std::string_view>
106 {
107 return { "age", "length", "nu1", "nu2", "nu_eff_1", "nu_eff_2",
108 "a_p1", "a_p2", "a_p3", "phi_o2", "phi_g", "phi_pts" };
109 }
110
111 static std::vector<std::size_t>
113 {
114 return { INDEX_FROM_ENUM(particle_var::age),
115 INDEX_FROM_ENUM(particle_var::length),
116 INDEX_FROM_ENUM(particle_var::nu1),
117 INDEX_FROM_ENUM(particle_var::nu2),
118 INDEX_FROM_ENUM(particle_var::nu_eff_1),
119 INDEX_FROM_ENUM(particle_var::nu_eff_2),
120 INDEX_FROM_ENUM(Uptakeparticle_var::ap_1),
121 INDEX_FROM_ENUM(Uptakeparticle_var::ap_2),
122 INDEX_FROM_ENUM(Uptakeparticle_var::ap_3),
123 INDEX_FROM_ENUM(particle_var::contrib_phi_o2),
124 INDEX_FROM_ENUM(particle_var::contrib_phi_s),
125 INDEX_FROM_ENUM(particle_var::phi_pts) };
126 }
127
128 static KOKKOS_INLINE_FUNCTION void
130 {
131 Kokkos::printf("[Model]: PRENINIT:BEGIN\r\n");
132 // Kokkos::printf("[Model]: phi_max:%.12f\r\n", phi_pts_max * 1e12);
133 // Kokkos::printf("[Model]: PRENINIT:END\r\n");
134 }
135
136 KOKKOS_INLINE_FUNCTION static void init(const MC::pool_type& random_pool,
137 std::size_t idx,
138 const SelfParticle& arr);
139
140 KOKKOS_INLINE_FUNCTION static MC::Status
141 update(const MC::pool_type& random_pool,
142 FloatType d_t,
143 std::size_t idx,
144 const SelfParticle& arr,
145 const MC::LocalConcentration& c);
146
147 KOKKOS_INLINE_FUNCTION static void
148 division(const MC::pool_type& random_pool,
149 std::size_t idx,
150 std::size_t idx2,
151 const SelfParticle& arr,
152 const SelfParticle& buffer_arr);
153
154 KOKKOS_INLINE_FUNCTION static void
155 contribution(std::size_t idx,
156 std::size_t position,
157 double weight,
158 const SelfParticle& arr,
159 const MC::ContributionView& contributions);
160 };
161
162 CHECK_MODEL(TwoMetaNb)
163
164 KOKKOS_INLINE_FUNCTION void
165 TwoMetaNb::init([[maybe_unused]] const MC::pool_type& random_pool,
166 std::size_t idx,
167 const SelfParticle& arr)
168 {
169
170 // auto& v = init_uptake_cst;
171 constexpr auto local_ac = adder_dist;
172
173 constexpr auto length_dist = MC::Distributions::TruncatedNormal<FloatType>(
174 l_c_m / 2, l_c_m / 5., l_min_m, l_max_m);
175
176 constexpr auto mu_nu_dist = nu_max_kg_s * 0.1;
177 constexpr auto nu_1_initial_dist
179 mu_nu_dist, mu_nu_dist / 7., 0., static_cast<double>(nu_max_kg_s));
180
181 auto gen = random_pool.get_state();
182 auto l = length_dist.draw(gen);
183
184 GET_PROPERTY(Self::particle_var::length) = l;
185 GET_PROPERTY(Self::particle_var::l_cp) = l + local_ac.draw(gen);
186 GET_PROPERTY(particle_var::nu1) = nu_1_initial_dist.draw(gen);
187 random_pool.free_state(gen);
188 GET_PROPERTY(particle_var::contrib_phi_s) = 0;
189
190 Uptake<Self>::init(random_pool, idx, arr);
191 }
192
193 KOKKOS_INLINE_FUNCTION MC::Status
194 TwoMetaNb::update([[maybe_unused]] const MC::pool_type& random_pool,
195 FloatType d_t,
196 std::size_t idx,
197 const SelfParticle& arr,
198 const MC::LocalConcentration& concentrations)
199 {
200 const auto phi_s
202 d_t,
203 idx,
204 arr,
205 concentrations,
206 &GET_PROPERTY(Self::particle_var::phi_pts));
207
208 const auto o = Kokkos::max(static_cast<FloatType>(concentrations(1)), 0.F);
209
210 const auto phi_o2 = (phi_o2_max)*o / (o + k_o); // gO2/s
211
212 const auto nu_1_star
214 * Kokkos::min(phi_s / MolarMassG,
215 phi_o2 / MolarMassO2 / y_os_molar); // gX/s
216
217 const auto s_1_star = (1.F / y_sx_1 * nu_1_star);
218
219 const auto phi_s_residual_1_star = Kokkos::max(phi_s - s_1_star, 0.F);
220 KOKKOS_ASSERT(phi_s_residual_1_star >= 0.F);
221
222 const auto nu_2_star = y_sx_2 * phi_s_residual_1_star; // gX/s
223
224 auto& nu_eff_1 = GET_PROPERTY(Self::particle_var::nu_eff_1);
225 auto& nu_eff_2 = GET_PROPERTY(Self::particle_var::nu_eff_2);
226 auto& nu_1 = GET_PROPERTY(Self::particle_var::nu1);
227 auto& nu_2 = GET_PROPERTY(Self::particle_var::nu2);
228
229 nu_eff_1 = Kokkos::min(nu_1_star, nu_1); // gX/s
230
231 const auto s_1 = (1.F / y_sx_1 * nu_eff_1);
232 const auto phi_s_residual_1 = Kokkos::max(phi_s - s_1, 0.F);
233
234 nu_eff_2 = Kokkos::min(y_sx_2 * phi_s_residual_1,
235 nu_2); // gX/s
236
237 const auto sum_nu = (nu_eff_1 + nu_eff_2);
238
239 const auto s_growth = s_1 + (1 / y_sx_2 * nu_eff_2);
240
241 const auto s_overflow = phi_s - s_growth;
242
243 KOKKOS_ASSERT(nu_eff_1 >= 0.F);
244 KOKKOS_ASSERT(nu_eff_2 >= 0.F);
245
246 // CONTRIBS
247 GET_PROPERTY(Self::particle_var::contrib_phi_s) = -phi_s;
249 = -1.F
251 + 0.F * nu_eff_2);
252
254 = nu_eff_2 / y_sx_2 * y_sa
255 + (s_overflow > 0. ? y_sa * (s_overflow) : 0);
256
257 // ODE
258 nu_1 += d_t * ((nu_1_star - nu_1) / tau_1);
259
260 nu_2 += d_t * ((nu_2_star - nu_2) / tau_2);
261
262 GET_PROPERTY(Self::particle_var::length) += d_t * (sum_nu / lin_density);
263
264 GET_PROPERTY(Self::particle_var::age) += d_t;
265
266 return (GET_PROPERTY(Self::particle_var::length)
267 > GET_PROPERTY(Self::particle_var::l_cp))
270 }
271
272 KOKKOS_INLINE_FUNCTION void
274 std::size_t idx,
275 std::size_t idx2,
276 const SelfParticle& arr,
277 const SelfParticle& child_buffer_arr)
278 {
279 constexpr FloatType half = 0.5;
280 constexpr auto local_ac = adder_dist;
281 const FloatType new_current_length
282 = GET_PROPERTY(particle_var::length) / static_cast<FloatType>(2.);
283
284 GET_PROPERTY(Self::particle_var::length) = new_current_length;
285 GET_PROPERTY(Self::particle_var::age) = 0;
286
287 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::length)
288 = new_current_length;
289
290 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::age) = 0;
291
292 const auto nu_1_o
293 = GET_PROPERTY_FROM(idx, arr, Self::particle_var::nu_eff_1);
294 const auto nu_2_o
295 = GET_PROPERTY_FROM(idx, arr, Self::particle_var::nu_eff_2);
296
297 auto gen = random_pool.get_state();
298
299 GET_PROPERTY(Self::particle_var::l_cp)
300 = new_current_length + local_ac.draw(gen);
301
302 if (nu_1_o != 0)
303 {
304 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::nu1)
306 gen, nu_1_o, nu_1_o * half, 0.F, 1.F);
307 }
308
309 if (nu_2_o != 0)
310 {
311 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::nu2)
313 gen, nu_2_o, nu_2_o * half, 0.F, 1.F);
314 }
315
316 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::l_cp)
317 = new_current_length + local_ac.draw(gen);
318 random_pool.free_state(gen);
319
320 Uptake<Self>::division(random_pool, idx, idx2, arr, child_buffer_arr);
321 }
322
325 {
326 const int begin = INDEX_FROM_ENUM(Self::particle_var::contrib_phi_s);
327 const int end = INDEX_FROM_ENUM(Self::particle_var::contrib_phi_ac);
328 assert(begin < end);
329 return { .begin = begin, .end = end };
330 }
331 // KOKKOS_INLINE_FUNCTION void
332 // TwoMetaNb::contribution([[maybe_unused]] std::size_t idx,
333 // std::size_t position,
334 // double weight,
335 // [[maybe_unused]] const SelfParticle& arr,
336 // const MC::ContributionView& contributions)
337 // {
338 // auto access = contributions.access();
339 // access(0, position) +=
340 // weight * GET_PROPERTY(Self::particle_var::contrib_phi_s); // NOLINT
341 // access(1, position) +=
342 // weight * GET_PROPERTY(Self::particle_var::contrib_phi_o2); // NOLINT
343 // access(2, position) +=
344 // weight * GET_PROPERTY(Self::particle_var::contrib_phi_ac); // NOLINT
345 // }
346
347 static_assert(HasExportProperties<TwoMetaNb>, "ee");
348
349} // namespace Models
350
351#endif //__TWOMETA_MODEL_NB_HPP__
Model that can export properties.
Definition traits.hpp:166
Kokkos::Subview< KernelConcentrationType, int, decltype(Kokkos::ALL)> LocalConcentration
Definition alias.hpp:95
decltype(Kokkos::Experimental::create_scatter_view( kernelContribution())) ContributionView
Definition alias.hpp:88
Status
Definition alias.hpp:58
@ Division
Definition alias.hpp:60
@ Idle
Definition alias.hpp:59
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:39
Kokkos::View< F *[Nd], Kokkos::LayoutRight > ParticlesModel
Definition alias.hpp:19
constexpr T glucose
Definition utils.hpp:29
constexpr T dioxygen
Definition utils.hpp:30
Models definition.
Definition config_loader.hpp:9
KOKKOS_INLINE_FUNCTION consteval F c_linear_density(F rho, F d)
Definition utils.hpp:49
Definition alias.hpp:66
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:354
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F mu, F sigma, F lower, F upper)
Definition prng_extension.hpp:380
Definition two_meta_nb.hpp:16
std::nullopt_t Config
Definition two_meta_nb.hpp:20
MODEL_CONSTANT FloatType y_os_molar
Definition two_meta_nb.hpp:64
MODEL_CONSTANT FloatType y_sa
Definition two_meta_nb.hpp:63
MODEL_CONSTANT FloatType k
Definition two_meta_nb.hpp:76
MODEL_CONSTANT FloatType y_sx_1
Definition two_meta_nb.hpp:61
static std::vector< std::size_t > get_number()
Definition two_meta_nb.hpp:112
MODEL_CONSTANT auto adder_dist
Definition two_meta_nb.hpp:89
std::true_type uniform_weight
Definition two_meta_nb.hpp:17
MODEL_CONSTANT FloatType y_sx_2
Definition two_meta_nb.hpp:62
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition two_meta_nb.hpp:99
static std::vector< std::string_view > names()
Definition two_meta_nb.hpp:105
MC::ParticlesModel< Self::n_var, Self::FloatType > SelfParticle
Definition two_meta_nb.hpp:41
static constexpr std::string_view name
Definition two_meta_nb.hpp:40
MODEL_CONSTANT FloatType tau_ap_2
Definition two_meta_nb.hpp:80
MODEL_CONSTANT FloatType tau_1
Definition two_meta_nb.hpp:66
static KOKKOS_INLINE_FUNCTION MC::Status update(const MC::pool_type &random_pool, FloatType d_t, std::size_t idx, const SelfParticle &arr, const MC::LocalConcentration &c)
Definition two_meta_nb.hpp:194
MODEL_CONSTANT FloatType l_min_m
Definition two_meta_nb.hpp:52
MODEL_CONSTANT FloatType l_c_m
Definition two_meta_nb.hpp:50
static KOKKOS_INLINE_FUNCTION void division(const MC::pool_type &random_pool, std::size_t idx, std::size_t idx2, const SelfParticle &arr, const SelfParticle &buffer_arr)
Definition two_meta_nb.hpp:273
static KOKKOS_INLINE_FUNCTION void preinit()
Definition two_meta_nb.hpp:129
MODEL_CONSTANT FloatType d_m
Definition two_meta_nb.hpp:51
MODEL_CONSTANT FloatType nu_max_kg_s
Definition two_meta_nb.hpp:72
MODEL_CONSTANT FloatType phi_max
Definition two_meta_nb.hpp:69
static KOKKOS_INLINE_FUNCTION void init(const MC::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition two_meta_nb.hpp:165
MODEL_CONSTANT FloatType MolarMassO2
Definition two_meta_nb.hpp:47
MODEL_CONSTANT FloatType phi_o2_max
Definition two_meta_nb.hpp:71
MODEL_CONSTANT FloatType dl_max_ms
Definition two_meta_nb.hpp:55
MODEL_CONSTANT FloatType adder_mean
Definition two_meta_nb.hpp:87
MODEL_CONSTANT FloatType tau_2
Definition two_meta_nb.hpp:67
MODEL_CONSTANT FloatType tau_ap_1
Definition two_meta_nb.hpp:79
particle_var
Definition two_meta_nb.hpp:23
@ nu2
Definition two_meta_nb.hpp:27
@ length
Definition two_meta_nb.hpp:25
@ contrib_phi_o2
Definition two_meta_nb.hpp:34
@ COUNT
Definition two_meta_nb.hpp:36
@ phi_pts
Definition two_meta_nb.hpp:32
@ age
Definition two_meta_nb.hpp:24
@ nu1
Definition two_meta_nb.hpp:26
@ nu_eff_1
Definition two_meta_nb.hpp:29
@ contrib_phi_ac
Definition two_meta_nb.hpp:35
@ l_cp
Definition two_meta_nb.hpp:28
@ nu_eff_2
Definition two_meta_nb.hpp:30
@ contrib_phi_s
Definition two_meta_nb.hpp:33
MODEL_CONSTANT FloatType beta
Definition two_meta_nb.hpp:78
MODEL_CONSTANT FloatType tau_ap_3
Definition two_meta_nb.hpp:81
MODEL_CONSTANT FloatType MolarMassG
Definition two_meta_nb.hpp:45
float FloatType
Definition two_meta_nb.hpp:19
MODEL_CONSTANT FloatType k_o
Definition two_meta_nb.hpp:75
TwoMetaNb Self
Definition two_meta_nb.hpp:18
MODEL_CONSTANT FloatType l_max_m
Definition two_meta_nb.hpp:49
MODEL_CONSTANT auto length_c_dist
Definition two_meta_nb.hpp:84
MODEL_CONSTANT FloatType k_perm
Definition two_meta_nb.hpp:77
static KOKKOS_INLINE_FUNCTION void contribution(std::size_t idx, std::size_t position, double weight, const SelfParticle &arr, const MC::ContributionView &contributions)
MODEL_CONSTANT FloatType lin_density
Definition two_meta_nb.hpp:58
static MC::ContribIndexBounds get_bounds()
Definition two_meta_nb.hpp:324
static constexpr std::size_t n_var
Definition two_meta_nb.hpp:39
static KOKKOS_INLINE_FUNCTION void init(const MC::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition uptake_dyn.hpp:109
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:175
static KOKKOS_INLINE_FUNCTION void division(const MC::pool_type &random_pool, std::size_t idx, std::size_t idx2, const SelfParticle &arr, const SelfParticle &buffer_arr)
Definition uptake_dyn.hpp:223