home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / chernikov / part01 / Chernikov.c next >
Encoding:
C/C++ Source or Header  |  1993-04-02  |  4.1 KB  |  180 lines

  1. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Name : Chernikov
  4. Version: 1.0
  5. Written on   :  24-Apr-92     By : A. Etemadi
  6. Modified on  : 
  7. Directory    : ~atae/Space
  8.  
  9. ==============================================================================
  10.  
  11.  
  12. Usage:    
  13.     Chernikov -[AKUVhsnrc] > OutputPGMImage
  14.  
  15. Output result       : 
  16.  
  17.    0 = successful, 
  18.   -1 = error, 
  19.  
  20. Functionality: 
  21.  
  22. Iterates through the Chernikov equations (Nature Vol. 326, April 1987)
  23. and produces an image based on occupancy of cells.
  24.  
  25. ----------------------------------------------------------------------------*/
  26.  
  27. #include <stdio.h>     /* Standard C I/O library */
  28. #include <math.h>      /* Standard C mathematics library */
  29.  
  30. #define pi 2.0*acos(0.0)
  31.  
  32. main(argc,argv)
  33.  
  34.  int  argc;
  35.  char **argv;
  36.  
  37. {
  38.  
  39. /*
  40. =======================================================================
  41. ===================== START OF DECLARATION ============================
  42. =======================================================================
  43. */
  44.  
  45. /*
  46.  Indecies
  47. */
  48.  
  49.     register int i,j;
  50.     register int row,col;
  51.  
  52.     int Height,Width;
  53.  
  54.     FILE *p_OutFile;        /* Pointer to start of output file */
  55.  
  56. /* 
  57.  Create buffer for image
  58. */
  59.     char OutImage[512][512];
  60.  
  61. /* 
  62.  Chernikov parameters
  63. */
  64.     float Alpha;
  65.     float K;
  66.     float U0,U1;
  67.     float V0,V1;
  68.     float Scale;
  69.  
  70.     int NIter;
  71.  
  72.     float KoverAlpha;
  73.     float cosAlpha;
  74.     float sinAlpha;
  75.     float Beta;
  76. /*
  77. =======================================================================
  78. ===================== START OF INITIALISATION =========================
  79. =======================================================================
  80. */
  81.  
  82. /*
  83.  Set defaults, process command line options
  84. */
  85.     Alpha     = 2.0*pi/5.0; 
  86.     K     = 0.9;
  87.     U0     = 0.0;
  88.     V0     = 15.0;
  89.        Scale     = 1.5;
  90.     NIter     = 10000;
  91.     Height    = Width = 256;
  92.  
  93.   for (i=1;i<argc;i++) {
  94.     if (argv[i][0] == '-') {
  95.       switch (argv[i][1]) {
  96.         case 'A': Alpha     = atof(argv[++i]); continue;    
  97.         case 'K': K     = atof(argv[++i]); continue;    
  98.         case 'U': U0     = atof(argv[++i]); continue;    
  99.         case 'V': V0     = atof(argv[++i]); continue;    
  100.         case 's': Scale     = atof(argv[++i]); continue;    
  101.         case 'n': NIter  = atoi(argv[++i]); continue;    
  102.         case 'r': Height = atoi(argv[++i]); continue;    
  103.         case 'c': Width  = atoi(argv[++i]); continue;    
  104.            default : fprintf(stderr,"unrecognized option: %s",argv[i]); return(-1);
  105.         case 'h':
  106.          fprintf(stderr,"\nUSAGE :\n");
  107.         fprintf(stderr," Chernikov -[AKUVhsnrc] > OutImage\n\n");
  108.         fprintf(stderr,"OPTION:                    DEFAULT:\n");
  109.         fprintf(stderr,"-----------------------------------\n");
  110.         fprintf(stderr,"-A Alpha                   2pi/15\n");
  111.         fprintf(stderr,"-K Kappa                   0.9\n");
  112.         fprintf(stderr,"-U Initial pahse           0.0\n");
  113.         fprintf(stderr,"-V Initial Velocity        15.0\n");
  114.         fprintf(stderr,"-s Scale                   2.0\n");
  115.         fprintf(stderr,"-n Interations             10000\n");
  116.         fprintf(stderr,"-r Image Height            256\n");
  117.         fprintf(stderr,"-c Image Width             256\n");
  118.         return(-1);
  119.       }
  120.     }
  121.   }
  122.  
  123. /*
  124.  Use standard output
  125. */
  126.        p_OutFile = stdout;
  127. /*
  128.   Initialise OutImage pixels and starting parameters
  129. */
  130.   for (i = 0; i < Height; i++) {
  131.       for (j = 0; j < Width; j++) {
  132.          OutImage[i][j] = (char) 0;
  133.     }
  134.   }
  135.  
  136.         cosAlpha = cos(Alpha);
  137.         sinAlpha = sin(Alpha);
  138.         KoverAlpha = K/Alpha;
  139.      fprintf(stderr,"\n");
  140.  
  141. /*
  142.  Now iterate through the Chernikov equation
  143. */
  144.   for (i = 0; i < NIter; i++) {
  145.  
  146.      row = (int) (U0*Scale) + Height/2; 
  147.      col = (int) (V0*Scale) + Width/2;
  148.  
  149.      if ( (i+1)%10000 == 0)
  150.      fprintf(stderr,"Iteration = %d\r",i+1);
  151.  
  152.     if (row < Height && col < Width && row > 0 && col > 0 &&
  153.              (OutImage[row][col] & 0377) < 255 )
  154.               OutImage[row][col] += (char) 1;
  155.  
  156.          Beta = U0 + KoverAlpha*sin(V0);
  157.          U1 =  Beta*cosAlpha + V0*sinAlpha;
  158.          V1 = -Beta*sinAlpha + V0*cosAlpha;
  159.          U0 = U1; V0 = V1;         
  160.   }
  161. /*
  162.  Write resultant image with PGM header
  163. */
  164.  
  165.   fprintf(p_OutFile,"P5\n%d %d\n255\n",Height,Width);
  166.   for (i = 0; i < Height; i++) {
  167.       for (j = 0; j < Width; j++) {
  168.            putc(OutImage[i][j],p_OutFile);
  169.     }
  170.   }
  171.  
  172. /*
  173.  Close file
  174. */
  175.          fprintf(stderr,"\n");
  176.          fclose(p_OutFile);
  177.  
  178.       return(0);
  179. }
  180.