Trisurf Monte Carlo simulator
Samo Penic
2014-09-11 267db570e1ff394eca2f26ace11188e3b113577e
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>
@@ -10,7 +9,7 @@
#include <dirent.h>
#include "initial_distribution.h"
#include "poly.h"
#include "cell.h"
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -22,7 +21,7 @@
    /* 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);
@@ -169,9 +168,9 @@
        poly_list->poly->bond
*/
   fwrite(vesicle->clist, sizeof(ts_cell_list),1,  fh);
//   fwrite(vesicle->clist, sizeof(ts_cell_list),1,  fh);
/* write tape information on vesicle */
    fwrite(vesicle->tape,sizeof(ts_tape),1,fh);
//    fwrite(vesicle->tape,sizeof(ts_tape),1,fh);
   fwrite(&iteration, sizeof(ts_uint),1,fh);
    fclose(fh);
    return TS_SUCCESS;
@@ -181,10 +180,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;
    }
@@ -419,19 +418,12 @@
                vesicle->filament_list->poly[i]->blist->bond[j]->vtx2=vesicle->filament_list->poly[i]->vlist->vtx[idx];
        }
    }
    vesicle->tape=parsetape(command_line_args.tape_fullfilename);
// recreating space for cells // 
   vesicle->clist=(ts_cell_list *)malloc(sizeof(ts_cell_list));
   retval=fread(vesicle->clist, sizeof(ts_cell_list), 1,fh);
   vesicle->clist->cell=(ts_cell **)malloc(sizeof(ts_cell *)*vesicle->clist->ncmax[0]*vesicle->clist->ncmax[1]*vesicle->clist->ncmax[2]);
   for(i=0;i<vesicle->clist->ncmax[0]*vesicle->clist->ncmax[1]*vesicle->clist->ncmax[2];i++){
           vesicle->clist->cell[i]=(ts_cell *)calloc(1,sizeof(ts_cell));
           vesicle->clist->cell[i]->idx=i+1; // We enumerate cells! Probably never required!
       }
//recreating stored tape information//
    vesicle->tape=(ts_tape *)malloc(sizeof(ts_tape));
    retval=fread(vesicle->tape, sizeof(ts_tape),1,fh);
    vesicle->clist=init_cell_list(vesicle->tape->ncxmax, vesicle->tape->ncymax, vesicle->tape->nczmax, vesicle->tape->stepsize);
   vesicle->clist->max_occupancy=8;
//    vesicle->tape=(ts_tape *)malloc(sizeof(ts_tape));
//    retval=fread(vesicle->tape, sizeof(ts_tape),1,fh);
   retval=fread(iteration,sizeof(ts_uint),1,fh);
    if(retval); 
    fclose(fh);
@@ -444,9 +436,9 @@
    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");
            FILE *file;
    
while (1)
@@ -459,12 +451,13 @@
           {"output-file",  required_argument, 0, 'o'},
           {"directory",  required_argument, 0, 'd'},
           {"dump-filename", required_argument,0, 'f'},
           {"tape-options",required_argument,0,'c'},
           {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. */
@@ -483,25 +476,14 @@
           printf ("\n");
           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;
@@ -526,14 +508,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;
@@ -549,6 +523,53 @@
         }
     }
//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);
            } 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) {
                ts_fprintf(stderr,"Tape '%s' does not exist!\n",buffer);
                fatal("Please select correct tape or check permissions of the file",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;
}
@@ -724,7 +745,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;
@@ -753,10 +774,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");
@@ -956,15 +980,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),
@@ -1008,6 +1025,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);
@@ -1023,6 +1042,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;