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