models.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @file test/models.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2019 ULCO http://www.univ-littoral.fr
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef TEST_MODELS_HPP
  22. #define TEST_MODELS_HPP
  23. #include <artis/kernel/AbstractAtomicModel.hpp>
  24. #include <artis/kernel/AbstractCoupledModel.hpp>
  25. #include <artis/utils/Trace.hpp>
  26. #include <memory>
  27. struct GlobalParameters {
  28. };
  29. struct ModelParameters {
  30. };
  31. using Model = artis::kernel::AbstractModel<artis::utils::DoubleTime,
  32. ModelParameters>;
  33. using Models = artis::kernel::AbstractModels<artis::utils::DoubleTime,
  34. ModelParameters>;
  35. using Trace = artis::utils::Trace<artis::utils::DoubleTime>;
  36. using TraceElement = artis::utils::TraceElement<artis::utils::DoubleTime>;
  37. template<typename T>
  38. using AtomicModel = artis::kernel::AbstractAtomicModel<
  39. T, artis::utils::DoubleTime, ModelParameters>;
  40. template<typename T>
  41. using CoupledModel = artis::kernel::AbstractCoupledModel<
  42. T, artis::utils::DoubleTime, ModelParameters, GlobalParameters>;
  43. class AModel : public AtomicModel<AModel> {
  44. public:
  45. enum externals {
  46. };
  47. enum internals {
  48. DX, BX, IX
  49. };
  50. AModel(Models = Models())
  51. {
  52. Internal(IX, &AModel::_ix);
  53. Internal(BX, &AModel::_bx);
  54. Internal(DX, &AModel::_dx);
  55. }
  56. virtual ~AModel() { }
  57. void compute(double t, bool /* update */)
  58. {
  59. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
  60. << "Start";
  61. ::Trace::trace().flush();
  62. ++_ix;
  63. _bx = not _bx;
  64. ++_dx;
  65. ::Trace::trace() << ::TraceElement("A", t, artis::utils::COMPUTE)
  66. << "Stop";
  67. ::Trace::trace().flush();
  68. }
  69. void init(double /* t */, const ModelParameters& /* parameters */)
  70. {
  71. _ix = 0;
  72. _bx = false;
  73. _dx = 0.;
  74. }
  75. //private:
  76. int _ix;
  77. bool _bx;
  78. double _dx;
  79. };
  80. class BModel : public AtomicModel<BModel> {
  81. public:
  82. enum externals {
  83. IX, BX, DX
  84. };
  85. enum internals {
  86. IY, IZ, BY, DY
  87. };
  88. enum states {
  89. N
  90. };
  91. BModel(Models = Models())
  92. {
  93. // external(IX, &BModel::_ix);
  94. Externals(int, ((IX, &BModel::_ix)));
  95. External(BX, &BModel::_bx);
  96. External(DX, &BModel::_dx);
  97. // internal(IY, &BModel::_iy);
  98. Internals(int, ((IY, &BModel::_iy), (IZ, &BModel::_iz)));
  99. Internal(BY, &BModel::_by);
  100. Internal(DY, &BModel::_dy);
  101. States(int, ((N, &BModel::_n)));
  102. }
  103. virtual ~BModel() { }
  104. void compute(double t, bool /* update */)
  105. {
  106. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
  107. << "Start";
  108. ::Trace::trace().flush();
  109. _iy = _ix + 1;
  110. _by = not _bx;
  111. _dy = _dx + 1;
  112. ++_n;
  113. ::Trace::trace() << ::TraceElement("B", t, artis::utils::COMPUTE)
  114. << "Stop";
  115. ::Trace::trace().flush();
  116. }
  117. void init(double /* t */, const ModelParameters& /* parameters */)
  118. {
  119. _iy = 0;
  120. _by = false;
  121. _dy = 0.;
  122. _iz = 0;
  123. _n = 0;
  124. }
  125. private:
  126. // externals
  127. int _ix;
  128. bool _bx;
  129. double _dx;
  130. // internals
  131. int _iy;
  132. bool _by;
  133. double _dy;
  134. int _iz;
  135. // states
  136. int _n;
  137. };
  138. class RootModel : public CoupledModel<RootModel> {
  139. public:
  140. enum submodels {
  141. A, B
  142. };
  143. enum internals {
  144. IY, BY, DY, IZ, DX
  145. };
  146. enum states {
  147. N
  148. };
  149. RootModel(Models submodels)
  150. :
  151. _a(dynamic_cast < AModel* >(submodels[0])),
  152. _b(dynamic_cast < BModel* >(submodels[1]))
  153. {
  154. // submodels
  155. Submodels(((A, _a), (B, _b)));
  156. // internals
  157. Internal(DX, &RootModel::_dx);
  158. InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
  159. InternalS(BY, _b, BModel::BY);
  160. // states
  161. States(int, ((N, &RootModel::_n)));
  162. // State(N, &RootModel::_n);
  163. }
  164. RootModel()
  165. :_a(new AModel), _b(new BModel)
  166. {
  167. // submodels
  168. Submodels(((A, _a), (B, _b)));
  169. // internals
  170. Internal(DX, &RootModel::_dx);
  171. InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
  172. InternalS(BY, _b, BModel::BY);
  173. // states
  174. States(int, ((N, &RootModel::_n)));
  175. // State(N, &RootModel::_n);
  176. }
  177. virtual ~RootModel()
  178. {
  179. delete _a;
  180. delete _b;
  181. }
  182. void compute(double t, bool /* update */)
  183. {
  184. ::Trace::trace() << ::TraceElement(this->path(this), t,
  185. artis::utils::COMPUTE)
  186. << "Start";
  187. ::Trace::trace().flush();
  188. (*_a)(t);
  189. _b->put<double>(t, BModel::DX, _a->get<double>(t, AModel::DX));
  190. _b->put<int>(t, BModel::IX, _a->get<int>(t, AModel::IX));
  191. _b->put<bool>(t, BModel::BX, _a->get<bool>(t, AModel::BX));
  192. (*_b)(t);
  193. ++_n;
  194. this->trace_element(t, artis::utils::COMPUTE, "Stop");
  195. }
  196. void init(double t, const ModelParameters& parameters)
  197. {
  198. _a->init(t, parameters);
  199. _b->init(t, parameters);
  200. _n = 0;
  201. _dx = 0;
  202. }
  203. private:
  204. // submodels
  205. AModel* _a;
  206. BModel* _b;
  207. //internals
  208. double _dx;
  209. // states
  210. int _n;
  211. };
  212. #endif