Trisurf Monte Carlo simulator
Samo Penic
2014-11-12 43e534d0d6499cf47569787f2468a6941bd03721
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>
267db5 17 #include<string.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;
c0ae90 22     ts_double l1,l2,l3,vmsr,bfsr, vmsrt, bfsrt;
37d14a 23     ts_ulong epochtime;
5a3862 24     FILE *fd1,*fd2=NULL;
267db5 25      char filename[10000];
SP 26     strcpy(filename,command_line_args.path);
27     strcat(filename,"statistics.csv");
28     FILE *fd=fopen(filename,"w");
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){
267db5 35         strcpy(filename,command_line_args.path);
SP 36         strcat(filename,"ulm2.csv"); 
37         fd2=fopen(filename,"w");
5a3862 38         if(fd2==NULL){
M 39             fatal("Cannot open ulm2.csv file for writing",1);
40         }
41         fprintf(fd2, "Timestep u_00^2 u_10^2 u_11^2 u_20^2 ...\n");    
42
43     }
44
c60a49 45 /* RANDOM SEED SET BY CURRENT TIME */
M 46     epochtime=get_epoch();            
47     srand48(epochtime);
5a3862 48
d7a113 49     centermass(vesicle);
SP 50     cell_occupation(vesicle);
fe5069 51     vesicle_volume(vesicle); //needed for constant volume at this moment
c0ae90 52     vesicle_area(vesicle); //needed for constant area at this moment
1121fa 53     V0=vesicle->volume; 
c0ae90 54     A0=vesicle->area;
a54977 55     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 56     epsarea=A0/(ts_double)vesicle->tlist->n;
a54977 57   //  fprintf(stderr, "DVol=%1.16f (%1.16f), V0=%1.16f\n", epsvol,0.003e-2*V0,V0);
626811 58     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 59     for(i=start_iteration;i<inititer+iterations;i++){
37d14a 60         vmsr=0.0;
SP 61         bfsr=0.0;
3de289 62 /*    vesicle_volume(vesicle);
SP 63     fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume); */
d7a113 64         for(j=0;j<mcsweeps;j++){
37d14a 65             single_timestep(vesicle, &vmsrt, &bfsrt);
SP 66             vmsr+=vmsrt;
67             bfsr+=bfsrt;
d7a113 68         }
3de289 69 /*
SP 70     vesicle_volume(vesicle);
71     fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume); */
37d14a 72         vmsr/=(ts_double)mcsweeps;
SP 73         bfsr/=(ts_double)mcsweeps;
d7a113 74         centermass(vesicle);
SP 75         cell_occupation(vesicle);
f8e6ba 76         ts_fprintf(stdout,"Done %d out of %d iterations (x %d MC sweeps).\n",i+1,inititer+iterations,mcsweeps);
1ab449 77             dump_state(vesicle,i);
58230a 78         if(i>=inititer){
d7a113 79             write_vertex_xml_file(vesicle,i-inititer);
267db5 80             write_master_xml_file(command_line_args.output_fullfilename);
37d14a 81             epochtime=get_epoch();            
SP 82             gyration_eigen(vesicle, &l1, &l2, &l3);
c0ae90 83             vesicle_volume(vesicle); //calculates just volume. 
SP 84             vesicle_area(vesicle); //calculates area.
dc77e8 85             r0=getR0(vesicle);
632960 86             if(vesicle->sphHarmonics!=NULL){
SP 87                 preparationSh(vesicle,r0);
459ff9 88                 //calculateYlmi(vesicle);
SP 89                 calculateUlmComplex(vesicle);
90                 storeUlmComplex2(vesicle);
632960 91                 saveAvgUlm2(vesicle);
22cdfd 92                 kc1=calculateKc(vesicle, 2,9);
SP 93                 kc2=calculateKc(vesicle, 6,9);
94                 kc3=calculateKc(vesicle, 2,vesicle->sphHarmonics->l);
1665aa 95                 kc4=calculateKc(vesicle, 3,6);
267db5 96                 strcpy(filename,command_line_args.path);
SP 97                 strcat(filename,"state.dat");  
98                 fd1=fopen(filename,"w");
5bb6bb 99                 fprintf(fd1,"%e %e\n",vesicle->volume, getR0(vesicle));
M 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
c0ae90 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,vesicle->volume, vesicle->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