Trisurf Monte Carlo simulator
Samo Penic
2016-02-15 854cb6571e100b4259ca70861a226a0d8a14c2c1
src/io.c
@@ -1,7 +1,6 @@
#include "general.h"
#include<stdio.h>
#include "io.h"
#include <confuse.h>
#include "vertex.h"
#include "bond.h"
#include<string.h>
@@ -16,16 +15,17 @@
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <snapshot.h>
/** DUMP STATE TO DISK DRIVE **/
ts_bool dump_state(ts_vesicle *vesicle, ts_uint iteration){
    /* save current state with wrong pointers. Will fix that later */
    ts_uint i,j,k;
    FILE *fh=fopen("dump.bin","wb");
    FILE *fh=fopen(command_line_args.dump_fullfilename,"wb");
    /* dump vesicle */
    fwrite(vesicle, sizeof(ts_vesicle),1,fh);
    fwrite(vesicle, sizeof(ts_vesicle)-sizeof(ts_double),1,fh);
    /* dump vertex list */
    fwrite(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
    /* dump bond list */
@@ -181,10 +181,10 @@
/** RESTORE DUMP FROM DISK **/
ts_vesicle *restore_state(ts_uint *iteration){
    ts_uint i,j,k;
    FILE *fh=fopen("dump.bin","rb");
    FILE *fh=fopen(command_line_args.dump_fullfilename,"rb");
    struct stat sb;
    if (stat("dump.bin", &sb) == -1) {
    if (stat(command_line_args.dump_fullfilename, &sb) == -1) {
        //dump file does not exist.
        return NULL;
    }
@@ -202,7 +202,7 @@
/* we restore all the data from the dump */
    /* restore vesicle */
    ts_vesicle *vesicle=(ts_vesicle *)calloc(1,sizeof(ts_vesicle));
    retval=fread(vesicle, sizeof(ts_vesicle),1,fh);
    retval=fread(vesicle, sizeof(ts_vesicle)-sizeof(ts_double),1,fh);
//   fprintf(stderr,"was here! %e\n",vesicle->dmax);
    /* restore vertex list */
@@ -419,7 +419,7 @@
                vesicle->filament_list->poly[i]->blist->bond[j]->vtx2=vesicle->filament_list->poly[i]->vlist->vtx[idx];
        }
    }
    vesicle->tape=parsetape("tape");
    vesicle->tape=parsetape(command_line_args.tape_fullfilename);
// recreating space for cells // 
    vesicle->clist=init_cell_list(vesicle->tape->ncxmax, vesicle->tape->ncymax, vesicle->tape->nczmax, vesicle->tape->stepsize);
   vesicle->clist->max_occupancy=8;
@@ -437,9 +437,10 @@
    int c, retval;
    struct stat sb;
    sprintf(command_line_args.path, "./"); //clear string;
    sprintf(command_line_args.output_fullfilename,"./output.pvd");
    sprintf(command_line_args.dump_fullfilename,"./dump.bin");
    sprintf(command_line_args.tape_fullfilename,"./tape");
    sprintf(command_line_args.output_fullfilename,"output.pvd");
    sprintf(command_line_args.dump_fullfilename,"dump.bin");
    sprintf(command_line_args.tape_fullfilename,"tape");
    sprintf(command_line_args.tape_templatefull,"./tape");
            FILE *file;
    
while (1)
@@ -452,12 +453,15 @@
           {"output-file",  required_argument, 0, 'o'},
           {"directory",  required_argument, 0, 'd'},
           {"dump-filename", required_argument,0, 'f'},
           {"tape-options",required_argument,0,'c'},
           {"tape-template", required_argument,0,0},
            {"dump-from-vtk",required_argument,0,0},
           {0, 0, 0, 0}
         };
       /* getopt_long stores the option index here. */
       int option_index = 0;
       c = getopt_long (argc, argv, "d:f:o:t:",
       c = getopt_long (argc, argv, "d:f:o:t:c:",
                        long_options, &option_index);
       /* Detect the end of the options. */
@@ -470,31 +474,27 @@
           /* If this option set a flag, do nothing else now. */
           if (long_options[option_index].flag != 0)
             break;
           printf ("option %s", long_options[option_index].name);
/*           printf ("option %s", long_options[option_index].name);
           if (optarg)
             printf (" with arg %s", optarg);
           printf ("\n");
             printf (" with arg %s", optarg);
           printf ("\n"); */
            //TODO: find a better way.
            if(strcmp(long_options[option_index].name,"tape-template")==0){
                strcpy(command_line_args.tape_templatefull,optarg);
            }
            if(strcmp(long_options[option_index].name,"dump-from-vtk")==0){
                strcpy(command_line_args.dump_from_vtk,optarg);
            }
           break;
         case 't':
            //check if tape exists. If not, fail immediately.
            if (stat(optarg, &sb) == -1) {
                ts_fprintf(stderr,"Tape '%s' does not exist!\n",optarg);
                fatal("Please select correct tape",1);
            } else {
         case 'c':
              strcpy(command_line_args.tape_opts,optarg);
            break;
         case 't': //tape
                strcpy(command_line_args.tape_fullfilename,optarg);
            }
           break;
         case 'o':
            //set filename of master output file
            if ((file = fopen(optarg, "w")) == NULL) {
                fprintf(stderr,"Could not create output file!\n");
                fatal("Please specify correct output file",1);
            } else {
                fclose(file);
            }
         case 'o':  //set filename of master pvd output file
            strcpy(command_line_args.output_fullfilename, optarg);
            break;
@@ -519,14 +519,6 @@
           break;
        case 'f':
            //check if dump file specified exists. Defaults to dump.bin
            if ((file = fopen(optarg, "w")) == NULL) {
                fprintf(stderr,"Could not create dump file!\n");
                fatal("Please specify correct dump file",1);
            } else {
                fclose(file);
            }
            strcpy(command_line_args.dump_fullfilename, optarg);
            break;
@@ -542,10 +534,64 @@
         }
     }
//Here we set correct values for full filenames!
    char *buffer=(char *)malloc(10000*sizeof(char));
    //correct the path and add trailing /
    if(command_line_args.path[strlen(command_line_args.path)-1]!='/') strcat(command_line_args.path,"/");
/* master pvd output file */
    strcpy(buffer,command_line_args.path);
    strcat(buffer,command_line_args.output_fullfilename);
    if ((file = fopen(buffer, "w")) == NULL) {
                fprintf(stderr,"Could not create output file %s!\n", buffer);
                fatal("Please specify correct output file or check permissions of the file",1);
                //there is a tape template. make a copy into desired directory
            } else {
                fclose(file);
                strcpy(command_line_args.output_fullfilename,buffer);
            }
/* tape file */
    strcpy(buffer,command_line_args.path);
    strcat(buffer,command_line_args.tape_fullfilename);
    if (stat(buffer, &sb) == -1) {
                //tape does not exist. does tape template exist?
                if(stat(command_line_args.tape_templatefull, &sb)==-1){
                    ts_fprintf(stderr,"Tape '%s' does not exist and no tape template was specified (or does not exist)!\n",buffer);
                    fatal("Please select correct tape or check permissions of the file",1);
                } else {
                    //tape template found
                    fatal("Samo did not program template copy yet",1);
                }
            } else {
                strcpy(command_line_args.tape_fullfilename,buffer);
            }
/* dump file */
            strcpy(buffer,command_line_args.path);
            strcat(buffer,command_line_args.dump_fullfilename);
            //check if dump file exist first.
            if (stat(buffer, &sb) == -1) {
                //no dump file. check if we can create one.
                if ((file = fopen(buffer, "w")) == NULL) {
                    fprintf(stderr,"Could not create dump file '%s'!\n",buffer);
                    fatal("Please specify correct dump file or check permissions of the file",1);
                } else {
                    fclose(file);
                    //good, file is writeable. delete it for now.
                    remove(buffer);
                }
            }
            strcpy(command_line_args.dump_fullfilename, buffer);
    free(buffer);
    return TS_SUCCESS;
}
@@ -717,7 +763,7 @@
        }
   fprintf(fh,"<?xml version=\"1.0\"?>\n<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n<Collection>");
   DIR *dir = opendir(".");
   DIR *dir = opendir(command_line_args.path);
   if(dir){
      struct dirent *ent;
        tstep=0;
@@ -746,10 +792,13 @@
   ts_bond_list *blist=vesicle->blist;
   ts_vertex **vtx=vlist->vtx;
    ts_uint i,j;
       char filename[255];
       char filename[10000];
        char just_name[255];
   FILE *fh;
        strcpy(filename,command_line_args.path);
       sprintf(just_name,"timestep_%.6u.vtu",timestepno);
        strcat(filename,just_name);
       sprintf(filename,"timestep_%.6u.vtu",timestepno);
   fh=fopen(filename, "w");
   if(fh==NULL){
      err("Cannot open file %s for writing");
@@ -776,7 +825,9 @@
      }
   }
   fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n <UnstructuredGrid>\n");
   fprintf(fh, "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\" compressor=\"vtkZLibDataCompressor\">\n");
   xml_trisurf_data(fh,vesicle);
   fprintf(fh, " <UnstructuredGrid>\n");
    fprintf(fh, "<Piece NumberOfPoints=\"%u\" NumberOfCells=\"%u\">\n",vlist->n+monono*polyno+fonono*filno, blist->n+monono*polyno+filno*(fonono-1));
    fprintf(fh,"<PointData Scalars=\"scalars\">\n<DataArray type=\"Int64\" Name=\"scalars\" format=\"ascii\">");
      for(i=0;i<vlist->n;i++){
@@ -949,15 +1000,8 @@
ts_tape *parsetape(char *filename){
  //  long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20, pswitch=0;  // THIS IS DUE TO CONFUSE BUG!
    ts_tape *tape=(ts_tape *)calloc(1,sizeof(ts_tape));
    tape->multiprocessing=calloc(255,sizeof(char));
  /*  long int brezveze0=1;
    long int brezveze1=1;
    long int brezveze2=1;
    ts_double xk0=25.0, dmax=1.67,stepsize=0.15,kspring=800.0,pressure=0.0;
   long int iter=1000, init=1000, mcsw=1000;
*/
    cfg_opt_t opts[] = {
        CFG_SIMPLE_INT("nshell", &tape->nshell),
@@ -971,6 +1015,7 @@
        CFG_SIMPLE_FLOAT("xk0",&tape->xk0),
   CFG_SIMPLE_INT("pswitch",&tape->pswitch),
   CFG_SIMPLE_INT("constvolswitch",&tape->constvolswitch),
   CFG_SIMPLE_INT("constareaswitch",&tape->constareaswitch),
   CFG_SIMPLE_FLOAT("constvolprecision",&tape->constvolprecision),
   CFG_SIMPLE_FLOAT("pressure",&tape->pressure),
   CFG_SIMPLE_FLOAT("k_spring",&tape->kspring),
@@ -1001,6 +1046,8 @@
   fatal("Invalid tape!",100);
   }
    /* here we override all values read from tape with values from commandline*/
    getcmdline_tape(cfg,command_line_args.tape_opts);
    cfg_free(cfg);
@@ -1016,6 +1063,65 @@
}
ts_bool getcmdline_tape(cfg_t *cfg, char *opts){
   char *commands, *backup, *saveptr, *saveopptr, *command, *operator[2];
   ts_uint i,j;
   commands=(char *)malloc(10000*sizeof(char));
    backup=commands; //since the pointer to commands will be lost, we acquire a pointer that will serve as backup.
   strcpy(commands,opts);
   for(i=0; ;i++, commands=NULL){
      //breaks comma separated list of commands into specific commands.
      command=strtok_r(commands,",",&saveptr);
      if(command==NULL) break;
//      fprintf(stdout,"Command %d: %s\n",i,command);
      //extracts name of command and value of command into operator[2] array.
      for(j=0; j<2;j++,command=NULL){
         operator[j]=strtok_r(command,"=",&saveopptr);
         if(operator[j]==NULL) break;
//         fprintf(stdout," ---> Operator %d: %s\n",j,operator[j]);
      }
      //1. check: must have 2 operators.
      if(j!=2) fatal("Error. Command line tape options are not formatted properly",1);
    //    cfg_setstr(cfg,operator[0],operator[1]);
        cmdline_to_tape(cfg,operator[0],operator[1]);
      //2. check: must be named properly.
      //3. check: must be of right format (integer, double, string, ...)
   }
   free(backup);
    return TS_SUCCESS;
}
ts_bool cmdline_to_tape(cfg_t *cfg, char *key, char *val){
    cfg_opt_t *cfg_opt=cfg_getopt(cfg,key);
    if(cfg_opt==NULL) fatal("Commandline tape option not recognised",1); //return TS_FAIL;
    switch (cfg_opt->type){
        case CFGT_INT:
            cfg_setint(cfg,key,atol(val));
            break;
        case CFGT_FLOAT:
            cfg_setfloat(cfg,key,atof(val));
            break;
/*        case CFGT_BOOL:
            cfg_setbool(cfg,operator[0],operator[1]);
            break; */
        case CFGT_STR:
            cfg_setstr(cfg,key,val);
            break;
        default:
            break;
    }
    return TS_SUCCESS;
}
ts_bool read_geometry_file(char *fname, ts_vesicle *vesicle){
   FILE *fh;
    ts_uint i, nvtx,nedges,ntria;