LCOV - code coverage report
Current view: top level - models/sde_seirvv - parameters.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 82 82 100.0 %
Date: 2024-11-18 12:45:26 Functions: 10 10 100.0 %

          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_SEIRVV_PARAMETERS_H
      22             : #define MIO_SDE_SEIRVV_PARAMETERS_H
      23             : 
      24             : #include "memilio/utils/uncertain_value.h"
      25             : #include "memilio/epidemiology/contact_matrix.h"
      26             : #include "memilio/utils/parameter_set.h"
      27             : 
      28             : namespace mio
      29             : {
      30             : namespace sseirvv
      31             : {
      32             : 
      33             : /******************************************
      34             :  * Define Parameters of the SSEIRVV model *
      35             :  ******************************************/
      36             : 
      37             : /**
      38             :  * @brief Probability of getting infected from a contact 
      39             :  * with variant 1.
      40             :  */
      41             : struct TransmissionProbabilityOnContactV1 {
      42             :     using Type = UncertainValue<>;
      43          27 :     static Type get_default()
      44             :     {
      45          27 :         return Type(1.0);
      46             :     }
      47             :     static std::string name()
      48             :     {
      49             :         return "TransmissionProbabilityOnContactV1";
      50             :     }
      51             : };
      52             : 
      53             : /**
      54             :  * @brief Probability of getting infected from a contact 
      55             :  * with variant 2.
      56             :  */
      57             : struct TransmissionProbabilityOnContactV2 {
      58             :     using Type = UncertainValue<>;
      59          27 :     static Type get_default()
      60             :     {
      61          27 :         return Type(1.0);
      62             :     }
      63             :     static std::string name()
      64             :     {
      65             :         return "TransmissionProbabilityOnContactV2";
      66             :     }
      67             : };
      68             : 
      69             : /**
      70             :  * @brief The latent time of variant 1 in days.
      71             :  */
      72             : struct TimeExposedV1 {
      73             :     using Type = UncertainValue<ScalarType>;
      74          27 :     static Type get_default()
      75             :     {
      76          27 :         return Type(6.0);
      77             :     }
      78             :     static std::string name()
      79             :     {
      80             :         return "TimeExposedV1";
      81             :     }
      82             : };
      83             : 
      84             : /**
      85             :  * @brief The latent time of variant 2 in days.
      86             :  */
      87             : struct TimeExposedV2 {
      88             :     using Type = UncertainValue<ScalarType>;
      89          27 :     static Type get_default()
      90             :     {
      91          27 :         return Type(6.0);
      92             :     }
      93             :     static std::string name()
      94             :     {
      95             :         return "TimeExposedV2";
      96             :     }
      97             : };
      98             : 
      99             : /**
     100             :  * @brief The infectious time of variant 1 in days.
     101             :  */
     102             : struct TimeInfectedV1 {
     103             :     using Type = UncertainValue<ScalarType>;
     104          27 :     static Type get_default()
     105             :     {
     106          27 :         return Type(6.0);
     107             :     }
     108             :     static std::string name()
     109             :     {
     110             :         return "TimeInfectedV1";
     111             :     }
     112             : };
     113             : 
     114             : /**
     115             :  * @brief The infectious time of variant 2 in days.
     116             :  */
     117             : struct TimeInfectedV2 {
     118             :     using Type = UncertainValue<ScalarType>;
     119          27 :     static Type get_default()
     120             :     {
     121          27 :         return Type(6.0);
     122             :     }
     123             :     static std::string name()
     124             :     {
     125             :         return "TimeInfectedV2";
     126             :     }
     127             : };
     128             : 
     129             : /**
     130             :  * @brief The contact patterns within the society are modelled using a ContactMatrix.
     131             :  */
     132             : struct ContactPatterns {
     133             :     using Type = ContactMatrix;
     134          27 :     static Type get_default()
     135             :     {
     136          27 :         return Type{1};
     137             :     }
     138             :     static std::string name()
     139             :     {
     140             :         return "ContactPatterns";
     141             :     }
     142             : };
     143             : 
     144             : using ParametersBase = ParameterSet<TransmissionProbabilityOnContactV1, TransmissionProbabilityOnContactV2, 
     145             :     TimeExposedV1, TimeExposedV2, TimeInfectedV1, TimeInfectedV2, ContactPatterns>;
     146             : 
     147             : /**
     148             :  * @brief Parameters of stochastic SEIRVV model.
     149             :  */
     150             : class Parameters : public ParametersBase
     151             : {
     152             : public:
     153          27 :     Parameters()
     154          27 :         : ParametersBase()
     155             :     {
     156          27 :     }
     157             : 
     158             :     /**
     159             :      * @brief Checks whether all Parameters satisfy their corresponding constraints and applies them, if they do not.
     160             :      * Time spans cannot be negative and probabilities can only take values between [0,1]. 
     161             :      *
     162             :      * Attention: This function should be used with care. It is necessary for some test problems to run through quickly,
     163             :      *            but in a manual execution of an example, check_constraints() may be preferred. Note that the apply_constraints()
     164             :      *            function can and will not set Parameters to meaningful values in an epidemiological or virological context,
     165             :      *            as all models are designed to be transferable to multiple diseases. Consequently, only acceptable
     166             :      *            (like 0 or 1 for probabilities or small positive values for time spans) values are set here and a manual adaptation
     167             :      *            may often be necessary to have set meaningful values.
     168             :      *
     169             :      * @return Returns true if one ore more constraint were corrected, false otherwise.  
     170             :      */
     171          72 :     bool apply_constraints()
     172             :     {
     173          72 :         double tol_times = 1e-1;
     174             : 
     175          72 :         int corrected = false;
     176          72 :         if (this->get<TimeExposedV1>() < tol_times) {
     177          18 :             log_warning("Constraint check: Parameter TimeExposedV1 changed from {:.4f} to {:.4f}. Please note that "
     178             :                         "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     179             :                         "and reset parameters.",
     180           9 :                         this->get<TimeExposedV1>(), tol_times);
     181           9 :             this->get<TimeExposedV1>() = tol_times;
     182           9 :             corrected                  = true;
     183             :         }
     184          72 :         if (this->get<TimeExposedV2>() < tol_times) {
     185          36 :             log_warning("Constraint check: Parameter TimeExposedV2 changed from {:.4f} to {:.4f}. Please note that "
     186             :                         "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     187             :                         "and reset parameters.",
     188          18 :                         this->get<TimeExposedV2>(), tol_times);
     189          18 :             this->get<TimeExposedV2>() = tol_times;
     190          18 :             corrected                  = true;
     191             :         }
     192          72 :         if (this->get<TimeInfectedV1>() < tol_times) {
     193          18 :             log_warning("Constraint check: Parameter TimeInfectedV1 changed from {:.4f} to {:.4f}. Please note that "
     194             :                         "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     195             :                         "and reset parameters.",
     196           9 :                         this->get<TimeInfectedV1>(), tol_times);
     197           9 :             this->get<TimeInfectedV1>() = tol_times;
     198           9 :             corrected                   = true;
     199             :         }
     200          72 :         if (this->get<TimeInfectedV2>() < tol_times) {
     201          18 :             log_warning("Constraint check: Parameter TimeInfectedV2 changed from {:.4f} to {:.4f}. Please note that "
     202             :                         "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     203             :                         "and reset parameters.",
     204           9 :                         this->get<TimeInfectedV2>(), tol_times);
     205           9 :             this->get<TimeInfectedV2>() = tol_times;
     206           9 :             corrected                   = true;
     207             :         }
     208         144 :         if (this->get<TransmissionProbabilityOnContactV1>() < 0.0 ||
     209          72 :             this->get<TransmissionProbabilityOnContactV1>() > 1.0) {
     210          18 :             log_warning("Constraint check: Parameter TransmissionProbabilityOnContactV1 changed from {:0.4f} to {:d} ",
     211          18 :                         this->get<TransmissionProbabilityOnContactV1>(), 0.0);
     212           9 :             this->get<TransmissionProbabilityOnContactV1>() = 0.0;
     213           9 :             corrected                                       = true;
     214             :         }
     215         144 :         if (this->get<TransmissionProbabilityOnContactV2>() < 0.0 ||
     216          72 :             this->get<TransmissionProbabilityOnContactV2>() > 1.0) {
     217          18 :             log_warning("Constraint check: Parameter TransmissionProbabilityOnContactV2 changed from {:0.4f} to {:d} ",
     218          18 :                         this->get<TransmissionProbabilityOnContactV2>(), 0.0);
     219           9 :             this->get<TransmissionProbabilityOnContactV2>() = 0.0;
     220           9 :             corrected                                       = true;
     221             :         }
     222          72 :         return corrected;
     223             :     }
     224             : 
     225             :     /**
     226             :      * @brief Checks whether all Parameters satisfy their corresponding constraints and logs an error 
     227             :      * if constraints are not satisfied.
     228             :      * @return Returns true if one constraint is not satisfied, otherwise false.   
     229             :      */
     230          63 :     bool check_constraints() const
     231             :     {
     232          63 :         ScalarType tol_times = 1e-1;
     233             : 
     234          63 :         if (this->get<TimeExposedV1>() < tol_times) {
     235          18 :             log_error("Constraint check: Parameter TimeExposedV1 {:.4f} smaller or equal {:.4f}. Please note that "
     236             :                       "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     237             :                       "and reset parameters.",
     238          18 :                       this->get<TimeExposedV1>(), 0.0);
     239           9 :             return true;
     240             :         }
     241          54 :         if (this->get<TimeExposedV2>() < tol_times) {
     242          18 :             log_error("Constraint check: Parameter TimeExposedV2 {:.4f} smaller or equal {:.4f}. Please note that "
     243             :                       "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     244             :                       "and reset parameters.",
     245          18 :                       this->get<TimeExposedV2>(), 0.0);
     246           9 :             return true;
     247             :         }
     248          45 :         if (this->get<TimeInfectedV1>() < tol_times) {
     249          18 :             log_error("Constraint check: Parameter TimeInfectedV1 {:.4f} smaller or equal {:.4f}. Please note that "
     250             :                       "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     251             :                       "and reset parameters.",
     252          18 :                       this->get<TimeInfectedV1>(), 0.0);
     253           9 :             return true;
     254             :         }
     255          36 :         if (this->get<TimeInfectedV2>() < tol_times) {
     256          18 :             log_error("Constraint check: Parameter TimeInfectedV2 {:.4f} smaller or equal {:.4f}. Please note that "
     257             :                       "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     258             :                       "and reset parameters.",
     259          18 :                       this->get<TimeInfectedV2>(), 0.0);
     260           9 :             return true;
     261             :         }
     262          54 :         if (this->get<TransmissionProbabilityOnContactV1>() < 0.0 ||
     263          27 :             this->get<TransmissionProbabilityOnContactV1>() > 1.0) {
     264          18 :             log_error(
     265             :                 "Constraint check: Parameter TransmissionProbabilityOnContactV1 {:.4f} smaller {:.4f} or greater {:.4f}",
     266          18 :                 this->get<TransmissionProbabilityOnContactV1>(), 0.0, 1.0);
     267           9 :             return true;
     268             :         }
     269          36 :         if (this->get<TransmissionProbabilityOnContactV2>() < 0.0 ||
     270          18 :             this->get<TransmissionProbabilityOnContactV2>() > 1.0) {
     271          18 :             log_error(
     272             :                 "Constraint check: Parameter TransmissionProbabilityOnContactV2 {:.4f} smaller {:.4f} or greater {:.4f}",
     273          18 :                 this->get<TransmissionProbabilityOnContactV2>(), 0.0, 1.0);
     274           9 :             return true;
     275             :         }
     276           9 :         return false;
     277             :     }
     278             : 
     279             : private:
     280             :     Parameters(ParametersBase&& base)
     281             :         : ParametersBase(std::move(base))
     282             :     {
     283             :     }
     284             : 
     285             : public:
     286             :     /**
     287             :      * Deserialize an object of this class.
     288             :      * @see mio::deserialize
     289             :      */
     290             :     template <class IOContext>
     291             :     static IOResult<Parameters> deserialize(IOContext& io)
     292             :     {
     293             :         BOOST_OUTCOME_TRY(auto&& base, ParametersBase::deserialize(io));
     294             :         return success(Parameters(std::move(base)));
     295             :     }
     296             : };
     297             : 
     298             : } // namespace sseirvv
     299             : } // namespace mio
     300             : 
     301             : #endif // MIO_SDE_SEIRVV_PARAMETERS_H

Generated by: LCOV version 1.14