LCOV - code coverage report
Current view: top level - models/sde_sirs - parameters.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 47 100.0 %
Date: 2024-11-18 12:45:26 Functions: 7 7 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_SIRS_PARAMETERS_H
      22             : #define MIO_SDE_SIRS_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 ssirs
      31             : {
      32             : 
      33             : /***************************************
      34             :  * Define Parameters of the SIRS 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<>;
      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 infectious time in day unit
      69             :  */
      70             : struct TimeImmune {
      71             :     using Type = UncertainValue<>;
      72          27 :     static Type get_default()
      73             :     {
      74          27 :         return Type(6.0);
      75             :     }
      76             :     static std::string name()
      77             :     {
      78             :         return "TimeImmune";
      79             :     }
      80             : };
      81             : 
      82             : /**
      83             :  * @brief the contact patterns within the society are modelled using a ContactMatrix
      84             :  */
      85             : struct ContactPatterns {
      86             :     using Type = ContactMatrix;
      87          27 :     static Type get_default()
      88             :     {
      89          27 :         return Type{1};
      90             :     }
      91             :     static std::string name()
      92             :     {
      93             :         return "ContactPatterns";
      94             :     }
      95             : };
      96             : 
      97             : using ParametersBase = ParameterSet<TransmissionProbabilityOnContact, TimeInfected, ContactPatterns, TimeImmune>;
      98             : 
      99             : /**
     100             :  * @brief Parameters of SIR model.
     101             :  */
     102             : class Parameters : public ParametersBase
     103             : {
     104             : public:
     105          27 :     Parameters()
     106          27 :         : ParametersBase()
     107             :     {
     108          27 :     }
     109             : 
     110             :     /**
     111             :      * @brief Checks whether all Parameters satisfy their corresponding constraints and applies them, if they do not.
     112             :      * Time spans cannot be negative and probabilities can only take values between [0,1]. 
     113             :      *
     114             :      * Attention: This function should be used with care. It is necessary for some test problems to run through quickly,
     115             :      *            but in a manual execution of an example, check_constraints() may be preferred. Note that the apply_constraints()
     116             :      *            function can and will not set Parameters to meaningful values in an epidemiological or virological context,
     117             :      *            as all models are designed to be transferable to multiple diseases. Consequently, only acceptable
     118             :      *            (like 0 or 1 for probabilities or small positive values for time spans) values are set here and a manual adaptation
     119             :      *            may often be necessary to have set meaningful values.
     120             :      *
     121             :      * @return Returns true if one ore more constraint were corrected, false otherwise.  
     122             :      */
     123          36 :     bool apply_constraints()
     124             :     {
     125          36 :         ScalarType tol_times = 1e-1;
     126             : 
     127          36 :         int corrected = false;
     128          36 :         if (this->get<TimeInfected>() < tol_times) {
     129          18 :             log_warning("Constraint check: Parameter TimeInfected changed from {:.4f} to {:.4f}. Please note that "
     130             :                         "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     131             :                         "and reset parameters.",
     132           9 :                         this->get<TimeInfected>(), tol_times);
     133           9 :             this->get<TimeInfected>() = tol_times;
     134           9 :             corrected                 = true;
     135             :         }
     136          36 :         if (this->get<TimeImmune>() < tol_times) {
     137          18 :             log_warning("Constraint check: Parameter TimeInfected changed from {:.4f} to {:.4f}. Please note that "
     138             :                         "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     139             :                         "and reset parameters.",
     140           9 :                         this->get<TimeImmune>(), tol_times);
     141           9 :             this->get<TimeImmune>() = tol_times;
     142           9 :             corrected               = true;
     143             :         }
     144          72 :         if (this->get<TransmissionProbabilityOnContact>() < 0.0 ||
     145          36 :             this->get<TransmissionProbabilityOnContact>() > 1.0) {
     146          18 :             log_warning("Constraint check: Parameter TransmissionProbabilityOnContact changed from {:0.4f} to {:d} ",
     147          18 :                         this->get<TransmissionProbabilityOnContact>(), 0.0);
     148           9 :             this->get<TransmissionProbabilityOnContact>() = 0.0;
     149           9 :             corrected                                     = true;
     150             :         }
     151          36 :         return corrected;
     152             :     }
     153             : 
     154             :     /**
     155             :      * @brief Checks whether all Parameters satisfy their corresponding constraints and logs an error 
     156             :      * if constraints are not satisfied.
     157             :      * @return Returns true if one constraint is not satisfied, otherwise false.   
     158             :      */
     159          36 :     bool check_constraints() const
     160             :     {
     161          36 :         ScalarType tol_times = 1e-1;
     162             : 
     163          36 :         if (this->get<TimeInfected>() < tol_times) {
     164          18 :             log_error("Constraint check: Parameter TimeInfected {:.4f} smaller or equal {:.4f}. Please note that "
     165             :                       "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     166             :                       "and reset parameters.",
     167          18 :                       this->get<TimeInfected>(), 0.0);
     168           9 :             return true;
     169             :         }
     170          27 :         if (this->get<TimeImmune>() < tol_times) {
     171          18 :             log_error("Constraint check: Parameter TimeInfected {:.4f} smaller or equal {:.4f}. Please note that "
     172             :                       "unreasonably small compartment stays lead to massively increased run time. Consider to cancel "
     173             :                       "and reset parameters.",
     174          18 :                       this->get<TimeImmune>(), 0.0);
     175           9 :             return true;
     176             :         }
     177          36 :         if (this->get<TransmissionProbabilityOnContact>() < 0.0 ||
     178          18 :             this->get<TransmissionProbabilityOnContact>() > 1.0) {
     179          18 :             log_error(
     180             :                 "Constraint check: Parameter TransmissionProbabilityOnContact {:.4f} smaller {:.4f} or greater {:.4f}",
     181          18 :                 this->get<TransmissionProbabilityOnContact>(), 0.0, 1.0);
     182           9 :             return true;
     183             :         }
     184           9 :         return false;
     185             :     }
     186             : 
     187             : private:
     188             :     Parameters(ParametersBase&& base)
     189             :         : ParametersBase(std::move(base))
     190             :     {
     191             :     }
     192             : 
     193             : public:
     194             :     /**
     195             :      * deserialize an object of this class.
     196             :      * @see mio::deserialize
     197             :      */
     198             :     template <class IOContext>
     199             :     static IOResult<Parameters> deserialize(IOContext& io)
     200             :     {
     201             :         BOOST_OUTCOME_TRY(auto&& base, ParametersBase::deserialize(io));
     202             :         return success(Parameters(std::move(base)));
     203             :     }
     204             : };
     205             : 
     206             : } // namespace ssirs
     207             : } // namespace mio
     208             : 
     209             : #endif // MIO_SDE_SIRS_PARAMETERS_H

Generated by: LCOV version 1.14