CppNoddy  0.92
Loading...
Searching...
No Matches
EVPHarmonicLocal.cpp
Go to the documentation of this file.
1/// \file EVPHarmonicLocal.cpp
2/// \ingroup Tests
3/// \ingroup EVP
4/// Solves the harmonic equation
5/// \f[ f''(x) + \lambda f(x) = 0 \f]
6/// as a LOCAL eigenvalue problem for \f$ \lambda \f$ over the unit domain
7/// with homogeneous boundary conditions for \f$ f(x) \f$ but using
8/// the nonlinear BVP solver to refine a guess at an eigenvalue.
9/// For example, we would have run an (expensive) global eigenvalue
10/// search (EVP_Harmonic) then refined the eigenvalue of interest in
11/// the way shown in this example.
12///
13/// Note: This approach (as it stands) is not as efficient as it could
14/// be since we include N extra degrees of freedom (where N is the number
15/// of mesh points) into the problem for only one eigenvalue. This retains
16/// the banded structure, but it would perhaps be better to solve using
17/// a sparse structure, or an appropriate multi-step algorithm for the
18/// for the banded matrix.
19
20#include <BVP_bundle.h>
21
22#include "../Utils_Fill.h"
23
24// enumerate the 3 variables
25enum { f, fd, lambda };
26
27namespace CppNoddy
28{
29 namespace Example
30 {
31 /// Define the harmonic equation by inheriting Equation base class
32 class Harmonic_equation : public Equation_1matrix<D_complex>
33 {
34 public:
35
36 /// The eqn is a *3rd* order complex nonlinear ODE
37 /// because the eigenvalue is an unknown
39
40 /// Define the harmonic eqn
42 {
43 g[ f ] = z[ fd ];
44 g[ fd ] = - z[ lambda ] * z[ f ];
45 // inclusion of eigenvalue everywhere maintains the bandedness,
46 // albeit at an obvious cost.
47 g[ lambda ] = 0.0;
48 }
49
50 /// matrix to multiply the BVP coordinate
52 {
54 }
55
56 };
57
58 class Harmonic_left_BC : public Residual<D_complex>
59 {
60 public:
62
64 {
65 B[ 0 ] = z[ f ];
66 B[ 1 ] = z[ fd ] - 1.0; // arbitrary amplitude
67 }
68 };
69
70 class Harmonic_right_BC : public Residual<D_complex>
71 {
72 public:
74
76 {
77 B[ 0 ] = z[ f ];
78 }
79 };
80
81
82 } // end Example namespace
83} // end CppNoddy namespace
84
85using namespace CppNoddy;
86using namespace std;
87
88int main()
89{
90 cout << "\n";
91 cout << "=== EVP: Local refinement of an eigenvalue =========\n";
92 cout << "\n";
93
94 cout << " Number of points : |local eigenvalue - pi^2| \n";
95
96 // set up the problem
98 // set up BCs
101 // set up the domain from 0 to 1
102 double left = 0.0;
103 double right = 1.0;
104 // set our guess for the eigenvalue -- this would come
105 // from the global method in a less trivial problem.
106 D_complex eigenvalue_guess = 9.0;
107 bool failed( true );
108 // loop through some meshes with increasing numbers of points
109 for ( unsigned i = 6; i <= 10; ++i )
110 {
111 unsigned N( 0 );
112 N = unsigned( std::pow( 2.0, double( i ) ) );
113 // mesh
114 DenseVector<double> nodes( Utility::uniform_node_vector( left, right, N ) );
115 // construct the ODE_BVP object
116 ODE_BVP<D_complex> ode( &problem, nodes, &BC_left, &BC_right );
117 // makes no sense to monitor the determinant here
118 ode.set_monitor_det( false );
119 // run through the mesh & set an initial guess
120 for ( unsigned i = 0; i < N; ++i )
121 {
122 double x = ode.solution().coord( i );
123 ode.solution()( i, f ) = x * ( 1 - x );
124 ode.solution()( i, fd ) = 1.0 - 2 * x;
125 ode.solution()( i, lambda ) = eigenvalue_guess;
126 }
127 // solve for the eigenvalue/eigenfunction
128 ode.solve2();
129 // an error measure
130 double abs_error( std::abs( ode.solution()( 1, lambda ) - M_PI * M_PI ) );
131 // output for fun
132 cout << " " << N << " : " << abs_error << "\n";
133 if ( abs_error < 1.e-4 )
134 failed = false;
135 }
136
137 if ( failed )
138 {
139 cout << "\033[1;31;48m * FAILED \033[0m\n";
140 return 1;
141 }
142
143 cout << "\033[1;32;48m * PASSED \033[0m\n";
144 return 0;
145}
@ fd
Definition: BVPBerman.cpp:15
@ f
Definition: BVPBerman.cpp:15
A shorter bundled include file for ODE_BVP and PDE_IBVP codes.
@ lambda
int main()
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: BVPHarmonic.cpp:26
void residual_fn(const DenseVector< D_complex > &z, DenseVector< D_complex > &g) const
Define the harmonic eqn.
void matrix0(const DenseVector< D_complex > &z, DenseMatrix< D_complex > &m) const
matrix to multiply the BVP coordinate
Harmonic_equation()
The eqn is a 3rd order complex nonlinear ODE because the eigenvalue is an unknown.
void residual_fn(const DenseVector< D_complex > &z, DenseVector< D_complex > &B) const
A blank virtual residual function method.
void residual_fn(const DenseVector< D_complex > &z, DenseVector< D_complex > &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
void set_monitor_det(bool flag)
Set the flag that determines if the determinant will be monitored The default is to monitor.
Definition: ODE_BVP.h:174
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...
std::complex< double > D_complex
A complex double precision number using std::complex.
Definition: Types.h:98
void fill_identity(CppNoddy::Sequential_Matrix_base< _Type > &A)
Fill diagonal with unit values.
Definition: Utils_Fill.h:22

© 2012

R.E. Hewitt