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