#include "layers.h"

int sum() {

  int    n = 0;
  double P,Evdwloc,Vvdwloc;
  double cs = (double)(currentstep-neqsteps);
  double rcs, rns;

  if(fabs(cs)<0.1) cs = 1.0;
  rcs = 1.0 / cs;
  rns = 1.0 / (max(1.0,((double) nsites)));

  if(MD||GCMD) Tist = 2000.0 * Ekin / (double)Ndegfree / Rgas;
  else Tist = Tsoll;

/*********************************************************************/
/*                                MD                                 */
/*********************************************************************/

  if(MD) {

    rho = ((double)nsites)/V;

    P = 1.0e-06*((double)nsites*kB*Tist/V/1.0e-27 +
        1000.0*Vvdw/NLo/V/1.0e-27/3.0);

    Evdwloc  = Evdw*rns;
    Vvdwloc  = Vvdw*rns;

    if(currentstep>neqsteps) {
      Evdwsum += Evdwloc;
      Vvdwsum += Vvdwloc;
      Psum    += P;
      Tsum    += Tist;
    }

    if(fmod((double)currentstep,(double)printvl)==0.0) {
      n+=fprintf(output,"%8d %-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le\n",
      currentstep,rho,rho,Tist,Tsum*rcs,P,Psum*rcs,Evdwloc,Evdwsum*rcs,Vvdwloc,Vvdwsum*rcs);
    }

  } else if(GCMC) {

/*********************************************************************/
/*                                GCMC                               */
/*********************************************************************/

    rho = ((double)nsites)/V;

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

    dEdisp = dEmodn = dVdisp = dVmodn = 0.0;

    P = 1.0e-06*((double)nsites*kB*Tsoll/V/1.0e-27 + 
        1000.0*Vvdw/NLo/V/1.0e-27/3.0);

    Evdwloc  = Evdw*rns;
    Vvdwloc  = Vvdw*rns;

    if(currentstep>neqsteps) {
      Evdwsum += Evdwloc;
      Vvdwsum += Vvdwloc;
      Psum    += P;
      rhosum  += rho;
    }


    if(fmod((double)currentstep,(double)printvl)==0.0) {
      n+=fprintf(output,"%8d %-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le\n",
      currentstep,rho,rhosum*rcs,P,Psum*rcs,Evdwloc,Evdwsum*rcs,Vvdwloc,Vvdwsum*rcs,
      (double)ndispacpt/(double)ndisptried,
      (double)ndeleacpt/(double)ndeletried,(double)ncreaacpt/(double)ncreatried);
    }

  } else if (GCMD) {

/*********************************************************************/
/*                             GCMD                                  */
/*********************************************************************/

    rho = ((double)nsites)/V;

    P = 1.0e-06*((double)nsites*kB*Tist/V/1.0e-27 + 
        1000.0*Vvdw/NLo/V/1.0e-27/3.0);

    Evdwloc  = Evdw*rns;
    Vvdwloc  = Vvdw*rns;

    if(currentstep>neqsteps) {
      Evdwsum += Evdwloc;
      Vvdwsum += Vvdwloc;
      Psum    += P;
      Tsum    += Tist;
      rhosum  += rho;
    }


    if(fmod((double)currentstep,(double)printvl)==0.0) {
      n+=fprintf(output,"%8d %-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le\n",
      currentstep,rho,rhosum*rcs,Tist,Tsum*rcs,P,Psum*rcs,Evdwloc,Evdwsum*rcs,Vvdwloc,
      Vvdwsum*rcs,(double)ndeleacpt/(double)ndeletried,(double)ncreaacpt/(double)ncreatried);
    }

  } else if(MC) {

/*********************************************************************/
/*                                MC                                 */
/*********************************************************************/

    rho = ((double)nsites)/V;

    Evdw    += dEdisp;
    Vvdw    += dVdisp;

    dEdisp = dVdisp = 0.0;

    P = 1.0e-06*((double)nsites*kB*Tsoll/V/1.0e-27 + 
        1000.0*Vvdw/NLo/V/1.0e-27/3.0);

    Evdwloc  = Evdw*rns;
    Vvdwloc  = Vvdw*rns;

    if(currentstep>neqsteps) {
      Evdwsum += Evdwloc;
      Vvdwsum += Vvdwloc;
      Psum    += P;
    }


    if(fmod((double)currentstep,(double)printvl)==0.0) {
      n+=fprintf(output,"%8d %-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le%-14.6le\n",
      currentstep,rho,rho,P,Psum*rcs,Evdwloc,Evdwsum*rcs,Vvdwloc,Vvdwsum*rcs,
      (double)ndispacpt/(double)ndisptried);
    }

  }

  return(n);
}
