本文整理汇总了C++中OdeSolution::GetNumberOfTimeSteps方法的典型用法代码示例。如果您正苦于以下问题:C++ OdeSolution::GetNumberOfTimeSteps方法的具体用法?C++ OdeSolution::GetNumberOfTimeSteps怎么用?C++ OdeSolution::GetNumberOfTimeSteps使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OdeSolution
的用法示例。
在下文中一共展示了OdeSolution::GetNumberOfTimeSteps方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: MyTestSolverOnOdesWithEvents
// Test a given solver on an ODE which has a stopping event defined
void MyTestSolverOnOdesWithEvents(AbstractIvpOdeSolver& rSolver)
{
// ODE which has solution y0 = cos(t), and stopping event y0<0,
// ie should stop when t = pi/2;
OdeSecondOrderWithEvents ode_with_events;
OdeSolution solutions;
std::vector<double> state_variables =
ode_with_events.GetInitialConditions();
solutions = rSolver.Solve(&ode_with_events, state_variables, 0.0, 2.0,
0.001, 0.001);
unsigned num_timesteps = solutions.GetNumberOfTimeSteps();
// Final time should be around pi/2
TS_ASSERT_DELTA( solutions.rGetTimes()[num_timesteps], M_PI_2, 0.01);
// Penultimate y0 should be greater than zero
TS_ASSERT_LESS_THAN( 0, solutions.rGetSolutions()[num_timesteps-1][0]);
// Final y0 should be less than zero
TS_ASSERT_LESS_THAN( solutions.rGetSolutions()[num_timesteps][0], 0);
// Solver should correctly state the stopping event occurred
TS_ASSERT_EQUALS(rSolver.StoppingEventOccurred(), true);
// This is to cover the exception when a stopping event occurs before the first timestep.
TS_ASSERT_THROWS_ANYTHING(rSolver.Solve(&ode_with_events, state_variables, 2.0, 3.0, 0.001));
///////////////////////////////////////////////
// Repeat with sampling time larger than dt
///////////////////////////////////////////////
state_variables = ode_with_events.GetInitialConditions();
solutions = rSolver.Solve(&ode_with_events, state_variables, 0.0, 2.0,
0.001, 0.01);
num_timesteps = solutions.GetNumberOfTimeSteps();
// Final time should be around pi/2
TS_ASSERT_DELTA( solutions.rGetTimes()[num_timesteps], M_PI_2, 0.01);
// Penultimate y0 should be greater than zero
TS_ASSERT_LESS_THAN( 0, solutions.rGetSolutions()[num_timesteps-1][0]);
// Final y0 should be less than zero
TS_ASSERT_LESS_THAN( solutions.rGetSolutions()[num_timesteps][0], 0);
// Solver should correctly state the stopping event occurred
TS_ASSERT_EQUALS(rSolver.StoppingEventOccurred(), true);
// Cover the check event isn't initially true exception
std::vector<double> bad_init_cond;
bad_init_cond.push_back(-1); //y0 < 0 so stopping event true
bad_init_cond.push_back(0.0);
TS_ASSERT_THROWS_ANYTHING(rSolver.Solve(&ode_with_events, bad_init_cond, 0.0, 2.0, 0.001, 0.01));
}
示例2: throw
void TestRKFehlbergSystemOf3Equations() throw(Exception)
{
OdeThirdOrder ode_system;
double h_value = 0.1;
// Euler solver solution worked out
RungeKuttaFehlbergIvpOdeSolver rkf_solver;
OdeSolution solutions;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = rkf_solver.Solve(&ode_system, state_variables, 0.0, 2.0, 0.25, 1e-5);
unsigned last = solutions.GetNumberOfTimeSteps();
double numerical_solution[3];
numerical_solution[0] = solutions.rGetSolutions()[last][0];
numerical_solution[1] = solutions.rGetSolutions()[last][1];
numerical_solution[2] = solutions.rGetSolutions()[last][2];
// The tests
double analytical_solution[3];
analytical_solution[0] = -sin(2.0);
analytical_solution[1] = sin(2.0)+cos(2.0);
analytical_solution[2] = 2*sin(2.0);
double global_error_rkf = 0.5*2*(exp(2.0)-1)*h_value;
TS_ASSERT_DELTA(numerical_solution[0],analytical_solution[0],global_error_rkf);
TS_ASSERT_DELTA(numerical_solution[1],analytical_solution[1],global_error_rkf);
TS_ASSERT_DELTA(numerical_solution[2],analytical_solution[2],global_error_rkf);
}
示例3: TestBackwardEulerVanDerPolOde
void TestBackwardEulerVanDerPolOde()
{
VanDerPolOde ode_system;
double h_value = 0.01;
double end_time = 100.0;
// Euler solver solution worked out
BackwardEulerIvpOdeSolver backward_euler_solver(ode_system.GetNumberOfStateVariables());
backward_euler_solver.ForceUseOfNumericalJacobian(); // coverage
OdeSolution solutions;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = backward_euler_solver.Solve(&ode_system, state_variables, 0.0, end_time, h_value, 5*h_value);
unsigned last = solutions.GetNumberOfTimeSteps();
// OutputFileHandler handler("");
// out_stream rabbit_file=handler.OpenOutputFile("foxrabbit.dat");
//
// for (unsigned i=0; i<last; i++)
// {
// (*rabbit_file) << solutions.rGetSolutions()[i][0] << "\t" << solutions.rGetSolutions()[i][1] << "\n" << std::flush;
// }
// rabbit_file->close();
// assert that we are within a [-2,2] in x and [-2,2] in y (on limit cycle)
TS_ASSERT_DELTA(solutions.rGetSolutions()[last][0], 0, 2);
TS_ASSERT_DELTA(solutions.rGetSolutions()[last][1], 0, 2);
}
示例4: backward_euler_solver
void TestBackwardEulerSystemOf3EquationsWithEvents()
{
OdeThirdOrderWithEvents ode_system_with_events;
double h_value = 0.01;
// Euler solver solution worked out
BackwardEulerIvpOdeSolver backward_euler_solver(ode_system_with_events.GetNumberOfStateVariables());
OdeSolution solutions;
std::vector<double> state_variables = ode_system_with_events.GetInitialConditions();
solutions = backward_euler_solver.Solve(&ode_system_with_events, state_variables, 0.0, 2.0, h_value, h_value);
unsigned last = solutions.GetNumberOfTimeSteps();
// Final time should be pi/6 (?)
TS_ASSERT_DELTA( solutions.rGetTimes()[last], 0.5236, 0.01);
// Penultimate y0 should be greater than -0.5
TS_ASSERT_LESS_THAN(-0.5,solutions.rGetSolutions()[last-1][0]);
// Final y0 should be less than -0.5
TS_ASSERT_LESS_THAN( solutions.rGetSolutions()[last][0], -0.5);
// Solver should correctly state the stopping event occurred
TS_ASSERT_EQUALS(backward_euler_solver.StoppingEventOccurred(), true);
}
示例5: TestRKFehlbergWithExampleFromBook
void TestRKFehlbergWithExampleFromBook() throw(Exception)
{
/*
* Book is "Numerical Analysis 6th Edition by R.L. Burden and J. D. Faires
* This example is on P291 Table 5.9
*/
RkfTestOde ode;
double max_step_size = 0.25;
double start_time = 0.0;
double end_time = 2.0;
RungeKuttaFehlbergIvpOdeSolver rkf_solver;
OdeSolution solutions;
std::vector<double> state_variables = ode.GetInitialConditions();
double tolerance = 1e-5;
solutions = rkf_solver.Solve(&ode, state_variables, start_time, end_time, max_step_size, tolerance);
// Times (from MatLab Code) to check timstepping is being adapted properly
TS_ASSERT_DELTA(solutions.rGetTimes()[0], 0, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[1], 2.500000000000000e-01, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[2], 4.868046415733731e-01, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[3], 7.298511818781566e-01, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[4], 9.798511818781566e-01, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[5], 1.229851181878157e+00, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[6], 1.479851181878157e+00, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[7], 1.729851181878157e+00, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[8], 1.979851181878157e+00, 1e-7);
TS_ASSERT_DELTA(solutions.rGetTimes()[9], 2.000000000000000e+00, 1e-7);
TS_ASSERT_EQUALS(solutions.GetNumberOfTimeSteps(), 9u);
// y values (from analytic result)
for (unsigned i=0; i<solutions.GetNumberOfTimeSteps(); i++)
{
double time = solutions.rGetTimes()[i];
double y = (time+1.0)*(time+1.0) - 0.5*exp(time);
// Tolerance set to 1e-5, so 2e-5 to pass here
TS_ASSERT_DELTA(solutions.rGetSolutions()[i][0], y, 2e-5);
}
}
示例6: TestArchivingRkfSolver
void TestArchivingRkfSolver() throw(Exception)
{
OutputFileHandler handler("archive",false);
std::string archive_filename;
archive_filename = handler.GetOutputDirectoryFullPath() + "rkf_solver.arch";
Ode5Jacobian ode_system;
OdeSolution solutions;
double h_value = 0.1;
double end_time = 1.0;
// Create and archive simulation time
{
std::ofstream ofs(archive_filename.c_str());
boost::archive::text_oarchive output_arch(ofs);
// Set up a solver
AbstractIvpOdeSolver* const p_rkf_ode_solver = new RungeKuttaFehlbergIvpOdeSolver;
// Should always archive a pointer
output_arch << p_rkf_ode_solver;
// Change stimulus a bit
delete p_rkf_ode_solver;
}
// Restore
{
std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
boost::archive::text_iarchive input_arch(ifs);
// Create a pointer
AbstractIvpOdeSolver* p_rkf;
input_arch >> p_rkf;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = p_rkf->Solve(&ode_system, state_variables, 0.0, end_time, h_value, 1e-5);
unsigned last = solutions.GetNumberOfTimeSteps();
double numerical_solution;
numerical_solution = solutions.rGetSolutions()[last][0];
// The tests
double analytical_solution = 1.0/(1.0+4.0*exp(-100.0*end_time));
TS_ASSERT_DELTA(numerical_solution,analytical_solution,1.0e-3);
delete p_rkf;
}
}
示例7: TestArchivingSolver
void TestArchivingSolver() throw(Exception)
{
OutputFileHandler handler("archive", false);
ArchiveLocationInfo::SetArchiveDirectory(handler.FindFile(""));
std::string archive_filename = ArchiveLocationInfo::GetProcessUniqueFilePath("backward_euler_solver.arch");
VanDerPolOde ode_system;
double h_value = 0.01;
double end_time = 100.0;
// Create and archive simulation time
{
std::ofstream ofs(archive_filename.c_str());
boost::archive::text_oarchive output_arch(ofs);
// Set up a solver
AbstractIvpOdeSolver* const p_backward_euler_solver = new BackwardEulerIvpOdeSolver(ode_system.GetNumberOfStateVariables());
// Should always archive a pointer
output_arch << p_backward_euler_solver;
// Change stimulus a bit
delete p_backward_euler_solver;
}
// Restore
{
std::ifstream ifs(archive_filename.c_str(), std::ios::binary);
boost::archive::text_iarchive input_arch(ifs);
// Create a pointer
AbstractIvpOdeSolver* p_backward_euler;
input_arch >> p_backward_euler;
OdeSolution solutions;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = p_backward_euler->Solve(&ode_system, state_variables, 0.0, end_time, h_value, 5*h_value);
unsigned last = solutions.GetNumberOfTimeSteps();
// assert that we are within a [-2,2] in x and [-2,2] in y (on limit cycle)
TS_ASSERT_DELTA(solutions.rGetSolutions()[last][0], 0, 2);
TS_ASSERT_DELTA(solutions.rGetSolutions()[last][1], 0, 2);
delete p_backward_euler;
}
}
示例8: TestRKFehlbergNonlinearEquation
void TestRKFehlbergNonlinearEquation() throw(Exception)
{
Ode4 ode_system;
double h_value = 0.1;
// Euler solver solution worked out
RungeKuttaFehlbergIvpOdeSolver rkf_solver;
OdeSolution solutions;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = rkf_solver.Solve(&ode_system, state_variables, 0.0, 2.0, h_value, 1e-5);
int last = solutions.GetNumberOfTimeSteps();
double numerical_solution;
numerical_solution = solutions.rGetSolutions()[last][0];
// The tests
double analytical_solution = 1.0/(1.0+exp(-12.5));
TS_ASSERT_DELTA(numerical_solution,analytical_solution,1.0e-4);
}
示例9: TestBackwardEulerNonlinearEquation
void TestBackwardEulerNonlinearEquation()
{
Ode4 ode_system;
double h_value = 0.01;
// Euler solver solution worked out
BackwardEulerIvpOdeSolver backward_euler_solver(ode_system.GetNumberOfStateVariables());
OdeSolution solutions;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = backward_euler_solver.Solve(&ode_system, state_variables, 0.0, 2.0, h_value, h_value);
unsigned last = solutions.GetNumberOfTimeSteps();
double numerical_solution;
numerical_solution = solutions.rGetSolutions()[last][0];
// The tests
double analytical_solution = 1.0/(1.0+exp(-12.5));
TS_ASSERT_DELTA(numerical_solution, analytical_solution, 1.0e-4);
}
示例10: TestRKFehlbergAnotherNonlinearEquationAnalytic
void TestRKFehlbergAnotherNonlinearEquationAnalytic() throw(Exception)
{
Ode5Jacobian ode_system;
double h_value = 0.1;
double end_time = 1.0;
// Euler solver solution worked out
RungeKuttaFehlbergIvpOdeSolver rkf_solver;
OdeSolution solutions;
std::vector<double> state_variables = ode_system.GetInitialConditions();
solutions = rkf_solver.Solve(&ode_system, state_variables, 0.0, end_time, h_value, 1e-5);
unsigned last = solutions.GetNumberOfTimeSteps();
double numerical_solution;
numerical_solution = solutions.rGetSolutions()[last][0];
// The tests
double analytical_solution = 1.0/(1.0+4.0*exp(-100.0*end_time));
TS_ASSERT_DELTA(numerical_solution,analytical_solution,1.0e-3);
}