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