Page MenuHomec4science

testAdamBashforth.cpp
No OneTemporary

File Metadata

Created
Wed, Jul 24, 23:21

testAdamBashforth.cpp

//
// Created by lionel on 01.12.19.
//
# include <gtest/gtest.h>
# include <cmath>
# include <cassert>
# include <exception>
#include "../ODElibrary/AdamsBashforth.h"
// Function g(t) for testing
Eigen::VectorXd ForcedOscillations(double t){
Eigen::VectorXd s(2);
s[0] = 0;
s[1] = cos(2*M_PI*t); // derivative of velocity
return s;
};
// Fixture class
class AdamsBashforthSolverTest : public ::testing::Test {
protected:
void SetUp() override {
// create a solver
solver.setInitialTime(-3);
Eigen::VectorXd y0(2); // set the spring at rest
y0[0] = 0;
y0[1] = 0;
double k = 0.5; // spring constant
Eigen::MatrixXd m(2,2); // from spring equation
m(0,0)=0;
m(0,1)=1;
m(1,0)=-k;
m(1,1)=0.3;
//AdamsBashforth solver=AdamsBashforth(0,20,2000,2,y0);
solver.setInitialTime(0.);
solver.setFinalTime(20.);
solver.setStepSize(0.01);
solver.setRightHandSide(m, ForcedOscillations);
solver.setInitialValue(y0);
solver.setStepsAlgo(2);
}
void TearDown() override {
// clear particles
// solver;
}
AdamsBashforth solver;
};
// tests that we can assign and read a positive initial time
TEST_F(AdamsBashforthSolverTest, initialAndFinalTimes) {
solver.setInitialTime(0.5);
EXPECT_EQ(solver.getInitialTime(), 0.5);
solver.setFinalTime(4.2);
EXPECT_EQ(solver.getFinalTime(), 4.2);
}
// tests that we can assign and read a positive timestep
TEST_F(AdamsBashforthSolverTest, positiveTimestep) {
solver.setStepSize(0.3);
EXPECT_EQ(solver.getStepSize(), 0.3);
}
// tests we cannot assign a 0 or negative timestep
TEST_F(AdamsBashforthSolverTest, nonPositiveTimestep) {
ASSERT_THROW(solver.setStepSize(-0.1), std::domain_error);
ASSERT_THROW(solver.setStepSize(0), std::domain_error);
}
// tests we cannot assign a different size inputs for the initial value
TEST_F(AdamsBashforthSolverTest, differentSizedInitialValue) {
Eigen::VectorXd yTooLong(3);
ASSERT_THROW(solver.setInitialValue(yTooLong), std::length_error);
}
// tests we cannot assign a different size inputs for the rhs matrix
TEST_F(AdamsBashforthSolverTest, differentSizedRHS) {
Eigen::MatrixXd mFalse1(2,3);
Eigen::MatrixXd mFalse2(1,2);
Eigen::MatrixXd mFalse3(3,3);
ASSERT_THROW(solver.setRightHandSide(mFalse1, ForcedOscillations), std::length_error);
ASSERT_THROW(solver.setRightHandSide(mFalse2, ForcedOscillations), std::length_error);
ASSERT_THROW(solver.setRightHandSide(mFalse3, ForcedOscillations), std::length_error);
}
// tests we cannot assign a rhs function
Eigen::VectorXd RHSgTooLong(double t){
Eigen::VectorXd s(4);
return s;
};
Eigen::VectorXd RHSgThrowingException(double t){
Eigen::VectorXd s(2);
throw std::exception();
return s;
};
Eigen::VectorXd RHSgThrowingInteger(double t){
Eigen::VectorXd s(2);
throw 42;
return s;
};
TEST_F(AdamsBashforthSolverTest, timeDependentRHSFunction) {
Eigen::MatrixXd m(2,2);
ASSERT_THROW(solver.setRightHandSide(m, RHSgTooLong), std::length_error);
ASSERT_THROW(solver.setRightHandSide(m, RHSgThrowingException), std::runtime_error);
ASSERT_THROW(solver.setRightHandSide(m, RHSgThrowingInteger), std::runtime_error);
}

Event Timeline