models.hpp 22 KB

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