Virtual Testbed
Ship dynamics simulator for extreme conditions
statistics.cc
1 #include <ostream>
2 
3 #include <vtestbed/config/real_type.hh>
4 #include <vtestbed/core/statistics.hh>
5 #include <vtestbed/geometry/linear_interpolation.hh>
6 
7 namespace {
8 
9  constexpr const char* headers[vtb::core::Statistics_base::size()] = {
10  // please, use lowercase letters
11  "time",
12  "surge", "sway", "heave",
13  "roll", "pitch", "yaw",
14  "velocity_x", "velocity_y", "velocity_z",
15  "angular_velocity_x", "angular_velocity_y", "angular_velocity_z",
16  "acceleration_x", "acceleration_y", "acceleration_z",
17  "angular_acceleration_x", "angular_acceleration_y", "angular_acceleration_z",
18  "underwater_volume",
19  "wave_length_t", "wave_length_x", "wave_length_y",
20  "wave_number_t", "wave_number_x", "wave_number_y",
21  "wave_height_t", "wave_height_x", "wave_height_y",
22  "wavy_surface",
23  "angular_momentum_x", "angular_momentum_y", "angular_momentum_z",
24  };
25 
26  static_assert(vtb::core::Statistics_base::size() > 0 &&
27  headers[vtb::core::Statistics_base::size()-1] != 0,
28  "add header name");
29 
30 }
31 
32 void
33 vtb::core::Statistics_base::write_header(std::ostream& out) {
34  static_assert(size() > 0, "bad no. of records");
35  out << headers[0];
36  for (size_type i=1; i<size(); ++i){ out << ',' << headers[i]; }
37  out << '\n';
38 }
39 
40 template <class T>
41 void
43  static_assert(size() > 0, "bad no. of records");
44  out << this->_series[0].back();
45  for (size_type i=1; i<size(); ++i){ out << ',' << this->_series[i].back(); }
46  out << '\n';
47 }
48 
49 template <class T>
50 auto
52 -> std::pair<array1,grid1> {
55  typedef typename curve_type::vertex_array vertex_array;
56  const auto& t = this->time_instant_series();
57  const auto& wss = this->wavy_surface_series();
58  array1 result; grid1 grid;
59  const int nt = t.size();
60  if (nt <= 3) { return std::make_pair(result,grid); }
61  const T delta = T{1}/(nt-1);
62  vertex_array points;
63  points.reserve(nt);
64  grid.begin() = 0;
65  grid.end() = nt;
66  grid.lbound() = t.front();
67  grid.ubound() = t.back();
68  result.resize(nt);
69  points.clear();
70  for (auto first = t.cbegin(), last = t.cend(),
71  first2 = wss.cbegin(); first != last; ++first, ++first2) {
72  points.emplace_back(*first, *first2);
73  }
74  curve_type curve(points);
75  for (int k=0; k<nt; ++k) { result(k) = curve(k*delta)(1); }
76  return std::make_pair(result,grid);
77 }
78 
79 template <class T>
81  for (auto& s : this->_series) { s.clear(); }
82  for (auto& w : this->_waves) { w.clear(); }
83 }
84 
85 template <class T>
87  const auto& pair = this->uniform_wavy_surface();
88  this->_waves(0) = temporal_wave_statistics(pair.first, pair.second);
89  this->record(Record::Wave_length_t, this->_waves(0).length.mean());
90  this->record(Record::Wave_length_x, this->_waves(1).length.mean());
91  this->record(Record::Wave_length_y, this->_waves(2).length.mean());
92  this->record(Record::Wave_number_t, this->_waves(0).number.mean());
93  this->record(Record::Wave_number_x, this->_waves(1).number.mean());
94  this->record(Record::Wave_number_y, this->_waves(2).number.mean());
95  this->record(Record::Wave_height_t, this->_waves(0).height.mean());
96  this->record(Record::Wave_height_x, this->_waves(1).height.mean());
97  this->record(Record::Wave_height_y, this->_waves(2).height.mean());
98 }
99 
T length(const blitz::TinyVector< T, N > &x)
Computes vector length without overflow.
Definition: blitz.hh:471
void write_back(std::ostream &out) const
Write the last sample.
Definition: statistics.cc:42