-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_glct_secir.cpp
More file actions
executable file
·533 lines (495 loc) · 33.6 KB
/
test_glct_secir.cpp
File metadata and controls
executable file
·533 lines (495 loc) · 33.6 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* Copyright (C) 2020-2026 MEmilio
*
* Authors: Lena Ploetzke
*
* 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 "glct_secir/model.h"
#include "glct_secir/parameters.h"
#include "memilio/config.h"
#include "memilio/utils/time_series.h"
#include "memilio/utils/logging.h"
#include "memilio/epidemiology/uncertain_matrix.h"
#include "memilio/compartments/simulation.h"
#include "memilio/math/eigen.h"
#include "load_test_data.h"
#include <gtest/gtest.h>
#include "boost/numeric/odeint/stepper/runge_kutta_cash_karp54.hpp"
// Test if the function eval_right_hand_side() is working using a hand calculated result.
TEST(TestGLCTSecir, testEvalRightHandSide)
{
// Define initial values, parameters and numbers of subcompartments according to the choices of the
// testEvalRightHandSide of the LCT testing suite. For more details,
// we refer to the example glct_secir.cpp.
using Model = mio::glsecir::Model<ScalarType, 2, 6, 4, 4, 4>;
using LctState = Model::LctState;
using InfectionState = LctState::InfectionState;
Model model;
// Set parameters such that the stay times are Erlang-distributed as in the corresponding LCT model.
// Exposed.
// Default functions are used to set the parameters but the corresponding dimensions have to be set manually.
model.parameters.get<mio::glsecir::StartingProbabilitiesExposed<ScalarType>>() =
mio::glsecir::StartingProbabilitiesExposed<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::Exposed>());
model.parameters.get<mio::glsecir::TransitionMatrixExposedToInfectedNoSymptoms<ScalarType>>() =
mio::glsecir::TransitionMatrixExposedToInfectedNoSymptoms<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::Exposed>(), 3.2);
// InfectedNoSymptoms.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedNoSymptoms = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>());
StartingProbabilitiesInfectedNoSymptoms[0] = 1 - 0.09;
StartingProbabilitiesInfectedNoSymptoms[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>() / 2.)] = 0.09;
model.parameters.get<mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>>() =
StartingProbabilitiesInfectedNoSymptoms;
model.parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToInfectedSymptoms<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedNoSymptomsToInfectedSymptoms<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>() / 2.), 2.);
model.parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedNoSymptomsToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>() / 2.), 2.);
// InfectedSymptoms.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedSymptoms = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>());
StartingProbabilitiesInfectedSymptoms[0] = 0.2;
StartingProbabilitiesInfectedSymptoms[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>() / 2.)] = 1 - 0.2;
model.parameters.get<mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>>() =
StartingProbabilitiesInfectedSymptoms;
model.parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToInfectedSevere<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSymptomsToInfectedSevere<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>() / 2.), 5.8);
model.parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSymptomsToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>() / 2.), 5.8);
// InfectedSevere.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedSevere = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedSevere>());
StartingProbabilitiesInfectedSevere[0] = 0.25;
StartingProbabilitiesInfectedSevere[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedSevere>() / 2.)] = 1 - 0.25;
model.parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>() =
StartingProbabilitiesInfectedSevere;
model.parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToInfectedCritical<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSevereToInfectedCritical<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSevere>() / 2.), 9.5);
model.parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSevereToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSevere>() / 2.), 9.5);
// InfectedCritical.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedCritical = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedCritical>());
StartingProbabilitiesInfectedCritical[0] = 0.3;
StartingProbabilitiesInfectedCritical[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.)] = 1 - 0.3;
model.parameters.get<mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>>() =
StartingProbabilitiesInfectedCritical;
model.parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToDead<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedCriticalToDead<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.), 7.1);
model.parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.), 7.1);
model.parameters.get<mio::glsecir::TransmissionProbabilityOnContact<ScalarType>>() = 0.05;
mio::ContactMatrixGroup<ScalarType>& contact_matrix =
model.parameters.get<mio::glsecir::ContactPatterns<ScalarType>>();
contact_matrix[0] = mio::ContactMatrix<ScalarType>(Eigen::MatrixX<ScalarType>::Constant(1, 1, 10));
model.parameters.get<mio::glsecir::RelativeTransmissionNoSymptoms<ScalarType>>() = 0.7;
model.parameters.get<mio::glsecir::RiskOfInfectionFromSymptomatic<ScalarType>>() = 0.25;
model.parameters.get<mio::glsecir::Seasonality<ScalarType>>() = 0.;
model.parameters.get<mio::glsecir::StartDay<ScalarType>>() = 0;
// Define initial population distribution in infection states, one entry per subcompartment.
std::vector<std::vector<ScalarType>> initial_populations = {
{750},
{30, 20},
{20 * StartingProbabilitiesInfectedNoSymptoms[0], 10 * StartingProbabilitiesInfectedNoSymptoms[0],
10 * StartingProbabilitiesInfectedNoSymptoms[0], 20 * (1 - StartingProbabilitiesInfectedNoSymptoms[0]),
10 * (1 - StartingProbabilitiesInfectedNoSymptoms[0]), 10 * (1 - StartingProbabilitiesInfectedNoSymptoms[0])},
{30 * StartingProbabilitiesInfectedSymptoms[0], 20 * StartingProbabilitiesInfectedSymptoms[0],
30 * (1 - StartingProbabilitiesInfectedSymptoms[0]), 20 * (1 - StartingProbabilitiesInfectedSymptoms[0])},
{40 * StartingProbabilitiesInfectedSevere[0], 10 * StartingProbabilitiesInfectedSevere[0],
40 * (1 - StartingProbabilitiesInfectedSevere[0]), 10 * (1 - StartingProbabilitiesInfectedSevere[0])},
{10 * StartingProbabilitiesInfectedCritical[0], 20 * StartingProbabilitiesInfectedCritical[0],
10 * (1 - StartingProbabilitiesInfectedCritical[0]), 20 * (1 - StartingProbabilitiesInfectedCritical[0])},
{20},
{10}};
std::vector<ScalarType> flat_initial_populations;
for (auto&& vec : initial_populations) {
flat_initial_populations.insert(flat_initial_populations.end(), vec.begin(), vec.end());
}
Eigen::VectorX<ScalarType> pop(LctState::Count);
for (size_t i = 0; i < LctState::Count; i++) {
pop[i] = flat_initial_populations[i];
}
// Compare the result of get_derivatives() with a hand calculated result.
Eigen::VectorX<ScalarType> dydt(LctState::Count);
model.get_derivatives(pop, pop, 0, dydt);
// This vector is the equivalent of the result defined in the test suite testEvalRightHandSide of the LCT model.
Eigen::VectorX<ScalarType> compare(LctState::Count);
compare << -15.3409, -3.4091, 6.25, -17.5 * 0.91, 15 * 0.91, 0 * 0.91, -17.5 * 0.09, 15 * 0.09, 0 * 0.09,
3.3052 * 0.2, 3.4483 * 0.2, 3.3052 * 0.8, 3.4483 * 0.8, -7.0417 * 0.25, 6.3158 * 0.25, -7.0417 * 0.75,
6.3158 * 0.75, -2.2906 * 0.3, -2.8169 * 0.3, -2.2906 * 0.7, -2.8169 * 0.7, 12.3899, 1.6901;
for (size_t i = 0; i < LctState::Count; i++) {
EXPECT_NEAR(compare[i], dydt[i], 1e-3) << "Condition failed at index: " << i;
}
}
// Model setup to compare result with a previous output of an LCT model.
class ModelTestGLCTSecir : public testing::Test
{
public:
using Model = mio::glsecir::Model<ScalarType, 2, 6, 2, 2, 10>;
using LctState = Model::LctState;
using InfectionState = LctState::InfectionState;
protected:
virtual void SetUp()
{
model = new Model();
// --- Set parameters. ---
// Exposed.
model->parameters.get<mio::glsecir::StartingProbabilitiesExposed<ScalarType>>() =
mio::glsecir::StartingProbabilitiesExposed<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::Exposed>());
model->parameters.get<mio::glsecir::TransitionMatrixExposedToInfectedNoSymptoms<ScalarType>>() =
mio::glsecir::TransitionMatrixExposedToInfectedNoSymptoms<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::Exposed>(), 3.2);
// InfectedNoSymptoms.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedNoSymptoms = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>());
StartingProbabilitiesInfectedNoSymptoms[0] = 1 - 0.09;
StartingProbabilitiesInfectedNoSymptoms[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>() / 2.)] = 0.09;
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>>() =
StartingProbabilitiesInfectedNoSymptoms;
model->parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToInfectedSymptoms<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedNoSymptomsToInfectedSymptoms<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>() / 2.), 2.);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedNoSymptomsToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>() / 2.), 2.);
// InfectedSymptoms.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedSymptoms = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>());
StartingProbabilitiesInfectedSymptoms[0] = 0.2;
StartingProbabilitiesInfectedSymptoms[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>() / 2.)] = 1 - 0.2;
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>>() =
StartingProbabilitiesInfectedSymptoms;
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToInfectedSevere<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSymptomsToInfectedSevere<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>() / 2.), 5.8);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSymptomsToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>() / 2.), 5.8);
// InfectedSevere.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedSevere = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedSevere>());
StartingProbabilitiesInfectedSevere[0] = 0.25;
StartingProbabilitiesInfectedSevere[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedSevere>() / 2.)] = 1 - 0.25;
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>() =
StartingProbabilitiesInfectedSevere;
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToInfectedCritical<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSevereToInfectedCritical<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSevere>() / 2.), 9.5);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedSevereToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedSevere>() / 2.), 9.5);
// InfectedCritical.
Eigen::VectorX<ScalarType> StartingProbabilitiesInfectedCritical = Eigen::VectorX<ScalarType>::Zero(
(Eigen::Index)LctState::get_num_subcompartments<InfectionState::InfectedCritical>());
StartingProbabilitiesInfectedCritical[0] = 0.3;
StartingProbabilitiesInfectedCritical[(
Eigen::Index)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.)] = 1 - 0.3;
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>>() =
StartingProbabilitiesInfectedCritical;
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToDead<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedCriticalToDead<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.), 7.1);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.), 7.1);
model->parameters.get<mio::glsecir::TransmissionProbabilityOnContact<ScalarType>>() = 0.05;
mio::ContactMatrixGroup<ScalarType>& contact_matrix =
model->parameters.get<mio::glsecir::ContactPatterns<ScalarType>>();
contact_matrix[0] = mio::ContactMatrix<ScalarType>(Eigen::MatrixX<ScalarType>::Constant(1, 1, 10));
contact_matrix[0].add_damping(0.7, mio::SimulationTime<ScalarType>(2.));
model->parameters.get<mio::glsecir::RelativeTransmissionNoSymptoms<ScalarType>>() = 0.7;
model->parameters.get<mio::glsecir::RiskOfInfectionFromSymptomatic<ScalarType>>() = 0.25;
model->parameters.get<mio::glsecir::Seasonality<ScalarType>>() = 0.;
model->parameters.get<mio::glsecir::StartDay<ScalarType>>() = 0;
// --- Define initial distribution of the population in the subcompartments. ---
std::vector<std::vector<ScalarType>> initial_populations = {
{750},
{30, 20},
{20 * StartingProbabilitiesInfectedNoSymptoms[0], 10 * StartingProbabilitiesInfectedNoSymptoms[0],
10 * StartingProbabilitiesInfectedNoSymptoms[0], 20 * (1 - StartingProbabilitiesInfectedNoSymptoms[0]),
10 * (1 - StartingProbabilitiesInfectedNoSymptoms[0]),
10 * (1 - StartingProbabilitiesInfectedNoSymptoms[0])},
{50 * StartingProbabilitiesInfectedSymptoms[0], 50 * (1 - StartingProbabilitiesInfectedSymptoms[0])},
{50 * StartingProbabilitiesInfectedSevere[0], 50 * (1 - StartingProbabilitiesInfectedSevere[0])},
{10 * StartingProbabilitiesInfectedCritical[0], 10 * StartingProbabilitiesInfectedCritical[0],
5 * StartingProbabilitiesInfectedCritical[0], 3 * StartingProbabilitiesInfectedCritical[0],
2 * StartingProbabilitiesInfectedCritical[0], 10 * (1 - StartingProbabilitiesInfectedCritical[0]),
10 * (1 - StartingProbabilitiesInfectedCritical[0]), 5 * (1 - StartingProbabilitiesInfectedCritical[0]),
3 * (1 - StartingProbabilitiesInfectedCritical[0]), 2 * (1 - StartingProbabilitiesInfectedCritical[0])},
{20},
{10}};
// Transfer the initial values in initial_populations to the model->
std::vector<ScalarType> flat_initial_populations;
for (auto&& vec : initial_populations) {
flat_initial_populations.insert(flat_initial_populations.end(), vec.begin(), vec.end());
}
for (size_t i = 0; i < LctState::Count; i++) {
model->populations[mio::Index<LctState>(i)] = flat_initial_populations[i];
}
}
virtual void TearDown()
{
delete model;
}
public:
Model* model = nullptr;
};
// Test compares a simulation with a previous output of an LCT model stored in a .csv file.
// This tests that the GLCT model reduces to an LCT model with the default parameters as well as nothing has changed
// in the implementation such that we still obtain an previous result.
TEST_F(ModelTestGLCTSecir, compareWithPreviousRun)
{
ScalarType tmax = 3;
mio::TimeSeries<ScalarType> result = mio::simulate<ScalarType, ModelTestGLCTSecir::Model>(
0, tmax, 0.5, *model,
std::make_unique<mio::ControlledStepperWrapper<ScalarType, boost::numeric::odeint::runge_kutta_cash_karp54>>());
// Compare InfectionState compartments.
mio::TimeSeries<ScalarType> population = model->calculate_compartments(result);
auto compare_population = load_test_data_csv<ScalarType>("lct-secir-compartments-compare.csv");
ASSERT_EQ(compare_population.size(), static_cast<size_t>(population.get_num_time_points()));
for (size_t i = 0; i < compare_population.size(); i++) {
ASSERT_EQ(compare_population[i].size(), static_cast<size_t>(population.get_num_elements()) + 1)
<< "at row " << i;
EXPECT_NEAR(population.get_time(i), compare_population[i][0], 1e-3) << "at row " << i;
for (size_t j = 1; j < compare_population[i].size(); j++) {
EXPECT_NEAR(population.get_value(i)[j - 1], compare_population[i][j], 1e-3) << " at row " << i;
}
}
}
// Check constraints of Parameters class.
TEST_F(ModelTestGLCTSecir, testConstraintsModel)
{
// Deactivate temporarily log output for next tests.
mio::set_log_level(mio::LogLevel::off);
// --- Check correct setup. ---
bool constraint_check = model->check_constraints();
EXPECT_FALSE(constraint_check);
// Check if the number of subcompartments does not match the dimension of the vector with StartingProbabilities.
Eigen::VectorX<ScalarType> wrong_size = Eigen::VectorX<ScalarType>::Zero(3);
wrong_size[0] = 1;
// Exposed.
model->parameters.get<mio::glsecir::StartingProbabilitiesExposed<ScalarType>>() = wrong_size;
constraint_check = model->check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesExposed<ScalarType>>() =
mio::glsecir::StartingProbabilitiesExposed<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::Exposed>());
// InfectedNoSymptoms.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>>() = wrong_size;
constraint_check = model->check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>());
// InfectedSymptoms.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>>() = wrong_size;
constraint_check = model->check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>());
// InfectedSevere.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>() = wrong_size;
constraint_check = model->check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedSevere>());
// InfectedCritical.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>>() = wrong_size;
constraint_check = model->check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedCritical>());
}
// Check constraints of Parameters class.
TEST_F(ModelTestGLCTSecir, testConstraintsParameters)
{
// Deactivate temporarily log output for next tests.
mio::set_log_level(mio::LogLevel::off);
// --- Check correct setup. ---
bool constraint_check = model->parameters.check_constraints();
EXPECT_FALSE(constraint_check);
// --- Parameters affecting the transmission of the virus. ---
// Check TransmissionProbabilityOnContact.
model->parameters.get<mio::glsecir::TransmissionProbabilityOnContact<ScalarType>>() = 5.1;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransmissionProbabilityOnContact<ScalarType>>() = 0.05;
// Check RelativeTransmissionNoSymptoms.
model->parameters.get<mio::glsecir::RelativeTransmissionNoSymptoms<ScalarType>>() = -0.05;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::RelativeTransmissionNoSymptoms<ScalarType>>() = 0.7;
// Check RiskOfInfectionFromSymptomatic.
model->parameters.get<mio::glsecir::RiskOfInfectionFromSymptomatic<ScalarType>>() = 1.1;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::RiskOfInfectionFromSymptomatic<ScalarType>>() = 0.25;
// Check Seasonality.
model->parameters.get<mio::glsecir::Seasonality<ScalarType>>() = 0.6;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::Seasonality<ScalarType>>() = 0.;
// --- Check with incorrect dimensions. ---
// Check non-quadratic TransitionMatrixInfectedCriticalToRecovered.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>() =
Eigen::MatrixX<ScalarType>::Zero(2, 3);
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>() =
mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>().get_default(
(size_t)(LctState::get_num_subcompartments<InfectionState::InfectedCritical>() / 2.), 7.1);
// Check non matching dimensions of TransitionMatrix and vector with StartingProbabilities.
Eigen::VectorX<ScalarType> wrong_size = Eigen::VectorX<ScalarType>::Zero(3);
wrong_size[0] = 1;
// Exposed.
model->parameters.get<mio::glsecir::StartingProbabilitiesExposed<ScalarType>>() = wrong_size;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesExposed<ScalarType>>() =
mio::glsecir::StartingProbabilitiesExposed<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::Exposed>());
// InfectedNoSymptoms.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>>() = wrong_size;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedNoSymptoms<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedNoSymptoms>());
// InfectedSymptoms.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>>() = wrong_size;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedSymptoms<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedSymptoms>());
// InfectedSevere.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>() = wrong_size;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedSevere>());
// InfectedCritical.
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>>() = wrong_size;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>>() =
mio::glsecir::StartingProbabilitiesInfectedCritical<ScalarType>().get_default(
LctState::get_num_subcompartments<InfectionState::InfectedCritical>());
// --- Check constraints of the starting probability vectors. ---
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>()[1] = 1.5;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>()[0] = 1.1;
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>()[1] = -0.1;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>()[0] = 1.;
model->parameters.get<mio::glsecir::StartingProbabilitiesInfectedSevere<ScalarType>>()[1] = 0.;
// --- Check with invalid transition matrices. ---
// ExposedToInfectedNoSymptoms.
model->parameters.get<mio::glsecir::TransitionMatrixExposedToInfectedNoSymptoms<ScalarType>>()(1, 0) = 10;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixExposedToInfectedNoSymptoms<ScalarType>>()(1, 0) = 0.1;
// InfectedNoSymptomsToInfectedSymptoms.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToInfectedSymptoms<ScalarType>>()(2, 1) = 50;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToInfectedSymptoms<ScalarType>>()(2, 1) = 0.1;
// InfectedNoSymptomsToRecovered.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToRecovered<ScalarType>>()(1, 1) = -1.45;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedNoSymptomsToRecovered<ScalarType>>()(1, 1) = -1.5;
// InfectedSymptomsToInfectedSevere.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToInfectedSevere<ScalarType>>()(0, 0) = 1.;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToInfectedSevere<ScalarType>>()(0, 0) = -1.;
// InfectedSymptomsToRecovered.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToRecovered<ScalarType>>()(0, 0) = 0.1;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSymptomsToRecovered<ScalarType>>()(0, 0) = -0.1;
// InfectedSevereToInfectedCritical.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToInfectedCritical<ScalarType>>()(0, 0) = 0.01;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToInfectedCritical<ScalarType>>()(0, 0) = -0.01;
// InfectedSevereToRecovered.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToRecovered<ScalarType>>()(0, 0) = 50;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedSevereToRecovered<ScalarType>>()(0, 0) = -0.1;
// InfectedCriticalToDead.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToDead<ScalarType>>()(3, 1) = 6;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToDead<ScalarType>>()(3, 1) = 0.;
// InfectedCriticalToRecovered.
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>()(0, 4) = 3;
constraint_check = model->parameters.check_constraints();
EXPECT_TRUE(constraint_check);
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>()(0, 0) = -3.;
model->parameters.get<mio::glsecir::TransitionMatrixInfectedCriticalToRecovered<ScalarType>>()(0, 4) = 1.2;
// --- Check with correct parameters. ---
constraint_check = model->parameters.check_constraints();
EXPECT_FALSE(constraint_check);
// Reactive log output.
mio::set_log_level(mio::LogLevel::warn);
}
// Test calculate_compartments with a TimeSeries that has an incorrect number of elements.
TEST_F(ModelTestGLCTSecir, testCalculatePopWrongSize)
{
// Deactivate temporarily log output because an error is expected.
mio::set_log_level(mio::LogLevel::off);
// TimeSeries has to have LctState::Count elements.
size_t wrong_size = LctState::Count - 2;
// Define TimeSeries with wrong_size elements.
mio::TimeSeries<ScalarType> wrong_num_elements(wrong_size);
Eigen::VectorX<ScalarType> vec_wrong_size = Eigen::VectorX<ScalarType>::Ones(wrong_size);
wrong_num_elements.add_time_point(-10, vec_wrong_size);
wrong_num_elements.add_time_point(-9, vec_wrong_size);
// Call the calculate_compartments function with the TimeSeries with a wrong number of elements.
mio::TimeSeries<ScalarType> population = model->calculate_compartments(wrong_num_elements);
// A TimeSeries of the right size with values -1 is expected.
ASSERT_EQ(1, population.get_num_time_points());
for (int i = 0; i < population.get_num_elements(); i++) {
EXPECT_EQ(-1, population.get_last_value()[i]);
}
// Reactive log output.
mio::set_log_level(mio::LogLevel::warn);
}