Trisurf Monte Carlo simulator
Samo Penic
2014-03-08 8a66144c9118f8df80eba28c5b363fac574194da
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          {
8a6614 336            {"force-from-tape", no_argument,       &(command_line_args.force_from_tape), 1},
SP 337        {"reset-iteration-count", no_argument, &(command_line_args.reset_iteration_count), 1},
083e03 338            {"tape",     no_argument,       0, 't'},
SP 339            {"output-file",  required_argument, 0, 'o'},
340            {"directory",  required_argument, 0, 'd'},
341            {"dump-file", required_argument,0, 'f'},
342            {0, 0, 0, 0}
343          };
344        /* getopt_long stores the option index here. */
345        int option_index = 0;
346
347        c = getopt_long (argc, argv, "d:fot",
348                         long_options, &option_index);
349
350        /* Detect the end of the options. */
351        if (c == -1)
352          break;
353
354        switch (c)
355          {
356          case 0:
357            /* If this option set a flag, do nothing else now. */
358            if (long_options[option_index].flag != 0)
359              break;
360            printf ("option %s", long_options[option_index].name);
361            if (optarg)
362              printf (" with arg %s", optarg);
363            printf ("\n");
364            break;
365
366          case 't':
367             //check if tape exists. If not, fail immediately.
368            puts ("option -t\n");
369            break;
370
371          case 'o':
372             //set filename of master output file
373            printf ("option -o with value `%s'\n", optarg);
374            break;
375
376          case 'd':
377             //check if directory exists. If not create one. If creation is
378             //successful, set directory for output files.
379             //printf ("option -d with value `%s'\n", optarg);
380             dir = opendir(optarg);
381             if (dir)
382             {
383                 /* Directory exists. */
384                 closedir(dir);
385             }
386             else if (ENOENT == errno)
387             {
388                 /* Directory does not exist. */
389                 retval=mkdir(optarg, 0700);
390                 if(retval){
391                 fatal("Could not create requested directory. Check if you have permissions",1);
392                 }
393             }
394             else
395             {
396                 /* opendir() failed for some other reason. */
397                 fatal("Could not check if directory exists. Reason unknown",1);
398             }
399             ts_fprintf(stdout,"\n*** Using output directory: %s\n\n", optarg);
400 //            sprintf(path,"%s", optarg);
401             strcpy(path, optarg);
402            // ts_fprintf(stdout,"ok!\n"); 
403
404            break;
405
406         case 'f':
407             //check if dump file specified exists. Defaults to dump.bin
408             break;
409
410          case '?':
411            /* getopt_long already printed an error message. */
412
413             ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
414             fatal("Ooops, read help first",1);
415            break;
416
417          default:
418            exit (1);
419          }
420      }
421
422     return TS_SUCCESS;
423
424 }
425
426
427
428
d7639a 429 ts_bool print_vertex_list(ts_vertex_list *vlist){
SP 430     ts_uint i;
431     printf("Number of vertices: %u\n",vlist->n);
432     for(i=0;i<vlist->n;i++){
a6b1b5 433         printf("%u: %f %f %f\n",
8f6a69 434 vlist->vtx[i]->idx,vlist->vtx[i]->x, vlist->vtx[i]->y, vlist->vtx[i]->z);
d7639a 435     }
SP 436     return TS_SUCCESS;
437 }
438
439 ts_bool print_vertex_neighbours(ts_vertex_list *vlist){
440     ts_uint i,j;
a6b1b5 441     ts_vertex **vtx=vlist->vtx;
d7639a 442     printf("Vertex id(neigh no): (neighvertex coord) (neighvertex coord) ...\n");
SP 443     for(i=0;i<vlist->n;i++){
8f6a69 444         printf("%u(%u): ",vtx[i]->idx,vtx[i]->neigh_no);
SP 445         for(j=0;j<vtx[i]->neigh_no;j++){
446             printf("(%f,%f,%f)",vtx[i]->neigh[j]->x,
447 vtx[i]->neigh[j]->y,vtx[i]->neigh[j]->z);
d7639a 448         }
SP 449         printf("\n");
450     }
451
452 return TS_SUCCESS;
453 }
454
455 ts_bool write_vertex_fcompat_file(ts_vertex_list *vlist,ts_char *filename){
a6b1b5 456     ts_vertex **vtx=vlist->vtx;
d7639a 457     ts_uint i;
SP 458     FILE *fh;
459     
460     fh=fopen(filename, "w");
461     if(fh==NULL){
462         err("Cannot open file %s for writing");
463         return TS_FAIL;
464     }
465     for(i=0;i<vlist->n;i++)
8f6a69 466         fprintf(fh," %E\t%E\t%E\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 467
SP 468     fclose(fh);
469 return TS_SUCCESS;
470 }
471
472
473 ts_bool fprint_vertex_list(FILE *fh,ts_vertex_list *vlist){
474     ts_uint i,j;
475     for(i=0;i<vlist->n;i++){
8f6a69 476         fprintf(fh," %.17E\t%.17E\t%.17E\t%u\n",vlist->vtx[i]->x,
SP 477             vlist->vtx[i]->y, vlist->vtx[i]->z,
478             vlist->vtx[i]->neigh_no);
479         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
480             fprintf(fh,"\t%u",(ts_uint)(vlist->vtx[i]->neigh[j]->idx));
a6b1b5 481         //-vlist->vtx+1));
d7639a 482         }
SP 483         fprintf(fh,"\n");
484     }
485     return TS_SUCCESS;
486 }
487
488 ts_bool fprint_tristar(FILE *fh, ts_vesicle *vesicle){
489     ts_uint i,j;
a6b1b5 490     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 491         fprintf(fh,"\t%u",vesicle->vlist->vtx[i]->tristar_no);
SP 492         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
493             fprintf(fh,"\t%u",(ts_uint)(vesicle->vlist->vtx[i]->tristar[j]->idx));//-vesicle->tlist->tria+1));
d7639a 494         }
SP 495         fprintf(fh,"\n");
496     }
497     return TS_SUCCESS;
498 }
499
500 ts_bool fprint_triangle_list(FILE *fh, ts_vesicle *vesicle){
a6b1b5 501         ts_triangle_list *tlist=vesicle->tlist;
d7639a 502       ts_uint i,j;
SP 503     for(i=0;i<tlist->n;i++){
41a035 504         fprintf(fh,"\t%u",tlist->tria[i]->neigh_no);
SP 505         for(j=0;j<tlist->tria[i]->neigh_no;j++){
506             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->neigh[j]->idx));//-tlist->tria+1)); 
d7639a 507         }
SP 508         fprintf(fh,"\n");
509             for(j=0;j<3;j++){
41a035 510             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->vertex[j]->idx));//-vesicle->vlist->vtx+1)); 
d7639a 511             }
SP 512         fprintf(fh,"\n");
41a035 513         fprintf(fh,"%.17E\t%.17E\t%.17E\n",tlist->tria[i]->xnorm,
SP 514 tlist->tria[i]->ynorm,tlist->tria[i]->znorm);
d7639a 515         fprintf(fh,"0.00000000000000000\n0.00000000000000000\n");
SP 516     }
517     return TS_SUCCESS;
518 }
519
520 ts_bool fprint_vertex_data(FILE *fh,ts_vertex_list *vlist){
521     ts_uint i,j;
522     for(i=0;i<vlist->n;i++){
523         fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
8f6a69 524         vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
SP 525         vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
cef6ca 526         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 527             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
d7639a 528         }
SP 529             fprintf(fh,"\n");
cef6ca 530         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 531             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
d7639a 532         }
SP 533             fprintf(fh,"\n");
534     }
535     return TS_SUCCESS;
536 }
537
538 ts_bool fprint_bonds(FILE *fh,ts_vesicle *vesicle){
539     ts_uint i;
a6b1b5 540     for(i=0;i<vesicle->blist->n;i++){
e016c4 541         fprintf(fh,"\t%u\t%u\n",(ts_uint)(vesicle->blist->bond[i]->vtx1->idx),
a6b1b5 542 //-vesicle->vlist->vtx+1),
e016c4 543         (ts_uint)(vesicle->blist->bond[i]->vtx2->idx));
a6b1b5 544     //-vesicle->vlist.vtx+1));
d7639a 545     }
SP 546     return TS_SUCCESS;
547 }
548
549
550 ts_bool write_dout_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
551     FILE *fh;
552     fh=fopen(filename, "w");
553     if(fh==NULL){
554         err("Cannot open file %s for writing");
555         return TS_FAIL;
556     }
557     fprintf(fh,"%.17E\n%.17E\n",vesicle->stepsize,vesicle->dmax);
a6b1b5 558     fprint_vertex_list(fh,vesicle->vlist);
d7639a 559     fprint_tristar(fh,vesicle);
SP 560     fprint_triangle_list(fh,vesicle);
a6b1b5 561     fprint_vertex_data(fh,vesicle->vlist);
d7639a 562     fprint_bonds(fh,vesicle);
SP 563     fclose(fh);    
564     return TS_SUCCESS;
565 }
566
567 ts_bool read_tape_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
568     FILE *fh;
569     char line[255];
570     fh=fopen(filename, "r");
571         if(fh==NULL){
572                 err("Cannot open file for reading... Nonexistant file?");
573                 return TS_FAIL;
574         }
a6b1b5 575     ts_uint retval=1;
d7639a 576     while(retval!=EOF){
SP 577         retval=fscanf(fh,"%s",line);
578         
579         fprintf(stderr,"%s",line);
580     }    
581     fclose(fh);    
582     return TS_SUCCESS;
583 }
584
585 ts_bool write_master_xml_file(ts_char *filename){
586      FILE *fh;
587     ts_char *i,*j;
588     ts_uint tstep;
589     ts_char *number;
590         fh=fopen(filename, "w");
591         if(fh==NULL){
592                 err("Cannot open file %s for writing");
593                 return TS_FAIL;
594         }
595
596     fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
597     DIR *dir = opendir(".");
598     if(dir){
599         struct dirent *ent;
600         tstep=0;
601         while((ent = readdir(dir)) != NULL)
602         {
603             i=rindex(ent->d_name,'.');
604             if(i==NULL) continue;
605             if(strcmp(i+1,"vtu")==0){
606                     j=rindex(ent->d_name,'_');
607                     if(j==NULL) continue;
608                     number=strndup(j+1,j-i); 
a6b1b5 609                 fprintf(fh,"<DataSet timestep=\"%u\" group=\"\" part=\"0\" file=\"%s\"/>\n",atoi(number),ent->d_name);
d7639a 610                 tstep++;
SP 611                     free(number);
612             }  
613         }
614     }
f74313 615     free(dir);
d7639a 616     fprintf(fh,"</Collection>\n</VTKFile>\n");
SP 617     fclose(fh);
618     return TS_SUCCESS;
619 }
620
621 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno){
a6b1b5 622     ts_vertex_list *vlist=vesicle->vlist;
SP 623     ts_bond_list *blist=vesicle->blist;
624     ts_vertex **vtx=vlist->vtx;
40aa5b 625     ts_uint i,j;
d7639a 626         char filename[255];
SP 627     FILE *fh;
628
629         sprintf(filename,"timestep_%.6u.vtu",timestepno);
630     fh=fopen(filename, "w");
631     if(fh==NULL){
632         err("Cannot open file %s for writing");
633         return TS_FAIL;
634     }
635     /* Here comes header of the file */
40aa5b 636
SP 637     //find number of extra vtxs and bonds of polymeres
3c1ac1 638     ts_uint monono=0, polyno=0, poly_idx=0;
40aa5b 639     ts_bool poly=0;
SP 640     if(vesicle->poly_list!=NULL){
641         if(vesicle->poly_list->poly[0]!=NULL){
642         polyno=vesicle->poly_list->n;
643         monono=vesicle->poly_list->poly[0]->vlist->n;
644         poly=1;
645         }
646     }
d7639a 647     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n <UnstructuredGrid>\n");
40aa5b 648     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno, blist->n+monono*polyno);
d7639a 649     fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
SP 650        for(i=0;i<vlist->n;i++){
a6b1b5 651         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 652     }
40aa5b 653     //polymeres
SP 654     if(poly){
3c1ac1 655         poly_idx=vlist->n;
40aa5b 656         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 657             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
SP 658                 fprintf(fh,"%u ", poly_idx);
40aa5b 659             }
SP 660         }
661     }
d7639a 662
SP 663     fprintf(fh,"</DataArray>\n</PointData>\n<CellData>\n</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
664     for(i=0;i<vlist->n;i++){
8f6a69 665         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 666     }
SP 667     //polymeres
668     if(poly){
669         for(i=0;i<vesicle->poly_list->n;i++){
670             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
671                 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 );
672             }
673         }
d7639a 674     }
SP 675
676     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
677     for(i=0;i<blist->n;i++){
e016c4 678             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 679     }
40aa5b 680     //polymeres
SP 681     if(poly){
3c1ac1 682         poly_idx=vlist->n;
40aa5b 683         for(i=0;i<vesicle->poly_list->n;i++){
SP 684             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 685 //                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 686                 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 687             }
SP 688     //grafted bonds
3c1ac1 689         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 690         }
SP 691
692     }
693     
694
d7639a 695     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
40aa5b 696     for (i=2;i<(blist->n+monono*polyno)*2+1;i+=2){
d7639a 697     fprintf(fh,"%u ",i);
SP 698     }
699     fprintf(fh,"\n");
700     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
40aa5b 701      for (i=0;i<blist->n+monono*polyno;i++){
d7639a 702         fprintf(fh,"3 ");
SP 703     }
704
705     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
706     fclose(fh);
707     return TS_SUCCESS;
708
709 }
710
711 ts_bool write_vertex_vtk_file(ts_vesicle *vesicle,ts_char *filename, ts_char *text){
a6b1b5 712     ts_vertex_list *vlist=vesicle->vlist;
SP 713     ts_bond_list *blist=vesicle->blist;
714     ts_vertex **vtx=vlist->vtx;
715     ts_uint i;
d7639a 716     FILE *fh;
SP 717     
718     fh=fopen(filename, "w");
719     if(fh==NULL){
720         err("Cannot open file %s for writing");
721         return TS_FAIL;
722     }
723     /* Here comes header of the file */
724 //    fprintf(stderr,"NSHELL=%u\n",nshell);
725     fprintf(fh, "# vtk DataFile Version 2.0\n");
726     /* TODO: Do a sanity check on text. Max 255 char, must not me \n terminated */ 
727     fprintf(fh, "%s\n", text);
728     fprintf(fh,"ASCII\n");
729     fprintf(fh,"DATASET UNSTRUCTURED_GRID\n");
730     fprintf(fh,"POINTS %u double\n", vlist->n);
731     for(i=0;i<vlist->n;i++){
8f6a69 732         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 733     }
SP 734     
735     fprintf(fh,"CELLS %u %u\n",blist->n,3*blist->n);
736     for(i=0;i<blist->n;i++){
e016c4 737             fprintf(fh,"2 %u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 738     }
SP 739     fprintf(fh,"CELL_TYPES %u\n",blist->n);
740     for(i=0;i<blist->n;i++)
741         fprintf(fh,"3\n");
742
743     fprintf(fh,"POINT_DATA %u\n", vlist->n);
744     fprintf(fh,"SCALARS scalars long 1\n");
745     fprintf(fh,"LOOKUP_TABLE default\n");
746
747     for(i=0;i<vlist->n;i++)
8f6a69 748         fprintf(fh,"%u\n",vtx[i]->idx);
d7639a 749
SP 750     fclose(fh);
751     return TS_SUCCESS;
752 }
753
754
755
1ab449 756 ts_tape *parsetape(char *filename){
SP 757   //  long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20, pswitch=0;  // THIS IS DUE TO CONFUSE BUG!
758     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
759     tape->multiprocessing=calloc(255,sizeof(char));
760   /*  long int brezveze0=1;
b14a8d 761     long int brezveze1=1;
SP 762     long int brezveze2=1;
e86357 763     ts_double xk0=25.0, dmax=1.67,stepsize=0.15,kspring=800.0,pressure=0.0;
d7a113 764     long int iter=1000, init=1000, mcsw=1000;
1ab449 765 */    
40aa5b 766
d7639a 767     cfg_opt_t opts[] = {
1ab449 768         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 769         CFG_SIMPLE_INT("npoly", &tape->npoly),
770         CFG_SIMPLE_INT("nmono", &tape->nmono),
771         CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
772         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
773     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
774     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
775     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
776         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
777         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
778         CFG_SIMPLE_INT("nymax", &tape->ncymax),
779         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
780         CFG_SIMPLE_INT("iterations",&tape->iterations),
781     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
782     CFG_SIMPLE_INT("inititer", &tape->inititer),
783         CFG_SIMPLE_BOOL("quiet",&tape->quiet),
784         CFG_SIMPLE_STR("multiprocessing",tape->multiprocessing),
785         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
786         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
787         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
d7639a 788         CFG_END()
SP 789     };
790     cfg_t *cfg;    
791     ts_uint retval;
792     cfg = cfg_init(opts, 0);
1ab449 793     retval=cfg_parse(cfg, filename);
d7639a 794     if(retval==CFG_FILE_ERROR){
a6b1b5 795     fatal("No tape file.",100);
d7639a 796     }
SP 797     else if(retval==CFG_PARSE_ERROR){
798     fatal("Invalid tape!",100);
799     }
a2db52 800
d7639a 801     cfg_free(cfg);
40aa5b 802
SP 803
1ab449 804     /* global variables are set automatically */
SP 805     quiet=tape->quiet;
806     return tape;
807 }
d7639a 808
1ab449 809 ts_bool tape_free(ts_tape *tape){
SP 810     free(tape->multiprocessing);
811     free(tape);
812     return TS_SUCCESS;
d7639a 813 }
f74313 814
SP 815
816 ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
817     FILE *fh;
818     ts_uint i, nvtx,nedges,ntria;
819     ts_uint vtxi1,vtxi2;
820     float x,y,z;
821     ts_vertex_list *vlist;
822     fh=fopen(fname, "r");
823         if(fh==NULL){
824                 err("Cannot open file for reading... Nonexistant file?");
825                 return TS_FAIL;
826         }
827     ts_uint retval;
828     retval=fscanf(fh,"%u %u %u",&nvtx, &nedges, &ntria);
829     vesicle->vlist=init_vertex_list(nvtx);
830     vlist=vesicle->vlist;
831     for(i=0;i<nvtx;i++){
8f6a69 832    //     fscanf(fh,"%F %F %F",&vlist->vtx[i]->x,&vlist->vtx[i]->y,&vlist->vtx[i]->z);
f74313 833        retval=fscanf(fh,"%F %F %F",&x,&y,&z);
8f6a69 834         vlist->vtx[i]->x=x;
SP 835         vlist->vtx[i]->y=y;
836         vlist->vtx[i]->z=z;
f74313 837     }
SP 838     for(i=0;i<nedges;i++){
839         retval=fscanf(fh,"%u %u",&vtxi1,&vtxi2);
840         bond_add(vesicle->blist,vesicle->vlist->vtx[vtxi1-1],vesicle->vlist->vtx[vtxi2-1]);
841     }
842     //TODO: neighbours from bonds,
843     //TODO: triangles from neigbours
844
845 //    Don't need to read triangles. Already have enough data
846     /*
847     for(i=0;i<ntria;i++){
848         retval=fscanf(fh,"%u %u %u", &bi1, &bi2, &bi3);
8f6a69 849         vtxi1=vesicle->blist->vertex1->idx;
SP 850         vtxi2=vesicle->blist->vertex1->idx;
f74313 851         
SP 852     }
853     */
41a035 854     if(retval);
f74313 855     fclose(fh);    
SP 856
857
858
859     return TS_SUCCESS;
860 }