Meteo.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "Meteo.hpp"
  2. #include <vle/utils/Exception.hpp>
  3. Meteo::Meteo(const vle::graph::AtomicModel& model,
  4. const vle::devs::InitEventList& lst) :
  5. vle::devs::Dynamics(model, lst)
  6. {
  7. file_path = vle::value::toString(lst.get("meteo_file"));
  8. }
  9. Meteo::~Meteo()
  10. {
  11. }
  12. vle::devs::Time Meteo::timeAdvance() const
  13. {
  14. switch (state) {
  15. case STATE2:
  16. return 1.0;
  17. case STATE1:
  18. return 0.0;
  19. default:
  20. Throw(vle::utils::InternalError, "Meteo::timeAdvance()");
  21. }
  22. }
  23. vle::devs::Time Meteo::init(const vle::devs::Time& /*time*/)
  24. {
  25. state = STATE1;
  26. file.open(file_path.c_str());
  27. // skip first line (header)
  28. if (file.good()) {
  29. getline(file, line);
  30. }
  31. year = 0;
  32. month = 0;
  33. day = 0;
  34. Tmin = 0;
  35. Tmax = 0;
  36. Tmoy = 0;
  37. Hmin = 0;
  38. Hmax = 0;
  39. Hmoy = 0;
  40. return 0.0;
  41. }
  42. void Meteo::internalTransition(const vle::devs::Time& /*time*/)
  43. {
  44. switch (state) {
  45. case STATE2 :
  46. updateState();
  47. state = STATE1;
  48. break;
  49. case STATE1:
  50. state = STATE2;
  51. break;
  52. default:
  53. Throw(vle::utils::InternalError, "Meteo::internalTransition");
  54. }
  55. }
  56. void Meteo::output(const vle::devs::Time& currentTime,
  57. vle::devs::ExternalEventList& output) const
  58. {
  59. if (state == STATE1) {
  60. {
  61. vle::devs::ExternalEvent* ee = new vle::devs::ExternalEvent("Tmin");
  62. ee << vle::devs::attribute("name", buildString("Tmin"));
  63. ee << vle::devs::attribute("value", buildDouble(Tmin));
  64. output.addEvent(ee);
  65. }
  66. {
  67. vle::devs::ExternalEvent* ee = new vle::devs::ExternalEvent("Tmax");
  68. ee << vle::devs::attribute("name", buildString("Tmax"));
  69. ee << vle::devs::attribute("value", buildDouble(Tmax));
  70. output.addEvent(ee);
  71. }
  72. {
  73. vle::devs::ExternalEvent* ee = new vle::devs::ExternalEvent("Tmoy");
  74. ee << vle::devs::attribute("name", buildString("Tmoy"));
  75. ee << vle::devs::attribute("value", buildDouble(Tmoy));
  76. output.addEvent(ee);
  77. }
  78. {
  79. vle::devs::ExternalEvent* ee = new vle::devs::ExternalEvent("Hmin");
  80. ee << vle::devs::attribute("name", buildString("Hmin"));
  81. ee << vle::devs::attribute("value", buildDouble(Hmin));
  82. output.addEvent(ee);
  83. }
  84. {
  85. vle::devs::ExternalEvent* ee = new vle::devs::ExternalEvent("Hmax");
  86. ee << vle::devs::attribute("name", buildString("Hmax"));
  87. ee << vle::devs::attribute("value", buildDouble(Hmax));
  88. output.addEvent(ee);
  89. }
  90. {
  91. vle::devs::ExternalEvent* ee = new vle::devs::ExternalEvent("Hmoy");
  92. ee << vle::devs::attribute("name", buildString("Hmoy"));
  93. ee << vle::devs::attribute("value", buildDouble(Hmoy));
  94. output.addEvent(ee);
  95. }
  96. }
  97. }
  98. vle::value::Value* Meteo::observation(const vle::devs::ObservationEvent& event) const
  99. {
  100. if (event.onPort("obs_m_Tx"))
  101. return buildDouble(Tmax);
  102. if (event.onPort("obs_m_Tn"))
  103. return buildDouble(Tmin);
  104. if (event.onPort("obs_m_Tmoy"))
  105. return buildDouble(Tmoy);
  106. if (event.onPort("obs_m_Hx"))
  107. return buildDouble(Hmax);
  108. if (event.onPort("obs_m_Hn"))
  109. return buildDouble(Hmin);
  110. if (event.onPort("obs_m_Hmoy"))
  111. return buildDouble(Hmoy);
  112. }
  113. void Meteo::updateState()
  114. {
  115. if (file.good()) {
  116. getline(file, line);
  117. }
  118. std::stringstream ss(line);
  119. double x;
  120. ss >> year;
  121. ss >> month;
  122. ss >> day;
  123. ss >> Tmax;
  124. ss >> Tmoy;
  125. ss >> Tmin;
  126. ss >> x;
  127. ss >> x;
  128. ss >> x;
  129. ss >> Hmax;
  130. ss >> Hmoy;
  131. ss >> Hmin;
  132. }