/*-----------------------------colorize.c---------------------------
  This is a utility to add data to an abc file which indicates
  bond energies, and other useful information. The data can then
  be visualized using the color rendering of abcmol. This software
  was written by Stephen Jordan.
  ------------------------------------------------------------------*/

#include "abcio.h"               //for loadabc and saveabc
#include "cell.h"                //for the cell structure
#include "tbinit.h"              //for tbinit
#include "potential.h"           //for energy
#include "handy.h"               //for strip_path
#include <stdio.h>               //for sprintf, fopen, FILE
#include <stdlib.h>              //for EXIT_SUCCESS and EXIT_FAILURE
#include <string.h>              //for strchr and strcpy


int main(int argc, char *argv[]) {
  FILE *infile;
  FILE *outfile;
  char filename[256];
  double tb_energy, sum;
  int i, j;
  cell C;
  if(argc < 2) {
    printf("Usage: colorize filename.abc\n");
    return EXIT_FAILURE;
  }
  for(i = 1; i < argc; i++) {
    infile = fopen(argv[i], "r");
    if(infile == NULL) {
      printf("Unable to open %s\n", argv[i]);
      return EXIT_FAILURE;
    }
    strip_path(argv[i]);
    sprintf(filename, "C_%s", argv[i]);
    outfile = fopen(filename, "w");
    if(outfile == NULL) {
      printf("Unable to create %s\n", filename);
      return EXIT_FAILURE;
    }
    printf("creating %s\n", filename);
    loadabc(infile, &C, 0);
    tbinit(&C, 0);  
    printf("atoms = %i\tbonds = %i\n", C.atoms, C.bonds);
    tb_energy = energy(C.bond_array, C.bonds);
    //the external force is zero so instead we make the atom
    //colors represent the sum of the energies of the bonds
    //posessed by each atom
    for(i = 0; i < C.atoms; i++) {
      sum = 0;
      for(j = 0; j < C.atom_array[i].neighbs; j++)
        sum += C.atom_array[i].neighb_b[j]->E;
      C.atom_array[i].f_x[0] = sum;
      C.atom_array[i].f_x[1] = 0;
      C.atom_array[i].f_x[2] = 0;
    }
    saveabc(outfile, &C, "COMM\tCreated by colorize.\n", 6, 1);
    fclose(infile);
    fclose(outfile);
    destroy_cell(&C);
  }
  printf("Done.\n");
}






