Trisurf Monte Carlo simulator
Samo Penic
2014-07-08 5ec690fdf8f463a02c4e4d5505d5486b9a5bbd70
commit | author | age
d7639a 1 #include<stdlib.h>
SP 2 #include<stdio.h>
aec47d 3 #include<math.h>
SP 4 //#include "io.h"
5 #include "general.h"
6 #include "timestep.h"
7 #include "vertexmove.h"
30ee9c 8 #include "bondflip.h"
d7a113 9 #include "frame.h"
SP 10 #include "io.h"
37d14a 11 #include "stats.h"
dc77e8 12 #include "sh.h"
459ff9 13 #include "shcomplex.h"
dc77e8 14 #include "vesicle.h"
5a3862 15 #include<gsl/gsl_complex.h>
M 16 #include<gsl/gsl_complex_math.h>
d27c07 17 #include "cross-section.h"
fedf2b 18
626811 19 ts_bool run_simulation(ts_vesicle *vesicle, ts_uint mcsweeps, ts_uint inititer, ts_uint iterations, ts_uint start_iteration){
5a3862 20     ts_uint i, j,k,l,m;
1665aa 21     ts_double r0,kc1,kc2,kc3,kc4;
37d14a 22     ts_double l1,l2,l3,volume=0.0,area=0.0,vmsr,bfsr, vmsrt, bfsrt;
SP 23     ts_ulong epochtime;
5a3862 24     FILE *fd1,*fd2=NULL;
fe24d2 25 //     char filename[255];
37d14a 26     FILE *fd=fopen("statistics.csv","w");
d27c07 27
SP 28     ts_coord_list *pts;
37d14a 29     if(fd==NULL){
SP 30         fatal("Cannot open statistics.csv file for writing",1);
31     }
1665aa 32     fprintf(fd, "Epoch OuterLoop VertexMoveSucessRate BondFlipSuccessRate Volume Area lamdba1 lambda2 lambda3 Kc(2-9) Kc(6-9) Kc(2-end) Kc(3-6)\n");
5a3862 33
M 34      if(vesicle->sphHarmonics!=NULL){
35         fd2=fopen("ulm2.csv","w");
36         if(fd2==NULL){
37             fatal("Cannot open ulm2.csv file for writing",1);
38         }
39         fprintf(fd2, "Timestep u_00^2 u_10^2 u_11^2 u_20^2 ...\n");    
40
41     }
42
c60a49 43 /* RANDOM SEED SET BY CURRENT TIME */
M 44     epochtime=get_epoch();            
45     srand48(epochtime);
5a3862 46
d7a113 47     centermass(vesicle);
SP 48     cell_occupation(vesicle);
fe5069 49     vesicle_volume(vesicle); //needed for constant volume at this moment
626811 50     if(start_iteration<inititer) ts_fprintf(stdout, "Starting simulation (first %d x %d MC sweeps will not be recorded on disk)\n", inititer, mcsweeps);
SP 51     for(i=start_iteration;i<inititer+iterations;i++){
37d14a 52         vmsr=0.0;
SP 53         bfsr=0.0;
3de289 54 /*    vesicle_volume(vesicle);
SP 55     fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume); */
d7a113 56         for(j=0;j<mcsweeps;j++){
37d14a 57             single_timestep(vesicle, &vmsrt, &bfsrt);
SP 58             vmsr+=vmsrt;
59             bfsr+=bfsrt;
d7a113 60         }
3de289 61 /*
SP 62     vesicle_volume(vesicle);
63     fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume); */
37d14a 64         vmsr/=(ts_double)mcsweeps;
SP 65         bfsr/=(ts_double)mcsweeps;
d7a113 66         centermass(vesicle);
SP 67         cell_occupation(vesicle);
f8e6ba 68         ts_fprintf(stdout,"Done %d out of %d iterations (x %d MC sweeps).\n",i+1,inititer+iterations,mcsweeps);
1ab449 69             dump_state(vesicle,i);
58230a 70         if(i>=inititer){
d7a113 71             write_vertex_xml_file(vesicle,i-inititer);
37d14a 72             write_master_xml_file("test.pvd");
8a72f3 73                     epochtime=get_epoch();            
37d14a 74             gyration_eigen(vesicle, &l1, &l2, &l3);
632960 75             vesicle_volume(vesicle); //calculates just volume. Area is not added to ts_vesicle yet!
SP 76             get_area_volume(vesicle, &area,&volume); //that's why I must recalculate area (and volume for no particular reason).
dc77e8 77             r0=getR0(vesicle);
632960 78             if(vesicle->sphHarmonics!=NULL){
8a72f3 79             pts=get_crossection_with_plane(vesicle, 0.0,0.0,1.0,0.0);
SP 80             save_crossection_snapshot(pts,i-inititer);
81             free(pts);
82
632960 83                 preparationSh(vesicle,r0);
459ff9 84                 //calculateYlmi(vesicle);
SP 85                 calculateUlmComplex(vesicle);
86                 storeUlmComplex2(vesicle);
632960 87                 saveAvgUlm2(vesicle);
22cdfd 88                 kc1=calculateKc(vesicle, 2,9);
SP 89                 kc2=calculateKc(vesicle, 6,9);
90                 kc3=calculateKc(vesicle, 2,vesicle->sphHarmonics->l);
1665aa 91                 kc4=calculateKc(vesicle, 3,6);
22cdfd 92             
5bb6bb 93                 fd1=fopen("state.dat","w");
M 94                 fprintf(fd1,"%e %e\n",vesicle->volume, getR0(vesicle));
95                 for(k=0;k<vesicle->vlist->n;k++){
96                     fprintf(fd1,"%e %e %e %e %e\n",
97                         vesicle->vlist->vtx[k]->x,
98                         vesicle->vlist->vtx[k]->y,
99                         vesicle->vlist->vtx[k]->z,
100                         vesicle->vlist->vtx[k]->solAngle,
101                         vesicle->vlist->vtx[k]->relR
102                     );
103                 }
104                 fclose(fd1);
5a3862 105         
M 106             fprintf(fd2,"%u ", i);
107             for(l=0;l<vesicle->sphHarmonics->l;l++){
108                 for(m=l;m<2*l+1;m++){
109                     fprintf(fd2,"%e ", gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m]) );
110                 }
111             }
112                 fprintf(fd2,"\n");
113     
114                 fflush(fd2);    
115
632960 116             }
dc77e8 117
1665aa 118             fprintf(fd, "%lu %u %e %e %1.16e %1.16e %1.16e %1.16e %1.16e %1.16e %1.16e %1.16e %1.16e\n",epochtime,i,vmsr,bfsr,volume, area,l1,l2,l3,kc1, kc2, kc3,kc4);
5a3862 119
632960 120             fflush(fd);    
144784 121         //    sprintf(filename,"timestep-%05d.pov",i-inititer);
fe24d2 122         //    write_pov_file(vesicle,filename);
d7a113 123         }
SP 124     }
37d14a 125     fclose(fd);
5a3862 126     if(fd2!=NULL) fclose(fd2);
d7a113 127     return TS_SUCCESS;
SP 128 }
d7639a 129
37d14a 130 ts_bool single_timestep(ts_vesicle *vesicle,ts_double *vmsr, ts_double *bfsr){
3de289 131 //    vesicle_volume(vesicle);
SP 132 //    fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume);
d7639a 133     ts_bool retval;
SP 134     ts_double rnvec[3];
fe5069 135     ts_uint i,j, b;
37d14a 136     ts_uint vmsrcnt=0;
aec47d 137     for(i=0;i<vesicle->vlist->n;i++){
d7639a 138         rnvec[0]=drand48();
SP 139         rnvec[1]=drand48();
140         rnvec[2]=drand48();
aec47d 141         retval=single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec);
37d14a 142     if(retval==TS_SUCCESS) vmsrcnt++;        
d7639a 143     }
SP 144
37d14a 145     ts_int bfsrcnt=0;
fedf2b 146     for(i=0;i<3*vesicle->vlist->n;i++){
fe5069 147     b=rand() % vesicle->blist->n;
d7639a 148         //find a bond and return a pointer to a bond...
SP 149         //call single_bondflip_timestep...
fe5069 150         retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
3de289 151        //     b++; retval=TS_FAIL;
37d14a 152     if(retval==TS_SUCCESS) bfsrcnt++;        
fedf2b 153     }
M 154
155     for(i=0;i<vesicle->poly_list->n;i++){
58230a 156         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
M 157             rnvec[0]=drand48();
158             rnvec[1]=drand48();
159             rnvec[2]=drand48();
160             retval=single_poly_vertex_move(vesicle,vesicle->poly_list->poly[i],vesicle->poly_list->poly[i]->vlist->vtx[j],rnvec);    
161         }
fedf2b 162     }
M 163
58230a 164
M 165     for(i=0;i<vesicle->filament_list->n;i++){
166         for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
167             rnvec[0]=drand48();
168             rnvec[1]=drand48();
169             rnvec[2]=drand48();
170             retval=single_filament_vertex_move(vesicle,vesicle->filament_list->poly[i],vesicle->filament_list->poly[i]->vlist->vtx[j],rnvec);    
171         }
fedf2b 172     }
M 173  
58230a 174
fedf2b 175 //    printf("Bondflip success rate in one sweep: %d/%d=%e\n", cnt,3*vesicle->blist->n,(double)cnt/(double)vesicle->blist->n/3.0);
37d14a 176     *vmsr=(ts_double)vmsrcnt/(ts_double)vesicle->vlist->n;
SP 177     *bfsr=(ts_double)bfsrcnt/(ts_double)vesicle->vlist->n/3.0;
3de289 178 //    vesicle_volume(vesicle);
SP 179 //    fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume);
d7639a 180     return TS_SUCCESS;
SP 181 }
182
183
184