CppNoddy  0.92
Loading...
Searching...
No Matches
IVPLorenz.cpp
Go to the documentation of this file.
1/// \file IVPLorenz.cpp
2/// \ingroup Tests
3/// \ingroup IVP
4/// Integrate the Lorenz equations
5/// \f[ \dot x = a( y - x )\,,\quad \dot y = x ( b - z ) - y\,, \quad \dot z = -c z + xy\,, \f]
6/// forward in time using an adaptive Runge-Kutta-Fehlberg routine. The
7/// parameters are chosen to be \f$ a=10 \f$, \f$ b = 7 \f$, \f$ c = 8/3 \f$.
8/// There is a fixed point solution \f$ (x,y,z) = (4,4,6) \f$, which is tested here.
9
10#include <IVP_bundle.h>
11
12namespace CppNoddy
13{
14 namespace Example
15 {
16 /// Define the Lorenz equations by inheriting Equation base class
17 class Lorenz_equations : public Equation<double>
18 {
19 public:
20
21 /// Construct a 3rd order ODE
22 Lorenz_equations() : Equation<double>( 3 ) {};
23
24 /// We implement the equation as 3 first-order ODEs
26 {
27 f[ 0 ] = a * ( z[ 1 ] - z[ 0 ] );
28 f[ 1 ] = z[ 0 ] * ( b - z[ 2 ] ) - z[ 1 ];
29 f[ 2 ] = -c * z[ 2 ] + z[ 0 ] * z[ 1 ];
30 }
31
32 /// The usual 3 parameters of the Lorenz eqns
33 double a, b, c;
34
35 };
36 } // end Example namespace
37} // end CppNoddy namespace
38
39using namespace CppNoddy;
40using namespace std;
41
42int main()
43{
44 cout << "\n";
45 cout << "=== IVP: integrating the Lorenz system ==============\n";
46 cout << "\n";
47 cout << " Running shoot method - fixed step choice \n";
48
49 DenseVector<double> u( 3, 0.0 ), u_init( 3, 0.0 );
50 // initialise
51 u_init[ 0 ] = 1.0;
52 u_init[ 1 ] = 1.0;
53 u_init[ 2 ] = 1.0;
54 const int num_of_steps( 5000000 );
55 // set up the problem.
57 problem.a = 10.0;
58 problem.b = 7.0;
59 problem.c = 8.0 / 3.0;
60
61 // Construct an ODE from the problem.
62 ODE_IVP<double> ode( &problem, 0.0, 200.0, num_of_steps );
63 ode.store_every() = 10;
64
65 const double tol = 1.e-7;
66#ifdef TIME
67 Timer timer;
68 timer.start();
69 do
70 {
71#endif
72 try
73 {
74 u = ode.shoot45( u_init, tol, 0.1 );
75 }
76 catch (const std::runtime_error &error )
77 {
78 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
79 return 1;
80 }
81
82#ifdef TIME
83 timer.counter()++;
84 }
85 while ( timer.get_time() < 5000.0 );
86 timer.stop();
87 timer.print();
88#endif
89
90 cout.precision( 12 );
91 if ( abs( u[ 0 ] - 4.0 ) > tol )
92 {
93 cout << " Difference = " << abs( u[ 0 ] - 4.0 ) << "\n";
94 cout << "\033[1;31;48m * FAILED \033[0m\n";
95 }
96 else
97 {
98 cout << "\033[1;32;48m * PASSED \033[0m\n";
99 }
100 ode.get_mesh().dump_gnu("./DATA/test.dat");
101}
@ f
Definition: BVPBerman.cpp:15
int main()
Definition: IVPLorenz.cpp:42
A shorter bundled include file for initial-value problems.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
An equation object base class used in the BVP/IVP classes.
Definition: Equation.h:22
Define the Lorenz equations by inheriting Equation base class.
Definition: IVPLorenz.cpp:18
double a
The usual 3 parameters of the Lorenz eqns.
Definition: IVPLorenz.cpp:33
void residual_fn(const DenseVector< double > &z, DenseVector< double > &f) const
We implement the equation as 3 first-order ODEs.
Definition: IVPLorenz.cpp:25
Lorenz_equations()
Construct a 3rd order ODE.
Definition: IVPLorenz.cpp:22
A templated object for real/complex vector system of first-order ordinary differential equations.
Definition: ODE_IVP.h:20
OneD_Node_Mesh< _Type > & get_mesh()
Return the history of the stepped solution.
Definition: ODE_IVP.cpp:242
unsigned & store_every()
Return a handle to the STORE_EVERY object.
Definition: ODE_IVP.cpp:247
DenseVector< _Type > shoot45(DenseVector< _Type > u, const double &tol, const double &h_init)
A Runge-Kutta-Fehlberg integrator.
Definition: ODE_IVP.cpp:95
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
double get_time() const
Return the time.
Definition: Timer.cpp:34
void start()
Start the timer & reset stored time to zero.
Definition: Timer.cpp:12
int & counter()
Increment an internal discrete counter.
Definition: Timer.cpp:44
void print() const
Write a string to cout stating the time taken.
Definition: Timer.cpp:59
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
Definition: Timer.cpp:17
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt