Trisurf Monte Carlo simulator
Samo Penic
2014-09-18 c0ae90d68622063a44af443acc01f7e4925fbc56
commit | author | age
f74313 1 #include "general.h"
d7639a 2 #include<stdio.h>
SP 3 #include "io.h"
f74313 4 #include "vertex.h"
SP 5 #include "bond.h"
d7639a 6 #include<string.h>
a6b1b5 7 #include<stdlib.h>
d7639a 8 #include <sys/types.h>
SP 9 #include <dirent.h>
b14a8d 10 #include "initial_distribution.h"
a2db52 11 #include "poly.h"
055dd7 12 #include "cell.h"
083e03 13 #include <getopt.h>
SP 14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17 #include <errno.h>
e9eab4 18 /** DUMP STATE TO DISK DRIVE **/
SP 19
1ab449 20 ts_bool dump_state(ts_vesicle *vesicle, ts_uint iteration){
e9eab4 21
SP 22     /* save current state with wrong pointers. Will fix that later */
23     ts_uint i,j,k;
267db5 24     FILE *fh=fopen(command_line_args.dump_fullfilename,"wb");
e9eab4 25
SP 26     /* dump vesicle */
86f5e7 27     fwrite(vesicle, sizeof(ts_vesicle),1,fh);
e9eab4 28     /* dump vertex list */
SP 29     fwrite(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
30     /* dump bond list */
31     fwrite(vesicle->blist, sizeof(ts_bond_list),1,fh);
32     /* dump triangle list */
33     fwrite(vesicle->tlist, sizeof(ts_triangle_list),1,fh);
34     /* dump cell list */
35     fwrite(vesicle->clist, sizeof(ts_cell_list),1,fh);
36     /* dump poly list */
37     fwrite(vesicle->poly_list, sizeof(ts_poly_list),1,fh);
bcf455 38     /* dump filament list */
M 39     fwrite(vesicle->filament_list, sizeof(ts_poly_list),1,fh);
e9eab4 40     /* level 1 complete */
SP 41
42     /*dump vertices*/
43     for(i=0;i<vesicle->vlist->n;i++){
44         fwrite(vesicle->vlist->vtx[i],sizeof(ts_vertex),1,fh);
45         /* dump pointer offsets for:
46                     neigh
47                     bond
48                     tria
49                     cell is ignored
50         */
51         for(j=0;j<vesicle->vlist->vtx[i]->neigh_no;j++){
3c1ac1 52             fwrite(&vesicle->vlist->vtx[i]->neigh[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 53         }
SP 54         for(j=0;j<vesicle->vlist->vtx[i]->bond_no;j++){
3c1ac1 55             fwrite(&vesicle->vlist->vtx[i]->bond[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 56         }
SP 57         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
3c1ac1 58             fwrite(&vesicle->vlist->vtx[i]->tristar[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 59         }
SP 60     }
61
62     /*dump bonds*/
63     for(i=0;i<vesicle->blist->n;i++){
64         fwrite(vesicle->blist->bond[i],sizeof(ts_bond),1,fh);
65         /* dump pointer offsets for vtx1 and vtx2 */
3c1ac1 66         //off=(ts_ulong)(vesicle->blist->bond[i]->vtx1-vesicle->vlist->vtx[0]);
SP 67         fwrite(&vesicle->blist->bond[i]->vtx1->idx,sizeof(ts_uint),1,fh); 
68         //off=(ts_ulong)(vesicle->blist->bond[i]->vtx2-vesicle->vlist->vtx[0]);
69         fwrite(&vesicle->blist->bond[i]->vtx2->idx,sizeof(ts_uint),1,fh); 
e9eab4 70     }
SP 71
72     /*dump triangles*/
73     for(i=0;i<vesicle->tlist->n;i++){
74         fwrite(vesicle->tlist->tria[i],sizeof(ts_triangle),1,fh);
75         /* dump pointer offsets for vertex */
3c1ac1 76         fwrite(&vesicle->tlist->tria[i]->vertex[0]->idx,sizeof(ts_uint),1,fh); 
SP 77         fwrite(&vesicle->tlist->tria[i]->vertex[1]->idx,sizeof(ts_uint),1,fh); 
78         fwrite(&vesicle->tlist->tria[i]->vertex[2]->idx,sizeof(ts_uint),1,fh); 
e9eab4 79         /* dump pointer offsets for neigh */
SP 80         for(j=0;j<vesicle->tlist->tria[i]->neigh_no;j++){
3c1ac1 81             fwrite(&vesicle->tlist->tria[i]->neigh[j]->idx,sizeof(ts_uint),1,fh); 
e9eab4 82         }
SP 83     }
84
85
86     /*dump polymeres */
87     for(i=0;i<vesicle->poly_list->n;i++){
88         fwrite(vesicle->poly_list->poly[i],sizeof(ts_poly),1,fh);
3c1ac1 89         fwrite(vesicle->poly_list->poly[i]->vlist,sizeof(ts_vertex_list),1,fh);
SP 90         fwrite(vesicle->poly_list->poly[i]->blist,sizeof(ts_bond_list),1,fh);
e9eab4 91     } 
SP 92      
93     /* dump poly vertex(monomer) list*/
94     for(i=0;i<vesicle->poly_list->n;i++){
95         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
96             fwrite(vesicle->poly_list->poly[i]->vlist->vtx[j],sizeof(ts_vertex),1,fh);
97             /* dump offset for neigh and bond */
98             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh_no;k++){
3c1ac1 99                // off=(ts_ulong)(vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]-vesicle->poly_list->poly[i]->vlist->vtx[0]);
SP 100                 fwrite(&vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]->idx,sizeof(ts_uint),1,fh); 
e9eab4 101             }
SP 102             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->bond_no;k++){
3c1ac1 103                 //off=(ts_ulong)(vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]-vesicle->poly_list->poly[i]->blist->bond[0]);
SP 104                 fwrite(&vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]->idx,sizeof(ts_uint),1,fh); 
e9eab4 105             }
SP 106         }
3c1ac1 107     // grafted vtx on vesicle data dump
SP 108         fwrite(&vesicle->poly_list->poly[i]->grafted_vtx->idx, sizeof(ts_uint),1,fh);
e9eab4 109     }
SP 110     /* dump poly bonds between monomers list*/
111     for(i=0;i<vesicle->poly_list->n;i++){
112         for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
113             fwrite(vesicle->poly_list->poly[i]->blist->bond[j],sizeof(ts_bond),1,fh);
114             /* dump vtx1 and vtx2 offsets */
3c1ac1 115             //off=(ts_ulong)(vesicle->poly_list->poly[i]->blist->bond[j]->vtx1-vesicle->poly_list->poly[i]->vlist->vtx[0]);
SP 116             fwrite(&vesicle->poly_list->poly[i]->blist->bond[j]->vtx1->idx,sizeof(ts_uint),1,fh); 
117 //            off=(ts_ulong)(vesicle->poly_list->poly[i]->blist->bond[j]->vtx2-vesicle->poly_list->poly[i]->vlist->vtx[0]);
118             fwrite(&vesicle->poly_list->poly[i]->blist->bond[j]->vtx2->idx,sizeof(ts_uint),1,fh); 
e9eab4 119         }
SP 120     }
121
bcf455 122
M 123   /*dump filamentes grandes svinjas */
124     for(i=0;i<vesicle->filament_list->n;i++){
125         fwrite(vesicle->filament_list->poly[i],sizeof(ts_poly),1,fh);
126         fwrite(vesicle->filament_list->poly[i]->vlist,sizeof(ts_vertex_list),1,fh);
127         fwrite(vesicle->filament_list->poly[i]->blist,sizeof(ts_bond_list),1,fh);
128     } 
129      
130     /* dump filamentes vertex(monomer) list*/
131     for(i=0;i<vesicle->filament_list->n;i++){
132         for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
133             fwrite(vesicle->filament_list->poly[i]->vlist->vtx[j],sizeof(ts_vertex),1,fh);
134             /* dump offset for neigh and bond */
135             for(k=0;k<vesicle->filament_list->poly[i]->vlist->vtx[j]->neigh_no;k++){
136                // off=(ts_ulong)(vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]-vesicle->poly_list->poly[i]->vlist->vtx[0]);
137                 fwrite(&vesicle->filament_list->poly[i]->vlist->vtx[j]->neigh[k]->idx,sizeof(ts_uint),1,fh); 
138             }
139             for(k=0;k<vesicle->filament_list->poly[i]->vlist->vtx[j]->bond_no;k++){
140                 //off=(ts_ulong)(vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]-vesicle->poly_list->poly[i]->blist->bond[0]);
141                 fwrite(&vesicle->filament_list->poly[i]->vlist->vtx[j]->bond[k]->idx,sizeof(ts_uint),1,fh); 
142             }
143         }
144     }
145     /* dump poly bonds between monomers list*/
146     for(i=0;i<vesicle->filament_list->n;i++){
147         for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
148             fwrite(vesicle->filament_list->poly[i]->blist->bond[j],sizeof(ts_bond),1,fh);
149             /* dump vtx1 and vtx2 offsets */
150             //off=(ts_ulong)(vesicle->poly_list->poly[i]->blist->bond[j]->vtx1-vesicle->poly_list->poly[i]->vlist->vtx[0]);
151             fwrite(&vesicle->filament_list->poly[i]->blist->bond[j]->vtx1->idx,sizeof(ts_uint),1,fh); 
152 //            off=(ts_ulong)(vesicle->poly_list->poly[i]->blist->bond[j]->vtx2-vesicle->poly_list->poly[i]->vlist->vtx[0]);
153             fwrite(&vesicle->filament_list->poly[i]->blist->bond[j]->vtx2->idx,sizeof(ts_uint),1,fh); 
154         }
155     }
156
157
158
e9eab4 159 /* pointer offsets for fixing the restored pointers */
SP 160 /* need pointers for 
161     vlist->vtx
162     blist->bond
163     tlist->tria
164     clist->cell
165     poly_list->poly
166     and for each poly:
167         poly_list->poly->vtx
168         poly_list->poly->bond
169 */
170
bd2210 171 //    fwrite(vesicle->clist, sizeof(ts_cell_list),1,  fh);
9166cb 172 /* write tape information on vesicle */
bd2210 173 //    fwrite(vesicle->tape,sizeof(ts_tape),1,fh);
1ab449 174     fwrite(&iteration, sizeof(ts_uint),1,fh);
e9eab4 175     fclose(fh);
SP 176     return TS_SUCCESS;
177 }
178
179
180 /** RESTORE DUMP FROM DISK **/
1ab449 181 ts_vesicle *restore_state(ts_uint *iteration){
e9eab4 182     ts_uint i,j,k;
267db5 183     FILE *fh=fopen(command_line_args.dump_fullfilename,"rb");
3f5c83 184
SP 185     struct stat sb;
267db5 186     if (stat(command_line_args.dump_fullfilename, &sb) == -1) {
3f5c83 187         //dump file does not exist.
SP 188         return NULL;
189     }
190
191     //check if it is regular file
192     if((sb.st_mode & S_IFMT) != S_IFREG) {
193         //dump file is not a regular file.
194         ts_fprintf(stderr,"Dump file is not a regular file!\n");
195         return NULL;
196     }
197
e9eab4 198     ts_uint retval;
3c1ac1 199     ts_uint idx;
e9eab4 200
SP 201 /* we restore all the data from the dump */
202     /* restore vesicle */
203     ts_vesicle *vesicle=(ts_vesicle *)calloc(1,sizeof(ts_vesicle));
86f5e7 204     retval=fread(vesicle, sizeof(ts_vesicle),1,fh);
SP 205 //    fprintf(stderr,"was here! %e\n",vesicle->dmax);
206
e9eab4 207     /* restore vertex list */
SP 208     vesicle->vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
209     retval=fread(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
210     /* restore bond list */
211     vesicle->blist=(ts_bond_list *)malloc(sizeof(ts_bond_list));
212     retval=fread(vesicle->blist, sizeof(ts_bond_list),1,fh);
213     /* restore triangle list */
214     vesicle->tlist=(ts_triangle_list *)malloc(sizeof(ts_triangle_list));
215     retval=fread(vesicle->tlist, sizeof(ts_triangle_list),1,fh);
216     /* restore cell list */
217     vesicle->clist=(ts_cell_list *)malloc(sizeof(ts_cell_list));
218     retval=fread(vesicle->clist, sizeof(ts_cell_list),1,fh);
219     /* restore poly list */
220     vesicle->poly_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
221     retval=fread(vesicle->poly_list, sizeof(ts_poly_list),1,fh);
bcf455 222     /* restore filament list */
M 223     vesicle->filament_list=(ts_poly_list *)calloc(1,sizeof(ts_poly_list));
224     retval=fread(vesicle->filament_list, sizeof(ts_poly_list),1,fh);
e9eab4 225     /* level 1 complete */
SP 226
3c1ac1 227 /* prerequisity. Bonds must be malloced before vertexes are recreated */
SP 228   vesicle->blist->bond=(ts_bond **)calloc(vesicle->blist->n,sizeof(ts_bond *));
229     for(i=0;i<vesicle->blist->n;i++){
230         vesicle->blist->bond[i]=(ts_bond *)malloc(sizeof(ts_bond));
231     }
232 /* prerequisity. Triangles must be malloced before vertexes are recreated */
233   vesicle->tlist->tria=(ts_triangle **)calloc(vesicle->tlist->n,sizeof(ts_triangle *));
234     for(i=0;i<vesicle->tlist->n;i++){
235         vesicle->tlist->tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle));
236 }
a37ba2 237 /* prerequisity. Vertices must be malloced before vertexes are recreated */
e9eab4 238     vesicle->vlist->vtx=(ts_vertex **)calloc(vesicle->vlist->n,sizeof(ts_vertex *));
a37ba2 239  for(i=0;i<vesicle->vlist->n;i++){
e9eab4 240         vesicle->vlist->vtx[i]=(ts_vertex *)malloc(sizeof(ts_vertex));
a37ba2 241  }
SP 242   /*restore vertices*/
243     for(i=0;i<vesicle->vlist->n;i++){
e9eab4 244         retval=fread(vesicle->vlist->vtx[i],sizeof(ts_vertex),1,fh);
SP 245         /*restore neigh, bond, tristar. Ignoring cell */
246         vesicle->vlist->vtx[i]->neigh=(ts_vertex **)calloc(vesicle->vlist->vtx[i]->neigh_no, sizeof(ts_vertex *));
247         for(j=0;j<vesicle->vlist->vtx[i]->neigh_no;j++){
3c1ac1 248             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 249             vesicle->vlist->vtx[i]->neigh[j]=vesicle->vlist->vtx[idx];
e9eab4 250         }
SP 251         vesicle->vlist->vtx[i]->bond=(ts_bond **)calloc(vesicle->vlist->vtx[i]->bond_no, sizeof(ts_bond *));
252         for(j=0;j<vesicle->vlist->vtx[i]->bond_no;j++){
3c1ac1 253             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 254 /* 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 */
255             vesicle->vlist->vtx[i]->bond[j]=vesicle->blist->bond[idx];    
e9eab4 256         }
SP 257
258         vesicle->vlist->vtx[i]->tristar=(ts_triangle **)calloc(vesicle->vlist->vtx[i]->tristar_no, sizeof(ts_triangle *));
259         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
3c1ac1 260             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 261 /* same comment as above */
262             vesicle->vlist->vtx[i]->tristar[j]=vesicle->tlist->tria[idx];
e9eab4 263         }
SP 264
265     }
266
267     /*restore bonds*/
3c1ac1 268    // vesicle->blist->bond=(ts_bond **)calloc(vesicle->blist->n,sizeof(ts_bond *)); // done before.
e9eab4 269     for(i=0;i<vesicle->blist->n;i++){
3c1ac1 270      //   vesicle->blist->bond[i]=(ts_bond *)malloc(sizeof(ts_bond)); //done before.
e9eab4 271         retval=fread(vesicle->blist->bond[i],sizeof(ts_bond),1,fh);
SP 272         /* restore vtx1 and vtx2 */
3c1ac1 273         retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 274         vesicle->blist->bond[i]->vtx1=vesicle->vlist->vtx[idx];
275         retval=fread(&idx,sizeof(ts_uint),1,fh);
276         vesicle->blist->bond[i]->vtx2=vesicle->vlist->vtx[idx];
e9eab4 277     }
SP 278
279     /*restore triangles*/
3c1ac1 280 //    vesicle->tlist->tria=(ts_triangle **)calloc(vesicle->tlist->n,sizeof(ts_triangle *)); // done before
e9eab4 281     for(i=0;i<vesicle->tlist->n;i++){
3c1ac1 282  //       vesicle->tlist->tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle)); // done before
e9eab4 283         retval=fread(vesicle->tlist->tria[i],sizeof(ts_triangle),1,fh);
SP 284         /* restore pointers for vertices */
3c1ac1 285         retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 286         vesicle->tlist->tria[i]->vertex[0]=vesicle->vlist->vtx[idx];
287         retval=fread(&idx,sizeof(ts_uint),1,fh);
288         vesicle->tlist->tria[i]->vertex[1]=vesicle->vlist->vtx[idx];
289         retval=fread(&idx,sizeof(ts_uint),1,fh);
290         vesicle->tlist->tria[i]->vertex[2]=vesicle->vlist->vtx[idx];
e9eab4 291         /* restore pointers for neigh */
3c1ac1 292      vesicle->tlist->tria[i]->neigh=(ts_triangle **)malloc(vesicle->tlist->tria[i]->neigh_no*sizeof(ts_triangle *));
e9eab4 293         for(j=0;j<vesicle->tlist->tria[i]->neigh_no;j++){
3c1ac1 294             retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 295             vesicle->tlist->tria[i]->neigh[j]=vesicle->tlist->tria[idx];
e9eab4 296         }
SP 297
298     }
299    
300     /*restore cells */
301 /*TODO: do we need to recalculate cells here? */
302 /*    vesicle->clist->cell=(ts_cell **)malloc(vesicle->clist->cellno*sizeof(ts_cell *));
303     for(i=0;i<vesicle->clist->cellno;i++){
304         vesicle->clist->cell[i]=(ts_cell *)malloc(sizeof(ts_cell));
305         retval=fread(vesicle->clist->cell[i],sizeof(ts_cell),1,fh);
306     }
307 */
308     /*restore polymeres */
309     vesicle->poly_list->poly = (ts_poly **)calloc(vesicle->poly_list->n,sizeof(ts_poly *));
310     for(i=0;i<vesicle->poly_list->n;i++){
311         vesicle->poly_list->poly[i]=(ts_poly *)calloc(1,sizeof(ts_poly));
312         retval=fread(vesicle->poly_list->poly[i],sizeof(ts_poly),1,fh);
3c1ac1 313         vesicle->poly_list->poly[i]->vlist=(ts_vertex_list *)calloc(1,sizeof(ts_vertex_list));
SP 314         retval=fread(vesicle->poly_list->poly[i]->vlist,sizeof(ts_vertex_list),1,fh);
315         vesicle->poly_list->poly[i]->blist=(ts_bond_list *)calloc(1,sizeof(ts_bond_list));
316         retval=fread(vesicle->poly_list->poly[i]->blist,sizeof(ts_bond_list),1,fh);
317     /* initialize adress space for pointers that will hold specific vertices (monomers) and bonds */
318         vesicle->poly_list->poly[i]->vlist->vtx=(ts_vertex **)calloc(vesicle->poly_list->poly[i]->vlist->n,sizeof(ts_vertex *));
319         vesicle->poly_list->poly[i]->blist->bond=(ts_bond **)calloc(vesicle->poly_list->poly[i]->blist->n,sizeof(ts_bond *));
320      for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
321             vesicle->poly_list->poly[i]->vlist->vtx[j]=(ts_vertex *)malloc(sizeof(ts_vertex));
322     }
323     for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
324             vesicle->poly_list->poly[i]->blist->bond[j]=(ts_bond *)malloc(sizeof(ts_bond));
325     }
326
e9eab4 327     } 
3c1ac1 328
e9eab4 329      
SP 330     /* restore poly vertex(monomer) list*/
331     for(i=0;i<vesicle->poly_list->n;i++){
332         for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
333             retval=fread(vesicle->poly_list->poly[i]->vlist->vtx[j],sizeof(ts_vertex),1,fh);
3c1ac1 334                 
e9eab4 335             /* restore neigh and bonds */
SP 336             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 *));
337             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh_no;k++){
3c1ac1 338                 retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 339                 vesicle->poly_list->poly[i]->vlist->vtx[j]->neigh[k]=vesicle->poly_list->poly[i]->vlist->vtx[idx];
e9eab4 340             }
SP 341             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 *));
342             for(k=0;k<vesicle->poly_list->poly[i]->vlist->vtx[j]->bond_no;k++){
3c1ac1 343                 retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 344                 vesicle->poly_list->poly[i]->vlist->vtx[j]->bond[k]=vesicle->poly_list->poly[i]->blist->bond[idx];
e9eab4 345             }
SP 346
347         }
3c1ac1 348     /* restore grafted vtx on vesicle and grafted_poly */
SP 349                 retval=fread(&idx,sizeof(ts_uint),1,fh);
350         vesicle->vlist->vtx[idx]->grafted_poly=vesicle->poly_list->poly[i];
351         vesicle->poly_list->poly[i]->grafted_vtx=vesicle->vlist->vtx[idx];    
e9eab4 352     }
3c1ac1 353
e9eab4 354     /* restore poly bonds between monomers list*/
SP 355     for(i=0;i<vesicle->poly_list->n;i++){
356         for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 357        //     vesicle->poly_list->poly[i]->blist->bond[j]=(ts_bond *)malloc(sizeof(ts_bond));
e9eab4 358             retval=fread(vesicle->poly_list->poly[i]->blist->bond[j],sizeof(ts_bond),1,fh);
SP 359             /* restore vtx1 and vtx2 */
3c1ac1 360                 retval=fread(&idx,sizeof(ts_uint),1,fh);
SP 361                 vesicle->poly_list->poly[i]->blist->bond[j]->vtx1=vesicle->poly_list->poly[i]->vlist->vtx[idx];
362                 retval=fread(&idx,sizeof(ts_uint),1,fh);
363                 vesicle->poly_list->poly[i]->blist->bond[j]->vtx2=vesicle->poly_list->poly[i]->vlist->vtx[idx];
e9eab4 364         }
SP 365     }
366
bcf455 367     /*restore filaments */
M 368     vesicle->filament_list->poly = (ts_poly **)calloc(vesicle->filament_list->n,sizeof(ts_poly *));
369     for(i=0;i<vesicle->filament_list->n;i++){
370         vesicle->filament_list->poly[i]=(ts_poly *)calloc(1,sizeof(ts_poly));
371         retval=fread(vesicle->filament_list->poly[i],sizeof(ts_poly),1,fh);
372         vesicle->filament_list->poly[i]->vlist=(ts_vertex_list *)calloc(1,sizeof(ts_vertex_list));
373         retval=fread(vesicle->filament_list->poly[i]->vlist,sizeof(ts_vertex_list),1,fh);
374         vesicle->filament_list->poly[i]->blist=(ts_bond_list *)calloc(1,sizeof(ts_bond_list));
375         retval=fread(vesicle->filament_list->poly[i]->blist,sizeof(ts_bond_list),1,fh);
376     /* initialize adress space for pointers that will hold specific vertices (monomers) and bonds */
377         vesicle->filament_list->poly[i]->vlist->vtx=(ts_vertex **)calloc(vesicle->filament_list->poly[i]->vlist->n,sizeof(ts_vertex *));
378         vesicle->filament_list->poly[i]->blist->bond=(ts_bond **)calloc(vesicle->filament_list->poly[i]->blist->n,sizeof(ts_bond *));
379      for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
380             vesicle->filament_list->poly[i]->vlist->vtx[j]=(ts_vertex *)malloc(sizeof(ts_vertex));
381     }
382     for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
383             vesicle->filament_list->poly[i]->blist->bond[j]=(ts_bond *)malloc(sizeof(ts_bond));
384     }
385
386     } 
387
388      
389     /* restore poly vertex(monomer) list*/
390     for(i=0;i<vesicle->filament_list->n;i++){
391         for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
392             retval=fread(vesicle->filament_list->poly[i]->vlist->vtx[j],sizeof(ts_vertex),1,fh);
393                 
394             /* restore neigh and bonds */
395             vesicle->filament_list->poly[i]->vlist->vtx[j]->neigh=(ts_vertex **)calloc(vesicle->filament_list->poly[i]->vlist->vtx[j]->neigh_no, sizeof(ts_vertex *));
396             for(k=0;k<vesicle->filament_list->poly[i]->vlist->vtx[j]->neigh_no;k++){
397                 retval=fread(&idx,sizeof(ts_uint),1,fh);
398                 vesicle->filament_list->poly[i]->vlist->vtx[j]->neigh[k]=vesicle->filament_list->poly[i]->vlist->vtx[idx];
399             }
400             vesicle->filament_list->poly[i]->vlist->vtx[j]->bond=(ts_bond **)calloc(vesicle->filament_list->poly[i]->vlist->vtx[j]->bond_no, sizeof(ts_bond *));
401             for(k=0;k<vesicle->filament_list->poly[i]->vlist->vtx[j]->bond_no;k++){
402                 retval=fread(&idx,sizeof(ts_uint),1,fh);
403                 vesicle->filament_list->poly[i]->vlist->vtx[j]->bond[k]=vesicle->filament_list->poly[i]->blist->bond[idx];
404             }
405
406         }
407     }
408
409     /* restore poly bonds between monomers list*/
410     for(i=0;i<vesicle->filament_list->n;i++){
411         for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
412        //     vesicle->poly_list->poly[i]->blist->bond[j]=(ts_bond *)malloc(sizeof(ts_bond));
413             retval=fread(vesicle->filament_list->poly[i]->blist->bond[j],sizeof(ts_bond),1,fh);
414             /* restore vtx1 and vtx2 */
415                 retval=fread(&idx,sizeof(ts_uint),1,fh);
416                 vesicle->filament_list->poly[i]->blist->bond[j]->vtx1=vesicle->filament_list->poly[i]->vlist->vtx[idx];
417                 retval=fread(&idx,sizeof(ts_uint),1,fh);
418                 vesicle->filament_list->poly[i]->blist->bond[j]->vtx2=vesicle->filament_list->poly[i]->vlist->vtx[idx];
419         }
420     }
267db5 421     vesicle->tape=parsetape(command_line_args.tape_fullfilename);
063289 422 // recreating space for cells // 
055dd7 423     vesicle->clist=init_cell_list(vesicle->tape->ncxmax, vesicle->tape->ncymax, vesicle->tape->nczmax, vesicle->tape->stepsize);
364c30 424     vesicle->clist->max_occupancy=8;
47a7ac 425 //    vesicle->tape=(ts_tape *)malloc(sizeof(ts_tape));
SP 426 //    retval=fread(vesicle->tape, sizeof(ts_tape),1,fh);
1ab449 427     retval=fread(iteration,sizeof(ts_uint),1,fh);
e9eab4 428     if(retval); 
SP 429     fclose(fh);
430     return vesicle;
431 }
432
433
434
083e03 435 ts_bool parse_args(int argc, char **argv){
3f5c83 436     int c, retval;
SP 437     struct stat sb;
438     sprintf(command_line_args.path, "./"); //clear string;
7b0c07 439     sprintf(command_line_args.output_fullfilename,"output.pvd");
SP 440     sprintf(command_line_args.dump_fullfilename,"dump.bin");
441     sprintf(command_line_args.tape_fullfilename,"tape");
1bf3c3 442     sprintf(command_line_args.tape_templatefull,"./tape");
3f5c83 443             FILE *file;
SP 444     
083e03 445 while (1)
SP 446      {
447        static struct option long_options[] =
448          {
8a6614 449            {"force-from-tape", no_argument,       &(command_line_args.force_from_tape), 1},
SP 450        {"reset-iteration-count", no_argument, &(command_line_args.reset_iteration_count), 1},
083e03 451            {"tape",     no_argument,       0, 't'},
SP 452            {"output-file",  required_argument, 0, 'o'},
453            {"directory",  required_argument, 0, 'd'},
3f5c83 454            {"dump-filename", required_argument,0, 'f'},
7b0c07 455            {"tape-options",required_argument,0,'c'},
1bf3c3 456            {"tape-template", required_argument,0,0},
083e03 457            {0, 0, 0, 0}
SP 458          };
459        /* getopt_long stores the option index here. */
460        int option_index = 0;
461
7b0c07 462        c = getopt_long (argc, argv, "d:f:o:t:c:",
083e03 463                         long_options, &option_index);
SP 464
465        /* Detect the end of the options. */
466        if (c == -1)
467          break;
468
469        switch (c)
470          {
471          case 0:
472            /* If this option set a flag, do nothing else now. */
473            if (long_options[option_index].flag != 0)
474              break;
1bf3c3 475 /*           printf ("option %s", long_options[option_index].name);
083e03 476            if (optarg)
1bf3c3 477              printf (" with arg %s", optarg); 
SP 478            printf ("\n"); */
479             //TODO: find a better way.
480             if(strcmp(long_options[option_index].name,"tape-template")==0){
481                 strcpy(command_line_args.tape_templatefull,optarg);
482             }
083e03 483            break;
SP 484
7b0c07 485          case 'c':
SP 486               strcpy(command_line_args.tape_opts,optarg);
487             break;
488          case 't': //tape
3f5c83 489                 strcpy(command_line_args.tape_fullfilename,optarg);
083e03 490            break;
SP 491
7b0c07 492          case 'o':  //set filename of master pvd output file
3f5c83 493             strcpy(command_line_args.output_fullfilename, optarg);
SP 494             break;
083e03 495
SP 496          case 'd':
497             //check if directory exists. If not create one. If creation is
498             //successful, set directory for output files.
499             //printf ("option -d with value `%s'\n", optarg);
3f5c83 500             if (stat(optarg, &sb) == -1) {
SP 501                 //directory does not exist
083e03 502                 retval=mkdir(optarg, 0700);
SP 503                 if(retval){
3f5c83 504                     fatal("Could not create requested directory. Check if you have permissions",1);
083e03 505                 }
SP 506             }
3f5c83 507             //check if is a proper directory
SP 508             else if((sb.st_mode & S_IFMT) != S_IFDIR) {
509                 //it is not a directory. fatal error.
510                 ts_fprintf(stderr,"%s is not a directory!\n",optarg);
511                 fatal("Cannot continue",1);
083e03 512             }
3f5c83 513             strcpy(command_line_args.path, optarg);
083e03 514            break;
SP 515
516         case 'f':
3f5c83 517             strcpy(command_line_args.dump_fullfilename, optarg);
083e03 518             break;
SP 519
520          case '?':
521            /* getopt_long already printed an error message. */
522
523             ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
524             fatal("Ooops, read help first",1);
525            break;
526
527          default:
528            exit (1);
529          }
530      }
531
7b0c07 532 //Here we set correct values for full filenames!
SP 533     char *buffer=(char *)malloc(10000*sizeof(char));
534     //correct the path and add trailing /
535     if(command_line_args.path[strlen(command_line_args.path)-1]!='/') strcat(command_line_args.path,"/");
536    
537 /* master pvd output file */ 
538     strcpy(buffer,command_line_args.path);
539     strcat(buffer,command_line_args.output_fullfilename);
540     if ((file = fopen(buffer, "w")) == NULL) {
541                 fprintf(stderr,"Could not create output file %s!\n", buffer);
542                 fatal("Please specify correct output file or check permissions of the file",1);
1bf3c3 543                 //there is a tape template. make a copy into desired directory
7b0c07 544                 
SP 545             } else {
546                 fclose(file);
547                 strcpy(command_line_args.output_fullfilename,buffer);
548             }
549
550 /* tape file */
551     strcpy(buffer,command_line_args.path);
552     strcat(buffer,command_line_args.tape_fullfilename);
553     if (stat(buffer, &sb) == -1) {
1bf3c3 554
SP 555                 //tape does not exist. does tape template exist?
556                 if(stat(command_line_args.tape_templatefull, &sb)==-1){ 
557                     ts_fprintf(stderr,"Tape '%s' does not exist and no tape template was specified (or does not exist)!\n",buffer);
558                     fatal("Please select correct tape or check permissions of the file",1);
559                 } else {
560                     //tape template found
561                     fatal("Samo did not program template copy yet",1); 
562                 }
7b0c07 563             } else {
SP 564                 strcpy(command_line_args.tape_fullfilename,buffer);
565             }
566
567
568 /* dump file */
569             strcpy(buffer,command_line_args.path);
570             strcat(buffer,command_line_args.dump_fullfilename);
571             //check if dump file exist first.
572             if (stat(buffer, &sb) == -1) {
573                 //no dump file. check if we can create one.
574                 if ((file = fopen(buffer, "w")) == NULL) {
575                     fprintf(stderr,"Could not create dump file '%s'!\n",buffer);
576                     fatal("Please specify correct dump file or check permissions of the file",1);
577                 } else {
578                     fclose(file);
579                     //good, file is writeable. delete it for now.
580                     remove(buffer);
581                 }
582             }
583             strcpy(command_line_args.dump_fullfilename, buffer);
584
585
586     free(buffer);
083e03 587     return TS_SUCCESS;
SP 588
589 }
590
591
592
d7639a 593 ts_bool print_vertex_list(ts_vertex_list *vlist){
SP 594     ts_uint i;
595     printf("Number of vertices: %u\n",vlist->n);
596     for(i=0;i<vlist->n;i++){
a6b1b5 597         printf("%u: %f %f %f\n",
8f6a69 598 vlist->vtx[i]->idx,vlist->vtx[i]->x, vlist->vtx[i]->y, vlist->vtx[i]->z);
d7639a 599     }
SP 600     return TS_SUCCESS;
601 }
602
603 ts_bool print_vertex_neighbours(ts_vertex_list *vlist){
604     ts_uint i,j;
a6b1b5 605     ts_vertex **vtx=vlist->vtx;
d7639a 606     printf("Vertex id(neigh no): (neighvertex coord) (neighvertex coord) ...\n");
SP 607     for(i=0;i<vlist->n;i++){
8f6a69 608         printf("%u(%u): ",vtx[i]->idx,vtx[i]->neigh_no);
SP 609         for(j=0;j<vtx[i]->neigh_no;j++){
610             printf("(%f,%f,%f)",vtx[i]->neigh[j]->x,
611 vtx[i]->neigh[j]->y,vtx[i]->neigh[j]->z);
d7639a 612         }
SP 613         printf("\n");
614     }
615
616 return TS_SUCCESS;
617 }
618
619 ts_bool write_vertex_fcompat_file(ts_vertex_list *vlist,ts_char *filename){
a6b1b5 620     ts_vertex **vtx=vlist->vtx;
d7639a 621     ts_uint i;
SP 622     FILE *fh;
623     
624     fh=fopen(filename, "w");
625     if(fh==NULL){
626         err("Cannot open file %s for writing");
627         return TS_FAIL;
628     }
629     for(i=0;i<vlist->n;i++)
8f6a69 630         fprintf(fh," %E\t%E\t%E\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 631
SP 632     fclose(fh);
633 return TS_SUCCESS;
634 }
635
636
637 ts_bool fprint_vertex_list(FILE *fh,ts_vertex_list *vlist){
638     ts_uint i,j;
639     for(i=0;i<vlist->n;i++){
8f6a69 640         fprintf(fh," %.17E\t%.17E\t%.17E\t%u\n",vlist->vtx[i]->x,
SP 641             vlist->vtx[i]->y, vlist->vtx[i]->z,
642             vlist->vtx[i]->neigh_no);
643         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
644             fprintf(fh,"\t%u",(ts_uint)(vlist->vtx[i]->neigh[j]->idx));
a6b1b5 645         //-vlist->vtx+1));
d7639a 646         }
SP 647         fprintf(fh,"\n");
648     }
649     return TS_SUCCESS;
650 }
651
652 ts_bool fprint_tristar(FILE *fh, ts_vesicle *vesicle){
653     ts_uint i,j;
a6b1b5 654     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 655         fprintf(fh,"\t%u",vesicle->vlist->vtx[i]->tristar_no);
SP 656         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
657             fprintf(fh,"\t%u",(ts_uint)(vesicle->vlist->vtx[i]->tristar[j]->idx));//-vesicle->tlist->tria+1));
d7639a 658         }
SP 659         fprintf(fh,"\n");
660     }
661     return TS_SUCCESS;
662 }
663
664 ts_bool fprint_triangle_list(FILE *fh, ts_vesicle *vesicle){
a6b1b5 665         ts_triangle_list *tlist=vesicle->tlist;
d7639a 666       ts_uint i,j;
SP 667     for(i=0;i<tlist->n;i++){
41a035 668         fprintf(fh,"\t%u",tlist->tria[i]->neigh_no);
SP 669         for(j=0;j<tlist->tria[i]->neigh_no;j++){
670             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->neigh[j]->idx));//-tlist->tria+1)); 
d7639a 671         }
SP 672         fprintf(fh,"\n");
673             for(j=0;j<3;j++){
41a035 674             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->vertex[j]->idx));//-vesicle->vlist->vtx+1)); 
d7639a 675             }
SP 676         fprintf(fh,"\n");
41a035 677         fprintf(fh,"%.17E\t%.17E\t%.17E\n",tlist->tria[i]->xnorm,
SP 678 tlist->tria[i]->ynorm,tlist->tria[i]->znorm);
d7639a 679         fprintf(fh,"0.00000000000000000\n0.00000000000000000\n");
SP 680     }
681     return TS_SUCCESS;
682 }
683
684 ts_bool fprint_vertex_data(FILE *fh,ts_vertex_list *vlist){
685     ts_uint i,j;
686     for(i=0;i<vlist->n;i++){
687         fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
8f6a69 688         vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
SP 689         vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
cef6ca 690         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 691             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
d7639a 692         }
SP 693             fprintf(fh,"\n");
cef6ca 694         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 695             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
d7639a 696         }
SP 697             fprintf(fh,"\n");
698     }
699     return TS_SUCCESS;
700 }
701
702 ts_bool fprint_bonds(FILE *fh,ts_vesicle *vesicle){
703     ts_uint i;
a6b1b5 704     for(i=0;i<vesicle->blist->n;i++){
e016c4 705         fprintf(fh,"\t%u\t%u\n",(ts_uint)(vesicle->blist->bond[i]->vtx1->idx),
a6b1b5 706 //-vesicle->vlist->vtx+1),
e016c4 707         (ts_uint)(vesicle->blist->bond[i]->vtx2->idx));
a6b1b5 708     //-vesicle->vlist.vtx+1));
d7639a 709     }
SP 710     return TS_SUCCESS;
711 }
712
713
714 ts_bool write_dout_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
715     FILE *fh;
716     fh=fopen(filename, "w");
717     if(fh==NULL){
718         err("Cannot open file %s for writing");
719         return TS_FAIL;
720     }
721     fprintf(fh,"%.17E\n%.17E\n",vesicle->stepsize,vesicle->dmax);
a6b1b5 722     fprint_vertex_list(fh,vesicle->vlist);
d7639a 723     fprint_tristar(fh,vesicle);
SP 724     fprint_triangle_list(fh,vesicle);
a6b1b5 725     fprint_vertex_data(fh,vesicle->vlist);
d7639a 726     fprint_bonds(fh,vesicle);
SP 727     fclose(fh);    
728     return TS_SUCCESS;
729 }
730
731 ts_bool read_tape_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
732     FILE *fh;
733     char line[255];
734     fh=fopen(filename, "r");
735         if(fh==NULL){
736                 err("Cannot open file for reading... Nonexistant file?");
737                 return TS_FAIL;
738         }
a6b1b5 739     ts_uint retval=1;
d7639a 740     while(retval!=EOF){
SP 741         retval=fscanf(fh,"%s",line);
742         
743         fprintf(stderr,"%s",line);
744     }    
745     fclose(fh);    
746     return TS_SUCCESS;
747 }
748
749 ts_bool write_master_xml_file(ts_char *filename){
750      FILE *fh;
751     ts_char *i,*j;
752     ts_uint tstep;
753     ts_char *number;
754         fh=fopen(filename, "w");
755         if(fh==NULL){
756                 err("Cannot open file %s for writing");
757                 return TS_FAIL;
758         }
759
760     fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
267db5 761     DIR *dir = opendir(command_line_args.path);
d7639a 762     if(dir){
SP 763         struct dirent *ent;
764         tstep=0;
765         while((ent = readdir(dir)) != NULL)
766         {
767             i=rindex(ent->d_name,'.');
768             if(i==NULL) continue;
769             if(strcmp(i+1,"vtu")==0){
770                     j=rindex(ent->d_name,'_');
771                     if(j==NULL) continue;
772                     number=strndup(j+1,j-i); 
a6b1b5 773                 fprintf(fh,"<DataSet timestep=\"%u\" group=\"\" part=\"0\" file=\"%s\"/>\n",atoi(number),ent->d_name);
d7639a 774                 tstep++;
SP 775                     free(number);
776             }  
777         }
778     }
f74313 779     free(dir);
d7639a 780     fprintf(fh,"</Collection>\n</VTKFile>\n");
SP 781     fclose(fh);
782     return TS_SUCCESS;
783 }
784
785 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno){
a6b1b5 786     ts_vertex_list *vlist=vesicle->vlist;
SP 787     ts_bond_list *blist=vesicle->blist;
788     ts_vertex **vtx=vlist->vtx;
40aa5b 789     ts_uint i,j;
267db5 790         char filename[10000];
SP 791         char just_name[255];
d7639a 792     FILE *fh;
267db5 793         strcpy(filename,command_line_args.path);
SP 794         sprintf(just_name,"timestep_%.6u.vtu",timestepno);
795         strcat(filename,just_name);
d7639a 796
SP 797     fh=fopen(filename, "w");
798     if(fh==NULL){
799         err("Cannot open file %s for writing");
800         return TS_FAIL;
801     }
802     /* Here comes header of the file */
40aa5b 803
SP 804     //find number of extra vtxs and bonds of polymeres
58230a 805     ts_uint monono=0, polyno=0, poly_idx=0, filno=0, fonono=0;
M 806     ts_bool poly=0, fil=0;
40aa5b 807     if(vesicle->poly_list!=NULL){
SP 808         if(vesicle->poly_list->poly[0]!=NULL){
809         polyno=vesicle->poly_list->n;
810         monono=vesicle->poly_list->poly[0]->vlist->n;
811         poly=1;
812         }
813     }
58230a 814
M 815     if(vesicle->filament_list!=NULL){
816         if(vesicle->filament_list->poly[0]!=NULL){
817         filno=vesicle->filament_list->n;
818         fonono=vesicle->filament_list->poly[0]->vlist->n;
819         fil=1;
820         }
821     }
822
d7639a 823     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n <UnstructuredGrid>\n");
58230a 824     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno+fonono*filno, blist->n+monono*polyno+filno*(fonono-1));
d7639a 825     fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
SP 826        for(i=0;i<vlist->n;i++){
a6b1b5 827         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 828     }
40aa5b 829     //polymeres
SP 830     if(poly){
3c1ac1 831         poly_idx=vlist->n;
40aa5b 832         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 833             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
58230a 834                 fprintf(fh,"%u ", poly_idx);
M 835             }
836         }
837     }
838     //filaments
839     if(fil){
840         poly_idx=vlist->n+monono*polyno;
841         for(i=0;i<vesicle->filament_list->n;i++){
842             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
843     //    fprintf(stderr,"was here\n");
3c1ac1 844                 fprintf(fh,"%u ", poly_idx);
40aa5b 845             }
SP 846         }
847     }
d7639a 848
SP 849     fprintf(fh,"</DataArray>\n</PointData>\n<CellData>\n</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
850     for(i=0;i<vlist->n;i++){
8f6a69 851         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 852     }
SP 853     //polymeres
854     if(poly){
855         for(i=0;i<vesicle->poly_list->n;i++){
856             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
857                 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 );
58230a 858             }
M 859         }
860     }
861     //filaments
862     if(fil){
863         for(i=0;i<vesicle->filament_list->n;i++){
864             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
865                 fprintf(fh,"%e %e %e\n", vesicle->filament_list->poly[i]->vlist->vtx[j]->x,vesicle->filament_list->poly[i]->vlist->vtx[j]->y, vesicle->filament_list->poly[i]->vlist->vtx[j]->z );
40aa5b 866             }
SP 867         }
d7639a 868     }
SP 869
870     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
871     for(i=0;i<blist->n;i++){
e016c4 872             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 873     }
40aa5b 874     //polymeres
SP 875     if(poly){
3c1ac1 876         poly_idx=vlist->n;
40aa5b 877         for(i=0;i<vesicle->poly_list->n;i++){
SP 878             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 879 //                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 880                 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 881             }
SP 882     //grafted bonds
3c1ac1 883         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 884         }
SP 885
886     }
887     
58230a 888     //filaments
M 889     if(fil){
890         poly_idx=vlist->n+monono*polyno;
891         for(i=0;i<vesicle->filament_list->n;i++){
892             for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
893                 fprintf(fh,"%u %u\n", vesicle->filament_list->poly[i]->blist->bond[j]->vtx1->idx+vlist->n+monono*polyno+i*fonono,vesicle->filament_list->poly[i]->blist->bond[j]->vtx2->idx+vlist->n+monono*polyno+i*fonono);
894 //        fprintf(stderr,"was here\n");
895             
896             }
897         }
898
899     }
40aa5b 900
d7639a 901     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
58230a 902     for (i=2;i<(blist->n+monono*polyno+(fonono-1)*filno)*2+1;i+=2){
d7639a 903     fprintf(fh,"%u ",i);
SP 904     }
905     fprintf(fh,"\n");
906     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
58230a 907      for (i=0;i<blist->n+monono*polyno+fonono*filno;i++){
d7639a 908         fprintf(fh,"3 ");
SP 909     }
910
911     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
912     fclose(fh);
913     return TS_SUCCESS;
914
915 }
916
917 ts_bool write_vertex_vtk_file(ts_vesicle *vesicle,ts_char *filename, ts_char *text){
a6b1b5 918     ts_vertex_list *vlist=vesicle->vlist;
SP 919     ts_bond_list *blist=vesicle->blist;
920     ts_vertex **vtx=vlist->vtx;
921     ts_uint i;
d7639a 922     FILE *fh;
SP 923     
924     fh=fopen(filename, "w");
925     if(fh==NULL){
926         err("Cannot open file %s for writing");
927         return TS_FAIL;
928     }
929     /* Here comes header of the file */
930 //    fprintf(stderr,"NSHELL=%u\n",nshell);
931     fprintf(fh, "# vtk DataFile Version 2.0\n");
932     /* TODO: Do a sanity check on text. Max 255 char, must not me \n terminated */ 
933     fprintf(fh, "%s\n", text);
934     fprintf(fh,"ASCII\n");
935     fprintf(fh,"DATASET UNSTRUCTURED_GRID\n");
936     fprintf(fh,"POINTS %u double\n", vlist->n);
937     for(i=0;i<vlist->n;i++){
8f6a69 938         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 939     }
SP 940     
941     fprintf(fh,"CELLS %u %u\n",blist->n,3*blist->n);
942     for(i=0;i<blist->n;i++){
e016c4 943             fprintf(fh,"2 %u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 944     }
SP 945     fprintf(fh,"CELL_TYPES %u\n",blist->n);
946     for(i=0;i<blist->n;i++)
947         fprintf(fh,"3\n");
948
949     fprintf(fh,"POINT_DATA %u\n", vlist->n);
950     fprintf(fh,"SCALARS scalars long 1\n");
951     fprintf(fh,"LOOKUP_TABLE default\n");
952
953     for(i=0;i<vlist->n;i++)
8f6a69 954         fprintf(fh,"%u\n",vtx[i]->idx);
d7639a 955
SP 956     fclose(fh);
957     return TS_SUCCESS;
958 }
959
960
961
144784 962 ts_bool write_pov_file(ts_vesicle *vesicle, char *filename){
SP 963     FILE *fh;
964     ts_uint i;
965     
966     fh=fopen(filename, "w");
967     if(fh==NULL){
968         err("Cannot open file %s for writing");
969         return TS_FAIL;
970     }
971
972     for(i=0;i<vesicle->tlist->n;i++){
973     
974     fprintf(fh,"\ttriangle {");
975     fprintf(fh,"\t<%e,%e,%e> <%e,%e,%e> <%e,%e,%e> }\n", 
976     vesicle->tlist->tria[i]->vertex[0]->x,
977     vesicle->tlist->tria[i]->vertex[0]->y,
978     vesicle->tlist->tria[i]->vertex[0]->z,
979
980     vesicle->tlist->tria[i]->vertex[1]->x,
981     vesicle->tlist->tria[i]->vertex[1]->y,
982     vesicle->tlist->tria[i]->vertex[1]->z,
983
984     vesicle->tlist->tria[i]->vertex[2]->x,
985     vesicle->tlist->tria[i]->vertex[2]->y,
986     vesicle->tlist->tria[i]->vertex[2]->z
987     );
988     }
989         
990     fclose(fh);
991     return TS_SUCCESS;
992 }
993
994
1ab449 995 ts_tape *parsetape(char *filename){
SP 996     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
997     tape->multiprocessing=calloc(255,sizeof(char));
40aa5b 998
d7639a 999     cfg_opt_t opts[] = {
1ab449 1000         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 1001         CFG_SIMPLE_INT("npoly", &tape->npoly),
1002         CFG_SIMPLE_INT("nmono", &tape->nmono),
58230a 1003     CFG_SIMPLE_INT("nfil",&tape->nfil),
M 1004     CFG_SIMPLE_INT("nfono",&tape->nfono),
1005     CFG_SIMPLE_INT("R_nucleus",&tape->R_nucleus),
1006     CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
ea1cce 1007     CFG_SIMPLE_FLOAT("dmin_interspecies", &tape->dmin_interspecies),
1ab449 1008         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
SP 1009     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
9166cb 1010     CFG_SIMPLE_INT("constvolswitch",&tape->constvolswitch),
c0ae90 1011     CFG_SIMPLE_INT("constareaswitch",&tape->constareaswitch),
43c042 1012     CFG_SIMPLE_FLOAT("constvolprecision",&tape->constvolprecision),
1ab449 1013     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
SP 1014     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
b30f45 1015     CFG_SIMPLE_FLOAT("xi",&tape->xi),
1ab449 1016         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
SP 1017         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
1018         CFG_SIMPLE_INT("nymax", &tape->ncymax),
1019         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
1020         CFG_SIMPLE_INT("iterations",&tape->iterations),
1021     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
1022     CFG_SIMPLE_INT("inititer", &tape->inititer),
1023         CFG_SIMPLE_BOOL("quiet",&tape->quiet),
1024         CFG_SIMPLE_STR("multiprocessing",tape->multiprocessing),
1025         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
1026         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
1027         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
dc77e8 1028     CFG_SIMPLE_INT("spherical_harmonics_coefficients",&tape->shc),
d7639a 1029         CFG_END()
SP 1030     };
1031     cfg_t *cfg;    
1032     ts_uint retval;
1033     cfg = cfg_init(opts, 0);
1ab449 1034     retval=cfg_parse(cfg, filename);
d7639a 1035     if(retval==CFG_FILE_ERROR){
a6b1b5 1036     fatal("No tape file.",100);
d7639a 1037     }
SP 1038     else if(retval==CFG_PARSE_ERROR){
1039     fatal("Invalid tape!",100);
1040     }
a2db52 1041
7b0c07 1042     /* here we override all values read from tape with values from commandline*/
SP 1043     getcmdline_tape(cfg,command_line_args.tape_opts);
d7639a 1044     cfg_free(cfg);
40aa5b 1045
SP 1046
1ab449 1047     /* global variables are set automatically */
SP 1048     quiet=tape->quiet;
1049     return tape;
1050 }
d7639a 1051
1ab449 1052 ts_bool tape_free(ts_tape *tape){
SP 1053     free(tape->multiprocessing);
1054     free(tape);
1055     return TS_SUCCESS;
d7639a 1056 }
f74313 1057
SP 1058
7b0c07 1059
SP 1060 ts_bool getcmdline_tape(cfg_t *cfg, char *opts){
1061
1062     char *commands, *backup, *saveptr, *saveopptr, *command, *operator[2];
1063     ts_uint i,j;
1064     commands=(char *)malloc(10000*sizeof(char));
1065     backup=commands; //since the pointer to commands will be lost, we acquire a pointer that will serve as backup.
1066     strcpy(commands,opts);
1067     for(i=0; ;i++, commands=NULL){
1068         //breaks comma separated list of commands into specific commands.
1069         command=strtok_r(commands,",",&saveptr);    
1070         if(command==NULL) break;
1071 //        fprintf(stdout,"Command %d: %s\n",i,command);    
1072         //extracts name of command and value of command into operator[2] array.
1073         for(j=0; j<2;j++,command=NULL){
1074             operator[j]=strtok_r(command,"=",&saveopptr);
1075             if(operator[j]==NULL) break;
1076 //            fprintf(stdout," ---> Operator %d: %s\n",j,operator[j]);        
1077         }
1078         //1. check: must have 2 operators.
1079         if(j!=2) fatal("Error. Command line tape options are not formatted properly",1);
1080
1081     //    cfg_setstr(cfg,operator[0],operator[1]);
1082         cmdline_to_tape(cfg,operator[0],operator[1]);
1083         //2. check: must be named properly.
1084         //3. check: must be of right format (integer, double, string, ...)
1085         
1086     }
1087     free(backup);
1088     return TS_SUCCESS;
1089 }
1090
1091
1092 ts_bool cmdline_to_tape(cfg_t *cfg, char *key, char *val){
1093
1094     cfg_opt_t *cfg_opt=cfg_getopt(cfg,key);
1095     if(cfg_opt==NULL) fatal("Commandline tape option not recognised",1); //return TS_FAIL; 
1096     switch (cfg_opt->type){
1097         case CFGT_INT:
1098             cfg_setint(cfg,key,atol(val));
1099             break;
1100         case CFGT_FLOAT:
1101             cfg_setfloat(cfg,key,atof(val));
1102             break;
1103 /*        case CFGT_BOOL:
1104             cfg_setbool(cfg,operator[0],operator[1]);
1105             break; */
1106         case CFGT_STR:
1107             cfg_setstr(cfg,key,val);
1108             break;
1109         default:
1110             break;
1111
1112     }
1113     return TS_SUCCESS;
1114 }
1115
1116
1117
f74313 1118 ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
SP 1119     FILE *fh;
1120     ts_uint i, nvtx,nedges,ntria;
1121     ts_uint vtxi1,vtxi2;
1122     float x,y,z;
1123     ts_vertex_list *vlist;
1124     fh=fopen(fname, "r");
1125         if(fh==NULL){
1126                 err("Cannot open file for reading... Nonexistant file?");
1127                 return TS_FAIL;
1128         }
1129     ts_uint retval;
1130     retval=fscanf(fh,"%u %u %u",&nvtx, &nedges, &ntria);
1131     vesicle->vlist=init_vertex_list(nvtx);
1132     vlist=vesicle->vlist;
1133     for(i=0;i<nvtx;i++){
8f6a69 1134    //     fscanf(fh,"%F %F %F",&vlist->vtx[i]->x,&vlist->vtx[i]->y,&vlist->vtx[i]->z);
f74313 1135        retval=fscanf(fh,"%F %F %F",&x,&y,&z);
8f6a69 1136         vlist->vtx[i]->x=x;
SP 1137         vlist->vtx[i]->y=y;
1138         vlist->vtx[i]->z=z;
f74313 1139     }
SP 1140     for(i=0;i<nedges;i++){
1141         retval=fscanf(fh,"%u %u",&vtxi1,&vtxi2);
1142         bond_add(vesicle->blist,vesicle->vlist->vtx[vtxi1-1],vesicle->vlist->vtx[vtxi2-1]);
1143     }
1144     //TODO: neighbours from bonds,
1145     //TODO: triangles from neigbours
1146
1147 //    Don't need to read triangles. Already have enough data
1148     /*
1149     for(i=0;i<ntria;i++){
1150         retval=fscanf(fh,"%u %u %u", &bi1, &bi2, &bi3);
8f6a69 1151         vtxi1=vesicle->blist->vertex1->idx;
SP 1152         vtxi2=vesicle->blist->vertex1->idx;
f74313 1153         
SP 1154     }
1155     */
41a035 1156     if(retval);
f74313 1157     fclose(fh);    
SP 1158
1159
1160
1161     return TS_SUCCESS;
1162 }