Trisurf Monte Carlo simulator
Samo Penic
2013-11-22 4e56fe682e6ad34c161aa40c06af4e3e0088962e
commit | author | age
d7639a 1 #include<stdlib.h>
SP 2 #include<stdio.h>
aec47d 3 #include<math.h>
c9884c 4 #include<pthread.h>
4e56fe 5 #include<unistd.h> //usleep requires it
aec47d 6 //#include "io.h"
SP 7 #include "general.h"
8 #include "timestep.h"
9 #include "vertexmove.h"
30ee9c 10 #include "bondflip.h"
d7a113 11 #include "frame.h"
200815 12 #include "vertex.h"
d7a113 13 #include "io.h"
c9884c 14
d7a113 15 ts_bool run_simulation(ts_vesicle *vesicle, ts_uint mcsweeps, ts_uint inititer, ts_uint iterations){
SP 16     ts_uint i, j;
17
18     centermass(vesicle);
19     cell_occupation(vesicle);
20     ts_fprintf(stdout, "Starting simulation (first %d x %d MC sweeps will not be recorded on disk)\n", inititer, mcsweeps);
4e56fe 21     pthread_t tid1,tid2, tid3, tid4;
SP 22     thdata data1, data2, data3, data4;
23     pthread_mutex_t mutex;
c9884c 24     data1.thread_no=0;
SP 25     data2.thread_no=1;
26     data1.vesicle=vesicle;
27     data2.vesicle=vesicle;
4e56fe 28            data3.thread_no=2;
SP 29     data4.thread_no=3;
30     data3.vesicle=vesicle;
31     data4.vesicle=vesicle;
32
33     data1.vtx_tainting_lock=&mutex;
34     data2.vtx_tainting_lock=&mutex;
35     data3.vtx_tainting_lock=&mutex;
36     data4.vtx_tainting_lock=&mutex;
37
38     pthread_mutex_init(&mutex,NULL);
39
40      for(i=0;i<inititer+iterations;i++){
d7a113 41         for(j=0;j<mcsweeps;j++){
c9884c 42             pthread_create(&tid1,NULL,single_timestep,(void *)&data1);
SP 43                 pthread_create(&tid2,NULL,single_timestep,(void *)&data2);
4e56fe 44                 pthread_create(&tid3,NULL,single_timestep,(void *)&data3);
SP 45                 pthread_create(&tid4,NULL,single_timestep,(void *)&data4);
c9884c 46                 pthread_join(tid1,NULL);
SP 47                 pthread_join(tid2,NULL);
4e56fe 48                 pthread_join(tid3,NULL);
SP 49                 pthread_join(tid4,NULL);
c9884c 50
SP 51 //    single_timestep(vesicle);
d7a113 52         }
SP 53         centermass(vesicle);
54         cell_occupation(vesicle);
55         if(i>inititer){
56             write_vertex_xml_file(vesicle,i-inititer);
57         }
58     }
4e56fe 59
SP 60     pthread_mutex_destroy(&mutex);
61     pthread_exit(NULL);
d7a113 62     return TS_SUCCESS;
SP 63 }
d7639a 64
c9884c 65 void * single_timestep(void *thread_data){
SP 66     thdata *data;
67     data=(thdata *)thread_data;
68     ts_vesicle *vesicle=data->vesicle;
69     ts_uint thID=data->thread_no;
4e56fe 70     ts_uint partition_size=(ts_uint)(vesicle->vlist->n/4);
c9884c 71     ts_uint end;
4e56fe 72     if(thID==3){
c9884c 73         end=vesicle->vlist->n;
SP 74     } else {
75         end=(thID+1)*partition_size;
76     }
d7639a 77     ts_double rnvec[3];
c9884c 78     ts_uint i; //b;
SP 79     for(i=thID*partition_size;i<end;i++){
d7639a 80         rnvec[0]=drand48();
SP 81         rnvec[1]=drand48();
82         rnvec[2]=drand48();
4e56fe 83
SP 84     /**** THREAD IS POTENTIALLY LOCKED ******/
85     pthread_mutex_lock(data->vtx_tainting_lock); //taint if no other process is tainting or wait until you can taint
86     //    ts_fprintf(stdout, "thID=%d:: Tainting vertex %d, level=%d. Waiting....\n",thID, i, vesicle->vlist->vtx[i]->locked);
c9884c 87     while(vertex_tainted(vesicle->vlist->vtx[i],1,1)){
4e56fe 88         ts_fprintf(stdout, "thID=%d:: Vertex %d is tainted, so I cannot try vertexmove. Level=%d. WAITING!\n",thID, i, vesicle->vlist->vtx[i]->locked);
200815 89     }
4e56fe 90     vertex_taint(vesicle->vlist->vtx[i],1);
SP 91     pthread_mutex_unlock(data->vtx_tainting_lock);
92     /**** THREAD IS RELEASING MUTEX RESOURCES ******/
93
c9884c 94         single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec);
200815 95     vertex_untaint(vesicle->vlist->vtx[i],1);
SP 96 //        ts_fprintf(stdout, "Vertex %d should be untainted, level=%d.\n", i, vesicle->vlist->vtx[i]->locked);
d7639a 97     }
SP 98
b7c9b3 99 //    ts_int cnt=0;
c9884c 100 /*
142a67 101     for(i=0;i<vesicle->vlist->n;i++){
SP 102     b=rand() % vesicle->blist->n;
d7639a 103         //find a bond and return a pointer to a bond...
SP 104         //call single_bondflip_timestep...
142a67 105         retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
b7c9b3 106 //    if(retval==TS_SUCCESS) cnt++;        
d7639a 107     } 
b7c9b3 108 //    printf("Bondflip success rate in one sweep: %d/%d=%e\n", cnt,vesicle->blist->n,(double)cnt/(double)vesicle->blist->n);
c9884c 109 */
SP 110 /*    if(retval);
d7639a 111     return TS_SUCCESS;
c9884c 112 */
SP 113      pthread_exit(0); /* exit */
d7639a 114 }
SP 115
116
117