models.hpp 17 KB

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