Trisurf Monte Carlo simulator
Samo Penic
2014-12-16 6bc95bfe19012a33f185adf1476e623d98d33465
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"
fda1ab 14 #include "shreal.h"
dc77e8 15 #include "vesicle.h"
5a3862 16 #include<gsl/gsl_complex.h>
M 17 #include<gsl/gsl_complex_math.h>
267db5 18 #include<string.h>
fedf2b 19
626811 20 ts_bool run_simulation(ts_vesicle *vesicle, ts_uint mcsweeps, ts_uint inititer, ts_uint iterations, ts_uint start_iteration){
5a3862 21     ts_uint i, j,k,l,m;
1665aa 22     ts_double r0,kc1,kc2,kc3,kc4;
c0ae90 23     ts_double l1,l2,l3,vmsr,bfsr, vmsrt, bfsrt;
37d14a 24     ts_ulong epochtime;
fda1ab 25     ts_double diff;
5a3862 26     FILE *fd1,*fd2=NULL;
267db5 27      char filename[10000];
SP 28     strcpy(filename,command_line_args.path);
29     strcat(filename,"statistics.csv");
30     FILE *fd=fopen(filename,"w");
37d14a 31     if(fd==NULL){
SP 32         fatal("Cannot open statistics.csv file for writing",1);
33     }
1665aa 34     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 35
M 36      if(vesicle->sphHarmonics!=NULL){
267db5 37         strcpy(filename,command_line_args.path);
SP 38         strcat(filename,"ulm2.csv"); 
39         fd2=fopen(filename,"w");
5a3862 40         if(fd2==NULL){
M 41             fatal("Cannot open ulm2.csv file for writing",1);
42         }
43         fprintf(fd2, "Timestep u_00^2 u_10^2 u_11^2 u_20^2 ...\n");    
44
45     }
46
c60a49 47 /* RANDOM SEED SET BY CURRENT TIME */
M 48     epochtime=get_epoch();            
49     srand48(epochtime);
5a3862 50
d7a113 51     centermass(vesicle);
SP 52     cell_occupation(vesicle);
fe5069 53     vesicle_volume(vesicle); //needed for constant volume at this moment
c0ae90 54     vesicle_area(vesicle); //needed for constant area at this moment
1121fa 55     V0=vesicle->volume; 
c0ae90 56     A0=vesicle->area;
a54977 57     epsvol=4.0*sqrt(2.0*M_PI)/pow(3.0,3.0/4.0)*V0/pow(vesicle->tlist->n,3.0/2.0);
c0ae90 58     epsarea=A0/(ts_double)vesicle->tlist->n;
a54977 59   //  fprintf(stderr, "DVol=%1.16f (%1.16f), V0=%1.16f\n", epsvol,0.003e-2*V0,V0);
626811 60     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 61     for(i=start_iteration;i<inititer+iterations;i++){
37d14a 62         vmsr=0.0;
SP 63         bfsr=0.0;
3de289 64 /*    vesicle_volume(vesicle);
SP 65     fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume); */
d7a113 66         for(j=0;j<mcsweeps;j++){
37d14a 67             single_timestep(vesicle, &vmsrt, &bfsrt);
SP 68             vmsr+=vmsrt;
69             bfsr+=bfsrt;
d7a113 70         }
3de289 71 /*
SP 72     vesicle_volume(vesicle);
73     fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume); */
37d14a 74         vmsr/=(ts_double)mcsweeps;
SP 75         bfsr/=(ts_double)mcsweeps;
d7a113 76         centermass(vesicle);
SP 77         cell_occupation(vesicle);
f8e6ba 78         ts_fprintf(stdout,"Done %d out of %d iterations (x %d MC sweeps).\n",i+1,inititer+iterations,mcsweeps);
1ab449 79             dump_state(vesicle,i);
58230a 80         if(i>=inititer){
d7a113 81             write_vertex_xml_file(vesicle,i-inititer);
267db5 82             write_master_xml_file(command_line_args.output_fullfilename);
37d14a 83             epochtime=get_epoch();            
SP 84             gyration_eigen(vesicle, &l1, &l2, &l3);
c0ae90 85             vesicle_volume(vesicle); //calculates just volume. 
SP 86             vesicle_area(vesicle); //calculates area.
dc77e8 87             r0=getR0(vesicle);
632960 88             if(vesicle->sphHarmonics!=NULL){
SP 89                 preparationSh(vesicle,r0);
459ff9 90                 //calculateYlmi(vesicle);
SP 91                 calculateUlmComplex(vesicle);
92                 storeUlmComplex2(vesicle);
fda1ab 93                 saveAvgUlm2Complex(vesicle);
SP 94                 calculateUlmReal(vesicle);
95                 storeUlm2Real(vesicle);
96                 saveAvgUlm2Real(vesicle);
22cdfd 97                 kc1=calculateKc(vesicle, 2,9);
SP 98                 kc2=calculateKc(vesicle, 6,9);
99                 kc3=calculateKc(vesicle, 2,vesicle->sphHarmonics->l);
1665aa 100                 kc4=calculateKc(vesicle, 3,6);
267db5 101                 strcpy(filename,command_line_args.path);
SP 102                 strcat(filename,"state.dat");  
103                 fd1=fopen(filename,"w");
5bb6bb 104                 fprintf(fd1,"%e %e\n",vesicle->volume, getR0(vesicle));
M 105                 for(k=0;k<vesicle->vlist->n;k++){
106                     fprintf(fd1,"%e %e %e %e %e\n",
107                         vesicle->vlist->vtx[k]->x,
108                         vesicle->vlist->vtx[k]->y,
109                         vesicle->vlist->vtx[k]->z,
110                         vesicle->vlist->vtx[k]->solAngle,
111                         vesicle->vlist->vtx[k]->relR
112                     );
113                 }
114                 fclose(fd1);
5a3862 115         
M 116             fprintf(fd2,"%u ", i);
117             for(l=0;l<vesicle->sphHarmonics->l;l++){
118                 for(m=l;m<2*l+1;m++){
119                     fprintf(fd2,"%e ", gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m]) );
fda1ab 120                     if(l<5) {
SP 121                          if(m==l) diff=  gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m])-pow(vesicle->sphHarmonics->ulmReal[l][m],2);
122                          else     diff=  gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m])-(pow(vesicle->sphHarmonics->ulmReal[l][m],2)+pow(vesicle->sphHarmonics->ulmReal[l][2*l-m],2))/2.0;       
123                         fprintf(stderr,"%e ", diff/gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m]) );
124                     }
5a3862 125                 }
fda1ab 126                 if(l<5) fprintf(stderr,"\n");
5a3862 127             }
M 128                 fprintf(fd2,"\n");
fda1ab 129                 fprintf(stderr,"---\n");
5a3862 130     
M 131                 fflush(fd2);    
132
632960 133             }
dc77e8 134
c0ae90 135             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,vesicle->volume, vesicle->area,l1,l2,l3,kc1, kc2, kc3,kc4);
5a3862 136
632960 137             fflush(fd);    
144784 138         //    sprintf(filename,"timestep-%05d.pov",i-inititer);
fe24d2 139         //    write_pov_file(vesicle,filename);
d7a113 140         }
SP 141     }
37d14a 142     fclose(fd);
5a3862 143     if(fd2!=NULL) fclose(fd2);
d7a113 144     return TS_SUCCESS;
SP 145 }
d7639a 146
37d14a 147 ts_bool single_timestep(ts_vesicle *vesicle,ts_double *vmsr, ts_double *bfsr){
3de289 148 //    vesicle_volume(vesicle);
SP 149 //    fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume);
d7639a 150     ts_bool retval;
SP 151     ts_double rnvec[3];
fe5069 152     ts_uint i,j, b;
37d14a 153     ts_uint vmsrcnt=0;
aec47d 154     for(i=0;i<vesicle->vlist->n;i++){
d7639a 155         rnvec[0]=drand48();
SP 156         rnvec[1]=drand48();
157         rnvec[2]=drand48();
aec47d 158         retval=single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec);
37d14a 159     if(retval==TS_SUCCESS) vmsrcnt++;        
d7639a 160     }
SP 161
37d14a 162     ts_int bfsrcnt=0;
fedf2b 163     for(i=0;i<3*vesicle->vlist->n;i++){
fe5069 164     b=rand() % vesicle->blist->n;
d7639a 165         //find a bond and return a pointer to a bond...
SP 166         //call single_bondflip_timestep...
fe5069 167         retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
3de289 168        //     b++; retval=TS_FAIL;
37d14a 169     if(retval==TS_SUCCESS) bfsrcnt++;        
fedf2b 170     }
M 171
172     for(i=0;i<vesicle->poly_list->n;i++){
58230a 173         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
M 174             rnvec[0]=drand48();
175             rnvec[1]=drand48();
176             rnvec[2]=drand48();
177             retval=single_poly_vertex_move(vesicle,vesicle->poly_list->poly[i],vesicle->poly_list->poly[i]->vlist->vtx[j],rnvec);    
178         }
fedf2b 179     }
M 180
58230a 181
M 182     for(i=0;i<vesicle->filament_list->n;i++){
183         for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
184             rnvec[0]=drand48();
185             rnvec[1]=drand48();
186             rnvec[2]=drand48();
187             retval=single_filament_vertex_move(vesicle,vesicle->filament_list->poly[i],vesicle->filament_list->poly[i]->vlist->vtx[j],rnvec);    
188         }
fedf2b 189     }
M 190  
58230a 191
fedf2b 192 //    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 193     *vmsr=(ts_double)vmsrcnt/(ts_double)vesicle->vlist->n;
SP 194     *bfsr=(ts_double)bfsrcnt/(ts_double)vesicle->vlist->n/3.0;
3de289 195 //    vesicle_volume(vesicle);
SP 196 //    fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume);
d7639a 197     return TS_SUCCESS;
SP 198 }
199
200
201