CppNoddy  0.92
Loading...
Searching...
No Matches
1DNodeMeshAiry_lapack.cpp
Go to the documentation of this file.
1/// \file 1DNodeMeshAiry_lapack.cpp
2/// \ingroup Tests
3/// \ingroup Generic
4/// Solves the Airy equation
5/// \f[ f''(x) - xf(x) = 0 \f]
6/// on the negative real axis over the
7/// range [-10,0] then pushes the data into a OneD_GenMesh object
8/// and integrates the result. The integral is checked against the
9/// table in Abramowitz & Stegun:
10/// \f[ \int_{-10}^0 \mbox{Ai}(s) \, \mbox{d}s \approx 0.7656984 \f]
11/// The finite-difference solution of
12/// the Airy equation and the trapezoidal scheme used in the
13/// integration are both second order.
14
15#include <Utility.h>
16#include <OneD_Node_Mesh.h>
17#include <BandedLinearSystem.h>
18
19using namespace CppNoddy;
20using namespace std;
21
22int main()
23{
24
25 cout << "\n";
26 cout << "=== OneD_Node_Mesh & BandedMatrix: Airy function ====\n";
27 cout << "\n";
28
29 size_t n = 8001; // number of points
30 double l = -10.0; // domain size
31 double d = abs( l ) / ( n - 1 ); // Mesh step
32 // a uniform mesh to store the result in
34
35 BandedMatrix<double> a( n, 3, 0.0 ); // Tridiagonal banded matrix
36 DenseVector<double> b( n, 0.0 ); // RHS vector
37
38 a( 0, 0 ) = 1.0; // BC is that f(-10) = Ai(-10)
39 b[ 0 ] = 0.04024123849; // value from Abramowitz & Stegun
40 for ( size_t i = 1; i < n - 1; ++i )
41 {
42 // set up the f''(x) operator
43 a( i, i-1 ) = 1.0/(d*d);
44 a( i, i ) = -2.0/(d*d);
45 a( i, i+1 ) = 1.0/(d*d);
46 // add the -xf(x) term
47 a( i, i ) -= soln.coord( i );
48 }
49 a( n - 1, n - 1 ) = 1.0; // BC is that f(0) = Ai(0)
50 b[ n - 1 ] = 0.3550280539; // value from Abramowitz & Stegun
51
52 // solve the FD equations
53 BandedLinearSystem<double> system( &a, &b, "lapack" );
54
55 try
56 {
57 system.solve();
58 }
59 catch (const std::runtime_error &error )
60 {
61 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
62 return 1;
63 }
64
65 // put the result into the solution mesh
66 soln.set_vars_from_vector( b );
67
68 // check the integral against Abramowitz & Stegun data
69 const double tol = 1.e-5;
70 if ( abs( soln.integral2() - 0.7656984 ) > tol )
71 {
72 cout << "\033[1;31;48m * FAILED \033[0m\n";
73 cout.precision( 10 );
74 cout << n << " " << soln.integral2() - 0.7656984 << "\n";
75 return 1;
76 }
77 else
78 {
79 cout << "\033[1;32;48m * PASSED \033[0m\n";
80 return 0;
81 }
82
83}
int main()
Specification of the linear system class.
A specification for a one dimensional mesh object.
A spec for a collection of utility functions.
A linear system class for vector right-hand sides.
void solve()
Solve the banded system.
A matrix class that constructs a BANDED matrix.
Definition: BandedMatrix.h:16
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A one dimensional mesh utility object.
void set_vars_from_vector(const DenseVector< _Type > &vec)
Set the variables of this mesh from a vector.
const _Xtype & coord(const std::size_t &node) const
Access a nodal position.
_Type integral2(std::size_t var=0) const
Integrate over the domain.
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