CppNoddy  0.92
Loading...
Searching...
No Matches
BVPNonIdentityMatrix0.cpp
Go to the documentation of this file.
1/// \file BVPNonIdentityMatrix0.cpp
2/// \ingroup Tests
3/// \ingroup BVP
4/// Solving the equation
5/// \f[ f(y) f''(y) + f'(y)^2 = 1+\gamma y \f]
6/// subject to \f$ f(0) = 1 \f$ and \f$ f(1) = 2 \f$.
7/// We don't divide by the \f$ f(y) \f$ that multiplies the
8/// highest derivative, rather we define a non-identity matrix0.
9/// The solution is compared at all nodes to the exact solution
10/// \f[ f(y)=\left ( y^2 + \frac{\gamma}{3}y^3 + (2-\frac{\gamma}{3}) y + 1 \right )^{1/2}\,.\f]
11
12#include <BVP_bundle.h>
13
14// enumerate the variables in the ODE
15enum {f, fd };
16
17namespace CppNoddy
18{
19 namespace Example
20 {
21
22 /// Define the harmonic equation by inheriting the Equation base class
23 template <typename _Type, typename _Xtype>
24 class Nonidentity_equation : public Equation_1matrix<_Type, _Xtype>
25 {
26 public:
27
28 double gamma;
29
30 /// The harmonic equation is a 2nd order ODE
31 Nonidentity_equation() : Equation_1matrix<_Type, _Xtype>( 2 ) {}
32
33 /// The Berman equation
35 {
36 const double y( this -> coord(0) );
37 g[ f ] = z[ fd ];
38 g[ fd ] = 1 + gamma*y - z[ fd ]*z[ fd ];
39 }
40
42 {
43 m( 0, 0 ) = 1.0;
44 m( 1, 1 ) = z[f];
45 }
46
47 };
48
49 template <typename _Type>
50 class Nonidentity_left_BC : public Residual<_Type>
51 {
52 public:
54
56 {
57 B[ 0 ] = z[ f ] - 1;
58 }
59 };
60
61 template <typename _Type>
62 class Nonidentity_right_BC : public Residual<_Type>
63 {
64 public:
66
68 {
69 B[ 0 ] = z[ f ] - 2;
70 }
71 };
72
73
74 } // end Example namespace
75} // end CppNoddy namespace
76
77using namespace CppNoddy;
78using namespace std;
79
80int main()
81{
82 cout << "\n";
83 cout << "=== BVP: finite-difference, non-identity matrix0 ====\n";
84 cout << "\n";
85
87 Example::Nonidentity_left_BC <double> BC_left;
89
90 double left = 0.0;
91 double right = 1.0;
92 // number of nodal points
93 unsigned N = 151;
94 // a real mesh -- along real axis
95 DenseVector<double> nodes( Utility::uniform_node_vector( left, right, N ) );
96 // Example tolerance
97 const double tol = 1.e-4;
98
99 // a real ODE BVP
100 problem.gamma = 5.0;
101 ODE_BVP<double> ode( &problem, nodes, &BC_left, &BC_right );
102
103 // our initial guess
104 for ( unsigned i = 0; i < N; ++i )
105 {
106 double y = ode.solution().coord( i );
107 // set f(y)
108 ode.solution()( i, f ) = 1+y;
109 // set f'(y)
110 ode.solution()( i, fd ) = 1;
111 }
112
113 // solve the problem using 2nd order finite-difference
114 try
115 {
116 ode.solve2();
117 }
118 catch (const std::runtime_error& error )
119 {
120 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
121 return 1;
122 }
123
124
125 // find deviation from exact solution
126 double diff = 0;
127 for ( unsigned i = 0; i < N; ++i )
128 {
129 const double y( ode.solution().coord(i) );
130 const double exact( sqrt(y*y + problem.gamma*y*y*y/3 + (2-problem.gamma/3)*y + 1) );
131 diff = std::max( std::abs( ode.solution().get_interpolated_vars(y)[f] - exact ), diff );
132 }
133
134 // validation test
135 if ( diff > tol )
136 {
137 cout << "\033[1;31;48m * FAILED \033[0m\n";
138 cout << "Real problem error " << diff << "\n";
139 return 1;
140 }
141 else
142 {
143 cout << "\033[1;32;48m * PASSED \033[0m\n";
144 return 0;
145 }
146}
@ fd
Definition: BVPBerman.cpp:15
@ f
Definition: BVPBerman.cpp:15
int main()
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.
void matrix0(const DenseVector< _Type > &z, DenseMatrix< _Type > &m) const
Define the matrix in terms of the current state vector.
Nonidentity_equation()
The harmonic equation is a 2nd order ODE.
void residual_fn(const DenseVector< _Type > &z, DenseVector< _Type > &g) const
The Berman equation.
void residual_fn(const DenseVector< _Type > &z, DenseVector< _Type > &B) const
A blank virtual residual function method.
void residual_fn(const DenseVector< _Type > &z, DenseVector< _Type > &B) const
A blank virtual residual function method.
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
_Xtype & coord(const unsigned &i)
General handle access to the coordinates.
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...

© 2012

R.E. Hewitt