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