CP_core_IPS.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "CP_core_IPS.h"
  2. // Initialize the lattice
  3. void initialize_IPS(IPSmodel *s)
  4. {
  5. int r=5;
  6. //Start with en empty lattice
  7. int x,y;
  8. for (x = 0; x < X_SIZE; x++)
  9. for (y = 0; y < Y_SIZE; y++)
  10. {
  11. s->lattice_configuration[x][y]=0;
  12. }
  13. s->occupancy = 0;
  14. // then add some r-size cluster with particles
  15. for (x = 128-r; x < 128+r; x++)
  16. for (y = 128-r; y < 128+r; y++)
  17. {
  18. s->lattice_configuration[x][y]= 1;
  19. s->occupancy ++;
  20. }
  21. s->initialized = TRUE;
  22. s->generation_time = 0;
  23. }
  24. // Update function
  25. void update_IPS(IPSmodel *s)
  26. {
  27. int random_neighbor_state, tape_symbol;
  28. long random_neighboor;
  29. int sites = 0;
  30. int random_x_coor, random_y_coor;
  31. // The Monte Carlo step (MC) is 4x the number of sites in the Lattice
  32. for (sites; sites < (int)(Y_SIZE*X_SIZE)*4; sites++)
  33. {
  34. //pick a random focal site
  35. random_x_coor = (int) floor(genrand64_real3()*X_SIZE);
  36. random_y_coor = (int) floor(genrand64_real3()*Y_SIZE);
  37. switch(s->lattice_configuration[random_x_coor][random_y_coor])
  38. {
  39. case 0: // if the site is empty
  40. // chose a random neighboor from the 4 posible ones
  41. random_neighboor = 1 + (long) floor(genrand64_real3()*4);
  42. switch(random_neighboor)
  43. {
  44. case 1: // North
  45. random_neighbor_state = s->lattice_configuration[random_x_coor][(int)(((int)Y_SIZE + random_y_coor-1) % (int)Y_SIZE)];
  46. break;
  47. case 2: // West
  48. random_neighbor_state = s->lattice_configuration[(int)((X_SIZE + random_x_coor-1)%X_SIZE)][random_y_coor];
  49. break;
  50. case 3: // South
  51. random_neighbor_state = s->lattice_configuration[random_x_coor][(int)((Y_SIZE + random_y_coor+1) % Y_SIZE)];
  52. break;
  53. case 4: // East
  54. random_neighbor_state = s->lattice_configuration[(int)((X_SIZE + random_x_coor+1)%X_SIZE)][random_y_coor];
  55. break;
  56. }
  57. // if its random neighbor is occupied: put a copy at the focal site state.
  58. // Note that when all 4 newighboors are occupied
  59. // colonization of the focal site happens with probability 1.0.
  60. if(random_neighbor_state != 0)
  61. {
  62. s->lattice_configuration[random_x_coor][random_y_coor] = random_neighbor_state;
  63. s->occupancy ++;
  64. }
  65. break;
  66. case 1: // if the site is occupied and points North (N)
  67. // if a particle is present at the focal site,
  68. //it can die with probability dead_rate* dt
  69. if (genrand64_real2() < s->dead_rate)
  70. {
  71. s->lattice_configuration[random_x_coor][random_y_coor]= 0;
  72. s->occupancy --;
  73. }
  74. break;
  75. }
  76. }
  77. s->generation_time ++;
  78. }
  79. void prepare_IPS(IPSmodel *s)
  80. {
  81. // we initialize the mt algorithm for random number genration
  82. unsigned long long seed = (unsigned int)time(NULL);
  83. init_genrand64(seed);
  84. //define default parameters of the simulation
  85. s->dead_rate = (float) MORTALITY;
  86. s->running = FALSE;
  87. s->initialized = FALSE; // SET SIMULATION FLAGS
  88. }
  89. // Dialog Box
  90. // For info at a website:
  91. //lab wiki on the contact process
  92. void show_about(GtkWidget *widget, gpointer data)
  93. {
  94. GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("kimero_LAB_transparent.tiff", NULL);
  95. GtkWidget *dialog = gtk_about_dialog_new();
  96. gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG(dialog),
  97. "IPS Process App");
  98. gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "v 1.0, 2024");
  99. gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog),"Open Source Code");
  100. gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog),
  101. "The Contact process, a stochastic process, is a model of spatial competition. Particles die and spread in a 2D lattice");
  102. gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog),
  103. "https://github.com/jekeymer/Contact-Process/wiki");
  104. gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), pixbuf);
  105. g_object_unref(pixbuf), pixbuf = NULL;
  106. gtk_dialog_run(GTK_DIALOG (dialog));
  107. gtk_widget_destroy(dialog);
  108. }