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