main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @file pdevs/tests.cpp
  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. #include <tests/qss/graph_manager.hpp>
  26. #include <artis-star/common/RootCoordinator.hpp>
  27. #include <artis-star/common/observer/Iterator.hpp>
  28. #include <fstream>
  29. #include <iostream>
  30. #include <boost/archive/binary_oarchive.hpp>
  31. using namespace artis::tests::qss;
  32. void test_parabola()
  33. {
  34. artis::pdevs::qss::QSSParameters<ParabolaParameters> parameters = {{0.5},
  35. {true, true, 0.001, 3},
  36. {0.2}};
  37. class View : public artis::observer::View<artis::common::DoubleTime> {
  38. public:
  39. View()
  40. {
  41. selector("Value", {OnlyOneParabolaGraphManager::A, ParabolaGraphManager::S_Integrator,
  42. artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
  43. }
  44. };
  45. artis::common::context::Context<artis::common::DoubleTime> context(0, 5);
  46. artis::common::RootCoordinator<
  47. artis::common::DoubleTime, artis::pdevs::Coordinator<
  48. artis::common::DoubleTime,
  49. OnlyOneParabolaGraphManager,
  50. artis::pdevs::qss::QSSParameters<ParabolaParameters>>
  51. > rc(context, "root", parameters, artis::common::NoParameters());
  52. rc.attachView("Value", new View());
  53. rc.run(context);
  54. const View::Values& values = rc.observer().view("Value").get("Value");
  55. for (const auto& value: values) {
  56. double v;
  57. value.second(v);
  58. std::cout << value.first << ": " << v << std::endl;
  59. }
  60. }
  61. class PredatorPreyView : public artis::observer::View<artis::common::DoubleTime> {
  62. public:
  63. PredatorPreyView()
  64. {
  65. selector("PredatorView",
  66. {PreyPredatorGraphManager::PREDATOR, PredatorGraphManager::S_Integrator,
  67. artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
  68. selector("PreyView",
  69. {PreyPredatorGraphManager::PREY, PreyGraphManager::S_Integrator,
  70. artis::pdevs::qss::Integrator<artis::common::DoubleTime>::VALUE});
  71. }
  72. };
  73. void run_predator_prey(artis::common::context::Context<artis::common::DoubleTime>& context,
  74. const PreyPredatorGraphManagerParameters& parameters)
  75. {
  76. artis::common::RootCoordinator<
  77. artis::common::DoubleTime, artis::pdevs::Coordinator<
  78. artis::common::DoubleTime,
  79. PreyPredatorGraphManager,
  80. PreyPredatorGraphManagerParameters>
  81. > rc(context, "root", parameters, artis::common::NoParameters());
  82. rc.attachView("Value", new PredatorPreyView());
  83. rc.run(context);
  84. rc.save(context);
  85. std::ofstream os("state");
  86. boost::archive::binary_oarchive oa(os);
  87. oa << context;
  88. {
  89. artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
  90. rc.observer().view("Value").get("PredatorView"), context.begin(), 0.1);
  91. while (it.has_next()) {
  92. double v;
  93. (*it).second(v);
  94. std::cout << (*it).first << ";" << v << std::endl;
  95. ++it;
  96. }
  97. }
  98. {
  99. artis::observer::DiscreteTimeIterator<artis::common::DoubleTime> it(
  100. rc.observer().view("Value").get("PreyView"), context.begin(), 0.1);
  101. while (it.has_next()) {
  102. double v;
  103. (*it).second(v);
  104. std::cout << (*it).first << ";" << v << std::endl;
  105. ++it;
  106. }
  107. }
  108. }
  109. void test_predator_prey()
  110. {
  111. PreyPredatorGraphManagerParameters parameters = {{{45.},
  112. {true, true, 0.1, 3},
  113. {0.5, 0.01, 0.01, 0.2}},
  114. {{5000.},
  115. {true, true, 1, 3},
  116. {0.5, 0.01, 0.01, 0.2}}};
  117. artis::common::context::Context<artis::common::DoubleTime> context(0, 100);
  118. run_predator_prey(context, parameters);
  119. artis::common::context::Context<artis::common::DoubleTime> new_context(context);
  120. new_context.end(200);
  121. run_predator_prey(new_context, parameters);
  122. }
  123. int main()
  124. {
  125. // test_parabola();
  126. test_predator_prey();
  127. }