Gazebo Sim

API Reference

10.1.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
26namespace gz
27{
28namespace sim
29{
30inline namespace GZ_SIM_VERSION_NAMESPACE {
31namespace systems
32{
33namespace 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 GZ_SIM_VERSION_NAMESPACE
177} // namespace sim
178} // namespace gz
179
180#endif