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