/*-------------------------------abcstats.c-------------------------------
  This software calculates and displays various basic information about the
  molecule represented in an .abc file. It is designed for use with
  molecules made entirely of carbon and hydrogen. This software was written
  by Stephen Jordan.
  ------------------------------------------------------------------------*/


#include <stdio.h>
#include <stdlib.h>
#include "abcio.h"

double mass(const cell *C, int i) {
  //mass of Carbon 12 in eV*S^2/A^2:
  if(C->atom_array[i].type == 1) return 1.2437e-27; 
  //mass of Hydrogen 1 in eV*S^2/A^2:
  if(C->atom_array[i].type == 2) return 1.0364e-28; 
  //otherwise
  printf("Invalid type in mass!\n");
  return 1.0;
}


void cm(const cell *C, double *coords) {
  int i;
  double mi;
  double total_mass;
  total_mass = 0.0;
  coords[0] = 0.0;
  coords[1] = 0.0;
  coords[2] = 0.0;
  for(i = 0; i < C->atoms; i++) {
    mi = mass(C, i);
    coords[0] += C->atom_array[i].x[0]*mi;
    coords[1] += C->atom_array[i].x[1]*mi;
    coords[2] += C->atom_array[i].x[2]*mi;
    total_mass += mi;
  }
  coords[0] /= total_mass;
  coords[1] /= total_mass;
  coords[2] /= total_mass;
}


int main(int argc, char *argv[]) {
  cell C;
  FILE *fp;
  int i, q;
  double max[3];
  double min[3];
  double cmass[3];
  if(argc != 2) {
    printf("Usage: abcstats filename.abc\n");
    return EXIT_FAILURE;
  }
  fp = fopen(argv[1], "r");
  if(fp == NULL) {
    printf("Unable to open %s\n", argv[1]);
    return EXIT_FAILURE;
  }
  loadabc(fp, &C, 0);
  printf("atoms = %i\n", C.atoms);
  printf("explicit bonds = %i\n", C.bonds);
  //find_neighb_i(&C);
  //printf("implied bonds  = %i\n", C.bonds);
  cm(&C, cmass);
  printf("center of mass: (%f,%f,%f)\n", cmass[0], cmass[1],cmass[2]);
  for(q = 0; q < 3; q++) {
    max[q] = C.atom_array[0].x[q];
    min[q] = C.atom_array[0].x[q];
    for(i = 1; i < C.atoms; i++) {
      if(C.atom_array[i].x[q] > max[q]) max[q] = C.atom_array[i].x[q];
      if(C.atom_array[i].x[q] < min[q]) min[q] = C.atom_array[i].x[q];
    }
  }
  printf("\tminimum         maximum\n");
  printf("x\t%f\t%f\n", min[0], max[0]);
  printf("y\t%f\t%f\n", min[1], max[1]);
  printf("z\t%f\t%f\n", min[2], max[2]);
  return EXIT_SUCCESS;
}
