34 #define _USE_MATH_DEFINES
37 #include "Eigen/Dense"
39 #include "Waves/WaveOptions.hpp"
46 #include <initializer_list>
68 return floor(val + 0.5);
76 typedef Matrix<float, 6, 1> Vector6f;
77 typedef Matrix<float, 6, 6> Matrix6f;
78 typedef Matrix<double, 6, 1> Vector6d;
79 typedef Matrix<double, 6, 6> Matrix6d;
80 typedef Matrix<int, 6, 1> Vector6i;
81 typedef Matrix<int, 6, 6> Matrix6i;
82 typedef Matrix<float, 7, 1> Vector7f;
83 typedef Matrix<float, 7, 7> Matrix7f;
84 typedef Matrix<double, 7, 1> Vector7d;
85 typedef Matrix<double, 7, 7> Matrix7d;
86 typedef Matrix<int, 7, 1> Vector7i;
87 typedef Matrix<int, 7, 7> Matrix7i;
89 #ifdef MOORDYN_SINGLEPRECISSION
90 typedef MatrixXf MatrixXr;
92 typedef MatrixXd MatrixXr;
100 #ifdef MOORDYN_SINGLEPRECISSION
102 typedef Eigen::Vector2f
vec2;
103 typedef Eigen::Vector3f
vec3;
104 typedef Eigen::Vector4f
vec4;
105 typedef Eigen::Vector6f
vec6;
106 typedef Eigen::Vector7f
vec7;
108 typedef Eigen::Matrix2f
mat2;
109 typedef Eigen::Matrix3f
mat3;
110 typedef Eigen::Matrix4f
mat4;
111 typedef Eigen::Matrix6f
mat6;
112 typedef Eigen::Matrix7f
mat7;
115 typedef Eigen::Matrix<real, Eigen::Dynamic, 1>
list;
146 typedef Eigen::Matrix<real, Eigen::Dynamic, 1>
list;
169 typedef Eigen::Matrix<InstanceStateVar, Eigen::Dynamic, 1>
StateVar;
190 constexpr
real eps = std::numeric_limits<moordyn::real>::epsilon();
193 const real fraction = (std::max)(std::abs(a1 + a2), ((
real)1.0));
194 return std::abs(a1 - a2) <= fraction * tol;
198 canonicalEulerAngles(
const quaternion& quat,
int a0,
int a1,
int a2)
202 mat3 coeff = quat.normalized().toRotationMatrix();
206 const Index odd = ((a0 + 1) % 3 == a1) ? 0 : 1;
208 const Index j = (a0 + 1 + odd) % 3;
209 const Index k = (a0 + 2 - odd) % 3;
219 Scalar s2 = Eigen::numext::hypot(coeff(j, i), coeff(k, i));
221 res[0] = atan2(coeff(j, i), coeff(k, i));
224 res[1] = atan2(s2, coeff(i, i));
240 res[0] = atan2(-coeff(j, i), -coeff(k, i));
241 res[1] = -atan2(s2, coeff(i, i));
252 Scalar s1 = sin(res[0]);
253 Scalar c1 = cos(res[0]);
254 res[2] = atan2(c1 * coeff(j, k) - s1 * coeff(k, k),
255 c1 * coeff(j, j) - s1 * coeff(k, j));
265 res[0] = atan2(coeff(j, k), coeff(k, k));
266 Scalar c2 = Eigen::numext::hypot(coeff(i, i), coeff(i, j));
269 res[1] = atan2(-coeff(i, k), c2);
270 Scalar s1 = sin(res[0]);
271 Scalar c1 = cos(res[0]);
272 res[2] = atan2(s1 * coeff(k, i) - c1 * coeff(j, i),
273 c1 * coeff(j, j) - s1 * coeff(k, j));
282 Quat2Euler(
const quaternion& q)
285 return canonicalEulerAngles(q, 0, 1, 2);
289 Euler2Quat(
const vec3& angles)
291 using AngleAxis = Eigen::AngleAxis<real>;
292 quaternion q = AngleAxis(angles.x(), vec3::UnitX()) *
293 AngleAxis(angles.y(), vec3::UnitY()) *
294 AngleAxis(angles.z(), vec3::UnitZ());
318 return XYZQuat{ vec3::Zero(), quaternion::Identity() };
327 out.head<3>() = this->pos;
328 out.tail<3>() = Quat2Euler(this->quat);
339 out.tail<4>() = quat.coeffs();
418 a[0][0] = (T)v(0, 0);
419 a[0][1] = (T)v(0, 1);
420 a[0][2] = (T)v(0, 2);
421 a[1][0] = (T)v(1, 0);
422 a[1][1] = (T)v(1, 1);
423 a[1][2] = (T)v(1, 2);
424 a[2][0] = (T)v(2, 0);
425 a[2][1] = (T)v(2, 1);
426 a[2][2] = (T)v(2, 2);
456 a[0][0] = (T)v(0, 0);
457 a[0][1] = (T)v(0, 1);
458 a[0][2] = (T)v(0, 2);
459 a[0][3] = (T)v(0, 3);
460 a[0][4] = (T)v(0, 4);
461 a[0][5] = (T)v(0, 5);
462 a[1][0] = (T)v(1, 0);
463 a[1][1] = (T)v(1, 1);
464 a[1][2] = (T)v(1, 2);
465 a[1][3] = (T)v(1, 3);
466 a[1][4] = (T)v(1, 4);
467 a[1][5] = (T)v(1, 5);
468 a[2][0] = (T)v(2, 0);
469 a[2][1] = (T)v(2, 1);
470 a[2][2] = (T)v(2, 2);
471 a[2][3] = (T)v(2, 3);
472 a[2][4] = (T)v(2, 4);
473 a[2][5] = (T)v(2, 5);
514 auto first = v.begin() + m;
515 auto last = first + n;
516 std::vector<T> v2(first, last);
540 v.reserve(v.size() + distance(v_prime.begin(), v_prime.end()));
541 v.insert(v.end(), v_prime.begin(), v_prime.end());
550 template<
typename T,
int NROWS,
int NCOLS>
551 std::vector<T>
flatten(std::vector<Eigen::Matrix<T, NROWS, NCOLS>>
const& v)
553 const int stride = NROWS * NCOLS;
554 std::vector<T> out(v.size() * stride);
555 for (
unsigned int i = 0; i < v.size(); i++) {
556 for (
unsigned int j = 0; j < NROWS; j++) {
557 for (
unsigned int k = 0; k < NCOLS; k++) {
558 out[i * stride + j * NCOLS + k] = v[i](j, k);
586 return char(
'A' + (
int)p);
597 #define MAKE_EXCEPTION(name) \
598 class name : public std::runtime_error \
601 name(const char* msg) \
602 : std::runtime_error(msg) \
626 #define MOORDYN_THROW(err, msg) \
630 case MOORDYN_SUCCESS: \
632 case MOORDYN_INVALID_INPUT_FILE: \
633 throw moordyn::input_file_error(msg); \
635 case MOORDYN_INVALID_OUTPUT_FILE: \
636 throw moordyn::output_file_error(msg); \
638 case MOORDYN_INVALID_INPUT: \
639 throw moordyn::input_error(msg); \
641 case MOORDYN_NAN_ERROR: \
642 throw moordyn::nan_error(msg); \
644 case MOORDYN_MEM_ERROR: \
645 throw moordyn::mem_error(msg); \
647 case MOORDYN_INVALID_VALUE: \
648 throw moordyn::invalid_value_error(msg); \
650 case MOORDYN_NON_IMPLEMENTED: \
651 throw moordyn::non_implemented_error(msg); \
654 throw moordyn::unhandled_error(msg); \
662 #define MOORDYN_CATCHER(err, msg) \
663 catch (moordyn::input_file_error const& e) \
665 err = MOORDYN_INVALID_INPUT_FILE; \
668 catch (moordyn::output_file_error const& e) \
670 err = MOORDYN_INVALID_OUTPUT_FILE; \
673 catch (moordyn::input_error const& e) \
675 err = MOORDYN_INVALID_INPUT; \
678 catch (moordyn::nan_error const& e) \
680 err = MOORDYN_NAN_ERROR; \
683 catch (moordyn::mem_error const& e) \
685 err = MOORDYN_MEM_ERROR; \
688 catch (moordyn::invalid_value_error const& e) \
690 err = MOORDYN_INVALID_VALUE; \
693 catch (moordyn::unhandled_error const& e) \
695 err = MOORDYN_UNHANDLED_ERROR; \
714 lower(
const string& str);
721 upper(
const string& str);
729 startswith(
const string& str,
const string& prefix);
738 has(
const string& str,
const vector<string> terms);
746 split(
const string& str,
const char sep);
754 inline vector<string>
755 split(
const string& s)
757 vector<string> sout = split(s,
' ');
758 if (sout.size() == 1)
759 return split(sout[0],
776 decomposeString(
const std::string& outWord,
784 isOneOf(
const std::string& str,
785 const std::initializer_list<const std::string> values);
795 std::vector<std::string>
796 fileToLines(
const std::filesystem::path& path);
834 solveMat6(
const mat6& mat,
const vec6& vec);
850 const double l = u.norm();
923 return R * M * R.transpose();
960 const real s = sin(rads);
961 const real c = cos(rads);
963 R << 1., 0., 0., 0., c, -s, 0., s, c;
974 const real s = sin(rads);
975 const real c = cos(rads);
977 R << c, 0., s, 0., 1., 0., -s, 0., c;
988 const real s = sin(rads);
989 const real c = cos(rads);
991 R << c, -s, 0., s, c, 0., 0., 0., 1.;
997 #define MAKE_EULER_ROT(a,b,c) \
998 inline mat Rot ## a ## b ## c(real a1, real a2, real a3) \
1000 return Rot ## a (a1) * Rot ## b (a2) * Rot ## c (a3); \
1002 inline mat Rot ## a ## b ## c(vec rads) \
1004 return Rot ## a ## b ## c (rads[0], rads[1], rads[2]); \
1028 std::pair<real, real>
1045 #if !defined(MOORDYN_SINGLEPRECISSION) && defined(M_PIl)
1047 const real pi = M_PIl;
1051 const real pi = 3.141592653589793238462643383279502884197169399375105820974944;
1100 const int nCoef = 30;
1141 typedef std::shared_ptr<EnvCond> EnvCondRef;
1170 double stiffYs[nCoef];
1173 double dampXs[nCoef];
1175 double dampYs[nCoef];
1180 double bstiffYs[nCoef];
1237 enum QTypeEnum :
int
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition: MoorDynAPI.h:68
A mooring line.
Definition: Line.hpp:71
A point for a line endpoint.
Definition: Point.hpp:69
A cylindrical rod.
Definition: Rod.hpp:65
struct moordyn::_FailProps FailProps
Failure conditions.
seafloor_settings
Definition: Misc.hpp:811
@ SEAFLOOR_3D
3D seafloor
Definition: Misc.hpp:815
@ SEAFLOOR_FLAT
Flat seafloor:
Definition: Misc.hpp:813
#define MAKE_EXCEPTION(name)
Simple macro to define custom exceptions.
Definition: Misc.hpp:597
int error_id
Error identifier.
Definition: Misc.hpp:594
MoorDyn2 C++ API namespace.
Definition: Body.cpp:27
vec3 vec
vec3 renaming
Definition: Misc.hpp:130
bool EqualRealNos(const real a1, const real a2)
This function compares two real numbers and determines if they are "almost" equal.
Definition: Misc.hpp:188
void array2vec6(const T *a, vec6 &v)
Convert a C-ish array to a vector.
Definition: Misc.hpp:400
std::vector< T > vector_slice(std::vector< T > const &v, unsigned int m, unsigned int n)
Definition: Misc.hpp:512
Eigen::Quaterniond quaternion
Quaternion of real numbers.
Definition: Misc.hpp:144
Eigen::Vector7d vec7
7-D vector of real numbers
Definition: Misc.hpp:128
Eigen::Block< InstanceStateVar, Eigen::Dynamic > InstanceStateVarView
View of the State variables for a particular instance.
Definition: Misc.hpp:167
Eigen::Matrix< real, Eigen::Dynamic, Eigen::Dynamic > InstanceStateVar
State variables for a particular instance.
Definition: Misc.hpp:165
void mat2array(const mat &v, T a[3][3])
Convert a matrix to a C-ish array.
Definition: Misc.hpp:416
void array2mat6(const T a[6][6], mat6 &v)
Convert a C-ish array to a matrix.
Definition: Misc.hpp:482
void mat62array(const mat6 &v, T a[6][6])
Convert a matrix to a C-ish array.
Definition: Misc.hpp:454
Eigen::Vector6d vec6
6-D vector of real numbers
Definition: Misc.hpp:126
Eigen::Vector7i ivec7
7-D vector of integers
Definition: Misc.hpp:157
void vector_extend(std::vector< T > &v, std::vector< T > const &v_prime)
Definition: Misc.hpp:538
Eigen::Matrix7d mat7
7x7 matrix of real numbers
Definition: Misc.hpp:140
Eigen::Vector4i ivec4
4-D vector of integers
Definition: Misc.hpp:153
Eigen::VectorBlock< StateVar, Eigen::Dynamic > StateVarView
View of the State variable.
Definition: Misc.hpp:171
Eigen::Matrix6d mat6
6x6 matrix of real numbers
Definition: Misc.hpp:138
Eigen::Matrix3d mat3
3x3 matrix of real numbers
Definition: Misc.hpp:134
const real deg2rad
Constant to convert degrees into radians.
Definition: Misc.hpp:1057
EndPoints
End point qualifiers.
Definition: Misc.hpp:570
@ ENDPOINT_A
Bottom of the line.
Definition: Misc.hpp:572
@ ENDPOINT_B
Top of the line.
Definition: Misc.hpp:574
Eigen::Vector2i ivec2
2-D vector of integers
Definition: Misc.hpp:149
const complex i1(0., 1.)
The imaginary unit.
std::vector< T > flatten(std::vector< Eigen::Matrix< T, NROWS, NCOLS >> const &v)
Flatten a list of vectors or matrices.
Definition: Misc.hpp:551
Eigen::Vector3i ivec3
3-D vector of integers
Definition: Misc.hpp:151
Eigen::Matrix< InstanceStateVar, Eigen::Dynamic, 1 > StateVar
State variable.
Definition: Misc.hpp:169
Eigen::Vector2d vec2
2-D vector of real numbers
Definition: Misc.hpp:120
double real
Real numbers wrapper. It is either double or float.
Definition: Misc.hpp:118
ivec3 ivec
Renaming of ivec3.
Definition: Misc.hpp:159
Eigen::Matrix2d mat2
2x2 matrix of real numbers
Definition: Misc.hpp:132
const real rad2deg
Constant to convert radians into degrees.
Definition: Misc.hpp:1054
Eigen::Vector6i ivec6
6-D vector of integers
Definition: Misc.hpp:155
Eigen::Matrix< real, Eigen::Dynamic, 1 > list
A resizable list of reals.
Definition: Misc.hpp:146
void vec62array(const vec6 &v, T *a)
Convert a vector to a C-ish array.
Definition: Misc.hpp:384
void array2mat(const T a[3][3], mat &v)
Convert a C-ish array to a matrix.
Definition: Misc.hpp:435
char end_point_name(EndPoints p)
Gives an character representation of the end point.
Definition: Misc.hpp:584
std::complex< real > complex
Complex numbers.
Definition: Misc.hpp:162
Eigen::Vector4d vec4
4-D vector of real numbers
Definition: Misc.hpp:124
Eigen::Vector3d vec3
3-D vector of real numbers
Definition: Misc.hpp:122
Eigen::Matrix4d mat4
4x4 matrix of real numbers
Definition: Misc.hpp:136
void array2vec(const T *a, vec &v)
Convert a C-ish array to a vector.
Definition: Misc.hpp:371
mat3 mat
mat3 renaming
Definition: Misc.hpp:142
void vec2array(const vec &v, T *a)
Convert a vector to a C-ish array.
Definition: Misc.hpp:358
const real pi
Pi constant.
Definition: Misc.hpp:1051
Definition: Misc.hpp:1218
Definition: Misc.hpp:1146
Definition: Misc.hpp:1292
Definition: Misc.hpp:1201
Definition: Misc.hpp:1188
Definition: Misc.hpp:1108
double rho_w
Water density (kg/m^3)
Definition: Misc.hpp:1114
double FrictionCoefficient
general bottom friction coefficient, as a start
Definition: Misc.hpp:1127
double g
Gavity acceleration (m/s^2)
Definition: Misc.hpp:1110
double WtrDpth
Water depth (m)
Definition: Misc.hpp:1112
moordyn::waves::WaterKinOptions waterKinOptions
Water Kinematics Options.
Definition: Misc.hpp:1124
double cb
bottom damping (Pa/m/s)
Definition: Misc.hpp:1119
double FricDamp
a damping coefficient used to model the friction at speeds near zero
Definition: Misc.hpp:1129
int WriteUnits
Definition: Misc.hpp:1135
double StatDynFricScale
a ratio of static to dynamic friction ( = mu_static/mu_dynamic)
Definition: Misc.hpp:1131
moordyn::seafloor_settings SeafloorMode
Bottom modeling mode (0=flat, 1=3d...)<<<.
Definition: Misc.hpp:1121
int writeLog
Definition: Misc.hpp:1138
double kb
bottom stiffness (Pa/m)
Definition: Misc.hpp:1117
Failure conditions.
Definition: Misc.hpp:1071
std::vector< Line * > lines
The attached lines.
Definition: Misc.hpp:1079
std::vector< EndPoints > line_end_points
Definition: Misc.hpp:1082
Rod * rod
The rod the lines are attached to, if any. Otherwise it is NULL.
Definition: Misc.hpp:1073
bool status
false until the line fails
Definition: Misc.hpp:1088
EndPoints rod_end_point
The rod attachment end point, useless if rod is NULL.
Definition: Misc.hpp:1075
Point * point
The point the lines are attached to, if any. Otherwise it is NULL.
Definition: Misc.hpp:1077
real ten
Failure criteria based on tension (N)
Definition: Misc.hpp:1086
real time
Failure criteria based on simulation time (s)
Definition: Misc.hpp:1084
Joint of a point and a quaternion.
Definition: Misc.hpp:304