BioCMAMC-ST
utils.hpp
1#ifndef __BIO__UTILS_HPP__
2#define __BIO__UTILS_HPP__
3
4#include "Kokkos_Macros.hpp"
5#include <Kokkos_Core.hpp>
6#include <Kokkos_MathematicalConstants.hpp>
7#include <Kokkos_Random.hpp>
8#include <common/traits.hpp>
9#include <mc/alias.hpp>
10#include <mc/macros.hpp>
11#include <mc/prng/prng.hpp>
12#include <tuple>
13
17namespace Models
18{
19 template <typename T>
20 KOKKOS_INLINE_FUNCTION T
22 const std::size_t position_index,
23 const std::size_t species_index)
24 {
25 return static_cast<T>(Kokkos::max(
26 static_cast<typename MC::LocalConcentration::non_const_value_type>(0),
27 GET_CONCENTRATION(species_index)));
28 }
29
30#define GET_CLAMPED_CONCENTRATION_CAST(__T__, __species_index__) \
31 get_clamped_concentration_cast<__T__>(c, position_index, __species_index__)
32
33#define GET_CLAMPED_CONCENTRATION(__species_index__) \
34 get_clamped_concentration_cast<MC::LocalConcentration::const_value_type>( \
35 c, position_index, __species_index__)
36
37 // template<FloatingPointType T>
38 // struct partiton_t
39 // {
40 // T a;
41 // T b;
42 // };
43
44 template <FloatingPointType F>
45 static F consteval _get_phi_s_max(F density,
46 F dl,
47 F glucose_to_biomass_yield = 0.5)
48 {
49 // dl and density must be same unit, dl*density -> mass and y is mass yield
50 return (dl * density) / glucose_to_biomass_yield;
51 }
52
53 template <FloatingPointType T>
54 KOKKOS_INLINE_FUNCTION Kokkos::pair<T, T>
55 g_partition(T a, T p)
56 {
57 const auto a1 = a * p;
58 const auto a2 = (static_cast<T>(1) - p) * a;
59 return Kokkos::pair(a1, a2);
60 }
61
62 template <FloatingPointType T>
63 KOKKOS_INLINE_FUNCTION MC::Status
64 check_div(const T l, const T lc)
65 {
66 return (l >= lc) ? MC::Status::Division : MC::Status::Idle;
67 }
68
69 namespace MolarMass
70 {
71 namespace GramPerMole
72 {
73
74 template <FloatingPointType T> constexpr T glucose = static_cast<T>(180);
75 template <FloatingPointType T> constexpr T dioxygen = static_cast<T>(32);
76 template <FloatingPointType T> constexpr T acetate = static_cast<T>(59);
77
78 constexpr double co2 = 44;
79 ;
80 }; // namespace GramPerMole
81
82 constexpr double glucose = GramPerMole::glucose<double> * 1e-3;
83 constexpr double dioxygen = GramPerMole::dioxygen<double> * 1e-3;
84 ;
85 constexpr double acetate = GramPerMole::acetate<double> * 1e-3;
86 ;
87 constexpr double co2 = GramPerMole::co2 * 1e-3;
88 ;
89 [[deprecated]] constexpr double X = 113.1e-3;
90 }; // namespace MolarMass
91
92 template <FloatingPointType F>
93 KOKKOS_INLINE_FUNCTION consteval F
94 c_linear_density(F rho, F d)
95 {
96 return rho * F(Kokkos::numbers::pi) * d * d / F(4.);
97 }
98
99 static constexpr double tau_division_proba = 1e-7;
100
101 KOKKOS_INLINE_FUNCTION bool
103 double gamma,
104 MC::pool_type random_pool)
105 {
106 (void)d_t;
107 // const double proba_div =
108 // (1 - Kokkos::exp(-d_t / tau_division_proba)) * gamma;
109
110 auto generator = random_pool.get_state();
111 const double x = generator.drand(0., 1.);
112 random_pool.free_state(generator);
113
114 return x < gamma;
115 }
116
118 {
119 KOKKOS_INLINE_FUNCTION double
120 threshold_linear(double lenght, double threshold, double upper_bound)
121 {
122 return (lenght <= threshold)
123 ? 0
124 : (lenght - threshold) / (upper_bound - threshold);
125 }
126
127 KOKKOS_INLINE_FUNCTION constexpr float
128 logistic(float x, float xmax, float alpha)
129 {
130 // auto blna = alpha * std::log(xmax);
131 // return 1 / (1 + std::exp(-alpha * std::log(x) + blna));
132 // return x<xmax?1. / (1 + std::exp(alpha * std::log((x-xmax)/xmax ))):1.;
133
134 const auto z = x < xmax ? std::pow(x / (xmax - x), alpha) : 1.;
135 return z / (1. + z);
136 }
137
138 } // namespace GammaDivision
139
140 KOKKOS_INLINE_FUNCTION bool
141 almost_equal(double val, double val2, double tolerance = 1e-8)
142 {
143 return Kokkos::abs(val - val2) < tolerance;
144 }
145
146 KOKKOS_INLINE_FUNCTION constexpr bool
147 almost_zero(double x, double tol = 1e-8)
148 {
149 return (-tol < x) && (x < tol);
150 }
151
152 template <typename... Args>
153 KOKKOS_INLINE_FUNCTION double
154 min_var(Args... args)
155 {
156 return (Kokkos::min)({ args... });
157 }
158 template <>
159 KOKKOS_INLINE_FUNCTION double
161 {
162 return 0.;
163 }
164
165} // namespace Models
166
167#endif
Status
Definition alias.hpp:125
@ Division
Definition alias.hpp:127
@ Idle
Definition alias.hpp:126
gen_pool_type< Kokkos::DefaultExecutionSpace > pool_type
Definition alias.hpp:100
KernelConcentrationType LocalConcentration
Definition alias.hpp:170
Definition utils.hpp:118
KOKKOS_INLINE_FUNCTION double threshold_linear(double lenght, double threshold, double upper_bound)
Definition utils.hpp:120
KOKKOS_INLINE_FUNCTION constexpr float logistic(float x, float xmax, float alpha)
Definition utils.hpp:128
Definition utils.hpp:72
constexpr T acetate
Definition utils.hpp:76
constexpr T glucose
Definition utils.hpp:74
constexpr T dioxygen
Definition utils.hpp:75
constexpr double co2
Definition utils.hpp:78
Definition utils.hpp:70
constexpr double acetate
Definition utils.hpp:85
constexpr double co2
Definition utils.hpp:87
constexpr double X
Definition utils.hpp:89
constexpr double glucose
Definition utils.hpp:82
constexpr double dioxygen
Definition utils.hpp:83
Models definition.
Definition config_loader.hpp:9
KOKKOS_INLINE_FUNCTION bool check_probability_division(double d_t, double gamma, MC::pool_type random_pool)
Definition utils.hpp:102
KOKKOS_INLINE_FUNCTION T get_clamped_concentration_cast(const MC::LocalConcentration &c, const std::size_t position_index, const std::size_t species_index)
Definition utils.hpp:21
KOKKOS_INLINE_FUNCTION Kokkos::pair< T, T > g_partition(T a, T p)
Definition utils.hpp:55
KOKKOS_INLINE_FUNCTION double min_var()
Definition utils.hpp:160
KOKKOS_INLINE_FUNCTION constexpr bool almost_zero(double x, double tol=1e-8)
Definition utils.hpp:147
KOKKOS_INLINE_FUNCTION MC::Status check_div(const T l, const T lc)
Definition utils.hpp:64
KOKKOS_INLINE_FUNCTION bool almost_equal(double val, double val2, double tolerance=1e-8)
Definition utils.hpp:141
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
static constexpr double tau_division_proba
Definition utils.hpp:99