BioCMAMC-ST
two_meta_div.hpp
1#ifndef __TwoMetaDiv_DIV_MODEL_HPP__
2#define __TwoMetaDiv_DIV_MODEL_HPP__
3
4#include "Kokkos_Printf.hpp"
5#include "common/traits.hpp"
6#include "mc/macros.hpp"
7#include "models/utils.hpp"
8#include <mc/prng/prng_extension.hpp>
9#include <mc/traits.hpp>
10#include <models/uptake.hpp>
11#include <optional>
12#include <string_view>
13
14namespace
15{
16 // template <FloatingPointType F> static F consteval get_phi_s_max(F density, F dl)
17 // {
18 // // dl and density must be same unit, dl*density -> mass and y is mass yield
19 // return (dl * density) * 0.5;
20 // }
21} // namespace
22
23namespace Models
24{
25
27 {
28 struct Yields
29 {
30
31 static constexpr double Y_EG = 12.05; // mol_E/mol_G
32 static constexpr double Y_XG = 1.15; // mol_X/mol_G
33 static constexpr double m = 250; // µmol_G/g_X.h
34 static constexpr double Y_OG = 6.; // mol_O/mol_G
35 static constexpr double Yo_EG = 20.; // mol_E/mol_G
36 static constexpr double Yf_EG = 3.; // mol_E/mol_G
37 static constexpr double Y_AG = 1.5; // mol_A/mol_G (modified for CO2 production)
38 static constexpr double Y_EA = 12.05 / 3.; // mol_E/mol_A
39 static constexpr double Y_XA = 0.4; // mol_X/mol_A
40 static constexpr double Y_OA = 2.; // mol_O/mol_A
41 static constexpr double Yo_EA = 4.67; // mol_E/mol_A
42 };
43
44 using uniform_weight = std::true_type; // Using type alias
46 using FloatType = float;
47
48 enum class particle_var : int
49 {
50 age = INDEX_FROM_ENUM(Uptakeparticle_var::COUNT),
51 length,
52 nu1,
53 nu2,
54 l_cp,
55 nu_eff_1, // This is not itself a model property but stored to be exported
56 nu_eff_2, // This is not itself a model property but stored to be exported
57 // TODO FIND BETTER WAY TO STORE/GET CONTRIBUTIONS
61 COUNT
62 };
63
64 static constexpr std::size_t n_var = INDEX_FROM_ENUM(particle_var::COUNT);
65 static constexpr std::string_view name = "two_meta_div";
67 MODEL_CONSTANT FloatType l_max_m = 5e-6; // m
68 MODEL_CONSTANT FloatType l_c_m = 3e-6; // m
69 MODEL_CONSTANT FloatType d_m = 0.6e-6; // m
70 MODEL_CONSTANT FloatType l_min_m = 0.9e-6; // m
71 MODEL_CONSTANT FloatType lin_density = c_linear_density(static_cast<FloatType>(1000), d_m);
76 (Yields::Y_EG + Yields::Yo_EG); // Mode 1 S to X yield (mass)
77 MODEL_CONSTANT FloatType y_sx_2 =
78 Yields::Y_XG * Yields::Yf_EG / (Yields::Y_EG + Yields::Yf_EG); // Mode 2 S to X yield (mass)
80 MolarMassG; // S to A yield (mass)
81 MODEL_CONSTANT FloatType y_os_molar = 6; // 6 mol o2 per mol for glucose
82 MODEL_CONSTANT FloatType k_o = 0.0001; // g/L: Anane et. al 2017 (Biochem. Eng. J) (g/g)
83 MODEL_CONSTANT FloatType dl_max_ms = 8 * 2e-10; // m/s https://doi.org/10.7554/eLife.67495;
84 MODEL_CONSTANT FloatType tau_1 = 1000.; // s
85 MODEL_CONSTANT FloatType tau_2 = 1000.; // s
87 MODEL_CONSTANT FloatType phi_perm_max = phi_s_max / 40.; // kgS/
88 MODEL_CONSTANT FloatType phi_o2_max =
89 10 * phi_s_max / MolarMassG * y_os_molar * MolarMassO2; // kgS/s
90 MODEL_CONSTANT float nu_max_kg_s = dl_max_ms * lin_density;
91
92 struct TMDConfig
93 {
94 FloatType mu = l_c_m; // Mean
95 FloatType sigma = l_c_m / 2.; // Standard deviation
96 FloatType lower = l_min_m; // Standard deviation
97 FloatType upper = l_max_m; // Standard deviation
98 };
99
101 MODEL_CONSTANT auto length_c_dist =
103 // div1,div2
104
105 // MODEL_CONSTANT auto length_c_dist =
106 // MC::Distributions::TruncatedNormal<FloatType>(l_c_m, l_c_m, l_min_m, l_max_m);
107
108 // MODEL_CONSTANT auto length_c_dist = MC::Distributions::TruncatedNormal<FloatType>(
109 // l_c_m, l_c_m / 2., l_min_m, l_max_m); // use in out_str_l3
110
111 // MODEL_CONSTANT auto length_c_dist =
112 // MC::Distributions::TruncatedNormal<FloatType>(1.5*l_c_m, l_c_m / 7., 3*l_min_m, l_max_m);
113
114 KOKKOS_INLINE_FUNCTION static void init(const MC::KPRNG::pool_type& random_pool,
115 std::size_t idx,
116 const SelfParticle& arr,
117 const Config& config);
118
119 KOKKOS_INLINE_FUNCTION static MC::Status update(const MC::KPRNG::pool_type& random_pool,
120 FloatType d_t,
121 std::size_t idx,
122 const SelfParticle& arr,
123 const MC::LocalConcentration& c);
124
125 KOKKOS_INLINE_FUNCTION static void division(const MC::KPRNG::pool_type& random_pool,
126 std::size_t idx,
127 std::size_t idx2,
128 const SelfParticle& arr,
129 const SelfParticle& buffer_arr);
130
131 KOKKOS_INLINE_FUNCTION static void contribution(std::size_t idx,
132 std::size_t position,
133 double weight,
134 const SelfParticle& arr,
135 const MC::ContributionView& contributions);
136
137 KOKKOS_INLINE_FUNCTION static double mass(std::size_t idx, const SelfParticle& arr)
138 {
139 return GET_PROPERTY(Self::particle_var::length) * lin_density;
140 }
141
142 static std::vector<std::string_view> names()
143 {
144 return {"age", "length", "nu1", "nu2", "nu_eff_1", "nu_eff_2", "a_perm", "a_pts"};
145 }
146
147 static std::vector<std::size_t> get_number()
148 {
149 return {INDEX_FROM_ENUM(particle_var::age),
150 INDEX_FROM_ENUM(particle_var::length),
151 INDEX_FROM_ENUM(particle_var::nu1),
152 INDEX_FROM_ENUM(particle_var::nu2),
153 INDEX_FROM_ENUM(particle_var::nu_eff_1),
154 INDEX_FROM_ENUM(particle_var::nu_eff_2),
155 INDEX_FROM_ENUM(Uptakeparticle_var::a_permease),
156 INDEX_FROM_ENUM(Uptakeparticle_var::a_pts)};
157 }
158
159 KOKKOS_INLINE_FUNCTION static void preinit()
160 {
161 Kokkos::printf("Two Meta Divsion Pre init\r\n");
162 // Self::lenght_init = MC::Distributions::Normal<FloatType>(2e-6, l_c_m / 5.);
163 }
164 };
165
166 CHECK_MODEL(TwoMetaDiv)
167
168 KOKKOS_INLINE_FUNCTION void
169 TwoMetaDiv::init([[maybe_unused]] const MC::KPRNG::pool_type& random_pool,
170 std::size_t idx,
171 const SelfParticle& arr,
172 const Config& config)
173 {
174 auto lc = l_c_m; // MC::Distributions::TruncatedNormal<FloatType>(
175 // config.mu, config.sigma, config.lower, config.upper);
176
177 constexpr auto mu_nu_dist = nu_max_kg_s * 0.1;
178 constexpr auto nu_1_initial_dist = MC::Distributions::TruncatedNormal<float>(
179 mu_nu_dist, mu_nu_dist / 7., 0., static_cast<double>(nu_max_kg_s));
180 auto lenght_init = MC::Distributions::Normal<FloatType>(2e-6, l_c_m / 5.);
181 auto gen = random_pool.get_state();
182 GET_PROPERTY(Self::particle_var::length) = Kokkos::max(0.F, lenght_init.draw(gen));
183 GET_PROPERTY(Self::particle_var::l_cp) = lc;//lc.draw(gen);
184 GET_PROPERTY(particle_var::nu1) = nu_1_initial_dist.draw(gen);
185 random_pool.free_state(gen);
186 GET_PROPERTY(particle_var::contrib_phi_s) = 0;
187 Uptake<Self>::init(random_pool, idx, arr);
188 }
189
190 KOKKOS_INLINE_FUNCTION MC::Status TwoMetaDiv::update(const MC::KPRNG::pool_type& random_pool,
191 FloatType d_t,
192 std::size_t idx,
193 const SelfParticle& arr,
194 const MC::LocalConcentration& concentrations)
195 {
196
197
198 (void)random_pool;
199
200 const auto phi_s =
201 Uptake<Self>::uptake_step(phi_s_max, phi_perm_max, d_t, idx, arr, concentrations);
202
203 const auto o = Kokkos::max(static_cast<float>(concentrations(1)), 0.F);
204
205 const float phi_o2 = (phi_o2_max)*o / (o + k_o); // gO2/s
206
207 const float nu_1_star = y_sx_1 * MolarMassG *
208 Kokkos::min(phi_s / MolarMassG,
209 phi_o2 / MolarMassO2 / y_os_molar); // gX/s
210
211 const float s_1_star = (1 / y_sx_1 * nu_1_star);
212
213 const float phi_s_residual_1_star = Kokkos::max(phi_s - s_1_star, 0.F);
214 KOKKOS_ASSERT(phi_s_residual_1_star >= 0.F);
215
216 const float nu_2_star = y_sx_2 * phi_s_residual_1_star; // gX/s
217
218 GET_PROPERTY(Self::particle_var::nu_eff_1) =
219 Kokkos::min(nu_1_star, GET_PROPERTY(Self::particle_var::nu1)); // gX/s
220
221 const float s_1 = (1 / y_sx_1 * GET_PROPERTY(Self::particle_var::nu_eff_1));
222 const float phi_s_residual_1 = Kokkos::max(phi_s - s_1, 0.F);
223 GET_PROPERTY(Self::particle_var::nu_eff_2) =
224 Kokkos::min(y_sx_2 * phi_s_residual_1, GET_PROPERTY(Self::particle_var::nu2)); // gX/s
225
226 const float s_growth = s_1 + (1 / y_sx_2 * GET_PROPERTY(Self::particle_var::nu_eff_2));
227
228 const float s_overflow = phi_s - s_growth;
229
230 KOKKOS_ASSERT(GET_PROPERTY(Self::particle_var::nu_eff_1) >= 0.F);
231 KOKKOS_ASSERT(GET_PROPERTY(Self::particle_var::nu_eff_2) >= 0.F);
232
233 // CONTRIBS
234 GET_PROPERTY(Self::particle_var::contrib_phi_s) = -phi_s;
235
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) * ((nu_1_star - GET_PROPERTY(Self::particle_var::nu1)) / tau_1);
248
249 GET_PROPERTY(Self::particle_var::nu2) +=
250 static_cast<float>(d_t) * ((nu_2_star - GET_PROPERTY(Self::particle_var::nu2)) / tau_2);
251
252 const auto sum_nu =
253 (GET_PROPERTY(Self::particle_var::nu_eff_1) + GET_PROPERTY(Self::particle_var::nu_eff_2));
254
255 GET_PROPERTY(Self::particle_var::length) += static_cast<float>(d_t) * (sum_nu / lin_density);
256
257 GET_PROPERTY(Self::particle_var::age) += d_t;
258
259 return (GET_PROPERTY(Self::particle_var::length) > GET_PROPERTY(Self::particle_var::l_cp))
262 }
263
264 KOKKOS_INLINE_FUNCTION void TwoMetaDiv::division(const MC::KPRNG::pool_type& random_pool,
265 std::size_t idx,
266 std::size_t idx2,
267 const SelfParticle& arr,
268 const SelfParticle& child_buffer_arr)
269 {
270 constexpr auto local_lc = length_c_dist;
271 const FloatType new_current_length =
272 GET_PROPERTY(particle_var::length) / static_cast<FloatType>(2.);
273
274 GET_PROPERTY(Self::particle_var::length) = new_current_length;
275 GET_PROPERTY(Self::particle_var::age) = 0;
276
277 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::length) = new_current_length;
278 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::age) = 0;
279
280 auto nu_1_o = GET_PROPERTY_FROM(idx, arr, Self::particle_var::nu_eff_1);
281 auto nu_2_o = GET_PROPERTY_FROM(idx, arr, Self::particle_var::nu_eff_2);
282 auto gen = random_pool.get_state();
283
284 if (nu_1_o != 0)
285 {
286 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::nu1) =
287 MC::Distributions::TruncatedNormal<FloatType>::draw_from(gen, nu_1_o, nu_1_o / 2., 0, 1.);
288 }
289 if (nu_2_o != 0)
290 {
291 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::nu2) =
292 MC::Distributions::TruncatedNormal<FloatType>::draw_from(gen, nu_2_o, nu_2_o / 2., 0, 1.);
293 }
294 GET_PROPERTY(Self::particle_var::l_cp) = l_c_m;//local_lc.draw(gen);
295 GET_PROPERTY_FROM(idx2, child_buffer_arr, Self::particle_var::l_cp) = l_c_m;//local_lc.draw(gen);
296 random_pool.free_state(gen);
297
298 Uptake<Self>::division(random_pool, idx, idx2, arr, child_buffer_arr);
299 }
300
301 KOKKOS_INLINE_FUNCTION void TwoMetaDiv::contribution([[maybe_unused]] std::size_t idx,
302 std::size_t position,
303 double weight,
304 [[maybe_unused]] const SelfParticle& arr,
305 const MC::ContributionView& contributions)
306 {
307 auto access = contributions.access();
308 access(0, position) += weight * GET_PROPERTY(Self::particle_var::contrib_phi_s); // NOLINT
309 access(1, position) += weight * GET_PROPERTY(Self::particle_var::contrib_phi_o2); // NOLINT
310 access(2, position) += weight * GET_PROPERTY(Self::particle_var::contrib_phi_ac); // NOLINT
311 }
312
313 static_assert(HasExportProperties<TwoMetaDiv>, "ee");
314
315} // namespace Models
316
317#endif
Kokkos::Random_XorShift1024_Pool< Kokkos::DefaultExecutionSpace > pool_type
Definition prng.hpp:17
Model that can export properties.
Definition traits.hpp:159
Kokkos::Subview< KernelConcentrationType, int, decltype(Kokkos::ALL)> LocalConcentration
Definition alias.hpp:42
Kokkos::Experimental::ScatterView< double **, Kokkos::LayoutRight > ContributionView
Definition alias.hpp:31
Kokkos::View< F *[Nd]> ParticlesModel
Definition traits.hpp:34
Status
Definition alias.hpp:11
constexpr T acetate
Definition utils.hpp:23
constexpr T glucose
Definition utils.hpp:21
constexpr T dioxygen
Definition utils.hpp:22
constexpr double X
Definition utils.hpp:36
constexpr double glucose
Definition utils.hpp:29
Models definition.
Definition config_loader.hpp:8
@ a_permease
Definition uptake.hpp:15
@ a_pts
Definition uptake.hpp:14
@ COUNT
Definition uptake.hpp:17
KOKKOS_INLINE_FUNCTION consteval F c_linear_density(F rho, F d)
Definition utils.hpp:39
Represents a normal (Gaussian) probability distribution.
Definition prng_extension.hpp:184
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:263
static KOKKOS_INLINE_FUNCTION F draw_from(Kokkos::Random_XorShift1024< DeviceType > &gen, F mu, F sigma, F lower, F upper)
Definition prng_extension.hpp:287
Definition two_meta_div.hpp:93
FloatType sigma
Definition two_meta_div.hpp:95
FloatType mu
Definition two_meta_div.hpp:94
FloatType upper
Definition two_meta_div.hpp:97
FloatType lower
Definition two_meta_div.hpp:96
Definition two_meta_div.hpp:29
static constexpr double Y_EA
Definition two_meta_div.hpp:38
static constexpr double Y_XA
Definition two_meta_div.hpp:39
static constexpr double Y_OG
Definition two_meta_div.hpp:34
static constexpr double Yo_EA
Definition two_meta_div.hpp:41
static constexpr double Y_EG
Definition two_meta_div.hpp:31
static constexpr double Yo_EG
Definition two_meta_div.hpp:35
static constexpr double Yf_EG
Definition two_meta_div.hpp:36
static constexpr double m
Definition two_meta_div.hpp:33
static constexpr double Y_AG
Definition two_meta_div.hpp:37
static constexpr double Y_XG
Definition two_meta_div.hpp:32
static constexpr double Y_OA
Definition two_meta_div.hpp:40
Definition two_meta_div.hpp:27
MODEL_CONSTANT float nu_max_kg_s
Definition two_meta_div.hpp:90
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_div.hpp:264
static std::vector< std::string_view > names()
Definition two_meta_div.hpp:142
MODEL_CONSTANT FloatType y_sa
Definition two_meta_div.hpp:79
MODEL_CONSTANT FloatType phi_o2_max
Definition two_meta_div.hpp:88
MODEL_CONSTANT FloatType d_m
Definition two_meta_div.hpp:69
MODEL_CONSTANT FloatType l_min_m
Definition two_meta_div.hpp:70
MODEL_CONSTANT FloatType k_o
Definition two_meta_div.hpp:82
MODEL_CONSTANT FloatType y_sx_1
Definition two_meta_div.hpp:74
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_div.hpp:190
MC::ParticlesModel< Self::n_var, Self::FloatType > SelfParticle
Definition two_meta_div.hpp:66
MODEL_CONSTANT FloatType y_sx_2
Definition two_meta_div.hpp:77
static std::vector< std::size_t > get_number()
Definition two_meta_div.hpp:147
MODEL_CONSTANT FloatType MolarMassG
Definition two_meta_div.hpp:72
MODEL_CONSTANT FloatType tau_2
Definition two_meta_div.hpp:85
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_div.hpp:301
MODEL_CONSTANT FloatType lin_density
Definition two_meta_div.hpp:71
MODEL_CONSTANT FloatType phi_s_max
Definition two_meta_div.hpp:86
float FloatType
Definition two_meta_div.hpp:46
static constexpr std::size_t n_var
Definition two_meta_div.hpp:64
MODEL_CONSTANT FloatType l_c_m
Definition two_meta_div.hpp:68
static constexpr std::string_view name
Definition two_meta_div.hpp:65
MODEL_CONSTANT FloatType phi_perm_max
Definition two_meta_div.hpp:87
MODEL_CONSTANT FloatType y_os_molar
Definition two_meta_div.hpp:81
MODEL_CONSTANT FloatType tau_1
Definition two_meta_div.hpp:84
MODEL_CONSTANT FloatType dl_max_ms
Definition two_meta_div.hpp:83
static KOKKOS_INLINE_FUNCTION void preinit()
Definition two_meta_div.hpp:159
particle_var
Definition two_meta_div.hpp:49
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition two_meta_div.hpp:137
MODEL_CONSTANT FloatType MolarMassO2
Definition two_meta_div.hpp:73
MODEL_CONSTANT FloatType l_max_m
Definition two_meta_div.hpp:67
MODEL_CONSTANT auto length_c_dist
Definition two_meta_div.hpp:101
static KOKKOS_INLINE_FUNCTION void init(const MC::KPRNG::pool_type &random_pool, std::size_t idx, const SelfParticle &arr, const Config &config)
Definition two_meta_div.hpp:169
std::true_type uniform_weight
Definition two_meta_div.hpp:44
static KOKKOS_INLINE_FUNCTION void init(const MC::KPRNG::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition uptake.hpp:68
static KOKKOS_INLINE_FUNCTION FloatType uptake_step(FloatType phi_pts_max, FloatType phi_permease_specific, FloatType d_t, std::size_t idx, const SelfParticle &arr, const MC::LocalConcentration &c)
Definition uptake.hpp:105
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.hpp:174