BioCMAMC-ST
simple_acetate.hpp
1
2#ifndef __SIMPLE_ACETATE_MODEL_HPP__
3#define __SIMPLE_ACETATE_MODEL_HPP__
4
5#include "Kokkos_Assert.hpp"
6#include "Kokkos_Core_fwd.hpp"
7#include "Kokkos_Macros.hpp"
8#include "common/common.hpp"
9#include "common/traits.hpp"
10#include "mc/macros.hpp"
11#include "models/utils.hpp"
12#include <mc/prng/prng_extension.hpp>
13#include <mc/traits.hpp>
14#include <optional>
15#include <string_view>
16
17namespace Models
18{
19 // template <FloatingPointType F>
20 // static F consteval
21 // _get_phi_s_max(F density, F dl, F glucose_to_biomass_yield = 0.5)
22 // {
23 // // dl and density must be same unit, dl*density -> mass and y is mass
24 // yield return (dl * density) / glucose_to_biomass_yield;
25 // }
27 {
28 using uniform_weight = std::true_type;
30 using FloatType = float;
31 using Config = std::nullopt_t;
32
33 enum class particle_var : int // NOLINT
34 {
35 length = 0,
39 // Export
43 // Con
47 };
48
49 static constexpr std::size_t n_var
50 = INDEX_FROM_ENUM(particle_var::__COUNT__);
51
52 static constexpr std::string_view name = "simple_acetate";
54
55 static constexpr std::size_t n_c = 2;
56
58
59 MODEL_CONSTANT std::size_t N_N = 2; // Number of species
60 MODEL_CONSTANT FloatType a_max_m = 2e-6 / 3600.; // m
61 MODEL_CONSTANT FloatType l_max_m = 2e-6; // m
62 MODEL_CONSTANT FloatType l_min_m = l_max_m / 2.; // m
63 // MODEL_CONSTANT FloatType k = 1e-3; // m
64 MODEL_CONSTANT FloatType d_m = 0.6e-6; // m
65 MODEL_CONSTANT FloatType lin_density
66 = c_linear_density(static_cast<FloatType>(1000), d_m);
67
68 MODEL_CONSTANT FloatType k_s = 1e-3;
69 MODEL_CONSTANT FloatType k_a = 1e-4;
70 MODEL_CONSTANT Kokkos::Array<FloatType, N_N> k = { k_s, k_a };
71
72 MODEL_CONSTANT FloatType y_s = 2;
73 MODEL_CONSTANT FloatType y_a = 3;
74 MODEL_CONSTANT Kokkos::Array<FloatType, N_N> y = { y_s, y_a };
75
76 MODEL_CONSTANT FloatType phi_s_max
78
79 MODEL_CONSTANT auto l_max_dist
81 l_max_m, l_max_m / 10., l_max_m * 0.7, 1.3 * l_max_m);
82
84 l_max_m * 0.75, l_max_m / 10., 0.7 * l_min_m, l_max_m * 1.3);
85
86 // static Self::Config get_config(std::size_t n);
87
88 KOKKOS_INLINE_FUNCTION static void init(const MC::pool_type& random_pool,
89 std::size_t idx,
90 const SelfParticle& arr);
91
92 KOKKOS_INLINE_FUNCTION static MC::Status
93 update([[maybe_unused]] const MC::pool_type& random_pool,
94 FloatType d_t,
95 std::size_t idx,
96 const SelfParticle& arr,
97 const SelfContribs& arr_contribs,
98 std::size_t position_index,
99 const MC::LocalConcentration& c);
100
101 KOKKOS_INLINE_FUNCTION static void
102 division(const MC::pool_type& random_pool,
103 std::size_t idx,
104 std::size_t idx2,
105 const SelfParticle& arr,
106 const SelfParticle& buffer_arr);
107
108 KOKKOS_INLINE_FUNCTION static double
109 mass(std::size_t idx, const SelfParticle& arr)
110 {
111 return GET_PROPERTY(Self::particle_var::length) * lin_density;
112 }
113
114 static std::array<std::string_view, Self::n_var>
116 {
117 return {
118 "length", "l_max", "a_p", "a_max", "a_e",
119 "a_e_s", "a_e_a", "phi_s", "phi_a",
120 };
121 }
122
123 // static std::vector<std::size_t>
124 // get_number()
125 // {
126 // return { INDEX_FROM_ENUM(particle_var::length) };
127 // }
128 };
129
130 CHECK_MODEL(SimpleAcetate)
131
132 KOKKOS_INLINE_FUNCTION void
133 SimpleAcetate::init([[maybe_unused]] const MC::pool_type& random_pool,
134 std::size_t idx,
135 const SelfParticle& arr)
136 {
137
138 MODEL_CONSTANT auto a_max_dist
140 a_max_m, a_max_m / 2., 0.5 * a_max_m, a_max_m * 1.5);
141
142 static constexpr auto ld = l_dist;
143 static constexpr auto lm = l_dist;
144 auto gen = random_pool.get_state();
145 GET_PROPERTY(particle_var::length) = ld.draw(gen);
146 GET_PROPERTY(particle_var::l_max) = lm.draw(gen);
147 GET_PROPERTY(particle_var::a_p) = a_max_m / 2.;
148 GET_PROPERTY(particle_var::a_max) = a_max_dist.mean();
149 random_pool.free_state(gen);
150
151 GET_PROPERTY(particle_var::phi_s) = 0.;
152 }
153
154 KOKKOS_INLINE_FUNCTION MC::Status
155 SimpleAcetate::update([[maybe_unused]] const MC::pool_type& random_pool,
156 FloatType d_t,
157 std::size_t idx,
158 const SelfParticle& arr,
159 const SelfContribs& arr_contribs,
160 const std::size_t position_index,
161 const MC::LocalConcentration& c)
162 {
163 Kokkos::Array<FloatType, N_N> adm
164 = { GET_PROPERTY(particle_var::a_max),
165 GET_PROPERTY(particle_var::a_max) / 3 };
166
167 Kokkos::Array<FloatType, N_N> D{};
168 const auto c0 = GET_CONCENTRATION(0);
169 const auto c1 = GET_CONCENTRATION(1);
170
171 FloatType inv = 1. / (c0 + k[0]);
172 D[0] = adm[0] * c0 * inv;
173 inv = 1. / (c1 + k[1]);
174 D[1] = adm[1] * c1 * inv;
175
176 GET_PROPERTY(particle_var::a_e) = 0.;
177 Kokkos::Array<FloatType, N_N> U{};
178 U[0] = Kokkos::min(D[0], GET_PROPERTY(particle_var::a_p));
179 GET_PROPERTY(particle_var::a_e) += U[0];
180
181 const FloatType pa = D[0] - GET_PROPERTY(particle_var::a_p);
182 const auto mask_pa = static_cast<FloatType>(pa < 0.F);
183
184 U[1] = mask_pa * Kokkos::min(D[1], -pa) + (1 - mask_pa) * 0.F;
185 GET_PROPERTY(particle_var::a_e) += U[1];
186
187 GET_PROPERTY(particle_var::length) += d_t * GET_PROPERTY(particle_var::a_e);
188
189 GET_PROPERTY(particle_var::a_e_s) = U[0];
190 GET_PROPERTY(particle_var::a_e_a) = U[1];
191
192 const auto phi_s = -1 * D[0] * lin_density * y[0];
193 const auto phi_a = mask_pa * (-U[1] * lin_density * y[1])
194 + (1.F - mask_pa) * (pa * lin_density * y[0] / y[1]);
195
196 GET_PROPERTY(particle_var::phi_s) = phi_s;
197 GET_PROPERTY(particle_var::phi_a) = phi_a;
198
199 GET_CONTRIBS(0) = phi_s;
200 GET_CONTRIBS(1) = phi_a;
201
202 return check_div(GET_PROPERTY(Self::particle_var::length),
203 GET_PROPERTY(Self::particle_var::l_max));
204 }
205
206 KOKKOS_INLINE_FUNCTION void
207 SimpleAcetate::division([[maybe_unused]] const MC::pool_type& random_pool,
208 std::size_t idx,
209 std::size_t idx2,
210 const SelfParticle& arr,
211 const SelfParticle& buffer_arr)
212 {
213 Kokkos::View<FloatType**,
214 ComputeSpace::array_layout,
215 Kokkos::MemoryTraits<Kokkos::MemoryTraitsFlags::Restrict>>
216 buffer_e = buffer_arr;
217
218 const FloatType current_l = GET_PROPERTY(particle_var::length);
219 const FloatType new_current_length = current_l / 2.F;
220 GET_PROPERTY(particle_var::length) = new_current_length;
221 constexpr auto binf = INDEX_FROM_ENUM(particle_var::length);
222 constexpr auto bsup = INDEX_FROM_ENUM(particle_var::a_e);
223 for (auto i = binf; i < bsup; ++i)
224 {
225 COPY_PROPERTY_TO(i, idx2, buffer_e);
226 }
227
228 const auto current_a_e = GET_PROPERTY(particle_var::a_e);
229 auto gen = random_pool.get_state();
230 const double sigma = 0.2;
231 const double average = Kokkos::log(current_a_e) - sigma * sigma / 2;
232 const auto dist = MC::Distributions::LogNormal<double>(average, sigma);
233 const auto gen1 = static_cast<FloatType>(dist.draw(gen));
234 const auto gen2 = static_cast<FloatType>(dist.draw(gen));
235
236 static constexpr auto local_l = l_max_dist;
237 const FloatType lmax1 = local_l.draw(gen);
238 const FloatType lmax2 = local_l.draw(gen);
239
240 // KOKKOS_ASSERT(gen1 != 0 && gen2 != 0);
241
242 GET_PROPERTY(particle_var::a_p) = gen1;
243 GET_PROPERTY(particle_var::l_max) = lmax1;
244 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::a_p) = gen2;
245 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l_max) = lmax2;
246
247 random_pool.free_state(gen);
248 }
249
250} // namespace Models
251
252#endif
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 config_loader.hpp:9
KOKKOS_INLINE_FUNCTION MC::Status check_div(const T l, const T lc)
Definition utils.hpp:64
static F consteval _get_phi_s_max(F density, F dl, F glucose_to_biomass_yield=0.5)
Definition utils.hpp:45
KOKKOS_INLINE_FUNCTION consteval F c_linear_density(F rho, F d)
Definition utils.hpp:94
Represents a LogNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:525
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:354
Definition simple_acetate.hpp:27
particle_var
Definition simple_acetate.hpp:34
@ phi_s
Definition simple_acetate.hpp:44
@ a_e_a
Definition simple_acetate.hpp:42
@ length
Definition simple_acetate.hpp:35
@ a_e
Definition simple_acetate.hpp:40
@ phi_a
Definition simple_acetate.hpp:45
@ a_e_s
Definition simple_acetate.hpp:41
@ l_max
Definition simple_acetate.hpp:36
@ __COUNT__
Definition simple_acetate.hpp:46
@ a_max
Definition simple_acetate.hpp:38
@ a_p
Definition simple_acetate.hpp:37
MC::ParticlesContribs< Self::n_c, Self::FloatType > SelfContribs
Definition simple_acetate.hpp:57
static std::array< std::string_view, Self::n_var > names()
Definition simple_acetate.hpp:115
MODEL_CONSTANT FloatType y_a
Definition simple_acetate.hpp:73
std::nullopt_t Config
Definition simple_acetate.hpp:31
MODEL_CONSTANT Kokkos::Array< FloatType, N_N > k
Definition simple_acetate.hpp:70
MODEL_CONSTANT FloatType phi_s_max
Definition simple_acetate.hpp:77
MODEL_CONSTANT auto l_dist
Definition simple_acetate.hpp:83
float FloatType
Definition simple_acetate.hpp:30
std::true_type uniform_weight
Definition simple_acetate.hpp:28
MC::ParticlesModel< Self::n_var, Self::FloatType > SelfParticle
Definition simple_acetate.hpp:53
MODEL_CONSTANT Kokkos::Array< FloatType, N_N > y
Definition simple_acetate.hpp:74
SimpleAcetate Self
Definition simple_acetate.hpp:29
MODEL_CONSTANT FloatType lin_density
Definition simple_acetate.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 SelfContribs &arr_contribs, std::size_t position_index, const MC::LocalConcentration &c)
Definition simple_acetate.hpp:155
MODEL_CONSTANT auto l_max_dist
Definition simple_acetate.hpp:80
MODEL_CONSTANT FloatType y_s
Definition simple_acetate.hpp:72
MODEL_CONSTANT FloatType a_max_m
Definition simple_acetate.hpp:60
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 simple_acetate.hpp:207
MODEL_CONSTANT FloatType d_m
Definition simple_acetate.hpp:64
MODEL_CONSTANT FloatType l_min_m
Definition simple_acetate.hpp:62
MODEL_CONSTANT FloatType l_max_m
Definition simple_acetate.hpp:61
static constexpr std::string_view name
Definition simple_acetate.hpp:52
MODEL_CONSTANT std::size_t N_N
Definition simple_acetate.hpp:59
static constexpr std::size_t n_c
Definition simple_acetate.hpp:55
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition simple_acetate.hpp:109
MODEL_CONSTANT FloatType k_s
Definition simple_acetate.hpp:68
MODEL_CONSTANT FloatType k_a
Definition simple_acetate.hpp:69
static constexpr std::size_t n_var
Definition simple_acetate.hpp:50
static KOKKOS_INLINE_FUNCTION void init(const MC::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition simple_acetate.hpp:133