-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstring_simulation.cpp
389 lines (360 loc) · 8.92 KB
/
string_simulation.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
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <ctime>
# include <fstream>
using namespace std;
int main ( );
double f ( double x );
double g ( double x );
void timestamp ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for STRING_SIMULATION.
//
// Discussion:
//
// This program solves the 1D wave equation of the form:
//
// Utt = c^2 Uxx
//
// over the spatial interval [X1,X2] and time interval [T1,T2],
// with initial conditions:
//
// U(T1,X) = U_T1(X),
// Ut(T1,X) = UT_T1(X),
//
// and boundary conditions of Dirichlet type:
//
// U(T,X1) = U_X1(T),
// U(T,X2) = U_X2(T).
//
// The value C represents the propagation speed of waves.
//
// The program uses the finite difference method, and marches
// forward in time, solving for all the values of U at the next
// time step by using the values known at the previous two time steps.
//
// Central differences may be used to approximate both the time
// and space derivatives in the original differential equation.
//
// Thus, assuming we have available the approximated values of U
// at the current and previous times, we may write a discretized
// version of the wave equation as follows:
//
// Uxx(T,X) = ( U(T, X+dX) - 2 U(T,X) + U(T, X-dX) ) / dX^2
// Utt(T,X) = ( U(T+dt,X ) - 2 U(T,X) + U(T-dt,X ) ) / dT^2
//
// If we multiply the first term by C^2 and solve for the single
// unknown value U(T+dt,X), we have:
//
// U(T+dT,X) = ( C^2 * dT^2 / dX^2 ) * U(T, X+dX)
// + 2 * ( 1 - C^2 * dT^2 / dX^2 ) * U(T, X )
// + ( C^2 * dT^2 / dX^2 ) * U(T, X-dX)
// - U(T-dT,X )
//
// (Equation to advance from time T to time T+dT, except for FIRST step)
//
// However, on the very first step, we only have the values of U
// for the initial time, but not for the previous time step.
// In that case, we use the initial condition information for dUdT
// which can be approximated by a central difference that involves
// U(T+dT,X) and U(T-dT,X):
//
// dU/dT(T,X) = ( U(T+dT,X) - U(T-dT,X) ) / ( 2 * dT )
//
// and so we can estimate U(T-dT,X) as
//
// U(T-dT,X) = U(T+dT,X) - 2 * dT * dU/dT(T,X)
//
// If we replace the "missing" value of U(T-dT,X) by the known values
// on the right hand side, we now have U(T+dT,X) on both sides of the
// equation, so we have to rearrange to get the formula we use
// for just the first time step:
//
// U(T+dT,X) = 1/2 * ( C^2 * dT^2 / dX^2 ) * U(T, X+dX)
// + ( 1 - C^2 * dT^2 / dX^2 ) * U(T, X )
// + 1/2 * ( C^2 * dT^2 / dX^2 ) * U(T, X-dX)
// + dT * dU/dT(T, X )
//
// (Equation to advance from time T to time T+dT for FIRST step.)
//
// It should be clear now that the quantity ALPHA = C * DT / DX will affect
// the stability of the calculation. If it is greater than 1, then
// the middle coefficient 1-C^2 DT^2 / DX^2 is negative, and the
// sum of the magnitudes of the three coefficients becomes unbounded.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 25 December 2012
//
// Author:
//
// John Burkardt
//
// Local Parameters:
//
// Local, double ALPHA, the CFL stability parameter.
//
// Local, double C, the wave speed.
//
// Local, double DT, the time step.
//
// Local, double DX, the spatial step.
//
// Local, int M, the number of time steps.
//
// Local, int N, the number of spatial intervals.
//
// Local, double T1, T2, the initial and final times.
//
// Local, double U[M+1][N+1], the computed solution.
//
// Local, double X1, X2, the left and right spatial endpoints.
//
{
# define m 30
# define n 40
double alpha;
double c = 0.25;
ofstream command_unit;
ofstream data_unit;
double dt;
double dx;
int i;
int j;
double t;
double t1 = 0.0;
double t2 = 3.0;
double u[m+1][n+1];
double x;
double x1 = 0.0;
double x2 = 1.0;
timestamp ( );
cout << "\n";
cout << "STRING_SIMULATION:\n";
cout << " C++ version\n";
cout << " Simulate the behavior of a vibrating string.\n";
dx = ( x2 - x1 ) / ( double ) n;
dt = ( t2 - t1 ) / ( double ) m;
alpha = pow ( c * dt / dx, 2 );
cout << " ALPHA = ( C * dT / dX )^2 = " << alpha << "\n";
//
// Warn the user if ALPHA will cause an unstable computation.
//
if ( 1.0 < alpha )
{
cout << "\n";
cout << " Warning!\n";
cout << " ALPHA is greater than 1.\n";
cout << " The computation is unstable.\n";
}
//
// Time step 0:
// Use the initial condition for U.
//
u[0][0] = 0.0;
for ( j = 1; j < n; j++ )
{
x = j * dx;
u[0][j] = f ( x );
}
u[0][n] = 0.0;
//
// Time step 1:
// Use the initial condition for dUdT.
//
u[1][0] = 0.0;
for ( j = 1; j < n; j++ )
{
x = j * dx;
u[1][j] =
( alpha / 2.0 ) * u[0][j-1]
+ ( 1.0 - alpha ) * u[0][j]
+ ( alpha / 2.0 ) * u[0][j+1]
+ dt * g ( x );
}
u[1][n] = 0.0;
//
// Time steps 2 through M:
//
for ( i = 2; i <= m; i++ )
{
u[i][0] = 0.0;
for ( j = 1; j < n; j++ )
{
u[i][j] =
alpha * u[i-1][j-1]
+ 2.0 * ( 1.0 - alpha ) * u[i-1][j]
+ alpha * u[i-1][j+1]
- u[i-2][j];
}
u[i][n] = 0.0;
}
//
// Write data file.
//
data_unit.open ( "string_data.txt" );
for ( i = 0; i <= m; i++ )
{
t = i * dt;
for ( j = 0; j <= n; j++ )
{
x = j * dx;
data_unit << " " << x
<< " " << t
<< " " << u[i][j] << "\n";
}
data_unit << "\n";
}
data_unit.close ( );
cout << "\n";
cout << " Plot data written to the file \"string_data.txt\".\n";
//
// Write gnuplot command file.
//
command_unit.open ( "string_commands.txt" );
command_unit << "set term png\n";
command_unit << "set output \"string.png\"\n";
command_unit << "set grid\n";
command_unit << "set style data lines\n";
command_unit << "unset key\n";
command_unit << "set xlabel '<---X--->'\n";
command_unit << "set ylabel '<---Time--->'\n";
command_unit << "splot \"string_data.txt\" using 1:2:3 with lines\n";
command_unit << "quit\n";
command_unit.close ( );
cout << " Gnuplot command data written to the file \"string_commands.txt\".\n";
//
// Terminate.
//
cout << "\n";
cout << "STRING_SIMULATION:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
# undef m
# undef n
}
//****************************************************************************80
double f ( double x )
//****************************************************************************80
//
// Purpose:
//
// F supplies the initial condition.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 December 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double X, the location.
//
// Output, double F, the value of the solution at time 0 and location X.
//
{
const double pi = 3.14159265;
double value;
if ( 0.25 <= x && x <= 0.50 )
{
value = ( x - 0.25 ) * ( 0.50 - x );
}
else
{
value = 0.0;
}
return value;
}
//****************************************************************************80
double g ( double x )
//****************************************************************************80
//
// Purpose:
//
// G supplies the initial derivative.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 December 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double X, the location.
//
// Output, double G, the value of the time derivative of the solution
// at time 0 and location X.
//
{
double value;
value = 0.0;
return value;
}
//****************************************************************************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:
//
// 01 December 2012
//
// 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
}