CppNoddy  0.92
Loading...
Searching...
No Matches
Classes | Namespaces | Enumerations | Functions
BVPHarmonic.cpp File Reference
#include <BVP_bundle.h>
#include "../Utils_Fill.h"

Go to the source code of this file.

Classes

class  CppNoddy::Example::Harmonic_equation< _Type, _Xtype >
 Define the harmonic equation by inheriting the Equation base class. More...
 
class  CppNoddy::Example::Harmonic_left_BC< _Type >
 
class  CppNoddy::Example::Harmonic_right_BC< _Type >
 

Namespaces

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

Enumerations

enum  { f , fd }
 

Functions

int main ()
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
fd 

Definition at line 17 of file BVPHarmonic.cpp.

17{f, fd };
@ fd
Definition: BVPHarmonic.cpp:17
@ f
Definition: BVPHarmonic.cpp:17

Function Documentation

◆ main()

int main ( )

Definition at line 77 of file BVPHarmonic.cpp.

78{
79 cout << "\n";
80 cout << "=== BVP: finite-difference solution of Harmonic eqn =\n";
81 cout << "\n";
82
83 Example::Harmonic_equation<double, double> real_problem;
84 Example::Harmonic_left_BC <double> real_BC_left;
85 Example::Harmonic_right_BC<double> real_BC_right;
86 Example::Harmonic_equation<D_complex, D_complex> complex_problem;
87 Example::Harmonic_left_BC <D_complex> complex_BC_left;
88 Example::Harmonic_right_BC<D_complex> complex_BC_right;
89
90 double left = 0.0;
91 double right = 1.0;
92 // number of nodal points
93 unsigned N = 21;
94 // a real mesh -- along real axis
95 DenseVector<double> real_nodes( Utility::uniform_node_vector( left, right, N ) );
96 // a complex mesh -- along imaginary axis
97 D_complex eye( 0.0, 1.0 );
98 DenseVector<D_complex> complex_nodes( real_nodes );
99 complex_nodes *= eye;
100 // Example tolerance
101 const double tol = 1.e-4;
102
103 // a real ODE BVP
104 ODE_BVP<double> real_ode( &real_problem, real_nodes, &real_BC_left, &real_BC_right );
105 // a complex ODE BVP solved on a line in the complex plane
106 ODE_BVP<D_complex, D_complex> complex_ode( &complex_problem, complex_nodes, &complex_BC_left, &complex_BC_right );
107 complex_ode.set_monitor_det( false );
108
109 // our initial guess
110 for ( unsigned i = 0; i < N; ++i )
111 {
112 double y = real_ode.solution().coord( i );
113 // set f(y)
114 real_ode.solution()( i, f ) = y;
115 complex_ode.solution()( i, f ) = y;
116 // set f'(y)
117 real_ode.solution()( i, fd ) = 1;
118 complex_ode.solution()( i, fd ) = 1;
119 }
120
121 // solve the problem using 2nd order finite-difference
122 try
123 {
124 real_ode.solve2();
125 complex_ode.solve2();
126 }
127 catch ( const std::runtime_error &error )
128 {
129 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
130 return 1;
131 }
132
133 // find the maximum deviation from the analytical solutions of sin(x)/sin(1) and sinh(z)/sinh(1)
134 double real_diff = 0;
135 double complex_diff = 0;
136 for ( unsigned i = 0; i < N; ++i )
137 {
138 real_diff = std::max( std::abs( real_ode.solution()( i, f ) - sin( real_ode.solution().coord( i ) ) / sin( 1 ) ), real_diff );
139 complex_diff = std::max( std::abs( complex_ode.solution()( i, f ) - sin( complex_ode.solution().coord( i ) ) / sin( eye ) ), complex_diff );
140 }
141
142 // validation test
143 if ( real_diff > tol || complex_diff > tol )
144 {
145 cout << "\033[1;31;48m * FAILED \033[0m\n";
146 cout << "Real problem error " << real_diff << "\n";
147 cout << "Complex problem error " << complex_diff << "\n";
148 return 1;
149 }
150 else
151 {
152 cout << "\033[1;32;48m * PASSED \033[0m\n";
153 return 0;
154 }
155}
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A templated object for real/complex vector system of first-order ordinary differential equations.
Definition: ODE_BVP.h:37
const D_complex eye(0., 1.0)
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 f, fd, CppNoddy::ODE_BVP< _Type, _Xtype >::set_monitor_det(), CppNoddy::ODE_BVP< _Type, _Xtype >::solution(), CppNoddy::ODE_BVP< _Type, _Xtype >::solve2(), and CppNoddy::Utility::uniform_node_vector().

© 2012

R.E. Hewitt