Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change testGCVSpline to use catch (#3555) #3564

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 53 additions & 57 deletions OpenSim/Common/Test/testGCVSpline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,69 +24,65 @@
#include <OpenSim/Common/GCVSplineSet.h>
#include <OpenSim/Auxiliary/auxiliaryTestFunctions.h>

#define CATCH_CONFIG_MAIN
#include <OpenSim/Auxiliary/catch.hpp>

using namespace OpenSim;
using namespace std;

int main() {
try {
const int size = 101;
const double T = 1.0;
const double omega = 2 * SimTK::Pi;
const double dt = T / (size - 1);
double x[size], y[size];
for (int i = 0; i < size; ++i) {
x[i] = dt*i;
y[i] = sin(omega*x[i]);
}
GCVSpline spline(5, size, x, y);
SimTK::Vector t(1, 0.0);
// Should obtain the input samples exactly
for (int i = 0; i < size; ++i) {
t[0] = x[i];
//cout << t[0] << " error = " << y[i] - spline.calcValue(t) << endl;
ASSERT_EQUAL(y[i], spline.calcValue(t),
SimTK::SignificantReal, __FILE__, __LINE__,
"GCVSpline failed to reproduce input data points.");
}
cout << "GCVSpline successfully reproduced input data points." << endl;

for (int i = 0; i < (2*size-1); ++i) {
t[0] = dt / 2 * i;
//cout << t[0] << " error = " << sin(omega*t[0]) - spline.calcValue(t) << endl;
ASSERT_EQUAL(sin(omega*t[0]), spline.calcValue(t),
dt*dt, __FILE__, __LINE__,
"GCVSpline failed to interpolate within accuracy requirement.");
}
cout << "GCVSpline successfully interpolated within accuracy." << endl;
TEST_CASE("GCVSpline Behaves as Expected")
{
const int size = 101;
const double T = 1.0;
const double omega = 2 * SimTK::Pi;
const double dt = T / (size - 1);
double x[size], y[size];
for (int i = 0; i < size; ++i) {
x[i] = dt*i;
y[i] = sin(omega*x[i]);
}
GCVSpline spline(5, size, x, y);
SimTK::Vector t(1, 0.0);
// Should obtain the input samples exactly
for (int i = 0; i < size; ++i) {
t[0] = x[i];
//cout << t[0] << " error = " << y[i] - spline.calcValue(t) << endl;
ASSERT_EQUAL(y[i], spline.calcValue(t),
SimTK::SignificantReal, __FILE__, __LINE__,
"GCVSpline failed to reproduce input data points.");
}
cout << "GCVSpline successfully reproduced input data points." << endl;

std::vector<int> derivComponents(1, 0); //take first derivative
for (int i = 5; i < size-5; ++i) {
t[0] = x[i];
double dy = omega*cos(omega*t[0]);
double dS = spline.calcDerivative(derivComponents, t);
for (int i = 0; i < (2*size-1); ++i) {
t[0] = dt / 2 * i;
//cout << t[0] << " error = " << sin(omega*t[0]) - spline.calcValue(t) << endl;
ASSERT_EQUAL(sin(omega*t[0]), spline.calcValue(t),
dt*dt, __FILE__, __LINE__,
"GCVSpline failed to interpolate within accuracy requirement.");
}
cout << "GCVSpline successfully interpolated within accuracy." << endl;

//cout << t[0] << " error = " << dy - dS<< endl;
ASSERT_EQUAL(dy, dS,
omega*dt*dt, __FILE__, __LINE__,
"GCVSpline failed to reproduce accurate first derivative.");
}
cout << "GCVSpline successfully produced first derivatives." << endl;
std::vector<int> derivComponents(1, 0); //take first derivative
for (int i = 5; i < size-5; ++i) {
t[0] = x[i];
double dy = omega*cos(omega*t[0]);
double dS = spline.calcDerivative(derivComponents, t);

GCVSpline spline2(5, size, x, y);
for (int i = 0; i < size; ++i) {
t[0] = x[i];
double dS = spline.calcDerivative(derivComponents, t);
double dS2 = spline2.calcDerivative(derivComponents, t);
//cout << t[0] << " error = " << dS - dS2 << endl;
ASSERT_EQUAL(dS, dS,
SimTK::Eps, __FILE__, __LINE__,
"Duplicate GCVSpline failed to reproduce identical first derivative.");
}
//cout << t[0] << " error = " << dy - dS<< endl;
ASSERT_EQUAL(dy, dS,
omega*dt*dt, __FILE__, __LINE__,
"GCVSpline failed to reproduce accurate first derivative.");
}
catch(const Exception& e) {
e.print(cerr);
return 1;
cout << "GCVSpline successfully produced first derivatives." << endl;

GCVSpline spline2(5, size, x, y);
for (int i = 0; i < size; ++i) {
t[0] = x[i];
double dS = spline.calcDerivative(derivComponents, t);
double dS2 = spline2.calcDerivative(derivComponents, t);
//cout << t[0] << " error = " << dS - dS2 << endl;
ASSERT_EQUAL(dS, dS,
SimTK::Eps, __FILE__, __LINE__,
"Duplicate GCVSpline failed to reproduce identical first derivative.");
}
cout << "Done" << endl;
return 0;
}