Skip to content

Commit 579ea34

Browse files
965 use int instead of unsigned int in lct infection state (#1002)
Ints instead of unsigned ints are now used in the LctInfectionState class. Co-authored-by: reneSchm <[email protected]>
1 parent 0986215 commit 579ea34

File tree

6 files changed

+48
-49
lines changed

6 files changed

+48
-49
lines changed

cpp/examples/lct_secir.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,59 +47,58 @@ int main()
4747
{50}, {10, 10, 5, 3, 2}, {20}, {10}};
4848

4949
// Assert that initial_populations has the right shape.
50-
if (initial_populations.size() != (int)LctState::InfectionState::Count) {
50+
if (initial_populations.size() != (size_t)LctState::InfectionState::Count) {
5151
mio::log_error("The number of vectors in initial_populations does not match the number of InfectionStates.");
5252
return 1;
5353
}
5454
if ((initial_populations[(int)LctState::InfectionState::Susceptible].size() !=
55-
LctState::get_num_subcompartments<LctState::InfectionState::Susceptible>()) ||
55+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::Susceptible>()) ||
5656
(initial_populations[(int)LctState::InfectionState::Exposed].size() !=
57-
LctState::get_num_subcompartments<LctState::InfectionState::Exposed>()) ||
57+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::Exposed>()) ||
5858
(initial_populations[(int)LctState::InfectionState::InfectedNoSymptoms].size() !=
59-
LctState::get_num_subcompartments<LctState::InfectionState::InfectedNoSymptoms>()) ||
59+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::InfectedNoSymptoms>()) ||
6060
(initial_populations[(int)LctState::InfectionState::InfectedSymptoms].size() !=
61-
LctState::get_num_subcompartments<LctState::InfectionState::InfectedSymptoms>()) ||
61+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::InfectedSymptoms>()) ||
6262
(initial_populations[(int)LctState::InfectionState::InfectedSevere].size() !=
63-
LctState::get_num_subcompartments<LctState::InfectionState::InfectedSevere>()) ||
63+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::InfectedSevere>()) ||
6464
(initial_populations[(int)LctState::InfectionState::InfectedCritical].size() !=
65-
LctState::get_num_subcompartments<LctState::InfectionState::InfectedCritical>()) ||
65+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::InfectedCritical>()) ||
6666
(initial_populations[(int)LctState::InfectionState::Recovered].size() !=
67-
LctState::get_num_subcompartments<LctState::InfectionState::Recovered>()) ||
67+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::Recovered>()) ||
6868
(initial_populations[(int)LctState::InfectionState::Dead].size() !=
69-
LctState::get_num_subcompartments<LctState::InfectionState::Dead>())) {
69+
(size_t)LctState::get_num_subcompartments<LctState::InfectionState::Dead>())) {
7070
mio::log_error("The length of at least one vector in initial_populations does not match the related number of "
7171
"subcompartments.");
7272
return 1;
7373
}
7474

7575
// Transfer the initial values in initial_populations to the vector init.
7676
Eigen::VectorXd init = Eigen::VectorXd::Zero(LctState::Count);
77-
init[(int)LctState::get_first_index<LctState::InfectionState::Susceptible>()] =
77+
init[LctState::get_first_index<LctState::InfectionState::Susceptible>()] =
7878
initial_populations[(int)LctState::InfectionState::Susceptible][0];
79-
for (unsigned int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::Exposed>(); i++) {
80-
init[(int)LctState::get_first_index<LctState::InfectionState::Exposed>() + i] =
79+
for (int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::Exposed>(); i++) {
80+
init[LctState::get_first_index<LctState::InfectionState::Exposed>() + i] =
8181
initial_populations[(int)LctState::InfectionState::Exposed][i];
8282
}
83-
for (unsigned int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedNoSymptoms>();
84-
i++) {
85-
init[(int)LctState::get_first_index<LctState::InfectionState::InfectedNoSymptoms>() + i] =
83+
for (int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedNoSymptoms>(); i++) {
84+
init[LctState::get_first_index<LctState::InfectionState::InfectedNoSymptoms>() + i] =
8685
initial_populations[(int)LctState::InfectionState::InfectedNoSymptoms][i];
8786
}
88-
for (unsigned int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedSymptoms>(); i++) {
89-
init[(int)LctState::get_first_index<LctState::InfectionState::InfectedSymptoms>() + i] =
87+
for (int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedSymptoms>(); i++) {
88+
init[LctState::get_first_index<LctState::InfectionState::InfectedSymptoms>() + i] =
9089
initial_populations[(int)LctState::InfectionState::InfectedSymptoms][i];
9190
}
92-
for (unsigned int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedSevere>(); i++) {
93-
init[(int)LctState::get_first_index<LctState::InfectionState::InfectedSevere>() + i] =
91+
for (int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedSevere>(); i++) {
92+
init[LctState::get_first_index<LctState::InfectionState::InfectedSevere>() + i] =
9493
initial_populations[(int)LctState::InfectionState::InfectedSevere][i];
9594
}
96-
for (unsigned int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedCritical>(); i++) {
97-
init[(int)LctState::get_first_index<LctState::InfectionState::InfectedCritical>() + i] =
95+
for (int i = 0; i < LctState::get_num_subcompartments<LctState::InfectionState::InfectedCritical>(); i++) {
96+
init[LctState::get_first_index<LctState::InfectionState::InfectedCritical>() + i] =
9897
initial_populations[(int)LctState::InfectionState::InfectedCritical][i];
9998
}
100-
init[(int)LctState::get_first_index<LctState::InfectionState::Recovered>()] =
99+
init[LctState::get_first_index<LctState::InfectionState::Recovered>()] =
101100
initial_populations[(int)LctState::InfectionState::Recovered][0];
102-
init[(int)LctState::get_first_index<LctState::InfectionState::Dead>()] =
101+
init[LctState::get_first_index<LctState::InfectionState::Dead>()] =
103102
initial_populations[(int)LctState::InfectionState::Dead][0];
104103

105104
// Initialize model.

cpp/memilio/epidemiology/lct_infection_state.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ namespace mio
3131
* @tparam Ns Number of subcompartments for each infection state defined in InfectionState.
3232
* The number of given template arguments must be equal to the entry Count from InfectionState.
3333
*/
34-
template <class InfectionStates, unsigned int... Ns>
34+
template <class InfectionStates, int... Ns>
3535
class LctInfectionState
3636
{
3737
public:
3838
using InfectionState = InfectionStates;
39-
static_assert((unsigned int)InfectionState::Count == sizeof...(Ns),
39+
static_assert((size_t)InfectionState::Count == sizeof...(Ns),
4040
"The number of integers provided as template parameters must be "
4141
"the same as the entry Count of InfectionState.");
4242

@@ -46,10 +46,10 @@ class LctInfectionState
4646
* @brief Gets the number of subcompartments in an infection state.
4747
*
4848
* @tparam State: Infection state for which the number of subcompartments should be returned.
49-
* @return Number of subcompartments for State.
49+
* @return Number of subcompartments for State. Returned value is always at least one.
5050
*/
5151
template <InfectionState State>
52-
static constexpr unsigned int get_num_subcompartments()
52+
static constexpr int get_num_subcompartments()
5353
{
5454
static_assert(State < InfectionState::Count, "State must be a a valid InfectionState.");
5555
return m_subcompartment_numbers[(int)State];
@@ -61,23 +61,24 @@ class LctInfectionState
6161
* In a simulation, the number of individuals in the subcompartments are stored in vectors.
6262
* Accordingly, the index of the first subcompartment of State in such a vector is returned.
6363
* @tparam State: Infection state for which the index should be returned.
64-
* @return Index of the first subcompartment for a vector with one entry per subcompartment.
64+
* @return Index of the first subcompartment for a vector with one entry per subcompartment.
65+
* Returned value is always non-negative.
6566
*/
6667
template <InfectionState State>
67-
static constexpr unsigned int get_first_index()
68+
static constexpr int get_first_index()
6869
{
6970
static_assert(State < InfectionState::Count, "State must be a a valid InfectionState.");
70-
unsigned int index = 0;
71+
int index = 0;
7172
for (int i = 0; i < (int)(State); i++) {
7273
index = index + m_subcompartment_numbers[i];
7374
}
7475
return index;
7576
}
7677

77-
static constexpr unsigned int Count{(... + Ns)};
78+
static constexpr int Count{(... + Ns)};
7879

7980
private:
80-
static constexpr const std::array<unsigned int, sizeof...(Ns)> m_subcompartment_numbers{
81+
static constexpr const std::array<int, sizeof...(Ns)> m_subcompartment_numbers{
8182
Ns...}; ///< Vector which defines the number of subcompartments for each infection state of InfectionState.
8283
};
8384

cpp/models/lct_secir/model.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ namespace lsecir
4343
* @tparam NumInfectedSevere The number of subcompartents used for the InfectedSevere compartment.
4444
* @tparam NumInfectedCritical The number of subcompartents used for the InfectedCritical compartment.
4545
*/
46-
template <unsigned int NumExposed, unsigned int NumInfectedNoSymptoms, unsigned int NumInfectedSymptoms,
47-
unsigned int NumInfectedSevere, unsigned int NumInfectedCritical>
46+
template <int NumExposed, int NumInfectedNoSymptoms, int NumInfectedSymptoms, int NumInfectedSevere,
47+
int NumInfectedCritical>
4848
class Model
4949
{
5050

@@ -75,7 +75,7 @@ class Model
7575
log_error("Size of the initial values does not match subcompartments.");
7676
return true;
7777
}
78-
for (unsigned int i = 0; i < LctState::Count; i++) {
78+
for (int i = 0; i < LctState::Count; i++) {
7979
if (m_initial_values[i] < 0) {
8080
log_warning(
8181
"Initial values for one subcompartment are less than zero. Simulation results are not realistic.");
@@ -291,7 +291,7 @@ class Model
291291
void set_initial_values(Eigen::VectorXd init)
292292
{
293293
m_initial_values = init;
294-
m_N0 = m_initial_values.sum();
294+
m_N0 = m_initial_values.sum();
295295
}
296296

297297
Parameters parameters{}; ///< Parameters of the model.

cpp/models/lct_secir/parameters_io.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ IOResult<void> set_initial_data_from_confirmed_cases(Model& model, const std::st
118118
int idxInfectedNoSymptoms_last =
119119
LctState::template get_first_index<InfectionState::InfectedNoSymptoms>() +
120120
LctState::template get_num_subcompartments<InfectionState::InfectedNoSymptoms>() - 1;
121-
for (int i = 0;
122-
i < (int)LctState::template get_num_subcompartments<InfectionState::InfectedNoSymptoms>(); i++) {
121+
for (int i = 0; i < LctState::template get_num_subcompartments<InfectionState::InfectedNoSymptoms>();
122+
i++) {
123123
if (offset == std::floor(i * timeInfectedNoSymptoms_i)) {
124124
init[idxInfectedNoSymptoms_last - i] -=
125125
(1 - (i * timeInfectedNoSymptoms_i - std::floor(i * timeInfectedNoSymptoms_i))) *
@@ -152,7 +152,7 @@ IOResult<void> set_initial_data_from_confirmed_cases(Model& model, const std::st
152152
// Index of the last subcompartment of Exposed.
153153
int idxExposed_last = LctState::template get_first_index<InfectionState::Exposed>() +
154154
LctState::template get_num_subcompartments<InfectionState::Exposed>() - 1;
155-
for (int i = 0; i < (int)LctState::template get_num_subcompartments<InfectionState::Exposed>(); i++) {
155+
for (int i = 0; i < LctState::template get_num_subcompartments<InfectionState::Exposed>(); i++) {
156156
if (offset == std::floor(timeInfectedNoSymptoms + i * timeExposed_i)) {
157157
init[idxExposed_last - i] -= (1 - (timeInfectedNoSymptoms + i * timeExposed_i -
158158
std::floor(timeInfectedNoSymptoms + i * timeExposed_i))) *
@@ -185,7 +185,7 @@ IOResult<void> set_initial_data_from_confirmed_cases(Model& model, const std::st
185185
(ScalarType)LctState::template get_num_subcompartments<InfectionState::InfectedSymptoms>();
186186
// Index of the first subcompartment of InfectedSymptoms.
187187
int idxInfectedSymptoms_first = LctState::template get_first_index<InfectionState::InfectedSymptoms>();
188-
for (int i = 0; i < (int)LctState::template get_num_subcompartments<InfectionState::InfectedSymptoms>();
188+
for (int i = 0; i < LctState::template get_num_subcompartments<InfectionState::InfectedSymptoms>();
189189
i++) {
190190
if (offset == std::floor(-timeInfectedSymptoms_i * (i + 1))) {
191191
init[idxInfectedSymptoms_first + i] -=
@@ -221,8 +221,7 @@ IOResult<void> set_initial_data_from_confirmed_cases(Model& model, const std::st
221221
ScalarType prob_SeverePerInfectedSymptoms = model.parameters.template get<SeverePerInfectedSymptoms>();
222222
// Index of the first subcompartment of InfectedSevere.
223223
int idxInfectedSevere_first = LctState::template get_first_index<InfectionState::InfectedSevere>();
224-
for (int i = 0; i < (int)LctState::template get_num_subcompartments<InfectionState::InfectedSevere>();
225-
i++) {
224+
for (int i = 0; i < LctState::template get_num_subcompartments<InfectionState::InfectedSevere>(); i++) {
226225
if (offset == std::floor(-timeInfectedSymptoms - timeInfectedSevere_i * (i + 1))) {
227226
init[idxInfectedSevere_first + i] -=
228227
prob_SeverePerInfectedSymptoms *
@@ -265,7 +264,7 @@ IOResult<void> set_initial_data_from_confirmed_cases(Model& model, const std::st
265264
ScalarType prob_CriticalPerSevere = model.parameters.template get<CriticalPerSevere>();
266265
// Index of the first subcompartment of InfectedCritical.
267266
int idxInfectedCritical_first = LctState::template get_first_index<InfectionState::InfectedCritical>();
268-
for (int i = 0; i < (int)LctState::template get_num_subcompartments<InfectionState::InfectedCritical>();
267+
for (int i = 0; i < LctState::template get_num_subcompartments<InfectionState::InfectedCritical>();
269268
i++) {
270269
if (offset ==
271270
std::floor(-timeInfectedSymptoms - timeInfectedSevere - timeInfectedCritical_i * (i + 1))) {

cpp/tests/test_lct_parameters_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ TEST(TestLCTParametersIo, ReadPopulationDataRKI)
6767
Eigen::VectorXd compare(LctState::Count);
6868
compare << 863.05, 14.30625, 8.53125, 30.1125, 36.1875, 3.8125, 9.88, 3.52, 0.09, 0.25, 0.6888, 27.8712, 1.7;
6969

70-
for (unsigned int i = 0; i < LctState::Count; i++) {
70+
for (int i = 0; i < LctState::Count; i++) {
7171
EXPECT_NEAR(model.get_initial_values()[i], compare[i], 1e-4) << "at subcompartment number " << i;
7272
}
7373
}

cpp/tests/test_lct_secir.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ TEST(TestLCTSecir, testEvalRightHandSide)
226226
model.parameters.get<mio::lsecir::DeathsPerCritical>() = 0.3;
227227

228228
// Compare the result of eval_right_hand_side() with a hand calculated result.
229-
unsigned int num_subcompartments = LctState::Count;
229+
int num_subcompartments = LctState::Count;
230230
Eigen::VectorXd dydt(num_subcompartments);
231231
model.eval_right_hand_side(model.get_initial_values(), 0, dydt);
232232

233233
Eigen::VectorXd compare(num_subcompartments);
234234
compare << -15.3409, -3.4091, 6.25, -17.5, 15, 0, 3.3052, 3.4483, -7.0417, 6.3158, -2.2906, -2.8169, 12.3899,
235235
1.6901;
236236

237-
for (unsigned int i = 0; i < num_subcompartments; i++) {
237+
for (int i = 0; i < num_subcompartments; i++) {
238238
ASSERT_NEAR(compare[i], dydt[i], 1e-3);
239239
}
240240
}
@@ -460,17 +460,17 @@ TEST(TestLCTSecir, testConstraints)
460460
using LctState = Model::LctState;
461461

462462
// Check wrong size of initial value vector.
463-
Model model1(std::move(Eigen::VectorXd::Ones((int)LctState::Count - 1)), std::move(parameters_lct));
463+
Model model1(std::move(Eigen::VectorXd::Ones(LctState::Count - 1)), std::move(parameters_lct));
464464
constraint_check = model1.check_constraints();
465465
EXPECT_TRUE(constraint_check);
466466

467467
// Check with values smaller than zero.
468-
Model model2(std::move(Eigen::VectorXd::Constant((int)LctState::Count, -1)), std::move(parameters_lct));
468+
Model model2(std::move(Eigen::VectorXd::Constant(LctState::Count, -1)), std::move(parameters_lct));
469469
constraint_check = model2.check_constraints();
470470
EXPECT_TRUE(constraint_check);
471471

472472
// Check with correct conditions.
473-
Model model3(std::move(Eigen::VectorXd::Constant((int)LctState::Count, 1)));
473+
Model model3(std::move(Eigen::VectorXd::Constant(LctState::Count, 1)));
474474
constraint_check = model3.check_constraints();
475475
EXPECT_FALSE(constraint_check);
476476

0 commit comments

Comments
 (0)