CppNoddy  0.92
Loading...
Searching...
No Matches
IVPHarmonic.cpp
Go to the documentation of this file.
1/// \file IVPHarmonic.cpp
2/// \ingroup Tests
3/// \ingroup IVP
4/// Integrate the harmonic equation
5/// \f[ f''(y) + \lambda f(y) = 0 \f] with \f$\lambda=10\f$
6/// from \f$ y = 0\f$ to \f$1\f$ as an IVP,
7/// using an adaptive Runge-Kutta-Fehlberg routine.
8
9
10#include <IVP_bundle.h>
11
12namespace CppNoddy
13{
14 namespace Example
15 {
16 /// Define the harmonic equation by inheriting Equation base class
17 class Harmonic_equation : public Equation<double>
18 {
19 public:
20 /// The harmonic equation is 2nd order
21 Harmonic_equation() : Equation<double> ( 2 )
22 {
23 }
24
25 /// We implement the equation as 2 first-order ODEs
27 {
28 f[ 0 ] = z[ 1 ];
29 f[ 1 ] = - lambda * z[ 0 ];
30 }
31
32 /// A parameter
33 double lambda;
34
35 };
36 } // end Example namespace
37} // end CppNoddy namespace
38
39using namespace CppNoddy;
40using namespace std;
41
42int main()
43{
44
45 Timer timer;
46 cout << "\n";
47 cout << "=== IVP: solving harmonic equation ==================\n";
48 cout << " Solving between x = 0 to x = 10 \n";
49 cout << "\n";
50 cout << " Running shoot45 method - auto step choice \n";
51
52 // set up the problem
54 problem.lambda = 10.0;
55
56 // pass it to the ode
57 ODE_IVP<double> ode( &problem, 0.0, 10.0, 1000 );
58 // run the checks
59 cout.precision( 12 );
60
61 double tol( 1.e-7 );
62 DenseVector<double> u_init( 2, 0.0 );
63 DenseVector<double> u_final( 2, 0.0 );
64 u_init[ 0 ] = 1.0;
65 u_init[ 1 ] = 0.0;
66
67 problem.coord(0) = 0.0;
68 ode.shoot45( u_init, tol, 0.01 );
69
70 try
71 {
72 u_final = ode.shoot45( u_init, tol, 0.01 );
73 }
74 catch (const std::runtime_error &error )
75 {
76 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
77 return 1;
78 }
79 timer.counter()++;
80
81 bool failed( false );
82 for ( int j = 1; j < 7; j++ )
83 {
84 // run through a few tolerance choices
85 tol = 1. / ( pow( 10., j ) );
86 // auto step choice; tolerance; initial step
87 u_final = ode.shoot45( u_init, tol, 0.01 );
88 // some output
89 cout << "\n";
90 cout << " Error relative tol : " << tol << "\n";
91 cout << " Error |num - exact|: "
92 << abs( u_final[ 0 ] - cos( sqrt( problem.lambda ) * 10. ) ) << "\n";
93
94 if ( abs( u_final[ 0 ] - cos( sqrt( problem.lambda ) * 10. ) ) > 10. * tol )
95 {
96 failed = true;
97 }
98 }
99
100 if ( failed )
101 {
102 cout << "\033[1;31;48m * FAILED \033[0m\n";
103 return 1;
104 }
105 else
106 {
107 cout << "\033[1;32;48m * PASSED \033[0m\n";
108 return 0;
109 }
110}
@ f
Definition: BVPBerman.cpp:15
int main()
Definition: IVPHarmonic.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 harmonic equation by inheriting the Equation base class.
Definition: BVPHarmonic.cpp:26
void residual_fn(const DenseVector< double > &z, DenseVector< double > &f) const
We implement the equation as 2 first-order ODEs.
Definition: IVPHarmonic.cpp:26
Harmonic_equation()
The harmonic equation is 2nd order.
Definition: IVPHarmonic.cpp:21
A templated object for real/complex vector system of first-order ordinary differential equations.
Definition: ODE_IVP.h:20
DenseVector< _Type > shoot45(DenseVector< _Type > u, const double &tol, const double &h_init)
A Runge-Kutta-Fehlberg integrator.
Definition: ODE_IVP.cpp:95
_Xtype & coord(const unsigned &i)
General handle access to the coordinates.
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
int & counter()
Increment an internal discrete counter.
Definition: Timer.cpp:44
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt