models.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * @file tests/mpi/cluster/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-2016 ULCO http://www.univ-littoral.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_MPI_CLUSTER_MODELS_HPP
  26. #define TESTS_MPI_CLUSTER_MODELS_HPP 1
  27. #include <paradevs/common/time/DoubleTime.hpp>
  28. #include <paradevs/kernel/pdevs/Dynamics.hpp>
  29. namespace paradevs { namespace tests { namespace mpi { namespace cluster {
  30. class ThreeStateModel :
  31. public paradevs::pdevs::Dynamics < common::DoubleTime >
  32. {
  33. public:
  34. ThreeStateModel(const std::string& name,
  35. const common::NoParameters& parameters) :
  36. paradevs::pdevs::Dynamics < common::DoubleTime >(name, parameters)
  37. { }
  38. virtual ~ThreeStateModel()
  39. { }
  40. void compute()
  41. {
  42. for (unsigned int i = 0; i < heights.size(); ++i) {
  43. if (heights[i] != -1 and heights[i] < 10) {
  44. heights[i] += speeds[i] * scales[i];
  45. }
  46. }
  47. }
  48. void display() const
  49. {
  50. for (std::vector < double >::const_iterator it = heights.begin();
  51. it != heights.end(); ++it) {
  52. std::cout << *it << " ";
  53. }
  54. std::cout << std::endl;
  55. }
  56. void display_full() const
  57. {
  58. unsigned int i = 1;
  59. for (std::vector < double >::const_iterator it = heights.begin();
  60. it != heights.end(); ++it, ++i) {
  61. if (*it > 10) {
  62. std::cout << "S" << i;
  63. }
  64. }
  65. std::cout << std::endl;
  66. }
  67. bool full() const
  68. {
  69. unsigned int n = 0;
  70. for (std::vector < double >::const_iterator it = heights.begin();
  71. it != heights.end(); ++it) {
  72. if (*it > 10) {
  73. ++n;
  74. }
  75. }
  76. return n > 0;
  77. }
  78. bool full_N() const
  79. {
  80. unsigned int n = 0;
  81. for (std::vector < double >::const_iterator it = heights.begin();
  82. it != heights.end(); ++it) {
  83. if (*it == -1) {
  84. ++n;
  85. }
  86. }
  87. return n >= 2;
  88. }
  89. void mark_full(typename common::DoubleTime::type t)
  90. {
  91. for (std::vector < double >::iterator it = heights.begin();
  92. it != heights.end(); ++it) {
  93. if (*it > 10) {
  94. *it = -1;
  95. _last_time = t;
  96. }
  97. }
  98. }
  99. void raz()
  100. {
  101. for (std::vector < double >::iterator it = heights.begin();
  102. it != heights.end(); ++it) {
  103. if (*it == -1) {
  104. *it = 0;
  105. }
  106. }
  107. }
  108. void dconf(typename common::DoubleTime::type t,
  109. typename common::DoubleTime::type e,
  110. const common::Bag < common::DoubleTime >& msgs)
  111. {
  112. dext(t, e, msgs);
  113. }
  114. void dext(typename common::DoubleTime::type /* t */,
  115. typename common::DoubleTime::type /* e */,
  116. const common::Bag < common::DoubleTime >& msgs)
  117. {
  118. for (common::Bag < common::DoubleTime >::const_iterator
  119. it = msgs.begin(); it != msgs.end(); ++it) {
  120. ++n;
  121. }
  122. // std::cout << get_name() << "|I|" << (t - _last_time) << " ";
  123. if (sigma == 1) {
  124. if (n > 3) {
  125. ++index;
  126. if (index == scales.size()) {
  127. index = 0;
  128. }
  129. sigma = std::numeric_limits < double >::max();
  130. if (scales[index] == 1) {
  131. scales[index] = 2;
  132. } else {
  133. scales[index] = 1;
  134. }
  135. // std::cout << get_name() << "||INF ";
  136. n = 0;
  137. }
  138. } else {
  139. sigma = 1;
  140. n = 0;
  141. }
  142. }
  143. void dint(typename common::DoubleTime::type t)
  144. {
  145. // std::cout << get_name() << " at " << t << ": dint" << std::endl;
  146. mark_full(t);
  147. if (full_N()) {
  148. raz();
  149. }
  150. compute();
  151. }
  152. typename common::DoubleTime::type start(
  153. typename common::DoubleTime::type t)
  154. {
  155. heights = { 0, 0, 0, 0, 0 };
  156. speeds = { 0.21, 0.3, 0.7, 0.56, 0.14 };
  157. scales = { 1, 1, 1, 1, 1 };
  158. index = 0;
  159. n = 0;
  160. sigma = 1;
  161. _last_time = t;
  162. return 0;
  163. }
  164. typename common::DoubleTime::type ta(
  165. typename common::DoubleTime::type /* t */) const
  166. { return sigma; }
  167. common::Bag < common::DoubleTime > lambda(
  168. typename common::DoubleTime::type /* t */) const
  169. {
  170. common::Bag < common::DoubleTime > msgs;
  171. if (full()) {
  172. // std::cout << get_name() << "|O|" << (t - _last_time) << " ";
  173. // display_full();
  174. msgs.push_back(common::ExternalEvent < common::DoubleTime >(
  175. "out", 0));
  176. }
  177. return msgs;
  178. }
  179. private:
  180. std::vector < double > heights;
  181. std::vector < double > speeds;
  182. std::vector < double > scales;
  183. unsigned int index;
  184. unsigned int n;
  185. typename common::DoubleTime::type sigma;
  186. typename common::DoubleTime::type _last_time;
  187. };
  188. struct AParameters
  189. {
  190. unsigned int frequency;
  191. };
  192. class A : public paradevs::pdevs::Dynamics < common::DoubleTime, AParameters >
  193. {
  194. public:
  195. A(const std::string& name,
  196. const AParameters& parameters) :
  197. paradevs::pdevs::Dynamics < common::DoubleTime, AParameters >(
  198. name, parameters), _frequency(parameters.frequency)
  199. { }
  200. virtual ~A()
  201. { }
  202. void dconf(typename common::DoubleTime::type t,
  203. typename common::DoubleTime::type e,
  204. const common::Bag < common::DoubleTime >& msgs)
  205. { dext(t, e, msgs); }
  206. void dext(typename common::DoubleTime::type t,
  207. typename common::DoubleTime::type /* e */,
  208. const common::Bag < common::DoubleTime >& /* msgs */)
  209. {
  210. _sigma -= t - _last_time;
  211. _last_time = t;
  212. }
  213. void dint(typename common::DoubleTime::type /* t */)
  214. {
  215. // _sigma = ((double)rand() / RAND_MAX);
  216. _sigma = 1. / _frequency;
  217. }
  218. typename common::DoubleTime::type start(
  219. typename common::DoubleTime::type t)
  220. {
  221. // _sigma = ((double)rand() / RAND_MAX);
  222. _sigma = 1. / _frequency;
  223. _last_time = t;
  224. return 0;
  225. }
  226. typename common::DoubleTime::type ta(
  227. typename common::DoubleTime::type /* t */) const
  228. { return _sigma; }
  229. common::Bag < common::DoubleTime > lambda(
  230. typename common::DoubleTime::type /* t */) const
  231. {
  232. common::Bag < common::DoubleTime > msgs;
  233. msgs.push_back(common::ExternalEvent < common::DoubleTime >("out", 0));
  234. return msgs;
  235. }
  236. private:
  237. typename common::DoubleTime::type _sigma;
  238. typename common::DoubleTime::type _last_time;
  239. unsigned int _frequency;
  240. };
  241. } } } } // namespace paradevs tests mpi cluster
  242. #endif