models.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. PlotData(const PlotData& d) : index(d.index), points(d.points), ready_spore_numbers(d.ready_spore_numbers)
  58. { }
  59. int index;
  60. Points points;
  61. Doubles ready_spore_numbers;
  62. };
  63. class Plot : public paradevs::pdevs::Dynamics < common::DoubleTime,
  64. PlotParameters >
  65. {
  66. public:
  67. Plot(const std::string& name, const PlotParameters& parameters) :
  68. paradevs::pdevs::Dynamics < common::DoubleTime, PlotParameters >(
  69. name, parameters),
  70. _index(parameters._index),
  71. _points(parameters._points),
  72. _neighbour_number(parameters._neighbour_number)
  73. {
  74. _data.index = _index;
  75. _data.points = parameters._points;
  76. _active = _data.points.size() > 0;
  77. std::cout << _data.points.size() << std::endl;
  78. for (Points::const_iterator it = parameters._points.begin();
  79. it != parameters._points.end(); ++it) {
  80. _milsol.push_back(Milsol());
  81. _data.ready_spore_numbers.push_back(0.);
  82. }
  83. _handle = DBFOpen("/home/eric/vagrant_data/tmp/parcelle/test/parcellaire.dbf",
  84. "rb+");
  85. if (!_handle) {
  86. std::cout << "Error" << std::endl;
  87. }
  88. }
  89. virtual ~Plot()
  90. { DBFClose(_handle); }
  91. void dint(typename common::DoubleTime::type t)
  92. {
  93. if (_phase == SEND) {
  94. if (_neighbour_number > 0) {
  95. _phase = WAIT;
  96. _sigma = common::DoubleTime::infinity;
  97. } else {
  98. _phase = NEWSTATE;
  99. _sigma = 0;
  100. }
  101. } else if (_phase == NEWSTATE) {
  102. // cohorte update
  103. int i = 0;
  104. for (std::vector < Milsol >::iterator it = _milsol.begin();
  105. it != _milsol.end(); ++it) {
  106. (*it)(t);
  107. _data.ready_spore_numbers[i] = it->get_ready_spore_number();
  108. ++i;
  109. }
  110. // compute
  111. // neighbour -> local
  112. {
  113. for (int k = 0; k < _data.points.size(); ++k) {
  114. double sum = 0;
  115. for (std::vector < PlotData >::iterator it =
  116. _neighbour_data.begin();
  117. it != _neighbour_data.end(); ++it) {
  118. for (int i = 0; i < it->points.size(); ++i) {
  119. if (it->ready_spore_numbers[i] > 0) {
  120. sum += _dispersion_function(
  121. t,
  122. paradevs::tests::boost_graph::Point(
  123. it->points[i]._x, it->points[i]._y),
  124. _data.points[k],
  125. WIND_SPEED, WIND_DIRECTION) *
  126. it->ready_spore_numbers[i];
  127. }
  128. }
  129. }
  130. _milsol[k].add_zoospore_number(sum);
  131. }
  132. }
  133. // local -> local
  134. {
  135. for (int k = 0; k < _data.points.size(); ++k) {
  136. double sum = 0;
  137. for (int i = 0; i < _data.points.size(); ++i) {
  138. if (i != k and _data.ready_spore_numbers[i] > 0) {
  139. sum += _dispersion_function(
  140. t, _data.points[i], _data.points[k],
  141. WIND_SPEED, WIND_DIRECTION) *
  142. _data.ready_spore_numbers[i];
  143. }
  144. }
  145. _milsol[k].add_zoospore_number(sum);
  146. }
  147. }
  148. // affect new spores to milsol model
  149. // and compute mean
  150. {
  151. double sum = 0;
  152. double p = 0;
  153. for (int k = 0; k < _data.points.size(); ++k) {
  154. sum += _milsol[k].get_zoospore_number();
  155. }
  156. sum /= _data.points.size();
  157. if (sum > 0) {
  158. std::cout << "OK" << std::endl;
  159. p = std::log10(sum);
  160. if (p < 0) {
  161. p = 0;
  162. }
  163. }
  164. if (_active)
  165. DBFWriteDoubleAttribute(_handle, _index, (int)t, p);
  166. else
  167. DBFWriteDoubleAttribute(_handle, _index, (int)t, 0.);
  168. }
  169. _phase = SEND;
  170. _sigma = 1;
  171. _received = 0;
  172. _neighbour_data.clear();
  173. }
  174. }
  175. void dext(typename common::DoubleTime::type t,
  176. typename common::DoubleTime::type /* e */,
  177. const common::Bag < common::DoubleTime >& bag)
  178. {
  179. for (common::Bag < common::DoubleTime >::const_iterator it =
  180. bag.begin(); it != bag.end(); ++it) {
  181. PlotData data = it->get_content().get_content < PlotData >();
  182. _neighbour_data.push_back(data);
  183. // std::cout << _index << " " << t << ": dext - "
  184. // << data.index << " [ ";
  185. for (int k = 0; k < data.ready_spore_numbers.size(); ++k) {
  186. std::cout << data.ready_spore_numbers[k] << " ";
  187. }
  188. // std::cout << "]" << std::endl;
  189. ++_received;
  190. }
  191. if (_received == _neighbour_number) {
  192. _phase = NEWSTATE;
  193. _sigma = 0;
  194. } else {
  195. _phase = WAIT;
  196. _sigma = common::DoubleTime::infinity;
  197. }
  198. }
  199. void dconf(typename common::DoubleTime::type t,
  200. typename common::DoubleTime::type e,
  201. const common::Bag < common::DoubleTime >& bag)
  202. {
  203. // std::cout << t << "[" << get_name() << "]: dconf" << std::endl;
  204. dext(t, e, bag);
  205. }
  206. typename common::DoubleTime::type start(
  207. typename common::DoubleTime::type /* t */)
  208. {
  209. if (_index == PLOT_ID) {
  210. for (std::vector < Milsol >::iterator it = _milsol.begin();
  211. it != _milsol.end(); ++it) {
  212. it->set_inoculum_primaire_number(1e6);
  213. }
  214. } else {
  215. for (std::vector < Milsol >::iterator it = _milsol.begin();
  216. it != _milsol.end(); ++it) {
  217. it->set_inoculum_primaire_number(0);
  218. }
  219. }
  220. _phase = SEND;
  221. if (_neighbour_number > 0) {
  222. _sigma = 0;
  223. } else {
  224. _sigma = 1;
  225. }
  226. _received = 0;
  227. return 0;
  228. }
  229. typename common::DoubleTime::type ta(
  230. typename common::DoubleTime::type /* t */) const
  231. { return _sigma; }
  232. common::Bag < common::DoubleTime > lambda(
  233. typename common::DoubleTime::type t) const
  234. {
  235. common::Bag < common::DoubleTime > bag;
  236. if (_phase == SEND) {
  237. bag.push_back(common::ExternalEvent < common::DoubleTime >(
  238. "out", _data));
  239. for (int k = 0; k < _data.ready_spore_numbers.size(); ++k) {
  240. std::cout << _data.ready_spore_numbers[k] << " ";
  241. }
  242. }
  243. return bag;
  244. }
  245. void observation(std::ostream& /* file */) const
  246. { }
  247. private:
  248. enum Phase { WAIT, SEND, NEWSTATE, NEXT };
  249. // parameters
  250. int _index;
  251. paradevs::tests::boost_graph::Points _points;
  252. int _neighbour_number;
  253. bool _active;
  254. DBFHandle _handle;
  255. // data
  256. PlotData _data;
  257. std::vector < PlotData > _neighbour_data;
  258. // submodels
  259. Plume2dDispersionFunction _dispersion_function;
  260. // KleinDispersionFunction _dispersion_function;
  261. std::vector < Milsol > _milsol;
  262. Climate _climate;
  263. // state
  264. Phase _phase;
  265. common::DoubleTime::type _sigma;
  266. int _received;
  267. };
  268. } } } // namespace paradevs tests plot
  269. #endif