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