Meteo.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* @@tagdepends: @@endtagdepends
  2. @@tagdynamic@@ */
  3. #include <vle/devs/Dynamics.hpp>
  4. #include <vle/utils/Package.hpp>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <iostream>
  8. #include<boost/algorithm/string.hpp>
  9. #include<cstdlib>
  10. namespace malariaspread {
  11. class Meteo : public vle::devs::Dynamics
  12. {
  13. enum State { STATE1, STATE2 };
  14. // Phase du modèle
  15. State state;
  16. // Working variables
  17. std::ifstream file;
  18. std::string line;
  19. // Parameters
  20. std::string file_path;
  21. //! Variables d'état du modèle
  22. int year;
  23. int month;
  24. int day;
  25. double Tmin;
  26. double Tmax;
  27. double Tmoy;
  28. double Hmin;
  29. double Hmax;
  30. double Hmoy;
  31. void updateState()
  32. {
  33. if (file.good()) {
  34. getline(file, line);
  35. }
  36. std::stringstream ss(line);
  37. double x;
  38. std::vector <std::string> V;
  39. boost::split(V, line,boost::is_any_of(","));
  40. //std::cout<<year; std::cout<<"| "; std::cout<<month; std::cout<< "| "; std::cout<<day<<std::endl;
  41. year=atof(V[0].c_str());
  42. month=atof(V[1].c_str());
  43. day=atof(V[2].c_str());
  44. Tmax=atof(V[3].c_str());
  45. Tmoy=atof(V[4].c_str());
  46. Tmin=atof(V[5].c_str());
  47. x=atof(V[6].c_str());
  48. x=atof(V[7].c_str());
  49. x=atof(V[8].c_str());
  50. Hmax=atof(V[9].c_str());
  51. Hmoy=atof(V[10].c_str());
  52. Hmin=atof(V[11].c_str());
  53. //std::cout<<year; std::cout<<"| "; std::cout<<month; std::cout<< "| "; std::cout<<day<<std::endl;
  54. }
  55. public:
  56. Meteo(const vle::devs::DynamicsInit& model,
  57. const vle::devs::InitEventList& events) :
  58. vle::devs::Dynamics(model, events)
  59. {
  60. file_path = vle::value::toString(events.get("meteo_file"));
  61. }
  62. virtual ~Meteo()
  63. {
  64. }
  65. vle::devs::Time timeAdvance() const
  66. {
  67. switch (state) {
  68. case STATE2:
  69. return 1.0;
  70. case STATE1:
  71. return 0.0;
  72. default:
  73. return vle::devs::infinity;
  74. }
  75. }
  76. vle::devs::Time init(const vle::devs::Time& /*time*/)
  77. {
  78. vle::utils::Package p("malariaspread");
  79. state = STATE1;
  80. file.open(p.getDataFile(file_path).c_str());
  81. // skip first line (header)
  82. if (file.good()) {
  83. getline(file, line);
  84. }
  85. year = 0;
  86. month = 0;
  87. day = 0;
  88. Tmin = 0;
  89. Tmax = 0;
  90. Tmoy = 0;
  91. Hmin = 0;
  92. Hmax = 0;
  93. Hmoy = 0;
  94. return 0.0;
  95. }
  96. void internalTransition(const vle::devs::Time& /*time*/)
  97. {
  98. switch (state) {
  99. case STATE2 :
  100. updateState();
  101. state = STATE1;
  102. break;
  103. case STATE1:
  104. state = STATE2;
  105. break;
  106. }
  107. }
  108. void output(const vle::devs::Time& /* time */,
  109. vle::devs::ExternalEventList& output) const
  110. {
  111. if (state == STATE1) {
  112. {
  113. vle::devs::ExternalEvent* ee =
  114. new vle::devs::ExternalEvent("Tmin");
  115. ee << vle::devs::attribute("name", buildString("Tmin"));
  116. ee << vle::devs::attribute("value", buildDouble(Tmin));
  117. output.push_back(ee);
  118. }
  119. {
  120. vle::devs::ExternalEvent* ee =
  121. new vle::devs::ExternalEvent("Tmax");
  122. ee << vle::devs::attribute("name", buildString("Tmax"));
  123. ee << vle::devs::attribute("value", buildDouble(Tmax));
  124. output.push_back(ee);
  125. }
  126. {
  127. vle::devs::ExternalEvent* ee =
  128. new vle::devs::ExternalEvent("Tmoy");
  129. ee << vle::devs::attribute("name", buildString("Tmoy"));
  130. ee << vle::devs::attribute("value", buildDouble(Tmoy));
  131. output.push_back(ee);
  132. }
  133. {
  134. vle::devs::ExternalEvent* ee =
  135. new vle::devs::ExternalEvent("Hmin");
  136. ee << vle::devs::attribute("name", buildString("Hmin"));
  137. ee << vle::devs::attribute("value", buildDouble(Hmin));
  138. output.push_back(ee);
  139. }
  140. {
  141. vle::devs::ExternalEvent* ee =
  142. new vle::devs::ExternalEvent("Hmax");
  143. ee << vle::devs::attribute("name", buildString("Hmax"));
  144. ee << vle::devs::attribute("value", buildDouble(Hmax));
  145. output.push_back(ee);
  146. }
  147. {
  148. vle::devs::ExternalEvent* ee =
  149. new vle::devs::ExternalEvent("Hmoy");
  150. ee << vle::devs::attribute("name", buildString("Hmoy"));
  151. ee << vle::devs::attribute("value", buildDouble(Hmoy));
  152. output.push_back(ee);
  153. }
  154. }
  155. }
  156. vle::value::Value* observation(
  157. const vle::devs::ObservationEvent& event) const
  158. {
  159. if (event.onPort("obs_m_Tx"))
  160. return buildDouble(Tmax);
  161. if (event.onPort("obs_m_Tn"))
  162. return buildDouble(Tmin);
  163. if (event.onPort("obs_m_Tmoy"))
  164. return buildDouble(Tmoy);
  165. if (event.onPort("obs_m_Hx"))
  166. return buildDouble(Hmax);
  167. if (event.onPort("obs_m_Hn"))
  168. return buildDouble(Hmin);
  169. if (event.onPort("obs_m_Hmoy"))
  170. return buildDouble(Hmoy);
  171. return 0;
  172. }
  173. };
  174. } // namespace malariaspread
  175. DECLARE_DYNAMICS(malariaspread::Meteo)