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