Trisurf Monte Carlo simulator
Samo Penic
2019-10-17 80ebbe388f3e945e62f47b0141c660ee6f05c8fc
commit | author | age
7f6076 1 /* vim: set ts=4 sts=4 sw=4 noet : */
f74313 2 #include "general.h"
d7639a 3 #include<stdio.h>
SP 4 #include "io.h"
f74313 5 #include "vertex.h"
SP 6 #include "bond.h"
d7639a 7 #include<string.h>
a6b1b5 8 #include<stdlib.h>
d7639a 9 #include <sys/types.h>
SP 10 #include <dirent.h>
b14a8d 11 #include "initial_distribution.h"
a2db52 12 #include "poly.h"
055dd7 13 #include "cell.h"
083e03 14 #include <getopt.h>
SP 15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <errno.h>
854cb6 19 #include <snapshot.h>
e9eab4 20
SP 21
083e03 22 ts_bool parse_args(int argc, char **argv){
3f5c83 23     int c, retval;
SP 24     struct stat sb;
25     sprintf(command_line_args.path, "./"); //clear string;
7b0c07 26     sprintf(command_line_args.output_fullfilename,"output.pvd");
SP 27     sprintf(command_line_args.dump_fullfilename,"dump.bin");
28     sprintf(command_line_args.tape_fullfilename,"tape");
1bf3c3 29     sprintf(command_line_args.tape_templatefull,"./tape");
3f5c83 30             FILE *file;
SP 31     
083e03 32 while (1)
SP 33      {
34        static struct option long_options[] =
35          {
8a6614 36            {"force-from-tape", no_argument,       &(command_line_args.force_from_tape), 1},
SP 37        {"reset-iteration-count", no_argument, &(command_line_args.reset_iteration_count), 1},
083e03 38            {"tape",     no_argument,       0, 't'},
fd8126 39        {"version", no_argument, 0, 'v'},
083e03 40            {"output-file",  required_argument, 0, 'o'},
SP 41            {"directory",  required_argument, 0, 'd'},
3f5c83 42            {"dump-filename", required_argument,0, 'f'},
7b0c07 43            {"tape-options",required_argument,0,'c'},
1bf3c3 44            {"tape-template", required_argument,0,0},
ab798b 45             {"restore-from-vtk",required_argument,0,0},
083e03 46            {0, 0, 0, 0}
SP 47          };
48        /* getopt_long stores the option index here. */
49        int option_index = 0;
50
fd8126 51        c = getopt_long (argc, argv, "d:f:o:t:c:v",
083e03 52                         long_options, &option_index);
SP 53
54        /* Detect the end of the options. */
55        if (c == -1)
56          break;
57
58        switch (c)
59          {
60          case 0:
61            /* If this option set a flag, do nothing else now. */
62            if (long_options[option_index].flag != 0)
63              break;
1bf3c3 64 /*           printf ("option %s", long_options[option_index].name);
083e03 65            if (optarg)
1bf3c3 66              printf (" with arg %s", optarg); 
SP 67            printf ("\n"); */
68             //TODO: find a better way.
69             if(strcmp(long_options[option_index].name,"tape-template")==0){
70                 strcpy(command_line_args.tape_templatefull,optarg);
71             }
ab798b 72             if(strcmp(long_options[option_index].name,"restore-from-vtk")==0){
ee84bd 73                 strcpy(command_line_args.dump_from_vtk,optarg);
SP 74             }
083e03 75            break;
fd8126 76      case 'v':
SP 77         fprintf(stdout,"TRISURF-NG v. %s, compiled on: %s %s.\n", TS_VERSION, __DATE__, __TIME__);
78             fprintf(stdout,"Programming done by: Samo Penic and Miha Fosnaric\n");
79             fprintf(stdout,"Released under terms of GPLv3\n");
80         exit(0);
083e03 81
7b0c07 82          case 'c':
SP 83               strcpy(command_line_args.tape_opts,optarg);
84             break;
85          case 't': //tape
3f5c83 86                 strcpy(command_line_args.tape_fullfilename,optarg);
083e03 87            break;
SP 88
7b0c07 89          case 'o':  //set filename of master pvd output file
3f5c83 90             strcpy(command_line_args.output_fullfilename, optarg);
SP 91             break;
083e03 92
SP 93          case 'd':
94             //check if directory exists. If not create one. If creation is
95             //successful, set directory for output files.
96             //printf ("option -d with value `%s'\n", optarg);
3f5c83 97             if (stat(optarg, &sb) == -1) {
SP 98                 //directory does not exist
083e03 99                 retval=mkdir(optarg, 0700);
SP 100                 if(retval){
3f5c83 101                     fatal("Could not create requested directory. Check if you have permissions",1);
083e03 102                 }
SP 103             }
3f5c83 104             //check if is a proper directory
SP 105             else if((sb.st_mode & S_IFMT) != S_IFDIR) {
106                 //it is not a directory. fatal error.
107                 ts_fprintf(stderr,"%s is not a directory!\n",optarg);
108                 fatal("Cannot continue",1);
083e03 109             }
3f5c83 110             strcpy(command_line_args.path, optarg);
083e03 111            break;
SP 112
113         case 'f':
3f5c83 114             strcpy(command_line_args.dump_fullfilename, optarg);
083e03 115             break;
SP 116
117          case '?':
118            /* getopt_long already printed an error message. */
ba73ab 119             print_help(stdout);
SP 120 //ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
083e03 121             fatal("Ooops, read help first",1);
SP 122            break;
123
124          default:
125            exit (1);
126          }
127      }
128
7b0c07 129 //Here we set correct values for full filenames!
SP 130     char *buffer=(char *)malloc(10000*sizeof(char));
131     //correct the path and add trailing /
132     if(command_line_args.path[strlen(command_line_args.path)-1]!='/') strcat(command_line_args.path,"/");
133    
134 /* master pvd output file */ 
135     strcpy(buffer,command_line_args.path);
136     strcat(buffer,command_line_args.output_fullfilename);
137     if ((file = fopen(buffer, "w")) == NULL) {
138                 fprintf(stderr,"Could not create output file %s!\n", buffer);
139                 fatal("Please specify correct output file or check permissions of the file",1);
1bf3c3 140                 //there is a tape template. make a copy into desired directory
7b0c07 141                 
SP 142             } else {
143                 fclose(file);
144                 strcpy(command_line_args.output_fullfilename,buffer);
145             }
146
147 /* tape file */
148     strcpy(buffer,command_line_args.path);
149     strcat(buffer,command_line_args.tape_fullfilename);
150     if (stat(buffer, &sb) == -1) {
1bf3c3 151
SP 152                 //tape does not exist. does tape template exist?
153                 if(stat(command_line_args.tape_templatefull, &sb)==-1){ 
154                     ts_fprintf(stderr,"Tape '%s' does not exist and no tape template was specified (or does not exist)!\n",buffer);
155                     fatal("Please select correct tape or check permissions of the file",1);
156                 } else {
157                     //tape template found
158                     fatal("Samo did not program template copy yet",1); 
159                 }
7b0c07 160             } else {
SP 161                 strcpy(command_line_args.tape_fullfilename,buffer);
162             }
163
164
165 /* dump file */
166             strcpy(buffer,command_line_args.path);
167             strcat(buffer,command_line_args.dump_fullfilename);
168             //check if dump file exist first.
169             if (stat(buffer, &sb) == -1) {
170                 //no dump file. check if we can create one.
171                 if ((file = fopen(buffer, "w")) == NULL) {
172                     fprintf(stderr,"Could not create dump file '%s'!\n",buffer);
173                     fatal("Please specify correct dump file or check permissions of the file",1);
174                 } else {
175                     fclose(file);
176                     //good, file is writeable. delete it for now.
177                     remove(buffer);
178                 }
179             }
180             strcpy(command_line_args.dump_fullfilename, buffer);
181
182
183     free(buffer);
083e03 184     return TS_SUCCESS;
SP 185
186 }
187
188
ba73ab 189 ts_bool print_help(FILE *fd){
SP 190     fprintf(fd,"TRISURF-NG v. %s, compiled on: %s %s.\n", TS_VERSION, __DATE__, __TIME__);
191     fprintf(fd,"Programming done by: Samo Penic and Miha Fosnaric\n");
192     fprintf(fd,"Released under terms of GPLv3\n\n");
193
194     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");
195     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");
196     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");
197     fprintf(fd,"Flags:\n\n");
198     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");
199     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 200     fprintf(fd,"--reset-iteration-count\t\t starts simulation from the beginning (using binary dump).\n");
ba73ab 201     fprintf(fd,"--tape (or -t)\t\t specifies tape filename. For --force-from-tape and restoring from binary dump. Defaults to 'tape'.\n");
SP 202     fprintf(fd,"--version (or -v)\t\t Prints version information.\n");
203     fprintf(fd,"--output-file (or -o)\t\t Specifies filename of .PVD file. Defaults to 'output.pvd'\n");
c4178c 204     fprintf(fd,"--dump-filename (or -f)\t\t specifies filename for binary dump&restore. Defaults to 'dump.bin'\n\n\n");
SP 205     fprintf(fd,"Examples:\n\n");
206     fprintf(fd,"trisurf --force-from-tape\n");
207     fprintf(fd,"trisurf --reset-iteration-count\n");
208     fprintf(fd,"trisurf --restore-from-vtk filename.vtu\n");
209     fprintf(fd,"\n\n");
210
d7639a 211     return TS_SUCCESS;
SP 212 }
213
0a2c81 214 ts_bool write_vertex_xml_file(ts_vesicle *vesicle, ts_uint timestepno, ts_cluster_list *cstlist){
a6b1b5 215     ts_vertex_list *vlist=vesicle->vlist;
SP 216     ts_bond_list *blist=vesicle->blist;
217     ts_vertex **vtx=vlist->vtx;
40aa5b 218     ts_uint i,j;
13d445 219     //ts_double senergy=0.0;
267db5 220         char filename[10000];
SP 221         char just_name[255];
d7639a 222     FILE *fh;
267db5 223         strcpy(filename,command_line_args.path);
SP 224         sprintf(just_name,"timestep_%.6u.vtu",timestepno);
225         strcat(filename,just_name);
d7639a 226
SP 227     fh=fopen(filename, "w");
228     if(fh==NULL){
229         err("Cannot open file %s for writing");
230         return TS_FAIL;
231     }
232     /* Here comes header of the file */
40aa5b 233
SP 234     //find number of extra vtxs and bonds of polymeres
58230a 235     ts_uint monono=0, polyno=0, poly_idx=0, filno=0, fonono=0;
M 236     ts_bool poly=0, fil=0;
40aa5b 237     if(vesicle->poly_list!=NULL){
SP 238         if(vesicle->poly_list->poly[0]!=NULL){
239         polyno=vesicle->poly_list->n;
240         monono=vesicle->poly_list->poly[0]->vlist->n;
241         poly=1;
242         }
243     }
58230a 244
M 245     if(vesicle->filament_list!=NULL){
246         if(vesicle->filament_list->poly[0]!=NULL){
247         filno=vesicle->filament_list->n;
248         fonono=vesicle->filament_list->poly[0]->vlist->n;
249         fil=1;
250         }
251     }
252
854cb6 253     fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n");
SP 254     xml_trisurf_data(fh,vesicle);
255     fprintf(fh, " <UnstructuredGrid>\n");
8db203 256     fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno+fonono*filno, blist->n+monono*polyno+filno*(fonono-1)+vesicle->tlist->n);
80ebbe 257     fprintf(fh,"<PointData Scalars=\"vertices_idx\">\n<DataArray type=\"Int64\" Name=\"vertices_idx\" format=\"binary\">");
SP 258     int *int_vector=(int *)malloc(vlist->n+monono*polyno+fonono*filno*sizeof(ts_uint));
259     int offset=0;
d7639a 260        for(i=0;i<vlist->n;i++){
80ebbe 261     //    fprintf(fh,"%u ",vtx[i]->idx);
SP 262         int_vector[i+offset]=vtx[i]->idx;
d7639a 263     }
40aa5b 264     //polymeres
SP 265     if(poly){
3c1ac1 266         poly_idx=vlist->n;
80ebbe 267         offset=offset+i;
40aa5b 268         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 269             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
80ebbe 270                 //fprintf(fh,"%u ", poly_idx);
SP 271                 int_vector[j+offset]=poly_idx;
58230a 272             }
80ebbe 273             offset=offset+j;
58230a 274         }
M 275     }
276     //filaments
277     if(fil){
278         poly_idx=vlist->n+monono*polyno;
279         for(i=0;i<vesicle->filament_list->n;i++){
280             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
281     //    fprintf(stderr,"was here\n");
80ebbe 282                 //fprintf(fh,"%u ", poly_idx);
SP 283                 int_vector[j+offset]=poly_idx;
40aa5b 284             }
80ebbe 285             offset=offset+j;
40aa5b 286         }
SP 287     }
80ebbe 288     char *printout=ts_compress_intlist(int_vector,vlist->n+monono*polyno+fonono*filno*sizeof(ts_uint));
SP 289     fprintf(fh,"%s",printout);
290     free(printout);
d7639a 291
45c708 292         fprintf(fh,"</DataArray>\n");
0a2c81 293     if(cstlist!=NULL){
SP 294         fprintf(fh,"<DataArray type=\"Int64\" Name=\"vertices_in_cluster\" format=\"ascii\">");
295         for(i=0;i<vlist->n;i++){
296             if(vtx[i]->cluster!=NULL){
297                 fprintf(fh,"%u ",vtx[i]->cluster->nvtx);
298             } else {
299                 fprintf(fh,"-1 ");
300             }
301             }
302         //polymeres
303         if(poly){
304             poly_idx=vlist->n;
305             for(i=0;i<vesicle->poly_list->n;i++){
306                 for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
307                     fprintf(fh,"-1 ");
308                 }
309             }
310         }
311         //filaments
312         if(fil){
313             poly_idx=vlist->n+monono*polyno;
314             for(i=0;i<vesicle->filament_list->n;i++){
315                 for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
316         //    fprintf(stderr,"was here\n");
317                     fprintf(fh,"-1 ");
318                 }
319             }
320         }
321
322         fprintf(fh,"</DataArray>\n");
323
324
325     }
326
45c708 327     //here comes additional data as needed. Currently only spontaneous curvature
SP 328     fprintf(fh,"<DataArray type=\"Float64\" Name=\"spontaneous_curvature\" format=\"ascii\">");
329     for(i=0;i<vlist->n;i++){
330         fprintf(fh,"%.17e ",vtx[i]->c);
331     }
332         //polymeres
333         if(poly){
334             poly_idx=vlist->n;
335             for(i=0;i<vesicle->poly_list->n;i++){
336                 for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
337                     fprintf(fh,"%.17e ", vesicle->poly_list->poly[i]->vlist->vtx[j]->c);
338                 }
339             }
340         }
341         //filaments
342         if(fil){
343             poly_idx=vlist->n+monono*polyno;
344             for(i=0;i<vesicle->filament_list->n;i++){
345                 for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
346         //    fprintf(stderr,"was here\n");
347                     fprintf(fh,"%.17e ",  vesicle->filament_list->poly[i]->vlist->vtx[j]->c);
348                 }
349             }
350         }
351     fprintf(fh,"</DataArray>\n");
352
5c64e2 353     //here comes additional data. Energy!
SP 354     fprintf(fh,"<DataArray type=\"Float64\" Name=\"bending_energy\" format=\"ascii\">");
355     for(i=0;i<vlist->n;i++){
356         fprintf(fh,"%.17e ",vtx[i]->energy*vtx[i]->xk);
357     }
358         //polymeres
359         if(poly){
360             poly_idx=vlist->n;
361             for(i=0;i<vesicle->poly_list->n;i++){
362                 for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
363                     fprintf(fh,"%.17e ", vesicle->poly_list->poly[i]->vlist->vtx[j]->energy* vesicle->poly_list->poly[i]->k);
364                 }
365             }
366         }
367         //filaments
368         if(fil){
369             poly_idx=vlist->n+monono*polyno;
370             for(i=0;i<vesicle->filament_list->n;i++){
371                 for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
372         //    fprintf(stderr,"was here\n");
373                     fprintf(fh,"%.17e ",  vesicle->filament_list->poly[i]->vlist->vtx[j]->energy*  vesicle->filament_list->poly[i]->k);
374                 }
375             }
376         }
377     fprintf(fh,"</DataArray>\n");
378
45c708 379     
13d445 380     fprintf(fh,"</PointData>\n<CellData>\n");
SP 381
7ec6fb 382     if(vesicle->tape->stretchswitch==1){
SP 383         fprintf(fh,"<DataArray type=\"Float64\" Name=\"stretching_energy\" format=\"ascii\">");
cf7aba 384         for(i=0;i<blist->n;i++){
SP 385             fprintf(fh, "0.0 ");
386         }
387         for(i=0;i<monono*polyno+filno*(fonono-1);i++){
13d445 388             fprintf(fh,"0.0 ");
7ec6fb 389         }
13d445 390         for(i=0;i<vesicle->tlist->n;i++){
SP 391             fprintf(fh,"%.17e ",vesicle->tlist->tria[i]->energy);
392         }
7ec6fb 393         fprintf(fh,"</DataArray>\n");
SP 394     }
45c708 395
13d445 396
SP 397
398     fprintf(fh,"</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
d7639a 399     for(i=0;i<vlist->n;i++){
aad500 400         fprintf(fh,"%.17e %.17e %.17e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 401     }
SP 402     //polymeres
403     if(poly){
404         for(i=0;i<vesicle->poly_list->n;i++){
405             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
aad500 406                 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 407             }
M 408         }
409     }
410     //filaments
411     if(fil){
412         for(i=0;i<vesicle->filament_list->n;i++){
413             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
aad500 414                 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 415             }
SP 416         }
d7639a 417     }
SP 418
419     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
420     for(i=0;i<blist->n;i++){
e016c4 421             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 422     }
40aa5b 423     //polymeres
SP 424     if(poly){
3c1ac1 425         poly_idx=vlist->n;
40aa5b 426         for(i=0;i<vesicle->poly_list->n;i++){
SP 427             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 428 //                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 429                 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 430             }
SP 431     //grafted bonds
3c1ac1 432         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 433         }
SP 434
435     }
436     
58230a 437     //filaments
M 438     if(fil){
439         poly_idx=vlist->n+monono*polyno;
440         for(i=0;i<vesicle->filament_list->n;i++){
441             for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
442                 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);
443 //        fprintf(stderr,"was here\n");
444             
445             }
446         }
447
448     }
8db203 449     for(i=0;i<vesicle->tlist->n;i++){
SP 450         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);
451     }
d7639a 452     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
58230a 453     for (i=2;i<(blist->n+monono*polyno+(fonono-1)*filno)*2+1;i+=2){
d7639a 454     fprintf(fh,"%u ",i);
SP 455     }
8db203 456     for(j=i+1;j<i+3*(vesicle->tlist->n);j+=3){ //let's continue counting from where we left of
SP 457         fprintf(fh,"%u ", j);
458     }
d7639a 459     fprintf(fh,"\n");
SP 460     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
8db203 461      for (i=0;i<blist->n+monono*polyno+(fonono-1)*filno;i++){
d7639a 462         fprintf(fh,"3 ");
SP 463     }
8db203 464     for(i=0;i<vesicle->tlist->n;i++){
SP 465         fprintf(fh,"5 ");
466     }
d7639a 467     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
SP 468     fclose(fh);
469     return TS_SUCCESS;
470
471 }
472
144784 473 ts_bool write_pov_file(ts_vesicle *vesicle, char *filename){
SP 474     FILE *fh;
475     ts_uint i;
476     
477     fh=fopen(filename, "w");
478     if(fh==NULL){
479         err("Cannot open file %s for writing");
480         return TS_FAIL;
481     }
482
483     for(i=0;i<vesicle->tlist->n;i++){
484     
485     fprintf(fh,"\ttriangle {");
486     fprintf(fh,"\t<%e,%e,%e> <%e,%e,%e> <%e,%e,%e> }\n", 
487     vesicle->tlist->tria[i]->vertex[0]->x,
488     vesicle->tlist->tria[i]->vertex[0]->y,
489     vesicle->tlist->tria[i]->vertex[0]->z,
490
491     vesicle->tlist->tria[i]->vertex[1]->x,
492     vesicle->tlist->tria[i]->vertex[1]->y,
493     vesicle->tlist->tria[i]->vertex[1]->z,
494
495     vesicle->tlist->tria[i]->vertex[2]->x,
496     vesicle->tlist->tria[i]->vertex[2]->y,
497     vesicle->tlist->tria[i]->vertex[2]->z
498     );
499     }
500         
501     fclose(fh);
502     return TS_SUCCESS;
503 }
504
505
1ab449 506 ts_tape *parsetape(char *filename){
698ae1 507     FILE *fd = fopen (filename, "r");
SP 508     long length;
509     size_t size;
510     fseek (fd, 0, SEEK_END);
511       length = ftell (fd);
512     fseek (fd, 0, SEEK_SET);
513     size=fread (tapetxt, 1, length, fd);
514     fclose(fd);
515     if(size);
516     ts_tape *tape=parsetapebuffer(tapetxt);
517     return tape;
518 }
519
520 ts_tape *parsetapebuffer(char *buffer){
1ab449 521     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
SP 522     tape->multiprocessing=calloc(255,sizeof(char));
40aa5b 523
d7639a 524     cfg_opt_t opts[] = {
1ab449 525         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 526         CFG_SIMPLE_INT("npoly", &tape->npoly),
527         CFG_SIMPLE_INT("nmono", &tape->nmono),
58230a 528     CFG_SIMPLE_INT("nfil",&tape->nfil),
M 529     CFG_SIMPLE_INT("nfono",&tape->nfono),
e98482 530     CFG_SIMPLE_INT("internal_poly",&tape->internal_poly),
58230a 531     CFG_SIMPLE_INT("R_nucleus",&tape->R_nucleus),
37791b 532     CFG_SIMPLE_FLOAT("R_nucleusX",&tape->R_nucleusX),
SP 533     CFG_SIMPLE_FLOAT("R_nucleusY",&tape->R_nucleusY),
534     CFG_SIMPLE_FLOAT("R_nucleusZ",&tape->R_nucleusZ),
58230a 535     CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
ea1cce 536     CFG_SIMPLE_FLOAT("dmin_interspecies", &tape->dmin_interspecies),
1ab449 537         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
SP 538     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
9166cb 539     CFG_SIMPLE_INT("constvolswitch",&tape->constvolswitch),
c0ae90 540     CFG_SIMPLE_INT("constareaswitch",&tape->constareaswitch),
43c042 541     CFG_SIMPLE_FLOAT("constvolprecision",&tape->constvolprecision),
2ae815 542     CFG_SIMPLE_INT("stretchswitch",&tape->stretchswitch),
SP 543     CFG_SIMPLE_FLOAT("xkA0",&tape->xkA0),    
1ab449 544     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
SP 545     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
b30f45 546     CFG_SIMPLE_FLOAT("xi",&tape->xi),
1ab449 547         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
SP 548         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
549         CFG_SIMPLE_INT("nymax", &tape->ncymax),
550         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
551         CFG_SIMPLE_INT("iterations",&tape->iterations),
552     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
553     CFG_SIMPLE_INT("inititer", &tape->inititer),
819a09 554                 CFG_SIMPLE_BOOL("quiet",(cfg_bool_t *)&tape->quiet),
S 555         CFG_SIMPLE_STR("multiprocessing",&tape->multiprocessing),
1ab449 556         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
SP 557         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
558         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
dc77e8 559     CFG_SIMPLE_INT("spherical_harmonics_coefficients",&tape->shc),
e5858f 560     CFG_SIMPLE_INT("number_of_vertices_with_c0", &tape->number_of_vertices_with_c0),
SP 561     CFG_SIMPLE_FLOAT("c0",&tape->c0),
562     CFG_SIMPLE_FLOAT("w",&tape->w),
250de4 563     CFG_SIMPLE_FLOAT("F",&tape->F),
6c274d 564 /* Variables related to plane confinement */
514ebc 565     CFG_INT("plane_confinement_switch", 0, CFGF_NONE),
SP 566     CFG_FLOAT("plane_d", 15, CFGF_NONE),
567     CFG_FLOAT("plane_F", 1000, CFGF_NONE),
6c274d 568 /* Variables related to stretching */
36bc6d 569 //    CFG_FLOAT("stretchswitch", 0, CFGF_NONE),
SP 570 //    CFG_FLOAT("xkA0",0,CFGF_NONE),
d7639a 571         CFG_END()
SP 572     };
573     cfg_t *cfg;    
574     ts_uint retval;
575     cfg = cfg_init(opts, 0);
698ae1 576     retval=cfg_parse_buf(cfg, buffer);
514ebc 577     tape->plane_confinement_switch=cfg_getint(cfg,"plane_confinement_switch");
88bdd7 578     tape->plane_d=cfg_getfloat(cfg,"plane_d");
SP 579     tape->plane_F=cfg_getfloat(cfg,"plane_F");
514ebc 580
d7639a 581     if(retval==CFG_FILE_ERROR){
a6b1b5 582     fatal("No tape file.",100);
d7639a 583     }
SP 584     else if(retval==CFG_PARSE_ERROR){
585     fatal("Invalid tape!",100);
586     }
a2db52 587
7b0c07 588     /* here we override all values read from tape with values from commandline*/
SP 589     getcmdline_tape(cfg,command_line_args.tape_opts);
d7639a 590     cfg_free(cfg);
40aa5b 591
SP 592
1ab449 593     /* global variables are set automatically */
SP 594     quiet=tape->quiet;
595     return tape;
596 }
d7639a 597
1ab449 598 ts_bool tape_free(ts_tape *tape){
SP 599     free(tape->multiprocessing);
600     free(tape);
601     return TS_SUCCESS;
d7639a 602 }
f74313 603
SP 604
7b0c07 605
SP 606 ts_bool getcmdline_tape(cfg_t *cfg, char *opts){
607
608     char *commands, *backup, *saveptr, *saveopptr, *command, *operator[2];
07e3de 609     operator[0]=0;
SP 610     operator[1]=0;
7b0c07 611     ts_uint i,j;
SP 612     commands=(char *)malloc(10000*sizeof(char));
613     backup=commands; //since the pointer to commands will be lost, we acquire a pointer that will serve as backup.
614     strcpy(commands,opts);
615     for(i=0; ;i++, commands=NULL){
616         //breaks comma separated list of commands into specific commands.
617         command=strtok_r(commands,",",&saveptr);    
618         if(command==NULL) break;
619 //        fprintf(stdout,"Command %d: %s\n",i,command);    
620         //extracts name of command and value of command into operator[2] array.
621         for(j=0; j<2;j++,command=NULL){
622             operator[j]=strtok_r(command,"=",&saveopptr);
623             if(operator[j]==NULL) break;
624 //            fprintf(stdout," ---> Operator %d: %s\n",j,operator[j]);        
625         }
626         //1. check: must have 2 operators.
627         if(j!=2) fatal("Error. Command line tape options are not formatted properly",1);
628
629     //    cfg_setstr(cfg,operator[0],operator[1]);
630         cmdline_to_tape(cfg,operator[0],operator[1]);
631         //2. check: must be named properly.
632         //3. check: must be of right format (integer, double, string, ...)
633         
634     }
635     free(backup);
636     return TS_SUCCESS;
637 }
638
639
640 ts_bool cmdline_to_tape(cfg_t *cfg, char *key, char *val){
641
642     cfg_opt_t *cfg_opt=cfg_getopt(cfg,key);
643     if(cfg_opt==NULL) fatal("Commandline tape option not recognised",1); //return TS_FAIL; 
644     switch (cfg_opt->type){
645         case CFGT_INT:
646             cfg_setint(cfg,key,atol(val));
647             break;
648         case CFGT_FLOAT:
649             cfg_setfloat(cfg,key,atof(val));
650             break;
651 /*        case CFGT_BOOL:
652             cfg_setbool(cfg,operator[0],operator[1]);
653             break; */
654         case CFGT_STR:
655             cfg_setstr(cfg,key,val);
656             break;
657         default:
658             break;
659
660     }
f74313 661     return TS_SUCCESS;
SP 662 }