models.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. namespace paradevs { namespace tests { namespace plot {
  35. struct PlotParameters
  36. {
  37. PlotParameters(int index,
  38. const paradevs::tests::boost_graph::Point& centroid,
  39. const paradevs::tests::boost_graph::Points&
  40. neighbour_centroids)
  41. : _index(index), _centroid(centroid),
  42. _neighbour_centroids(neighbour_centroids)
  43. { }
  44. int _index;
  45. paradevs::tests::boost_graph::Point _centroid;
  46. paradevs::tests::boost_graph::Points _neighbour_centroids;
  47. };
  48. struct PlotData
  49. {
  50. PlotData() : index(-1), x(-1), y(-1), ready_spore_number(0)
  51. { }
  52. PlotData(int index, double x, double y, double ready_spore_number) :
  53. index(index), x(x), y(y), ready_spore_number(ready_spore_number)
  54. { }
  55. int index;
  56. double x;
  57. double y;
  58. double ready_spore_number;
  59. };
  60. class Plot : public paradevs::pdevs::Dynamics < common::DoubleTime,
  61. PlotParameters >
  62. {
  63. public:
  64. Plot(const std::string& name, const PlotParameters& parameters) :
  65. paradevs::pdevs::Dynamics < common::DoubleTime, PlotParameters >(
  66. name, parameters),
  67. _index(parameters._index),
  68. _centroid(parameters._centroid),
  69. _neighbour_centroids(parameters._neighbour_centroids)
  70. {
  71. _data.index = _index;
  72. _data.x = parameters._centroid._x;
  73. _data.y = parameters._centroid._y;
  74. // std::cout << "(" << _centroid._x << "," << _centroid._y << "): ";
  75. // for (Points::const_iterator it = _neighbour_centroids.begin();
  76. // it != _neighbour_centroids.end(); ++it) {
  77. // double distance = std::sqrt(
  78. // (_centroid._x - it->_x) * (_centroid._x - it->_x) +
  79. // (_centroid._y - it->_y) * (_centroid._y - it->_y));
  80. // std::cout << "(" << it->_x << "," << it->_y << ") -> " << distance
  81. // << " ";
  82. // }
  83. // std::cout << std::endl;
  84. _handle = DBFOpen("/home/eric/tmp/parcelle/test/parcellaire.dbf",
  85. "rb+");
  86. if (!_handle) {
  87. std::cout << "Error" << std::endl;
  88. }
  89. }
  90. virtual ~Plot()
  91. {
  92. DBFClose(_handle);
  93. }
  94. void dint(typename common::DoubleTime::type t)
  95. {
  96. // std::cout << _index << " " << t << ": dint - BEFORE - "
  97. // << _phase << std::endl;
  98. if (_phase == SEND) {
  99. if (_neighbour_centroids.size() > 0) {
  100. _phase = WAIT;
  101. _sigma = common::DoubleTime::infinity;
  102. } else {
  103. _phase = NEWSTATE;
  104. _sigma = 0;
  105. }
  106. } else if (_phase == NEWSTATE) {
  107. // cohorte update
  108. _milsol(t);
  109. _data.ready_spore_number = _milsol.get_ready_spore_number();
  110. // compute
  111. double sum = 0;
  112. for (std::vector < PlotData >::const_iterator it =
  113. _neighbour_data.begin(); it != _neighbour_data.end();
  114. ++it) {
  115. if (it->ready_spore_number > 0) {
  116. sum += _dispersion_function(
  117. t,
  118. paradevs::tests::boost_graph::Point(it->x, it->y),
  119. _centroid, 6., 0.) * it->ready_spore_number;
  120. // std::cout << (_index + 1) << " " << (it->index + 1) << " " << it->concentration << std::endl;
  121. }
  122. }
  123. sum += _dispersion_function(
  124. t, _centroid,
  125. paradevs::tests::boost_graph::Point(_centroid._x + 10,
  126. _centroid._y), 6., 0.) *
  127. _data.ready_spore_number;
  128. _milsol.add_zoospore_number(sum);
  129. double p = 0;
  130. if (_milsol.get_zoospore_number() > 0) {
  131. p = std::log10(_milsol.get_zoospore_number());
  132. }
  133. // std::cout << t << "\t" << _index << "\t"
  134. // << _data.ready_spore_number << "\t"
  135. // << _milsol.get_zoospore_number() << "\t"
  136. // << sum << "\t"
  137. // << p << std::endl;
  138. DBFWriteDoubleAttribute(_handle, _index, 0, p);
  139. _phase = SEND;
  140. _sigma = 1;
  141. _received = 0;
  142. _neighbour_data.clear();
  143. }
  144. // std::cout << _index << " " << t << ": dint - AFTER - "
  145. // << _phase << std::endl;
  146. }
  147. void dext(typename common::DoubleTime::type t,
  148. typename common::DoubleTime::type /* e */,
  149. const common::Bag < common::DoubleTime >& bag)
  150. {
  151. for (common::Bag < common::DoubleTime >::const_iterator it =
  152. bag.begin(); it != bag.end(); ++it) {
  153. _neighbour_data.push_back(*(PlotData*)(it->get_content()));
  154. ++_received;
  155. }
  156. if (_received == _neighbour_centroids.size()) {
  157. _phase = NEWSTATE;
  158. _sigma = 0;
  159. } else {
  160. _phase = WAIT;
  161. _sigma = common::DoubleTime::infinity;
  162. }
  163. // std::cout << _index << " " << t << ": dext - "
  164. // << _received << "/"
  165. // << _neighbour_centroids.size()
  166. // << " " << _phase << std::endl;
  167. }
  168. void dconf(typename common::DoubleTime::type t,
  169. typename common::DoubleTime::type e,
  170. const common::Bag < common::DoubleTime >& bag)
  171. {
  172. // std::cout << t << "[" << get_name() << "]: dconf" << std::endl;
  173. dext(t, e, bag);
  174. }
  175. typename common::DoubleTime::type start(
  176. typename common::DoubleTime::type /* t */)
  177. {
  178. if (_index == 210) {
  179. _milsol.set_inoculum_primaire_number(1e6);
  180. } else {
  181. _milsol.set_inoculum_primaire_number(0);
  182. }
  183. _phase = SEND;
  184. if (_neighbour_centroids.size() > 0) {
  185. _sigma = 0;
  186. } else {
  187. _sigma = 1;
  188. }
  189. _received = 0;
  190. return 0;
  191. }
  192. typename common::DoubleTime::type ta(
  193. typename common::DoubleTime::type /* t */) const
  194. { return _sigma; }
  195. common::Bag < common::DoubleTime > lambda(
  196. typename common::DoubleTime::type t) const
  197. {
  198. common::Bag < common::DoubleTime > bag;
  199. if (_phase == SEND) {
  200. bag.push_back(common::ExternalEvent < common::DoubleTime >(
  201. "out", (void*)(&_data)));
  202. // std::cout << t << "[" << get_name() << "]: lambda - "
  203. // << _phase << std::endl;
  204. }
  205. return bag;
  206. }
  207. void observation(std::ostream& /* file */) const
  208. { }
  209. private:
  210. enum Phase { WAIT, SEND, NEWSTATE, NEXT };
  211. // parameters
  212. int _index;
  213. paradevs::tests::boost_graph::Point _centroid;
  214. std::vector < paradevs::tests::boost_graph::Point > _neighbour_centroids;
  215. DBFHandle _handle;
  216. // data
  217. PlotData _data;
  218. std::vector < PlotData > _neighbour_data;
  219. // submodels
  220. Plume2dDispersionFunction _dispersion_function;
  221. // KleinDispersionFunction _dispersion_function;
  222. Milsol _milsol;
  223. Climate _climate;
  224. // state
  225. Phase _phase;
  226. common::DoubleTime::type _sigma;
  227. int _received;
  228. };
  229. } } } // namespace paradevs tests plot
  230. #endif