BioCMAMC-ST
simulation.hpp
1#ifndef __SIMULATIONS_UNIT_HPP__
2#define __SIMULATIONS_UNIT_HPP__
3
4#include "mixture/species_descriptor.hpp"
5#include <Kokkos_Core.hpp>
6#include <Kokkos_ScatterView.hpp>
7#include <biocma_cst_config.hpp>
8#include <cassert>
9#include <cma_utils/alias.hpp>
10#include <common/common.hpp>
11#include <common/logger.hpp>
12#include <cstddef>
13#include <mc/domain.hpp>
14#include <mc/events.hpp>
15#include <mc/prng/prng.hpp>
16#include <mc/unit.hpp>
17#include <memory>
18#include <optional>
19#include <simulation/descriptors/dimensions.hpp>
20#include <simulation/feed_descriptor.hpp>
21#include <simulation/kernels/kernels.hpp>
22#include <simulation/mass_transfer.hpp>
23#include <simulation/probe.hpp>
24#include <simulation/scalar_initializer.hpp>
25#include <simulation/simulation_exception.hpp>
26#include <simulation/simulation_getter.hpp>
27#include <simulation/simulation_times.hpp>
28// TODO Clean
29static constexpr size_t trigger_const_particle_number = 1e6;
30
31namespace CmaUtils
32{
33 class PreCalculatedHydroState;
34} // namespace CmaUtils
35
41namespace Simulation
42{
43 class ScalarSimulation;
44
46 {
47 private:
49
50 public:
51 friend class Getter;
52
53 // Getter get() const;
54
56 std::unique_ptr<MC::MonteCarloUnit>&& _unit,
57 ScalarInitializer&& scalar_init,
58 std::optional<Feed::SimulationFeed> _feed,
59 std::shared_ptr<Mixture::SpecieTable> table,
60 std::optional<MassTransfer::Type::MtrTypeVariant>&& mt_model_variant,
61 Sparam params);
62
64
65 SimulationUnit(SimulationUnit&& other) noexcept;
66 SimulationUnit(const SimulationUnit& other) = delete;
69
70 // Setters
71 // void setEndTime(double _end_time) noexcept;
72 void overwriteStartTime(double _start_time) noexcept;
73 void setProbes(ProbeType type, ProbeAutogeneratedBuffer&& _probes);
74
75 void setLogger(std::shared_ptr<IO::Logger>);
76
77 Getter getter() const noexcept;
78
84 [[nodiscard]] std::unique_ptr<MC::MonteCarloUnit> move_mcu() noexcept;
85
86 // Getters
87
88 // Simulation methods
89 void cycleProcess(auto& container, double d_t, auto& _functors);
90 void ode_step(double d_t) const;
91 void clearContribution() const noexcept;
92 void update_feed(double d_t, bool update_scalar = true) noexcept;
93
94 double advance(double d_t) noexcept;
95
96 void updateHydro(const CmaUtils::IterationStatePtrType& newstate);
97 void updateMCHydro(std::span<const double> newliquid_volume,
98 std::span<const std::size_t> neighors_flat,
99 std::span<const double> proba_flat,
100 std::span<const double> out_flows) const;
101
102 void calculate_mtr();
103
104 bool checkScalar() const;
105
106 // Memory management
107 [[deprecated]] void reset();
108
109 template <typename Space, ModelType Model>
110 KernelInline::CycleFunctors<Space, Model>
111 init_functors(MC::ParticlesContainer<Model> container,
112 KernelDispatchOptions options);
113
114 private:
115 std::unique_ptr<MC::MonteCarloUnit> mc_unit; // NOLINT
116
117 std::shared_ptr<Mixture::SpecieTable> m_table;
118
119 void updateScalarHydro(const CmaUtils::IterationStatePtrType& newstate);
120
121 void setLiquidFlow(CmaUtils::PreCalculatedHydroState* _flows_l);
122 void setGasFlow(CmaUtils::PreCalculatedHydroState* _flows_g);
124
125 [[nodiscard]] MC::KernelConcentrationType getkernel_concentration() const;
126
127 MC::ContributionView contribs_scatter;
130
131 // Attributes
132 Simulation::Feed::SimulationFeed m_feed;
133
137
139
140 void scatter_contribute();
141
142 void setMtrModel(MassTransfer::Type::MtrTypeVariant&& variant);
143
144 [[nodiscard]] MC::kernelContribution get_kernel_contribution() const;
145
146 void post_init_concentration(ScalarInitializer&& scalar_init);
147
149 std::shared_ptr<ScalarSimulation> gas_scalar;
150 // TODO add default null value (no model)
151 MassTransfer::MassTransferModel m_mt_model;
152
153 std::shared_ptr<IO::Logger> logger;
154
155 template <ModelType Model>
156 void post_cycle(MC::ParticlesContainer<Model>& container,
157 auto& cycle_functors);
158
159 template <ModelType Model>
160 void pre_cycle(MC::ParticlesContainer<Model>& container,
161 double d_t,
162 auto& functors);
163 };
164
165 template <ModelType Model>
166 void
167 SimulationUnit::pre_cycle(MC::ParticlesContainer<Model>& container,
168 double d_t,
169 auto& cycle_functors)
170 {
171 PROFILE_SECTION("Simulation::pre_cycle")
172
173 // Must precede the move kernel: it consumes move_probability, which is
174 // only valid for this d_t and this flowmap.
175 this->mc_unit->domain.set_move_probability(d_t);
176
177 cycle_functors.update(
178 d_t, container, this->mc_unit->domain.get_const_inner());
179 }
180
181 template <typename Space, ModelType Model>
184 KernelDispatchOptions options)
185 {
186
188 options,
189 container,
190 mc_unit->rng.random_pool,
193 mc_unit->events,
194 mc_unit->domain.get_const_inner(),
195 probes[ProbeType ::LeavingTime],
196 probes[ProbeType ::DivisionTime]);
197 }
198
199 void
201 double d_t,
202 auto& cycle_functors)
203 {
204 PROFILE_SECTION("cycleProcess")
205 using CurrentModel =
206 typename std::remove_reference<decltype(container)>::type::UsedModel;
207 const size_t n_particle = container.n_particles();
208 if (n_particle == 0)
209 {
210 return;
211 }
212
213 pre_cycle(container, d_t, cycle_functors);
214
215 if (f_reaction)
216 {
217 this->contribs_scatter.reset();
218 cycle_functors.launch_model(n_particle);
219 }
220
221 if (cycle_functors.move_kernel.need_launch())
222 {
223 cycle_functors.launch_move(n_particle);
224 }
225
226 post_cycle<CurrentModel>(container, cycle_functors);
227 }
228
229 template <ModelType Model>
230 void
232 auto& cycle_functors)
233 {
234 PROFILE_SECTION("Simulation::post_cycle")
235 Kokkos::fence();
236 this->scatter_contribute();
237 const auto [host_red, host_out_counter]
238 = cycle_functors.get_host_reduction();
239
240 container.update_and_remove_inactive(host_out_counter, host_red.dead_total);
241
242 container.merge_buffer();
243
244 if (host_red.waiting_allocation_particle != 0)
245 {
246 if (logger)
247 {
248 logger->alert("Simulation",
249 "Overflow of particle not implemented yet (ignore "
250 "_waiting_allocation_particle)");
251 }
252 }
253
254 // set_kernel_contribs_to_host();
255 }
256
257} // namespace Simulation
258
259#endif //__SIMULATIONS_UNIT_HPP__
Main owning object for Monte-Carlo particles.
Definition particles_container.hpp:59
void update_and_remove_inactive(std::size_t out, std::size_t dead)
Update the number of non-idle particle and remove if total number exceeds threshold May call : remove...
Definition particles_container.hpp:546
void merge_buffer()
Insert particle buffer into the main container.
Definition particles_container.hpp:582
Definition scalar_simulation.hpp:29
void ode_step(double d_t) const
Definition simulation.model.cpp:141
Getter accesor
Definition simulation.hpp:48
SimulatimeTimes m_times
Definition simulation.hpp:138
SimulationUnit & operator=(const SimulationUnit &rhs)=delete
bool f_reaction
Definition simulation.hpp:136
void updateMCHydro(std::span< const double > newliquid_volume, std::span< const std::size_t > neighors_flat, std::span< const double > proba_flat, std::span< const double > out_flows) const
Definition simulation.cpp:120
void updateScalarHydro(const CmaUtils::IterationStatePtrType &newstate)
Definition simulation.cpp:131
void post_init_concentration(ScalarInitializer &&scalar_init)
Definition simulation.cpp:164
void setLogger(std::shared_ptr< IO::Logger >)
Definition simulation.getset.cpp:191
MC::KernelConcentrationType getkernel_concentration() const
Definition simulation.getset.cpp:220
bool const_number_simulation
Definition simulation.hpp:134
std::shared_ptr< Mixture::SpecieTable > m_table
Definition simulation.hpp:117
std::unique_ptr< MC::MonteCarloUnit > move_mcu() noexcept
Definition simulation.getset.cpp:208
MapProbes probes
Definition simulation.hpp:128
Getter getter() const noexcept
Definition simulation.getset.cpp:185
void cycleProcess(auto &container, double d_t, auto &_functors)
Definition simulation.hpp:200
Dimensions dims
Definition simulation.hpp:129
void updateHydro(const CmaUtils::IterationStatePtrType &newstate)
Definition simulation.cpp:104
void setLiquidFlow(CmaUtils::PreCalculatedHydroState *_flows_l)
double advance(double d_t) noexcept
Definition simulation.model.cpp:125
Simulation::Feed::SimulationFeed m_feed
Definition simulation.hpp:132
std::unique_ptr< MC::MonteCarloUnit > mc_unit
Definition simulation.hpp:115
void setGasFlow(CmaUtils::PreCalculatedHydroState *_flows_g)
void setProbes(ProbeType type, ProbeAutogeneratedBuffer &&_probes)
Definition simulation.getset.cpp:214
void overwriteStartTime(double _start_time) noexcept
Definition simulation.getset.cpp:226
KernelInline::CycleFunctors< Space, Model > init_functors(MC::ParticlesContainer< Model > container, KernelDispatchOptions options)
Definition simulation.hpp:183
SimulationUnit(const SimulationUnit &other)=delete
std::shared_ptr< ScalarSimulation > gas_scalar
Definition simulation.hpp:149
void setMtrModel(MassTransfer::Type::MtrTypeVariant &&variant)
Definition simulation.getset.cpp:197
void post_cycle(MC::ParticlesContainer< Model > &container, auto &cycle_functors)
Definition simulation.hpp:231
void reset()
Definition simulation.cpp:95
SimulationUnit & operator=(SimulationUnit &&rhs)=delete
bool is_two_phase_flow
Definition simulation.hpp:135
void update_feed(double d_t, bool update_scalar=true) noexcept
Definition simulation.model.cpp:73
std::shared_ptr< ScalarSimulation > liquid_scalar
Definition simulation.hpp:148
friend class Getter
Definition simulation.hpp:51
void calculate_mtr()
Definition simulation.model.cpp:132
SimulationUnit(std::unique_ptr< MC::MonteCarloUnit > &&_unit, ScalarInitializer &&scalar_init, std::optional< Feed::SimulationFeed > _feed, std::shared_ptr< Mixture::SpecieTable > table, std::optional< MassTransfer::Type::MtrTypeVariant > &&mt_model_variant, Sparam params)
Definition simulation.cpp:47
MassTransfer::MassTransferModel m_mt_model
Definition simulation.hpp:151
bool checkScalar() const
Definition simulation.model.cpp:21
void pre_cycle(MC::ParticlesContainer< Model > &container, double d_t, auto &functors)
Definition simulation.hpp:167
void scatter_contribute()
Definition simulation.cpp:153
std::shared_ptr< IO::Logger > logger
Definition simulation.hpp:153
void clearContribution() const noexcept
Definition simulation.model.cpp:43
MC::kernelContribution get_kernel_contribution() const
Definition simulation.getset.cpp:232
MC::ContributionView contribs_scatter
Definition simulation.hpp:127
Model type.
Definition traits.hpp:155
Definition host_specific.hpp:17
Definition impl_post_process.hpp:11
Namespace that contains classes and structures related to Monte Carlo (MC) simulations.
Definition alias.hpp:16
Definition species_descriptor.hpp:10
Definition feed_descriptor.hpp:36
Definition cycle_reducer.hpp:13
Definition impl_mass_transfer.hpp:23
Namespace that contains classes and structures related to simulation handling.
Definition host_specific.hpp:12
ProbeType
Definition probe.hpp:15
Probes< AutoGenerated::probe_buffer_size > ProbeAutogeneratedBuffer
Definition probe.hpp:150
std::unordered_map< ProbeType, ProbeAutogeneratedBuffer > MapProbes
Definition probe.hpp:152
Definition execinfo.hpp:13
Definition dimensions.hpp:9
Definition scalar_initializer.hpp:29
Definition simulation_times.hpp:7
Definition simulation_getter.hpp:26