-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfd1d_heat_explicit.cpp
472 lines (435 loc) · 10.3 KB
/
fd1d_heat_explicit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <ctime>
# include <cmath>
using namespace std;
# include "fd1d_heat_explicit.hpp"
//****************************************************************************80
double *fd1d_heat_explicit ( int x_num, double x[], double t, double dt,
double cfl, double *rhs ( int x_num, double x[], double t ),
void bc ( int x_num, double x[], double t, double h[] ), double h[] )
//****************************************************************************80
//
// Purpose:
//
// FD1D_HEAT_EXPLICIT: Finite difference solution of 1D heat equation.
//
// Discussion:
//
// This program takes one time step to solve the 1D heat equation
// with an explicit method.
//
// This program solves
//
// dUdT - k * d2UdX2 = F(X,T)
//
// over the interval [A,B] with boundary conditions
//
// U(A,T) = UA(T),
// U(B,T) = UB(T),
//
// over the time interval [T0,T1] with initial conditions
//
// U(X,T0) = U0(X)
//
// The code uses the finite difference method to approximate the
// second derivative in space, and an explicit forward Euler approximation
// to the first derivative in time.
//
// The finite difference form can be written as
//
// U(X,T+dt) - U(X,T) ( U(X-dx,T) - 2 U(X,T) + U(X+dx,T) )
// ------------------ = F(X,T) + k * ------------------------------------
// dt dx * dx
//
// or, assuming we have solved for all values of U at time T, we have
//
// U(X,T+dt) = U(X,T)
// + cfl * ( U(X-dx,T) - 2 U(X,T) + U(X+dx,T) ) + dt * F(X,T)
//
// Here "cfl" is the Courant-Friedrichs-Loewy coefficient:
//
// cfl = k * dt / dx / dx
//
// In order for accurate results to be computed by this explicit method,
// the CFL coefficient must be less than 0.5.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 26 January 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int X_NUM, the number of points to use in the
// spatial dimension.
//
// Input, double X(X_NUM), the coordinates of the nodes.
//
// Input, double T, the current time.
//
// Input, double DT, the size of the time step.
//
// Input, double CFL, the Courant-Friedrichs-Loewy coefficient,
// computed by FD1D_HEAT_EXPLICIT_CFL.
//
// Input, double H[X_NUM], the solution at the current time.
//
// Input, double *RHS ( int x_num, double x[], double t ), the function
// which evaluates the right hand side.
//
// Input, void BC ( int x_num, double x[], double t, double h[] ),
// the function which evaluates the boundary conditions.
//
// Output, double FD1D_HEAT_EXPLICIT[X_NUM)], the solution at time T+DT.
//
{
double *f;
double *h_new;
int j;
f = rhs ( x_num, x, t );
h_new = new double[x_num];
h_new[0] = 0.0;
for ( j = 1; j < x_num - 1; j++ )
{
h_new[j] = h[j] + dt * f[j]
+ cfl * ( h[j-1]
- 2.0 * h[j]
+ h[j+1] );
}
h_new[x_num-1] = 0.0;
bc ( x_num, x, t + dt, h_new );
delete [] f;
return h_new;
}
//****************************************************************************80
double fd1d_heat_explicit_cfl ( double k, int t_num, double t_min, double t_max,
int x_num, double x_min, double x_max )
//****************************************************************************80
//
// Purpose:
//
// FD1D_HEAT_EXPLICIT_CFL: compute the Courant-Friedrichs-Loewy coefficient.
//
// Discussion:
//
// The equation to be solved has the form:
//
// dUdT - k * d2UdX2 = F(X,T)
//
// over the interval [X_MIN,X_MAX] with boundary conditions
//
// U(X_MIN,T) = U_X_MIN(T),
// U(X_MIN,T) = U_X_MAX(T),
//
// over the time interval [T_MIN,T_MAX] with initial conditions
//
// U(X,T_MIN) = U_T_MIN(X)
//
// The code uses the finite difference method to approximate the
// second derivative in space, and an explicit forward Euler approximation
// to the first derivative in time.
//
// The finite difference form can be written as
//
// U(X,T+dt) - U(X,T) ( U(X-dx,T) - 2 U(X,T) + U(X+dx,T) )
// ------------------ = F(X,T) + k * ------------------------------------
// dt dx * dx
//
// or, assuming we have solved for all values of U at time T, we have
//
// U(X,T+dt) = U(X,T)
// + cfl * ( U(X-dx,T) - 2 U(X,T) + U(X+dx,T) ) + dt * F(X,T)
//
// Here "cfl" is the Courant-Friedrichs-Loewy coefficient:
//
// cfl = k * dt / dx / dx
//
// In order for accurate results to be computed by this explicit method,
// the CFL coefficient must be less than 0.5!
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 26 January 2012
//
// Author:
//
// John Burkardt
//
// Reference:
//
// George Lindfield, John Penny,
// Numerical Methods Using MATLAB,
// Second Edition,
// Prentice Hall, 1999,
// ISBN: 0-13-012641-1,
// LC: QA297.P45.
//
// Parameters:
//
// Input, double K, the heat conductivity coefficient.
//
// Input, int T_NUM, the number of time values, including
// the initial value.
//
// Input, double T_MIN, T_MAX, the minimum and maximum times.
//
// Input, int X_NUM, the number of nodes.
//
// Input, double X_MIN, X_MAX, the minimum and maximum spatial
// coordinates.
//
// Output, double FD1D_HEAT_EXPLICIT_CFL, the Courant-Friedrichs-Loewy coefficient.
//
{
double cfl;
double dx;
double dt;
dx = ( x_max - x_min ) / ( double ) ( x_num - 1 );
dt = ( t_max - t_min ) / ( double ) ( t_num - 1 );
//
// Check the CFL condition, print out its value, and quit if it is too large.
//
cfl = k * dt / dx / dx;
cout << "\n";
cout << " CFL stability criterion value = " << cfl << "\n";
if ( 0.5 <= cfl )
{
cerr << "\n";
cerr << "FD1D_HEAT_EXPLICIT_CFL - Fatal error!\n";
cerr << " CFL condition failed.\n";
cerr << " 0.5 <= K * dT / dX / dX = CFL.\n";
exit ( 1 );
}
return cfl;
}
//****************************************************************************80
void r8mat_write ( string output_filename, int m, int n, double table[] )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_WRITE writes an R8MAT file.
//
// Discussion:
//
// An R8MAT is an array of R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 29 June 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string OUTPUT_FILENAME, the output filename.
//
// Input, int M, the spatial dimension.
//
// Input, int N, the number of points.
//
// Input, double TABLE[M*N], the data.
//
{
int i;
int j;
ofstream output;
//
// Open the file.
//
output.open ( output_filename.c_str ( ) );
if ( !output )
{
cerr << "\n";
cerr << "R8MAT_WRITE - Fatal error!\n";
cerr << " Could not open the output file.\n";
exit ( 1 );
}
//
// Write the data.
//
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m; i++ )
{
output << " " << setw(24) << setprecision(16) << table[i+j*m];
}
output << "\n";
}
//
// Close the file.
//
output.close ( );
return;
}
//****************************************************************************80
double *r8vec_linspace_new ( int n, double a_first, double a_last )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_LINSPACE_NEW creates a vector of linearly spaced values.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 29 March 2011
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of entries in the vector.
//
// Input, double A_FIRST, A_LAST, the first and last entries.
//
// Output, double R8VEC_LINSPACE_NEW[N], a vector of linearly spaced data.
//
{
double *a;
int i;
a = new double[n];
if ( n == 1 )
{
a[0] = ( a_first + a_last ) / 2.0;
}
else
{
for ( i = 0; i < n; i++ )
{
a[i] = ( ( double ) ( n - 1 - i ) * a_first
+ ( double ) ( i ) * a_last )
/ ( double ) ( n - 1 );
}
}
return a;
}
//****************************************************************************80
void r8vec_write ( string output_filename, int n, double x[] )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_WRITE writes an R8VEC file.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 July 2011
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string OUTPUT_FILENAME, the output filename.
//
// Input, int N, the number of points.
//
// Input, double X[N], the data.
//
{
int j;
ofstream output;
//
// Open the file.
//
output.open ( output_filename.c_str ( ) );
if ( !output )
{
cerr << "\n";
cerr << "R8VEC_WRITE - Fatal error!\n";
cerr << " Could not open the output file.\n";
exit ( 1 );
}
//
// Write the data.
//
for ( j = 0; j < n; j++ )
{
output << " " << setw(24) << setprecision(16) << x[j] << "\n";
}
//
// Close the file.
//
output.close ( );
return;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}