Trisurf Monte Carlo simulator
Samo Penic
2018-10-11 b4e2e6dbd5049ae279f0f74838cd21b625148c72
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>
ac9826 19 #include <sys/stat.h>
SP 20
fedf2b 21
626811 22 ts_bool run_simulation(ts_vesicle *vesicle, ts_uint mcsweeps, ts_uint inititer, ts_uint iterations, ts_uint start_iteration){
5a3862 23     ts_uint i, j,k,l,m;
cfab63 24     ts_double r0,kc1=0,kc2=0,kc3=0,kc4=0;
c0ae90 25     ts_double l1,l2,l3,vmsr,bfsr, vmsrt, bfsrt;
37d14a 26     ts_ulong epochtime;
b4e2e6 27     ts_double max_z=0, min_z=0, cm_backup[3];
3d0247 28     FILE *fd1,*fd2=NULL,*fd3=NULL;
267db5 29      char filename[10000];
ac9826 30     //struct stat st;
SP 31     strcpy(filename,command_line_args.path);
32     strcat(filename,"statistics.csv");
33     //int result = stat(filename, &st);
34     FILE *fd;
35     if(start_iteration==0)
36         fd=fopen(filename,"w");
37     else
38         fd=fopen(filename,"a");
37d14a 39     if(fd==NULL){
SP 40         fatal("Cannot open statistics.csv file for writing",1);
41     }
ac9826 42     if(start_iteration==0)
SP 43         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 44
M 45      if(vesicle->sphHarmonics!=NULL){
267db5 46         strcpy(filename,command_line_args.path);
SP 47         strcat(filename,"ulm2.csv"); 
ac9826 48 //    int result = stat(filename, &st);
SP 49     if(start_iteration==0)
267db5 50         fd2=fopen(filename,"w");
ac9826 51     else
SP 52         fd2=fopen(filename,"a");
819a09 53     if(fd2==NULL){
S 54         fatal("Cannot open ulm2.csv file for writing",1);
55     }
56     if(start_iteration==0) //file does not exist
57         fprintf(fd2, "Timestep u_00^2 u_10^2 u_11^2 u_20^2 ...\n");    
5a3862 58     }
M 59
c60a49 60 /* RANDOM SEED SET BY CURRENT TIME */
M 61     epochtime=get_epoch();            
62     srand48(epochtime);
d7a113 63     centermass(vesicle);
SP 64     cell_occupation(vesicle);
fe5069 65     vesicle_volume(vesicle); //needed for constant volume at this moment
c0ae90 66     vesicle_area(vesicle); //needed for constant area at this moment
49981c 67     if(V0<0.000001) 
SP 68         V0=vesicle->volume; 
69     ts_fprintf(stdout,"Setting volume V0=%.17f\n",V0);
70     if(A0<0.000001)
71         A0=vesicle->area;
819a09 72     ts_fprintf(stdout,"Setting area A0=%.17f\n",A0);
a54977 73     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 74     epsarea=A0/(ts_double)vesicle->tlist->n;
5eefcc 75     if(vesicle->tape->plane_confinement_switch){
SP 76                 min_z=1e10;
77                 max_z=-1e10;
78         for(k=0;k<vesicle->vlist->n;k++){
79                     if(vesicle->vlist->vtx[k]->z > max_z) max_z=vesicle->vlist->vtx[k]->z;
80                     if(vesicle->vlist->vtx[k]->z < min_z) min_z=vesicle->vlist->vtx[k]->z;
81                 }
82                 if(max_z-min_z<=vesicle->tape->plane_d) {
83                     vesicle->confinement_plane.z_max=max_z;
84                     vesicle->confinement_plane.z_min=max_z-vesicle->tape->plane_d;
85                 }  else {
86                     vesicle->confinement_plane.z_min=min_z;//-1e-5;
87                     vesicle->confinement_plane.z_max=max_z;//+1e-5;
88                 }
89     }
267cf1 90
a54977 91   //  fprintf(stderr, "DVol=%1.16f (%1.16f), V0=%1.16f\n", epsvol,0.003e-2*V0,V0);
626811 92     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 93     for(i=start_iteration;i<inititer+iterations;i++){
37d14a 94         vmsr=0.0;
SP 95         bfsr=0.0;
267cf1 96
b4e2e6 97
SP 98 /*    vesicle_volume(vesicle);
99     fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume); */
100         for(j=0;j<mcsweeps;j++){
101             single_timestep(vesicle, &vmsrt, &bfsrt);
102             vmsr+=vmsrt;
103             bfsr+=bfsrt;
104         }
105 /*
106     vesicle_volume(vesicle);
107     fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume); */
108         vmsr/=(ts_double)mcsweeps;
109         bfsr/=(ts_double)mcsweeps;
110         for (k=0;k<3;k++) cm_backup[k]=vesicle->cm[k];
111
112         centermass(vesicle);
113         cell_occupation(vesicle);
114
267cf1 115         // plane confinement
SP 116         if(vesicle->tape->plane_confinement_switch){
117             if(vesicle->confinement_plane.z_max-vesicle->confinement_plane.z_min<=vesicle->tape->plane_d){
5eefcc 118                 ts_fprintf(stdout,"Force is off\n");
SP 119             } else {
120                 ts_fprintf(stdout,"Force is on\n");
121             
267cf1 122                 min_z=1e10;
SP 123                 max_z=-1e10;
124                 for(k=0;k<vesicle->vlist->n;k++){
125                     if(vesicle->vlist->vtx[k]->z > max_z) max_z=vesicle->vlist->vtx[k]->z;
126                     if(vesicle->vlist->vtx[k]->z < min_z) min_z=vesicle->vlist->vtx[k]->z;
127                 }
128                 if(max_z-min_z<=vesicle->tape->plane_d) {
b4e2e6 129                     vesicle->confinement_plane.z_max+=(vesicle->cm[2]-cm_backup[2]);    
SP 130                     vesicle->confinement_plane.z_min+=(vesicle->cm[2]-cm_backup[2]);
131                     //vesicle->confinement_plane.z_max=max_z;
132                     //vesicle->confinement_plane.z_min=max_z-vesicle->tape->plane_d;
267cf1 133                 }  else {
5eefcc 134                     vesicle->confinement_plane.z_min=min_z;//-1e-5;
SP 135                     vesicle->confinement_plane.z_max=max_z;//+1e-5;
267cf1 136                 }
5eefcc 137             }
SP 138             ts_fprintf(stdout,"Planes (zmin, zmax)=(%e,%e)\n",vesicle->confinement_plane.z_min,vesicle->confinement_plane.z_max);    
267cf1 139         }
b4e2e6 140         // end plane confinement
267cf1 141
1ab449 142             dump_state(vesicle,i);
58230a 143         if(i>=inititer){
0a2c81 144             write_vertex_xml_file(vesicle,i-inititer,NULL);
267db5 145             write_master_xml_file(command_line_args.output_fullfilename);
37d14a 146             epochtime=get_epoch();            
SP 147             gyration_eigen(vesicle, &l1, &l2, &l3);
c0ae90 148             vesicle_volume(vesicle); //calculates just volume. 
SP 149             vesicle_area(vesicle); //calculates area.
dc77e8 150             r0=getR0(vesicle);
632960 151             if(vesicle->sphHarmonics!=NULL){
SP 152                 preparationSh(vesicle,r0);
459ff9 153                 //calculateYlmi(vesicle);
SP 154                 calculateUlmComplex(vesicle);
155                 storeUlmComplex2(vesicle);
632960 156                 saveAvgUlm2(vesicle);
22cdfd 157                 kc1=calculateKc(vesicle, 2,9);
SP 158                 kc2=calculateKc(vesicle, 6,9);
159                 kc3=calculateKc(vesicle, 2,vesicle->sphHarmonics->l);
1665aa 160                 kc4=calculateKc(vesicle, 3,6);
267db5 161                 strcpy(filename,command_line_args.path);
SP 162                 strcat(filename,"state.dat");  
163                 fd1=fopen(filename,"w");
5bb6bb 164                 fprintf(fd1,"%e %e\n",vesicle->volume, getR0(vesicle));
M 165                 for(k=0;k<vesicle->vlist->n;k++){
166                     fprintf(fd1,"%e %e %e %e %e\n",
167                         vesicle->vlist->vtx[k]->x,
168                         vesicle->vlist->vtx[k]->y,
169                         vesicle->vlist->vtx[k]->z,
170                         vesicle->vlist->vtx[k]->solAngle,
171                         vesicle->vlist->vtx[k]->relR
172                     );
173                 }
174                 fclose(fd1);
5a3862 175         
M 176             fprintf(fd2,"%u ", i);
177             for(l=0;l<vesicle->sphHarmonics->l;l++){
178                 for(m=l;m<2*l+1;m++){
179                     fprintf(fd2,"%e ", gsl_complex_abs2(vesicle->sphHarmonics->ulmComplex[l][m]) );
180                 }
181             }
182                 fprintf(fd2,"\n");
183     
184                 fflush(fd2);    
185
632960 186             }
dc77e8 187
c0ae90 188             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 189
632960 190             fflush(fd);    
144784 191         //    sprintf(filename,"timestep-%05d.pov",i-inititer);
fe24d2 192         //    write_pov_file(vesicle,filename);
49981c 193         } //end if(inititer....)
SP 194         fd3=fopen(".status","w"); //write status file when everything is written to disk.
195         if(fd3==NULL){
196             fatal("Cannot open .status file for writing",1);
d7a113 197         }
49981c 198         fprintf(fd3,"%d",i);
SP 199         fclose(fd3);
200         ts_fprintf(stdout,"Done %d out of %d iterations (x %d MC sweeps).\n",i+1,inititer+iterations,mcsweeps);
d7a113 201     }
37d14a 202     fclose(fd);
5a3862 203     if(fd2!=NULL) fclose(fd2);
d7a113 204     return TS_SUCCESS;
SP 205 }
d7639a 206
37d14a 207 ts_bool single_timestep(ts_vesicle *vesicle,ts_double *vmsr, ts_double *bfsr){
3de289 208 //    vesicle_volume(vesicle);
SP 209 //    fprintf(stderr,"Volume before TS=%1.16e\n", vesicle->volume);
d7639a 210     ts_bool retval;
SP 211     ts_double rnvec[3];
fe5069 212     ts_uint i,j, b;
37d14a 213     ts_uint vmsrcnt=0;
aec47d 214     for(i=0;i<vesicle->vlist->n;i++){
d7639a 215         rnvec[0]=drand48();
SP 216         rnvec[1]=drand48();
217         rnvec[2]=drand48();
aec47d 218         retval=single_verticle_timestep(vesicle,vesicle->vlist->vtx[i],rnvec);
37d14a 219     if(retval==TS_SUCCESS) vmsrcnt++;        
d7639a 220     }
SP 221
37d14a 222     ts_int bfsrcnt=0;
fedf2b 223     for(i=0;i<3*vesicle->vlist->n;i++){
fe5069 224     b=rand() % vesicle->blist->n;
d7639a 225         //find a bond and return a pointer to a bond...
SP 226         //call single_bondflip_timestep...
fe5069 227         retval=single_bondflip_timestep(vesicle,vesicle->blist->bond[b],rnvec);
3de289 228        //     b++; retval=TS_FAIL;
37d14a 229     if(retval==TS_SUCCESS) bfsrcnt++;        
fedf2b 230     }
M 231
232     for(i=0;i<vesicle->poly_list->n;i++){
58230a 233         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
M 234             rnvec[0]=drand48();
235             rnvec[1]=drand48();
236             rnvec[2]=drand48();
237             retval=single_poly_vertex_move(vesicle,vesicle->poly_list->poly[i],vesicle->poly_list->poly[i]->vlist->vtx[j],rnvec);    
238         }
fedf2b 239     }
M 240
58230a 241
M 242     for(i=0;i<vesicle->filament_list->n;i++){
243         for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
244             rnvec[0]=drand48();
245             rnvec[1]=drand48();
246             rnvec[2]=drand48();
247             retval=single_filament_vertex_move(vesicle,vesicle->filament_list->poly[i],vesicle->filament_list->poly[i]->vlist->vtx[j],rnvec);    
248         }
fedf2b 249     }
M 250  
58230a 251
fedf2b 252 //    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 253     *vmsr=(ts_double)vmsrcnt/(ts_double)vesicle->vlist->n;
SP 254     *bfsr=(ts_double)bfsrcnt/(ts_double)vesicle->vlist->n/3.0;
3de289 255 //    vesicle_volume(vesicle);
SP 256 //    fprintf(stderr,"Volume after TS=%1.16e\n", vesicle->volume);
d7639a 257     return TS_SUCCESS;
SP 258 }
259
260
261