Trisurf Monte Carlo simulator
mihaf
2014-03-07 b866cf902b92a94fb438e40a24dae0a89fa2afec
commit | author | age
f74313 1 #include "general.h"
d7639a 2 #include<stdio.h>
SP 3 #include "io.h"
f74313 4 #include <confuse.h>
SP 5 #include "vertex.h"
6 #include "bond.h"
d7639a 7 #include<string.h>
a6b1b5 8 #include<stdlib.h>
d7639a 9 #include <sys/types.h>
SP 10 #include <dirent.h>
b14a8d 11 #include "initial_distribution.h"
a2db52 12 #include "poly.h"
d7639a 13
SP 14 ts_bool print_vertex_list(ts_vertex_list *vlist){
15     ts_uint i;
16     printf("Number of vertices: %u\n",vlist->n);
17     for(i=0;i<vlist->n;i++){
a6b1b5 18         printf("%u: %f %f %f\n",
8f6a69 19 vlist->vtx[i]->idx,vlist->vtx[i]->x, vlist->vtx[i]->y, vlist->vtx[i]->z);
d7639a 20     }
SP 21     return TS_SUCCESS;
22 }
23
24 ts_bool print_vertex_neighbours(ts_vertex_list *vlist){
25     ts_uint i,j;
a6b1b5 26     ts_vertex **vtx=vlist->vtx;
d7639a 27     printf("Vertex id(neigh no): (neighvertex coord) (neighvertex coord) ...\n");
SP 28     for(i=0;i<vlist->n;i++){
8f6a69 29         printf("%u(%u): ",vtx[i]->idx,vtx[i]->neigh_no);
SP 30         for(j=0;j<vtx[i]->neigh_no;j++){
31             printf("(%f,%f,%f)",vtx[i]->neigh[j]->x,
32 vtx[i]->neigh[j]->y,vtx[i]->neigh[j]->z);
d7639a 33         }
SP 34         printf("\n");
35     }
36
37 return TS_SUCCESS;
38 }
39
40 ts_bool write_vertex_fcompat_file(ts_vertex_list *vlist,ts_char *filename){
a6b1b5 41     ts_vertex **vtx=vlist->vtx;
d7639a 42     ts_uint i;
SP 43     FILE *fh;
44     
45     fh=fopen(filename, "w");
46     if(fh==NULL){
47         err("Cannot open file %s for writing");
48         return TS_FAIL;
49     }
50     for(i=0;i<vlist->n;i++)
8f6a69 51         fprintf(fh," %E\t%E\t%E\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 52
SP 53     fclose(fh);
54 return TS_SUCCESS;
55 }
56
57
58 ts_bool fprint_vertex_list(FILE *fh,ts_vertex_list *vlist){
59     ts_uint i,j;
60     for(i=0;i<vlist->n;i++){
8f6a69 61         fprintf(fh," %.17E\t%.17E\t%.17E\t%u\n",vlist->vtx[i]->x,
SP 62             vlist->vtx[i]->y, vlist->vtx[i]->z,
63             vlist->vtx[i]->neigh_no);
64         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
65             fprintf(fh,"\t%u",(ts_uint)(vlist->vtx[i]->neigh[j]->idx));
a6b1b5 66         //-vlist->vtx+1));
d7639a 67         }
SP 68         fprintf(fh,"\n");
69     }
70     return TS_SUCCESS;
71 }
72
73 ts_bool fprint_tristar(FILE *fh, ts_vesicle *vesicle){
74     ts_uint i,j;
a6b1b5 75     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 76         fprintf(fh,"\t%u",vesicle->vlist->vtx[i]->tristar_no);
SP 77         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
78             fprintf(fh,"\t%u",(ts_uint)(vesicle->vlist->vtx[i]->tristar[j]->idx));//-vesicle->tlist->tria+1));
d7639a 79         }
SP 80         fprintf(fh,"\n");
81     }
82     return TS_SUCCESS;
83 }
84
85 ts_bool fprint_triangle_list(FILE *fh, ts_vesicle *vesicle){
a6b1b5 86         ts_triangle_list *tlist=vesicle->tlist;
d7639a 87       ts_uint i,j;
SP 88     for(i=0;i<tlist->n;i++){
41a035 89         fprintf(fh,"\t%u",tlist->tria[i]->neigh_no);
SP 90         for(j=0;j<tlist->tria[i]->neigh_no;j++){
91             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->neigh[j]->idx));//-tlist->tria+1)); 
d7639a 92         }
SP 93         fprintf(fh,"\n");
94             for(j=0;j<3;j++){
41a035 95             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->vertex[j]->idx));//-vesicle->vlist->vtx+1)); 
d7639a 96             }
SP 97         fprintf(fh,"\n");
41a035 98         fprintf(fh,"%.17E\t%.17E\t%.17E\n",tlist->tria[i]->xnorm,
SP 99 tlist->tria[i]->ynorm,tlist->tria[i]->znorm);
d7639a 100         fprintf(fh,"0.00000000000000000\n0.00000000000000000\n");
SP 101     }
102     return TS_SUCCESS;
103 }
104
105 ts_bool fprint_vertex_data(FILE *fh,ts_vertex_list *vlist){
106     ts_uint i,j;
107     for(i=0;i<vlist->n;i++){
108         fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
8f6a69 109         vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
SP 110         vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
111         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
112             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
d7639a 113         }
SP 114             fprintf(fh,"\n");
8f6a69 115         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
SP 116             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
d7639a 117         }
SP 118             fprintf(fh,"\n");
119     }
120     return TS_SUCCESS;
121 }
122
123 ts_bool fprint_bonds(FILE *fh,ts_vesicle *vesicle){
124     ts_uint i;
a6b1b5 125     for(i=0;i<vesicle->blist->n;i++){
e016c4 126         fprintf(fh,"\t%u\t%u\n",(ts_uint)(vesicle->blist->bond[i]->vtx1->idx),
a6b1b5 127 //-vesicle->vlist->vtx+1),
e016c4 128         (ts_uint)(vesicle->blist->bond[i]->vtx2->idx));
a6b1b5 129     //-vesicle->vlist.vtx+1));
d7639a 130     }
SP 131     return TS_SUCCESS;
132 }
133
134
135 ts_bool write_dout_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
136     FILE *fh;
137     fh=fopen(filename, "w");
138     if(fh==NULL){
139         err("Cannot open file %s for writing");
140         return TS_FAIL;
141     }
142     fprintf(fh,"%.17E\n%.17E\n",vesicle->stepsize,vesicle->dmax);
a6b1b5 143     fprint_vertex_list(fh,vesicle->vlist);
d7639a 144     fprint_tristar(fh,vesicle);
SP 145     fprint_triangle_list(fh,vesicle);
a6b1b5 146     fprint_vertex_data(fh,vesicle->vlist);
d7639a 147     fprint_bonds(fh,vesicle);
SP 148     fclose(fh);    
149     return TS_SUCCESS;
150 }
151
152 ts_bool read_tape_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
153     FILE *fh;
154     char line[255];
155     fh=fopen(filename, "r");
156         if(fh==NULL){
157                 err("Cannot open file for reading... Nonexistant file?");
158                 return TS_FAIL;
159         }
a6b1b5 160     ts_uint retval=1;
d7639a 161     while(retval!=EOF){
SP 162         retval=fscanf(fh,"%s",line);
163         
164         fprintf(stderr,"%s",line);
165     }    
166     fclose(fh);    
167     return TS_SUCCESS;
168 }
169
170 ts_bool write_master_xml_file(ts_char *filename){
171      FILE *fh;
172     ts_char *i,*j;
173     ts_uint tstep;
174     ts_char *number;
175         fh=fopen(filename, "w");
176         if(fh==NULL){
177                 err("Cannot open file %s for writing");
178                 return TS_FAIL;
179         }
180
181     fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
182     DIR *dir = opendir(".");
183     if(dir){
184         struct dirent *ent;
185         tstep=0;
186         while((ent = readdir(dir)) != NULL)
187         {
188             i=rindex(ent->d_name,'.');
189             if(i==NULL) continue;
190             if(strcmp(i+1,"vtu")==0){
191                     j=rindex(ent->d_name,'_');
192                     if(j==NULL) continue;
193                     number=strndup(j+1,j-i); 
a6b1b5 194                 fprintf(fh,"<DataSet timestep=\"%u\" group=\"\" part=\"0\" file=\"%s\"/>\n",atoi(number),ent->d_name);
d7639a 195                 tstep++;
SP 196                     free(number);
197             }  
198         }
199     }
f74313 200     free(dir);
d7639a 201     fprintf(fh,"</Collection>\n</VTKFile>\n");
SP 202     fclose(fh);
203     return TS_SUCCESS;
204 }
205
206 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno){
a6b1b5 207     ts_vertex_list *vlist=vesicle->vlist;
SP 208     ts_bond_list *blist=vesicle->blist;
209     ts_vertex **vtx=vlist->vtx;
40aa5b 210     ts_uint i,j;
d7639a 211         char filename[255];
SP 212     FILE *fh;
213
214         sprintf(filename,"timestep_%.6u.vtu",timestepno);
215     fh=fopen(filename, "w");
216     if(fh==NULL){
217         err("Cannot open file %s for writing");
218         return TS_FAIL;
219     }
220     /* Here comes header of the file */
40aa5b 221
SP 222     //find number of extra vtxs and bonds of polymeres
223     ts_uint monono=0, polyno=0;
224     ts_bool poly=0;
225     if(vesicle->poly_list!=NULL){
226         if(vesicle->poly_list->poly[0]!=NULL){
227         polyno=vesicle->poly_list->n;
228         monono=vesicle->poly_list->poly[0]->vlist->n;
229         poly=1;
230         }
231     }
d7639a 232     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n <UnstructuredGrid>\n");
40aa5b 233     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno, blist->n+monono*polyno);
d7639a 234     fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
SP 235        for(i=0;i<vlist->n;i++){
a6b1b5 236         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 237     }
40aa5b 238     //polymeres
SP 239     if(poly){
240         for(i=0;i<vesicle->poly_list->n;i++){
241             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
242                 fprintf(fh,"%u ", vesicle->poly_list->poly[i]->vlist->vtx[j]->idx);
243             }
244         }
245     }
d7639a 246
SP 247     fprintf(fh,"</DataArray>\n</PointData>\n<CellData>\n</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
248     for(i=0;i<vlist->n;i++){
8f6a69 249         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 250     }
SP 251     //polymeres
252     if(poly){
253         for(i=0;i<vesicle->poly_list->n;i++){
254             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
255                 fprintf(fh,"%e %e %e\n", vesicle->poly_list->poly[i]->vlist->vtx[j]->x,vesicle->poly_list->poly[i]->vlist->vtx[j]->y, vesicle->poly_list->poly[i]->vlist->vtx[j]->z );
256             }
257         }
d7639a 258     }
SP 259
260     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
261     for(i=0;i<blist->n;i++){
e016c4 262             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 263     }
40aa5b 264     //polymeres
SP 265     if(poly){
266         for(i=0;i<vesicle->poly_list->n;i++){
267             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
268                 fprintf(fh,"%u %u\n", vesicle->poly_list->poly[i]->blist->bond[j]->vtx1->idx,vesicle->poly_list->poly[i]->blist->bond[j]->vtx2->idx);
269             }
270     //grafted bonds
271         fprintf(fh,"%u %u\n", vesicle->poly_list->poly[i]->grafted_vtx->idx, vesicle->poly_list->poly[i]->vlist->vtx[0]->idx);
272         }
273
274     }
275     
276
d7639a 277     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
40aa5b 278     for (i=2;i<(blist->n+monono*polyno)*2+1;i+=2){
d7639a 279     fprintf(fh,"%u ",i);
SP 280     }
281     fprintf(fh,"\n");
282     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
40aa5b 283      for (i=0;i<blist->n+monono*polyno;i++){
d7639a 284         fprintf(fh,"3 ");
SP 285     }
286
287     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
288     fclose(fh);
289     return TS_SUCCESS;
290
291 }
292
293 ts_bool write_vertex_vtk_file(ts_vesicle *vesicle,ts_char *filename, ts_char *text){
a6b1b5 294     ts_vertex_list *vlist=vesicle->vlist;
SP 295     ts_bond_list *blist=vesicle->blist;
296     ts_vertex **vtx=vlist->vtx;
297     ts_uint i;
d7639a 298     FILE *fh;
SP 299     
300     fh=fopen(filename, "w");
301     if(fh==NULL){
302         err("Cannot open file %s for writing");
303         return TS_FAIL;
304     }
305     /* Here comes header of the file */
306 //    fprintf(stderr,"NSHELL=%u\n",nshell);
307     fprintf(fh, "# vtk DataFile Version 2.0\n");
308     /* TODO: Do a sanity check on text. Max 255 char, must not me \n terminated */ 
309     fprintf(fh, "%s\n", text);
310     fprintf(fh,"ASCII\n");
311     fprintf(fh,"DATASET UNSTRUCTURED_GRID\n");
312     fprintf(fh,"POINTS %u double\n", vlist->n);
313     for(i=0;i<vlist->n;i++){
8f6a69 314         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 315     }
SP 316     
317     fprintf(fh,"CELLS %u %u\n",blist->n,3*blist->n);
318     for(i=0;i<blist->n;i++){
e016c4 319             fprintf(fh,"2 %u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 320     }
SP 321     fprintf(fh,"CELL_TYPES %u\n",blist->n);
322     for(i=0;i<blist->n;i++)
323         fprintf(fh,"3\n");
324
325     fprintf(fh,"POINT_DATA %u\n", vlist->n);
326     fprintf(fh,"SCALARS scalars long 1\n");
327     fprintf(fh,"LOOKUP_TABLE default\n");
328
329     for(i=0;i<vlist->n;i++)
8f6a69 330         fprintf(fh,"%u\n",vtx[i]->idx);
d7639a 331
SP 332     fclose(fh);
333     return TS_SUCCESS;
334 }
335
336
337
d7a113 338 ts_vesicle *parsetape(ts_uint *mcsweeps, ts_uint *inititer, ts_uint *iterations){
b866cf 339     long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20, pswitch=0;  // THIS IS DUE TO CONFUSE BUG!
b14a8d 340     char *buf=malloc(255*sizeof(char));
SP 341     long int brezveze0=1;
342     long int brezveze1=1;
343     long int brezveze2=1;
e86357 344     ts_double xk0=25.0, dmax=1.67,stepsize=0.15,kspring=800.0,pressure=0.0;
d7a113 345     long int iter=1000, init=1000, mcsw=1000;
40aa5b 346
SP 347
d7639a 348     cfg_opt_t opts[] = {
SP 349         CFG_SIMPLE_INT("nshell", &nshell),
a2db52 350         CFG_SIMPLE_INT("npoly", &npoly),
SP 351         CFG_SIMPLE_INT("nmono", &nmono),
d7639a 352         CFG_SIMPLE_FLOAT("dmax", &dmax),
SP 353         CFG_SIMPLE_FLOAT("xk0",&xk0),
b866cf 354     CFG_SIMPLE_INT("pswitch",&pswitch),
e86357 355     CFG_SIMPLE_FLOAT("pressure",&pressure),
M 356     CFG_SIMPLE_FLOAT("k_spring",&kspring),
d7639a 357         CFG_SIMPLE_FLOAT("stepsize",&stepsize),
SP 358         CFG_SIMPLE_INT("nxmax", &ncxmax),
359         CFG_SIMPLE_INT("nymax", &ncymax),
360         CFG_SIMPLE_INT("nzmax", &nczmax),
d7a113 361         CFG_SIMPLE_INT("iterations",&iter),
SP 362     CFG_SIMPLE_INT("mcsweeps",&mcsw),
363     CFG_SIMPLE_INT("inititer", &init),
d7639a 364         CFG_SIMPLE_BOOL("quiet",&quiet),
314f2d 365         CFG_SIMPLE_STR("multiprocessing",buf),
b14a8d 366         CFG_SIMPLE_INT("smp_cores",&brezveze0),
SP 367         CFG_SIMPLE_INT("cluster_nodes",&brezveze1),
368         CFG_SIMPLE_INT("distributed_processes",&brezveze2),
d7639a 369         CFG_END()
SP 370     };
371     cfg_t *cfg;    
372     ts_uint retval;
373     cfg = cfg_init(opts, 0);
a6b1b5 374     retval=cfg_parse(cfg, "tape");
d7639a 375     if(retval==CFG_FILE_ERROR){
a6b1b5 376     fatal("No tape file.",100);
d7639a 377     }
SP 378     else if(retval==CFG_PARSE_ERROR){
379     fatal("Invalid tape!",100);
380     }
b14a8d 381     ts_vesicle *vesicle;
d7a113 382         *iterations=iter;
SP 383     *inititer=init;
384     *mcsweeps=mcsw;
b14a8d 385     vesicle=initial_distribution_dipyramid(nshell,ncxmax,ncymax,nczmax,stepsize);
a2db52 386     vesicle->poly_list=init_poly_list(npoly,nmono, vesicle->vlist);
48bb92 387     vesicle->spring_constant=kspring;
M 388     poly_assign_spring_const(vesicle);
389     
a2db52 390
d7639a 391     vesicle->nshell=nshell;
SP 392     vesicle->dmax=dmax*dmax;
393     vesicle->bending_rigidity=xk0;
e86357 394     vtx_set_global_values(vesicle); //copies xk0 to every vertex
M 395
396
d7639a 397     vesicle->stepsize=stepsize;
a6b1b5 398     vesicle->clist->ncmax[0]=ncxmax;
SP 399     vesicle->clist->ncmax[1]=ncymax;
400     vesicle->clist->ncmax[2]=nczmax;
401     vesicle->clist->max_occupancy=8;
e86357 402     vesicle->pressure=pressure/vesicle->bending_rigidity;    //all energy contributions need to be divided by bending_rigidity!
b866cf 403     vesicle->pswitch=pswitch;
d7639a 404     cfg_free(cfg);
b14a8d 405     free(buf);
SP 406   //  fprintf(stderr,"NSHELL=%u\n",vesicle->nshell);
40aa5b 407
SP 408             
409
b14a8d 410     return vesicle;
d7639a 411
SP 412 }
f74313 413
SP 414
415 ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
416     FILE *fh;
417     ts_uint i, nvtx,nedges,ntria;
418     ts_uint vtxi1,vtxi2;
419     float x,y,z;
420     ts_vertex_list *vlist;
421     fh=fopen(fname, "r");
422         if(fh==NULL){
423                 err("Cannot open file for reading... Nonexistant file?");
424                 return TS_FAIL;
425         }
426     ts_uint retval;
427     retval=fscanf(fh,"%u %u %u",&nvtx, &nedges, &ntria);
428     vesicle->vlist=init_vertex_list(nvtx);
429     vlist=vesicle->vlist;
430     for(i=0;i<nvtx;i++){
8f6a69 431    //     fscanf(fh,"%F %F %F",&vlist->vtx[i]->x,&vlist->vtx[i]->y,&vlist->vtx[i]->z);
f74313 432        retval=fscanf(fh,"%F %F %F",&x,&y,&z);
8f6a69 433         vlist->vtx[i]->x=x;
SP 434         vlist->vtx[i]->y=y;
435         vlist->vtx[i]->z=z;
f74313 436     }
SP 437     for(i=0;i<nedges;i++){
438         retval=fscanf(fh,"%u %u",&vtxi1,&vtxi2);
439         bond_add(vesicle->blist,vesicle->vlist->vtx[vtxi1-1],vesicle->vlist->vtx[vtxi2-1]);
440     }
441     //TODO: neighbours from bonds,
442     //TODO: triangles from neigbours
443
444 //    Don't need to read triangles. Already have enough data
445     /*
446     for(i=0;i<ntria;i++){
447         retval=fscanf(fh,"%u %u %u", &bi1, &bi2, &bi3);
8f6a69 448         vtxi1=vesicle->blist->vertex1->idx;
SP 449         vtxi2=vesicle->blist->vertex1->idx;
f74313 450         
SP 451     }
452     */
41a035 453     if(retval);
f74313 454     fclose(fh);    
SP 455
456
457
458     return TS_SUCCESS;
459 }