Virtual Testbed
Ship dynamics simulator for extreme conditions
core/ship.cc
1 #include <cmath>
2 #include <ostream>
3 #include <stdexcept>
4 
5 #include <vtestbed/config/real_type.hh>
6 #include <vtestbed/core/ship.hh>
7 #include <vtestbed/core/types.hh>
8 #include <vtestbed/geometry/basis.hh>
9 #include <vtestbed/geometry/inertia_tensor.hh>
10 #include <vtestbed/geometry/quaternion.hh>
11 
12 template <class T>
13 void
15  this->_hull = std::move(rhs);
16  this->_inertia_matrix = this->_hull.mass_moments().inertia.matrix();
17  this->_mass = this->_hull.mass();
18 }
19 
20 template <class T>
21 void
23  this->_hull = rhs;
24  this->_inertia_matrix = this->_hull.mass_moments().inertia.matrix();
25  this->_mass = this->_hull.mass();
26 }
27 
28 template <class T>
29 T
31  T sum{};
32  for (const auto& room : compartments()) { sum += room.flooded_mass(); }
33  return sum;
34 }
35 
36 template <class T>
37 void
38 vtb::core::Ship<T>::angular_velocity(const vec3& angvelocity) {
39  const auto& R = rotation_matrix();
40  const auto& R_inv = inverse(R);
41  const auto& I_body = inertia_matrix();
42  angular_displacement(1, angvelocity);
43  angular_momentum(R*(I_body*(R_inv*angvelocity)));
44 }
45 
46 template <class T>
47 void
48 vtb::core::Ship<T>::euler_angles(const vec3& angles) {
49  quaternion(vtb::geometry::from_euler_angles(angles));
50 }
51 
52 template <class T>
53 void
55  using vtb::geometry::rotation_matrix;
56  this->_quaternion = q;
57  this->_rotation_matrix = rotation_matrix(q);
58 }
59 
60 template <class T>
61 auto
64  v.position = position(0);
65  v.velocity = position(1);
66  v.quaternion = quaternion();
67  v.angmomentum = angular_momentum();
68  return v;
69 };
70 
71 template <class T>
72 void
74  auto old_position_1 = position(1);
75  auto old_angdisplacement_1 = angular_displacement(1);
76  this->position(0, v.position);
77  this->position(1, v.velocity);
78  this->position(2, dv.velocity);
79  this->quaternion(unit(v.quaternion));
80  this->angular_momentum(v.angmomentum);
81  const auto& R = rotation_matrix();
82  const auto& R_inv = inverse(R);
83  const auto& I_body_inv = inverse(inertia_matrix());
84 // auto I_inv = R*I_body_inv*R_inv;
85 // vec3 angvelocity = I_inv*v.angmomentum;
86  auto angvelocity = R*(I_body_inv*(R_inv*v.angmomentum));
87  auto angacceleration = R*(I_body_inv*(R_inv*dv.angmomentum));
88  this->angular_displacement(0, angular_displacement() + angvelocity*dt);
89  this->angular_displacement(1, angvelocity);
90  this->angular_displacement(2, angacceleration);
91 }
92 
93 template <class T>
94 void
96  this->_quaternion.clear();
97  this->_position[0] = 0;
98  this->_position[1] = 0;
99  this->_position[2] = 0;
100  this->_angdisplacement[0] = 0;
101  this->_angdisplacement[1] = 0;
102  this->_angdisplacement[2] = 0;
103  this->_angmomentum = 0;
104 }
105 
106 template <class T>
107 void
109  out << std::setw(20) << "position"
110  << std::setw(12) << "[m]"
111  << " " << this->position(0)
112  << '\n';
113  out << std::setw(20) << "velocity"
114  << std::setw(12) << "[m/s]"
115  << " " << this->position(1)
116  << '\n';
117  out << std::setw(20) << "acceleration"
118  << std::setw(12) << "[m/s^2]"
119  << " " << this->position(2)
120  << '\n';
121  out << std::setw(20) << "Euler angles"
122  << std::setw(12) << "[rad]"
123  << " " << this->euler_angles()
124  << '\n';
125  out << std::setw(20) << "angular velocity"
126  << std::setw(12) << "[rad/s]"
127  << " " << this->angular_displacement(1)
128  << '\n';
129  out << std::setw(20) << "angular acceleration"
130  << std::setw(12) << "[rad/s^2]"
131  << " " << this->angular_displacement(2)
132  << '\n';
133  out << std::setw(20) << "mass"
134  << std::setw(12) << "[t]"
135  << " " << this->mass()/T{1000}
136  << '\n';
137  out << std::setw(20) << "dimensions"
138  << std::setw(12) << "[m]"
139  << " " << this->hull().bounding_box().extent()
140  << '\n';
141  out << std::setw(20) << "no. of vertices"
142  << std::setw(12) << ""
143  << " " << this->hull().vertices().size()
144  << '\n';
145  out << std::setw(20) << "no. of faces"
146  << std::setw(12) << ""
147  << " " << this->hull().faces().size()
148  << '\n';
149  out << std::setw(20) << "average face area"
150  << std::setw(12) << "[m^2]"
151  << " " << this->hull().average_face_area()
152  << '\n';
153  out << std::setw(20) << "volume"
154  << std::setw(12) << "[m^3]"
155  << " " << volume(this->hull())
156  << '\n';
157  out << std::setw(20) << "centroid"
158  << std::setw(12) << "[m]"
159  << " " << centroid(this->hull())
160  << '\n';
161 }
162 
163 template <class T>
165 vtb::core::operator<<(std::ostream& out, const Ship<T>& rhs) {
166  rhs.dump(out); return out;
167 }
168 
169 template <class T>
171 vtb::core::operator<<(bstream& out, const Ship<T>& rhs) {
172  out << rhs._quaternion;
173  for (const auto& x : rhs._position) { out << x; }
174  for (auto& x : rhs._angdisplacement) { out << x; }
175  out << rhs._hull;
176  out << rhs._mass;
177  return out;
178 }
179 
180 template <class T>
182 vtb::core::operator>>(bstream& in, Ship<T>& rhs) {
183  in >> rhs._quaternion;
184  for (auto& x : rhs._position) { in >> x; }
185  for (auto& x : rhs._angdisplacement) { in >> x; }
186  in >> rhs._hull;
187  in >> rhs._mass;
188  return in;
189 }
190 
191 template class vtb::core::Ship<VTB_REAL_TYPE>;
192 
193 template std::ostream&
194 vtb::core::operator<<(std::ostream& out, const Ship<VTB_REAL_TYPE>& rhs);
195 
196 template vtb::core::bstream&
197 vtb::core::operator<<(bstream& out, const Ship<VTB_REAL_TYPE>& rhs);
198 
199 template vtb::core::bstream&
200 vtb::core::operator>>(bstream& in, Ship<VTB_REAL_TYPE>& rhs);
Mass_moments< T, N > mass_moments() const
Calculate volume, centre of mass and inertia tensor.
Definition: polyhedron.hh:270
T flooded_mass() const
Total mass of all fluid volumes inside compartments.
Definition: core/ship.cc:30
Rigid ship with a mass and translational and angular velocity.
Definition: core/ship.hh:186
vec3 euler_angles() const
Get Euler angles or their derivatives.
Definition: core/ship.hh:278
const vec3 & angular_velocity() const
Angular velocity in Earth-fixed coordinate system.
Definition: core/ship.hh:314
const quaternion_type & quaternion() const
Get rotation quaternion.
Definition: core/ship.hh:281
state_vector_type state_vector() const
Get state vector for ship motion equation.
Definition: core/ship.cc:62