Skip to content

Commit f43dba2

Browse files
committed
readjust test, write some documentation
1 parent 58856cd commit f43dba2

File tree

4 files changed

+88
-79
lines changed

4 files changed

+88
-79
lines changed

cpp/models/ode_seir_metapop/model.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ using Flows = TypeList<Flow<InfectionState::Susceptible, InfectionState::Exposed
4343
Flow<InfectionState::Infected, InfectionState::Recovered>>;
4444

4545
/**
46-
* @brief The Model holds the Parameters and the initial Populations and defines the ordinary differential equations.
46+
* @brief The Model holds the Parameters and the initial Populations for every region and defines the ordinary differential equations.
4747
*/
4848
template <typename FP = ScalarType>
4949
class Model : public FlowModel<FP, InfectionState, mio::Populations<FP, mio::regions::Region, AgeGroup, InfectionState>,
@@ -58,7 +58,7 @@ class Model : public FlowModel<FP, InfectionState, mio::Populations<FP, mio::reg
5858
using typename Base::Populations;
5959

6060
/**
61-
* @brief Create a Model with the given number of Region%s and AgeGroups.
61+
* @brief Create a Model with the given number of Region%s and AgeGroup%s.
6262
* @param[in] num_regions The number of Region%s.
6363
* @param[in] num_agegroups The number of AgeGroup%s.
6464
*/
@@ -104,14 +104,10 @@ class Model : public FlowModel<FP, InfectionState, mio::Populations<FP, mio::reg
104104
for (size_t age_i = 0; age_i < n_age_groups; age_i++) {
105105
for (size_t age_j = 0; age_j < n_age_groups; age_j++) {
106106
for (size_t region_n = 0; region_n < n_regions; region_n++) {
107-
const size_t Ejn =
108-
population.get_flat_index({Region(region_n), AgeGroup(age_j), Exposed});
109-
const size_t Ijn =
110-
population.get_flat_index({Region(region_n), AgeGroup(age_j), Infected});
111-
const size_t Rjn =
112-
population.get_flat_index({Region(region_n), AgeGroup(age_j), Recovered});
113-
const size_t Sjn =
114-
population.get_flat_index({Region(region_n), AgeGroup(age_j), Susceptible});
107+
const size_t Ejn = population.get_flat_index({Region(region_n), AgeGroup(age_j), Exposed});
108+
const size_t Ijn = population.get_flat_index({Region(region_n), AgeGroup(age_j), Infected});
109+
const size_t Rjn = population.get_flat_index({Region(region_n), AgeGroup(age_j), Recovered});
110+
const size_t Sjn = population.get_flat_index({Region(region_n), AgeGroup(age_j), Susceptible});
115111

116112
const double Nj_inv = 1.0 / (pop[Sjn] + pop[Ejn] + pop[Ijn] + pop[Rjn]);
117113
double coeffStoE =
@@ -126,12 +122,10 @@ class Model : public FlowModel<FP, InfectionState, mio::Populations<FP, mio::reg
126122
}
127123
}
128124
for (size_t region = 0; region < n_regions; region++) {
129-
flows[Base::template get_flat_flow_index<Exposed, Infected>(
130-
{Region(region), AgeGroup(age_i)})] =
125+
flows[Base::template get_flat_flow_index<Exposed, Infected>({Region(region), AgeGroup(age_i)})] =
131126
y[population.get_flat_index({Region(region), AgeGroup(age_i), Exposed})] /
132127
params.template get<TimeExposed<FP>>()[AgeGroup(age_i)];
133-
flows[Base::template get_flat_flow_index<Infected, Recovered>(
134-
{Region(region), AgeGroup(age_i)})] =
128+
flows[Base::template get_flat_flow_index<Infected, Recovered>({Region(region), AgeGroup(age_i)})] =
135129
y[population.get_flat_index({Region(region), AgeGroup(age_i), Infected})] /
136130
params.template get<TimeInfected<FP>>()[AgeGroup(age_i)];
137131
}
@@ -256,8 +250,7 @@ class Model : public FlowModel<FP, InfectionState, mio::Populations<FP, mio::reg
256250
for (size_t age = 0; age < number_age_groups; ++age) {
257251
double population_n = 0;
258252
for (size_t state = 0; state < (size_t)InfectionState::Count; state++) {
259-
population_n += population[{Region(region_n), mio::AgeGroup(age),
260-
InfectionState(state)}];
253+
population_n += population[{Region(region_n), mio::AgeGroup(age), InfectionState(state)}];
261254
}
262255
population_after_commuting[{Region(region_n), mio::AgeGroup(age)}] += population_n;
263256
for (size_t region_m = 0; region_m < number_regions; ++region_m) {

cpp/models/ode_seir_metapop/parameters.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ class Parameters : public ParametersBase<FP>
184184
bool apply_constraints()
185185
{
186186
double tol_times = 1e-1;
187-
int corrected = false;
187+
int corrected = false;
188188

189189
for (auto i = AgeGroup(0); i < AgeGroup(m_num_agegroups); i++) {
190190
if (this->template get<TimeExposed<FP>>()[i] < tol_times) {
191191
log_warning(
192-
"Constraint check: Parameter TimeInfected changed from {:.4f} to {:.4f}. Please note that "
192+
"Constraint check: Parameter TimeInfected changed from {} to {}. Please note that "
193193
"unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
194194
"and reset parameters.",
195195
this->template get<TimeExposed<FP>>()[i], tol_times);
@@ -198,7 +198,7 @@ class Parameters : public ParametersBase<FP>
198198
}
199199
if (this->template get<TimeInfected<FP>>()[i] < tol_times) {
200200
log_warning(
201-
"Constraint check: Parameter TimeInfected changed from {:.4f} to {:.4f}. Please note that "
201+
"Constraint check: Parameter TimeInfected changed from {} to {}. Please note that "
202202
"unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
203203
"and reset parameters.",
204204
this->template get<TimeInfected<FP>>()[i], tol_times);
@@ -207,16 +207,15 @@ class Parameters : public ParametersBase<FP>
207207
}
208208
if (this->template get<TransmissionProbabilityOnContact<FP>>()[i] < 0.0 ||
209209
this->template get<TransmissionProbabilityOnContact<FP>>()[i] > 1.0) {
210-
log_warning(
211-
"Constraint check: Parameter TransmissionProbabilityOnContact changed from {:0.4f} to {:d} ",
212-
this->template get<TransmissionProbabilityOnContact<FP>>()[i], 0.0);
210+
log_warning("Constraint check: Parameter TransmissionProbabilityOnContact changed from {} to {} ",
211+
this->template get<TransmissionProbabilityOnContact<FP>>()[i], 0.0);
213212
this->template get<TransmissionProbabilityOnContact<FP>>() = 0.0;
214213
corrected = true;
215214
}
216215
for (auto j = Region(0); j < Region(m_num_regions); j++) {
217216
if (this->template get<PopulationAfterCommuting<FP>>()[{j, i}] <= 0.0) {
218217
log_warning(
219-
"Constraint check: Parameter PopulationAfterCommuting changed from {:.4f} to {:.4f}. Please "
218+
"Constraint check: Parameter PopulationAfterCommuting changed from {} to {}. Please "
220219
"note that this only prevents division by zero. Consider to cancel and reset parameters.",
221220
this->template get<PopulationAfterCommuting<FP>>()[{j, i}], 1.0);
222221
this->template get<PopulationAfterCommuting<FP>>()[{j, i}] = 1.0;
@@ -250,30 +249,30 @@ class Parameters : public ParametersBase<FP>
250249
for (auto i = AgeGroup(0); i < AgeGroup(m_num_agegroups); i++) {
251250
if (this->template get<TimeExposed<FP>>()[i] < tol_times) {
252251
log_error(
253-
"Constraint check: Parameter TimeExposed {:.4f} smaller or equal {:.4f}. Please note that "
252+
"Constraint check: Parameter TimeExposed {} smaller or equal {}. Please note that "
254253
"unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
255254
"and reset parameters.",
256255
this->template get<TimeExposed<FP>>()[i], 0.0);
257256
return true;
258257
}
259258
if (this->template get<TimeInfected<FP>>()[i] < tol_times) {
260259
log_error(
261-
"Constraint check: Parameter TimeInfected {:.4f} smaller or equal {:.4f}. Please note that "
260+
"Constraint check: Parameter TimeInfected {} smaller or equal {}. Please note that "
262261
"unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
263262
"and reset parameters.",
264263
this->template get<TimeInfected<FP>>()[i], 0.0);
265264
return true;
266265
}
267266
if (this->template get<TransmissionProbabilityOnContact<FP>>()[i] < 0.0 ||
268267
this->template get<TransmissionProbabilityOnContact<FP>>()[i] > 1.0) {
269-
log_error("Constraint check: Parameter TransmissionProbabilityOnContact {:.4f} smaller {:.4f} or "
268+
log_error("Constraint check: Parameter TransmissionProbabilityOnContact {} smaller {} or "
270269
"greater {:.4f}",
271270
this->template get<TransmissionProbabilityOnContact<FP>>()[i], 0.0, 1.0);
272271
return true;
273272
}
274273
for (auto j = Region(0); j < Region(m_num_regions); j++) {
275274
if (this->template get<PopulationAfterCommuting<FP>>()[{j, i}] <= 0.0) {
276-
log_error("Constraint check: Parameter PopulationAfterCommuting {:.4f} smaller or equal {:.4f}",
275+
log_error("Constraint check: Parameter PopulationAfterCommuting {} smaller or equal {}",
277276
this->template get<PopulationAfterCommuting<FP>>()[{j, i}], 0.0);
278277
return true;
279278
}

cpp/tests/test_odemetapop.cpp

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,21 @@ class ModelTestOdeMetapop : public testing::Test
3333
total_population_per_region = 1061000;
3434

3535
for (size_t i = 0; i < (size_t)model.parameters.get_num_regions(); i++) {
36-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
37-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Exposed)}] = 10000;
38-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
39-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Infected)}] = 1000;
40-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
41-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Recovered)}] = 1000;
42-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
43-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Susceptible)}] =
36+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
37+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Exposed)}] = 10000;
38+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
39+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Infected)}] = 1000;
40+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
41+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Recovered)}] = 1000;
42+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
43+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Susceptible)}] =
4444
total_population_per_region -
45-
model.populations[{
46-
mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
47-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Exposed)}] -
48-
model.populations[{
49-
mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
50-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Infected)}] -
51-
model.populations[{
52-
mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
53-
mio::regions::Region(i), mio::AgeGroup(0), mio::oseir::InfectionState::Recovered)}];
45+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
46+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Exposed)}] -
47+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
48+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Infected)}] -
49+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
50+
mio::regions::Region(i), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Recovered)}];
5451
}
5552
model.set_commuting_strengths();
5653
}
@@ -72,17 +69,13 @@ TEST_F(ModelTestOdeMetapop, checkPopulationConservation)
7269

7370
TEST_F(ModelTestOdeMetapop, compareWithPreviousRun)
7471
{
75-
mio::oseir::Parameters<ScalarType> local_params(1);
76-
77-
local_params.set<mio::oseir::TimeExposed<ScalarType>>(5.2);
78-
local_params.set<mio::oseir::TimeInfected<ScalarType>>(6);
79-
local_params.set<mio::oseir::TransmissionProbabilityOnContact<ScalarType>>(0.1);
80-
mio::ContactMatrixGroup& contact_matrix = local_params.get<mio::oseir::ContactPatterns<ScalarType>>();
72+
model.parameters.set<mio::oseirmetapop::TimeExposed<ScalarType>>(5.2);
73+
model.parameters.set<mio::oseirmetapop::TimeInfected<ScalarType>>(6);
74+
model.parameters.set<mio::oseirmetapop::TransmissionProbabilityOnContact<ScalarType>>(0.1);
75+
mio::ContactMatrixGroup& contact_matrix = model.parameters.get<mio::oseirmetapop::ContactPatterns<ScalarType>>();
8176
contact_matrix[0].get_baseline().setConstant(2.7);
8277
contact_matrix[0].add_damping(0.6, mio::SimulationTime(12.5));
8378

84-
model.set_local_parameters(local_params);
85-
8679
Eigen::MatrixXd mobility_data_commuter((size_t)model.parameters.get_num_regions(),
8780
(size_t)model.parameters.get_num_regions());
8881
mobility_data_commuter << 0., 0., 0., 1., 0.2, 0., 0.6, 0.2, 0., 0.5, 0.5, 0., 0., 0., 0., 1.;
@@ -155,8 +148,8 @@ TEST_F(ModelTestOdeMetapop, check_constraints_parameters)
155148
mobility_data_commuter(0, 3) = 1.;
156149
model.set_commuting_strengths(mobility_data_commuter);
157150
model.parameters.set<mio::oseirmetapop::PopulationAfterCommuting<ScalarType>>(
158-
mio::Populations<ScalarType, mio::regions::Region, mio::AgeGroup>(
159-
{mio::regions::Region(4), mio::AgeGroup(1)}, 0.));
151+
mio::Populations<ScalarType, mio::regions::Region, mio::AgeGroup>({mio::regions::Region(4), mio::AgeGroup(1)},
152+
0.));
160153
ASSERT_EQ(model.parameters.check_constraints(), 1);
161154

162155
// Nobody commutes to region 2 but everybody originating fron there commutes to other regions.
@@ -214,8 +207,8 @@ TEST_F(ModelTestOdeMetapop, apply_constraints_parameters)
214207
mobility_data_commuter(0, 3) = 1.;
215208
model.set_commuting_strengths(mobility_data_commuter);
216209
model.parameters.set<mio::oseirmetapop::PopulationAfterCommuting<ScalarType>>(
217-
mio::Populations<ScalarType, mio::regions::Region, mio::AgeGroup>(
218-
{mio::regions::Region(4), mio::AgeGroup(1)}, 0.));
210+
mio::Populations<ScalarType, mio::regions::Region, mio::AgeGroup>({mio::regions::Region(4), mio::AgeGroup(1)},
211+
0.));
219212
EXPECT_EQ(model.parameters.apply_constraints(), 1);
220213
EXPECT_NEAR((model.parameters.get<mio::oseirmetapop::PopulationAfterCommuting<ScalarType>>()[{
221214
mio::regions::Region(3), mio::AgeGroup(0)}]),
@@ -242,33 +235,29 @@ TEST(TestOdeMetapop, compareSEIR)
242235

243236
mio::oseirmetapop::Model<ScalarType> model(1, 1);
244237

245-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
246-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Exposed)}] = 10000;
247-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
248-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Infected)}] = 1000;
249-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
250-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Recovered)}] = 1000;
251-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
252-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Susceptible)}] =
238+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
239+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Exposed)}] = 10000;
240+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
241+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Infected)}] = 1000;
242+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
243+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Recovered)}] = 1000;
244+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
245+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Susceptible)}] =
253246
total_population -
254-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
255-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Exposed)}] -
256-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
257-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Infected)}] -
258-
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseir::InfectionState>(
259-
mio::regions::Region(0), mio::AgeGroup(0), mio::oseir::InfectionState::Recovered)}];
260-
261-
mio::oseir::Parameters<ScalarType> local_params(1);
262-
263-
local_params.set<mio::oseir::TimeExposed<ScalarType>>(5.2);
264-
local_params.set<mio::oseir::TimeInfected<ScalarType>>(6);
265-
local_params.set<mio::oseir::TransmissionProbabilityOnContact<ScalarType>>(0.1);
266-
mio::ContactMatrixGroup& contact_matrix = local_params.get<mio::oseir::ContactPatterns<ScalarType>>();
247+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
248+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Exposed)}] -
249+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
250+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Infected)}] -
251+
model.populations[{mio::Index<mio::regions::Region, mio::AgeGroup, mio::oseirmetapop::InfectionState>(
252+
mio::regions::Region(0), mio::AgeGroup(0), mio::oseirmetapop::InfectionState::Recovered)}];
253+
254+
model.parameters.set<mio::oseirmetapop::TimeExposed<ScalarType>>(5.2);
255+
model.parameters.set<mio::oseirmetapop::TimeInfected<ScalarType>>(6);
256+
model.parameters.set<mio::oseirmetapop::TransmissionProbabilityOnContact<ScalarType>>(0.1);
257+
mio::ContactMatrixGroup& contact_matrix = model.parameters.get<mio::oseirmetapop::ContactPatterns<ScalarType>>();
267258
contact_matrix[0].get_baseline().setConstant(2.7);
268259
contact_matrix[0].add_damping(0.6, mio::SimulationTime(12.5));
269260

270-
model.set_local_parameters(local_params);
271-
272261
model.set_commuting_strengths();
273262

274263
model.check_constraints();

0 commit comments

Comments
 (0)