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

Solving the unstead diffusion problem: More...

#include <IBVP_bundle.h>

Go to the source code of this file.

Classes

class  CppNoddy::Example::Diffusion_equations
 
class  CppNoddy::Example::Diffusion_left_BC
 Define the boundary conditions. More...
 
class  CppNoddy::Example::Diffusion_right_BC
 

Namespaces

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

Enumerations

enum  { U , Ud }
 

Functions

double CppNoddy::Example::A (10.0)
 
double CppNoddy::Example::sigma (100.0)
 
int main ()
 

Detailed Description

Solving the unstead diffusion problem:

\[ U_t - U U_{yy} = -Ae^{-t} sin(\pi y) + ( (1+y) + Ae^{-t} sin(\pi y) )\pi^2 Ae^{-t} sin(\pi y)/\sigma \]

with boundary conditions $ U(y=0)=1, U(y=1)=2 $; where $ A=10 $. The initial condition is $ U(y,t=0)=1+y+A sin(\pi y) $. The class constructs and solves the IBVP using 2nd order finite-differencing in both space and time over a non-uniform spatial mesh. The output is compared to the exact solution $ U(y,t) = =1+y+A sin(\pi y)e^{-t} $.

Definition in file IBVPDiffusionNonlinear.cpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
Ud 

Definition at line 14 of file IBVPDiffusionNonlinear.cpp.

14{ U, Ud };

Function Documentation

◆ main()

int main ( )

Definition at line 100 of file IBVPDiffusionNonlinear.cpp.

101{
102 cout << "\n";
103 cout << "=== IBVP: A nonlinear diffusion problem ============\n";
104 cout << "\n";
105
106 // define the system
107 Example::Diffusion_equations problem;
108 // define the boundary conditions
109 Example::Diffusion_left_BC BC_left;
110 Example::Diffusion_right_BC BC_right;
111
112 // domain definition
113 double left = 0.0;
114 double right = 1.0;
115 // number of (spatial) nodal points
116 unsigned ny = 400;
117 // time step
118 double dt = 0.005;
119 // number of time steps
120 unsigned max_steps = ( unsigned ) ( 2.0 / dt );
121
122 // construct our IBVP with more nodes near the boundary
123 PDE_IBVP<double> diffusion( &problem, Utility::power_node_vector( left, right, ny, 1.0 ), &BC_left, &BC_right );
124 //
125 for ( unsigned i = 0; i < ny; ++i )
126 {
127 double y( diffusion.solution().coord(i) );
128 diffusion.solution()( i, U ) = (1+y) + Example::A*sin(M_PI*y);
129 diffusion.solution()( i, Ud ) = 1 + Example::A*M_PI*cos(M_PI*y);
130 }
131
132 // set up output details
133 std::string dirname("./DATA");
134 mkdir( dirname.c_str(), S_IRWXU );
135 TrackerFile metric( "./DATA/IBVP_diffusion_metric.dat" );
136 metric.push_ptr( &diffusion.coord(), "time" );
137 metric.push_ptr( &diffusion.solution()( 0, Ud ), "y=0 derivative" );
138 metric.header();
139 TrackerFile profs( "./DATA/IBVP_diffusion_profs.dat" );
140 profs.push_ptr( &diffusion.coord(), "time" );
141 profs.push_ptr( &diffusion.solution(), "solution" );
142 profs.header();
143
144 Timer timer;
145 timer.start();
146 //
147 double max_error( 0.0 );
148 // time step
149 for ( unsigned i = 1; i < max_steps; ++i )
150 {
151 // take a time step
152 try
153 {
154 diffusion.step2( dt );
155 }
156 catch (const std::runtime_error &error )
157 {
158 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
159 return 1;
160 }
161 if ( i % 20 == 0 )
162 {
163 cout << diffusion.coord() << "\n";
164 profs.update();
165 profs.newline();
166 }
167 timer.counter()++;
168 metric.update();
169 //
170 double yy( 0.5 );
171 double Uexact = 1 + yy + Example::A*exp(-diffusion.coord())*sin(M_PI*yy);
172 double error = diffusion.solution().get_interpolated_vars( 0.5 )[ U ] - Uexact;
173 max_error = std::max( std::abs( error ), max_error );
174 //
175 // std::cout << " ****** " << diffusion.t() << " " << max_error << "\n";
176 }
177 timer.stop();
178 timer.print();
179 //
180 const double tol( 1.e-4 );
181 // check the BL transpiration vs the known solution
182 if ( max_error > tol )
183 {
184 cout << "\033[1;31;48m * FAILED \033[0m\n";
185 cout << " Max difference over time steps = " << max_error << "\n";
186 return 1;
187 }
188 else
189 {
190 cout << "\033[1;32;48m * PASSED \033[0m\n";
191 return 0;
192 }
193
194}
A templated object for real/complex vector system of unsteady equations.
Definition: PDE_IBVP.h:37
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
void start()
Start the timer & reset stored time to zero.
Definition: Timer.cpp:12
int & counter()
Increment an internal discrete counter.
Definition: Timer.cpp:44
void print() const
Write a string to cout stating the time taken.
Definition: Timer.cpp:59
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
Definition: Timer.cpp:17
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

References CppNoddy::PDE_IBVP< _Type >::coord(), CppNoddy::Timer::counter(), CppNoddy::TrackerFile::header(), CppNoddy::TrackerFile::newline(), CppNoddy::Utility::power_node_vector(), CppNoddy::Timer::print(), CppNoddy::TrackerFile::push_ptr(), CppNoddy::PDE_IBVP< _Type >::solution(), CppNoddy::Timer::start(), CppNoddy::PDE_IBVP< _Type >::step2(), CppNoddy::Timer::stop(), U, Ud, and CppNoddy::TrackerFile::update().

© 2012

R.E. Hewitt