-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlorenz_ode.cpp
415 lines (386 loc) · 9.18 KB
/
lorenz_ode.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
# include <cstdlib>
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cmath>
# include <ctime>
using namespace std;
int main ( );
double *lorenz_rhs ( double t, int m, double x[] );
double *r8vec_linspace_new ( int n, double a, double b );
double *rk4vec ( double t0, int n, double u0[], double dt,
double *f ( double t, int n, double u[] ) );
void timestamp ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for LORENZ_ODE.
//
// Discussion:
//
// Thanks to Ben Whitney for pointing out an error in specifying a loop,
// 24 May 2016.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 24 May 2016
//
// Author:
//
// John Burkardt
//
{
string command_filename = "lorenz_ode_commands.txt";
ofstream command_unit;
string data_filename = "lorenz_ode_data.txt";
ofstream data_unit;
double dt;
int i;
int j;
int m = 3;
int n = 200000;
double *t;
double t_final;
double *x;
double *xnew;
timestamp ( );
cout << "\n";
cout << "LORENZ_ODE\n";
cout << " C++ version\n";
cout << " Compute solutions of the Lorenz system.\n";
cout << " Write data to a file for use by gnuplot.\n";
//
// Data
//
t_final = 40.0;
dt = t_final / ( double ) ( n );
//
// Store the initial conditions in entry 0.
//
t = r8vec_linspace_new ( n + 1, 0.0, t_final );
x = new double[m*(n+1)];
x[0+0*m] = 8.0;
x[0+1*m] = 1.0;
x[0+2*m] = 1.0;
//
// Compute the approximate solution at equally spaced times.
//
for ( j = 0; j < n; j++ )
{
xnew = rk4vec ( t[j], m, x+j*m, dt, lorenz_rhs );
for ( i = 0; i < m; i++ )
{
x[i+(j+1)*m] = xnew[i];
}
delete [] xnew;
}
//
// Create the plot data file.
//
data_unit.open ( data_filename.c_str ( ) );
for ( j = 0; j <= n; j = j + 50 )
{
data_unit << " " << setw(14) << t[j]
<< " " << setw(14) << x[0+j*m]
<< " " << setw(14) << x[1+j*m]
<< " " << setw(14) << x[2+j*m] << "\n";
}
data_unit.close ( );
cout << " Created data file \"" << data_filename << "\".\n";
/*
Create the plot command file.
*/
command_unit.open ( command_filename.c_str ( ) );
command_unit << "# " << command_filename << "\n";
command_unit << "#\n";
command_unit << "# Usage:\n";
command_unit << "# gnuplot < " << command_filename << "\n";
command_unit << "#\n";
command_unit << "set term png\n";
command_unit << "set output 'xyz_time.png'\n";
command_unit << "set xlabel '<--- T --->'\n";
command_unit << "set ylabel '<--- X(T), Y(T), Z(T) --->'\n";
command_unit << "set title 'X, Y and Z versus Time'\n";
command_unit << "set grid\n";
command_unit << "set style data lines\n";
command_unit << "plot '" << data_filename
<< "' using 1:2 lw 3 linecolor rgb 'blue',";
command_unit << "'' using 1:3 lw 3 linecolor rgb 'red',";
command_unit << "'' using 1:4 lw 3 linecolor rgb 'green'\n";
command_unit << "set output 'xyz_3d.png'\n";
command_unit << "set xlabel '<--- X(T) --->'\n";
command_unit << "set ylabel '<--- Y(T) --->'\n";
command_unit << "set zlabel '<--- Z(T) --->'\n";
command_unit << "set title '(X(T),Y(T),Z(T)) trajectory'\n";
command_unit << "set grid\n";
command_unit << "set style data lines\n";
command_unit << "splot '" << data_filename
<< "' using 2:3:4 lw 1 linecolor rgb 'blue'\n";
command_unit << "quit\n";
command_unit.close ( );
cout << " Created command file '" << command_filename << "'\n";
//
// Terminate.
//
cout << "\n";
cout << "LORENZ_ODE:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
double *lorenz_rhs ( double t, int m, double x[] )
//****************************************************************************80
//
// Purpose:
//
// LORENZ_RHS evaluates the right hand side of the Lorenz ODE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 October 2013
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double T, the value of the independent variable.
//
// Input, int M, the spatial dimension.
//
// Input, double X[M], the values of the dependent variables
// at time T.
//
// Output, double DXDT[M], the values of the derivatives
// of the dependent variables at time T.
//
{
double beta = 8.0 / 3.0;
double *dxdt;
double rho = 28.0;
double sigma = 10.0;
dxdt = new double[m];
dxdt[0] = sigma * ( x[1] - x[0] );
dxdt[1] = x[0] * ( rho - x[2] ) - x[1];
dxdt[2] = x[0] * x[1] - beta * x[2];
return dxdt;
}
//****************************************************************************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.
//
// 4 points evenly spaced between 0 and 12 will yield 0, 4, 8, 12.
//
// In other words, the interval is divided into N-1 even subintervals,
// and the endpoints of intervals are used as the points.
//
// 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
double *rk4vec ( double t0, int m, double u0[], double dt,
double *f ( double t, int m, double u[] ) )
//****************************************************************************80
//
// Purpose:
//
// RK4VEC takes one Runge-Kutta step for a vector ODE.
//
// Discussion:
//
// It is assumed that an initial value problem, of the form
//
// du/dt = f ( t, u )
// u(t0) = u0
//
// is being solved.
//
// If the user can supply current values of t, u, a stepsize dt, and a
// function to evaluate the derivative, this function can compute the
// fourth-order Runge Kutta estimate to the solution at time t+dt.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2013
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double T0, the current time.
//
// Input, int M, the spatial dimension.
//
// Input, double U0[M], the solution estimate at the current time.
//
// Input, double DT, the time step.
//
// Input, double *F ( double T, int M, double U[] ), a function which evaluates
// the derivative, or right hand side of the problem.
//
// Output, double RK4VEC[M], the fourth-order Runge-Kutta solution estimate
// at time T0+DT.
//
{
double *f0;
double *f1;
double *f2;
double *f3;
int i;
double t1;
double t2;
double t3;
double *u;
double *u1;
double *u2;
double *u3;
//
// Get four sample values of the derivative.
//
f0 = f ( t0, m, u0 );
t1 = t0 + dt / 2.0;
u1 = new double[m];
for ( i = 0; i < m; i++ )
{
u1[i] = u0[i] + dt * f0[i] / 2.0;
}
f1 = f ( t1, m, u1 );
t2 = t0 + dt / 2.0;
u2 = new double[m];
for ( i = 0; i < m; i++ )
{
u2[i] = u0[i] + dt * f1[i] / 2.0;
}
f2 = f ( t2, m, u2 );
t3 = t0 + dt;
u3 = new double[m];
for ( i = 0; i < m; i++ )
{
u3[i] = u0[i] + dt * f2[i];
}
f3 = f ( t3, m, u3 );
//
// Combine them to estimate the solution.
//
u = new double[m];
for ( i = 0; i < m; i++ )
{
u[i] = u0[i] + dt * ( f0[i] + 2.0 * f1[i] + 2.0 * f2[i] + f3[i] ) / 6.0;
}
//
// Free memory.
//
delete [] f0;
delete [] f1;
delete [] f2;
delete [] f3;
delete [] u1;
delete [] u2;
delete [] u3;
return u;
}
//****************************************************************************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
}