BioCMAMC-ST
probability_leaving.hpp
1#ifndef __SIMULATION_PROBA_LEAVING_HPP__
2#define __SIMULATION_PROBA_LEAVING_HPP__
3
4#include <Kokkos_Assert.hpp>
5#include <Kokkos_Core.hpp>
6#include <common/maths.hpp>
7
9{
10
11 using fast_tag = void;
12 using precision_tag = int;
13
14 static constexpr bool _use_kokkos_log = true; // FIXME
15
16 // template <typename T>
17 // KOKKOS_INLINE_FUNCTION bool
18 // bernouilli_proba(T volume, T flow, T dt)
19 // {
20 // const auto _lambda = dt * flow / volume;
21 // return T{ 1 } - Kokkos::exp(-_lambda);
22 // }
23
24 template <typename T, typename FastSample = precision_tag>
25 KOKKOS_INLINE_FUNCTION bool
26 probability_leaving(const T random_number, const double lambda)
27 {
28 return lambda > -CommonMaths::_ln<_use_kokkos_log>(random_number);
29 }
30
31 template <typename T, typename FastSample = precision_tag>
32 KOKKOS_INLINE_FUNCTION bool
33 probability_leaving(T random_number, double volume, double flow, double dt)
34 {
35 KOKKOS_ASSERT(random_number >= 0. && random_number <= 1.);
36 KOKKOS_ASSERT(volume >= 0.);
37 KOKKOS_ASSERT(flow >= 0.);
38 KOKKOS_ASSERT(dt >= 0.);
39 // Default behavior (with ln)
40 return (dt * flow)
41 > (-CommonMaths::_ln<_use_kokkos_log>(random_number) * volume);
42 }
43
44 // Specialization for when FastSample is provided
45 template <>
46 KOKKOS_INLINE_FUNCTION bool
47 probability_leaving<float, fast_tag>(float random_number,
48 double volume,
49 double flow,
50 double dt)
51 {
52 KOKKOS_ASSERT(random_number >= 0. && random_number <= 1.);
53 KOKKOS_ASSERT(volume >= 0.);
54 KOKKOS_ASSERT(flow >= 0.);
55 KOKKOS_ASSERT(dt >= 0.);
56 // Fast version without ln
57 return (dt * flow / volume) > random_number;
58 }
59
60 // Specialization for when FastSample is provided
61 template <>
62 KOKKOS_INLINE_FUNCTION bool
63 probability_leaving<double, fast_tag>(double random_number,
64 double volume,
65 double flow,
66 double dt)
67 {
68 KOKKOS_ASSERT(random_number >= 0. && random_number <= 1.);
69 KOKKOS_ASSERT(volume >= 0.);
70 KOKKOS_ASSERT(flow >= 0.);
71 KOKKOS_ASSERT(dt >= 0.);
72 // Fast version without ln
73 return (dt * flow / volume) > random_number;
74 }
75
76} // namespace Simulation::KernelInline
77
78#endif
KOKKOS_INLINE_FUNCTION float _ln(float x)
Definition maths.hpp:11
Definition kernels.hpp:19
KOKKOS_INLINE_FUNCTION bool probability_leaving< double, fast_tag >(double random_number, double volume, double flow, double dt)
Definition probability_leaving.hpp:63
static constexpr bool _use_kokkos_log
Definition probability_leaving.hpp:14
KOKKOS_INLINE_FUNCTION bool probability_leaving(const T random_number, const double lambda)
Definition probability_leaving.hpp:26
void fast_tag
Definition probability_leaving.hpp:11
int precision_tag
Definition probability_leaving.hpp:12
KOKKOS_INLINE_FUNCTION bool probability_leaving< float, fast_tag >(float random_number, double volume, double flow, double dt)
Definition probability_leaving.hpp:47