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