-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgrid_sir_cell.hpp
More file actions
63 lines (54 loc) · 2.17 KB
/
grid_sir_cell.hpp
File metadata and controls
63 lines (54 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2022-present Román Cárdenas Rodríguez
* ARSLab - Carleton University
*/
#ifndef CADMIUM_EXAMPLE_CELLDEVS_SIR_GRID_CELL_HPP_
#define CADMIUM_EXAMPLE_CELLDEVS_SIR_GRID_CELL_HPP_
#include <cmath>
#include <nlohmann/json.hpp>
#include <cadmium/modeling/celldevs/grid/cell.hpp>
#include <cadmium/modeling/celldevs/grid/config.hpp>
#include "state.hpp"
namespace cadmium::celldevs::example::sir {
//! Grid Susceptible-Infected-Recovered cell.
class GridSIRCell : public GridCell<SIRState, double> {
double rec; //!< recovery factor.
double susc; //!< susceptibility factor.
double vir; //!< virulence factor.
public:
GridSIRCell(const std::vector<int>& id, const std::shared_ptr<const GridCellConfig<SIRState, double>>& config):
GridCell<SIRState, double>(id, config), rec(), susc(), vir() {
config->rawCellConfig.at("rec").get_to(rec);
config->rawCellConfig.at("susc").get_to(susc);
config->rawCellConfig.at("vir").get_to(vir);
}
[[nodiscard]] SIRState localComputation(SIRState state,
const std::unordered_map<std::vector<int>, NeighborData<SIRState, double>>& neighborhood) const override {
auto newI = newInfections(state, neighborhood);
auto newR = newRecoveries(state);
// We round the outcome to three decimals:
state.r = std::round((state.r + newR) * 1000) / 1000;
state.i = std::round((state.i + newI - newR) * 1000) / 1000;
state.s = 1 - state.i - state.r;
return state;
}
[[nodiscard]] double outputDelay(const SIRState& state) const override {
return 1.;
}
[[nodiscard]] double newInfections(const SIRState& state,
const std::unordered_map<std::vector<int>, NeighborData<SIRState, double>>& neighborhood) const {
double aux = 0;
for (const auto& [neighborId, neighborData]: neighborhood) {
auto s = neighborData.state;
auto v = neighborData.vicinity;
aux += s->i * (double)s->p * v;
}
return state.s * susc * std::min(1., vir * aux / state.p);
}
[[nodiscard]] double newRecoveries(const SIRState& state) const {
return state.i * rec;
}
};
} //namespace admium::celldevs::example::sir
#endif //CADMIUM_EXAMPLE_CELLDEVS_SYMM_SIR_CELLS_SIR_HPP_