Line data Source code
1 : /* 2 : * Copyright (C) 2020-2024 MEmilio 3 : * 4 : * Authors: Nils Wassmuth, Rene Schmieding, Martin J. Kuehn 5 : * 6 : * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> 7 : * 8 : * Licensed under the Apache License, Version 2.0 (the "License"); 9 : * you may not use this file except in compliance with the License. 10 : * You may obtain a copy of the License at 11 : * 12 : * http://www.apache.org/licenses/LICENSE-2.0 13 : * 14 : * Unless required by applicable law or agreed to in writing, software 15 : * distributed under the License is distributed on an "AS IS" BASIS, 16 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 : * See the License for the specific language governing permissions and 18 : * limitations under the License. 19 : */ 20 : 21 : #ifndef MIO_SDE_SIR_PARAMETERS_H 22 : #define MIO_SDE_SIR_PARAMETERS_H 23 : 24 : #include "memilio/epidemiology/contact_matrix.h" 25 : #include "memilio/utils/parameter_set.h" 26 : #include "memilio/utils/uncertain_value.h" 27 : 28 : namespace mio 29 : { 30 : namespace ssir 31 : { 32 : 33 : /******************************************* 34 : * Define Parameters of the SIR model * 35 : *******************************************/ 36 : 37 : /** 38 : * @brief probability of getting infected from a contact 39 : */ 40 : struct TransmissionProbabilityOnContact { 41 : using Type = UncertainValue<>; 42 27 : static Type get_default() 43 : { 44 27 : return Type(1.0); 45 : } 46 : static std::string name() 47 : { 48 : return "TransmissionProbabilityOnContact"; 49 : } 50 : }; 51 : 52 : /** 53 : * @brief the infectious time in day unit 54 : */ 55 : struct TimeInfected { 56 : using Type = UncertainValue<ScalarType>; 57 27 : static Type get_default() 58 : { 59 27 : return Type(6.0); 60 : } 61 : static std::string name() 62 : { 63 : return "TimeInfected"; 64 : } 65 : }; 66 : 67 : /** 68 : * @brief the contact patterns within the society are modelled using a ContactMatrix 69 : */ 70 : struct ContactPatterns { 71 : using Type = ContactMatrix; 72 27 : static Type get_default() 73 : { 74 27 : return Type{1}; 75 : } 76 : static std::string name() 77 : { 78 : return "ContactPatterns"; 79 : } 80 : }; 81 : 82 : using ParametersBase = ParameterSet<TransmissionProbabilityOnContact, TimeInfected, ContactPatterns>; 83 : 84 : /** 85 : * @brief Parameters of SIR model. 86 : */ 87 : class Parameters : public ParametersBase 88 : { 89 : public: 90 27 : Parameters() 91 27 : : ParametersBase() 92 : { 93 27 : } 94 : 95 : /** 96 : * @brief Checks whether all Parameters satisfy their corresponding constraints and applies them, if they do not. 97 : * Time spans cannot be negative and probabilities can only take values between [0,1]. 98 : * 99 : * Attention: This function should be used with care. It is necessary for some test problems to run through quickly, 100 : * but in a manual execution of an example, check_constraints() may be preferred. Note that the apply_constraints() 101 : * function can and will not set Parameters to meaningful values in an epidemiological or virological context, 102 : * as all models are designed to be transferable to multiple diseases. Consequently, only acceptable 103 : * (like 0 or 1 for probabilities or small positive values for time spans) values are set here and a manual adaptation 104 : * may often be necessary to have set meaningful values. 105 : * 106 : * @return Returns true if one ore more constraint were corrected, false otherwise. 107 : */ 108 27 : bool apply_constraints() 109 : { 110 27 : ScalarType tol_times = 1e-1; 111 : 112 27 : int corrected = false; 113 27 : if (this->get<TimeInfected>() < tol_times) { 114 18 : log_warning("Constraint check: Parameter TimeInfected changed from {:.4f} to {:.4f}. Please note that " 115 : "unreasonably small compartment stays lead to massively increased run time. Consider to cancel " 116 : "and reset parameters.", 117 9 : this->get<TimeInfected>(), tol_times); 118 9 : this->get<TimeInfected>() = tol_times; 119 9 : corrected = true; 120 : } 121 54 : if (this->get<TransmissionProbabilityOnContact>() < 0.0 || 122 27 : this->get<TransmissionProbabilityOnContact>() > 1.0) { 123 18 : log_warning("Constraint check: Parameter TransmissionProbabilityOnContact changed from {:0.4f} to {:d} ", 124 18 : this->get<TransmissionProbabilityOnContact>(), 0.0); 125 9 : this->get<TransmissionProbabilityOnContact>() = 0.0; 126 9 : corrected = true; 127 : } 128 27 : return corrected; 129 : } 130 : 131 : /** 132 : * @brief Checks whether all Parameters satisfy their corresponding constraints and logs an error 133 : * if constraints are not satisfied. 134 : * @return Returns true if one constraint is not satisfied, otherwise false. 135 : */ 136 27 : bool check_constraints() const 137 : { 138 27 : ScalarType tol_times = 1e-1; 139 : 140 27 : if (this->get<TimeInfected>() < tol_times) { 141 18 : log_error("Constraint check: Parameter TimeInfected {:.4f} smaller or equal {:.4f}. Please note that " 142 : "unreasonably small compartment stays lead to massively increased run time. Consider to cancel " 143 : "and reset parameters.", 144 18 : this->get<TimeInfected>(), 0.0); 145 9 : return true; 146 : } 147 36 : if (this->get<TransmissionProbabilityOnContact>() < 0.0 || 148 18 : this->get<TransmissionProbabilityOnContact>() > 1.0) { 149 18 : log_error( 150 : "Constraint check: Parameter TransmissionProbabilityOnContact {:.4f} smaller {:.4f} or greater {:.4f}", 151 18 : this->get<TransmissionProbabilityOnContact>(), 0.0, 1.0); 152 9 : return true; 153 : } 154 9 : return false; 155 : } 156 : 157 : private: 158 : Parameters(ParametersBase&& base) 159 : : ParametersBase(std::move(base)) 160 : { 161 : } 162 : 163 : public: 164 : /** 165 : * deserialize an object of this class. 166 : * @see mio::deserialize 167 : */ 168 : template <class IOContext> 169 : static IOResult<Parameters> deserialize(IOContext& io) 170 : { 171 : BOOST_OUTCOME_TRY(auto&& base, ParametersBase::deserialize(io)); 172 : return success(Parameters(std::move(base))); 173 : } 174 : }; 175 : 176 : } // namespace ssir 177 : } // namespace mio 178 : 179 : #endif // MIO_SDE_SIR_PARAMETERS_H