CppNoddy  0.92
Loading...
Searching...
No Matches
Classes | Namespaces | Enumerations | Functions
IBVPDiffusion.cpp File Reference

Solving the heat diffusion equation. More...

#include <IBVP_bundle.h>

Go to the source code of this file.

Classes

class  CppNoddy::Example::Diff_equation
 
class  CppNoddy::Example::Diff_both_BC
 

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 ()
 

Detailed Description

Solving the heat diffusion equation.

\[ f_t = f_{yy} \]

subject to $ f(0) = 0 $ and $ f(1) = 0 $ with initial condition $ f(y,t=0) = y(1-y) $. The solution is computed over a range of $ t $ and the maximum deviation away from the analytical solution is found. The test fails if this deviation is larger than a set tolerance $ \tau $. The analytical solution is the Fourier series:

\[ f(y,t) = \sum_{n=1,3,...}^M \frac{8}{n^3\pi^3} \sin ( n\pi y) \exp{ -n^2\pi^2 t } \]

The series is truncated at the $ M $-th term, in such a way that the first neglected term is of magnitude $ < \tau /10 $.

Definition in file IBVPDiffusion.cpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
fd 

Definition at line 19 of file IBVPDiffusion.cpp.

19{ f, fd };
@ fd
@ f

Function Documentation

◆ main()

int main ( )

Definition at line 83 of file IBVPDiffusion.cpp.

84{
85 cout << "\n";
86 cout << "=== IBVP: An unsteady diffusion eqn =================\n";
87 cout << "\n";
88
89 // Diffusion equation
90 Example::Diff_equation problem;
91 // boundary conditions
92 Example::Diff_both_BC BC_both;
93
94 // domain definition
95 double left = 0.0;
96 double right = 1.0;
97 // number of (spatial) nodal points
98 unsigned ny = 201;
99 // time step
100 double dt = 0.005;
101 // number of time steps
102 unsigned max_steps = ( unsigned ) ( 3.0 / dt );
103 // test tolerance
104 double tol = 1.e-4;
105
106 // construct our IBVP
107 PDE_IBVP<double> heat( &problem, Utility::uniform_node_vector( left, right, ny ), &BC_both, &BC_both );
108
109 for ( unsigned i = 0; i < ny; ++i )
110 {
111 double y = heat.solution().coord( i );
112 heat.solution()( i, f ) = y * ( 1 - y );
113 heat.solution()( i, fd ) = 1 - 2 * y;
114 }
115
116 // maximum difference between numerical and series solution at centre point
117 double max_diff( 0.0 );
118 // time step
119 for ( unsigned i = 1; i < max_steps; ++i )
120 {
121 // take a time step
122 try
123 {
124 heat.step2( dt );
125 }
126 catch (const std::runtime_error &error )
127 {
128 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
129 return 1;
130 }
131
132 // evaluate analytical Fourier series solution at y = 0.5
133 unsigned mid = ( ny - 1 ) / 2;
134 double y = left + mid * ( right - left ) / ( ny - 1 );
135 double u( 0.0 );
136 int en( -1 );
137 double correction( 0.0 );
138 do
139 {
140 en += 2;
141 correction = 8 / ( std::pow( en * M_PI, 3 ) )
142 * std::exp( -std::pow( en * M_PI, 2 ) * heat.coord() ) * std::sin( en * M_PI * y );
143 u += correction;
144 }
145 while ( std::abs( correction ) > tol / 10. );
146 // examine the difference between the numerical and series solutions
147 max_diff = std::max( max_diff, std::abs( u - heat.solution()( mid, f ) ) );
148 }
149
150 bool failed = true;
151 if ( abs( max_diff ) < tol )
152 {
153 failed = false;
154 }
155
156 if ( failed )
157 {
158 cout << "\033[1;31;48m * FAILED \033[0m\n";
159 return 1;
160 }
161 else
162 {
163 cout << "\033[1;32;48m * PASSED \033[0m\n";
164 return 0;
165 }
166
167}
A templated object for real/complex vector system of unsteady equations.
Definition: PDE_IBVP.h:37
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::PDE_IBVP< _Type >::coord(), f, fd, CppNoddy::PDE_IBVP< _Type >::solution(), CppNoddy::PDE_IBVP< _Type >::step2(), u, and CppNoddy::Utility::uniform_node_vector().

© 2012

R.E. Hewitt