Trisurf Monte Carlo simulator
Samo Penic
2014-07-08 47a7aced121a6a5348b5e718f36146c6ac76f6ed
commit | author | age
f74313 1 #include "general.h"
d7639a 2 #include<stdio.h>
SP 3 #include "io.h"
f74313 4 #include <confuse.h>
SP 5 #include "vertex.h"
6 #include "bond.h"
d7639a 7 #include<string.h>
a6b1b5 8 #include<stdlib.h>
d7639a 9 #include <sys/types.h>
SP 10 #include <dirent.h>
b14a8d 11 #include "initial_distribution.h"
a2db52 12 #include "poly.h"
d7639a 13
083e03 14 #include <getopt.h>
SP 15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <errno.h>
e9eab4 19 /** DUMP STATE TO DISK DRIVE **/
SP 20
1ab449 21 ts_bool dump_state(ts_vesicle *vesicle, ts_uint iteration){
e9eab4 22
SP 23     /* save current state with wrong pointers. Will fix that later */
24     ts_uint i,j,k;
25     FILE *fh=fopen("dump.bin","wb");
26
27     /* dump vesicle */
86f5e7 28     fwrite(vesicle, sizeof(ts_vesicle),1,fh);
e9eab4 29     /* dump vertex list */
SP 30     fwrite(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
31     /* dump bond list */
32     fwrite(vesicle->blist, sizeof(ts_bond_list),1,fh);
33     /* dump triangle list */
34     fwrite(vesicle->tlist, sizeof(ts_triangle_list),1,fh);
35     /* dump cell list */
36     fwrite(vesicle->clist, sizeof(ts_cell_list),1,fh);
37     /* dump poly list */
38     fwrite(vesicle->poly_list, sizeof(ts_poly_list),1,fh);
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 // 
SP 424     vesicle->clist=(ts_cell_list *)malloc(sizeof(ts_cell_list));
47a7ac 425     retval=fread(vesicle->clist, sizeof(ts_cell_list), 1,fh);
SP 426     vesicle->clist->ncmax[0]=vesicle->tape->ncxmax; 
427     vesicle->clist->ncmax[1]=vesicle->tape->ncymax; 
428     vesicle->clist->ncmax[2]=vesicle->tape->nczmax; 
063289 429     vesicle->clist->cell=(ts_cell **)malloc(sizeof(ts_cell *)*vesicle->clist->ncmax[0]*vesicle->clist->ncmax[1]*vesicle->clist->ncmax[2]);
SP 430     for(i=0;i<vesicle->clist->ncmax[0]*vesicle->clist->ncmax[1]*vesicle->clist->ncmax[2];i++){
431             vesicle->clist->cell[i]=(ts_cell *)calloc(1,sizeof(ts_cell));
432             vesicle->clist->cell[i]->idx=i+1; // We enumerate cells! Probably never required!
433         }
9166cb 434 //recreating stored tape information//
47a7ac 435 //    vesicle->tape=(ts_tape *)malloc(sizeof(ts_tape));
SP 436 //    retval=fread(vesicle->tape, sizeof(ts_tape),1,fh);
1ab449 437     retval=fread(iteration,sizeof(ts_uint),1,fh);
e9eab4 438     if(retval); 
SP 439     fclose(fh);
440     return vesicle;
441 }
442
443
444
083e03 445 ts_bool parse_args(int argc, char **argv){
3f5c83 446     int c, retval;
SP 447     struct stat sb;
448     sprintf(command_line_args.path, "./"); //clear string;
449     sprintf(command_line_args.output_fullfilename,"./output.pvd");
450     sprintf(command_line_args.dump_fullfilename,"./dump.bin");
451     sprintf(command_line_args.tape_fullfilename,"./tape");
452             FILE *file;
453     
083e03 454 while (1)
SP 455      {
456        static struct option long_options[] =
457          {
8a6614 458            {"force-from-tape", no_argument,       &(command_line_args.force_from_tape), 1},
SP 459        {"reset-iteration-count", no_argument, &(command_line_args.reset_iteration_count), 1},
083e03 460            {"tape",     no_argument,       0, 't'},
SP 461            {"output-file",  required_argument, 0, 'o'},
462            {"directory",  required_argument, 0, 'd'},
3f5c83 463            {"dump-filename", required_argument,0, 'f'},
083e03 464            {0, 0, 0, 0}
SP 465          };
466        /* getopt_long stores the option index here. */
467        int option_index = 0;
468
3f5c83 469        c = getopt_long (argc, argv, "d:f:o:t:",
083e03 470                         long_options, &option_index);
SP 471
472        /* Detect the end of the options. */
473        if (c == -1)
474          break;
475
476        switch (c)
477          {
478          case 0:
479            /* If this option set a flag, do nothing else now. */
480            if (long_options[option_index].flag != 0)
481              break;
482            printf ("option %s", long_options[option_index].name);
483            if (optarg)
484              printf (" with arg %s", optarg);
485            printf ("\n");
486            break;
487
488          case 't':
489             //check if tape exists. If not, fail immediately.
3f5c83 490             if (stat(optarg, &sb) == -1) {
SP 491                 ts_fprintf(stderr,"Tape '%s' does not exist!\n",optarg);
492                 fatal("Please select correct tape",1);
493             } else {
494                 strcpy(command_line_args.tape_fullfilename,optarg);
495             }
083e03 496            break;
SP 497
498          case 'o':
499             //set filename of master output file
3f5c83 500             if ((file = fopen(optarg, "w")) == NULL) {
SP 501                 fprintf(stderr,"Could not create output file!\n");
502                 fatal("Please specify correct output file",1);
503                 
504             } else {
505                 fclose(file);
506             }
507             strcpy(command_line_args.output_fullfilename, optarg);
508             break;
083e03 509
SP 510          case 'd':
511             //check if directory exists. If not create one. If creation is
512             //successful, set directory for output files.
513             //printf ("option -d with value `%s'\n", optarg);
3f5c83 514             if (stat(optarg, &sb) == -1) {
SP 515                 //directory does not exist
083e03 516                 retval=mkdir(optarg, 0700);
SP 517                 if(retval){
3f5c83 518                     fatal("Could not create requested directory. Check if you have permissions",1);
083e03 519                 }
SP 520             }
3f5c83 521             //check if is a proper directory
SP 522             else if((sb.st_mode & S_IFMT) != S_IFDIR) {
523                 //it is not a directory. fatal error.
524                 ts_fprintf(stderr,"%s is not a directory!\n",optarg);
525                 fatal("Cannot continue",1);
083e03 526             }
3f5c83 527             strcpy(command_line_args.path, optarg);
083e03 528            break;
SP 529
530         case 'f':
531             //check if dump file specified exists. Defaults to dump.bin
3f5c83 532             if ((file = fopen(optarg, "w")) == NULL) {
SP 533                 fprintf(stderr,"Could not create dump file!\n");
534                 fatal("Please specify correct dump file",1);
535                 
536             } else {
537                 fclose(file);
538             }
539             strcpy(command_line_args.dump_fullfilename, optarg);
083e03 540             break;
SP 541
542          case '?':
543            /* getopt_long already printed an error message. */
544
545             ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
546             fatal("Ooops, read help first",1);
547            break;
548
549          default:
550            exit (1);
551          }
552      }
553
554     return TS_SUCCESS;
555
556 }
557
558
559
560
d7639a 561 ts_bool print_vertex_list(ts_vertex_list *vlist){
SP 562     ts_uint i;
563     printf("Number of vertices: %u\n",vlist->n);
564     for(i=0;i<vlist->n;i++){
a6b1b5 565         printf("%u: %f %f %f\n",
8f6a69 566 vlist->vtx[i]->idx,vlist->vtx[i]->x, vlist->vtx[i]->y, vlist->vtx[i]->z);
d7639a 567     }
SP 568     return TS_SUCCESS;
569 }
570
571 ts_bool print_vertex_neighbours(ts_vertex_list *vlist){
572     ts_uint i,j;
a6b1b5 573     ts_vertex **vtx=vlist->vtx;
d7639a 574     printf("Vertex id(neigh no): (neighvertex coord) (neighvertex coord) ...\n");
SP 575     for(i=0;i<vlist->n;i++){
8f6a69 576         printf("%u(%u): ",vtx[i]->idx,vtx[i]->neigh_no);
SP 577         for(j=0;j<vtx[i]->neigh_no;j++){
578             printf("(%f,%f,%f)",vtx[i]->neigh[j]->x,
579 vtx[i]->neigh[j]->y,vtx[i]->neigh[j]->z);
d7639a 580         }
SP 581         printf("\n");
582     }
583
584 return TS_SUCCESS;
585 }
586
587 ts_bool write_vertex_fcompat_file(ts_vertex_list *vlist,ts_char *filename){
a6b1b5 588     ts_vertex **vtx=vlist->vtx;
d7639a 589     ts_uint i;
SP 590     FILE *fh;
591     
592     fh=fopen(filename, "w");
593     if(fh==NULL){
594         err("Cannot open file %s for writing");
595         return TS_FAIL;
596     }
597     for(i=0;i<vlist->n;i++)
8f6a69 598         fprintf(fh," %E\t%E\t%E\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 599
SP 600     fclose(fh);
601 return TS_SUCCESS;
602 }
603
604
605 ts_bool fprint_vertex_list(FILE *fh,ts_vertex_list *vlist){
606     ts_uint i,j;
607     for(i=0;i<vlist->n;i++){
8f6a69 608         fprintf(fh," %.17E\t%.17E\t%.17E\t%u\n",vlist->vtx[i]->x,
SP 609             vlist->vtx[i]->y, vlist->vtx[i]->z,
610             vlist->vtx[i]->neigh_no);
611         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
612             fprintf(fh,"\t%u",(ts_uint)(vlist->vtx[i]->neigh[j]->idx));
a6b1b5 613         //-vlist->vtx+1));
d7639a 614         }
SP 615         fprintf(fh,"\n");
616     }
617     return TS_SUCCESS;
618 }
619
620 ts_bool fprint_tristar(FILE *fh, ts_vesicle *vesicle){
621     ts_uint i,j;
a6b1b5 622     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 623         fprintf(fh,"\t%u",vesicle->vlist->vtx[i]->tristar_no);
SP 624         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
625             fprintf(fh,"\t%u",(ts_uint)(vesicle->vlist->vtx[i]->tristar[j]->idx));//-vesicle->tlist->tria+1));
d7639a 626         }
SP 627         fprintf(fh,"\n");
628     }
629     return TS_SUCCESS;
630 }
631
632 ts_bool fprint_triangle_list(FILE *fh, ts_vesicle *vesicle){
a6b1b5 633         ts_triangle_list *tlist=vesicle->tlist;
d7639a 634       ts_uint i,j;
SP 635     for(i=0;i<tlist->n;i++){
41a035 636         fprintf(fh,"\t%u",tlist->tria[i]->neigh_no);
SP 637         for(j=0;j<tlist->tria[i]->neigh_no;j++){
638             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->neigh[j]->idx));//-tlist->tria+1)); 
d7639a 639         }
SP 640         fprintf(fh,"\n");
641             for(j=0;j<3;j++){
41a035 642             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->vertex[j]->idx));//-vesicle->vlist->vtx+1)); 
d7639a 643             }
SP 644         fprintf(fh,"\n");
41a035 645         fprintf(fh,"%.17E\t%.17E\t%.17E\n",tlist->tria[i]->xnorm,
SP 646 tlist->tria[i]->ynorm,tlist->tria[i]->znorm);
d7639a 647         fprintf(fh,"0.00000000000000000\n0.00000000000000000\n");
SP 648     }
649     return TS_SUCCESS;
650 }
651
652 ts_bool fprint_vertex_data(FILE *fh,ts_vertex_list *vlist){
653     ts_uint i,j;
654     for(i=0;i<vlist->n;i++){
655         fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
8f6a69 656         vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
SP 657         vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
cef6ca 658         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 659             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
d7639a 660         }
SP 661             fprintf(fh,"\n");
cef6ca 662         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 663             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
d7639a 664         }
SP 665             fprintf(fh,"\n");
666     }
667     return TS_SUCCESS;
668 }
669
670 ts_bool fprint_bonds(FILE *fh,ts_vesicle *vesicle){
671     ts_uint i;
a6b1b5 672     for(i=0;i<vesicle->blist->n;i++){
e016c4 673         fprintf(fh,"\t%u\t%u\n",(ts_uint)(vesicle->blist->bond[i]->vtx1->idx),
a6b1b5 674 //-vesicle->vlist->vtx+1),
e016c4 675         (ts_uint)(vesicle->blist->bond[i]->vtx2->idx));
a6b1b5 676     //-vesicle->vlist.vtx+1));
d7639a 677     }
SP 678     return TS_SUCCESS;
679 }
680
681
682 ts_bool write_dout_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
683     FILE *fh;
684     fh=fopen(filename, "w");
685     if(fh==NULL){
686         err("Cannot open file %s for writing");
687         return TS_FAIL;
688     }
689     fprintf(fh,"%.17E\n%.17E\n",vesicle->stepsize,vesicle->dmax);
a6b1b5 690     fprint_vertex_list(fh,vesicle->vlist);
d7639a 691     fprint_tristar(fh,vesicle);
SP 692     fprint_triangle_list(fh,vesicle);
a6b1b5 693     fprint_vertex_data(fh,vesicle->vlist);
d7639a 694     fprint_bonds(fh,vesicle);
SP 695     fclose(fh);    
696     return TS_SUCCESS;
697 }
698
699 ts_bool read_tape_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
700     FILE *fh;
701     char line[255];
702     fh=fopen(filename, "r");
703         if(fh==NULL){
704                 err("Cannot open file for reading... Nonexistant file?");
705                 return TS_FAIL;
706         }
a6b1b5 707     ts_uint retval=1;
d7639a 708     while(retval!=EOF){
SP 709         retval=fscanf(fh,"%s",line);
710         
711         fprintf(stderr,"%s",line);
712     }    
713     fclose(fh);    
714     return TS_SUCCESS;
715 }
716
717 ts_bool write_master_xml_file(ts_char *filename){
718      FILE *fh;
719     ts_char *i,*j;
720     ts_uint tstep;
721     ts_char *number;
722         fh=fopen(filename, "w");
723         if(fh==NULL){
724                 err("Cannot open file %s for writing");
725                 return TS_FAIL;
726         }
727
728     fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
729     DIR *dir = opendir(".");
730     if(dir){
731         struct dirent *ent;
732         tstep=0;
733         while((ent = readdir(dir)) != NULL)
734         {
735             i=rindex(ent->d_name,'.');
736             if(i==NULL) continue;
737             if(strcmp(i+1,"vtu")==0){
738                     j=rindex(ent->d_name,'_');
739                     if(j==NULL) continue;
740                     number=strndup(j+1,j-i); 
a6b1b5 741                 fprintf(fh,"<DataSet timestep=\"%u\" group=\"\" part=\"0\" file=\"%s\"/>\n",atoi(number),ent->d_name);
d7639a 742                 tstep++;
SP 743                     free(number);
744             }  
745         }
746     }
f74313 747     free(dir);
d7639a 748     fprintf(fh,"</Collection>\n</VTKFile>\n");
SP 749     fclose(fh);
750     return TS_SUCCESS;
751 }
752
753 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno){
a6b1b5 754     ts_vertex_list *vlist=vesicle->vlist;
SP 755     ts_bond_list *blist=vesicle->blist;
756     ts_vertex **vtx=vlist->vtx;
40aa5b 757     ts_uint i,j;
d7639a 758         char filename[255];
SP 759     FILE *fh;
760
761         sprintf(filename,"timestep_%.6u.vtu",timestepno);
762     fh=fopen(filename, "w");
763     if(fh==NULL){
764         err("Cannot open file %s for writing");
765         return TS_FAIL;
766     }
767     /* Here comes header of the file */
40aa5b 768
SP 769     //find number of extra vtxs and bonds of polymeres
58230a 770     ts_uint monono=0, polyno=0, poly_idx=0, filno=0, fonono=0;
M 771     ts_bool poly=0, fil=0;
40aa5b 772     if(vesicle->poly_list!=NULL){
SP 773         if(vesicle->poly_list->poly[0]!=NULL){
774         polyno=vesicle->poly_list->n;
775         monono=vesicle->poly_list->poly[0]->vlist->n;
776         poly=1;
777         }
778     }
58230a 779
M 780     if(vesicle->filament_list!=NULL){
781         if(vesicle->filament_list->poly[0]!=NULL){
782         filno=vesicle->filament_list->n;
783         fonono=vesicle->filament_list->poly[0]->vlist->n;
784         fil=1;
785         }
786     }
787
d7639a 788     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n <UnstructuredGrid>\n");
58230a 789     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno+fonono*filno, blist->n+monono*polyno+filno*(fonono-1));
d7639a 790     fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
SP 791        for(i=0;i<vlist->n;i++){
a6b1b5 792         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 793     }
40aa5b 794     //polymeres
SP 795     if(poly){
3c1ac1 796         poly_idx=vlist->n;
40aa5b 797         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 798             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
58230a 799                 fprintf(fh,"%u ", poly_idx);
M 800             }
801         }
802     }
803     //filaments
804     if(fil){
805         poly_idx=vlist->n+monono*polyno;
806         for(i=0;i<vesicle->filament_list->n;i++){
807             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
808     //    fprintf(stderr,"was here\n");
3c1ac1 809                 fprintf(fh,"%u ", poly_idx);
40aa5b 810             }
SP 811         }
812     }
d7639a 813
SP 814     fprintf(fh,"</DataArray>\n</PointData>\n<CellData>\n</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
815     for(i=0;i<vlist->n;i++){
8f6a69 816         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 817     }
SP 818     //polymeres
819     if(poly){
820         for(i=0;i<vesicle->poly_list->n;i++){
821             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
822                 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 823             }
M 824         }
825     }
826     //filaments
827     if(fil){
828         for(i=0;i<vesicle->filament_list->n;i++){
829             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
830                 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 831             }
SP 832         }
d7639a 833     }
SP 834
835     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
836     for(i=0;i<blist->n;i++){
e016c4 837             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 838     }
40aa5b 839     //polymeres
SP 840     if(poly){
3c1ac1 841         poly_idx=vlist->n;
40aa5b 842         for(i=0;i<vesicle->poly_list->n;i++){
SP 843             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 844 //                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 845                 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 846             }
SP 847     //grafted bonds
3c1ac1 848         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 849         }
SP 850
851     }
852     
58230a 853     //filaments
M 854     if(fil){
855         poly_idx=vlist->n+monono*polyno;
856         for(i=0;i<vesicle->filament_list->n;i++){
857             for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
858                 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);
859 //        fprintf(stderr,"was here\n");
860             
861             }
862         }
863
864     }
40aa5b 865
d7639a 866     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
58230a 867     for (i=2;i<(blist->n+monono*polyno+(fonono-1)*filno)*2+1;i+=2){
d7639a 868     fprintf(fh,"%u ",i);
SP 869     }
870     fprintf(fh,"\n");
871     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
58230a 872      for (i=0;i<blist->n+monono*polyno+fonono*filno;i++){
d7639a 873         fprintf(fh,"3 ");
SP 874     }
875
876     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
877     fclose(fh);
878     return TS_SUCCESS;
879
880 }
881
882 ts_bool write_vertex_vtk_file(ts_vesicle *vesicle,ts_char *filename, ts_char *text){
a6b1b5 883     ts_vertex_list *vlist=vesicle->vlist;
SP 884     ts_bond_list *blist=vesicle->blist;
885     ts_vertex **vtx=vlist->vtx;
886     ts_uint i;
d7639a 887     FILE *fh;
SP 888     
889     fh=fopen(filename, "w");
890     if(fh==NULL){
891         err("Cannot open file %s for writing");
892         return TS_FAIL;
893     }
894     /* Here comes header of the file */
895 //    fprintf(stderr,"NSHELL=%u\n",nshell);
896     fprintf(fh, "# vtk DataFile Version 2.0\n");
897     /* TODO: Do a sanity check on text. Max 255 char, must not me \n terminated */ 
898     fprintf(fh, "%s\n", text);
899     fprintf(fh,"ASCII\n");
900     fprintf(fh,"DATASET UNSTRUCTURED_GRID\n");
901     fprintf(fh,"POINTS %u double\n", vlist->n);
902     for(i=0;i<vlist->n;i++){
8f6a69 903         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 904     }
SP 905     
906     fprintf(fh,"CELLS %u %u\n",blist->n,3*blist->n);
907     for(i=0;i<blist->n;i++){
e016c4 908             fprintf(fh,"2 %u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 909     }
SP 910     fprintf(fh,"CELL_TYPES %u\n",blist->n);
911     for(i=0;i<blist->n;i++)
912         fprintf(fh,"3\n");
913
914     fprintf(fh,"POINT_DATA %u\n", vlist->n);
915     fprintf(fh,"SCALARS scalars long 1\n");
916     fprintf(fh,"LOOKUP_TABLE default\n");
917
918     for(i=0;i<vlist->n;i++)
8f6a69 919         fprintf(fh,"%u\n",vtx[i]->idx);
d7639a 920
SP 921     fclose(fh);
922     return TS_SUCCESS;
923 }
924
925
926
144784 927 ts_bool write_pov_file(ts_vesicle *vesicle, char *filename){
SP 928     FILE *fh;
929     ts_uint i;
930     
931     fh=fopen(filename, "w");
932     if(fh==NULL){
933         err("Cannot open file %s for writing");
934         return TS_FAIL;
935     }
936
937     for(i=0;i<vesicle->tlist->n;i++){
938     
939     fprintf(fh,"\ttriangle {");
940     fprintf(fh,"\t<%e,%e,%e> <%e,%e,%e> <%e,%e,%e> }\n", 
941     vesicle->tlist->tria[i]->vertex[0]->x,
942     vesicle->tlist->tria[i]->vertex[0]->y,
943     vesicle->tlist->tria[i]->vertex[0]->z,
944
945     vesicle->tlist->tria[i]->vertex[1]->x,
946     vesicle->tlist->tria[i]->vertex[1]->y,
947     vesicle->tlist->tria[i]->vertex[1]->z,
948
949     vesicle->tlist->tria[i]->vertex[2]->x,
950     vesicle->tlist->tria[i]->vertex[2]->y,
951     vesicle->tlist->tria[i]->vertex[2]->z
952     );
953     }
954         
955     fclose(fh);
956     return TS_SUCCESS;
957 }
958
959
1ab449 960 ts_tape *parsetape(char *filename){
SP 961   //  long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20, pswitch=0;  // THIS IS DUE TO CONFUSE BUG!
962     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
963     tape->multiprocessing=calloc(255,sizeof(char));
964   /*  long int brezveze0=1;
b14a8d 965     long int brezveze1=1;
SP 966     long int brezveze2=1;
e86357 967     ts_double xk0=25.0, dmax=1.67,stepsize=0.15,kspring=800.0,pressure=0.0;
d7a113 968     long int iter=1000, init=1000, mcsw=1000;
1ab449 969 */    
40aa5b 970
d7639a 971     cfg_opt_t opts[] = {
1ab449 972         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 973         CFG_SIMPLE_INT("npoly", &tape->npoly),
974         CFG_SIMPLE_INT("nmono", &tape->nmono),
58230a 975     CFG_SIMPLE_INT("nfil",&tape->nfil),
M 976     CFG_SIMPLE_INT("nfono",&tape->nfono),
977     CFG_SIMPLE_INT("R_nucleus",&tape->R_nucleus),
978     CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
ea1cce 979     CFG_SIMPLE_FLOAT("dmin_interspecies", &tape->dmin_interspecies),
1ab449 980         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
SP 981     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
9166cb 982     CFG_SIMPLE_INT("constvolswitch",&tape->constvolswitch),
43c042 983     CFG_SIMPLE_FLOAT("constvolprecision",&tape->constvolprecision),
1ab449 984     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
SP 985     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
b30f45 986     CFG_SIMPLE_FLOAT("xi",&tape->xi),
1ab449 987         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
SP 988         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
989         CFG_SIMPLE_INT("nymax", &tape->ncymax),
990         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
991         CFG_SIMPLE_INT("iterations",&tape->iterations),
992     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
993     CFG_SIMPLE_INT("inititer", &tape->inititer),
994         CFG_SIMPLE_BOOL("quiet",&tape->quiet),
995         CFG_SIMPLE_STR("multiprocessing",tape->multiprocessing),
996         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
997         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
998         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
dc77e8 999     CFG_SIMPLE_INT("spherical_harmonics_coefficients",&tape->shc),
d7639a 1000         CFG_END()
SP 1001     };
1002     cfg_t *cfg;    
1003     ts_uint retval;
1004     cfg = cfg_init(opts, 0);
1ab449 1005     retval=cfg_parse(cfg, filename);
d7639a 1006     if(retval==CFG_FILE_ERROR){
a6b1b5 1007     fatal("No tape file.",100);
d7639a 1008     }
SP 1009     else if(retval==CFG_PARSE_ERROR){
1010     fatal("Invalid tape!",100);
1011     }
a2db52 1012
d7639a 1013     cfg_free(cfg);
40aa5b 1014
SP 1015
1ab449 1016     /* global variables are set automatically */
SP 1017     quiet=tape->quiet;
1018     return tape;
1019 }
d7639a 1020
1ab449 1021 ts_bool tape_free(ts_tape *tape){
SP 1022     free(tape->multiprocessing);
1023     free(tape);
1024     return TS_SUCCESS;
d7639a 1025 }
f74313 1026
SP 1027
1028 ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
1029     FILE *fh;
1030     ts_uint i, nvtx,nedges,ntria;
1031     ts_uint vtxi1,vtxi2;
1032     float x,y,z;
1033     ts_vertex_list *vlist;
1034     fh=fopen(fname, "r");
1035         if(fh==NULL){
1036                 err("Cannot open file for reading... Nonexistant file?");
1037                 return TS_FAIL;
1038         }
1039     ts_uint retval;
1040     retval=fscanf(fh,"%u %u %u",&nvtx, &nedges, &ntria);
1041     vesicle->vlist=init_vertex_list(nvtx);
1042     vlist=vesicle->vlist;
1043     for(i=0;i<nvtx;i++){
8f6a69 1044    //     fscanf(fh,"%F %F %F",&vlist->vtx[i]->x,&vlist->vtx[i]->y,&vlist->vtx[i]->z);
f74313 1045        retval=fscanf(fh,"%F %F %F",&x,&y,&z);
8f6a69 1046         vlist->vtx[i]->x=x;
SP 1047         vlist->vtx[i]->y=y;
1048         vlist->vtx[i]->z=z;
f74313 1049     }
SP 1050     for(i=0;i<nedges;i++){
1051         retval=fscanf(fh,"%u %u",&vtxi1,&vtxi2);
1052         bond_add(vesicle->blist,vesicle->vlist->vtx[vtxi1-1],vesicle->vlist->vtx[vtxi2-1]);
1053     }
1054     //TODO: neighbours from bonds,
1055     //TODO: triangles from neigbours
1056
1057 //    Don't need to read triangles. Already have enough data
1058     /*
1059     for(i=0;i<ntria;i++){
1060         retval=fscanf(fh,"%u %u %u", &bi1, &bi2, &bi3);
8f6a69 1061         vtxi1=vesicle->blist->vertex1->idx;
SP 1062         vtxi2=vesicle->blist->vertex1->idx;
f74313 1063         
SP 1064     }
1065     */
41a035 1066     if(retval);
f74313 1067     fclose(fh);    
SP 1068
1069
1070
1071     return TS_SUCCESS;
1072 }