CppNoddy  0.92
Loading...
Searching...
No Matches
Functions
1DNodeMesh.cpp File Reference

A simple check of the OneD_Node_Mesh container that stores nodal data over a given mesh. More...

#include <OneD_Node_Mesh.h>
#include <Utility.h>

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

A simple check of the OneD_Node_Mesh container that stores nodal data over a given mesh.

This checks that mesh data is interpolated and integrated correctly. We write $ \cos(x) $ to a uniform mesh, then check the integration routines converge appropriately. We then re-interpolate the data onto a non-uniform mesh and check the integral again.

Definition in file 1DNodeMesh.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 18 of file 1DNodeMesh.cpp.

19{
20 cout.precision( 10 );
21 cout << "\n";
22 cout << "=== OneD_Node_Mesh: wrapper =========================\n";
23 cout << "\n";
24 cout << " The test checks the convergence rate for an integral \n";
25 cout << " over an increasing number of mesh points. \n\n";
26
27 double error_trapezium = 1.0;
28 double error_Simpson = 1.0;
29 double error_remesh = 1.0;
30 double error_diffroot = 0.0;
31
32 {
33 double old_integral = 0.0;
34 double integral = 0.0;
35 std::size_t n = 2;
36 const unsigned jmax = 8;
37 for ( unsigned j = 1; j <= jmax ; ++j )
38 {
39 // A mesh of n points
40 n *= 2;
41 n += 1;
42 // make a uniform mesh
44
45 // Set the variable values to be defined by a Cosine.
46 for ( std::size_t i = 0; i < n; ++i )
47 {
48 // mesh stores cos(x)
49 Q( i, 0 ) = cos( Q.coord( i ) );
50 }
51
52 // cos integrates to sin(x) & limits are 0 to 1
53 integral = std::abs( Q.integral2( 0 ) - sin( 1.0 ) );
54 if ( j > 1 )
55 {
56 // Check the lin integral method.
57 cout << " n = " << n << " |Integral Error| = " << integral
58 << " Ratio = " << old_integral / integral << "\n";
59 }
60 old_integral = integral;
61 }
62 error_trapezium = integral;
63 }
64
65 cout << "\n Checking Simpson integration routine \n\n";
66
67 {
68 double old_integral = 0.0;
69 double integral = 0.0;
70 std::size_t n = 2;
71 const unsigned jmax = 8;
72 for ( unsigned j = 1; j <= jmax ; ++j )
73 {
74 // A mesh of n points
75 n *= 2;
76 n += 1;
77 // make a uniform mesh
79
80 // Set the nodal values to be defined by a Cosine.
81 for ( std::size_t i = 0; i < n; ++i )
82 {
83 // mesh contains cos(x)
84 Q( i, 0 ) = cos( Q.coord( i ) );
85 }
86
87 // mesh integrates to give sin(x) with limits 0 to 1
88 integral = std::abs( Q.integral4( 0 ) - sin( 1.0 ) );
89 if ( j > 1 )
90 {
91 // Check the lin integral method.
92 cout << " n = " << n << " |Integral Error| = " << integral
93 << " Ratio = " << old_integral / integral << "\n";
94 }
95 old_integral = integral;
96 }
97 error_Simpson = integral;
98 }
99
100 std::cout << "\n Checking the mesh linear interpolation. \n";
101 {
102 const std::size_t n = 501;
103
104 // make a nonuniform mesh distribution
105 OneD_Node_Mesh<D_complex> Q( Utility::power_node_vector( 0.0, 1.0, n, 2.0 ), 1 );
106
107 for ( std::size_t i = 0; i < n; ++i )
108 {
109 // set mesh to contain cos(x) but this time with non-uniform x nodes
110 Q( i, 0 ) = std::cos( Q.coord( i ) );
111 }
112
113 // integrate over the nonuniform mesh
114 const double Inonuniform = std::abs( Q.integral2( 0 ) );
115
116 // make a uniform mesh of nodes
117 DenseVector<double> Xuniform = Utility::uniform_node_vector( 0.0, 1.0, n );
118
119 // remesh the nonlinear distribution to a uniform mesh
120 Q.remesh1( Xuniform );
121
122 const double Iuniform = std::abs( Q.integral2( 0 ) );
123 error_remesh = std::abs( Inonuniform - Iuniform );
124
125 std::cout << " |error| = " << error_remesh << "\n";
126 }
127
128
129 std::cout << "\n Checking mesh root interpolation. \n";
130 {
131 const std::size_t n = 401;
132
134
135 for ( std::size_t i = 0; i < n; ++i )
136 {
137 // put sin(x) into var 0
138 F( i, 0 ) = std::sin( F.coord( i ) );
139 }
140
141
142 // find the roots of sin(x) with x in 0 to 10
143 {
144 DenseVector<double> roots( F.find_roots1( 0 ) );
145 for ( std::size_t i = 0; i < roots.size(); ++i )
146 {
147 // roots should be ordered, so just check that they are at n * pi
148 error_diffroot = std::max( std::abs( roots[ i ] - ( i + 1 ) * M_PI ) , error_diffroot );
149 }
150 }
151
152 }
153
154 if ( ( error_trapezium > 1.e-6 ) || ( error_Simpson > 1.e-12 )
155 || ( error_remesh > 1.e-6 ) || ( error_diffroot > 1.e-6 ) )
156 {
157 cout << "\033[1;31;48m * FAILED \033[0m\n";
158 return 1;
159 }
160 else
161 {
162 cout << "\033[1;32;48m * PASSED \033[0m\n";
163 return 0;
164 }
165
166}
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A one dimensional mesh utility object.
DenseVector< double > power_node_vector(const double &lower, const double &upper, const std::size_t &N, const double &power)
Return a DENSE vector with the nodal points of a non-uniform mesh distributed between the upper/lower...
Definition: Utility.cpp:123
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

References CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::coord(), CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::find_roots1(), CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::integral2(), CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::integral4(), CppNoddy::Utility::power_node_vector(), CppNoddy::OneD_Node_Mesh< _Type, _Xtype >::remesh1(), CppNoddy::DenseVector< _Type >::size(), and CppNoddy::Utility::uniform_node_vector().

© 2012

R.E. Hewitt