/*---------------------------------diags.c--------------------------------------
  This file contains code to display and log data collected during the running
  of the newdef software package.  It uses the diags data structure defined in
  diags.h and the defs data structure defined in defs.h.  This software was
  written by Stephen Jordan.
  ------------------------------------------------------------------------------*/

#include "diags.h"
#include "defs.h"
#include <stdio.h>


void printdiags(FILE *fp, const diags *dia, const defs *d) {
  int f;
  fprintf(fp, "max_f          %e\n", dia->max_f);
  fprintf(fp, "iterations     %i\n", dia->iter);
  fprintf(fp, "tb_energy      %e\n", dia->tb_energy);
  fprintf(fp, "tipforce       %e\t%e\t%e\n", dia->tip_force[0],
                                             dia->tip_force[1],
                                             dia->tip_force[2]);
  fprintf(fp, "center of mass %e\t%e\t%e\n", dia->cm[0],
                                             dia->cm[1],
                                             dia->cm[2]);
  for(f = 0; f < d->f_num; f++) {
    if(d->f_list[f].type > 4 || d->f_list[f].type < 0)
      fprintf(fp, "Invalid forcer type in disp_diags.\n");
    else {
      if(d->f_list[f].type != 3) { //no need to keep track of pushes
        fprintf(fp, "specific_max_f %e\t", dia->s_max_f[f]);
        if(d->f_list[f].type == 0) fprintf(fp, "LJBall\n");
        if(d->f_list[f].type == 1) fprintf(fp, "LJPlane\n");
        if(d->f_list[f].type == 2) fprintf(fp, "LJHole\n");
        if(d->f_list[f].type == 4) fprintf(fp, "Squeeze\n");
      }
    }
  }
  fprintf(fp, "\n");
}


/*The same idea as logdefs.  Will always get logged, but will only
  get displayed on the screen if the 'verbose' switch is selected.*/
void logdiags(const diags *dia, const defs *d) {
  FILE *log_ptr;
  char msg[256];
  log_ptr = fopen(d->logfile, "a");
  if(log_ptr == NULL) {
    sprintf(msg, "Unable to write to %s\n", d->logfile);
    msglog(msg, d);
    return;
  }
  printdiags(log_ptr, dia, d);
  if(d->verbose == 1) printdiags(stdout, dia, d);
  fclose(log_ptr);
}


void datalog(diags *dia, defs *d, int stage) {
  double height;
  int max, f;
  char header[65536];
  FILE *datafile;
  char msg[256];
  datafile = fopen(d->datafile, "a");
  if(datafile == NULL) {
    sprintf(msg, "Unable to open %s\n", d->datafile);
    msglog(msg, d);
    return;
  }
  if(d->f_num > 63) max = 63;
  else max = d->f_num;
  if(stage == 0) {
    def_display(header, d, "#");
    fprintf(datafile, header);
    fprintf(datafile, "#tip_height\ttb_energy\ttipforce[1]\ttipforce[2]");
    fprintf(datafile, "\ttipforce[3]\tm_center[1]\tm_center[2]\tm_center[3]");
    for(f = 0; f < max; f++) fprintf(datafile, "\tf_energy[%i]", f);
    fprintf(datafile, "\n");
  }
  height = d->f_list[d->tip].height + d->f_list[d->tip].height_step*stage;
  fprintf(datafile, "%e\t%e\t%e\t%e\t%e\t%e\t%e\t%e", height, dia->tb_energy,
    dia->tip_force[0], dia->tip_force[1], dia->tip_force[2],
    dia->cm[0], dia->cm[1], dia->cm[2]);
  for(f = 0; f < max; f++) fprintf(datafile, "\t%e", dia->obj_energy[f]);
  fprintf(datafile, "\n");
  fclose(datafile);
}

