Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F78339765
testBDF.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Aug 19, 22:19
Size
3 KB
Mime Type
text/x-c
Expires
Wed, Aug 21, 22:19 (2 d)
Engine
blob
Format
Raw Data
Handle
20028114
Attached To
R9686 PCSCproject
testBDF.cpp
View Options
# include <gtest/gtest.h>
# include <cmath>
# include <cassert>
# include <exception>
#include "../ODElibrary/BDF.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 BDFSolverTest : 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;
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;
}
BDF solver;
};
// tests that we can assign and read a positive initial time
TEST_F(BDFSolverTest, 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(BDFSolverTest, positiveTimestep) {
solver.setStepSize(0.3);
EXPECT_EQ(solver.getStepSize(), 0.3);
}
// tests we cannot assign a 0 or negative timestep
TEST_F(BDFSolverTest, 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(BDFSolverTest, 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(BDFSolverTest, 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(BDFSolverTest, 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
Log In to Comment