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