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