models.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @file tests/plot/models.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.fr
  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. #ifndef TESTS_PLOT_MODELS_HPP
  26. #define TESTS_PLOT_MODELS_HPP 1
  27. #include <paradevs/common/time/DoubleTime.hpp>
  28. #include <paradevs/kernel/pdevs/Dynamics.hpp>
  29. #include <tests/plot/graph_defs.hpp>
  30. #include <tests/plot/dispersion.hpp>
  31. #include <tests/plot/milsol.hpp>
  32. #include <shapefil.h>
  33. #include <cmath>
  34. #define WIND_SPEED 6
  35. #define WIND_DIRECTION 1.57 // 0 (ouest) 3.1415 (est)
  36. #define PLOT_ID 153 // 240 // (est) 50 (sud) 159 (centre) 126 (ouest)
  37. namespace paradevs { namespace tests { namespace plot {
  38. struct PlotParameters
  39. {
  40. PlotParameters(int index,
  41. const paradevs::tests::boost_graph::Points& points,
  42. int neighbour_number)
  43. : _index(index), _points(points), _neighbour_number(neighbour_number)
  44. { }
  45. int _index;
  46. paradevs::tests::boost_graph::Points _points;
  47. int _neighbour_number;
  48. };
  49. typedef std::vector < long double > Doubles;
  50. struct PlotData
  51. {
  52. PlotData() : index(-1)
  53. { }
  54. PlotData(int index, const Points& points, Doubles ready_spore_numbers) :
  55. index(index), points(points), ready_spore_numbers(ready_spore_numbers)
  56. { }
  57. int index;
  58. Points points;
  59. Doubles ready_spore_numbers;
  60. };
  61. class Plot : public paradevs::pdevs::Dynamics < common::DoubleTime,
  62. PlotParameters >
  63. {
  64. public:
  65. Plot(const std::string& name, const PlotParameters& parameters) :
  66. paradevs::pdevs::Dynamics < common::DoubleTime, PlotParameters >(
  67. name, parameters),
  68. _index(parameters._index),
  69. _points(parameters._points),
  70. _neighbour_number(parameters._neighbour_number)
  71. {
  72. _data.index = _index;
  73. _data.points = parameters._points;
  74. for (Points::const_iterator it = parameters._points.begin();
  75. it != parameters._points.end(); ++it) {
  76. _milsol.push_back(Milsol());
  77. _data.ready_spore_numbers.push_back(0.);
  78. }
  79. _handle = DBFOpen("/home/eric/tmp/parcelle/test/parcellaire.dbf",
  80. "rb+");
  81. if (!_handle) {
  82. std::cout << "Error" << std::endl;
  83. }
  84. }
  85. virtual ~Plot()
  86. { DBFClose(_handle); }
  87. void dint(typename common::DoubleTime::type t)
  88. {
  89. if (_phase == SEND) {
  90. if (_neighbour_number > 0) {
  91. _phase = WAIT;
  92. _sigma = common::DoubleTime::infinity;
  93. } else {
  94. _phase = NEWSTATE;
  95. _sigma = 0;
  96. }
  97. } else if (_phase == NEWSTATE) {
  98. // cohorte update
  99. int i = 0;
  100. for (std::vector < Milsol >::iterator it = _milsol.begin();
  101. it != _milsol.end(); ++it) {
  102. (*it)(t);
  103. _data.ready_spore_numbers[i] = it->get_ready_spore_number();
  104. ++i;
  105. }
  106. // compute
  107. // neighbour -> local
  108. {
  109. for (int k = 0; k < _data.points.size(); ++k) {
  110. double sum = 0;
  111. for (std::vector < PlotData >::iterator it =
  112. _neighbour_data.begin();
  113. it != _neighbour_data.end(); ++it) {
  114. for (int i = 0; i < it->points.size(); ++i) {
  115. if (it->ready_spore_numbers[i] > 0) {
  116. sum += _dispersion_function(
  117. t,
  118. paradevs::tests::boost_graph::Point(
  119. it->points[i]._x, it->points[i]._y),
  120. _data.points[k],
  121. WIND_SPEED, WIND_DIRECTION) *
  122. it->ready_spore_numbers[i];
  123. }
  124. }
  125. }
  126. _milsol[k].add_zoospore_number(sum);
  127. }
  128. }
  129. // local -> local
  130. {
  131. for (int k = 0; k < _data.points.size(); ++k) {
  132. double sum = 0;
  133. for (int i = 0; i < _data.points.size(); ++i) {
  134. if (i != k and _data.ready_spore_numbers[i] > 0) {
  135. sum += _dispersion_function(
  136. t, _data.points[i], _data.points[k],
  137. WIND_SPEED, WIND_DIRECTION) *
  138. _data.ready_spore_numbers[i];
  139. }
  140. }
  141. _milsol[k].add_zoospore_number(sum);
  142. }
  143. }
  144. // affect new spores to milsol model
  145. // and compute mean
  146. {
  147. double sum = 0;
  148. double p = 0;
  149. for (int k = 0; k < _data.points.size(); ++k) {
  150. sum += _milsol[k].get_zoospore_number();
  151. }
  152. sum /= _data.points.size();
  153. if (sum > 0) {
  154. p = std::log10(sum);
  155. if (p < 0) {
  156. p = 0;
  157. }
  158. }
  159. DBFWriteDoubleAttribute(_handle, _index, (int)t, p);
  160. }
  161. _phase = SEND;
  162. _sigma = 1;
  163. _received = 0;
  164. _neighbour_data.clear();
  165. }
  166. }
  167. void dext(typename common::DoubleTime::type t,
  168. typename common::DoubleTime::type /* e */,
  169. const common::Bag < common::DoubleTime >& bag)
  170. {
  171. for (common::Bag < common::DoubleTime >::const_iterator it =
  172. bag.begin(); it != bag.end(); ++it) {
  173. PlotData data; // = *(PlotData*)(it->get_content());
  174. _neighbour_data.push_back(data);
  175. std::cout << _index << " " << t << ": dext - "
  176. << data.index << " [ ";
  177. for (int k = 0; k < data.ready_spore_numbers.size(); ++k) {
  178. std::cout << data.ready_spore_numbers[k] << " ";
  179. }
  180. std::cout << "]" << std::endl;
  181. ++_received;
  182. }
  183. if (_received == _neighbour_number) {
  184. _phase = NEWSTATE;
  185. _sigma = 0;
  186. } else {
  187. _phase = WAIT;
  188. _sigma = common::DoubleTime::infinity;
  189. }
  190. }
  191. void dconf(typename common::DoubleTime::type t,
  192. typename common::DoubleTime::type e,
  193. const common::Bag < common::DoubleTime >& bag)
  194. {
  195. // std::cout << t << "[" << get_name() << "]: dconf" << std::endl;
  196. dext(t, e, bag);
  197. }
  198. typename common::DoubleTime::type start(
  199. typename common::DoubleTime::type /* t */)
  200. {
  201. if (_index == PLOT_ID) {
  202. for (std::vector < Milsol >::iterator it = _milsol.begin();
  203. it != _milsol.end(); ++it) {
  204. it->set_inoculum_primaire_number(1e6);
  205. }
  206. } else {
  207. for (std::vector < Milsol >::iterator it = _milsol.begin();
  208. it != _milsol.end(); ++it) {
  209. it->set_inoculum_primaire_number(0);
  210. }
  211. }
  212. _phase = SEND;
  213. if (_neighbour_number > 0) {
  214. _sigma = 0;
  215. } else {
  216. _sigma = 1;
  217. }
  218. _received = 0;
  219. return 0;
  220. }
  221. typename common::DoubleTime::type ta(
  222. typename common::DoubleTime::type /* t */) const
  223. { return _sigma; }
  224. common::Bag < common::DoubleTime > lambda(
  225. typename common::DoubleTime::type t) const
  226. {
  227. common::Bag < common::DoubleTime > bag;
  228. if (_phase == SEND) {
  229. // bag.push_back(common::ExternalEvent < common::DoubleTime >(
  230. // "out", _data));
  231. }
  232. return bag;
  233. }
  234. void observation(std::ostream& /* file */) const
  235. { }
  236. private:
  237. enum Phase { WAIT, SEND, NEWSTATE, NEXT };
  238. // parameters
  239. int _index;
  240. paradevs::tests::boost_graph::Points _points;
  241. int _neighbour_number;
  242. DBFHandle _handle;
  243. // data
  244. PlotData _data;
  245. std::vector < PlotData > _neighbour_data;
  246. // submodels
  247. Plume2dDispersionFunction _dispersion_function;
  248. // KleinDispersionFunction _dispersion_function;
  249. std::vector < Milsol > _milsol;
  250. Climate _climate;
  251. // state
  252. Phase _phase;
  253. common::DoubleTime::type _sigma;
  254. int _received;
  255. };
  256. } } } // namespace paradevs tests plot
  257. #endif