models.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /**
  2. * @file tests/pdevs/models.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2019 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_PDEVS_MODELS_HPP
  26. #define TESTS_PDEVS_MODELS_HPP 1
  27. #include <artis-star/common/time/DoubleTime.hpp>
  28. #include <artis-star/common/utils/Trace.hpp>
  29. #include <artis-star/kernel/pdevs/Dynamics.hpp>
  30. #include <chrono>
  31. #include <iostream>
  32. #define DELAY 100
  33. namespace artis {
  34. namespace tests {
  35. namespace pdevs {
  36. struct data {
  37. double x;
  38. double y;
  39. data()
  40. :x(0), y(0) { }
  41. data(double _x, double _y)
  42. :x(_x), y(_y) { }
  43. };
  44. class A : public artis::pdevs::Dynamics<common::DoubleTime, A> {
  45. public:
  46. enum inputs {
  47. IN
  48. };
  49. enum outputs {
  50. OUT
  51. };
  52. A(const std::string& name,
  53. const artis::pdevs::Context<common::DoubleTime, A, artis::common::NoParameters>& context)
  54. :
  55. artis::pdevs::Dynamics<common::DoubleTime, A>(name, context)
  56. {
  57. input_ports({{IN, "in"}});
  58. output_ports({{OUT, "out"}});
  59. }
  60. ~A() override = default;
  61. void dint(typename common::DoubleTime::type t) override
  62. {
  63. #ifndef WITH_TRACE
  64. (void)t;
  65. #endif
  66. #ifdef WITH_TRACE
  67. common::Trace<common::DoubleTime>::trace()
  68. << common::TraceElement<common::DoubleTime>(get_name(), t,
  69. common::DELTA_INT);
  70. common::Trace<common::DoubleTime>::trace().flush();
  71. #endif
  72. if (_phase == WAIT) {
  73. ++_value.x;
  74. --_value.y;
  75. _phase = SEND;
  76. } else if (_phase == SEND) {
  77. _phase = WAIT;
  78. }
  79. }
  80. void
  81. dext(typename common::DoubleTime::type t, typename common::DoubleTime::type /* e */,
  82. const common::Bag<common::DoubleTime>& msgs) override
  83. {
  84. #ifndef WITH_TRACE
  85. (void)t;
  86. (void)msgs;
  87. #endif
  88. #ifdef WITH_TRACE
  89. common::Trace<common::DoubleTime>::trace()
  90. << common::TraceElement<common::DoubleTime>(get_name(), t,
  91. common::DELTA_EXT)
  92. << "messages = " << msgs.to_string();
  93. common::Trace<common::DoubleTime>::trace().flush();
  94. #endif
  95. _phase = SEND;
  96. }
  97. void dconf(typename common::DoubleTime::type t,
  98. typename common::DoubleTime::type /* e */,
  99. const common::Bag<common::DoubleTime>& msgs) override
  100. {
  101. #ifndef WITH_TRACE
  102. (void)t;
  103. (void)msgs;
  104. #endif
  105. #ifdef WITH_TRACE
  106. common::Trace<common::DoubleTime>::trace()
  107. << common::TraceElement<common::DoubleTime>(get_name(), t,
  108. common::DELTA_CONF)
  109. << "messages = " << msgs.to_string();
  110. common::Trace<common::DoubleTime>::trace().flush();
  111. #endif
  112. }
  113. typename common::DoubleTime::type
  114. start(typename common::DoubleTime::type t) override
  115. {
  116. #ifndef WITH_TRACE
  117. (void)t;
  118. #endif
  119. #ifdef WITH_TRACE
  120. common::Trace<common::DoubleTime>::trace()
  121. << common::TraceElement<common::DoubleTime>(get_name(), t,
  122. common::START);
  123. common::Trace<common::DoubleTime>::trace().flush();
  124. #endif
  125. _phase = WAIT;
  126. return 0;
  127. }
  128. typename common::DoubleTime::type
  129. ta(typename common::DoubleTime::type t) const override
  130. {
  131. #ifndef WITH_TRACE
  132. (void)t;
  133. #endif
  134. #ifdef WITH_TRACE
  135. common::Trace<common::DoubleTime>::trace()
  136. << common::TraceElement<common::DoubleTime>(get_name(), t, common::TA);
  137. common::Trace<common::DoubleTime>::trace().flush();
  138. #endif
  139. if (_phase == WAIT) {
  140. return 1;
  141. } else {
  142. return 0;
  143. }
  144. }
  145. common::Bag<common::DoubleTime>
  146. lambda(typename common::DoubleTime::type t) const override
  147. {
  148. #ifndef WITH_TRACE
  149. (void)t;
  150. #endif
  151. common::Bag<common::DoubleTime> msgs;
  152. if (_phase == SEND) {
  153. msgs.push_back(
  154. artis::common::ExternalEvent<common::DoubleTime>(OUT, _value));
  155. }
  156. #ifdef WITH_TRACE
  157. common::Trace<common::DoubleTime>::trace()
  158. << common::TraceElement<common::DoubleTime>(get_name(), t,
  159. common::LAMBDA)
  160. << "messages = " << msgs.to_string();
  161. common::Trace<common::DoubleTime>::trace().flush();
  162. #endif
  163. return msgs;
  164. }
  165. private:
  166. enum Phase {
  167. WAIT, SEND
  168. };
  169. Phase _phase;
  170. data _value;
  171. };
  172. class B : public artis::pdevs::Dynamics<common::DoubleTime, B> {
  173. public:
  174. enum inputs {
  175. IN
  176. };
  177. enum outputs {
  178. OUT
  179. };
  180. B(const std::string& name,
  181. const artis::pdevs::Context<common::DoubleTime, B, artis::common::NoParameters>& context)
  182. :
  183. artis::pdevs::Dynamics<common::DoubleTime, B>(name, context),
  184. _value(0)
  185. {
  186. input_ports({{IN, "in"}});
  187. output_ports({{OUT, "out"}});
  188. }
  189. ~B() override = default;
  190. void dint(typename common::DoubleTime::type t) override
  191. {
  192. #ifndef WITH_TRACE
  193. (void)t;
  194. #endif
  195. #ifdef WITH_TRACE
  196. common::Trace<common::DoubleTime>::trace()
  197. << common::TraceElement<common::DoubleTime>(get_name(), t,
  198. common::DELTA_INT);
  199. common::Trace<common::DoubleTime>::trace().flush();
  200. #endif
  201. if (_phase == SEND) {
  202. _phase = WAIT;
  203. }
  204. }
  205. void
  206. dext(typename common::DoubleTime::type t, typename common::DoubleTime::type /* e */,
  207. const common::Bag<common::DoubleTime>& msgs) override
  208. {
  209. #ifndef WITH_TRACE
  210. (void)t;
  211. (void)msgs;
  212. #endif
  213. #ifdef WITH_TRACE
  214. common::Trace<common::DoubleTime>::trace()
  215. << common::TraceElement<common::DoubleTime>(get_name(), t,
  216. common::DELTA_EXT)
  217. << "messages = " << msgs.to_string();
  218. common::Trace<common::DoubleTime>::trace().flush();
  219. #endif
  220. _phase = SEND;
  221. }
  222. void dconf(typename common::DoubleTime::type t,
  223. typename common::DoubleTime::type e,
  224. const common::Bag<common::DoubleTime>& msgs) override
  225. {
  226. #ifndef WITH_TRACE
  227. (void)t;
  228. (void)msgs;
  229. #endif
  230. dext(t, e, msgs);
  231. #ifdef WITH_TRACE
  232. common::Trace<common::DoubleTime>::trace()
  233. << common::TraceElement<common::DoubleTime>(get_name(), t,
  234. common::DELTA_CONF)
  235. << "messages = " << msgs.to_string();
  236. common::Trace<common::DoubleTime>::trace().flush();
  237. #endif
  238. }
  239. typename common::DoubleTime::type
  240. start(typename common::DoubleTime::type t) override
  241. {
  242. #ifndef WITH_TRACE
  243. (void)t;
  244. #endif
  245. #ifdef WITH_TRACE
  246. common::Trace<common::DoubleTime>::trace()
  247. << common::TraceElement<common::DoubleTime>(get_name(), t,
  248. common::START);
  249. common::Trace<common::DoubleTime>::trace().flush();
  250. #endif
  251. _phase = WAIT;
  252. return common::DoubleTime::infinity;
  253. }
  254. typename common::DoubleTime::type ta(
  255. typename common::DoubleTime::type t) const override
  256. {
  257. #ifndef WITH_TRACE
  258. (void)t;
  259. #endif
  260. #ifdef WITH_TRACE
  261. common::Trace<common::DoubleTime>::trace()
  262. << common::TraceElement<common::DoubleTime>(get_name(), t, common::TA);
  263. common::Trace<common::DoubleTime>::trace().flush();
  264. #endif
  265. if (_phase == WAIT) {
  266. return common::DoubleTime::infinity;
  267. } else {
  268. return 0;
  269. }
  270. }
  271. common::Bag<common::DoubleTime> lambda(
  272. typename common::DoubleTime::type t) const override
  273. {
  274. #ifndef WITH_TRACE
  275. (void)t;
  276. #endif
  277. common::Bag<common::DoubleTime> msgs;
  278. if (_phase == SEND) {
  279. msgs.push_back(
  280. artis::common::ExternalEvent<common::DoubleTime>(OUT, _value));
  281. }
  282. #ifdef WITH_TRACE
  283. common::Trace<common::DoubleTime>::trace()
  284. << common::TraceElement<common::DoubleTime>(get_name(), t,
  285. common::LAMBDA)
  286. << "messages = " << msgs.to_string();
  287. common::Trace<common::DoubleTime>::trace().flush();
  288. #endif
  289. return msgs;
  290. }
  291. private:
  292. enum Phase {
  293. WAIT, SEND
  294. };
  295. Phase _phase;
  296. double _value;
  297. };
  298. class TwoStateModel : public artis::pdevs::Dynamics<common::DoubleTime, TwoStateModel> {
  299. public:
  300. TwoStateModel(const std::string& name,
  301. const artis::pdevs::Context<common::DoubleTime, TwoStateModel, artis::common::NoParameters>& context)
  302. :
  303. artis::pdevs::Dynamics<common::DoubleTime, TwoStateModel>(name, context) { }
  304. ~TwoStateModel() override = default;
  305. void dint(typename common::DoubleTime::type t) override
  306. {
  307. if (_phase == S1) {
  308. _phase = S2;
  309. } else if (_phase == S2) {
  310. _phase = S1;
  311. }
  312. _last_time = t;
  313. }
  314. typename common::DoubleTime::type
  315. start(typename common::DoubleTime::type t) override
  316. {
  317. _phase = S1;
  318. _last_time = t;
  319. return ta(t);
  320. }
  321. typename common::DoubleTime::type
  322. ta(typename common::DoubleTime::type /* t */) const override
  323. {
  324. if (_phase == S1) {
  325. return 5;
  326. } else {
  327. return 6;
  328. }
  329. }
  330. common::Bag<common::DoubleTime>
  331. lambda(typename common::DoubleTime::type t) const override
  332. {
  333. std::cout << (t - _last_time) << std::endl;
  334. return common::Bag<common::DoubleTime>();
  335. }
  336. private:
  337. enum Phase {
  338. S1, S2
  339. };
  340. Phase _phase;
  341. typename common::DoubleTime::type _last_time;
  342. };
  343. class ThreeStateModel
  344. : public artis::pdevs::Dynamics<common::DoubleTime, ThreeStateModel> {
  345. public:
  346. enum outputs {
  347. OUT
  348. };
  349. enum states {
  350. HEIGHTS, SPEEDS, SCALES, N, INDEX, SIGMA, LAST_TIME
  351. };
  352. ThreeStateModel(const std::string& name,
  353. const artis::pdevs::Context<common::DoubleTime, ThreeStateModel, artis::common::NoParameters>& context)
  354. :
  355. artis::pdevs::Dynamics<common::DoubleTime, ThreeStateModel>(name, context)
  356. {
  357. DECLARE_STATES(std::vector<double>,
  358. ((HEIGHTS, &ThreeStateModel::heights), (SPEEDS, &ThreeStateModel::speeds), (SCALES, &ThreeStateModel::scales)));
  359. DECLARE_STATES(unsigned int,
  360. ((N, &ThreeStateModel::n), (INDEX, &ThreeStateModel::index)));
  361. DECLARE_STATES(typename common::DoubleTime::type,
  362. ((SIGMA, &ThreeStateModel::sigma), (LAST_TIME, &ThreeStateModel::_last_time)));
  363. output_ports({{OUT, "out"}});
  364. }
  365. ~ThreeStateModel() override = default;
  366. void dconf(typename common::DoubleTime::type t,
  367. typename common::DoubleTime::type e,
  368. const common::Bag<common::DoubleTime>& msgs) override
  369. {
  370. dext(t, e, msgs);
  371. }
  372. void dext(typename common::DoubleTime::type /* t */,
  373. typename common::DoubleTime::type /* e */,
  374. const common::Bag<common::DoubleTime>& msgs) override
  375. {
  376. for (common::Bag<common::DoubleTime>::const_iterator it = msgs.begin();
  377. it != msgs.end();
  378. ++it) {
  379. ++n;
  380. }
  381. if (sigma == 1) {
  382. if (n > 3) {
  383. ++index;
  384. if (index == scales.size()) {
  385. index = 0;
  386. }
  387. sigma = common::DoubleTime::infinity;
  388. if (scales[index] == 1) {
  389. scales[index] = 2;
  390. } else {
  391. scales[index] = 1;
  392. }
  393. n = 0;
  394. }
  395. } else {
  396. sigma = 1;
  397. n = 0;
  398. }
  399. }
  400. void dint(typename common::DoubleTime::type t) override
  401. {
  402. mark_full(t);
  403. if (full_N()) {
  404. raz();
  405. }
  406. compute();
  407. }
  408. typename common::DoubleTime::type
  409. start(typename common::DoubleTime::type t) override
  410. {
  411. heights = {0, 0, 0, 0, 0};
  412. speeds = {0.21, 0.3, 0.7, 0.56, 0.14};
  413. scales = {1, 1, 1, 1, 1};
  414. index = 0;
  415. n = 0;
  416. sigma = 1;
  417. _last_time = t;
  418. return 0;
  419. }
  420. typename common::DoubleTime::type
  421. ta(typename common::DoubleTime::type /* t */) const override { return sigma; }
  422. common::Bag<common::DoubleTime>
  423. lambda(typename common::DoubleTime::type /* t */) const override
  424. {
  425. common::Bag<common::DoubleTime> msgs;
  426. if (full()) {
  427. msgs.push_back(artis::common::ExternalEvent<common::DoubleTime>(OUT, 0));
  428. }
  429. return msgs;
  430. }
  431. private:
  432. void compute()
  433. {
  434. for (unsigned int i = 0; i < heights.size(); ++i) {
  435. if (heights[i] != -1 and heights[i] < 10) {
  436. heights[i] += speeds[i] * scales[i];
  437. }
  438. }
  439. }
  440. void display() const
  441. {
  442. for (std::vector<double>::const_iterator it = heights.begin();
  443. it != heights.end(); ++it) {
  444. std::cout << *it << " ";
  445. }
  446. std::cout << std::endl;
  447. }
  448. void display_full() const
  449. {
  450. unsigned int i = 1;
  451. for (std::vector<double>::const_iterator it = heights.begin();
  452. it != heights.end(); ++it, ++i) {
  453. if (*it > 10) {
  454. std::cout << "S" << i;
  455. }
  456. }
  457. std::cout << std::endl;
  458. }
  459. bool full() const
  460. {
  461. unsigned int n = 0;
  462. for (std::vector<double>::const_iterator it = heights.begin();
  463. it != heights.end(); ++it) {
  464. if (*it > 10) {
  465. ++n;
  466. }
  467. }
  468. return n > 0;
  469. }
  470. bool full_N() const
  471. {
  472. unsigned int n = 0;
  473. for (std::vector<double>::const_iterator it = heights.begin();
  474. it != heights.end(); ++it) {
  475. if (*it == -1) {
  476. ++n;
  477. }
  478. }
  479. return n >= 2;
  480. }
  481. void mark_full(typename common::DoubleTime::type t)
  482. {
  483. for (std::vector<double>::iterator it = heights.begin();
  484. it != heights.end(); ++it) {
  485. if (*it > 10) {
  486. *it = -1;
  487. _last_time = t;
  488. }
  489. }
  490. }
  491. void raz()
  492. {
  493. for (std::vector<double>::iterator it = heights.begin();
  494. it != heights.end(); ++it) {
  495. if (*it == -1) {
  496. *it = 0;
  497. }
  498. }
  499. }
  500. // state
  501. std::vector<double> heights;
  502. std::vector<double> speeds;
  503. std::vector<double> scales;
  504. unsigned int index;
  505. unsigned int n;
  506. typename common::DoubleTime::type sigma;
  507. typename common::DoubleTime::type _last_time;
  508. };
  509. }
  510. }
  511. } // namespace artis tests pdevs
  512. #endif