Perturbation.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * @file test/dynamics/Perturb.cpp
  3. *
  4. * This file is part of VLE, a framework for multi-modeling, simulation
  5. * and analysis of complex dynamical systems
  6. * http://www.vle-project.org
  7. *
  8. * Copyright (c) 2011 INRA http://www.inra.fr
  9. *
  10. * See the AUTHORS or Authors.txt file for copyright owners and contributors
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include <vle/extension/DifferentialEquation.hpp>
  26. #include <sstream>
  27. #include <math.h>
  28. #include <vle/devs/Dynamics.hpp>
  29. #include <vle/value/Value.hpp>
  30. #include <vle/utils/Exception.hpp>
  31. #include <vle/devs/DynamicsDbg.hpp>
  32. #include <iostream>
  33. namespace malariaspread {
  34. // namespace ved = vle::extension::differential_equation;
  35. namespace vv = vle::value;
  36. namespace vd = vle::devs;
  37. namespace vu = vle::utils;
  38. /**
  39. * @brief This dynamic simulates a perturbation by sending a message
  40. * at a given time + a given number of bags
  41. *
  42. * parameters :
  43. * - message: the map value message to send
  44. * - sendTime: the time to send the message
  45. * - nbBags (default = 0): the number of bags to wait at sendTime before
  46. * sending the message
  47. */
  48. class Perturbation: public vd::Dynamics
  49. {
  50. private:
  51. enum STATE {
  52. BEFORE_PERT,
  53. DURING_PERT,
  54. AFTER_PERT
  55. };
  56. STATE mstate;
  57. vv::Map message;
  58. vd::Time sendTime;
  59. unsigned int nbBags;
  60. unsigned int currentBag;
  61. public:
  62. Perturbation(const vd::DynamicsInit& init, const vd::InitEventList& events):
  63. vd::Dynamics(init, events), mstate(BEFORE_PERT),
  64. message(events.getMap("message")),
  65. sendTime(events.getDouble("sendTime")), nbBags(0), currentBag(0)
  66. { if(events.exist("nbBags")){
  67. nbBags = events.getInt("nbBags");
  68. }
  69. }
  70. virtual ~Perturbation()
  71. {
  72. }
  73. /**
  74. * @brief Implementation of Dynamics::init
  75. */
  76. vd::Time init(const vd::Time& /*time*/)
  77. {
  78. mstate = BEFORE_PERT;
  79. return sendTime;
  80. }
  81. /**
  82. * @brief Implementation of Dynamics::timeAdvance
  83. */
  84. vd::Time timeAdvance() const
  85. {
  86. switch(mstate){
  87. case BEFORE_PERT:
  88. return sendTime;
  89. break;
  90. case DURING_PERT:
  91. return 0;
  92. break;
  93. case AFTER_PERT:
  94. return vd::infinity;
  95. break;
  96. default:
  97. return 0;
  98. }
  99. }
  100. /**
  101. * @brief Implementation of Dynamics::internalTransition
  102. */
  103. void internalTransition(const vd::Time& /* time */)
  104. {
  105. switch(mstate){
  106. case BEFORE_PERT: {
  107. if(nbBags == 0){
  108. mstate = AFTER_PERT;
  109. } else {
  110. mstate = DURING_PERT;
  111. }
  112. break;
  113. } case DURING_PERT: {
  114. currentBag++;
  115. if(currentBag == nbBags){
  116. mstate = AFTER_PERT;
  117. } else {
  118. mstate = DURING_PERT;
  119. }
  120. break;
  121. } case AFTER_PERT: {
  122. mstate = AFTER_PERT;
  123. break;
  124. }}
  125. }
  126. /**
  127. * @brief Implementation of Dynamics::output
  128. */
  129. void output(const vd::Time& /* time */, vd::ExternalEventList& output) const
  130. {
  131. switch(mstate){
  132. case BEFORE_PERT: {
  133. if(nbBags == 0){
  134. vd::ExternalEvent* ee = new vd::ExternalEvent("p");
  135. ee->putAttributes(message);
  136. output.push_back(ee);
  137. }
  138. break;
  139. } case DURING_PERT: {
  140. if(currentBag == nbBags){
  141. vd::ExternalEvent* ee = new vd::ExternalEvent("p");
  142. ee->putAttributes(message);
  143. output.push_back(ee);
  144. }
  145. break;
  146. } case AFTER_PERT: {
  147. break;
  148. }}
  149. }
  150. /**
  151. * @brief Implementation of Dynamics::externalTransition
  152. */
  153. void externalTransition(const vd::ExternalEventList& /*event*/,
  154. const vd::Time& /* time */)
  155. {
  156. throw vu::ArgError(vle::fmt(_(
  157. "[%1%] Model that does not handle external events "))
  158. % getModelName());
  159. }
  160. /**
  161. * @brief Implementation of Dynamics::observation
  162. */
  163. vv::Value* observation(const vd::ObservationEvent& /*event*/) const
  164. {
  165. return 0;
  166. }
  167. };
  168. } // namespace malariaspread
  169. DECLARE_DYNAMICS(malariaspread::Perturbation)
  170. //DECLARE_DYNAMICS_DBG(malariaspread::Perturbation)