Trisurf Monte Carlo simulator
Samo Penic
2014-03-08 0fb40c28545e8418469cf7d3600547dfe4083edb
src/io.c
@@ -11,8 +11,11 @@
#include "initial_distribution.h"
#include "poly.h"
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
/** DUMP STATE TO DISK DRIVE **/
ts_bool dump_state(ts_vesicle *vesicle){
@@ -22,7 +25,7 @@
    FILE *fh=fopen("dump.bin","wb");
    /* dump vesicle */
    fwrite(vesicle, sizeof(vesicle),1,fh);
    fwrite(vesicle, sizeof(ts_vesicle),1,fh);
    /* dump vertex list */
    fwrite(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
    /* dump bond list */
@@ -127,6 +130,8 @@
        poly_list->poly->bond
*/
   fwrite(vesicle->clist, sizeof(ts_cell_list),1,  fh);
    fclose(fh);
    return TS_SUCCESS;
}
@@ -142,7 +147,9 @@
/* we restore all the data from the dump */
    /* restore vesicle */
    ts_vesicle *vesicle=(ts_vesicle *)calloc(1,sizeof(ts_vesicle));
    retval=fread(vesicle, sizeof(vesicle),1,fh);
    retval=fread(vesicle, sizeof(ts_vesicle),1,fh);
//   fprintf(stderr,"was here! %e\n",vesicle->dmax);
    /* restore vertex list */
    vesicle->vlist=(ts_vertex_list *)malloc(sizeof(ts_vertex_list));
    retval=fread(vesicle->vlist, sizeof(ts_vertex_list),1,fh);
@@ -170,11 +177,13 @@
    for(i=0;i<vesicle->tlist->n;i++){
        vesicle->tlist->tria[i]=(ts_triangle *)malloc(sizeof(ts_triangle));
}
  /*restore vertices*/
/* prerequisity. Vertices must be malloced before vertexes are recreated */
    vesicle->vlist->vtx=(ts_vertex **)calloc(vesicle->vlist->n,sizeof(ts_vertex *));
    for(i=0;i<vesicle->vlist->n;i++){
 for(i=0;i<vesicle->vlist->n;i++){
        vesicle->vlist->vtx[i]=(ts_vertex *)malloc(sizeof(ts_vertex));
 }
  /*restore vertices*/
    for(i=0;i<vesicle->vlist->n;i++){
        retval=fread(vesicle->vlist->vtx[i],sizeof(ts_vertex),1,fh);
        /*restore neigh, bond, tristar. Ignoring cell */
        vesicle->vlist->vtx[i]->neigh=(ts_vertex **)calloc(vesicle->vlist->vtx[i]->neigh_no, sizeof(ts_vertex *));
@@ -298,13 +307,119 @@
        }
    }
// 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!
       }
    if(retval); 
    fclose(fh);
    return vesicle;
}
ts_bool parse_args(int argc, char **argv){
 int c, retval;
 DIR *dir;
    path[0]=0;
while (1)
     {
       static struct option long_options[] =
         {
           {"force-from-tape", no_argument,       &force_from_tape, 1},
           {"tape",     no_argument,       0, 't'},
           {"output-file",  required_argument, 0, 'o'},
           {"directory",  required_argument, 0, 'd'},
           {"dump-file", required_argument,0, 'f'},
           {0, 0, 0, 0}
         };
       /* getopt_long stores the option index here. */
       int option_index = 0;
       c = getopt_long (argc, argv, "d:fot",
                        long_options, &option_index);
       /* Detect the end of the options. */
       if (c == -1)
         break;
       switch (c)
         {
         case 0:
           /* 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);
           if (optarg)
             printf (" with arg %s", optarg);
           printf ("\n");
           break;
         case 't':
            //check if tape exists. If not, fail immediately.
           puts ("option -t\n");
           break;
         case 'o':
            //set filename of master output file
           printf ("option -o with value `%s'\n", optarg);
           break;
         case 'd':
            //check if directory exists. If not create one. If creation is
            //successful, set directory for output files.
            //printf ("option -d with value `%s'\n", optarg);
            dir = opendir(optarg);
            if (dir)
            {
                /* Directory exists. */
                closedir(dir);
            }
            else if (ENOENT == errno)
            {
                /* Directory does not exist. */
                retval=mkdir(optarg, 0700);
                if(retval){
                fatal("Could not create requested directory. Check if you have permissions",1);
                }
            }
            else
            {
                /* opendir() failed for some other reason. */
                fatal("Could not check if directory exists. Reason unknown",1);
            }
            ts_fprintf(stdout,"\n*** Using output directory: %s\n\n", optarg);
//            sprintf(path,"%s", optarg);
            strcpy(path, optarg);
           // ts_fprintf(stdout,"ok!\n");
           break;
        case 'f':
            //check if dump file specified exists. Defaults to dump.bin
            break;
         case '?':
           /* getopt_long already printed an error message. */
            ts_fprintf(stderr,"\n\nhere comes the help.\n\n");
            fatal("Ooops, read help first",1);
           break;
         default:
           exit (1);
         }
     }
    return TS_SUCCESS;
}
@@ -405,11 +520,11 @@
        fprintf(fh," %.17E\t%.17E\t%.17E\t%.17E\t%.17E\t%u\n",
        vlist->vtx[i]->xk,vlist->vtx[i]->c,vlist->vtx[i]->energy,
        vlist->vtx[i]->energy_h, vlist->vtx[i]->curvature, 0);
        for(j=0;j<vlist->vtx[i]->neigh_no;j++){
        for(j=0;j<vlist->vtx[i]->bond_no;j++){
            fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length_dual);
        }
            fprintf(fh,"\n");
        for(j=0;j<vlist->vtx[i]->neigh_no;j++){
        for(j=0;j<vlist->vtx[i]->bond_no;j++){
            fprintf(fh," %.17E", vlist->vtx[i]->bond[j]->bond_length);
        }
            fprintf(fh,"\n");
@@ -636,12 +751,12 @@
ts_vesicle *parsetape(ts_uint *mcsweeps, ts_uint *inititer, ts_uint *iterations){
    long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20;  // THIS IS DUE TO CONFUSE BUG!
    long int nshell=17,ncxmax=60, ncymax=60, nczmax=60, npoly=10, nmono=20, pswitch=0;  // THIS IS DUE TO CONFUSE BUG!
    char *buf=malloc(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;
    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;
@@ -651,7 +766,9 @@
        CFG_SIMPLE_INT("nmono", &nmono),
        CFG_SIMPLE_FLOAT("dmax", &dmax),
        CFG_SIMPLE_FLOAT("xk0",&xk0),
        CFG_SIMPLE_FLOAT("k_spring",&kspring),
   CFG_SIMPLE_INT("pswitch",&pswitch),
   CFG_SIMPLE_FLOAT("pressure",&pressure),
   CFG_SIMPLE_FLOAT("k_spring",&kspring),
        CFG_SIMPLE_FLOAT("stepsize",&stepsize),
        CFG_SIMPLE_INT("nxmax", &ncxmax),
        CFG_SIMPLE_INT("nymax", &ncymax),
@@ -689,12 +806,16 @@
    vesicle->nshell=nshell;
    vesicle->dmax=dmax*dmax;
    vesicle->bending_rigidity=xk0;
    vtx_set_global_values(vesicle); //copies xk0 to every vertex
    vesicle->stepsize=stepsize;
    vesicle->clist->ncmax[0]=ncxmax;
    vesicle->clist->ncmax[1]=ncymax;
    vesicle->clist->ncmax[2]=nczmax;
    vesicle->clist->max_occupancy=8;
   vesicle->pressure=pressure/vesicle->bending_rigidity;   //all energy contributions need to be divided by bending_rigidity!
    vesicle->pswitch=pswitch;
    cfg_free(cfg);
   free(buf);
  //  fprintf(stderr,"NSHELL=%u\n",vesicle->nshell);