BioCMAMC-ST
monod.hpp
1#ifndef __MODEL_MONOD_HPP__
2#define __MODEL_MONOD_HPP__
3
4#include "mc/macros.hpp"
5#include "models/utils.hpp"
6#include <Kokkos_MathematicalConstants.hpp>
7#include <mc/alias.hpp>
8#include <mc/prng/prng_extension.hpp>
9#include <mc/traits.hpp>
10#include <optional>
11
12namespace Models
13{
14
26 struct Monod
27 {
48 // Monod model consumes only glucose
49 MODEL_CONSTANT std::size_t n_c = 1;
50 // clang-format off
51 static constexpr std::size_t n_var = INDEX_FROM_ENUM(particle_var::__COUNT__);
52 static constexpr std::string_view name = "monod";
53 using uniform_weight = std::true_type; // Using type alias
54 using Self = Monod;
55 using FloatType = float;
56
59 using Config = std::nullopt_t;
60
61
68 MODEL_CONSTANT FloatType y_s_x = 2;
69 MODEL_CONSTANT FloatType mu_max = 0.77 / 3600.;
70 MODEL_CONSTANT FloatType tau_meta = 1. / mu_max;
71 MODEL_CONSTANT FloatType l_max_m = 2e-6;
72 MODEL_CONSTANT FloatType l_min_m = l_max_m / 2.;
73 MODEL_CONSTANT FloatType k_s = 1e-3;
74 MODEL_CONSTANT FloatType d_m = 0.6e-6;
75 MODEL_CONSTANT FloatType lin_density =
76 c_linear_density(static_cast<FloatType>(1000), d_m);
77 // clang-format on
78
79 MODEL_CONSTANT auto initial_length_dist
81 l_max_m * 0.75, l_max_m * 0.75 / 4, l_min_m, l_max_m);
82
83 KOKKOS_INLINE_FUNCTION static void
84 init([[maybe_unused]] const MC::pool_type& random_pool,
85 [[maybe_unused]] std::size_t idx,
86 [[maybe_unused]] const SelfParticle& arr)
87 {
88 // Helper cause, initial_length_dist may not be captured in cuda context
89 // (FIXME)
90 static constexpr auto local_dist = initial_length_dist;
91
92 const auto [l0, mu] = MC::sample_random_variables(
93 random_pool,
94 [](auto& gen)
95 {
96 const auto l0 = local_dist.draw(
97 gen); // Get initial fro given distribution (normal)
98 const auto mu = mu_max; // Set maximal for each cell mu_p
99 return std::make_tuple(l0, mu);
100 });
101
102 GET_PROPERTY(particle_var::l) = l0;
103 GET_PROPERTY(particle_var::l_max)
104 = l_max_m; // Set the same length for everyone
105 GET_PROPERTY(particle_var::mu_p) = mu_max;
106
107 // born at l_max/2, divides at l_max
108 // Need to be changed if division criteria is changed
109 constexpr auto dl = l_max_m / 2.;
110
111 // Monod factor to convert growth rate into elongation, it assumes
112 // doubling in mass during generation time G=ln(2)/mu factor= DL/ln(2)
113 // comes from mu=ln(2)/G
115 = dl / Kokkos::numbers::ln2;
116 }
117
118 KOKKOS_INLINE_FUNCTION static double
119 mass([[maybe_unused]] std::size_t idx,
120 [[maybe_unused]] const SelfParticle& arr)
121 {
122 return GET_PROPERTY(Self::particle_var::l) * lin_density; // Rod-shaped
123 }
124
125 KOKKOS_INLINE_FUNCTION static MC::Status
126 update([[maybe_unused]] const MC::pool_type& random_pool,
127 FloatType d_t,
128 std::size_t idx,
129 const SelfParticle& arr,
130 const SelfContribs& arr_contribs,
131 const std::size_t position_index,
132 const MC::LocalConcentration& c)
133 {
134 constexpr FloatType inv_tau = 1.0 / tau_meta;
135
136 const auto s = GET_CLAMPED_CONCENTRATION_CAST(FloatType, 0);
137
138 auto& l = GET_PROPERTY(Self::particle_var::l);
139 auto& mu_p = GET_PROPERTY(Self::particle_var::mu_p);
140
141 // Instantaneous mu from Monod
142 const FloatType mu = mu_max * s / (k_s + s);
143
144 // Efffective growth rate
145 const FloatType mu_eff = Kokkos::min(mu_p, mu);
146 // Store only for being exported in 1/h
147 GET_PROPERTY(Self::particle_var::mue) = mu_eff * 3600.;
148
149 // Contributions
150 const auto phi_s
151 = -mu_eff * y_s_x * static_cast<FloatType>(mass(idx, arr));
152
153 GET_PROPERTY(Self::particle_var::phi_s_c) = phi_s;
154 GET_CONTRIBS(0) = phi_s;
155
156 // ODE
157
158 // Lengthening
159 l += d_t
160 * (mu_eff
162
163 // Growth rate
164 mu_p += d_t * inv_tau * (mu - mu_p);
165
166 return check_div(l, GET_PROPERTY(Self::particle_var::l_max));
167 }
168
169 KOKKOS_INLINE_FUNCTION static void
170 division([[maybe_unused]] const MC::pool_type& random_pool,
171 [[maybe_unused]] std::size_t idx,
172 [[maybe_unused]] std::size_t idx2,
173 [[maybe_unused]] const SelfParticle& arr,
174 [[maybe_unused]] const SelfParticle& buffer_arr)
175 {
176
177 //_init_only_cell_lenghtening doesnt need to be redistributed bcause
178 // division criteria is the same for each cell then dl remains the same
179
180 // Even if division is deterministic, keep generate way to divide
181
185
186 const FloatType current_l = GET_PROPERTY(particle_var::l);
187 const FloatType new_current_length = current_l / 2.F;
188 GET_PROPERTY(particle_var::l) = new_current_length;
189
190 const FloatType mu_p_value = GET_PROPERTY(particle_var::mu_p);
191 const FloatType cell_lenghtening_value
193
194 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l) = new_current_length;
195 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l_max) = l_max_m;
196 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::mu_p) = mu_p_value;
197 GET_PROPERTY_FROM(
199 = cell_lenghtening_value;
200 }
201
202 static std::vector<std::string_view>
204 {
205 return { "length", "mu", "mu_eff" };
206 }
207
208 static std::vector<std::size_t>
210 {
211 return { INDEX_FROM_ENUM(particle_var::l),
212 INDEX_FROM_ENUM(particle_var::mu_p),
213 INDEX_FROM_ENUM(particle_var::mue) };
214 }
215
216 static std::vector<std::string_view>
218 {
219 return { "S" };
220 }
221 };
222
223 CHECK_MODEL(Monod)
224} // namespace Models
225#endif
KOKKOS_INLINE_FUNCTION auto sample_random_variables(const pool_type &pool, auto &&functor)
Definition prng.hpp:48
Status
Definition alias.hpp:125
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:100
KernelConcentrationType LocalConcentration
Definition alias.hpp:170
Kokkos::View< F *[Nc], ComputeSpace::array_layout, ComputeSpace, Kokkos::MemoryTraits< Kokkos::MemoryTraitsFlags::Restrict > > ParticlesContribs
Definition alias.hpp:66
Kokkos::View< F *[Nd], ComputeSpace::array_layout, ComputeSpace, Kokkos::MemoryTraits< Kokkos::MemoryTraitsFlags::Restrict > > ParticlesModel
Definition alias.hpp:52
Models definition.
Definition acetate.hpp:18
KOKKOS_INLINE_FUNCTION MC::Status check_div(const T l, const T lc)
Definition utils.hpp:64
KOKKOS_INLINE_FUNCTION consteval F c_linear_density(F rho, F d)
Definition utils.hpp:94
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:354
Simplified Monod model for glucose consumption and biomass growth.
Definition monod.hpp:27
MODEL_CONSTANT FloatType tau_meta
Metabolic time constant (s), inverse of max growth rate.
Definition monod.hpp:70
static std::vector< std::string_view > species()
Definition monod.hpp:217
MODEL_CONSTANT auto initial_length_dist
Definition monod.hpp:80
static constexpr std::string_view name
Definition monod.hpp:52
MC::ParticlesContribs< Self::n_c, Self::FloatType > SelfContribs
Definition monod.hpp:58
MODEL_CONSTANT FloatType d_m
Cell diameter (m)
Definition monod.hpp:74
static KOKKOS_INLINE_FUNCTION MC::Status update(const MC::pool_type &random_pool, FloatType d_t, std::size_t idx, const SelfParticle &arr, const SelfContribs &arr_contribs, const std::size_t position_index, const MC::LocalConcentration &c)
Definition monod.hpp:126
static constexpr std::size_t n_var
Definition monod.hpp:51
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition monod.hpp:119
float FloatType
Definition monod.hpp:55
static std::vector< std::size_t > get_number()
Definition monod.hpp:209
static KOKKOS_INLINE_FUNCTION void init(const MC::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition monod.hpp:84
MODEL_CONSTANT FloatType l_min_m
Minimum cell length (m), half of maximum length.
Definition monod.hpp:72
particle_var
Enumeration for the Monod model variables.
Definition monod.hpp:38
@ mu_p
Maximum specific growth rate (potential growth rate)
Definition monod.hpp:41
@ mue
Effective growth rate (used for export purposes only)
Definition monod.hpp:42
@ l
Length of the cell.
Definition monod.hpp:39
@ phi_s_c
Instantaneous glucose consumption rate.
Definition monod.hpp:45
@ l_max
Maximum cell length (assumed constant for all cells)
Definition monod.hpp:40
@ __COUNT__
Helper for determining the size of the variable list.
Definition monod.hpp:46
@ _init_only_cell_lenghtening
Definition monod.hpp:43
MODEL_CONSTANT FloatType lin_density
Linear density of the biomass (kg/m), calculated from cell diameter.
Definition monod.hpp:75
std::nullopt_t Config
Definition monod.hpp:59
MODEL_CONSTANT FloatType l_max_m
Maximum cell length (m)
Definition monod.hpp:71
MODEL_CONSTANT std::size_t n_c
Definition monod.hpp:49
Monod Self
Definition monod.hpp:54
static std::vector< std::string_view > names()
Definition monod.hpp:203
MC::ParticlesModel< Self::n_var, Self::FloatType > SelfParticle
Definition monod.hpp:57
MODEL_CONSTANT FloatType k_s
Monod constant for substrate concentration (m)
Definition monod.hpp:73
MODEL_CONSTANT FloatType mu_max
Maximum specific growth rate (1/s), converted from per hour.
Definition monod.hpp:69
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 monod.hpp:170
MODEL_CONSTANT FloatType y_s_x
Model constants used in biomass growth and cell elongation.
Definition monod.hpp:68
std::true_type uniform_weight
Definition monod.hpp:53