Trisurf Monte Carlo simulator
Samo Penic
2014-03-08 62681163a4c47fc6dbd6b2b96f84171b20cba3ed
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
083e03 14 #include <getopt.h>
SP 15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <errno.h>
e9eab4 19 /** DUMP STATE TO DISK DRIVE **/
SP 20
1ab449 21 ts_bool dump_state(ts_vesicle *vesicle, ts_uint iteration){
e9eab4 22
SP 23     /* save current state with wrong pointers. Will fix that later */
24     ts_uint i,j,k;
25     FILE *fh=fopen("dump.bin","wb");
26
27     /* dump vesicle */
86f5e7 28     fwrite(vesicle, sizeof(ts_vesicle),1,fh);
e9eab4 29     /* dump vertex list */
SP 30     fwrite(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
31     /* dump bond list */
32     fwrite(vesicle->blist, sizeof(ts_bond_list),1,fh);
33     /* dump triangle list */
34     fwrite(vesicle->tlist, sizeof(ts_triangle_list),1,fh);
35     /* dump cell list */
36     fwrite(vesicle->clist, sizeof(ts_cell_list),1,fh);
37     /* dump poly list */
38     fwrite(vesicle->poly_list, sizeof(ts_poly_list),1,fh);
39     /* level 1 complete */
40
41     /*dump vertices*/
42     for(i=0;i<vesicle->vlist->n;i++){
43         fwrite(vesicle->vlist->vtx[i],sizeof(ts_vertex),1,fh);
44         /* dump pointer offsets for:
45                     neigh
46                     bond
47                     tria
48                     cell is ignored
49         */
50         for(j=0;j<vesicle->vlist->vtx[i]->neigh_no;j++){
3c1ac1 51             fwrite(&vesicle->vlist->vtx[i]->neigh[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 52         }
SP 53         for(j=0;j<vesicle->vlist->vtx[i]->bond_no;j++){
3c1ac1 54             fwrite(&vesicle->vlist->vtx[i]->bond[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 55         }
SP 56         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
3c1ac1 57             fwrite(&vesicle->vlist->vtx[i]->tristar[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 58         }
SP 59     }
60
61     /*dump bonds*/
62     for(i=0;i<vesicle->blist->n;i++){
63         fwrite(vesicle->blist->bond[i],sizeof(ts_bond),1,fh);
64         /* dump pointer offsets for vtx1 and vtx2 */
3c1ac1 65         //off=(ts_ulong)(vesicle->blist->bond[i]->vtx1-vesicle->vlist->vtx[0]);
SP 66         fwrite(&vesicle->blist->bond[i]->vtx1->idx,sizeof(ts_uint),1,fh); 
67         //off=(ts_ulong)(vesicle->blist->bond[i]->vtx2-vesicle->vlist->vtx[0]);
68         fwrite(&vesicle->blist->bond[i]->vtx2->idx,sizeof(ts_uint),1,fh); 
e9eab4 69     }
SP 70
71     /*dump triangles*/
72     for(i=0;i<vesicle->tlist->n;i++){
73         fwrite(vesicle->tlist->tria[i],sizeof(ts_triangle),1,fh);
74         /* dump pointer offsets for vertex */
3c1ac1 75         fwrite(&vesicle->tlist->tria[i]->vertex[0]->idx,sizeof(ts_uint),1,fh); 
SP 76         fwrite(&vesicle->tlist->tria[i]->vertex[1]->idx,sizeof(ts_uint),1,fh); 
77         fwrite(&vesicle->tlist->tria[i]->vertex[2]->idx,sizeof(ts_uint),1,fh); 
e9eab4 78         /* dump pointer offsets for neigh */
SP 79         for(j=0;j<vesicle->tlist->tria[i]->neigh_no;j++){
3c1ac1 80             fwrite(&vesicle->tlist->tria[i]->neigh[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 81         }
SP 82     }
83
84
85     /*dump polymeres */
86     for(i=0;i<vesicle->poly_list->n;i++){
87         fwrite(vesicle->poly_list->poly[i],sizeof(ts_poly),1,fh);
3c1ac1 88         fwrite(vesicle->poly_list->poly[i]->vlist,sizeof(ts_vertex_list),1,fh);
SP 89         fwrite(vesicle->poly_list->poly[i]->blist,sizeof(ts_bond_list),1,fh);
e9eab4 90     } 
SP 91      
92     /* dump poly vertex(monomer) list*/
93     for(i=0;i<vesicle->poly_list->n;i++){
94         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
95             fwrite(vesicle->poly_list->poly[i]->vlist->vtx[j],sizeof(ts_vertex),1,fh);
96             /* dump offset for neigh and bond */
97             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh_no;k++){
3c1ac1 98                // off=(ts_ulong)(vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]-vesicle->poly_list->poly[i]->vlist->vtx[0]);
SP 99                 fwrite(&vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]->idx,sizeof(ts_uint),1,fh); 
e9eab4 100             }
SP 101             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->bond_no;k++){
3c1ac1 102                 //off=(ts_ulong)(vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]-vesicle->poly_list->poly[i]->blist->bond[0]);
SP 103                 fwrite(&vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]->idx,sizeof(ts_uint),1,fh); 
e9eab4 104             }
SP 105         }
3c1ac1 106     // grafted vtx on vesicle data dump
SP 107         fwrite(&vesicle->poly_list->poly[i]->grafted_vtx->idx, sizeof(ts_uint),1,fh);
e9eab4 108     }
SP 109     /* dump poly bonds between monomers list*/
110     for(i=0;i<vesicle->poly_list->n;i++){
111         for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
112             fwrite(vesicle->poly_list->poly[i]->blist->bond[j],sizeof(ts_bond),1,fh);
113             /* dump vtx1 and vtx2 offsets */
3c1ac1 114             //off=(ts_ulong)(vesicle->poly_list->poly[i]->blist->bond[j]->vtx1-vesicle->poly_list->poly[i]->vlist->vtx[0]);
SP 115             fwrite(&vesicle->poly_list->poly[i]->blist->bond[j]->vtx1->idx,sizeof(ts_uint),1,fh); 
116 //            off=(ts_ulong)(vesicle->poly_list->poly[i]->blist->bond[j]->vtx2-vesicle->poly_list->poly[i]->vlist->vtx[0]);
117             fwrite(&vesicle->poly_list->poly[i]->blist->bond[j]->vtx2->idx,sizeof(ts_uint),1,fh); 
e9eab4 118         }
SP 119     }
120
121 /* pointer offsets for fixing the restored pointers */
122 /* need pointers for 
123     vlist->vtx
124     blist->bond
125     tlist->tria
126     clist->cell
127     poly_list->poly
128     and for each poly:
129         poly_list->poly->vtx
130         poly_list->poly->bond
131 */
132
063289 133     fwrite(vesicle->clist, sizeof(ts_cell_list),1,  fh);
1ab449 134     
SP 135     fwrite(&iteration, sizeof(ts_uint),1,fh);
e9eab4 136     fclose(fh);
SP 137     return TS_SUCCESS;
138 }
139
140
141 /** RESTORE DUMP FROM DISK **/
1ab449 142 ts_vesicle *restore_state(ts_uint *iteration){
e9eab4 143     ts_uint i,j,k;
SP 144     FILE *fh=fopen("dump.bin","rb");
145     ts_uint retval;
3c1ac1 146     ts_uint idx;
e9eab4 147
SP 148 /* we restore all the data from the dump */
149     /* restore vesicle */
150     ts_vesicle *vesicle=(ts_vesicle *)calloc(1,sizeof(ts_vesicle));
86f5e7 151     retval=fread(vesicle, sizeof(ts_vesicle),1,fh);
SP 152 //    fprintf(stderr,"was here! %e\n",vesicle->dmax);
153
e9eab4 154     /* restore vertex list */
SP 155     vesicle->vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
156     retval=fread(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
157     /* restore bond list */
158     vesicle->blist=(ts_bond_list *)malloc(sizeof(ts_bond_list));
159     retval=fread(vesicle->blist, sizeof(ts_bond_list),1,fh);
160     /* restore triangle list */
161     vesicle->tlist=(ts_triangle_list *)malloc(sizeof(ts_triangle_list));
162     retval=fread(vesicle->tlist, sizeof(ts_triangle_list),1,fh);
163     /* restore cell list */
164     vesicle->clist=(ts_cell_list *)malloc(sizeof(ts_cell_list));
165     retval=fread(vesicle->clist, sizeof(ts_cell_list),1,fh);
166     /* restore poly list */
167     vesicle->poly_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
168     retval=fread(vesicle->poly_list, sizeof(ts_poly_list),1,fh);
169     /* level 1 complete */
170
3c1ac1 171 /* prerequisity. Bonds must be malloced before vertexes are recreated */
SP 172   vesicle->blist->bond=(ts_bond **)calloc(vesicle->blist->n,sizeof(ts_bond *));
173     for(i=0;i<vesicle->blist->n;i++){
174         vesicle->blist->bond[i]=(ts_bond *)malloc(sizeof(ts_bond));
175     }
176 /* prerequisity. Triangles must be malloced before vertexes are recreated */
177   vesicle->tlist->tria=(ts_triangle **)calloc(vesicle->tlist->n,sizeof(ts_triangle *));
178     for(i=0;i<vesicle->tlist->n;i++){
179         vesicle->tlist->tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle));
180 }
a37ba2 181 /* prerequisity. Vertices must be malloced before vertexes are recreated */
e9eab4 182     vesicle->vlist->vtx=(ts_vertex **)calloc(vesicle->vlist->n,sizeof(ts_vertex *));
a37ba2 183  for(i=0;i<vesicle->vlist->n;i++){
e9eab4 184         vesicle->vlist->vtx[i]=(ts_vertex *)malloc(sizeof(ts_vertex));
a37ba2 185  }
SP 186   /*restore vertices*/
187     for(i=0;i<vesicle->vlist->n;i++){
e9eab4 188         retval=fread(vesicle->vlist->vtx[i],sizeof(ts_vertex),1,fh);
SP 189         /*restore neigh, bond, tristar. Ignoring cell */
190         vesicle->vlist->vtx[i]->neigh=(ts_vertex **)calloc(vesicle->vlist->vtx[i]->neigh_no, sizeof(ts_vertex *));
191         for(j=0;j<vesicle->vlist->vtx[i]->neigh_no;j++){
3c1ac1 192             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 193             vesicle->vlist->vtx[i]->neigh[j]=vesicle->vlist->vtx[idx];
e9eab4 194         }
SP 195         vesicle->vlist->vtx[i]->bond=(ts_bond **)calloc(vesicle->vlist->vtx[i]->bond_no, sizeof(ts_bond *));
196         for(j=0;j<vesicle->vlist->vtx[i]->bond_no;j++){
3c1ac1 197             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 198 /* pointer can be assigned only when list of bonds is fully initialized in memory. Thus bondlist popularization must be done before vertex can reference to it */
199             vesicle->vlist->vtx[i]->bond[j]=vesicle->blist->bond[idx];    
e9eab4 200         }
SP 201
202         vesicle->vlist->vtx[i]->tristar=(ts_triangle **)calloc(vesicle->vlist->vtx[i]->tristar_no, sizeof(ts_triangle *));
203         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
3c1ac1 204             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 205 /* same comment as above */
206             vesicle->vlist->vtx[i]->tristar[j]=vesicle->tlist->tria[idx];
e9eab4 207         }
SP 208
209     }
210
211     /*restore bonds*/
3c1ac1 212    // vesicle->blist->bond=(ts_bond **)calloc(vesicle->blist->n,sizeof(ts_bond *)); // done before.
e9eab4 213     for(i=0;i<vesicle->blist->n;i++){
3c1ac1 214      //   vesicle->blist->bond[i]=(ts_bond *)malloc(sizeof(ts_bond)); //done before.
e9eab4 215         retval=fread(vesicle->blist->bond[i],sizeof(ts_bond),1,fh);
SP 216         /* restore vtx1 and vtx2 */
3c1ac1 217         retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 218         vesicle->blist->bond[i]->vtx1=vesicle->vlist->vtx[idx];
219         retval=fread(&idx,sizeof(ts_uint),1,fh);
220         vesicle->blist->bond[i]->vtx2=vesicle->vlist->vtx[idx];
e9eab4 221     }
SP 222
223     /*restore triangles*/
3c1ac1 224 //    vesicle->tlist->tria=(ts_triangle **)calloc(vesicle->tlist->n,sizeof(ts_triangle *)); // done before
e9eab4 225     for(i=0;i<vesicle->tlist->n;i++){
3c1ac1 226  //       vesicle->tlist->tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle)); // done before
e9eab4 227         retval=fread(vesicle->tlist->tria[i],sizeof(ts_triangle),1,fh);
SP 228         /* restore pointers for vertices */
3c1ac1 229         retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 230         vesicle->tlist->tria[i]->vertex[0]=vesicle->vlist->vtx[idx];
231         retval=fread(&idx,sizeof(ts_uint),1,fh);
232         vesicle->tlist->tria[i]->vertex[1]=vesicle->vlist->vtx[idx];
233         retval=fread(&idx,sizeof(ts_uint),1,fh);
234         vesicle->tlist->tria[i]->vertex[2]=vesicle->vlist->vtx[idx];
e9eab4 235         /* restore pointers for neigh */
3c1ac1 236      vesicle->tlist->tria[i]->neigh=(ts_triangle **)malloc(vesicle->tlist->tria[i]->neigh_no*sizeof(ts_triangle *));
e9eab4 237         for(j=0;j<vesicle->tlist->tria[i]->neigh_no;j++){
3c1ac1 238             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 239             vesicle->tlist->tria[i]->neigh[j]=vesicle->tlist->tria[idx];
e9eab4 240         }
SP 241
242     }
243    
244     /*restore cells */
245 /*TODO: do we need to recalculate cells here? */
246 /*    vesicle->clist->cell=(ts_cell **)malloc(vesicle->clist->cellno*sizeof(ts_cell *));
247     for(i=0;i<vesicle->clist->cellno;i++){
248         vesicle->clist->cell[i]=(ts_cell *)malloc(sizeof(ts_cell));
249         retval=fread(vesicle->clist->cell[i],sizeof(ts_cell),1,fh);
250     }
251 */
252     /*restore polymeres */
253     vesicle->poly_list->poly = (ts_poly **)calloc(vesicle->poly_list->n,sizeof(ts_poly *));
254     for(i=0;i<vesicle->poly_list->n;i++){
255         vesicle->poly_list->poly[i]=(ts_poly *)calloc(1,sizeof(ts_poly));
256         retval=fread(vesicle->poly_list->poly[i],sizeof(ts_poly),1,fh);
3c1ac1 257         vesicle->poly_list->poly[i]->vlist=(ts_vertex_list *)calloc(1,sizeof(ts_vertex_list));
SP 258         retval=fread(vesicle->poly_list->poly[i]->vlist,sizeof(ts_vertex_list),1,fh);
259         vesicle->poly_list->poly[i]->blist=(ts_bond_list *)calloc(1,sizeof(ts_bond_list));
260         retval=fread(vesicle->poly_list->poly[i]->blist,sizeof(ts_bond_list),1,fh);
261     /* initialize adress space for pointers that will hold specific vertices (monomers) and bonds */
262         vesicle->poly_list->poly[i]->vlist->vtx=(ts_vertex **)calloc(vesicle->poly_list->poly[i]->vlist->n,sizeof(ts_vertex *));
263         vesicle->poly_list->poly[i]->blist->bond=(ts_bond **)calloc(vesicle->poly_list->poly[i]->blist->n,sizeof(ts_bond *));
264      for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
265             vesicle->poly_list->poly[i]->vlist->vtx[j]=(ts_vertex *)malloc(sizeof(ts_vertex));
266     }
267     for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
268             vesicle->poly_list->poly[i]->blist->bond[j]=(ts_bond *)malloc(sizeof(ts_bond));
269     }
270
e9eab4 271     } 
3c1ac1 272
e9eab4 273      
SP 274     /* restore poly vertex(monomer) list*/
275     for(i=0;i<vesicle->poly_list->n;i++){
276         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
277             retval=fread(vesicle->poly_list->poly[i]->vlist->vtx[j],sizeof(ts_vertex),1,fh);
3c1ac1 278                 
e9eab4 279             /* restore neigh and bonds */
SP 280             vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh=(ts_vertex **)calloc(vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh_no, sizeof(ts_vertex *));
281             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh_no;k++){
3c1ac1 282                 retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 283                 vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]=vesicle->poly_list->poly[i]->vlist->vtx[idx];
e9eab4 284             }
SP 285             vesicle->poly_list->poly[i]->vlist->vtx[j]->bond=(ts_bond **)calloc(vesicle->poly_list->poly[i]->vlist->vtx[j]->bond_no, sizeof(ts_bond *));
286             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->bond_no;k++){
3c1ac1 287                 retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 288                 vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]=vesicle->poly_list->poly[i]->blist->bond[idx];
e9eab4 289             }
SP 290
291         }
3c1ac1 292     /* restore grafted vtx on vesicle and grafted_poly */
SP 293                 retval=fread(&idx,sizeof(ts_uint),1,fh);
294         vesicle->vlist->vtx[idx]->grafted_poly=vesicle->poly_list->poly[i];
295         vesicle->poly_list->poly[i]->grafted_vtx=vesicle->vlist->vtx[idx];    
e9eab4 296     }
3c1ac1 297
e9eab4 298     /* restore poly bonds between monomers list*/
SP 299     for(i=0;i<vesicle->poly_list->n;i++){
300         for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 301        //     vesicle->poly_list->poly[i]->blist->bond[j]=(ts_bond *)malloc(sizeof(ts_bond));
e9eab4 302             retval=fread(vesicle->poly_list->poly[i]->blist->bond[j],sizeof(ts_bond),1,fh);
SP 303             /* restore vtx1 and vtx2 */
3c1ac1 304                 retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 305                 vesicle->poly_list->poly[i]->blist->bond[j]->vtx1=vesicle->poly_list->poly[i]->vlist->vtx[idx];
306                 retval=fread(&idx,sizeof(ts_uint),1,fh);
307                 vesicle->poly_list->poly[i]->blist->bond[j]->vtx2=vesicle->poly_list->poly[i]->vlist->vtx[idx];
e9eab4 308         }
SP 309     }
310
063289 311 // recreating space for cells // 
SP 312     vesicle->clist=(ts_cell_list *)malloc(sizeof(ts_cell_list));
313     retval=fread(vesicle->clist, sizeof(ts_cell_list), 1,fh); 
314     vesicle->clist->cell=(ts_cell **)malloc(sizeof(ts_cell *)*vesicle->clist->ncmax[0]*vesicle->clist->ncmax[1]*vesicle->clist->ncmax[2]);
315     for(i=0;i<vesicle->clist->ncmax[0]*vesicle->clist->ncmax[1]*vesicle->clist->ncmax[2];i++){
316             vesicle->clist->cell[i]=(ts_cell *)calloc(1,sizeof(ts_cell));
317             vesicle->clist->cell[i]->idx=i+1; // We enumerate cells! Probably never required!
318         }
e9eab4 319
1ab449 320     retval=fread(iteration,sizeof(ts_uint),1,fh);
e9eab4 321     if(retval); 
SP 322     fclose(fh);
323     return vesicle;
324 }
325
326
327
083e03 328 ts_bool parse_args(int argc, char **argv){
SP 329  int c, retval;
330  DIR *dir;
331     path[0]=0;
332 while (1)
333      {
334        static struct option long_options[] =
335          {
336            {"force-from-tape", no_argument,       &force_from_tape, 1},
337            {"tape",     no_argument,       0, 't'},
338            {"output-file",  required_argument, 0, 'o'},
339            {"directory",  required_argument, 0, 'd'},
340            {"dump-file", required_argument,0, 'f'},
341            {0, 0, 0, 0}
342          };
343        /* getopt_long stores the option index here. */
344        int option_index = 0;
345
346        c = getopt_long (argc, argv, "d:fot",
347                         long_options, &option_index);
348
349        /* Detect the end of the options. */
350        if (c == -1)
351          break;
352
353        switch (c)
354          {
355          case 0:
356            /* If this option set a flag, do nothing else now. */
357            if (long_options[option_index].flag != 0)
358              break;
359            printf ("option %s", long_options[option_index].name);
360            if (optarg)
361              printf (" with arg %s", optarg);
362            printf ("\n");
363            break;
364
365          case 't':
366             //check if tape exists. If not, fail immediately.
367            puts ("option -t\n");
368            break;
369
370          case 'o':
371             //set filename of master output file
372            printf ("option -o with value `%s'\n", optarg);
373            break;
374
375          case 'd':
376             //check if directory exists. If not create one. If creation is
377             //successful, set directory for output files.
378             //printf ("option -d with value `%s'\n", optarg);
379             dir = opendir(optarg);
380             if (dir)
381             {
382                 /* Directory exists. */
383                 closedir(dir);
384             }
385             else if (ENOENT == errno)
386             {
387                 /* Directory does not exist. */
388                 retval=mkdir(optarg, 0700);
389                 if(retval){
390                 fatal("Could not create requested directory. Check if you have permissions",1);
391                 }
392             }
393             else
394             {
395                 /* opendir() failed for some other reason. */
396                 fatal("Could not check if directory exists. Reason unknown",1);
397             }
398             ts_fprintf(stdout,"\n*** Using output directory: %s\n\n", optarg);
399 //            sprintf(path,"%s", optarg);
400             strcpy(path, optarg);
401            // ts_fprintf(stdout,"ok!\n"); 
402
403            break;
404
405         case 'f':
406             //check if dump file specified exists. Defaults to dump.bin
407             break;
408
409          case '?':
410            /* getopt_long already printed an error message. */
411
412             ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
413             fatal("Ooops, read help first",1);
414            break;
415
416          default:
417            exit (1);
418          }
419      }
420
421     return TS_SUCCESS;
422
423 }
424
425
426
427
d7639a 428 ts_bool print_vertex_list(ts_vertex_list *vlist){
SP 429     ts_uint i;
430     printf("Number of vertices: %u\n",vlist->n);
431     for(i=0;i<vlist->n;i++){
a6b1b5 432         printf("%u: %f %f %f\n",
8f6a69 433 vlist->vtx[i]->idx,vlist->vtx[i]->x, vlist->vtx[i]->y, vlist->vtx[i]->z);
d7639a 434     }
SP 435     return TS_SUCCESS;
436 }
437
438 ts_bool print_vertex_neighbours(ts_vertex_list *vlist){
439     ts_uint i,j;
a6b1b5 440     ts_vertex **vtx=vlist->vtx;
d7639a 441     printf("Vertex id(neigh no): (neighvertex coord) (neighvertex coord) ...\n");
SP 442     for(i=0;i<vlist->n;i++){
8f6a69 443         printf("%u(%u): ",vtx[i]->idx,vtx[i]->neigh_no);
SP 444         for(j=0;j<vtx[i]->neigh_no;j++){
445             printf("(%f,%f,%f)",vtx[i]->neigh[j]->x,
446 vtx[i]->neigh[j]->y,vtx[i]->neigh[j]->z);
d7639a 447         }
SP 448         printf("\n");
449     }
450
451 return TS_SUCCESS;
452 }
453
454 ts_bool write_vertex_fcompat_file(ts_vertex_list *vlist,ts_char *filename){
a6b1b5 455     ts_vertex **vtx=vlist->vtx;
d7639a 456     ts_uint i;
SP 457     FILE *fh;
458     
459     fh=fopen(filename, "w");
460     if(fh==NULL){
461         err("Cannot open file %s for writing");
462         return TS_FAIL;
463     }
464     for(i=0;i<vlist->n;i++)
8f6a69 465         fprintf(fh," %E\t%E\t%E\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 466
SP 467     fclose(fh);
468 return TS_SUCCESS;
469 }
470
471
472 ts_bool fprint_vertex_list(FILE *fh,ts_vertex_list *vlist){
473     ts_uint i,j;
474     for(i=0;i<vlist->n;i++){
8f6a69 475         fprintf(fh," %.17E\t%.17E\t%.17E\t%u\n",vlist->vtx[i]->x,
SP 476             vlist->vtx[i]->y, vlist->vtx[i]->z,
477             vlist->vtx[i]->neigh_no);
478         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
479             fprintf(fh,"\t%u",(ts_uint)(vlist->vtx[i]->neigh[j]->idx));
a6b1b5 480         //-vlist->vtx+1));
d7639a 481         }
SP 482         fprintf(fh,"\n");
483     }
484     return TS_SUCCESS;
485 }
486
487 ts_bool fprint_tristar(FILE *fh, ts_vesicle *vesicle){
488     ts_uint i,j;
a6b1b5 489     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 490         fprintf(fh,"\t%u",vesicle->vlist->vtx[i]->tristar_no);
SP 491         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
492             fprintf(fh,"\t%u",(ts_uint)(vesicle->vlist->vtx[i]->tristar[j]->idx));//-vesicle->tlist->tria+1));
d7639a 493         }
SP 494         fprintf(fh,"\n");
495     }
496     return TS_SUCCESS;
497 }
498
499 ts_bool fprint_triangle_list(FILE *fh, ts_vesicle *vesicle){
a6b1b5 500         ts_triangle_list *tlist=vesicle->tlist;
d7639a 501       ts_uint i,j;
SP 502     for(i=0;i<tlist->n;i++){
41a035 503         fprintf(fh,"\t%u",tlist->tria[i]->neigh_no);
SP 504         for(j=0;j<tlist->tria[i]->neigh_no;j++){
505             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->neigh[j]->idx));//-tlist->tria+1)); 
d7639a 506         }
SP 507         fprintf(fh,"\n");
508             for(j=0;j<3;j++){
41a035 509             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->vertex[j]->idx));//-vesicle->vlist->vtx+1)); 
d7639a 510             }
SP 511         fprintf(fh,"\n");
41a035 512         fprintf(fh,"%.17E\t%.17E\t%.17E\n",tlist->tria[i]->xnorm,
SP 513 tlist->tria[i]->ynorm,tlist->tria[i]->znorm);
d7639a 514         fprintf(fh,"0.00000000000000000\n0.00000000000000000\n");
SP 515     }
516     return TS_SUCCESS;
517 }
518
519 ts_bool fprint_vertex_data(FILE *fh,ts_vertex_list *vlist){
520     ts_uint i,j;
521     for(i=0;i<vlist->n;i++){
522         fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
8f6a69 523         vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
SP 524         vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
cef6ca 525         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 526             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
d7639a 527         }
SP 528             fprintf(fh,"\n");
cef6ca 529         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 530             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
d7639a 531         }
SP 532             fprintf(fh,"\n");
533     }
534     return TS_SUCCESS;
535 }
536
537 ts_bool fprint_bonds(FILE *fh,ts_vesicle *vesicle){
538     ts_uint i;
a6b1b5 539     for(i=0;i<vesicle->blist->n;i++){
e016c4 540         fprintf(fh,"\t%u\t%u\n",(ts_uint)(vesicle->blist->bond[i]->vtx1->idx),
a6b1b5 541 //-vesicle->vlist->vtx+1),
e016c4 542         (ts_uint)(vesicle->blist->bond[i]->vtx2->idx));
a6b1b5 543     //-vesicle->vlist.vtx+1));
d7639a 544     }
SP 545     return TS_SUCCESS;
546 }
547
548
549 ts_bool write_dout_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
550     FILE *fh;
551     fh=fopen(filename, "w");
552     if(fh==NULL){
553         err("Cannot open file %s for writing");
554         return TS_FAIL;
555     }
556     fprintf(fh,"%.17E\n%.17E\n",vesicle->stepsize,vesicle->dmax);
a6b1b5 557     fprint_vertex_list(fh,vesicle->vlist);
d7639a 558     fprint_tristar(fh,vesicle);
SP 559     fprint_triangle_list(fh,vesicle);
a6b1b5 560     fprint_vertex_data(fh,vesicle->vlist);
d7639a 561     fprint_bonds(fh,vesicle);
SP 562     fclose(fh);    
563     return TS_SUCCESS;
564 }
565
566 ts_bool read_tape_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
567     FILE *fh;
568     char line[255];
569     fh=fopen(filename, "r");
570         if(fh==NULL){
571                 err("Cannot open file for reading... Nonexistant file?");
572                 return TS_FAIL;
573         }
a6b1b5 574     ts_uint retval=1;
d7639a 575     while(retval!=EOF){
SP 576         retval=fscanf(fh,"%s",line);
577         
578         fprintf(stderr,"%s",line);
579     }    
580     fclose(fh);    
581     return TS_SUCCESS;
582 }
583
584 ts_bool write_master_xml_file(ts_char *filename){
585      FILE *fh;
586     ts_char *i,*j;
587     ts_uint tstep;
588     ts_char *number;
589         fh=fopen(filename, "w");
590         if(fh==NULL){
591                 err("Cannot open file %s for writing");
592                 return TS_FAIL;
593         }
594
595     fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
596     DIR *dir = opendir(".");
597     if(dir){
598         struct dirent *ent;
599         tstep=0;
600         while((ent = readdir(dir)) != NULL)
601         {
602             i=rindex(ent->d_name,'.');
603             if(i==NULL) continue;
604             if(strcmp(i+1,"vtu")==0){
605                     j=rindex(ent->d_name,'_');
606                     if(j==NULL) continue;
607                     number=strndup(j+1,j-i); 
a6b1b5 608                 fprintf(fh,"<DataSet timestep=\"%u\" group=\"\" part=\"0\" file=\"%s\"/>\n",atoi(number),ent->d_name);
d7639a 609                 tstep++;
SP 610                     free(number);
611             }  
612         }
613     }
f74313 614     free(dir);
d7639a 615     fprintf(fh,"</Collection>\n</VTKFile>\n");
SP 616     fclose(fh);
617     return TS_SUCCESS;
618 }
619
620 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno){
a6b1b5 621     ts_vertex_list *vlist=vesicle->vlist;
SP 622     ts_bond_list *blist=vesicle->blist;
623     ts_vertex **vtx=vlist->vtx;
40aa5b 624     ts_uint i,j;
d7639a 625         char filename[255];
SP 626     FILE *fh;
627
628         sprintf(filename,"timestep_%.6u.vtu",timestepno);
629     fh=fopen(filename, "w");
630     if(fh==NULL){
631         err("Cannot open file %s for writing");
632         return TS_FAIL;
633     }
634     /* Here comes header of the file */
40aa5b 635
SP 636     //find number of extra vtxs and bonds of polymeres
3c1ac1 637     ts_uint monono=0, polyno=0, poly_idx=0;
40aa5b 638     ts_bool poly=0;
SP 639     if(vesicle->poly_list!=NULL){
640         if(vesicle->poly_list->poly[0]!=NULL){
641         polyno=vesicle->poly_list->n;
642         monono=vesicle->poly_list->poly[0]->vlist->n;
643         poly=1;
644         }
645     }
d7639a 646     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n <UnstructuredGrid>\n");
40aa5b 647     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno, blist->n+monono*polyno);
d7639a 648     fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
SP 649        for(i=0;i<vlist->n;i++){
a6b1b5 650         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 651     }
40aa5b 652     //polymeres
SP 653     if(poly){
3c1ac1 654         poly_idx=vlist->n;
40aa5b 655         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 656             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
SP 657                 fprintf(fh,"%u ", poly_idx);
40aa5b 658             }
SP 659         }
660     }
d7639a 661
SP 662     fprintf(fh,"</DataArray>\n</PointData>\n<CellData>\n</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
663     for(i=0;i<vlist->n;i++){
8f6a69 664         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 665     }
SP 666     //polymeres
667     if(poly){
668         for(i=0;i<vesicle->poly_list->n;i++){
669             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
670                 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 );
671             }
672         }
d7639a 673     }
SP 674
675     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
676     for(i=0;i<blist->n;i++){
e016c4 677             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 678     }
40aa5b 679     //polymeres
SP 680     if(poly){
3c1ac1 681         poly_idx=vlist->n;
40aa5b 682         for(i=0;i<vesicle->poly_list->n;i++){
SP 683             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 684 //                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);
SP 685                 fprintf(fh,"%u %u\n", vesicle->poly_list->poly[i]->blist->bond[j]->vtx1->idx+vlist->n+i*monono,vesicle->poly_list->poly[i]->blist->bond[j]->vtx2->idx+vlist->n+i*monono);
40aa5b 686             }
SP 687     //grafted bonds
3c1ac1 688         fprintf(fh,"%u %u\n", vesicle->poly_list->poly[i]->grafted_vtx->idx, vesicle->poly_list->poly[i]->vlist->vtx[0]->idx+vlist->n+i*monono);
40aa5b 689         }
SP 690
691     }
692     
693
d7639a 694     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
40aa5b 695     for (i=2;i<(blist->n+monono*polyno)*2+1;i+=2){
d7639a 696     fprintf(fh,"%u ",i);
SP 697     }
698     fprintf(fh,"\n");
699     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
40aa5b 700      for (i=0;i<blist->n+monono*polyno;i++){
d7639a 701         fprintf(fh,"3 ");
SP 702     }
703
704     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
705     fclose(fh);
706     return TS_SUCCESS;
707
708 }
709
710 ts_bool write_vertex_vtk_file(ts_vesicle *vesicle,ts_char *filename, ts_char *text){
a6b1b5 711     ts_vertex_list *vlist=vesicle->vlist;
SP 712     ts_bond_list *blist=vesicle->blist;
713     ts_vertex **vtx=vlist->vtx;
714     ts_uint i;
d7639a 715     FILE *fh;
SP 716     
717     fh=fopen(filename, "w");
718     if(fh==NULL){
719         err("Cannot open file %s for writing");
720         return TS_FAIL;
721     }
722     /* Here comes header of the file */
723 //    fprintf(stderr,"NSHELL=%u\n",nshell);
724     fprintf(fh, "# vtk DataFile Version 2.0\n");
725     /* TODO: Do a sanity check on text. Max 255 char, must not me \n terminated */ 
726     fprintf(fh, "%s\n", text);
727     fprintf(fh,"ASCII\n");
728     fprintf(fh,"DATASET UNSTRUCTURED_GRID\n");
729     fprintf(fh,"POINTS %u double\n", vlist->n);
730     for(i=0;i<vlist->n;i++){
8f6a69 731         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 732     }
SP 733     
734     fprintf(fh,"CELLS %u %u\n",blist->n,3*blist->n);
735     for(i=0;i<blist->n;i++){
e016c4 736             fprintf(fh,"2 %u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 737     }
SP 738     fprintf(fh,"CELL_TYPES %u\n",blist->n);
739     for(i=0;i<blist->n;i++)
740         fprintf(fh,"3\n");
741
742     fprintf(fh,"POINT_DATA %u\n", vlist->n);
743     fprintf(fh,"SCALARS scalars long 1\n");
744     fprintf(fh,"LOOKUP_TABLE default\n");
745
746     for(i=0;i<vlist->n;i++)
8f6a69 747         fprintf(fh,"%u\n",vtx[i]->idx);
d7639a 748
SP 749     fclose(fh);
750     return TS_SUCCESS;
751 }
752
753
754
1ab449 755 ts_tape *parsetape(char *filename){
SP 756   //  long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20, pswitch=0;  // THIS IS DUE TO CONFUSE BUG!
757     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
758     tape->multiprocessing=calloc(255,sizeof(char));
759   /*  long int brezveze0=1;
b14a8d 760     long int brezveze1=1;
SP 761     long int brezveze2=1;
e86357 762     ts_double xk0=25.0, dmax=1.67,stepsize=0.15,kspring=800.0,pressure=0.0;
d7a113 763     long int iter=1000, init=1000, mcsw=1000;
1ab449 764 */    
40aa5b 765
d7639a 766     cfg_opt_t opts[] = {
1ab449 767         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 768         CFG_SIMPLE_INT("npoly", &tape->npoly),
769         CFG_SIMPLE_INT("nmono", &tape->nmono),
770         CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
771         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
772     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
773     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
774     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
775         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
776         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
777         CFG_SIMPLE_INT("nymax", &tape->ncymax),
778         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
779         CFG_SIMPLE_INT("iterations",&tape->iterations),
780     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
781     CFG_SIMPLE_INT("inititer", &tape->inititer),
782         CFG_SIMPLE_BOOL("quiet",&tape->quiet),
783         CFG_SIMPLE_STR("multiprocessing",tape->multiprocessing),
784         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
785         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
786         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
d7639a 787         CFG_END()
SP 788     };
789     cfg_t *cfg;    
790     ts_uint retval;
791     cfg = cfg_init(opts, 0);
1ab449 792     retval=cfg_parse(cfg, filename);
d7639a 793     if(retval==CFG_FILE_ERROR){
a6b1b5 794     fatal("No tape file.",100);
d7639a 795     }
SP 796     else if(retval==CFG_PARSE_ERROR){
797     fatal("Invalid tape!",100);
798     }
a2db52 799
d7639a 800     cfg_free(cfg);
40aa5b 801
SP 802
1ab449 803     /* global variables are set automatically */
SP 804     quiet=tape->quiet;
805     return tape;
806 }
d7639a 807
1ab449 808 ts_bool tape_free(ts_tape *tape){
SP 809     free(tape->multiprocessing);
810     free(tape);
811     return TS_SUCCESS;
d7639a 812 }
f74313 813
SP 814
815 ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
816     FILE *fh;
817     ts_uint i, nvtx,nedges,ntria;
818     ts_uint vtxi1,vtxi2;
819     float x,y,z;
820     ts_vertex_list *vlist;
821     fh=fopen(fname, "r");
822         if(fh==NULL){
823                 err("Cannot open file for reading... Nonexistant file?");
824                 return TS_FAIL;
825         }
826     ts_uint retval;
827     retval=fscanf(fh,"%u %u %u",&nvtx, &nedges, &ntria);
828     vesicle->vlist=init_vertex_list(nvtx);
829     vlist=vesicle->vlist;
830     for(i=0;i<nvtx;i++){
8f6a69 831    //     fscanf(fh,"%F %F %F",&vlist->vtx[i]->x,&vlist->vtx[i]->y,&vlist->vtx[i]->z);
f74313 832        retval=fscanf(fh,"%F %F %F",&x,&y,&z);
8f6a69 833         vlist->vtx[i]->x=x;
SP 834         vlist->vtx[i]->y=y;
835         vlist->vtx[i]->z=z;
f74313 836     }
SP 837     for(i=0;i<nedges;i++){
838         retval=fscanf(fh,"%u %u",&vtxi1,&vtxi2);
839         bond_add(vesicle->blist,vesicle->vlist->vtx[vtxi1-1],vesicle->vlist->vtx[vtxi2-1]);
840     }
841     //TODO: neighbours from bonds,
842     //TODO: triangles from neigbours
843
844 //    Don't need to read triangles. Already have enough data
845     /*
846     for(i=0;i<ntria;i++){
847         retval=fscanf(fh,"%u %u %u", &bi1, &bi2, &bi3);
8f6a69 848         vtxi1=vesicle->blist->vertex1->idx;
SP 849         vtxi2=vesicle->blist->vertex1->idx;
f74313 850         
SP 851     }
852     */
41a035 853     if(retval);
f74313 854     fclose(fh);    
SP 855
856
857
858     return TS_SUCCESS;
859 }