AbstractAtomicModel.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * @file artis/kernel/AbstractModel.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2012-2016 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 __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
  22. #define __ARTIS_KERNEL_ABSTRACT_ATOMIC_MODEL_HPP
  23. #include <artis/kernel/AbstractModel.hpp>
  24. #include <artis/kernel/AbstractCoupledModel.hpp>
  25. #include <artis/kernel/Internals.hpp>
  26. #include <artis/kernel/States.hpp>
  27. #include <artis/utils/Exception.hpp>
  28. #include <vector>
  29. namespace artis { namespace kernel {
  30. template < typename T, typename U, typename V >
  31. class AbstractAtomicModel : public AbstractModel < U, V >,
  32. public States < T, U >,
  33. public Internals < T, U >,
  34. public Externals < T, U >
  35. {
  36. typedef AbstractModel < U, V > type;
  37. public:
  38. AbstractAtomicModel()
  39. { }
  40. virtual ~AbstractAtomicModel()
  41. { }
  42. virtual bool check(typename U::type t) const
  43. { return Externals < T, U >::check(t); }
  44. virtual void compute(typename U::type t, bool update) = 0;
  45. virtual double get(typename U::type t, unsigned int index) const
  46. {
  47. if (type::last_time != t) {
  48. throw utils::InvalidGet("Variable not computed");
  49. }
  50. return static_cast < const T* >(this)->*(
  51. Internals < T, U >::get(index));
  52. }
  53. virtual int getI(typename U::type t, unsigned int index) const
  54. {
  55. if (type::last_time != t) {
  56. throw utils::InvalidGet("Variable not computed");
  57. }
  58. return static_cast < const T* >(this)->*(
  59. Internals < T, U >::getI(index));
  60. }
  61. virtual bool getB(typename U::type t, unsigned int index) const
  62. {
  63. if (type::last_time != t) {
  64. throw utils::InvalidGet("Variable not computed");
  65. }
  66. return static_cast < const T* >(this)->*(
  67. Internals < T, U >::getB(index));
  68. }
  69. virtual void init(typename U::type t, const V& parameters) = 0;
  70. bool is_computed(typename U::type t, unsigned int /* index */) const
  71. { return type::last_time == t; }
  72. // bool is_ready(typename U::type t, unsigned int index) const
  73. // { return type::externalDates.at(index) == t; }
  74. // bool is_readyV(typename U::type t, unsigned int index) const
  75. // { return type::externalDatesV.at(index) == t; }
  76. bool is_stable(typename U::type t) const
  77. { return type::last_time == t; }
  78. virtual bool is_updated() const
  79. { return Externals < T, U >::updated; }
  80. // void put(typename U::type t, unsigned int index, double value)
  81. // {
  82. // if (type::externalDates.at(index) != t) {
  83. // static_cast < T* >(this)->*externals.at(index) = value;
  84. // type::externalDates.at(index) = t;
  85. // type::updated = true;
  86. // } else {
  87. // if (static_cast < T* >(this)->*externals.at(index) != value) {
  88. // static_cast < T* >(this)->*externals.at(index) = value;
  89. // type::updated = true;
  90. // }
  91. // }
  92. // }
  93. // void put(typename U::type t, unsigned int index,
  94. // const std::vector < double >& value)
  95. // {
  96. // if (type::externalDatesV.at(index) != t) {
  97. // static_cast < T* >(this)->*externalsV.at(index) = value;
  98. // type::externalDatesV.at(index) = t;
  99. // type::updated = true;
  100. // } else {
  101. // if (static_cast < T* >(this)->*externalsV.at(index) != value) {
  102. // static_cast < T* >(this)->*externalsV.at(index) = value;
  103. // type::updated = true;
  104. // }
  105. // }
  106. // }
  107. virtual void restore(const State < U >& state)
  108. {
  109. States < T, U >::restore(state);
  110. Internals < T, U >::restore(state);
  111. type::last_time = state.last_time();
  112. }
  113. virtual void save(State < U >& state) const
  114. {
  115. States < T, U >::save(state);
  116. Internals < T, U >::save(state);
  117. state.last_time(type::last_time);
  118. }
  119. virtual void stable()
  120. { Externals < T, U >::updated = false; }
  121. protected:
  122. // void external(unsigned int index, double T::* var)
  123. // {
  124. // if (externals.size() <= index) {
  125. // externals.resize(index + 1);
  126. // type::externalDates.resize(index + 1);
  127. // }
  128. // externals[index] = var;
  129. // type::externalDates[index] = -1;
  130. // }
  131. // void externalV(unsigned int index, std::vector < double > T::* var)
  132. // {
  133. // if (externalsV.size() <= index) {
  134. // externalsV.resize(index + 1);
  135. // type::externalDatesV.resize(index + 1);
  136. // }
  137. // externalsV[index] = var;
  138. // type::externalDatesV[index] = -1;
  139. // }
  140. private:
  141. // std::vector < double T::* > externals;
  142. // std::vector < std::vector < double > T::* > externalsV;
  143. };
  144. template < typename T, typename U, typename V >
  145. struct SA_t
  146. {
  147. SA_t(double t, AbstractAtomicModel < T, U, V >* model,
  148. unsigned int index) : t(t), model(model), index(index)
  149. { }
  150. double t;
  151. AbstractAtomicModel < T, U, V >* model;
  152. unsigned int index;
  153. };
  154. template < typename T, typename U, typename V >
  155. struct IN_SA_t : public SA_t < T, U, V >
  156. {
  157. IN_SA_t(double t, AbstractAtomicModel < T, U, V >* model,
  158. unsigned int index) : SA_t < T, U , V >(t, model, index)
  159. { }
  160. };
  161. template < typename T, typename U, typename V >
  162. struct IN_SA_I_t : public IN_SA_t < T, U, V >
  163. {
  164. IN_SA_I_t(double t, AbstractAtomicModel < T, U, V >* model,
  165. unsigned int index) : IN_SA_t < T, U , V >(t, model, index)
  166. { }
  167. };
  168. template < typename T, typename U, typename V >
  169. struct IN_SA_B_t : public IN_SA_t < T, U, V >
  170. {
  171. IN_SA_B_t(double t, AbstractAtomicModel < T, U, V >* model,
  172. unsigned int index) : IN_SA_t < T, U , V >(t, model, index)
  173. { }
  174. };
  175. template < typename T, typename U, typename V >
  176. struct OUT_SA_t : public SA_t < T, U, V >
  177. {
  178. OUT_SA_t(double t, AbstractAtomicModel < T, U, V >* model,
  179. unsigned int index) : SA_t < T, U , V >(t, model, index)
  180. { }
  181. };
  182. template < typename T, typename U, typename V >
  183. struct OUT_SA_I_t : public OUT_SA_t < T, U, V >
  184. {
  185. OUT_SA_I_t(double t, AbstractAtomicModel < T, U, V >* model,
  186. unsigned int index) : OUT_SA_t < T, U , V >(t, model, index)
  187. { }
  188. };
  189. template < typename T, typename U, typename V >
  190. struct OUT_SA_B_t : public OUT_SA_t < T, U, V >
  191. {
  192. OUT_SA_B_t(double t, AbstractAtomicModel < T, U, V >* model,
  193. unsigned int index) : OUT_SA_t < T, U , V >(t, model, index)
  194. { }
  195. };
  196. template < typename T, typename U, typename V >
  197. IN_SA_t < T, U, V > IN(double t, AbstractAtomicModel < T, U, V >* model,
  198. unsigned int index)
  199. { return IN_SA_t < T, U, V >(t, model, index); }
  200. template < typename T, typename U, typename V >
  201. IN_SA_I_t < T, U, V > IN_I(double t, AbstractAtomicModel < T, U, V >* model,
  202. unsigned int index)
  203. { return IN_SA_I_t < T, U, V >(t, model, index); }
  204. template < typename T, typename U, typename V >
  205. IN_SA_B_t < T, U, V > IN_B(double t, AbstractAtomicModel < T, U, V >* model,
  206. unsigned int index)
  207. { return IN_SA_B_t < T, U, V >(t, model, index); }
  208. template < typename T, typename U, typename V >
  209. OUT_SA_t < T, U, V > OUT(double t,
  210. AbstractAtomicModel < T, U, V >* model,
  211. unsigned int index)
  212. { return OUT_SA_t < T, U, V >(t, model, index); }
  213. template < typename T, typename U, typename V >
  214. OUT_SA_I_t < T, U, V > OUT_I(double t,
  215. AbstractAtomicModel < T, U, V >* model,
  216. unsigned int index)
  217. { return OUT_SA_I_t < T, U, V >(t, model, index); }
  218. template < typename T, typename U, typename V >
  219. OUT_SA_B_t < T, U, V > OUT_B(double t,
  220. AbstractAtomicModel < T, U, V >* model,
  221. unsigned int index)
  222. { return OUT_SA_B_t < T, U, V >(t, model, index); }
  223. template < typename T1, typename U1, typename V1,
  224. typename T2, typename U2, typename V2 >
  225. void operator>>(OUT_SA_t < T2, U2, V2 > out, IN_SA_t < T1, U1, V1 > in)
  226. {
  227. if (out.model->is_computed(out.t, out.index)) {
  228. in.model->put(out.t, in.index, out.model->get(out.t, out.index));
  229. }
  230. }
  231. template < typename T1, typename U1, typename V1,
  232. typename T2, typename U2, typename V2 >
  233. void operator>>(OUT_SA_I_t < T2, U2, V2 > out, IN_SA_I_t < T1, U1, V1 > in)
  234. {
  235. if (out.model->is_computed(out.t, out.index)) {
  236. in.model->put(out.t, in.index, out.model->getI(out.t, out.index));
  237. }
  238. }
  239. template < typename T1, typename U1, typename V1,
  240. typename T2, typename U2, typename V2 >
  241. void operator>>(OUT_SA_B_t < T2, U2, V2 > out, IN_SA_B_t < T1, U1, V1 > in)
  242. {
  243. if (out.model->is_computed(out.t, out.index)) {
  244. in.model->put(out.t, in.index, out.model->getB(out.t, out.index));
  245. }
  246. }
  247. template < typename T1, typename U1, typename V1, typename W1,
  248. typename T2, typename U2, typename V2 >
  249. void operator>>(OUT_SA_t < T2, U2, V2 > out, IN_SC_t < T1, U1, V1, W1 > in)
  250. {
  251. if (out.model->is_computed(out.t, out.index)) {
  252. in.model->put(out.t, in.index, out.model->get(out.t, out.index));
  253. }
  254. }
  255. template < typename T1, typename U1, typename V1, typename W1,
  256. typename T2, typename U2, typename V2 >
  257. void operator>>(OUT_SA_B_t < T2, U2, V2 > out, IN_SC_B_t < T1, U1, V1, W1 > in)
  258. {
  259. if (out.model->is_computed(out.t, out.index)) {
  260. in.model->put(out.t, in.index, out.model->getB(out.t, out.index));
  261. }
  262. }
  263. template < typename T1, typename U1, typename V1, typename W1,
  264. typename T2, typename U2, typename V2 >
  265. void operator>>(OUT_SA_I_t < T2, U2, V2 > out, IN_SC_I_t < T1, U1, V1, W1 > in)
  266. {
  267. if (out.model->is_computed(out.t, out.index)) {
  268. in.model->put(out.t, in.index, out.model->getI(out.t, out.index));
  269. }
  270. }
  271. } }
  272. #endif