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

  1. /*
  2.  * File: bz.m256.c
  3.  *   By: Dave Hiebeler
  4.  *       hiebeler@turing.cs.rpi.edu
  5.  *       February 1990
  6.  *
  7.  * The version of the Belousov-Zhabotinski ("BZ") reaction described in
  8.  * the CAM book by Toffoli and Margolus (also discussed as the "hodgepodge"
  9.  * model in A.K. Dewdney's "Computer Recreations" colum in Scientific
  10.  * American, sometime in 1989).  This rule is basically a model of phase
  11.  * waves in excitable media with latency.
  12.  *
  13.  * The two parameters are used as a threshold and annealing factor.
  14.  * Basically, each cell counts the number of "active" neighbors, call
  15.  * that N.  Then, if N >= parm1 *or* N == parm2, the current cell will
  16.  * become inactive for a few time-steps.
  17.  *
  18.  * So with parm1=3 and parm2=3, you get a fairly simple system of propagating
  19.  * phase waves.  With parm1=4 and parm2=2, you get a kind of "annealing"
  20.  * version of the system.  See the CAM book, section 9.3, for more details.
  21.  *
  22.  * The rule actually only uses values between 0 and 15 (the lower 4 bitplanes).
  23.  * It is a computed-function rule because a 16-state moore lookup table
  24.  * would be huge (2^36 entries!).  But you can start with a "quick random"
  25.  * image, as the rule will ignore the high bitplanes, and clear them out
  26.  * after the first time-step.
  27.  *
  28.  */
  29.  
  30.  
  31. #include "CMnborhood.h"
  32.  
  33.  
  34.  
  35. byte bz(), bz_exit();
  36.  
  37. static CM_field_id_t nbor_sum, bool1;
  38.  
  39. void
  40. init_function()
  41. {
  42.     update_function = bz;
  43.     exit_function = bz_exit;
  44.     parm1 = 4;
  45.     parm2 = 2;
  46.     nbor_sum = CM_allocate_heap_field(4);
  47.     bool1 = CM_allocate_heap_field(1);    /* used to store intermediate results
  48.                      * for boolean logic */
  49. }
  50.  
  51.  
  52. byte
  53. bz()
  54. {
  55.     CM_set_context();
  56.  
  57.     /* add up neighbors */
  58.     /* the "active" flag is kept up in the third bit-plane, or 2 bits above
  59.      * the start of the field, which is why there's a "+2" on all the
  60.      * neighbors used
  61.      */
  62.     CM_u_move_2L(nbor_sum, MNW+2, 4, 1);
  63.     CM_u_add_3_3L(nbor_sum, nbor_sum, MWest+2, 4, 4, 1);
  64.     CM_u_add_3_3L(nbor_sum, nbor_sum, MSW+2, 4, 4, 1);
  65.     CM_u_add_3_3L(nbor_sum, nbor_sum, MNorth+2, 4, 4, 1);
  66.     CM_u_add_3_3L(nbor_sum, nbor_sum, MSouth+2, 4, 4, 1);
  67.     CM_u_add_3_3L(nbor_sum, nbor_sum, MNE+2, 4, 4, 1);
  68.     CM_u_add_3_3L(nbor_sum, nbor_sum, MEast+2, 4, 4, 1);
  69.     CM_u_add_3_3L(nbor_sum, nbor_sum, MSE+2, 4, 4, 1);
  70.  
  71.     /* zero out cell */
  72.     CM_u_move_zero_1L(cell, 8);
  73.  
  74.     /* if clock == 0, set active flag */
  75.     CM_set_context();
  76.     CM_u_eq_zero_1L(MCenter,2);
  77.     CM_logand_context_with_test();
  78.     CM_u_move_constant_1L(cell, 4, 8);
  79.  
  80.     /* if nborsum >= parm1 or nborsum == parm2, set alarm meaning we're
  81.      * about to become inactive */
  82.     CM_set_context();
  83.     CM_u_ge_constant_1L(nbor_sum, parm1%256, 4);
  84.     CM_store_test(bool1);
  85.     CM_u_eq_constant_1L(nbor_sum, parm2%256, 4);
  86.     CM_logand_context_with_test();
  87.     CM_logior_context(bool1);
  88.     CM_u_move_constant_1L(cell+3, 1, 1);
  89.  
  90.     /* if active and alarm, set clock to 3 */
  91.     CM_set_context();
  92.     CM_u_eq_constant_1L(MCenter+2, 3, 2);
  93.     CM_logand_context_with_test();
  94.     CM_u_move_constant_1L(cell, 3, 2);
  95.  
  96.     /* else if clock is non-zero, decrement it */
  97.     CM_invert_context();
  98.     CM_u_ne_zero_1L(MCenter, 2);
  99.     CM_logand_context_with_test();
  100.     CM_u_subtract_constant_2_1L(MCenter, 1, 2);
  101.     CM_u_move_1L(cell, MCenter, 2);
  102.     CM_set_context();
  103. }
  104.  
  105.  
  106. byte
  107. bz_exit()
  108. {
  109.     CM_deallocate_heap_field(nbor_sum);
  110.     CM_deallocate_heap_field(bool1);
  111. }
  112.