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