-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_model.cpp
More file actions
169 lines (144 loc) · 4.98 KB
/
test_model.cpp
File metadata and controls
169 lines (144 loc) · 4.98 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2020-2026 MEmilio
*
* Authors: Daniel Abele, Jan Kleinert
*
* Contact: Martin J. Kuehn <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "memilio/compartments/compartmental_model.h"
#include "memilio/epidemiology/populations.h"
#include "memilio/epidemiology/parameterset.h"
#include "memilio/compartments/simulation.h"
#include "gtest/gtest.h"
TEST(TestCompartmentalModel, secir)
{
/********************************
* Define population categories *
********************************/
// Note: There can be an arbitrary numober of categories
// Each category is a dimension of a multidimensional array
// defining the populatons of each individual compartment
enum class InfectionType
{
S,
E,
C,
I,
H,
U,
R,
D,
Count = 8
};
enum class AgeGroup
{
leq20,
leq40,
leq60,
leq80,
o80,
Count = 5
};
enum class Gender
{
Female,
Male,
Divers,
Count = 3
};
enum class Income
{
poor,
rich,
Count = 2
};
using Po = mio::Populations<InfectionType, AgeGroup, Gender, Income>;
/***********************************************
* Define parameters and instantiate the model *
***********************************************/
// Note: There can be an arbitrary amount of parameters
// These are internally added to a compile time map for fast lookup
struct IncubationTime {
using Type = ScalarType;
static constexpr Type get_default()
{
return 1.0;
}
static constexpr const char* name()
{
return "IncubationTime";
}
};
struct SerialInterval {
using Type = ScalarType;
static constexpr Type get_default()
{
return 1.0;
}
static constexpr const char* name()
{
return "SerialInterval";
}
};
//ADD MORE PARAMETERS HERE
using Pa = mio::ParameterSet<IncubationTime, SerialInterval>;
mio::CompartmentalModel<InfectionType, Po, Pa> model;
/********************
* Define the flows *
********************/
for (size_t i = 0; i < static_cast<size_t>(AgeGroup::Count); ++i) {
AgeGroup ai = static_cast<AgeGroup>(i);
for (size_t j = 0; j < static_cast<size_t>(Gender::Count); ++j) {
Gender gj = static_cast<Gender>(j);
for (size_t k = 0; k < static_cast<size_t>(Income::Count); ++k) {
Income ik = static_cast<Income>(k);
//Ei to Ci
model.add_flow(std::make_tuple(InfectionType::S, ai, gj, ik),
std::make_tuple(InfectionType::E, ai, gj, ik),
[ai, gj, ik](Pa const& p, Eigen::Ref<const Eigen::VectorXd> y, double /*t*/) {
return Po::get_from(y, InfectionType::E, ai, gj, ik) /
(2 * p.get<SerialInterval>() - p.get<IncubationTime>());
});
}
}
}
/****************************
* Define initial conditios *
****************************/
for (size_t i = 0; i < static_cast<size_t>(AgeGroup::Count); ++i) {
AgeGroup ai = static_cast<AgeGroup>(i);
for (size_t j = 0; j < static_cast<size_t>(Gender::Count); ++j) {
Gender gj = static_cast<Gender>(j);
for (size_t k = 0; k < static_cast<size_t>(Income::Count); ++k) {
Income ik = static_cast<Income>(k);
model.populations.set(9750, InfectionType::S, ai, gj, ik);
model.populations.set(100, InfectionType::E, ai, gj, ik);
model.populations.set(100, InfectionType::C, ai, gj, ik);
model.populations.set(50, InfectionType::I, ai, gj, ik);
// all other populations are zero initialized
}
}
}
/********************************************************
* Do some simulations with different incubation times *
********************************************************/
double t0 = 0, tmax = 10, dt = 0.01;
std::vector<double> inc_times{2., 3., 4.};
std::vector<mio::TimeSeries<double>> results;
for (auto inc_time : inc_times) {
model.parameters.set<IncubationTime>(inc_time);
results.push_back(simulate(t0, tmax, dt, model));
}
}