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