Gazebo Gazebo

API Reference

6.17.1
HydrodynamicsUtils.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #ifndef GZ_SIM_SYSTEMS_HYDRODYNAMICS_UTILS_HH_
18 #define GZ_SIM_SYSTEMS_HYDRODYNAMICS_UTILS_HH_
19 
20 #include <cmath>
21 
22 #include <Eigen/Eigen>
23 
24 #include "gz/sim/config.hh"
25 
26 namespace ignition
27 {
28 namespace gazebo
29 {
30 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
31 namespace systems
32 {
33 namespace hydrodynamics
34 {
45  inline Eigen::Matrix<double, 6, 6> buildCoriolisMatrix(
46  const Eigen::Matrix<double, 6, 6> &_Ma,
47  const Eigen::Matrix<double, 6, 1> &_state)
48  {
49  Eigen::Matrix<double, 6, 6> Cmat =
50  Eigen::Matrix<double, 6, 6>::Zero();
51 
52  Cmat(0, 4) = - _Ma(2, 2) * _state(2);
53  Cmat(0, 5) = _Ma(1, 1) * _state(1);
54  Cmat(1, 3) = _Ma(2, 2) * _state(2);
55  Cmat(1, 5) = - _Ma(0, 0) * _state(0);
56  Cmat(2, 3) = - _Ma(1, 1) * _state(1);
57  Cmat(2, 4) = _Ma(0, 0) * _state(0);
58  Cmat(3, 1) = - _Ma(2, 2) * _state(2);
59  Cmat(3, 2) = _Ma(1, 1) * _state(1);
60  Cmat(3, 4) = - _Ma(5, 5) * _state(5);
61  Cmat(3, 5) = _Ma(4, 4) * _state(4);
62  Cmat(4, 0) = _Ma(2, 2) * _state(2);
63  Cmat(4, 2) = - _Ma(0, 0) * _state(0);
64  Cmat(4, 3) = _Ma(5, 5) * _state(5);
65  Cmat(4, 5) = - _Ma(3, 3) * _state(3);
66  Cmat(5, 0) = - _Ma(1, 1) * _state(1);
67  Cmat(5, 1) = _Ma(0, 0) * _state(0);
68  Cmat(5, 3) = - _Ma(4, 4) * _state(4);
69  Cmat(5, 4) = _Ma(3, 3) * _state(3);
70 
71  return Cmat;
72  }
73 
89  inline Eigen::Matrix<double, 6, 6> buildDampingMatrix(
90  const double (&_linearTerms)[36],
91  const double (&_quadAbsDerivs)[216],
92  const double (&_quadDerivs)[216],
93  const Eigen::Matrix<double, 6, 1> &_state)
94  {
95  Eigen::Matrix<double, 6, 6> Dmat =
96  Eigen::Matrix<double, 6, 6>::Zero();
97 
98  for (int i = 0; i < 6; i++)
99  {
100  for (int j = 0; j < 6; j++)
101  {
102  auto coeff = - _linearTerms[i * 6 + j];
103  for (int k = 0; k < 6; k++)
104  {
105  auto index = i * 36 + j * 6 + k;
106  coeff -= _quadAbsDerivs[index] * std::abs(_state(k));
107  coeff -= _quadDerivs[index] * _state(k);
108  }
109  Dmat(i, j) = coeff;
110  }
111  }
112 
113  return Dmat;
114  }
115 
122  inline Eigen::Matrix3d skew3(const Eigen::Vector3d &_v)
123  {
124  Eigen::Matrix3d S;
125  S << 0, -_v(2), _v(1),
126  _v(2), 0, -_v(0),
127  -_v(1), _v(0), 0;
128  return S;
129  }
130 
150  inline Eigen::Matrix<double, 6, 6> buildFullCoriolisMatrix(
151  const Eigen::Matrix<double, 6, 6> &_Ma,
152  const Eigen::Matrix<double, 6, 1> &_state)
153  {
154  Eigen::Vector3d v1 = _state.head<3>();
155  Eigen::Vector3d v2 = _state.tail<3>();
156 
157  Eigen::Matrix3d M11 = _Ma.block<3, 3>(0, 0);
158  Eigen::Matrix3d M12 = _Ma.block<3, 3>(0, 3);
159  Eigen::Matrix3d M21 = _Ma.block<3, 3>(3, 0);
160  Eigen::Matrix3d M22 = _Ma.block<3, 3>(3, 3);
161 
162  Eigen::Vector3d a1 = M11 * v1 + M12 * v2;
163  Eigen::Vector3d a2 = M21 * v1 + M22 * v2;
164 
165  Eigen::Matrix<double, 6, 6> C =
166  Eigen::Matrix<double, 6, 6>::Zero();
167  C.block<3, 3>(0, 3) = -skew3(a1);
168  C.block<3, 3>(3, 0) = -skew3(a1);
169  C.block<3, 3>(3, 3) = -skew3(a2);
170 
171  return C;
172  }
173 
174 } // namespace hydrodynamics
175 } // namespace systems
176 } // namespace IGNITION_GAZEBO_VERSION_NAMESPACE
177 } // namespace gazebo
178 } // namespace ignition
179 
180 #endif
This library is part of the Gazebo project.
Definition: gz/sim/Actor.hh:33
Eigen::Matrix3d skew3(const Eigen::Vector3d &_v)
3x3 skew-symmetric (cross-product) matrix from a 3-vector.
Definition: HydrodynamicsUtils.hh:122
Eigen::Matrix< double, 6, 6 > buildDampingMatrix(const double(&_linearTerms)[36], const double(&_quadAbsDerivs)[216], const double(&_quadDerivs)[216], const Eigen::Matrix< double, 6, 1 > &_state)
Build the hydrodynamic damping matrix from linear and quadratic stability derivatives.
Definition: HydrodynamicsUtils.hh:89
Eigen::Matrix< double, 6, 6 > buildFullCoriolisMatrix(const Eigen::Matrix< double, 6, 6 > &_Ma, const Eigen::Matrix< double, 6, 1 > &_state)
Build the added-mass Coriolis matrix C_A(v) for a full (non-diagonal) 6x6 added mass matrix,...
Definition: HydrodynamicsUtils.hh:150
Eigen::Matrix< double, 6, 6 > buildCoriolisMatrix(const Eigen::Matrix< double, 6, 6 > &_Ma, const Eigen::Matrix< double, 6, 1 > &_state)
Build the negated added-mass Coriolis matrix (-C_A) per Fossen (2011) Theorem 6.2,...
Definition: HydrodynamicsUtils.hh:45