Output.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @file observer/Output.hpp
  3. * @author See the AUTHORS file
  4. */
  5. /*
  6. * Copyright (C) 2013-2021 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_COMMON_OBSERVER_OUTPUT_HPP
  22. #define ARTIS_COMMON_OBSERVER_OUTPUT_HPP
  23. #include <artis-star/common/observer/Iterator.hpp>
  24. #include <artis-star/common/observer/Observer.hpp>
  25. #include <artis-star/common/observer/View.hpp>
  26. #include <boost/format.hpp>
  27. #include <fstream>
  28. namespace artis {
  29. namespace observer {
  30. template<typename Time>
  31. struct EventIterator
  32. {
  33. typedef artis::observer::Iterator<Time> iterator_type;
  34. double begin(const std::vector <Iterator<Time>> &its) const
  35. { return next_time(0, its); }
  36. iterator_type build(const typename View<Time>::Values &view) const
  37. { return iterator_type(view); }
  38. bool is_valid(double time, const iterator_type &it) const
  39. { return (*it).first == time; }
  40. double next_time(double /* time */, const std::vector <Iterator<Time>> &its) const
  41. {
  42. double min = std::numeric_limits<double>::max();
  43. for (const auto &it: its) {
  44. if (min > (*it).first) {
  45. min = (*it).first;
  46. }
  47. }
  48. return min;
  49. }
  50. };
  51. template<typename Time>
  52. struct TimedIterator
  53. {
  54. typedef artis::observer::DiscreteTimeIterator<Time> iterator_type;
  55. double _begin;
  56. double _step;
  57. double begin(const std::vector <DiscreteTimeIterator<Time>> & /* its */) const
  58. { return _begin; }
  59. iterator_type build(const typename View<Time>::Values &view) const
  60. { return iterator_type(view, _begin, _step); }
  61. bool is_valid(double /* time */, const iterator_type & /* it */) const
  62. { return true; }
  63. double next_time(double time, const std::vector <DiscreteTimeIterator<Time>> & /* its */) const
  64. { return time + _step; }
  65. };
  66. template<typename Time, typename Iterator>
  67. class Output
  68. {
  69. public:
  70. Output(const Observer <Time> &observer)
  71. : _observer(observer)
  72. {}
  73. virtual ~Output()
  74. {}
  75. void operator()(double /* begin */, double end, const Iterator &iterator) const
  76. {
  77. const typename Observer<Time>::Views &views = _observer.views();
  78. for (typename Observer<Time>::Views::const_iterator it =
  79. views.begin(); it != views.end(); ++it) {
  80. std::ofstream o((boost::format("%1%.csv") % it->first).str());
  81. const typename View<Time>::SelectorValues &values = it->second->values();
  82. std::vector<typename Iterator::iterator_type> its;
  83. o.precision(10);
  84. // write header
  85. o << "time";
  86. for (typename View<Time>::SelectorValues::const_iterator itv = values.begin();
  87. itv != values.end(); ++itv) {
  88. const typename View<Time>::VariableValues &vv = itv->second;
  89. for (typename View<Time>::VariableValues::const_iterator itvv = vv.begin();
  90. itvv != vv.end(); ++itvv) {
  91. o << ";" << itvv->first;
  92. its.push_back(iterator.build(itvv->second));
  93. }
  94. }
  95. o << std::endl;
  96. // write values
  97. bool more = true;
  98. double t = iterator.begin(its);
  99. while (more and t <= end) {
  100. more = false;
  101. o << t;
  102. for (auto &dtt: its) {
  103. if (dtt.has_next() and iterator.is_valid(t, dtt)) {
  104. o << ";" << (*dtt).second.to_string();
  105. ++dtt;
  106. more = true;
  107. } else {
  108. o << ";";
  109. }
  110. }
  111. o << std::endl;
  112. t = iterator.next_time(t, its);
  113. }
  114. }
  115. }
  116. private:
  117. const Observer <Time> &_observer;
  118. };
  119. }
  120. }
  121. #endif