BioCMAMC-ST
fixed_length.hpp
1
2#ifndef __FIXED_LENGTH_MODEL_HPP__
3#define __FIXED_LENGTH_MODEL_HPP__
4
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 <optional>
11#include <string_view>
12
13namespace Models
14{
15 template <FloatingPointType F>
16 static F consteval _get_phi_s_max(F density,
17 F dl,
18 F glucose_to_biomass_yield = 0.5)
19 {
20 // dl and density must be same unit, dl*density -> mass and y is mass yield
21 return (dl * density) / glucose_to_biomass_yield;
22 }
24 {
25 using uniform_weight = std::true_type; // Using type alias
27 using FloatType = float;
28 using Config = std::nullopt_t;
29
30 enum class particle_var : int
31 {
32 length = 0,
36 };
37
38 static constexpr std::size_t n_var = INDEX_FROM_ENUM(particle_var::COUNT);
39 static constexpr std::string_view name = "fixed-length";
41
42 MODEL_CONSTANT FloatType l_dot_max = 2e-6 / 3600.; // m
43 MODEL_CONSTANT FloatType l_max_m = 2e-6; // m
44 MODEL_CONSTANT FloatType l_min_m = l_max_m / 2.; // m
45 MODEL_CONSTANT FloatType k = 1e-3; // m
46 MODEL_CONSTANT FloatType d_m = 0.6e-6; // m
47 MODEL_CONSTANT FloatType lin_density =
48 c_linear_density(static_cast<FloatType>(1000), d_m);
49
50 MODEL_CONSTANT FloatType phi_s_max =
52
53 KOKKOS_INLINE_FUNCTION static void
54 init(const MC::KPRNG::pool_type& random_pool,
55 std::size_t idx,
56 const SelfParticle& arr);
57
58 KOKKOS_INLINE_FUNCTION static MC::Status
59 update(const MC::KPRNG::pool_type& random_pool,
60 FloatType d_t,
61 std::size_t idx,
62 const SelfParticle& arr,
63 const MC::LocalConcentration& c);
64
65 KOKKOS_INLINE_FUNCTION static void
66 division(const MC::KPRNG::pool_type& random_pool,
67 std::size_t idx,
68 std::size_t idx2,
69 const SelfParticle& arr,
70 const SelfParticle& buffer_arr);
71
72 KOKKOS_INLINE_FUNCTION static void
73 contribution(std::size_t idx,
74 std::size_t position,
75 double weight,
76 const SelfParticle& arr,
77 const MC::ContributionView& contributions);
78
79 KOKKOS_INLINE_FUNCTION static double mass(std::size_t idx,
80 const SelfParticle& arr)
81 {
82 return GET_PROPERTY(Self::particle_var::length) * lin_density;
83 }
84 static std::vector<std::string_view> names()
85 {
86 return {
87
88 "length",
89 };
90 }
91 static std::vector<std::size_t> get_number()
92 {
93 return {INDEX_FROM_ENUM(particle_var::length)};
94 }
95 };
96
97 CHECK_MODEL(FixedLength)
98
99 KOKKOS_INLINE_FUNCTION void
100 FixedLength::init([[maybe_unused]] const MC::KPRNG::pool_type& random_pool,
101 std::size_t idx,
102 const SelfParticle& arr)
103 {
104
105 // normal1
106 // constexpr auto l_initial_dist =
107 // MC::Distributions::TruncatedNormal<float>(
108 // l_min_m / 2, l_min_m / 5., 0., static_cast<double>(1));
109
110 // normal2 lmin/2 lmin/2
111 // normal3 l_min*0.7
112 // norma4l
113 constexpr auto l_initial_dist = MC::Distributions::TruncatedNormal<float>(
114 l_min_m, l_min_m / 2., 0., static_cast<double>(1));
115
116 auto gen = random_pool.get_state();
117 auto linit = l_initial_dist.draw(
118 gen); // l_min_m; // gen.drand(l_min_m * .8, l_max_m);
119 random_pool.free_state(gen);
120 GET_PROPERTY(particle_var::length) = linit;
121
122 // Stochastic 1
123 // auto gen = random_pool.get_state();
124 // GET_PROPERTY(particle_var::l_max) = gen.drand(l_max_m * 0.9, l_max_m
125 // * 1.1); random_pool.free_state(gen);
126
127 // Stochastic 2
128 // auto gen = random_pool.get_state();
129 // GET_PROPERTY(particle_var::l_max) = gen.drand(l_max_m * 0.8, l_max_m
130 // * 1.2); random_pool.free_state(gen);
131
132 GET_PROPERTY(particle_var::l_max) = l_max_m;
133
134 // GET_PROPERTY(particle_var::length) = l_max_m / 2.;
135
136 GET_PROPERTY(particle_var::phi_s) = 0.;
137 }
138
139 KOKKOS_INLINE_FUNCTION MC::Status
140 FixedLength::update([[maybe_unused]] const MC::KPRNG::pool_type& random_pool,
141 FloatType d_t,
142 std::size_t idx,
143 const SelfParticle& arr,
144 const MC::LocalConcentration& c)
145 {
146 const FloatType s = c(0);
147 const FloatType g = s / (k + s);
149 const auto ldot = l_dot_max * g;
150 GET_PROPERTY(Self::particle_var::phi_s) = phi_s;
151 GET_PROPERTY(Self::particle_var::length) += d_t * ldot;
152 return (GET_PROPERTY(Self::particle_var::length) >=
153 GET_PROPERTY(particle_var::l_max))
156 }
157
158 KOKKOS_INLINE_FUNCTION void
160 std::size_t idx,
161 std::size_t idx2,
162 const SelfParticle& arr,
163 const SelfParticle& buffer_arr)
164 {
165 const FloatType new_current_length =
166 GET_PROPERTY(particle_var::length) / 2.F;
167 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::length) =
168 new_current_length;
169 GET_PROPERTY(particle_var::length) = new_current_length;
170
171 // Deterministic
172 GET_PROPERTY(particle_var::l_max) = l_max_m;
173 GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l_max) = l_max_m;
174
175 // Stochastic 2
176 // auto gen = random_pool.get_state();
177 // GET_PROPERTY(particle_var::l_max) = gen.drand(l_max_m * 0.8, l_max_m
178 // * 1.2); GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l_max) =
179 // gen.drand(l_max_m * 0.8, l_max_m * 1.2);
180 // random_pool.free_state(gen);
181
182 // Stochastic 1
183 // auto gen = random_pool.get_state();
184 // GET_PROPERTY(particle_var::l_max) = gen.drand(l_max_m * 0.9, l_max_m
185 // * 1.1); GET_PROPERTY_FROM(idx2, buffer_arr, particle_var::l_max) =
186 // gen.drand(l_max_m * 0.9, l_max_m * 1.1);
187 // random_pool.free_state(gen);
188 }
189
190 KOKKOS_INLINE_FUNCTION void
191 FixedLength::contribution([[maybe_unused]] std::size_t idx,
192 std::size_t position,
193 double weight,
194 [[maybe_unused]] const SelfParticle& arr,
195 const MC::ContributionView& contributions)
196 {
197 auto access = contributions.access();
198 access(position, 0) +=
199 -weight * GET_PROPERTY(FixedLength::particle_var::phi_s); // NOLINT
200 }
201
202} // namespace Models
203
204#endif
Kokkos::Random_XorShift1024_Pool< Kokkos::DefaultExecutionSpace > pool_type
Definition prng.hpp:17
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
@ Division
Definition alias.hpp:13
@ Idle
Definition alias.hpp:12
Models definition.
Definition config_loader.hpp:8
static F consteval _get_phi_s_max(F density, F dl, F glucose_to_biomass_yield=0.5)
Definition fixed_length.hpp:16
KOKKOS_INLINE_FUNCTION consteval F c_linear_density(F rho, F d)
Definition utils.hpp:40
Represents a TruncatedNormal (Gaussian) probability distribution.
Definition prng_extension.hpp:263
Definition fixed_length.hpp:24
MC::ParticlesModel< Self::n_var, Self::FloatType > SelfParticle
Definition fixed_length.hpp:40
FixedLength Self
Definition fixed_length.hpp:26
static constexpr std::string_view name
Definition fixed_length.hpp:39
static KOKKOS_INLINE_FUNCTION void contribution(std::size_t idx, std::size_t position, double weight, const SelfParticle &arr, const MC::ContributionView &contributions)
Definition fixed_length.hpp:191
particle_var
Definition fixed_length.hpp:31
@ phi_s
Definition fixed_length.hpp:34
@ length
Definition fixed_length.hpp:32
@ COUNT
Definition fixed_length.hpp:35
@ l_max
Definition fixed_length.hpp:33
MODEL_CONSTANT FloatType l_max_m
Definition fixed_length.hpp:43
static std::vector< std::size_t > get_number()
Definition fixed_length.hpp:91
MODEL_CONSTANT FloatType l_dot_max
Definition fixed_length.hpp:42
MODEL_CONSTANT FloatType l_min_m
Definition fixed_length.hpp:44
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 fixed_length.hpp:140
MODEL_CONSTANT FloatType d_m
Definition fixed_length.hpp:46
MODEL_CONSTANT FloatType phi_s_max
Definition fixed_length.hpp:50
MODEL_CONSTANT FloatType lin_density
Definition fixed_length.hpp:47
static std::vector< std::string_view > names()
Definition fixed_length.hpp:84
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 fixed_length.hpp:159
MODEL_CONSTANT FloatType k
Definition fixed_length.hpp:45
float FloatType
Definition fixed_length.hpp:27
static KOKKOS_INLINE_FUNCTION void init(const MC::KPRNG::pool_type &random_pool, std::size_t idx, const SelfParticle &arr)
Definition fixed_length.hpp:100
std::true_type uniform_weight
Definition fixed_length.hpp:25
std::nullopt_t Config
Definition fixed_length.hpp:28
static constexpr std::size_t n_var
Definition fixed_length.hpp:38
static KOKKOS_INLINE_FUNCTION double mass(std::size_t idx, const SelfParticle &arr)
Definition fixed_length.hpp:79