home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / cm2 / rule_src / cmlv256.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-26  |  3.7 KB  |  117 lines

  1. /*
  2.  * File: cml2.v256.c
  3.  *   By: Dave Hiebeler
  4.  *       hiebeler@turing.cs.rpi.edu
  5.  *       Feb. 1990
  6.  *
  7.  * Coupled map lattice rule (coupled logistics eqn) using single-precision
  8.  * floating-point.  At least, that's what it was supposed to be; I think
  9.  * I still have a bug in it somewhere, I'm not convinced that it behaves
  10.  * the way it should. In any event, it's still a valid example of a
  11.  * floating-point rule being run under Cellsim.
  12.  *
  13.  * The logistics eqn:  x(t+1) = K x(t) (1 - x(t))
  14.  * is run, except instead of just plugging in the local value for x(t),
  15.  * we use a weighted average.
  16.  *
  17.  * Parm1 is the parameter K  (usually called lambda with the logistics eqn);
  18.  * it's been scaled here, so that parm1=100 corresponds to K=4, and parm1=0
  19.  * corresponds to K=0.  (So basically, parm1 = K*25).
  20.  * 
  21.  * Parm2 is the coupling strength: parm2=0 means no coupling (only the
  22.  * current value of the center cell will be used), parm2=100 means that
  23.  * all the cells in the neighborhood will be averaged as the value for x(t).
  24.  *
  25.  * Parm1 and parm2 both range between 0 and 100
  26.  */
  27.  
  28.  
  29. #include "CMnborhood.h"
  30.  
  31. byte cml_rule(), cml_exit();
  32. static CM_field_id_t x_val, avg_x, x_tmp, scell;
  33.  
  34.  
  35. void
  36. init_function()
  37. {
  38.     update_function = cml_rule;
  39.     exit_function = cml_exit;
  40.     parm1 = 90;
  41.     parm2 = 20;
  42.     x_val = CM_allocate_heap_field(32);    /* x_val holds the current floating
  43.                      * value of the cell */
  44.     avg_x = CM_allocate_heap_field(32);    /* used to find weighted average */
  45.     x_tmp = CM_allocate_heap_field(32);    /* scratch space */
  46.     scell = CM_allocate_heap_field(9);    /* used in the process of converting
  47.                     * our floating-point value back into
  48.                     * an 8-bit unsigned value to display */
  49.     
  50.     /* Now, we convert the "cell" field into a floating-point value, and
  51.      * rescale it to be between 0 and 1 rather than 0 and 255.
  52.      */
  53.     CM_f_u_float_2_2L(x_val, cell, 8, 23, 8);
  54.     CM_f_divide_constant_2_1L(x_val, (double)256.0, 23, 8);
  55. }
  56.  
  57.  
  58. byte
  59. cml_rule()
  60. {
  61.     double fparm1, fparm2;   /* hold floating-point copy of parm1&2 */
  62.  
  63.     /* get new values of parm1 & parm2, since user might have changed
  64.      * them since we were last called
  65.      */
  66.     fparm1 = ((double) parm1) / (double)100.0;
  67.     fparm2 = ((double) parm2) / (double)100.0;
  68.  
  69.     
  70.     CM_f_move_zero_always_1L(avg_x, 23, 8);
  71.  
  72.     /* get neighbors' floating-point values, and add into avg_x */
  73.     CM_get_from_news_1L(x_tmp, x_val, North, 32);
  74.     CM_f_add_always_2_1L(avg_x, x_tmp, 23, 8);
  75.  
  76.     CM_get_from_news_1L(x_tmp, x_val, South, 32);
  77.     CM_f_add_always_2_1L(avg_x, x_tmp, 23, 8);
  78.  
  79.     CM_get_from_news_1L(x_tmp, x_val, East, 32);
  80.     CM_f_add_always_2_1L(avg_x, x_tmp, 23, 8);
  81.  
  82.     CM_get_from_news_1L(x_tmp, x_val, West, 32);
  83.     CM_f_add_always_2_1L(avg_x, x_tmp, 23, 8);
  84.  
  85.     CM_f_move_always_1L(x_tmp, x_val, 23, 8);
  86.  
  87.     /* Now, do weighted average of neighbors and center, and store in avg_x */
  88.     CM_f_multiply_const_always_2_1L(x_tmp, fparm2,23,8);
  89.     CM_f_multiply_const_always_2_1L(avg_x, (((double)1.0)-fparm2)/(double)4.0,
  90.                     23, 8);
  91.     CM_f_add_always_2_1L(avg_x, x_tmp, 23, 8);
  92.  
  93.  
  94.     /* Do x <- K x(1-x) */
  95.     CM_f_negate_2_1L(x_val, avg_x, 23, 8);
  96.     CM_f_add_const_always_2_1L(x_val, (double)1.0, 23, 8);
  97.     CM_f_multiply_2_1L(x_val, avg_x, 23, 8);
  98.     CM_f_multiply_const_always_2_1L(x_val, (double)4.0*fparm1, 23, 8);
  99.  
  100.     /* Now scale up to the 0..255 range and copy into cell for display */
  101.     CM_f_multiply_const_always_3_1L(x_tmp, x_val, (double)255.0,23,8);
  102.     CM_s_f_floor_2_2L(scell, x_tmp, 9, 23,8);
  103.     CM_u_move_1L(cell, scell, 8);
  104. }
  105.  
  106.  
  107. byte
  108. cml_exit()
  109. {
  110.     /* deallocate local fields */
  111.     CM_deallocate_heap_field(x_val);
  112.     CM_deallocate_heap_field(avg_x);
  113.     CM_deallocate_heap_field(x_tmp);
  114.     CM_deallocate_heap_field(scell);
  115. }
  116.  
  117.