#include "layers.h"

int setup(char* inputfilename) {

  time_t t0;
  char outputfilename[200];
  int len,i;
  double temp, vsum = 0.0;

  if(MD+EN+MC+GCMC+GCMD != 1)
    merr("Only one out of MD, MC, GCMD, GCMC or EN must be given in the input file.");

  if(SHIFT && LRC)
    merr("shift and LRC options cannot be combined");

  if(KINSTA && NHT)
    merr("Cannot apply both NHT and KINSTA");

  t0 = time(NULL);

  srand(seed);
  set = 0;

  currentstep = 0;

  Evdw = 0.0;
  Vvdw = 0.0;
  dEdisp = 0.0;
  dEmodn = 0.0;
  dVdisp = 0.0;
  dVmodn = 0.0;

  Evdwsum = 0.0;
  Ekinsum = 0.0;
  Vvdwsum = 0.0;
  Tsum = 0.0;
  Psum = 0.0;
  rhosum = 0.0;

  ndispacpt = 0;
  ndeleacpt = 0;
  ncreaacpt = 0;
  nWid = 0;

  Ndegfree = 3 * nsites;
  NHzeta = 0.0;
  NHzetaold = 0.0;

  V = Lx*Ly*Lz;
  beta = 1000.0/Rgas/Tsoll;

  EshiftpIA =  4.0*epsilon*(pow(sigma/cutoff,12.0)-pow(sigma/cutoff,6.0));
  VshiftpIA =  4.0*epsilon*(-12.0*pow(sigma/cutoff,12.0)-pow(-6.0*sigma/cutoff,6.0));

  Elrc = 8.0/3.0*pi*epsilon/V*pow(sigma,3.0)*
         (pow(sigma/cutoff,9.0)/3.0-pow(sigma/cutoff,3.0));

  Vlrc = 16.0/3.0*pi/V/V*epsilon*pow(sigma,3.0)*
         (2.0*pow(sigma/cutoff,9.0)/3.0-pow(sigma/cutoff,3.0));


  for(i=0;i<100;i++) {
    vdist[i] = 0.0;
  }

  len = strlen(inputfilename);
  strncpy(outputfilename,inputfilename,len);
  outputfilename[len] = '.';
  outputfilename[len+1] = 'o';
  outputfilename[len+2] = 'u';
  outputfilename[len+3] = 't';
  outputfilename[len+4] = '\0';

  for(i=0;i<nsites;i++)
    vsum += (x[i]-xo[i])*(x[i]-xo[i])+(y[i]-yo[i])*(y[i]-yo[i])+(z[i]-zo[i])*(z[i]-zo[i]);
  temp = m*vsum/deltat/deltat/Ndegfree/Rgas;

  if((output = fopen(outputfilename,"w"))!=NULL) {

    if(MD) {
      fprintf(output,"# MD run started at %s#\n", ctime(&t0));
    } else if(MC) {
      fprintf(output,"# MC run started at %s#\n", ctime(&t0));
    } else if(GCMC) {
      fprintf(output,"# GCMC run started at %s#\n", ctime(&t0));
    } else if(GCMD) {
      fprintf(output,"# GCMD run started at %s#\n", ctime(&t0));
    } else if(EN) {
      fprintf(output,"# Energy calculation started at %s#\n", ctime(&t0));
    }

    fprintf(output,"# units: kJ/mol, MPa, K, nm, ps.\n");
    fprintf(output,"# nsites = %d\n", nsites);
    fprintf(output,"# totnsteps = %d\n", totnsteps);
    fprintf(output,"# neqsteps = %d\n", neqsteps);
    fprintf(output,"# printvl = %d\n", printvl);
    if(GCMD) {
      fprintf(output,"# nmodnMD = %d\n", nmodnMD);
      if(ASSVEL) fprintf(output,"# MB velocities are assigned to created particles\n");
      else fprintf(output,"# created particles will have zero velocity\n");
      if(RELAX) fprintf(output,"# inserted particles are going to be relaxed\n");
    }
    fprintf(output,"# velocity distribution file will be gernerated\n");
    fprintf(output,"# maxdisp = %lf\n", maxdisp);
    fprintf(output,"# L = %lf %lf %lf\n", Lx,Ly,Lz);
    fprintf(output,"# deltat = %lf\n", deltat);
    fprintf(output,"# cutoff = %lf\n", cutoff);
    if (NHT) {
      fprintf(output,"# Nose Hoover thermostat is on\n");
      fprintf(output,"# NHtau = %lf\n", NHtau);
    } else if(KINSTA) {
      fprintf(output,"# Kinetostat is on\n");
    }
    fprintf(output,"# Temperature of initial configuration: %lf K\n", temp);
    fprintf(output,"# T =   %lf\n", Tsoll);
    fprintf(output,"# V =   %lf\n", V);
    fprintf(output,"# rho = %lf\n", nsites/V);
    fprintf(output,"# B = %le\n", BA);

    if(SHIFT)
      fprintf(output,"# using shifted LJ potential\n");
    else if(LRC) { fprintf(output,"# using long range correction for LJ interactions\n");
      fprintf(output,"# Vlrc: %le, Elrc: %le\n", Vlrc,Elrc);
    } else 
      fprintf(output,"# using truncated LJ potential\n");

    fprintf(output,"#\n");

    if(MC) {
      fprintf(output,"# step     rho          <rho>         T             <T>           P             <P>");
      fprintf(output,"           Ekin          <Ekin>        Evdw          <Evdw>");
      fprintf(output,"        Virial        <Virial>      dispacpt\n");
    } else if(MD) {
      fprintf(output,"# step     rho          <rho>         T             <T>           P             <P>");
      fprintf(output,"           Ekin          <Ekin>        Evdw          <Evdw>");
      fprintf(output,"        Virial        <Virial>\n");
    } else if(GCMD) {
      fprintf(output,"# step     rho          <rho>         T             <T>           P             <P>");
      fprintf(output,"           Evdw          <Evdw>");
      fprintf(output,"        Virial        <Virial>");
      fprintf(output,"       creaacpt     deleacpt\n");
    } else if(GCMC) {
      fprintf(output,"# step     rho          <rho>           P             <P>");
      fprintf(output,"           Evdw          <Evdw>        Virial        <Virial>");
      fprintf(output,"      dispacpt      creaacpt      deleacpt\n");
    } else if(EN) {
      fprintf(output,"#   Temp         P             Ekin          Evdw          Virial\n");
    }

  } else merr("Cannot open output file.");

  return(1);
}
