Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 386 lines (314 sloc) 11.076 kB
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
1 /*
2 * based on freeglut-2.4.0:lorenz.c Lorenz Strange Attractor
3
4 * Written by John F. Fay in honor of the "freeglut" 2.0.0 release in July 2003
5 *
6 * What it does:
7 * This program starts with two particles right next to each other. The particles
8 * move through a three-dimensional phase space governed by the following equations:
9 * dx/dt = sigma * ( y - x )
10 * dy/dt = r * x - y + x * z
11 * dz/dt = x * y + b * z
12 * These are the Lorenz equations and define the "Lorenz Attractor." Any two particles
13 * arbitrarily close together will move apart as time increases, but their tracks are
14 * confined within a region of the space.
15 *
16 * Commands:
17 * Arrow keys: Rotate the view
18 * PgUp, PgDn: Zoom in and out
19 * Mouse click: Center on the nearest point on a particle trajectory
20 *
21 * 'r'/'R': Reset the simulation
22 * (removed) 'm'/'M': Modify the Lorenz parameters (in the text window)
23 * 's'/'S': Stop (the advancement in time)
24 * 'g'/'G': Go
25 * <spacebar>: Single-step
26 * <Escape>: Quit
27 */
28 /*
29 * Chua scheme written by Sergei Trofimovich
30 * C1*dVc1/dt = (Vc2 - Vc1)/R1 - In(Vc1)
31 * C2*dVc2/dt = (Vc1 - Vc2)/R2 + Il
32 * L*dIl/td = -Vc2
33 * In(Vc1) = Gb*Vc1 + 1/2 * (Ga - Gb)(|Vc1 + E| - |Vc1 - E|)
34 * ---------------------------------------------------------
35 * Vc1 -> x
36 * Vc2 -> y
37 * Il -> z
38 *
39 * dx/dt = 1/C1 * ((y - x)/R1 - (Gb*x + 1/2 * (Ga - Gb)(fabs (x + E) - fabs(x - E))))
40 * dy/dt = 1/C2 * ((x - y)/R2 + z)
41 * dz/dt = -1/L * y
42 */
43
44 /* Include Files */
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <math.h>
49 #include <GL/freeglut.h>
50 #ifdef WIN32
51 /* DUMP MEMORY LEAKS */
52 #include <crtdbg.h>
53 #endif
54
55
56 /************************************** Defined Constants ***************************************/
57 /* Number of points to draw in the curves */
58 #define NUM_POINTS 512
59
60 /* Angle to rotate when the user presses an arrow key */
61 #define ROTATION_ANGLE 5.0
62
63 /* Amount to scale bu when the user presses PgUp or PgDn */
64 #define SCALE_FACTOR 0.8
65
66
67 /*************************************** Global Variables ***************************************/
68 /* Chua consts */
69 /*
70 double Gb = -0.714e-3;
71 double Ga = -1.143e-3;
72 double E = 1.0;
73 double L = 0.0625;
74 double C2 = 1.0;
75 double C1 = 0.10204;
76 */
77
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
78 static const int TIMER_DELTA = 3;
79 static double Gb = -0.714;
80 static double Ga = -1.143;
81 static double E = 1.0;
82 static double L = 0.0625;
83 static double C2 = 1.0;
84 static double C1 = 0.10204;
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
85
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
86 static double R1 = 1.0;
87 static double R2 = 1.0;
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
88
89
90
91 /* Chua variables */
92
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
93 static double x_r0 = 0.0; // Vc1
94 static double y_r0 = 0.1; // Vc2
95 static double z_r0 = 0.0; // Il
96 static double x_g0 = 0.1;
97 static double y_g0 = 0.0;
98 static double z_g0 = 0.0;
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
99
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
100 static double time_step = 0.03 ; /* Time step in the simulation */
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
101
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
102 static double red_position[NUM_POINTS][3] ; /* Path of the red point */
103 static double grn_position[NUM_POINTS][3] ; /* Path of the green point */
104 static int array_index ; /* Position in *_position arrays of most recent point */
105 static double distance = 0.0 ; /* Distance between the two points */
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
106
107 /* GLUT variables */
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
108 static double xcen = 0.0, ycen = 0.0, zcen = 0.0 ; /* Coordinates of the point looked at */
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
109
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
110 static int animate = 1 ; /* 0 - stop, 1 = go, 2 = single-step */
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
111
112 /******************************************* Functions ******************************************/
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
113 static double In (double x)
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
114 {
115 return Gb * x + 0.5 * (Ga - Gb) * (fabs (x + E) - fabs (x - E));
116 }
117 /* The Lorenz Attractor */
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
118 static void calc_deriv ( double position[3], double deriv[3] )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
119 {
120 /* Calculate the Lorenz attractor derivatives */
121 deriv[0] = ((position [1]) - (position [0]) / R1 - In (position [0])) / C1;
122 deriv[1] = ((position [0]) - (position [1]) / R2 + position [2]) / C2;
123 deriv[2] = - position [1] / L;
124 }
125
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
126 static void advance_in_time ( double time_step, double position[3], double new_position[3] )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
127 {
128 // Move a point along the Lorenz attractor
129 double deriv0[3], deriv1[3], deriv2[3], deriv3[3] ;
130 int i ;
131 memcpy ( new_position, position, 3 * sizeof(double) ) ; // Save the present values
132 // First pass in a Fourth-Order Runge-Kutta integration method
133 calc_deriv ( position, deriv0 ) ;
134 for ( i = 0; i < 3; i++ )
135 new_position[i] = position[i] + 0.5 * time_step * deriv0[i] ;
136
137 // Second pass
138 calc_deriv ( new_position, deriv1 ) ;
139 for ( i = 0; i < 3; i++ )
140 new_position[i] = position[i] + 0.5 * time_step * deriv1[i] ;
141
142 // Third pass
143 calc_deriv ( position, deriv2 ) ;
144 for ( i = 0; i < 3; i++ )
145 new_position[i] = position[i] + time_step * deriv2[i] ;
146
147 // Second pass
148 calc_deriv ( new_position, deriv3 ) ;
149 for ( i = 0; i < 3; i++ )
150 new_position[i] = position[i] + 0.1666666666666666667 * time_step *
151 ( deriv0[i] + 2.0 * ( deriv1[i] + deriv2[i] ) + deriv3[i] ) ;
152 }
153
154 /* GLUT callbacks */
155
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
156 static void key_cb ( unsigned char key, int x, int y )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
157 {
158 int i ;
159 //char inputline [ INPUT_LINE_LENGTH ] ;
160
161 switch ( key )
162 {
163 case 'r' : case 'R' : /* Reset the simulation */
164 /* Reset the Lorenz parameters */
165 /* Set an initial position */
166 red_position[0][0] = x_r0;
167 red_position[0][1] = y_r0;
168 red_position[0][2] = z_r0;
169 grn_position[0][0] = x_g0;
170 grn_position[0][1] = y_g0;
171 grn_position[0][2] = z_g0;
172 array_index = 0 ;
173 /* Initialize the arrays */
174 for ( i = 1; i < NUM_POINTS; i++ )
175 {
176 memcpy ( red_position[i], red_position[0], 3 * sizeof(double) ) ;
177 memcpy ( grn_position[i], grn_position[0], 3 * sizeof(double) ) ;
178 }
179
180 break ;
181
182 case 's' : case 'S' : /* Stop the animation */
183 animate = 0 ;
184 break ;
185
186 case 'g' : case 'G' : /* Start the animation */
187 animate = 1 ;
188 break ;
189
190 case ' ' : /* Spacebar: Single step */
191 animate = 2 ;
192 break ;
193
194 case 27 : /* Escape key */
195 glutLeaveMainLoop () ;
196 break ;
197 }
198 }
199
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
200 static void special_cb ( int key, int x, int y )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
201 {
202 switch ( key )
203 {
204 case GLUT_KEY_UP : /* Rotate up a little */
205 glRotated ( ROTATION_ANGLE, 0.0, 1.0, 0.0 ) ;
206 break ;
207
208 case GLUT_KEY_DOWN : /* Rotate down a little */
209 glRotated ( -ROTATION_ANGLE, 0.0, 1.0, 0.0 ) ;
210 break ;
211
212 case GLUT_KEY_LEFT : /* Rotate left a little */
213 glRotated ( ROTATION_ANGLE, 0.0, 0.0, 1.0 ) ;
214 break ;
215
216 case GLUT_KEY_RIGHT : /* Rotate right a little */
217 glRotated ( -ROTATION_ANGLE, 0.0, 0.0, 1.0 ) ;
218 break ;
219
220 case GLUT_KEY_PAGE_UP : /* Zoom in a little */
221 glScaled ( 1.0 / SCALE_FACTOR, 1.0 / SCALE_FACTOR, 1.0 / SCALE_FACTOR ) ;
222 break ;
223
224 case GLUT_KEY_PAGE_DOWN : /* Zoom out a little */
225 glScaled ( SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR ) ;
226 break ;
227 }
228
229 glutPostRedisplay () ;
230 }
231
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
232 static void mouse_cb ( int button, int updown, int x, int y )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
233 {
234 if ( updown == GLUT_DOWN )
235 {
236 double dist = 1.0e20 ; /* A very large number */
237 dist = 0.0 ; /* so we don't get "unused variable" compiler warning */
238 /* The idea here is that we go into "pick" mode and pick the nearest point
239 to the mouse click position. Unfortunately I don't have the time to implement
240 it at the moment. */
241 }
242 }
243
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
244 static void draw_curve ( int index, double position [ NUM_POINTS ][3] )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
245 {
246 int i = index ;
247
248 glBegin ( GL_LINE_STRIP ) ;
249 do
250 {
251 i = ( i == NUM_POINTS-1 ) ? 0 : i + 1 ;
252 glVertex3dv ( position[i] ) ;
253 }
254 while ( i != index ) ;
255
256 glEnd () ;
257 }
258
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
259 static void display_cb ( void )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
260 {
261 char string [ 80 ] ;
262
263 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
264
265 glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
266 /* Draw some axes */
267 glBegin ( GL_LINES ) ;
268 // R G B
269 glColor3d ( 1.0, 0.0, 0.0 ) ;
270 glVertex3d ( 0.0, 0.0, 0.0 ) ;
271 glVertex3d ( 2.0, 0.0, 0.0 ) ;
272
273 glColor3d ( 0.0, 1.0, 0.0 ) ;
274 glVertex3d ( 0.0, 0.0, 0.0 ) ;
275 glVertex3d ( 0.0, 1.0, 0.0 ) ;
276
277 glColor3d ( 0.0, 0.0, 1.0 ) ;
278 glVertex3d ( 0.0, 0.0, 0.0 ) ;
279 glVertex3d ( 0.0, 0.0, 1.0 ) ;
280 glEnd () ;
281
282 glColor3d ( 1.0, 0.0, 0.0 ) ; /* Red */
283 draw_curve ( array_index, red_position ) ;
284
285 glColor3d ( 0.0, 1.0, 0.0 ) ; /* Green */
286 draw_curve ( array_index, grn_position ) ;
287
288 /* Print the distance between the two points */
289 glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
290 sprintf ( string, "Distance: %10.6f", distance ) ;
291 glRasterPos2i ( 10, 10 ) ;
292 glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)string ) ;
293
294 glutSwapBuffers();
295 }
296
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
297 static void reshape_cb ( int width, int height )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
298 {
299 float ar;
300 glViewport ( 0, 0, width, height ) ;
301 glMatrixMode ( GL_PROJECTION ) ;
302 glLoadIdentity () ;
303 ar = (float) width / (float) height ;
304 glFrustum ( -ar, ar, -1.0, 1.0, 10.0, 100.0 ) ;
305 glMatrixMode ( GL_MODELVIEW ) ;
306 glLoadIdentity () ;
307 xcen = 0.0 ;
308 ycen = 0.0 ;
309 zcen = 0.0 ;
310 glTranslated ( xcen, ycen, zcen - 50.0 ) ;
311 }
312
313
7692d93 @trofi chua.c: added 'static' where 'clang -Weverything' suggested
authored
314 static void timer_cb ( int value )
ace4e66 @trofi Lorenz' strange attractor. Initial.version.
authored
315 {
316 /* Function called at regular intervals to update the positions of the points */
317 double deltax, deltay, deltaz ;
318 int new_index = array_index + 1 ;
319
320 /* Set the next timed callback */
321 glutTimerFunc ( TIMER_DELTA, timer_cb, 0 ) ;
322
323 if ( animate > 0 )
324 {
325 if ( new_index == NUM_POINTS ) new_index = 0 ;
326 advance_in_time ( time_step, red_position[array_index], red_position[new_index] ) ;
327 advance_in_time ( time_step, grn_position[array_index], grn_position[new_index] ) ;
328 array_index = new_index ;
329
330 deltax = red_position[array_index][0] - grn_position[array_index][0] ;
331 deltay = red_position[array_index][1] - grn_position[array_index][1] ;
332 deltaz = red_position[array_index][2] - grn_position[array_index][2] ;
333 distance = sqrt ( deltax * deltax + deltay * deltay + deltaz * deltaz ) ;
334
335 if ( animate == 2 ) animate = 0 ;
336 }
337
338 glutPostRedisplay () ;
339 }
340
341
342
343 /* The Main Program */
344
345 int main ( int argc, char *argv[] )
346 {
347 int pargc = argc ;
348
349 /* Initialize the random number generator */
350 srand ( 1023 ) ;
351
352 /* Set up the OpenGL parameters */
353 glEnable ( GL_DEPTH_TEST ) ;
354 glClearColor ( 0.0, 0.0, 0.0, 0.0 ) ;
355 glClearDepth ( 1.0 ) ;
356
357 /* Initialize GLUT */
358 glutInitWindowSize ( 600, 600 ) ;
359 glutInit ( &pargc, argv ) ;
360 glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
361
362 /* Create the window */
363 glutCreateWindow ( "Chua Stohastic Generator" ) ;
364 glutKeyboardFunc ( key_cb ) ;
365 glutMouseFunc ( mouse_cb ) ;
366 glutSpecialFunc ( special_cb ) ;
367 glutDisplayFunc ( display_cb ) ;
368 glutReshapeFunc ( reshape_cb ) ;
369 glutTimerFunc ( 30, timer_cb, 0 ) ;
370
371 /* Initialize the attractor: The easiest way is to call the keyboard callback with an
372 * argument of 'r' for Reset.
373 */
374 key_cb ( 'r', 0, 0 ) ;
375
376 /* Enter the GLUT main loop */
377 glutMainLoop () ;
378
379 #ifdef WIN32
380 /* DUMP MEMORY LEAK INFORMATION */
381 _CrtDumpMemoryLeaks () ;
382 #endif
383
384 return 0 ;
385 }
Something went wrong with that request. Please try again.