#include "layers.h"

void averages() {

  /* internal (kJ/mol,ps,nm)  -> reduced */
  double r_eunit    = epsilon;
  double r_rhounit  = 1.0/pow(sigma,3.0);
  double r_Vunit    = pow(sigma,3.0);
  /* SI (MPa, K) -> reduced */
  double r_tempunit = epsilon*1000.0/Rgas;
  double r_punit    = 1.0e-06*(1000.0*epsilon/NLo)/pow(sigma*1.0e-09,3.0);

  double prodsteps = (double)(currentstep-neqsteps);
  double macptdele = ((double)ndeleacpt)/((double)ndeletried);
  double macptcrea = ((double)ncreaacpt)/((double)ncreatried);
  double macptdisp;

  double mvir       = Vvdwsum/prodsteps;
  double mU         = Evdwsum/prodsteps;
  double mP;
  double mT;
  double mrho;
  double mmuex;
  double tmp = Evdw;

  if(MC || GCMC)
    macptdisp = ((double)ndispacpt)/((double)ndisptried);
  else
    macptdisp = 0.0;

  forces();

  if(MD||MC)     mrho = ((double)nsites)/V; else mrho = rhosum/prodsteps;
  if(MD||GCMD)   mT = Tsum/prodsteps; else mT = Tsoll;
  if(GCMD||GCMC) mmuex = (BA-log(mrho*V))*Rgas*Tsoll/1000.0; else mmuex = 0.0;

  mP = 1.0e-06*(kB*mT*mrho/1.0e-27+1000.0*mvir/NLo/V/1.0e-27/3.0);

  fprintf(output,"# averages: [internal]         [reduced]\n");
  fprintf(output,"# V ------- %14.6le --- %14.6le\n",V,V/r_Vunit);
  fprintf(output,"# T ------- %14.6le --- %14.6le\n",mT,mT/r_tempunit);
  fprintf(output,"# rho ----- %14.6le --- %14.6le\n",mrho,mrho/r_rhounit);
    if(GCMD||GCMC)
  fprintf(output,"# muex ---- %14.6le --- %14.6le\n",mmuex,mmuex/r_eunit);
  fprintf(output,"# Uvdw ---- %14.6le --- %14.6le\n",mU,mU/r_eunit);
  fprintf(output,"# P ------- %14.6le --- %14.6le\n",mP,mP/r_punit);
  fprintf(output,"# accaptance ratios:\n");
  fprintf(output,"# displace: %.4lf, insert: %.4lf, delete: %.4lf\n",macptdisp,macptcrea,macptdele);
  fprintf(output,"# Evdw: running: %14.6le final: %14.6le diff: %14.6le\n", tmp,Evdw,tmp-Evdw);
}
