lorenz.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * 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. * '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. /* Include Files */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #include <GL/freeglut.h>
  35. #ifdef _MSC_VER
  36. /* DUMP MEMORY LEAKS */
  37. #include <crtdbg.h>
  38. #endif
  39. /************************************** Defined Constants ***************************************/
  40. /* Number of points to draw in the curves */
  41. #define NUM_POINTS 512
  42. /* Angle to rotate when the user presses an arrow key */
  43. #define ROTATION_ANGLE 5.0
  44. /* Amount to scale bu when the user presses PgUp or PgDn */
  45. #define SCALE_FACTOR 0.8
  46. /*************************************** Global Variables ***************************************/
  47. /* Lorenz Attractor variables */
  48. double s0 = 10.0, r0 = 28.0, b0 = 8.0/3.0 ; /* Default Lorenz attactor parameters */
  49. double time_step = 0.03 ; /* Time step in the simulation */
  50. double sigma = 10.0, r = 28.0, b = 8.0/3.0 ; /* Lorenz attactor parameters */
  51. double red_position[NUM_POINTS][3] ; /* Path of the red point */
  52. double grn_position[NUM_POINTS][3] ; /* Path of the green point */
  53. int array_index ; /* Position in *_position arrays of most recent point */
  54. double distance = 0.0 ; /* Distance between the two points */
  55. /* GLUT variables */
  56. double yaw = 0.0, pit = 0.0 ; /* Euler angles of the viewing rotation */
  57. double scale = 1.0 ; /* Scale factor */
  58. double xcen = 0.0, ycen = 0.0, zcen = 0.0 ; /* Coordinates of the point looked at */
  59. int animate = 1 ; /* 0 - stop, 1 = go, 2 = single-step */
  60. /******************************************* Functions ******************************************/
  61. /* The Lorenz Attractor */
  62. void calc_deriv ( double position[3], double deriv[3] )
  63. {
  64. /* Calculate the Lorenz attractor derivatives */
  65. deriv[0] = sigma * ( position[1] - position[0] ) ;
  66. deriv[1] = ( r + position[2] ) * position[0] - position[1] ;
  67. deriv[2] = -position[0] * position[1] - b * position[2] ;
  68. }
  69. void advance_in_time ( double time_step, double position[3], double new_position[3] )
  70. {
  71. /* Move a point along the Lorenz attractor */
  72. double deriv0[3], deriv1[3], deriv2[3], deriv3[3] ;
  73. int i ;
  74. memcpy ( new_position, position, 3 * sizeof(double) ) ; /* Save the present values */
  75. /* First pass in a Fourth-Order Runge-Kutta integration method */
  76. calc_deriv ( position, deriv0 ) ;
  77. for ( i = 0; i < 3; i++ )
  78. new_position[i] = position[i] + 0.5 * time_step * deriv0[i] ;
  79. /* Second pass */
  80. calc_deriv ( new_position, deriv1 ) ;
  81. for ( i = 0; i < 3; i++ )
  82. new_position[i] = position[i] + 0.5 * time_step * deriv1[i] ;
  83. /* Third pass */
  84. calc_deriv ( position, deriv2 ) ;
  85. for ( i = 0; i < 3; i++ )
  86. new_position[i] = position[i] + time_step * deriv2[i] ;
  87. /* Second pass */
  88. calc_deriv ( new_position, deriv3 ) ;
  89. for ( i = 0; i < 3; i++ )
  90. new_position[i] = position[i] + 0.1666666666666666667 * time_step *
  91. ( deriv0[i] + 2.0 * ( deriv1[i] + deriv2[i] ) + deriv3[i] ) ;
  92. }
  93. static void
  94. checkedFGets ( char *s, int size, FILE *stream )
  95. {
  96. if ( fgets ( s, size, stream ) == NULL ) {
  97. fprintf ( stderr, "fgets failed\n");
  98. exit ( EXIT_FAILURE );
  99. }
  100. }
  101. /* GLUT callbacks */
  102. #define INPUT_LINE_LENGTH 80
  103. void key_cb ( unsigned char key, int x, int y )
  104. {
  105. int i ;
  106. char inputline [ INPUT_LINE_LENGTH ] ;
  107. switch ( key )
  108. {
  109. case 'r' : case 'R' : /* Reset the simulation */
  110. /* Reset the Lorenz parameters */
  111. sigma = s0 ;
  112. b = b0 ;
  113. r = r0 ;
  114. /* Set an initial position */
  115. red_position[0][0] = (double)rand() / (double)RAND_MAX ;
  116. red_position[0][1] = (double)rand() / (double)RAND_MAX ;
  117. red_position[0][2] = (double)rand() / (double)RAND_MAX ;
  118. grn_position[0][0] = (double)rand() / (double)RAND_MAX ;
  119. grn_position[0][1] = (double)rand() / (double)RAND_MAX ;
  120. grn_position[0][2] = (double)rand() / (double)RAND_MAX ;
  121. array_index = 0 ;
  122. /* Initialize the arrays */
  123. for ( i = 1; i < NUM_POINTS; i++ )
  124. {
  125. memcpy ( red_position[i], red_position[0], 3 * sizeof(double) ) ;
  126. memcpy ( grn_position[i], grn_position[0], 3 * sizeof(double) ) ;
  127. }
  128. break ;
  129. case 'm' : case 'M' : /* Modify the Lorenz parameters */
  130. printf ( "Please enter new value for <sigma> (default %f, currently %f): ", s0, sigma ) ;
  131. checkedFGets ( inputline, sizeof ( inputline ), stdin ) ;
  132. sscanf ( inputline, "%lf", &sigma ) ;
  133. printf ( "Please enter new value for <b> (default %f, currently %f): ", b0, b ) ;
  134. checkedFGets ( inputline, sizeof ( inputline ), stdin ) ;
  135. sscanf ( inputline, "%lf", &b ) ;
  136. printf ( "Please enter new value for <r> (default %f, currently %f): ", r0, r ) ;
  137. checkedFGets ( inputline, sizeof ( inputline ), stdin ) ;
  138. sscanf ( inputline, "%lf", &r ) ;
  139. break ;
  140. case 's' : case 'S' : /* Stop the animation */
  141. animate = 0 ;
  142. break ;
  143. case 'g' : case 'G' : /* Start the animation */
  144. animate = 1 ;
  145. break ;
  146. case ' ' : /* Spacebar: Single step */
  147. animate = 2 ;
  148. break ;
  149. case 27 : /* Escape key */
  150. glutLeaveMainLoop () ;
  151. break ;
  152. }
  153. }
  154. void special_cb ( int key, int x, int y )
  155. {
  156. switch ( key )
  157. {
  158. case GLUT_KEY_UP : /* Rotate up a little */
  159. glRotated ( ROTATION_ANGLE, 0.0, 1.0, 0.0 ) ;
  160. break ;
  161. case GLUT_KEY_DOWN : /* Rotate down a little */
  162. glRotated ( -ROTATION_ANGLE, 0.0, 1.0, 0.0 ) ;
  163. break ;
  164. case GLUT_KEY_LEFT : /* Rotate left a little */
  165. glRotated ( ROTATION_ANGLE, 0.0, 0.0, 1.0 ) ;
  166. break ;
  167. case GLUT_KEY_RIGHT : /* Rotate right a little */
  168. glRotated ( -ROTATION_ANGLE, 0.0, 0.0, 1.0 ) ;
  169. break ;
  170. case GLUT_KEY_PAGE_UP : /* Zoom in a little */
  171. glScaled ( 1.0 / SCALE_FACTOR, 1.0 / SCALE_FACTOR, 1.0 / SCALE_FACTOR ) ;
  172. break ;
  173. case GLUT_KEY_PAGE_DOWN : /* Zoom out a little */
  174. glScaled ( SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR ) ;
  175. break ;
  176. }
  177. glutPostRedisplay () ;
  178. }
  179. void mouse_cb ( int button, int updown, int x, int y )
  180. {
  181. if ( updown == GLUT_DOWN )
  182. {
  183. /*double dist = 1.0e20 ; A very large number */
  184. /* The idea here is that we go into "pick" mode and pick the nearest point
  185. to the mouse click position. Unfortunately I don't have the time to implement
  186. it at the moment. */
  187. }
  188. }
  189. void draw_curve ( int index, double position [ NUM_POINTS ][3] )
  190. {
  191. int i = index ;
  192. glBegin ( GL_LINE_STRIP ) ;
  193. do
  194. {
  195. i = ( i == NUM_POINTS-1 ) ? 0 : i + 1 ;
  196. glVertex3dv ( position[i] ) ;
  197. }
  198. while ( i != index ) ;
  199. glEnd () ;
  200. }
  201. void bitmapPrintf (const char *fmt, ...)
  202. {
  203. static char buf[256];
  204. va_list args;
  205. va_start(args, fmt);
  206. #if defined(WIN32) && !defined(__CYGWIN__)
  207. (void) _vsnprintf (buf, sizeof(buf), fmt, args);
  208. #else
  209. (void) vsnprintf (buf, sizeof(buf), fmt, args);
  210. #endif
  211. va_end(args);
  212. glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
  213. }
  214. void display_cb ( void )
  215. {
  216. glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
  217. glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
  218. /* Draw some axes */
  219. glBegin ( GL_LINES ) ;
  220. glVertex3d ( 0.0, 0.0, 0.0 ) ;
  221. glVertex3d ( 2.0, 0.0, 0.0 ) ;
  222. glVertex3d ( 0.0, 0.0, 0.0 ) ;
  223. glVertex3d ( 0.0, 1.0, 0.0 ) ;
  224. glVertex3d ( 0.0, 0.0, 0.0 ) ;
  225. glVertex3d ( 0.0, 0.0, 1.0 ) ;
  226. glEnd () ;
  227. glColor3d ( 1.0, 0.0, 0.0 ) ; /* Red */
  228. draw_curve ( array_index, red_position ) ;
  229. glColor3d ( 0.0, 1.0, 0.0 ) ; /* Green */
  230. draw_curve ( array_index, grn_position ) ;
  231. /* Print the distance between the two points */
  232. glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
  233. glRasterPos2i ( 1, 1 ) ;
  234. bitmapPrintf ( "Distance: %10.6f", distance ) ;
  235. glutSwapBuffers();
  236. }
  237. void reshape_cb ( int width, int height )
  238. {
  239. float ar;
  240. glViewport ( 0, 0, width, height ) ;
  241. glMatrixMode ( GL_PROJECTION ) ;
  242. glLoadIdentity () ;
  243. ar = (float) width / (float) height ;
  244. glFrustum ( -ar, ar, -1.0, 1.0, 10.0, 100.0 ) ;
  245. glMatrixMode ( GL_MODELVIEW ) ;
  246. glLoadIdentity () ;
  247. xcen = 0.0 ;
  248. ycen = 0.0 ;
  249. zcen = 0.0 ;
  250. glTranslated ( xcen, ycen, zcen - 50.0 ) ;
  251. }
  252. void timer_cb ( int value )
  253. {
  254. /* Function called at regular intervals to update the positions of the points */
  255. double deltax, deltay, deltaz ;
  256. int new_index = array_index + 1 ;
  257. /* Set the next timed callback */
  258. glutTimerFunc ( 30, timer_cb, 0 ) ;
  259. if ( animate > 0 )
  260. {
  261. if ( new_index == NUM_POINTS ) new_index = 0 ;
  262. advance_in_time ( time_step, red_position[array_index], red_position[new_index] ) ;
  263. advance_in_time ( time_step, grn_position[array_index], grn_position[new_index] ) ;
  264. array_index = new_index ;
  265. deltax = red_position[array_index][0] - grn_position[array_index][0] ;
  266. deltay = red_position[array_index][1] - grn_position[array_index][1] ;
  267. deltaz = red_position[array_index][2] - grn_position[array_index][2] ;
  268. distance = sqrt ( deltax * deltax + deltay * deltay + deltaz * deltaz ) ;
  269. if ( animate == 2 ) animate = 0 ;
  270. }
  271. glutPostRedisplay () ;
  272. }
  273. /* The Main Program */
  274. int main ( int argc, char *argv[] )
  275. {
  276. int pargc = argc ;
  277. /* Initialize the random number generator */
  278. srand ( 1023 ) ;
  279. /* Set up the OpenGL parameters */
  280. glEnable ( GL_DEPTH_TEST ) ;
  281. glClearColor ( 0.0, 0.0, 0.0, 0.0 ) ;
  282. glClearDepth ( 1.0 ) ;
  283. /* Initialize GLUT */
  284. glutInitWindowSize ( 600, 600 ) ;
  285. glutInit ( &pargc, argv ) ;
  286. glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
  287. /* Create the window */
  288. glutCreateWindow ( "Lorenz Attractor" ) ;
  289. glutKeyboardFunc ( key_cb ) ;
  290. glutMouseFunc ( mouse_cb ) ;
  291. glutSpecialFunc ( special_cb ) ;
  292. glutDisplayFunc ( display_cb ) ;
  293. glutReshapeFunc ( reshape_cb ) ;
  294. glutTimerFunc ( 30, timer_cb, 0 ) ;
  295. /* Initialize the attractor: The easiest way is to call the keyboard callback with an
  296. * argument of 'r' for Reset.
  297. */
  298. key_cb ( 'r', 0, 0 ) ;
  299. /* Enter the GLUT main loop */
  300. glutMainLoop () ;
  301. #ifdef _MSC_VER
  302. /* DUMP MEMORY LEAK INFORMATION */
  303. _CrtDumpMemoryLeaks () ;
  304. #endif
  305. return 0 ;
  306. }