Trisurf Monte Carlo simulator
Samo Penic
2014-06-13 ad4e7069f10b80446be8d13eba28a03c2c642a3b
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");
d27c07 73             pts=get_crossection_with_plane(vesicle, 0.0,0.0,1.0,0.0);
ad4e70 74             save_crossection_snapshot(pts,i-inititer);
d27c07 75             free(pts);
37d14a 76             epochtime=get_epoch();            
SP 77             gyration_eigen(vesicle, &l1, &l2, &l3);
632960 78             vesicle_volume(vesicle); //calculates just volume. Area is not added to ts_vesicle yet!
SP 79             get_area_volume(vesicle, &area,&volume); //that's why I must recalculate area (and volume for no particular reason).
dc77e8 80             r0=getR0(vesicle);
632960 81             if(vesicle->sphHarmonics!=NULL){
SP 82                 preparationSh(vesicle,r0);
459ff9 83                 //calculateYlmi(vesicle);
SP 84                 calculateUlmComplex(vesicle);
85                 storeUlmComplex2(vesicle);
632960 86                 saveAvgUlm2(vesicle);
22cdfd 87                 kc1=calculateKc(vesicle, 2,9);
SP 88                 kc2=calculateKc(vesicle, 6,9);
89                 kc3=calculateKc(vesicle, 2,vesicle->sphHarmonics->l);
1665aa 90                 kc4=calculateKc(vesicle, 3,6);
22cdfd 91             
5bb6bb 92                 fd1=fopen("state.dat","w");
M 93                 fprintf(fd1,"%e %e\n",vesicle->volume, getR0(vesicle));
94                 for(k=0;k<vesicle->vlist->n;k++){
95                     fprintf(fd1,"%e %e %e %e %e\n",
96                         vesicle->vlist->vtx[k]->x,
97                         vesicle->vlist->vtx[k]->y,
98                         vesicle->vlist->vtx[k]->z,
99                         vesicle->vlist->vtx[k]->solAngle,
100                         vesicle->vlist->vtx[k]->relR
101                     );
102                 }
103                 fclose(fd1);
5a3862 104         
M 105             fprintf(fd2,"%u ", i);
106             for(l=0;l<vesicle->sphHarmonics->l;l++){
107                 for(m=l;m<2*l+1;m++){
108                     fprintf(fd2,"%e ", gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m]) );
109                 }
110             }
111                 fprintf(fd2,"\n");
112     
113                 fflush(fd2);    
114
632960 115             }
dc77e8 116
1665aa 117             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 118
632960 119             fflush(fd);    
144784 120         //    sprintf(filename,"timestep-%05d.pov",i-inititer);
fe24d2 121         //    write_pov_file(vesicle,filename);
d7a113 122         }
SP 123     }
37d14a 124     fclose(fd);
5a3862 125     if(fd2!=NULL) fclose(fd2);
d7a113 126     return TS_SUCCESS;
SP 127 }
d7639a 128
37d14a 129 ts_bool single_timestep(ts_vesicle *vesicle,ts_double *vmsr, ts_double *bfsr){
3de289 130 //    vesicle_volume(vesicle);
SP 131 //    fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume);
d7639a 132     ts_bool retval;
SP 133     ts_double rnvec[3];
fe5069 134     ts_uint i,j, b;
37d14a 135     ts_uint vmsrcnt=0;
aec47d 136     for(i=0;i<vesicle->vlist->n;i++){
d7639a 137         rnvec[0]=drand48();
SP 138         rnvec[1]=drand48();
139         rnvec[2]=drand48();
aec47d 140         retval=single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec);
37d14a 141     if(retval==TS_SUCCESS) vmsrcnt++;        
d7639a 142     }
SP 143
37d14a 144     ts_int bfsrcnt=0;
fedf2b 145     for(i=0;i<3*vesicle->vlist->n;i++){
fe5069 146     b=rand() % vesicle->blist->n;
d7639a 147         //find a bond and return a pointer to a bond...
SP 148         //call single_bondflip_timestep...
fe5069 149         retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
3de289 150        //     b++; retval=TS_FAIL;
37d14a 151     if(retval==TS_SUCCESS) bfsrcnt++;        
fedf2b 152     }
M 153
154     for(i=0;i<vesicle->poly_list->n;i++){
58230a 155         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
M 156             rnvec[0]=drand48();
157             rnvec[1]=drand48();
158             rnvec[2]=drand48();
159             retval=single_poly_vertex_move(vesicle,vesicle->poly_list->poly[i],vesicle->poly_list->poly[i]->vlist->vtx[j],rnvec);    
160         }
fedf2b 161     }
M 162
58230a 163
M 164     for(i=0;i<vesicle->filament_list->n;i++){
165         for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
166             rnvec[0]=drand48();
167             rnvec[1]=drand48();
168             rnvec[2]=drand48();
169             retval=single_filament_vertex_move(vesicle,vesicle->filament_list->poly[i],vesicle->filament_list->poly[i]->vlist->vtx[j],rnvec);    
170         }
fedf2b 171     }
M 172  
58230a 173
fedf2b 174 //    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 175     *vmsr=(ts_double)vmsrcnt/(ts_double)vesicle->vlist->n;
SP 176     *bfsr=(ts_double)bfsrcnt/(ts_double)vesicle->vlist->n/3.0;
3de289 177 //    vesicle_volume(vesicle);
SP 178 //    fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume);
d7639a 179     return TS_SUCCESS;
SP 180 }
181
182
183