CppNoddy  0.92
Loading...
Searching...
No Matches
Namespaces | Functions
EVPHarmonicComplex_lapack.cpp File Reference

Solves the harmonic equation. More...

#include <Utility.h>
#include <EVP_bundle.h>

Go to the source code of this file.

Namespaces

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

Functions

const D_complex CppNoddy::Example::eye (0., 1.0)
 
const double CppNoddy::Example::delta (0.5)
 
D_complex CppNoddy::Example::z (double x)
 
D_complex CppNoddy::Example::zx (double x)
 
D_complex CppNoddy::Example::zxx (double x)
 
int main ()
 

Detailed Description

Solves the harmonic equation.

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

as an eigenvalue problem for $ \lambda $ over a path in the complex plane with homogeneous boundary conditions for $ f(z) $, returning any eigenvalue(s) with absolute value less than 10. The resulting eigenvalues should approach $ m^2 \pi^2 $ as $n \to \infty $ with $ O(\Delta^2)$ corrections where $ \Delta = 1/(n-1) $ and $ n $ is the number of nodal points in the FD representation. The complex path is parametrised by a real parameter "x" and a 2nd order central difference representation is used. The matrix problem is solved for all eigenvalues within a specified distance of the origin of the comlplex plane by calling the LAPACK generalised eigenvalue routine.

Definition in file EVPHarmonicComplex_lapack.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 46 of file EVPHarmonicComplex_lapack.cpp.

47{
48 cout << "\n";
49 cout << "=== EVP: Harmonic equation solved using LAPACK =====\n";
50 cout << "=== with a manually assembled matrix problem.\n";
51 cout << "=== The problem is solved along a path in the complex\n";
52 cout << "=== plane, deformed away from the real axis.\n";
53 cout << "\n";
54
55 cout.precision( 12 );
56 cout << " Number of nodal points : Leading eigenvalue error : Total CPU time taken (ms) \n";
57 bool failed = false;
58 size_t N = 4;
59 // a vector for the eigenvalues
61 DenseMatrix<D_complex> eigenvectors;
62
63 Timer timer;
64 for ( int i = 2; i < 11; ++i )
65 {
66 N = ( size_t ) ( std::pow( 2., i ) );
67 const double delta = 1. / ( N - 1 );
68 const double delta2 = delta * delta;
69 // matrix problem
70 DenseMatrix<D_complex> a( N, N, 0.0 );
71 DenseMatrix<D_complex> b( N, N, 0.0 );
72 // boundary conditions at f(0) = 0
73 a( 0, 0 ) = 1.0;
74 a( 0, 1 ) = 0.0;
75 for ( unsigned j = 1; j < N-1; ++j ) {
76 // Finite difference representation of f''(x)
77 double x = j*delta;
78 a( j, j-1 ) = (1.0/pow(Example::zx(x),2.0))/delta2 + (Example::zxx(x)/pow(Example::zx(x),3.0))/(2*delta);
79 a( j, j) = -(2.0/pow(Example::zx(x),2.0))/delta2;
80 a( j, j+1 ) = (1.0/pow(Example::zx(x),2.0))/delta2 - (Example::zxx(x)/pow(Example::zx(x),3.0))/(2*delta);
81
82 // not a generalised problem - but we'll apply that routine anyway
83 b( j, j ) = -1.0;
84 }
85 // boundary conditions at f(1) = 0
86 a( N - 1, N - 1 ) = 1.0;
87 a( N - 1, N - 2 ) = 0.0;
88 // a vector for the eigenvectors - although we won't use them
89 DenseLinearEigenSystem<D_complex> system( &a, &b );
90 system.set_calc_eigenvectors( true );
91
92 timer.start();
93 try
94 {
95 system.eigensolve();
96 } catch (const std::runtime_error &error ) {
97 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
98 return 1;
99 }
100
101 system.tag_eigenvalues_disc( + 1, 10. );
102 lambdas = system.get_tagged_eigenvalues();
103
104 eigenvectors = system.get_tagged_eigenvectors();
105
106 cout << " " << N << " : " << lambdas[ 0 ].real() - M_PI * M_PI << " +i " << lambdas[0].imag()
107 << " : " << timer.get_time() << "\n";
108 timer.stop();
109 }
110
111 // real parameterisation of complex path
112 DenseVector<double> xNodes( Utility::uniform_node_vector( 0.0, 1.0, N ) );
113 // complex path
114 DenseVector<D_complex> zNodes( N, D_complex(0.0,0.0) );
115 // mesh of complex data along a complex path
116 OneD_Node_Mesh<D_complex, D_complex> mesh( zNodes, 1 );
117 int index(0); // first and only eigenvalue returned
118 for ( unsigned j = 0; j < N; ++j ) {
119 // store the nodes in the complex plane
120 mesh.coord(j) = Example::z(xNodes[j]);
121 mesh(j,0) = eigenvectors(index,j+0); //f_j
122 }
123 // write the complex solution on the complex path
124 mesh.dump_gnu("/DATA/complexMesh.dat");
125 const double tol = 1.e-4;
126 if ( abs( lambdas[ 0 ].real() - M_PI * M_PI ) > tol )
127 failed = true;
128
129 if ( failed )
130 {
131 cout << "\033[1;31;48m * FAILED \033[0m\n";
132 cout << " Final error = " << abs( lambdas[ 0 ].real() - M_PI * M_PI ) << "\n";
133 return 1;
134 }
135
136 cout << "\033[1;32;48m * PASSED \033[0m\n";
137 return 0;
138}
A linear Nth-order generalised eigensystem class.
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
A one dimensional mesh utility object.
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
double get_time() const
Return the time.
Definition: Timer.cpp:34
void start()
Start the timer & reset stored time to zero.
Definition: Timer.cpp:12
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
Definition: Timer.cpp:17
const double delta(0.5)
DenseVector< double > real(const DenseVector< D_complex > &X)
Return a double DENSE vector containing the real part of a complex DENSE vector.
Definition: Utility.cpp:177
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
std::complex< double > D_complex
A complex double precision number using std::complex.
Definition: Types.h:98

References CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::coord(), CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::dump_gnu(), CppNoddy::DenseLinearEigenSystem< _Type >::eigensolve(), CppNoddy::DenseLinearEigenSystem< _Type >::get_tagged_eigenvalues(), CppNoddy::DenseLinearEigenSystem< _Type >::get_tagged_eigenvectors(), CppNoddy::Timer::get_time(), CppNoddy::LinearEigenSystem_base::set_calc_eigenvectors(), CppNoddy::Timer::start(), CppNoddy::Timer::stop(), CppNoddy::DenseLinearEigenSystem< _Type >::tag_eigenvalues_disc(), and CppNoddy::Utility::uniform_node_vector().

© 2012

R.E. Hewitt