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