Page MenuHomec4science

test_embeddedrungekutta.hpp
No OneTemporary

File Metadata

Created
Tue, Nov 19, 08:24

test_embeddedrungekutta.hpp

/*-------------------------------------------------------
- Module : tests/odeint
- File : test_embeddedrungekutta.hpp
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget <fabieng@princeton.edu>, Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Princeton University nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------*/
#include "cxxtest/TestSuite.h"
#include "odeint/runge_kutta_step.hpp"
using namespace specmicp::odeint;
void rhs1(double x, const double& yin, double& yout)
{
yout = -2*yin;
}
void rhs2(double x, const vector_t<2>& y, vector_t<2>& yout)
{
yout(0) = y(1);
yout(1) = -y(0);
}
class TestEmbeddedRungeKutta : public CxxTest::TestSuite
{
public:
void test_simpletest_cashkarp()
{
CashKarpStep<1> algo(rhs1, butcher_cash_karp45);
double yout, yerr;
double tf = 0.01;
algo.run_step(1, -2, 0, tf, yout, yerr);
TS_ASSERT_DELTA(yout, std::exp(-2*tf), 1e-3);
tf = 0.05;
algo.run_step(1, -2, 0, tf, yout, yerr);
TS_ASSERT_DELTA(yout, std::exp(-2*tf), 1e-2);
}
void test_simpletest_dormandprince()
{
DormandPrinceStep<1> algo(rhs1, butcher_dormand_prince45);
double yout, yerr;
double tf = 0.005;
algo.run_step(1, -2, 0, tf, yout, yerr);
TS_ASSERT_DELTA(yout, std::exp(-2*tf), 1e-3);
tf = 0.01;
algo.run_step(1, -2, 0, tf, yout, yerr);
TS_ASSERT_DELTA(yout, std::exp(-2*tf), 1e-2);
}
void test_spring_cashkarp()
{
CashKarpStep<2> algo = get_cash_karp_step<2>(rhs2);
vector_t<2> yout, yerr;
vector_t<2> y0, dydx;
y0 << 1, 0;
dydx << 0, -1;
double tf=0.01;
algo.run_step(y0, dydx, 0, tf, yout, yerr);
TS_ASSERT_DELTA(yout(0), std::cos(tf), 1e-3);
}
void test_spring_dormandprince()
{
DormandPrinceStep<2> algo = get_dormand_prince_step<2>(rhs2);
vector_t<2> yout, yerr;
vector_t<2> y0, dydx;
y0 << 1, 0;
dydx << 0, -1;
double tf=0.01;
algo.run_step(y0, dydx, 0, tf, yout, yerr);
TS_ASSERT_DELTA(yout(0), std::cos(tf), 1e-3);
}
void test_adaptative_test_CK()
{
CashKarpStep<1> algo = get_cash_karp_step<1>(rhs1);
vector_t<1> y, dydx;
y = 1;
dydx = -2;
StepLength stepl(0.5);
double x = 0.0;
int cnt = 0;
while (x < 5)
{
algo.rk_step(y, dydx, x, stepl);
cnt +=1;
TS_ASSERT_DELTA(y, std::exp(-2*x), 1e-3);
stepl.get_next();
algo.get_rhs(x, y, dydx);
}
}
void test_adaptative_test_DP()
{
DormandPrinceStep<1> algo = get_dormand_prince_step<1>(rhs1);
vector_t<1> y, dydx;
y = 1;
dydx = -2;
StepLength stepl(0.5);
double x = 0.0;
int cnt = 0;
while (x < 5)
{
algo.rk_step(y, dydx, x, stepl);
cnt +=1;
TS_ASSERT_DELTA(y, std::exp(-2*x), 1e-3);
stepl.get_next();
algo.get_rhs(x, y, dydx);
}
}
void test_adaptative_cos()
{
CashKarpStep<2> algo = get_cash_karp_step<2>(rhs2);
vector_t<2> y, dydx;
y << 1, 0;
dydx << 0, -1;
StepLength stepl(0.5);
double x = 0.0;
int cnt = 0;
while (x <4)
{
algo.rk_step(y, dydx, x, stepl);
cnt +=1;
TS_ASSERT_DELTA(y(0), std::cos(x), 1e-3);
stepl.get_next();
algo.get_rhs(x, y, dydx);
}
}
};

Event Timeline