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