CppNoddy  0.92
Loading...
Searching...
No Matches
BVPTroesch.cpp
Go to the documentation of this file.
1/// \file BVP_Troesch.cpp
2/// \ingroup Tests
3/// \ingroup BVP
4/// Solving the Troesch equation
5/// \f[ f''(y) = c \sinh (cy) \f]
6/// subject to \f$ f(0) = 0 \f$ and \f$ f(1) = 1 \f$.
7
8#include <BVP_bundle.h>
9
10#include "../Utils_Fill.h"
11
12// enumerate the variables in the ODE
13enum {f, fd };
14
15namespace CppNoddy
16{
17 namespace Example
18 {
19
20 /// Define the harmonic equation by inheriting the Equation base class
21 template <typename _Type, typename _Xtype>
22 class Troesch_equation : public Equation_1matrix<_Type, _Xtype>
23 {
24 public:
25 double c;
26
27 /// The harmonic equation is a 2nd order ODE
28 Troesch_equation() : Equation_1matrix<_Type, _Xtype>( 2 ) {}
29
30 /// The Berman equation
32 {
33 g[ f ] = z[ fd ];
34 g[ fd ] = c*std::sinh(c*z[f]);
35 }
36
38 {
40 }
41
42 };
43
44 template <typename _Type>
45 class Troesch_left_BC : public Residual<_Type>
46 {
47 public:
49
51 {
52 B[ 0 ] = z[ f ];
53 }
54 };
55
56 template <typename _Type>
57 class Troesch_right_BC : public Residual<_Type>
58 {
59 public:
61
63 {
64 B[ 0 ] = z[ f ] - 1.;
65 }
66 };
67
68
69 } // end Example namespace
70} // end CppNoddy namespace
71
72using namespace CppNoddy;
73using namespace std;
74
75int main()
76{
77 cout << "\n";
78 cout << "=== BVP: finite-difference solution of Troesch eqn ==\n";
79 cout << "\n";
80
82 Example::Troesch_left_BC <double> BC_left;
84
85 double left = 0.0;
86 double right = 1.0;
87 // number of nodal points
88 unsigned N = 151;
89 // a real mesh -- along real axis
90 DenseVector<double> nodes( Utility::uniform_node_vector( left, right, N ) );
91 // Example tolerance
92 const double tol = 1.e-4;
93
94 // a real ODE BVP
95 problem.c = 5.0;
96 ODE_BVP<double> ode( &problem, nodes, &BC_left, &BC_right );
97
98 // our initial guess
99 for ( unsigned i = 0; i < N; ++i )
100 {
101 double y = ode.solution().coord( i );
102 // set f(y)
103 ode.solution()( i, f ) = y;
104 // set f'(y)
105 ode.solution()( i, fd ) = 1;
106 }
107
108 // solve the problem using 2nd order finite-difference
109 try
110 {
111 ode.solve2();
112 }
113 catch (const std::runtime_error &error )
114 {
115 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
116 return 1;
117 }
118
119 // find deviation from y=0.5 solution obtianed from: http://www2.imperial.ac.uk/~jcash/BVP_software/readme.php
120 const double diff = std::abs( ode.solution().get_interpolated_vars(0.5)[f] - 0.55437396E-01 );
121
122 // validation test
123 if ( diff > tol )
124 {
125 cout << "\033[1;31;48m * FAILED \033[0m\n";
126 cout << "Real problem error " << diff << "\n";
127 return 1;
128 }
129 else
130 {
131 cout << "\033[1;32;48m * PASSED \033[0m\n";
132 return 0;
133 }
134}
@ fd
Definition: BVPBerman.cpp:15
@ f
Definition: BVPBerman.cpp:15
@ fd
Definition: BVPTroesch.cpp:13
@ f
Definition: BVPTroesch.cpp:13
int main()
Definition: BVPTroesch.cpp:75
A shorter bundled include file for ODE_BVP and PDE_IBVP codes.
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
An equation object base class used in the IBVP classes (and others).
Define the harmonic equation by inheriting the Equation base class.
Definition: BVPTroesch.cpp:23
void residual_fn(const DenseVector< _Type > &z, DenseVector< _Type > &g) const
The Berman equation.
Definition: BVPTroesch.cpp:31
void matrix0(const DenseVector< _Type > &x, DenseMatrix< _Type > &m) const
Define the matrix in terms of the current state vector.
Definition: BVPTroesch.cpp:37
Troesch_equation()
The harmonic equation is a 2nd order ODE.
Definition: BVPTroesch.cpp:28
void residual_fn(const DenseVector< _Type > &z, DenseVector< _Type > &B) const
A blank virtual residual function method.
Definition: BVPTroesch.cpp:50
void residual_fn(const DenseVector< _Type > &z, DenseVector< _Type > &B) const
A blank virtual residual function method.
Definition: BVPTroesch.cpp:62
A templated object for real/complex vector system of first-order ordinary differential equations.
Definition: ODE_BVP.h:37
OneD_Node_Mesh< _Type, _Xtype > & solution()
Definition: ODE_BVP.h:187
void solve2()
Formulate and solve the ODE using Newton iteration and a second-order finite difference scheme.
Definition: ODE_BVP.cpp:83
A base class to be inherited by objects that define residuals.
Definition: Residual.h:15
double g(1.0)
gravitational acceleration
DenseVector< double > uniform_node_vector(const double &lower, const double &upper, const std::size_t &N)
Return a DENSE vector with the nodal points of a uniform mesh distributed between the upper/lower bou...
Definition: Utility.cpp:113
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...
void fill_identity(CppNoddy::Sequential_Matrix_base< _Type > &A)
Fill diagonal with unit values.
Definition: Utils_Fill.h:22

© 2012

R.E. Hewitt