validate_react_cond.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* To compile: gcc validate_react_trig.c react_cond.c rng.c */
  2. /* To run: ./a.out */
  3. /* Generates some reactions and tests to see if they will happen. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/time.h>
  7. #include "mcell_structs.h"
  8. #include "react.h"
  9. #define SAMPLE_SIZE 100000
  10. struct volume *world;
  11. long long bigtime()
  12. {
  13. struct timeval t;
  14. gettimeofday(&t,NULL);
  15. return ((1000000 * (long long)t.tv_sec) + (long long)t.tv_usec);
  16. }
  17. int main()
  18. {
  19. int i,j;
  20. double t;
  21. long long t0,tF;
  22. int count[5];
  23. struct rxn *rxp;
  24. struct molecule *m1,*m2;
  25. struct wall *w1,*w2;
  26. world = malloc(sizeof(struct volume));
  27. world->n_species = 9;
  28. world->species_list = malloc( world->n_species * sizeof(struct species*) );
  29. for (i=0,j=1;i<world->n_species;i++,j*=2)
  30. {
  31. world->species_list[i] = malloc( sizeof(struct species) );
  32. world->species_list[i]->hashval = j;
  33. }
  34. world->species_list[0]->flags = 0; /* Not used. */
  35. world->species_list[1]->flags = 0; /* GEN_MOL */
  36. world->species_list[2]->flags = IS_SURFACE; /* GEN_SURF */
  37. world->species_list[3]->flags = 0; /* A (free) */
  38. world->species_list[4]->flags = 0; /* B (free) */
  39. world->species_list[5]->flags = ON_SURFACE; /* C (surf) */
  40. world->species_list[6]->flags = ON_SURFACE; /* D (surf) */
  41. world->species_list[7]->flags = ON_GRID; /* E (grid) */
  42. world->species_list[8]->flags = IS_SURFACE; /* S (membrane) */
  43. world->n_reactions = 10;
  44. world->reaction_hash = malloc( j * sizeof(struct rxn*) );
  45. for (i=0;i<j;i++) world->reaction_hash[i] = NULL;
  46. /* Unimolecular: A */
  47. i = world->species_list[3]->hashval;
  48. world->reaction_hash[i] = malloc( sizeof(struct rxn) );
  49. rxp = world->reaction_hash[i];
  50. rxp->next = NULL;
  51. rxp->n_reactants = 1;
  52. rxp->players = malloc( rxp->n_reactants * sizeof(struct species*) );
  53. rxp->players[0] = world->species_list[3];
  54. rxp->n_pathways = 4;
  55. rxp->cum_rates = malloc( rxp->n_pathways * sizeof(double) );
  56. rxp->cum_rates[0] = 0.07;
  57. rxp->cum_rates[1] = 0.09;
  58. rxp->cum_rates[2] = 0.098;
  59. rxp->cum_rates[3] = 0.1;
  60. /* Bimolecular: A B */
  61. i = world->species_list[3]->hashval + world->species_list[4]->hashval;
  62. world->reaction_hash[i] = malloc( sizeof(struct rxn) );
  63. rxp = world->reaction_hash[i];
  64. rxp->next = NULL;
  65. rxp->n_reactants = 2;
  66. rxp->players = malloc( rxp->n_reactants * sizeof(struct species*) );
  67. rxp->players[0] = world->species_list[3];
  68. rxp->players[1] = world->species_list[4];
  69. rxp->n_pathways = 4;
  70. rxp->cum_rates = malloc( rxp->n_pathways * sizeof(double) );
  71. rxp->cum_rates[0] = 0.02;
  72. rxp->cum_rates[1] = 0.40;
  73. rxp->cum_rates[2] = 0.71;
  74. rxp->cum_rates[3] = 0.85;
  75. /* Intersect: A, W */
  76. i = world->species_list[3]->hashval + world->species_list[8]->hashval;
  77. world->reaction_hash[i] = malloc( sizeof(struct rxn) );
  78. rxp = world->reaction_hash[i];
  79. rxp->next = NULL;
  80. rxp->n_reactants = 2;
  81. rxp->players = malloc( rxp->n_reactants * sizeof(struct species*) );
  82. rxp->players[0] = world->species_list[3];
  83. rxp->players[1] = world->species_list[8];
  84. rxp->n_pathways = 3;
  85. rxp->cum_rates = malloc( rxp->n_pathways * sizeof(double) );
  86. rxp->cum_rates[0] = 0.1;
  87. rxp->cum_rates[1] = 0.3;
  88. rxp->cum_rates[2] = 0.6;
  89. /* Intersect: B, W */
  90. i = world->species_list[4]->hashval + world->species_list[8]->hashval;
  91. world->reaction_hash[i] = malloc( sizeof(struct rxn) );
  92. rxp = world->reaction_hash[i];
  93. rxp->next = NULL;
  94. rxp->n_reactants = 2;
  95. rxp->players = malloc( rxp->n_reactants * sizeof(struct species*) );
  96. rxp->players[0] = world->species_list[4];
  97. rxp->players[1] = world->species_list[8];
  98. rxp->n_pathways = 1;
  99. rxp->cum_rates = malloc( rxp->n_pathways * sizeof(double) );
  100. rxp->cum_rates[0] = 1.1;
  101. m1 = malloc( sizeof(struct molecule) );
  102. m1->properties = world->species_list[3];
  103. m2 = malloc( sizeof(struct molecule) );
  104. m2->properties = world->species_list[4];
  105. w1 = malloc( sizeof(struct wall) );
  106. w1->wall_type = world->species_list[8];
  107. t0 = bigtime();
  108. rxp = world->reaction_hash[ world->species_list[3]->hashval ];
  109. for (i=0;i<5;i++) count[i] = 0;
  110. for (i=0;i<SAMPLE_SIZE;i++)
  111. {
  112. j = test_unimolecular(rxp);
  113. count[j+1]++;
  114. }
  115. printf("test_unimolecular:\n");
  116. printf(" No reaction: %5d (expect about %5d)\n",count[0],(int)((1.0-rxp->cum_rates[3])*SAMPLE_SIZE));
  117. printf(" Path 0: %5d (expect about %5d)\n",count[1],(int)(rxp->cum_rates[0]*SAMPLE_SIZE));
  118. printf(" Path 1: %5d (expect about %5d)\n",count[2],(int)((rxp->cum_rates[1]-rxp->cum_rates[0])*SAMPLE_SIZE));
  119. printf(" Path 2: %5d (expect about %5d)\n",count[3],(int)((rxp->cum_rates[2]-rxp->cum_rates[1])*SAMPLE_SIZE));
  120. printf(" Path 3: %5d (expect about %5d)\n",count[4],(int)((rxp->cum_rates[3]-rxp->cum_rates[2])*SAMPLE_SIZE));
  121. printf("\n");
  122. t = 0;
  123. for (i=0;i<SAMPLE_SIZE;i++)
  124. {
  125. t += timeof_unimolecular(rxp);
  126. }
  127. t /= (double) SAMPLE_SIZE;
  128. printf("timeof_unimolecular:\n");
  129. printf(" Mean timesteps %f\n",t);
  130. printf(" Rate constant %f\n",rxp->cum_rates[3]);
  131. printf("\n");
  132. for (i=0;i<5;i++) count[i] = 0;
  133. for (i=0;i<SAMPLE_SIZE;i++)
  134. {
  135. j = which_unimolecular(rxp);
  136. count[j+1]++;
  137. }
  138. printf("which_unimolecular:\n");
  139. printf(" Path 0: %5d (expect about %5d)\n",count[1],(int)(rxp->cum_rates[0]*SAMPLE_SIZE/rxp->cum_rates[3]));
  140. printf(" Path 1: %5d (expect about %5d)\n",count[2],(int)((rxp->cum_rates[1]-rxp->cum_rates[0])*SAMPLE_SIZE/rxp->cum_rates[3]));
  141. printf(" Path 2: %5d (expect about %5d)\n",count[3],(int)((rxp->cum_rates[2]-rxp->cum_rates[1])*SAMPLE_SIZE/rxp->cum_rates[3]));
  142. printf(" Path 3: %5d (expect about %5d)\n",count[4],(int)((rxp->cum_rates[3]-rxp->cum_rates[2])*SAMPLE_SIZE/rxp->cum_rates[3]));
  143. printf("\n");
  144. rxp = world->reaction_hash[ world->species_list[3]->hashval ^ world->species_list[4]->hashval ];
  145. for (i=0;i<5;i++) count[i] = 0;
  146. for (i=0;i<SAMPLE_SIZE;i++)
  147. {
  148. j = test_bimolecular(rxp,1.0);
  149. count[j+1]++;
  150. }
  151. printf("test_bimolecular:\n");
  152. printf(" No reaction: %5d (expect about %5d)\n",count[0],(int)((1.0-rxp->cum_rates[3])*SAMPLE_SIZE));
  153. printf(" Path 0: %5d (expect about %5d)\n",count[1],(int)(rxp->cum_rates[0]*SAMPLE_SIZE));
  154. printf(" Path 1: %5d (expect about %5d)\n",count[2],(int)((rxp->cum_rates[1]-rxp->cum_rates[0])*SAMPLE_SIZE));
  155. printf(" Path 2: %5d (expect about %5d)\n",count[3],(int)((rxp->cum_rates[2]-rxp->cum_rates[1])*SAMPLE_SIZE));
  156. printf(" Path 3: %5d (expect about %5d)\n",count[4],(int)((rxp->cum_rates[3]-rxp->cum_rates[2])*SAMPLE_SIZE));
  157. printf("\n");
  158. rxp = world->reaction_hash[ world->species_list[3]->hashval ^ world->species_list[8]->hashval ];
  159. for (i=0;i<5;i++) count[i] = 0;
  160. for (i=0;i<SAMPLE_SIZE;i++)
  161. {
  162. j = test_intersect(rxp,1.0);
  163. count[j+1]++;
  164. }
  165. printf("test_intersect:\n");
  166. printf(" No reaction: %5d (expect about %5d)\n",count[0],(int)((1.0-rxp->cum_rates[2])*SAMPLE_SIZE));
  167. printf(" Path 0: %5d (expect about %5d)\n",count[1],(int)(rxp->cum_rates[0]*SAMPLE_SIZE));
  168. printf(" Path 1: %5d (expect about %5d)\n",count[2],(int)((rxp->cum_rates[1]-rxp->cum_rates[0])*SAMPLE_SIZE));
  169. printf(" Path 2: %5d (expect about %5d)\n",count[3],(int)((rxp->cum_rates[2]-rxp->cum_rates[1])*SAMPLE_SIZE));
  170. printf("\n");
  171. rxp = world->reaction_hash[ world->species_list[4]->hashval ^ world->species_list[8]->hashval ];
  172. for (i=0;i<SAMPLE_SIZE;i++)
  173. {
  174. j = test_intersect(rxp,1.0);
  175. if (j!=0) printf("Expected path 0 but didn't find it on trial #%05d.\n",i);
  176. }
  177. printf("Done testing always-reacting intersection.\n\n");
  178. tF = bigtime();
  179. printf("%d ms elapsed with %d conditions checked.\n",(long)((tF-t0)/1000),SAMPLE_SIZE*6);
  180. printf(" (Rate: %.0f/second.)\n\n",(6.0e6*(double)SAMPLE_SIZE)/((double)(tF-t0)));
  181. }