Trisurf Monte Carlo simulator
Samo Penic
2016-05-31 4891eb093f61d37056c50c572e669349dd49a65a
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'},
fd8126 454        {"version", no_argument, 0, 'v'},
083e03 455            {"output-file",  required_argument, 0, 'o'},
SP 456            {"directory",  required_argument, 0, 'd'},
3f5c83 457            {"dump-filename", required_argument,0, 'f'},
7b0c07 458            {"tape-options",required_argument,0,'c'},
1bf3c3 459            {"tape-template", required_argument,0,0},
ab798b 460             {"restore-from-vtk",required_argument,0,0},
083e03 461            {0, 0, 0, 0}
SP 462          };
463        /* getopt_long stores the option index here. */
464        int option_index = 0;
465
fd8126 466        c = getopt_long (argc, argv, "d:f:o:t:c:v",
083e03 467                         long_options, &option_index);
SP 468
469        /* Detect the end of the options. */
470        if (c == -1)
471          break;
472
473        switch (c)
474          {
475          case 0:
476            /* If this option set a flag, do nothing else now. */
477            if (long_options[option_index].flag != 0)
478              break;
1bf3c3 479 /*           printf ("option %s", long_options[option_index].name);
083e03 480            if (optarg)
1bf3c3 481              printf (" with arg %s", optarg); 
SP 482            printf ("\n"); */
483             //TODO: find a better way.
484             if(strcmp(long_options[option_index].name,"tape-template")==0){
485                 strcpy(command_line_args.tape_templatefull,optarg);
486             }
ab798b 487             if(strcmp(long_options[option_index].name,"restore-from-vtk")==0){
ee84bd 488                 strcpy(command_line_args.dump_from_vtk,optarg);
SP 489             }
083e03 490            break;
fd8126 491      case 'v':
SP 492         fprintf(stdout,"TRISURF-NG v. %s, compiled on: %s %s.\n", TS_VERSION, __DATE__, __TIME__);
493             fprintf(stdout,"Programming done by: Samo Penic and Miha Fosnaric\n");
494             fprintf(stdout,"Released under terms of GPLv3\n");
495         exit(0);
083e03 496
7b0c07 497          case 'c':
SP 498               strcpy(command_line_args.tape_opts,optarg);
499             break;
500          case 't': //tape
3f5c83 501                 strcpy(command_line_args.tape_fullfilename,optarg);
083e03 502            break;
SP 503
7b0c07 504          case 'o':  //set filename of master pvd output file
3f5c83 505             strcpy(command_line_args.output_fullfilename, optarg);
SP 506             break;
083e03 507
SP 508          case 'd':
509             //check if directory exists. If not create one. If creation is
510             //successful, set directory for output files.
511             //printf ("option -d with value `%s'\n", optarg);
3f5c83 512             if (stat(optarg, &sb) == -1) {
SP 513                 //directory does not exist
083e03 514                 retval=mkdir(optarg, 0700);
SP 515                 if(retval){
3f5c83 516                     fatal("Could not create requested directory. Check if you have permissions",1);
083e03 517                 }
SP 518             }
3f5c83 519             //check if is a proper directory
SP 520             else if((sb.st_mode & S_IFMT) != S_IFDIR) {
521                 //it is not a directory. fatal error.
522                 ts_fprintf(stderr,"%s is not a directory!\n",optarg);
523                 fatal("Cannot continue",1);
083e03 524             }
3f5c83 525             strcpy(command_line_args.path, optarg);
083e03 526            break;
SP 527
528         case 'f':
3f5c83 529             strcpy(command_line_args.dump_fullfilename, optarg);
083e03 530             break;
SP 531
532          case '?':
533            /* getopt_long already printed an error message. */
ba73ab 534             print_help(stdout);
SP 535 //ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
083e03 536             fatal("Ooops, read help first",1);
SP 537            break;
538
539          default:
540            exit (1);
541          }
542      }
543
7b0c07 544 //Here we set correct values for full filenames!
SP 545     char *buffer=(char *)malloc(10000*sizeof(char));
546     //correct the path and add trailing /
547     if(command_line_args.path[strlen(command_line_args.path)-1]!='/') strcat(command_line_args.path,"/");
548    
549 /* master pvd output file */ 
550     strcpy(buffer,command_line_args.path);
551     strcat(buffer,command_line_args.output_fullfilename);
552     if ((file = fopen(buffer, "w")) == NULL) {
553                 fprintf(stderr,"Could not create output file %s!\n", buffer);
554                 fatal("Please specify correct output file or check permissions of the file",1);
1bf3c3 555                 //there is a tape template. make a copy into desired directory
7b0c07 556                 
SP 557             } else {
558                 fclose(file);
559                 strcpy(command_line_args.output_fullfilename,buffer);
560             }
561
562 /* tape file */
563     strcpy(buffer,command_line_args.path);
564     strcat(buffer,command_line_args.tape_fullfilename);
565     if (stat(buffer, &sb) == -1) {
1bf3c3 566
SP 567                 //tape does not exist. does tape template exist?
568                 if(stat(command_line_args.tape_templatefull, &sb)==-1){ 
569                     ts_fprintf(stderr,"Tape '%s' does not exist and no tape template was specified (or does not exist)!\n",buffer);
570                     fatal("Please select correct tape or check permissions of the file",1);
571                 } else {
572                     //tape template found
573                     fatal("Samo did not program template copy yet",1); 
574                 }
7b0c07 575             } else {
SP 576                 strcpy(command_line_args.tape_fullfilename,buffer);
577             }
578
579
580 /* dump file */
581             strcpy(buffer,command_line_args.path);
582             strcat(buffer,command_line_args.dump_fullfilename);
583             //check if dump file exist first.
584             if (stat(buffer, &sb) == -1) {
585                 //no dump file. check if we can create one.
586                 if ((file = fopen(buffer, "w")) == NULL) {
587                     fprintf(stderr,"Could not create dump file '%s'!\n",buffer);
588                     fatal("Please specify correct dump file or check permissions of the file",1);
589                 } else {
590                     fclose(file);
591                     //good, file is writeable. delete it for now.
592                     remove(buffer);
593                 }
594             }
595             strcpy(command_line_args.dump_fullfilename, buffer);
596
597
598     free(buffer);
083e03 599     return TS_SUCCESS;
SP 600
601 }
602
603
ba73ab 604 ts_bool print_help(FILE *fd){
SP 605     fprintf(fd,"TRISURF-NG v. %s, compiled on: %s %s.\n", TS_VERSION, __DATE__, __TIME__);
606     fprintf(fd,"Programming done by: Samo Penic and Miha Fosnaric\n");
607     fprintf(fd,"Released under terms of GPLv3\n\n");
608
609     fprintf(fd, "Invoking trisurf-ng without any flags results in default operation. Program reads 'tape' file and 'dump.bin' binary representation of the vesicle from disk and continues the simulation where it was aborted (as specified in 'dump.bin').\n\n");
610     fprintf(fd,"If 'tape' has different values than binary dump, those are used (if possible -- some values in tape, such as nshell cannot be modified).\n\n\n");
611     fprintf(fd,"However, if dump.bin is not present, user is notified to specify --force-from-tape flag. The vesicle will be created from specifications in tape.\n\n\n");
612     fprintf(fd,"Flags:\n\n");
613     fprintf(fd,"--force-from-tape\t\t makes initial shape of the vesicle from tape. Ignores already existing binary dump and possible simulation results.\n");
614     fprintf(fd,"--restore-from-vtk\t\t VTK's file ending with '.vtu' are preferred way to make state snapshots for restoration. With this flag the restoration of the vesicle from vtk is possible. The simulation will continue if hidden '.status' file with last iteration done is present. Otherwise it will start simulation from timestep 0.\n");
c4178c 615     fprintf(fd,"--reset-iteration-count\t\t starts simulation from the beginning (using binary dump).\n");
ba73ab 616     fprintf(fd,"--tape (or -t)\t\t specifies tape filename. For --force-from-tape and restoring from binary dump. Defaults to 'tape'.\n");
SP 617     fprintf(fd,"--version (or -v)\t\t Prints version information.\n");
618     fprintf(fd,"--output-file (or -o)\t\t Specifies filename of .PVD file. Defaults to 'output.pvd'\n");
c4178c 619     fprintf(fd,"--dump-filename (or -f)\t\t specifies filename for binary dump&restore. Defaults to 'dump.bin'\n\n\n");
SP 620     fprintf(fd,"Examples:\n\n");
621     fprintf(fd,"trisurf --force-from-tape\n");
622     fprintf(fd,"trisurf --reset-iteration-count\n");
623     fprintf(fd,"trisurf --restore-from-vtk filename.vtu\n");
624     fprintf(fd,"\n\n");
625
ba73ab 626     return TS_SUCCESS;
SP 627 }
628
629
083e03 630
d7639a 631 ts_bool print_vertex_list(ts_vertex_list *vlist){
SP 632     ts_uint i;
633     printf("Number of vertices: %u\n",vlist->n);
634     for(i=0;i<vlist->n;i++){
a6b1b5 635         printf("%u: %f %f %f\n",
8f6a69 636 vlist->vtx[i]->idx,vlist->vtx[i]->x, vlist->vtx[i]->y, vlist->vtx[i]->z);
d7639a 637     }
SP 638     return TS_SUCCESS;
639 }
640
641 ts_bool print_vertex_neighbours(ts_vertex_list *vlist){
642     ts_uint i,j;
a6b1b5 643     ts_vertex **vtx=vlist->vtx;
d7639a 644     printf("Vertex id(neigh no): (neighvertex coord) (neighvertex coord) ...\n");
SP 645     for(i=0;i<vlist->n;i++){
8f6a69 646         printf("%u(%u): ",vtx[i]->idx,vtx[i]->neigh_no);
SP 647         for(j=0;j<vtx[i]->neigh_no;j++){
648             printf("(%f,%f,%f)",vtx[i]->neigh[j]->x,
649 vtx[i]->neigh[j]->y,vtx[i]->neigh[j]->z);
d7639a 650         }
SP 651         printf("\n");
652     }
653
654 return TS_SUCCESS;
655 }
656
657 ts_bool write_vertex_fcompat_file(ts_vertex_list *vlist,ts_char *filename){
a6b1b5 658     ts_vertex **vtx=vlist->vtx;
d7639a 659     ts_uint i;
SP 660     FILE *fh;
661     
662     fh=fopen(filename, "w");
663     if(fh==NULL){
664         err("Cannot open file %s for writing");
665         return TS_FAIL;
666     }
667     for(i=0;i<vlist->n;i++)
8f6a69 668         fprintf(fh," %E\t%E\t%E\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 669
SP 670     fclose(fh);
671 return TS_SUCCESS;
672 }
673
674
675 ts_bool fprint_vertex_list(FILE *fh,ts_vertex_list *vlist){
676     ts_uint i,j;
677     for(i=0;i<vlist->n;i++){
8f6a69 678         fprintf(fh," %.17E\t%.17E\t%.17E\t%u\n",vlist->vtx[i]->x,
SP 679             vlist->vtx[i]->y, vlist->vtx[i]->z,
680             vlist->vtx[i]->neigh_no);
681         for(j=0;j<vlist->vtx[i]->neigh_no;j++){
682             fprintf(fh,"\t%u",(ts_uint)(vlist->vtx[i]->neigh[j]->idx));
a6b1b5 683         //-vlist->vtx+1));
d7639a 684         }
SP 685         fprintf(fh,"\n");
686     }
687     return TS_SUCCESS;
688 }
689
690 ts_bool fprint_tristar(FILE *fh, ts_vesicle *vesicle){
691     ts_uint i,j;
a6b1b5 692     for(i=0;i<vesicle->vlist->n;i++){
8f6a69 693         fprintf(fh,"\t%u",vesicle->vlist->vtx[i]->tristar_no);
SP 694         for(j=0;j<vesicle->vlist->vtx[i]->tristar_no;j++){
695             fprintf(fh,"\t%u",(ts_uint)(vesicle->vlist->vtx[i]->tristar[j]->idx));//-vesicle->tlist->tria+1));
d7639a 696         }
SP 697         fprintf(fh,"\n");
698     }
699     return TS_SUCCESS;
700 }
701
702 ts_bool fprint_triangle_list(FILE *fh, ts_vesicle *vesicle){
a6b1b5 703         ts_triangle_list *tlist=vesicle->tlist;
d7639a 704       ts_uint i,j;
SP 705     for(i=0;i<tlist->n;i++){
41a035 706         fprintf(fh,"\t%u",tlist->tria[i]->neigh_no);
SP 707         for(j=0;j<tlist->tria[i]->neigh_no;j++){
708             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->neigh[j]->idx));//-tlist->tria+1)); 
d7639a 709         }
SP 710         fprintf(fh,"\n");
711             for(j=0;j<3;j++){
41a035 712             fprintf(fh,"\t%u",(ts_uint)(tlist->tria[i]->vertex[j]->idx));//-vesicle->vlist->vtx+1)); 
d7639a 713             }
SP 714         fprintf(fh,"\n");
41a035 715         fprintf(fh,"%.17E\t%.17E\t%.17E\n",tlist->tria[i]->xnorm,
SP 716 tlist->tria[i]->ynorm,tlist->tria[i]->znorm);
d7639a 717         fprintf(fh,"0.00000000000000000\n0.00000000000000000\n");
SP 718     }
719     return TS_SUCCESS;
720 }
721
722 ts_bool fprint_vertex_data(FILE *fh,ts_vertex_list *vlist){
723     ts_uint i,j;
724     for(i=0;i<vlist->n;i++){
725         fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
8f6a69 726         vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
SP 727         vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
cef6ca 728         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 729             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
d7639a 730         }
SP 731             fprintf(fh,"\n");
cef6ca 732         for(j=0;j<vlist->vtx[i]->bond_no;j++){
8f6a69 733             fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
d7639a 734         }
SP 735             fprintf(fh,"\n");
736     }
737     return TS_SUCCESS;
738 }
739
740 ts_bool fprint_bonds(FILE *fh,ts_vesicle *vesicle){
741     ts_uint i;
a6b1b5 742     for(i=0;i<vesicle->blist->n;i++){
e016c4 743         fprintf(fh,"\t%u\t%u\n",(ts_uint)(vesicle->blist->bond[i]->vtx1->idx),
a6b1b5 744 //-vesicle->vlist->vtx+1),
e016c4 745         (ts_uint)(vesicle->blist->bond[i]->vtx2->idx));
a6b1b5 746     //-vesicle->vlist.vtx+1));
d7639a 747     }
SP 748     return TS_SUCCESS;
749 }
750
751
752 ts_bool write_dout_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
753     FILE *fh;
754     fh=fopen(filename, "w");
755     if(fh==NULL){
756         err("Cannot open file %s for writing");
757         return TS_FAIL;
758     }
759     fprintf(fh,"%.17E\n%.17E\n",vesicle->stepsize,vesicle->dmax);
a6b1b5 760     fprint_vertex_list(fh,vesicle->vlist);
d7639a 761     fprint_tristar(fh,vesicle);
SP 762     fprint_triangle_list(fh,vesicle);
a6b1b5 763     fprint_vertex_data(fh,vesicle->vlist);
d7639a 764     fprint_bonds(fh,vesicle);
SP 765     fclose(fh);    
766     return TS_SUCCESS;
767 }
768
769 ts_bool read_tape_fcompat_file(ts_vesicle *vesicle, ts_char *filename){
770     FILE *fh;
771     char line[255];
772     fh=fopen(filename, "r");
773         if(fh==NULL){
774                 err("Cannot open file for reading... Nonexistant file?");
775                 return TS_FAIL;
776         }
a6b1b5 777     ts_uint retval=1;
d7639a 778     while(retval!=EOF){
SP 779         retval=fscanf(fh,"%s",line);
780         
781         fprintf(stderr,"%s",line);
782     }    
783     fclose(fh);    
784     return TS_SUCCESS;
785 }
786
787 ts_bool write_master_xml_file(ts_char *filename){
788      FILE *fh;
789     ts_char *i,*j;
790     ts_uint tstep;
791     ts_char *number;
792         fh=fopen(filename, "w");
793         if(fh==NULL){
794                 err("Cannot open file %s for writing");
795                 return TS_FAIL;
796         }
797
798     fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
267db5 799     DIR *dir = opendir(command_line_args.path);
d7639a 800     if(dir){
SP 801         struct dirent *ent;
802         tstep=0;
803         while((ent = readdir(dir)) != NULL)
804         {
805             i=rindex(ent->d_name,'.');
806             if(i==NULL) continue;
807             if(strcmp(i+1,"vtu")==0){
808                     j=rindex(ent->d_name,'_');
809                     if(j==NULL) continue;
810                     number=strndup(j+1,j-i); 
a6b1b5 811                 fprintf(fh,"<DataSet timestep=\"%u\" group=\"\" part=\"0\" file=\"%s\"/>\n",atoi(number),ent->d_name);
d7639a 812                 tstep++;
SP 813                     free(number);
814             }  
815         }
816     }
f74313 817     free(dir);
d7639a 818     fprintf(fh,"</Collection>\n</VTKFile>\n");
SP 819     fclose(fh);
820     return TS_SUCCESS;
821 }
822
823 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno){
a6b1b5 824     ts_vertex_list *vlist=vesicle->vlist;
SP 825     ts_bond_list *blist=vesicle->blist;
826     ts_vertex **vtx=vlist->vtx;
40aa5b 827     ts_uint i,j;
267db5 828         char filename[10000];
SP 829         char just_name[255];
d7639a 830     FILE *fh;
267db5 831         strcpy(filename,command_line_args.path);
SP 832         sprintf(just_name,"timestep_%.6u.vtu",timestepno);
833         strcat(filename,just_name);
d7639a 834
SP 835     fh=fopen(filename, "w");
836     if(fh==NULL){
837         err("Cannot open file %s for writing");
838         return TS_FAIL;
839     }
840     /* Here comes header of the file */
40aa5b 841
SP 842     //find number of extra vtxs and bonds of polymeres
58230a 843     ts_uint monono=0, polyno=0, poly_idx=0, filno=0, fonono=0;
M 844     ts_bool poly=0, fil=0;
40aa5b 845     if(vesicle->poly_list!=NULL){
SP 846         if(vesicle->poly_list->poly[0]!=NULL){
847         polyno=vesicle->poly_list->n;
848         monono=vesicle->poly_list->poly[0]->vlist->n;
849         poly=1;
850         }
851     }
58230a 852
M 853     if(vesicle->filament_list!=NULL){
854         if(vesicle->filament_list->poly[0]!=NULL){
855         filno=vesicle->filament_list->n;
856         fonono=vesicle->filament_list->poly[0]->vlist->n;
857         fil=1;
858         }
859     }
860
854cb6 861     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n");
SP 862     xml_trisurf_data(fh,vesicle);
863     fprintf(fh, " <UnstructuredGrid>\n");
58230a 864     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno+fonono*filno, blist->n+monono*polyno+filno*(fonono-1));
d7639a 865     fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
SP 866        for(i=0;i<vlist->n;i++){
a6b1b5 867         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 868     }
40aa5b 869     //polymeres
SP 870     if(poly){
3c1ac1 871         poly_idx=vlist->n;
40aa5b 872         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 873             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
58230a 874                 fprintf(fh,"%u ", poly_idx);
M 875             }
876         }
877     }
878     //filaments
879     if(fil){
880         poly_idx=vlist->n+monono*polyno;
881         for(i=0;i<vesicle->filament_list->n;i++){
882             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
883     //    fprintf(stderr,"was here\n");
3c1ac1 884                 fprintf(fh,"%u ", poly_idx);
40aa5b 885             }
SP 886         }
887     }
d7639a 888
SP 889     fprintf(fh,"</DataArray>\n</PointData>\n<CellData>\n</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
890     for(i=0;i<vlist->n;i++){
aad500 891         fprintf(fh,"%.17e %.17e %.17e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 892     }
SP 893     //polymeres
894     if(poly){
895         for(i=0;i<vesicle->poly_list->n;i++){
896             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
aad500 897                 fprintf(fh,"%.17e %.17e %.17e\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 898             }
M 899         }
900     }
901     //filaments
902     if(fil){
903         for(i=0;i<vesicle->filament_list->n;i++){
904             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
aad500 905                 fprintf(fh,"%.17e %.17e %.17e\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 906             }
SP 907         }
d7639a 908     }
SP 909
910     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
911     for(i=0;i<blist->n;i++){
e016c4 912             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 913     }
40aa5b 914     //polymeres
SP 915     if(poly){
3c1ac1 916         poly_idx=vlist->n;
40aa5b 917         for(i=0;i<vesicle->poly_list->n;i++){
SP 918             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 919 //                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 920                 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 921             }
SP 922     //grafted bonds
3c1ac1 923         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 924         }
SP 925
926     }
927     
58230a 928     //filaments
M 929     if(fil){
930         poly_idx=vlist->n+monono*polyno;
931         for(i=0;i<vesicle->filament_list->n;i++){
932             for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
933                 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);
934 //        fprintf(stderr,"was here\n");
935             
936             }
937         }
938
939     }
40aa5b 940
d7639a 941     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
58230a 942     for (i=2;i<(blist->n+monono*polyno+(fonono-1)*filno)*2+1;i+=2){
d7639a 943     fprintf(fh,"%u ",i);
SP 944     }
945     fprintf(fh,"\n");
946     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
58230a 947      for (i=0;i<blist->n+monono*polyno+fonono*filno;i++){
d7639a 948         fprintf(fh,"3 ");
SP 949     }
950
951     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
952     fclose(fh);
953     return TS_SUCCESS;
954
955 }
956
957 ts_bool write_vertex_vtk_file(ts_vesicle *vesicle,ts_char *filename, ts_char *text){
a6b1b5 958     ts_vertex_list *vlist=vesicle->vlist;
SP 959     ts_bond_list *blist=vesicle->blist;
960     ts_vertex **vtx=vlist->vtx;
961     ts_uint i;
d7639a 962     FILE *fh;
SP 963     
964     fh=fopen(filename, "w");
965     if(fh==NULL){
966         err("Cannot open file %s for writing");
967         return TS_FAIL;
968     }
969     /* Here comes header of the file */
970 //    fprintf(stderr,"NSHELL=%u\n",nshell);
971     fprintf(fh, "# vtk DataFile Version 2.0\n");
972     /* TODO: Do a sanity check on text. Max 255 char, must not me \n terminated */ 
973     fprintf(fh, "%s\n", text);
974     fprintf(fh,"ASCII\n");
975     fprintf(fh,"DATASET UNSTRUCTURED_GRID\n");
976     fprintf(fh,"POINTS %u double\n", vlist->n);
977     for(i=0;i<vlist->n;i++){
8f6a69 978         fprintf(fh,"%e %e %e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
d7639a 979     }
SP 980     
981     fprintf(fh,"CELLS %u %u\n",blist->n,3*blist->n);
982     for(i=0;i<blist->n;i++){
e016c4 983             fprintf(fh,"2 %u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 984     }
SP 985     fprintf(fh,"CELL_TYPES %u\n",blist->n);
986     for(i=0;i<blist->n;i++)
987         fprintf(fh,"3\n");
988
989     fprintf(fh,"POINT_DATA %u\n", vlist->n);
990     fprintf(fh,"SCALARS scalars long 1\n");
991     fprintf(fh,"LOOKUP_TABLE default\n");
992
993     for(i=0;i<vlist->n;i++)
8f6a69 994         fprintf(fh,"%u\n",vtx[i]->idx);
d7639a 995
SP 996     fclose(fh);
997     return TS_SUCCESS;
998 }
999
1000
1001
144784 1002 ts_bool write_pov_file(ts_vesicle *vesicle, char *filename){
SP 1003     FILE *fh;
1004     ts_uint i;
1005     
1006     fh=fopen(filename, "w");
1007     if(fh==NULL){
1008         err("Cannot open file %s for writing");
1009         return TS_FAIL;
1010     }
1011
1012     for(i=0;i<vesicle->tlist->n;i++){
1013     
1014     fprintf(fh,"\ttriangle {");
1015     fprintf(fh,"\t<%e,%e,%e> <%e,%e,%e> <%e,%e,%e> }\n", 
1016     vesicle->tlist->tria[i]->vertex[0]->x,
1017     vesicle->tlist->tria[i]->vertex[0]->y,
1018     vesicle->tlist->tria[i]->vertex[0]->z,
1019
1020     vesicle->tlist->tria[i]->vertex[1]->x,
1021     vesicle->tlist->tria[i]->vertex[1]->y,
1022     vesicle->tlist->tria[i]->vertex[1]->z,
1023
1024     vesicle->tlist->tria[i]->vertex[2]->x,
1025     vesicle->tlist->tria[i]->vertex[2]->y,
1026     vesicle->tlist->tria[i]->vertex[2]->z
1027     );
1028     }
1029         
1030     fclose(fh);
1031     return TS_SUCCESS;
1032 }
1033
1034
1ab449 1035 ts_tape *parsetape(char *filename){
698ae1 1036     FILE *fd = fopen (filename, "r");
SP 1037     long length;
1038     size_t size;
1039     fseek (fd, 0, SEEK_END);
1040       length = ftell (fd);
1041     fseek (fd, 0, SEEK_SET);
1042     size=fread (tapetxt, 1, length, fd);
1043     fclose(fd);
1044     if(size);
1045     ts_tape *tape=parsetapebuffer(tapetxt);
1046     return tape;
1047 }
1048
1049 ts_tape *parsetapebuffer(char *buffer){
1ab449 1050     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
SP 1051     tape->multiprocessing=calloc(255,sizeof(char));
40aa5b 1052
d7639a 1053     cfg_opt_t opts[] = {
1ab449 1054         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 1055         CFG_SIMPLE_INT("npoly", &tape->npoly),
1056         CFG_SIMPLE_INT("nmono", &tape->nmono),
58230a 1057     CFG_SIMPLE_INT("nfil",&tape->nfil),
M 1058     CFG_SIMPLE_INT("nfono",&tape->nfono),
1059     CFG_SIMPLE_INT("R_nucleus",&tape->R_nucleus),
37791b 1060     CFG_SIMPLE_FLOAT("R_nucleusX",&tape->R_nucleusX),
SP 1061     CFG_SIMPLE_FLOAT("R_nucleusY",&tape->R_nucleusY),
1062     CFG_SIMPLE_FLOAT("R_nucleusZ",&tape->R_nucleusZ),
58230a 1063     CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
ea1cce 1064     CFG_SIMPLE_FLOAT("dmin_interspecies", &tape->dmin_interspecies),
1ab449 1065         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
SP 1066     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
9166cb 1067     CFG_SIMPLE_INT("constvolswitch",&tape->constvolswitch),
c0ae90 1068     CFG_SIMPLE_INT("constareaswitch",&tape->constareaswitch),
43c042 1069     CFG_SIMPLE_FLOAT("constvolprecision",&tape->constvolprecision),
1ab449 1070     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
SP 1071     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
b30f45 1072     CFG_SIMPLE_FLOAT("xi",&tape->xi),
1ab449 1073         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
SP 1074         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
1075         CFG_SIMPLE_INT("nymax", &tape->ncymax),
1076         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
1077         CFG_SIMPLE_INT("iterations",&tape->iterations),
1078     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
1079     CFG_SIMPLE_INT("inititer", &tape->inititer),
1080         CFG_SIMPLE_BOOL("quiet",&tape->quiet),
1081         CFG_SIMPLE_STR("multiprocessing",tape->multiprocessing),
1082         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
1083         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
1084         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
dc77e8 1085     CFG_SIMPLE_INT("spherical_harmonics_coefficients",&tape->shc),
d7639a 1086         CFG_END()
SP 1087     };
1088     cfg_t *cfg;    
1089     ts_uint retval;
1090     cfg = cfg_init(opts, 0);
698ae1 1091     retval=cfg_parse_buf(cfg, buffer);
d7639a 1092     if(retval==CFG_FILE_ERROR){
a6b1b5 1093     fatal("No tape file.",100);
d7639a 1094     }
SP 1095     else if(retval==CFG_PARSE_ERROR){
1096     fatal("Invalid tape!",100);
1097     }
a2db52 1098
7b0c07 1099     /* here we override all values read from tape with values from commandline*/
SP 1100     getcmdline_tape(cfg,command_line_args.tape_opts);
d7639a 1101     cfg_free(cfg);
40aa5b 1102
SP 1103
1ab449 1104     /* global variables are set automatically */
SP 1105     quiet=tape->quiet;
1106     return tape;
1107 }
d7639a 1108
1ab449 1109 ts_bool tape_free(ts_tape *tape){
SP 1110     free(tape->multiprocessing);
1111     free(tape);
1112     return TS_SUCCESS;
d7639a 1113 }
f74313 1114
SP 1115
7b0c07 1116
SP 1117 ts_bool getcmdline_tape(cfg_t *cfg, char *opts){
1118
1119     char *commands, *backup, *saveptr, *saveopptr, *command, *operator[2];
1120     ts_uint i,j;
1121     commands=(char *)malloc(10000*sizeof(char));
1122     backup=commands; //since the pointer to commands will be lost, we acquire a pointer that will serve as backup.
1123     strcpy(commands,opts);
1124     for(i=0; ;i++, commands=NULL){
1125         //breaks comma separated list of commands into specific commands.
1126         command=strtok_r(commands,",",&saveptr);    
1127         if(command==NULL) break;
1128 //        fprintf(stdout,"Command %d: %s\n",i,command);    
1129         //extracts name of command and value of command into operator[2] array.
1130         for(j=0; j<2;j++,command=NULL){
1131             operator[j]=strtok_r(command,"=",&saveopptr);
1132             if(operator[j]==NULL) break;
1133 //            fprintf(stdout," ---> Operator %d: %s\n",j,operator[j]);        
1134         }
1135         //1. check: must have 2 operators.
1136         if(j!=2) fatal("Error. Command line tape options are not formatted properly",1);
1137
1138     //    cfg_setstr(cfg,operator[0],operator[1]);
1139         cmdline_to_tape(cfg,operator[0],operator[1]);
1140         //2. check: must be named properly.
1141         //3. check: must be of right format (integer, double, string, ...)
1142         
1143     }
1144     free(backup);
1145     return TS_SUCCESS;
1146 }
1147
1148
1149 ts_bool cmdline_to_tape(cfg_t *cfg, char *key, char *val){
1150
1151     cfg_opt_t *cfg_opt=cfg_getopt(cfg,key);
1152     if(cfg_opt==NULL) fatal("Commandline tape option not recognised",1); //return TS_FAIL; 
1153     switch (cfg_opt->type){
1154         case CFGT_INT:
1155             cfg_setint(cfg,key,atol(val));
1156             break;
1157         case CFGT_FLOAT:
1158             cfg_setfloat(cfg,key,atof(val));
1159             break;
1160 /*        case CFGT_BOOL:
1161             cfg_setbool(cfg,operator[0],operator[1]);
1162             break; */
1163         case CFGT_STR:
1164             cfg_setstr(cfg,key,val);
1165             break;
1166         default:
1167             break;
1168
1169     }
1170     return TS_SUCCESS;
1171 }
1172
1173
1174
f74313 1175 ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
SP 1176     FILE *fh;
1177     ts_uint i, nvtx,nedges,ntria;
1178     ts_uint vtxi1,vtxi2;
1179     float x,y,z;
1180     ts_vertex_list *vlist;
1181     fh=fopen(fname, "r");
1182         if(fh==NULL){
1183                 err("Cannot open file for reading... Nonexistant file?");
1184                 return TS_FAIL;
1185         }
1186     ts_uint retval;
1187     retval=fscanf(fh,"%u %u %u",&nvtx, &nedges, &ntria);
1188     vesicle->vlist=init_vertex_list(nvtx);
1189     vlist=vesicle->vlist;
1190     for(i=0;i<nvtx;i++){
8f6a69 1191    //     fscanf(fh,"%F %F %F",&vlist->vtx[i]->x,&vlist->vtx[i]->y,&vlist->vtx[i]->z);
f74313 1192        retval=fscanf(fh,"%F %F %F",&x,&y,&z);
8f6a69 1193         vlist->vtx[i]->x=x;
SP 1194         vlist->vtx[i]->y=y;
1195         vlist->vtx[i]->z=z;
f74313 1196     }
SP 1197     for(i=0;i<nedges;i++){
1198         retval=fscanf(fh,"%u %u",&vtxi1,&vtxi2);
1199         bond_add(vesicle->blist,vesicle->vlist->vtx[vtxi1-1],vesicle->vlist->vtx[vtxi2-1]);
1200     }
1201     //TODO: neighbours from bonds,
1202     //TODO: triangles from neigbours
1203
1204 //    Don't need to read triangles. Already have enough data
1205     /*
1206     for(i=0;i<ntria;i++){
1207         retval=fscanf(fh,"%u %u %u", &bi1, &bi2, &bi3);
8f6a69 1208         vtxi1=vesicle->blist->vertex1->idx;
SP 1209         vtxi2=vesicle->blist->vertex1->idx;
f74313 1210         
SP 1211     }
1212     */
41a035 1213     if(retval);
f74313 1214     fclose(fh);    
SP 1215
1216
1217
1218     return TS_SUCCESS;
1219 }