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
49 // clang-format off
50 static constexpr std::size_t n_var = INDEX_FROM_ENUM(particle_var::__COUNT__);
51 static constexpr std::string_view name = "monod";
52 using uniform_weight = std::true_type; // Using type alias
53 using Self = Monod;
54 using FloatType = float;
56 using Config = std::nullopt_t;
57
58
65 MODEL_CONSTANT FloatType y_s_x = 2;
66 MODEL_CONSTANT FloatType mu_max = 0.77 / 3600.;
67 MODEL_CONSTANT FloatType tau_meta = 1. / mu_max;
68 MODEL_CONSTANT FloatType l_max_m = 2e-6;
69 MODEL_CONSTANT FloatType l_min_m = l_max_m / 2.;
70 MODEL_CONSTANT FloatType k_s = 1e-3;
71 MODEL_CONSTANT FloatType d_m = 0.6e-6;
72 MODEL_CONSTANT FloatType lin_density =
73 c_linear_density(static_cast<FloatType>(1000), d_m);
74 // clang-format on
75
76 MODEL_CONSTANT auto initial_length_dist
78 l_max_m * 0.75, l_max_m * 0.75 / 4, l_min_m, l_max_m);
79
80 KOKKOS_INLINE_FUNCTION static void
81 init([[maybe_unused]] const MC::pool_type& random_pool,
82 [[maybe_unused]] std::size_t idx,
83 [[maybe_unused]] const SelfParticle& arr)
84 {
85 // Helper cause, initial_length_dist may not be captured in cuda context
86 // (FIXME)
87 static constexpr auto local_dist = initial_length_dist;
88
89 const auto [l0, mu] = MC::sample_random_variables(
90 random_pool,
91 [](auto& gen)
92 {
93 const auto l0 = local_dist.draw(
94 gen); // Get initial fro given distribution (normal)
95 const auto mu = mu_max; // Set maximal for each cell mu_p
96 return std::make_tuple(l0, mu);
97 });
98
99 GET_PROPERTY(particle_var::l) = l0;
100 GET_PROPERTY(particle_var::l_max)
101 = l_max_m; // Set the same length for everyone
102 GET_PROPERTY(particle_var::mu_p) = mu_max;
103
104 // born at l_max/2, divides at l_max
105 // Need to be changed if division criteria is changed
106 constexpr auto dl = l_max_m / 2.;
107
108 // Monod factor to convert growth rate into elongation, it assumes
109 // doubling in mass during generation time G=ln(2)/mu factor= DL/ln(2)
110 // comes from mu=ln(2)/G
112 = dl / Kokkos::numbers::ln2;
113 }
114
115 KOKKOS_INLINE_FUNCTION static double
116 mass([[maybe_unused]] std::size_t idx,
117 [[maybe_unused]] const SelfParticle& arr)
118 {
119 return GET_PROPERTY(Self::particle_var::l) * lin_density; // Rod-shaped
120 }
121
122 KOKKOS_INLINE_FUNCTION static MC::Status
123 update([[maybe_unused]] const MC::pool_type& random_pool,
124 [[maybe_unused]] FloatType d_t,
125 [[maybe_unused]] std::size_t idx,
126 [[maybe_unused]] const SelfParticle& arr,
127 [[maybe_unused]] const MC::LocalConcentration& c)
128 {
129 const FloatType s
130 = static_cast<FloatType>(Kokkos::max(0., c(0))); // Bounded
131 const FloatType mu = mu_max * s / (k_s + s); // Instantaneous mu from
132 // Monod
133
134 // Efffective growth rate
135 const FloatType mu_eff
136 = Kokkos::min(GET_PROPERTY(Self::particle_var::mu_p), mu);
137
138 // Lengthening
139 GET_PROPERTY(Self::particle_var::l)
140 += d_t
141 * (mu_eff
142 * GET_PROPERTY(
144
145 // Growth rate
146 GET_PROPERTY(Self::particle_var::mu_p)
147 += d_t * (1.0 / tau_meta)
148 * (mu - GET_PROPERTY(Self::particle_var::mu_p));
149
150 // Store only for being exported
151 GET_PROPERTY(Self::particle_var::mue) = mu_eff;
152
153 // Contributions
154 GET_PROPERTY(Self::particle_var::phi_s_c)
155 = -mu_eff * y_s_x * static_cast<FloatType>(mass(idx, arr));
156
157 return check_div(GET_PROPERTY(Self::particle_var::l),
158 GET_PROPERTY(Self::particle_var::l_max));
159 }
160
161 KOKKOS_INLINE_FUNCTION static void
162 division([[maybe_unused]] const MC::pool_type& random_pool,
163 [[maybe_unused]] std::size_t idx,
164 [[maybe_unused]] std::size_t idx2,
165 [[maybe_unused]] const SelfParticle& arr,
166 [[maybe_unused]] const SelfParticle& buffer_arr)
167 {
168
169 //_init_only_cell_lenghtening doesnt need to be redistributed bcause
170 // division criteria is the same for each cell then dl remains the same
171
172 // Even if division is deterministic, keep generate way to divide
173
177
178 const FloatType current_l = GET_PROPERTY(particle_var::l);
179 const FloatType new_current_length = current_l / 2.F;
180 GET_PROPERTY(particle_var::l) = new_current_length;
181
182 const FloatType mu_p_value = GET_PROPERTY(particle_var::mu_p);
183 const FloatType cell_lenghtening_value
185
186 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l) = new_current_length;
187 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l_max) = l_max_m;
188 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::mu_p) = mu_p_value;
189 GET_PROPERTY_FROM(
191 = cell_lenghtening_value;
192 }
193
196 {
197 int begin = INDEX_FROM_ENUM(Self::particle_var::phi_s_c);
198 return { .begin = begin, .end = begin + 1 };
199 }
200
201 static std::vector<std::string_view>
203 {
204 return { "length", "mu", "mu_eff" };
205 }
206
207 static std::vector<std::size_t>
209 {
210 return { INDEX_FROM_ENUM(particle_var::l),
211 INDEX_FROM_ENUM(particle_var::mu_p),
212 INDEX_FROM_ENUM(particle_var::mue) };
213 }
214 };
215
216 CHECK_MODEL(Monod)
217} // namespace Models
218#endif
Kokkos::Subview< KernelConcentrationType, int, decltype(Kokkos::ALL)> LocalConcentration
Definition alias.hpp:105
KOKKOS_INLINE_FUNCTION auto sample_random_variables(const pool_type &pool, auto &&functor)
Definition prng.hpp:45
Status
Definition alias.hpp:58
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:39
Kokkos::View< F *[Nd], Kokkos::LayoutRight > ParticlesModel
Definition alias.hpp:19
Models definition.
Definition config_loader.hpp:9
KOKKOS_INLINE_FUNCTION MC::Status check_div(const T l, const T lc)
Definition utils.hpp:19
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:362
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:67
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 monod.hpp:123
MODEL_CONSTANT auto initial_length_dist
Definition monod.hpp:77
static constexpr std::string_view name
Definition monod.hpp:51
static MC::ContribIndexBounds get_bounds()
Definition monod.hpp:195
MODEL_CONSTANT FloatType d_m
Cell diameter (m)
Definition monod.hpp:71
static constexpr std::size_t n_var
Definition monod.hpp:50
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition monod.hpp:116
float FloatType
Definition monod.hpp:54
static std::vector< std::size_t > get_number()
Definition monod.hpp:208
static KOKKOS_INLINE_FUNCTION void init(const MC::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition monod.hpp:81
MODEL_CONSTANT FloatType l_min_m
Minimum cell length (m), half of maximum length.
Definition monod.hpp:69
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:72
std::nullopt_t Config
Definition monod.hpp:56
MODEL_CONSTANT FloatType l_max_m
Maximum cell length (m)
Definition monod.hpp:68
Monod Self
Definition monod.hpp:53
static std::vector< std::string_view > names()
Definition monod.hpp:202
MC::ParticlesModel< Self::n_var, Self::FloatType > SelfParticle
Definition monod.hpp:55
MODEL_CONSTANT FloatType k_s
Monod constant for substrate concentration (m)
Definition monod.hpp:70
MODEL_CONSTANT FloatType mu_max
Maximum specific growth rate (1/s), converted from per hour.
Definition monod.hpp:66
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:162
MODEL_CONSTANT FloatType y_s_x
Model constants used in biomass growth and cell elongation.
Definition monod.hpp:65
std::true_type uniform_weight
Definition monod.hpp:52