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