#include "layers.h"

int read_parameters(char* name) {

  FILE* file;
  char line[200];
  char word[200];
  char* wordend;
  int read = 0;

/**************************************************/
/*              ... the defaults ...              */
/**************************************************/

  nsites =      100;
  totnsteps =     1;
  neqsteps =      1;
  printvl =       1;
  Lx = Ly = Lz =  7;
  deltat =        0.001;
  NHT =           0;
  NHtau =         1.0;
  Tsoll =         2.0;
  m =             1.0;
  cutoff =        2.5;
  epsilon =       1.0;
  sigma =         1.0;
  EN =            0;
  MD =            0;
  MC =            0;
  GCMD =          0;
  GCMC =          0;
  maxdisp =       0.2;
  nmodnMD =     100;
  BA =            0.0;
  SHIFT =         0;
  LRC =           0;
  RELAX =         0;
  ASSVEL =        0;
  VELDIS =        0;
  KINSTA =        0;
  seed =          230984;

/**************************************************/
/*      open parameterfile and read input         */
/**************************************************/

  if((file=fopen(name,"r"))==NULL) {

    printf("cannot open %s\n", name); 
    exit(1);

  } else {

    while(fgets(line, 200, file)) {

      wordend = (char*)getfirst(word,line);

      if(strcmp(word,"nsites\0")==0)
        read += sscanf(wordend,"%d",&nsites);
      else if(strcmp(word,"seed\0")==0)
        read += sscanf(wordend,"%d",&seed);
      else if(strcmp(word,"totnsteps\0")==0)
        read += sscanf(wordend,"%d",&totnsteps);
      else if(strcmp(word,"eqnsteps\0")==0)
        read += sscanf(wordend,"%d",&neqsteps);
      else if(strcmp(word,"printvl\0")==0)
        read += sscanf(wordend,"%d",&printvl);
      else if(strcmp(word,"box\0")==0)
        read += sscanf(wordend,"%le %le %le",&Lx, &Ly, &Lz);
      else if(strcmp(word,"timestep\0")==0)
        read += sscanf(wordend,"%le",&deltat);
      else if(strcmp(word,"NHtau\0")==0)
        read += sscanf(wordend,"%le",&NHtau);
      else if(strcmp(word,"temperature\0")==0)
        read += sscanf(wordend,"%le",&Tsoll);
      else if(strcmp(word,"mass\0")==0)
        read += sscanf(wordend,"%le",&m);
      else if(strcmp(word,"cutoff\0")==0)
        read += sscanf(wordend,"%le",&cutoff);
      else if(strcmp(word,"epsilon\0")==0)
        read += sscanf(wordend,"%le",&epsilon);
      else if(strcmp(word,"sigma\0")==0)
        read += sscanf(wordend,"%le",&sigma);
      else if(strcmp(word,"BA\0")==0)
        read += sscanf(wordend,"%le",&BA);
      else if(strcmp(word,"maxdisp\0")==0)
        read += sscanf(wordend,"%le",&maxdisp);
      else if(strcmp(word,"nmodnMD\0")==0)
        read += sscanf(wordend,"%d",&nmodnMD);
      else if(strcmp(word,"NHT\0")==0)
        NHT = 1;
      else if(strcmp(word,"kinsta\0")==0)
        KINSTA = 1;
      else if(strcmp(word,"shift\0")==0)
        SHIFT = 1;
      else if(strcmp(word,"lrc\0")==0)
        LRC = 1;
      else if(strcmp(word,"relax\0")==0)
        RELAX = 1;
      else if(strcmp(word,"veldis\0")==0)
        VELDIS = 1;
      else if(strcmp(word,"assvel\0")==0)
        ASSVEL = 1;
      else if(strcmp(word,"MD\0")==0)
        MD = 1;
      else if(strcmp(word,"EN\0")==0)
        EN = 1;
      else if(strcmp(word,"MC\0")==0)
        MC = 1;
      else if(strcmp(word,"GCMD\0")==0)
        GCMD = 1;
      else if(strcmp(word,"GCMC\0")==0)
        GCMC = 1;
      else if(word[0]=='#' || wordend==NULL)
        ;
      else fprintf(stderr,"Found unrecogniced keyword (%s) in input\n", word);
    }
  }

  fclose(file);

  return(read);
}
