4141#include < iomanip>
4242#include < cmath>
4343#include < vector>
44+ #include < catch2/catch_test_macros.hpp>
4445
4546// NOTE: this is largely built on the pendulum test framework
4647
@@ -54,50 +55,33 @@ compare(double v1, double v2, double tol)
5455 return fabs (v1 - v2) <= tol;
5556}
5657
57- #define CHECK_VALUE (name, v1, v2, tol, t ) \
58- if (!compare(v1, v2, tol)) { \
59- cerr << setprecision (8 ) << " Checking " << name \
60- << " failed at t = " << t << " s. " << v1 << " was expected" \
61- << " but " << v2 << " was computed" << endl; \
62- MoorDyn_Close (system); \
63- return false ; \
58+ void
59+ check_value (const char * name, double v1, double v2, double tol, double t)
60+ {
61+ if (!compare (v1, v2, tol)) {
62+ std::cerr << std::setprecision (8 ) << " Checking " << name
63+ << " failed at t = " << t << " s. " << v1 << " was expected"
64+ << " but " << v2 << " was computed" << std::endl;
65+ throw std::runtime_error (" Missmatching values" );
6466 }
67+ }
6568
6669using namespace std ;
6770
68- /* * @brief VIV frequency simulation and check
69- * @return true if the test worked, false otherwise
70- */
71- bool
72- VIV ()
71+ TEST_CASE (" VIV frequency simulation and check" )
7372{
7473 MoorDyn system = MoorDyn_Create (" Mooring/viv/viv.txt" );
75- if (!system) {
76- cerr << " Failure Creating the Mooring system" << endl;
77- return false ;
78- }
74+ REQUIRE (system);
7975
8076 unsigned int n_dof;
81- if (MoorDyn_NCoupledDOF (system, &n_dof) != MOORDYN_SUCCESS) {
82- MoorDyn_Close (system);
83- return false ;
84- }
85- if (n_dof != 0 ) {
86- cerr << " 0 DOFs were expected, but " << n_dof << " were reported"
87- << endl;
88- MoorDyn_Close (system);
89- return false ;
90- }
77+ REQUIRE (MoorDyn_NCoupledDOF (system, &n_dof) == MOORDYN_SUCCESS);
78+ REQUIRE (n_dof == 0 );
9179
92- int err;
93- err = MoorDyn_Init (system, NULL , NULL );
94- if (err != MOORDYN_SUCCESS) {
95- cerr << " Failure during the mooring initialization: " << err << endl;
96- return false ;
97- }
80+ REQUIRE (MoorDyn_Init (system, NULL , NULL ) == MOORDYN_SUCCESS);
9881
9982 // get the moordyn line object
10083 const MoorDynLine line1 = MoorDyn_GetLine (system, 1 );
84+ REQUIRE (line1);
10185
10286 const double T =
10387 1 / (2 * 1.04 ); // Target VIV period, fairten frequency is 2x target CF
@@ -113,18 +97,10 @@ VIV()
11397 0.0 ; // last peak tracker. For checking if there is a change in the peak
11498
11599 while (t < 15 ) { // run for 15 seconds
100+ REQUIRE (MoorDyn_Step (system, NULL , NULL , NULL , &t, &dt) ==
101+ MOORDYN_SUCCESS);
116102
117- err = MoorDyn_Step (system, NULL , NULL , NULL , &t, &dt);
118- if (err != MOORDYN_SUCCESS) {
119- cerr << " Failure during the mooring step: " << err << endl;
120- return false ;
121- }
122-
123- err = MoorDyn_GetLineFairTen (line1, &ten);
124- if (err != MOORDYN_SUCCESS) {
125- cerr << " Failure getting the fairlead tension: " << err << endl;
126- return false ;
127- }
103+ REQUIRE (MoorDyn_GetLineFairTen (line1, &ten) == MOORDYN_SUCCESS);
128104
129105 if (t > 10 ) { // only check period after 10 seconds. Before then, there
130106 // isn't complete lock in
@@ -141,42 +117,20 @@ VIV()
141117 }
142118 }
143119
144- if (peak_times.size () == 0 )
145- cerr << " No peaks detected in fairten signal"
146- << endl; // if no peaks something is wrong, like viv model was
147- // broken or disabled
120+ REQUIRE (peak_times.size () != 0 );
148121
149122 // Check period is correct
150123 for (unsigned int i = 2 ; i < peak_times.size (); i++) {
151- cout << " Target Period: " << T
152- << " s. Calculated period: " << peak_times[i] - peak_times[i - 2 ]
153- << " s" << endl;
124+ INFO (" Target Period: " << T << " s. Calculated period: "
125+ << peak_times[i] - peak_times[i - 2 ] << " s" );
154126 // check that peak_times[i]-peak_times[i-2] is within TOL of T
155- CHECK_VALUE (" Period" ,
156- T,
157- peak_times[i] - peak_times[i - 2 ],
158- TOL,
159- peak_times[i]); // note that peak_times[i]-peak_times[i-1]
160- // would be a half period
161- }
162-
163- err = MoorDyn_Close (system);
164- if (err != MOORDYN_SUCCESS) {
165- cerr << " Failure closing Moordyn: " << err << endl;
166- return false ;
127+ // note that peak_times[i]-peak_times[i-1] would be a half period
128+ REQUIRE_NOTHROW (check_value (" Period" ,
129+ T,
130+ peak_times[i] - peak_times[i - 2 ],
131+ TOL,
132+ peak_times[i]));
167133 }
168134
169- return true ;
170- }
171-
172- /* * @brief Runs all the test
173- * @return 0 if the tests have ran just fine. The index of the failing test
174- * otherwise
175- */
176- int
177- main (int , char **)
178- {
179- if (!VIV ())
180- return 1 ;
181- return 0 ;
135+ REQUIRE (MoorDyn_Close (system) == MOORDYN_SUCCESS);
182136}
0 commit comments