Trisurf Monte Carlo simulator
Samo Penic
2019-10-16 4a35612678295c86677923de3fa5d43e90371667
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);
5c64e2 257     fprintf(fh,"<PointData Scalars=\"vertices_idx\">\n<DataArray type=\"Int64\" Name=\"vertices_idx\" format=\"ascii\">");
d7639a 258        for(i=0;i<vlist->n;i++){
a6b1b5 259         fprintf(fh,"%u ",vtx[i]->idx);
d7639a 260     }
40aa5b 261     //polymeres
SP 262     if(poly){
3c1ac1 263         poly_idx=vlist->n;
40aa5b 264         for(i=0;i<vesicle->poly_list->n;i++){
3c1ac1 265             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
58230a 266                 fprintf(fh,"%u ", poly_idx);
M 267             }
268         }
269     }
270     //filaments
271     if(fil){
272         poly_idx=vlist->n+monono*polyno;
273         for(i=0;i<vesicle->filament_list->n;i++){
274             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
275     //    fprintf(stderr,"was here\n");
3c1ac1 276                 fprintf(fh,"%u ", poly_idx);
40aa5b 277             }
SP 278         }
279     }
d7639a 280
45c708 281         fprintf(fh,"</DataArray>\n");
0a2c81 282     if(cstlist!=NULL){
SP 283         fprintf(fh,"<DataArray type=\"Int64\" Name=\"vertices_in_cluster\" format=\"ascii\">");
284         for(i=0;i<vlist->n;i++){
285             if(vtx[i]->cluster!=NULL){
286                 fprintf(fh,"%u ",vtx[i]->cluster->nvtx);
287             } else {
288                 fprintf(fh,"-1 ");
289             }
290             }
291         //polymeres
292         if(poly){
293             poly_idx=vlist->n;
294             for(i=0;i<vesicle->poly_list->n;i++){
295                 for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
296                     fprintf(fh,"-1 ");
297                 }
298             }
299         }
300         //filaments
301         if(fil){
302             poly_idx=vlist->n+monono*polyno;
303             for(i=0;i<vesicle->filament_list->n;i++){
304                 for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
305         //    fprintf(stderr,"was here\n");
306                     fprintf(fh,"-1 ");
307                 }
308             }
309         }
310
311         fprintf(fh,"</DataArray>\n");
312
313
314     }
315
45c708 316     //here comes additional data as needed. Currently only spontaneous curvature
SP 317     fprintf(fh,"<DataArray type=\"Float64\" Name=\"spontaneous_curvature\" format=\"ascii\">");
318     for(i=0;i<vlist->n;i++){
319         fprintf(fh,"%.17e ",vtx[i]->c);
320     }
321         //polymeres
322         if(poly){
323             poly_idx=vlist->n;
324             for(i=0;i<vesicle->poly_list->n;i++){
325                 for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
326                     fprintf(fh,"%.17e ", vesicle->poly_list->poly[i]->vlist->vtx[j]->c);
327                 }
328             }
329         }
330         //filaments
331         if(fil){
332             poly_idx=vlist->n+monono*polyno;
333             for(i=0;i<vesicle->filament_list->n;i++){
334                 for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
335         //    fprintf(stderr,"was here\n");
336                     fprintf(fh,"%.17e ",  vesicle->filament_list->poly[i]->vlist->vtx[j]->c);
337                 }
338             }
339         }
340     fprintf(fh,"</DataArray>\n");
341
5c64e2 342     //here comes additional data. Energy!
SP 343     fprintf(fh,"<DataArray type=\"Float64\" Name=\"bending_energy\" format=\"ascii\">");
344     for(i=0;i<vlist->n;i++){
345         fprintf(fh,"%.17e ",vtx[i]->energy*vtx[i]->xk);
346     }
347         //polymeres
348         if(poly){
349             poly_idx=vlist->n;
350             for(i=0;i<vesicle->poly_list->n;i++){
351                 for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++,poly_idx++){
352                     fprintf(fh,"%.17e ", vesicle->poly_list->poly[i]->vlist->vtx[j]->energy* vesicle->poly_list->poly[i]->k);
353                 }
354             }
355         }
356         //filaments
357         if(fil){
358             poly_idx=vlist->n+monono*polyno;
359             for(i=0;i<vesicle->filament_list->n;i++){
360                 for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++,poly_idx++){
361         //    fprintf(stderr,"was here\n");
362                     fprintf(fh,"%.17e ",  vesicle->filament_list->poly[i]->vlist->vtx[j]->energy*  vesicle->filament_list->poly[i]->k);
363                 }
364             }
365         }
366     fprintf(fh,"</DataArray>\n");
367
45c708 368     
13d445 369     fprintf(fh,"</PointData>\n<CellData>\n");
SP 370
7ec6fb 371     if(vesicle->tape->stretchswitch==1){
SP 372         fprintf(fh,"<DataArray type=\"Float64\" Name=\"stretching_energy\" format=\"ascii\">");
cf7aba 373         for(i=0;i<blist->n;i++){
SP 374             fprintf(fh, "0.0 ");
375         }
376         for(i=0;i<monono*polyno+filno*(fonono-1);i++){
13d445 377             fprintf(fh,"0.0 ");
7ec6fb 378         }
13d445 379         for(i=0;i<vesicle->tlist->n;i++){
SP 380             fprintf(fh,"%.17e ",vesicle->tlist->tria[i]->energy);
381         }
7ec6fb 382         fprintf(fh,"</DataArray>\n");
SP 383     }
45c708 384
13d445 385
SP 386
387     fprintf(fh,"</CellData>\n<Points>\n<DataArray type=\"Float64\" Name=\"Koordinate tock\" NumberOfComponents=\"3\" format=\"ascii\">\n");
d7639a 388     for(i=0;i<vlist->n;i++){
aad500 389         fprintf(fh,"%.17e %.17e %.17e\n",vtx[i]->x,vtx[i]->y, vtx[i]->z);
40aa5b 390     }
SP 391     //polymeres
392     if(poly){
393         for(i=0;i<vesicle->poly_list->n;i++){
394             for(j=0;j<vesicle->poly_list->poly[i]->vlist->n;j++){
aad500 395                 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 396             }
M 397         }
398     }
399     //filaments
400     if(fil){
401         for(i=0;i<vesicle->filament_list->n;i++){
402             for(j=0;j<vesicle->filament_list->poly[i]->vlist->n;j++){
aad500 403                 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 404             }
SP 405         }
d7639a 406     }
SP 407
408     fprintf(fh,"</DataArray>\n</Points>\n<Cells>\n<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">");
409     for(i=0;i<blist->n;i++){
e016c4 410             fprintf(fh,"%u %u\n",blist->bond[i]->vtx1->idx,blist->bond[i]->vtx2->idx);
d7639a 411     }
40aa5b 412     //polymeres
SP 413     if(poly){
3c1ac1 414         poly_idx=vlist->n;
40aa5b 415         for(i=0;i<vesicle->poly_list->n;i++){
SP 416             for(j=0;j<vesicle->poly_list->poly[i]->blist->n;j++){
3c1ac1 417 //                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 418                 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 419             }
SP 420     //grafted bonds
3c1ac1 421         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 422         }
SP 423
424     }
425     
58230a 426     //filaments
M 427     if(fil){
428         poly_idx=vlist->n+monono*polyno;
429         for(i=0;i<vesicle->filament_list->n;i++){
430             for(j=0;j<vesicle->filament_list->poly[i]->blist->n;j++){
431                 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);
432 //        fprintf(stderr,"was here\n");
433             
434             }
435         }
436
437     }
8db203 438     for(i=0;i<vesicle->tlist->n;i++){
SP 439         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);
440     }
d7639a 441     fprintf(fh,"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">");
58230a 442     for (i=2;i<(blist->n+monono*polyno+(fonono-1)*filno)*2+1;i+=2){
d7639a 443     fprintf(fh,"%u ",i);
SP 444     }
8db203 445     for(j=i+1;j<i+3*(vesicle->tlist->n);j+=3){ //let's continue counting from where we left of
SP 446         fprintf(fh,"%u ", j);
447     }
d7639a 448     fprintf(fh,"\n");
SP 449     fprintf(fh,"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
8db203 450      for (i=0;i<blist->n+monono*polyno+(fonono-1)*filno;i++){
d7639a 451         fprintf(fh,"3 ");
SP 452     }
8db203 453     for(i=0;i<vesicle->tlist->n;i++){
SP 454         fprintf(fh,"5 ");
455     }
d7639a 456     fprintf(fh,"</DataArray>\n</Cells>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
SP 457     fclose(fh);
458     return TS_SUCCESS;
459
460 }
461
144784 462 ts_bool write_pov_file(ts_vesicle *vesicle, char *filename){
SP 463     FILE *fh;
464     ts_uint i;
465     
466     fh=fopen(filename, "w");
467     if(fh==NULL){
468         err("Cannot open file %s for writing");
469         return TS_FAIL;
470     }
471
472     for(i=0;i<vesicle->tlist->n;i++){
473     
474     fprintf(fh,"\ttriangle {");
475     fprintf(fh,"\t<%e,%e,%e> <%e,%e,%e> <%e,%e,%e> }\n", 
476     vesicle->tlist->tria[i]->vertex[0]->x,
477     vesicle->tlist->tria[i]->vertex[0]->y,
478     vesicle->tlist->tria[i]->vertex[0]->z,
479
480     vesicle->tlist->tria[i]->vertex[1]->x,
481     vesicle->tlist->tria[i]->vertex[1]->y,
482     vesicle->tlist->tria[i]->vertex[1]->z,
483
484     vesicle->tlist->tria[i]->vertex[2]->x,
485     vesicle->tlist->tria[i]->vertex[2]->y,
486     vesicle->tlist->tria[i]->vertex[2]->z
487     );
488     }
489         
490     fclose(fh);
491     return TS_SUCCESS;
492 }
493
494
1ab449 495 ts_tape *parsetape(char *filename){
698ae1 496     FILE *fd = fopen (filename, "r");
SP 497     long length;
498     size_t size;
499     fseek (fd, 0, SEEK_END);
500       length = ftell (fd);
501     fseek (fd, 0, SEEK_SET);
502     size=fread (tapetxt, 1, length, fd);
503     fclose(fd);
504     if(size);
505     ts_tape *tape=parsetapebuffer(tapetxt);
506     return tape;
507 }
508
509 ts_tape *parsetapebuffer(char *buffer){
1ab449 510     ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
SP 511     tape->multiprocessing=calloc(255,sizeof(char));
40aa5b 512
d7639a 513     cfg_opt_t opts[] = {
1ab449 514         CFG_SIMPLE_INT("nshell", &tape->nshell),
SP 515         CFG_SIMPLE_INT("npoly", &tape->npoly),
516         CFG_SIMPLE_INT("nmono", &tape->nmono),
58230a 517     CFG_SIMPLE_INT("nfil",&tape->nfil),
M 518     CFG_SIMPLE_INT("nfono",&tape->nfono),
e98482 519     CFG_SIMPLE_INT("internal_poly",&tape->internal_poly),
58230a 520     CFG_SIMPLE_INT("R_nucleus",&tape->R_nucleus),
37791b 521     CFG_SIMPLE_FLOAT("R_nucleusX",&tape->R_nucleusX),
SP 522     CFG_SIMPLE_FLOAT("R_nucleusY",&tape->R_nucleusY),
523     CFG_SIMPLE_FLOAT("R_nucleusZ",&tape->R_nucleusZ),
58230a 524     CFG_SIMPLE_FLOAT("dmax", &tape->dmax),
ea1cce 525     CFG_SIMPLE_FLOAT("dmin_interspecies", &tape->dmin_interspecies),
1ab449 526         CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
SP 527     CFG_SIMPLE_INT("pswitch",&tape->pswitch),
9166cb 528     CFG_SIMPLE_INT("constvolswitch",&tape->constvolswitch),
c0ae90 529     CFG_SIMPLE_INT("constareaswitch",&tape->constareaswitch),
43c042 530     CFG_SIMPLE_FLOAT("constvolprecision",&tape->constvolprecision),
2ae815 531     CFG_SIMPLE_INT("stretchswitch",&tape->stretchswitch),
SP 532     CFG_SIMPLE_FLOAT("xkA0",&tape->xkA0),    
1ab449 533     CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
SP 534     CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
b30f45 535     CFG_SIMPLE_FLOAT("xi",&tape->xi),
1ab449 536         CFG_SIMPLE_FLOAT("stepsize",&tape->stepsize),
SP 537         CFG_SIMPLE_INT("nxmax", &tape->ncxmax),
538         CFG_SIMPLE_INT("nymax", &tape->ncymax),
539         CFG_SIMPLE_INT("nzmax", &tape->nczmax),
540         CFG_SIMPLE_INT("iterations",&tape->iterations),
541     CFG_SIMPLE_INT("mcsweeps",&tape->mcsweeps),
542     CFG_SIMPLE_INT("inititer", &tape->inititer),
819a09 543                 CFG_SIMPLE_BOOL("quiet",(cfg_bool_t *)&tape->quiet),
S 544         CFG_SIMPLE_STR("multiprocessing",&tape->multiprocessing),
1ab449 545         CFG_SIMPLE_INT("smp_cores",&tape->brezveze0),
SP 546         CFG_SIMPLE_INT("cluster_nodes",&tape->brezveze1),
547         CFG_SIMPLE_INT("distributed_processes",&tape->brezveze2),
dc77e8 548     CFG_SIMPLE_INT("spherical_harmonics_coefficients",&tape->shc),
e5858f 549     CFG_SIMPLE_INT("number_of_vertices_with_c0", &tape->number_of_vertices_with_c0),
SP 550     CFG_SIMPLE_FLOAT("c0",&tape->c0),
551     CFG_SIMPLE_FLOAT("w",&tape->w),
250de4 552     CFG_SIMPLE_FLOAT("F",&tape->F),
6c274d 553 /* Variables related to plane confinement */
514ebc 554     CFG_INT("plane_confinement_switch", 0, CFGF_NONE),
SP 555     CFG_FLOAT("plane_d", 15, CFGF_NONE),
556     CFG_FLOAT("plane_F", 1000, CFGF_NONE),
6c274d 557 /* Variables related to stretching */
36bc6d 558 //    CFG_FLOAT("stretchswitch", 0, CFGF_NONE),
SP 559 //    CFG_FLOAT("xkA0",0,CFGF_NONE),
d7639a 560         CFG_END()
SP 561     };
562     cfg_t *cfg;    
563     ts_uint retval;
564     cfg = cfg_init(opts, 0);
698ae1 565     retval=cfg_parse_buf(cfg, buffer);
514ebc 566     tape->plane_confinement_switch=cfg_getint(cfg,"plane_confinement_switch");
88bdd7 567     tape->plane_d=cfg_getfloat(cfg,"plane_d");
SP 568     tape->plane_F=cfg_getfloat(cfg,"plane_F");
514ebc 569
d7639a 570     if(retval==CFG_FILE_ERROR){
a6b1b5 571     fatal("No tape file.",100);
d7639a 572     }
SP 573     else if(retval==CFG_PARSE_ERROR){
574     fatal("Invalid tape!",100);
575     }
a2db52 576
7b0c07 577     /* here we override all values read from tape with values from commandline*/
SP 578     getcmdline_tape(cfg,command_line_args.tape_opts);
d7639a 579     cfg_free(cfg);
40aa5b 580
SP 581
1ab449 582     /* global variables are set automatically */
SP 583     quiet=tape->quiet;
584     return tape;
585 }
d7639a 586
1ab449 587 ts_bool tape_free(ts_tape *tape){
SP 588     free(tape->multiprocessing);
589     free(tape);
590     return TS_SUCCESS;
d7639a 591 }
f74313 592
SP 593
7b0c07 594
SP 595 ts_bool getcmdline_tape(cfg_t *cfg, char *opts){
596
597     char *commands, *backup, *saveptr, *saveopptr, *command, *operator[2];
07e3de 598     operator[0]=0;
SP 599     operator[1]=0;
7b0c07 600     ts_uint i,j;
SP 601     commands=(char *)malloc(10000*sizeof(char));
602     backup=commands; //since the pointer to commands will be lost, we acquire a pointer that will serve as backup.
603     strcpy(commands,opts);
604     for(i=0; ;i++, commands=NULL){
605         //breaks comma separated list of commands into specific commands.
606         command=strtok_r(commands,",",&saveptr);    
607         if(command==NULL) break;
608 //        fprintf(stdout,"Command %d: %s\n",i,command);    
609         //extracts name of command and value of command into operator[2] array.
610         for(j=0; j<2;j++,command=NULL){
611             operator[j]=strtok_r(command,"=",&saveopptr);
612             if(operator[j]==NULL) break;
613 //            fprintf(stdout," ---> Operator %d: %s\n",j,operator[j]);        
614         }
615         //1. check: must have 2 operators.
616         if(j!=2) fatal("Error. Command line tape options are not formatted properly",1);
617
618     //    cfg_setstr(cfg,operator[0],operator[1]);
619         cmdline_to_tape(cfg,operator[0],operator[1]);
620         //2. check: must be named properly.
621         //3. check: must be of right format (integer, double, string, ...)
622         
623     }
624     free(backup);
625     return TS_SUCCESS;
626 }
627
628
629 ts_bool cmdline_to_tape(cfg_t *cfg, char *key, char *val){
630
631     cfg_opt_t *cfg_opt=cfg_getopt(cfg,key);
632     if(cfg_opt==NULL) fatal("Commandline tape option not recognised",1); //return TS_FAIL; 
633     switch (cfg_opt->type){
634         case CFGT_INT:
635             cfg_setint(cfg,key,atol(val));
636             break;
637         case CFGT_FLOAT:
638             cfg_setfloat(cfg,key,atof(val));
639             break;
640 /*        case CFGT_BOOL:
641             cfg_setbool(cfg,operator[0],operator[1]);
642             break; */
643         case CFGT_STR:
644             cfg_setstr(cfg,key,val);
645             break;
646         default:
647             break;
648
649     }
f74313 650     return TS_SUCCESS;
SP 651 }