CppNoddy  0.92
All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Modules Pages
Classes | Namespaces | Functions
IVPHarmonic.cpp File Reference

Integrate the harmonic equation. More...

#include <IVP_bundle.h>

Go to the source code of this file.

Classes

class  CppNoddy::Example::Harmonic_equation< _Type, _Xtype >
 Define the harmonic equation by inheriting the Equation base class. More...
 

Namespaces

namespace  CppNoddy
 A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechanics.
 
namespace  CppNoddy::Example
 

Functions

int main ()
 

Detailed Description

Integrate the harmonic equation.

\[ f''(y) + \lambda f(y) = 0 \]

with $\lambda=10$ from $ y = 0$ to $1$ as an IVP, using an adaptive Runge-Kutta-Fehlberg routine.

Definition in file IVPHarmonic.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 42 of file IVPHarmonic.cpp.

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
53 Example::Harmonic_equation 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}
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A templated object for real/complex vector system of first-order ordinary differential equations.
Definition: ODE_IVP.h:20
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
int & counter()
Increment an internal discrete counter.
Definition: Timer.cpp:44

References CppNoddy::Residual_with_coords< _Type, _Xtype >::coord(), CppNoddy::Timer::counter(), CppNoddy::Example::Harmonic_equation< _Type, _Xtype >::lambda, and CppNoddy::ODE_IVP< _Type >::shoot45().

© 2012

R.E. Hewitt