models.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, ModelParameters>;
  32. using Models = artis::kernel::AbstractModels<artis::utils::DoubleTime, ModelParameters>;
  33. using Trace = artis::utils::Trace<artis::utils::DoubleTime>;
  34. using TraceElement = artis::utils::TraceElement<artis::utils::DoubleTime>;
  35. template<typename T>
  36. using AtomicModel = artis::kernel::AbstractAtomicModel<T, artis::utils::DoubleTime, ModelParameters>;
  37. template<typename T>
  38. using CoupledModel = artis::kernel::AbstractCoupledModel<T, artis::utils::DoubleTime, ModelParameters, GlobalParameters>;
  39. class AModel : public AtomicModel<AModel> {
  40. public:
  41. enum externals {
  42. };
  43. enum internals {
  44. DX, BX, IX
  45. };
  46. AModel(const Models& = Models())
  47. :_ix(0), _bx(false), _dx(0.0)
  48. {
  49. // internals
  50. Internal(IX, &AModel::_ix);
  51. Internal(BX, &AModel::_bx);
  52. Internal(DX, &AModel::_dx);
  53. }
  54. ~AModel() override = default;
  55. void compute(double t, bool /* update */) override
  56. {
  57. Trace::trace() << TraceElement("A", t, artis::utils::COMPUTE) << "Start";
  58. Trace::trace().flush();
  59. ++_ix;
  60. _bx = not _bx;
  61. ++_dx;
  62. Trace::trace() << TraceElement("A", t, artis::utils::COMPUTE) << "Stop";
  63. Trace::trace().flush();
  64. }
  65. void init(double /* t */, const ModelParameters& /* parameters */) override
  66. {
  67. _ix = 0;
  68. _bx = false;
  69. _dx = 0.;
  70. }
  71. //private:
  72. int _ix;
  73. bool _bx;
  74. double _dx;
  75. };
  76. class BModel : public AtomicModel<BModel> {
  77. public:
  78. enum externals {
  79. IX, BX, DX
  80. };
  81. enum internals {
  82. IY, IZ, BY, DY
  83. };
  84. enum states {
  85. N
  86. };
  87. BModel(const Models& = Models())
  88. :_ix(0), _bx(false), _dx(0.0), _iy(0), _by(false), _dy(0.0), _iz(0), _n(0)
  89. {
  90. // externals
  91. Externals(int, ((IX, &BModel::_ix)));
  92. External(BX, &BModel::_bx);
  93. External(DX, &BModel::_dx);
  94. // internals
  95. Internals(int, ((IY, &BModel::_iy), (IZ, &BModel::_iz)));
  96. Internal(BY, &BModel::_by);
  97. Internal(DY, &BModel::_dy);
  98. States(int, ((N, &BModel::_n)));
  99. }
  100. ~BModel() override = default;
  101. void compute(double t, bool /* update */) override
  102. {
  103. Trace::trace() << TraceElement("B", t, artis::utils::COMPUTE) << "Start";
  104. Trace::trace().flush();
  105. _iy = _ix + 1;
  106. _by = not _bx;
  107. _dy = _dx + 1;
  108. ++_n;
  109. Trace::trace() << TraceElement("B", t, artis::utils::COMPUTE) << "Stop";
  110. Trace::trace().flush();
  111. }
  112. void init(double /* t */, const ModelParameters& /* parameters */) override
  113. {
  114. _iy = 0;
  115. _by = false;
  116. _dy = 0.;
  117. _iz = 0;
  118. _n = 0;
  119. }
  120. private:
  121. // externals
  122. int _ix;
  123. bool _bx;
  124. double _dx;
  125. // internals
  126. int _iy;
  127. bool _by;
  128. double _dy;
  129. int _iz;
  130. // states
  131. int _n;
  132. };
  133. class RootModel : public CoupledModel<RootModel> {
  134. public:
  135. enum submodels {
  136. A, B
  137. };
  138. enum internals {
  139. IY, BY, DY, IZ, DX
  140. };
  141. enum states {
  142. N
  143. };
  144. RootModel(Models submodels)
  145. :
  146. _a(dynamic_cast < AModel* >(submodels[0])),
  147. _b(dynamic_cast < BModel* >(submodels[1])),
  148. _dx(0.0),
  149. _n(0)
  150. {
  151. // submodels
  152. Submodels(((A, _a), (B, _b)));
  153. // internals
  154. Internal(DX, &RootModel::_dx);
  155. InternalsS(((IY, _b, BModel::IY), (IZ, _b, BModel::IZ)));
  156. InternalS(BY, _b, BModel::BY);
  157. // states
  158. States(int, ((N, &RootModel::_n)));
  159. // State(N, &RootModel::_n);
  160. }
  161. RootModel()
  162. :_a(new AModel),
  163. _b(new BModel),
  164. _dx(0.0),
  165. _n(0)
  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. }
  176. ~RootModel() override
  177. {
  178. delete _a;
  179. delete _b;
  180. }
  181. void compute(double t, bool /* update */) override
  182. {
  183. Trace::trace() << TraceElement(this->path(this), t, artis::utils::COMPUTE) << "Start";
  184. Trace::trace().flush();
  185. (*_a)(t);
  186. _b->put<double>(t, BModel::DX, _a->get<double>(t, AModel::DX));
  187. _b->put<int>(t, BModel::IX, _a->get<int>(t, AModel::IX));
  188. _b->put<bool>(t, BModel::BX, _a->get<bool>(t, AModel::BX));
  189. (*_b)(t);
  190. ++_n;
  191. Trace::trace() << TraceElement(this->path(this), t, artis::utils::COMPUTE) << "Stop";
  192. Trace::trace().flush();
  193. }
  194. void init(double t, const ModelParameters& parameters) override
  195. {
  196. _a->init(t, parameters);
  197. _b->init(t, parameters);
  198. _n = 0;
  199. _dx = 0;
  200. }
  201. private:
  202. // submodels
  203. AModel* _a;
  204. BModel* _b;
  205. //internals
  206. double _dx;
  207. // states
  208. int _n;
  209. };
  210. #endif