AbstractAtomicModel.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 OUT_SA_t
  146. {
  147. OUT_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. OUT_SA_t < T, U, V > OUT(double t,
  156. AbstractAtomicModel < T, U, V >* model,
  157. unsigned int index)
  158. { return OUT_SA_t < T, U, V >(t, model, index); }
  159. template < typename T1, typename U1, typename V1, typename W1,
  160. typename T2, typename U2, typename V2 >
  161. void operator>>(OUT_SA_t < T2, U2, V2 > out, IN_t < T1, U1, V1, W1 > in)
  162. {
  163. if (out.model->is_computed(out.t, out.index)) {
  164. in.model->put(out.t, in.index, out.model->get(out.t, out.index));
  165. }
  166. }
  167. } }
  168. #endif