//Selah Lynch  hbug2main.cc  Jun 2004
//sfl2@lehigh.edu

//Main file for using class HBug to make
//heatbug simulations, see declarations
//for heatbug description


#include "hbug.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void StartUser(int&, int&, int&, int&, char* &);
//only called by ROOT node in cluster
//outputs information user, and sets variables 
//to user inputed values


void EndUser(char* filename);
//ends the program in a user friendly way


void check(bool b, char* mess);


int main(int cnt, char** args){

  int myrank, ROOT=0, sizeofcluster;
  MPI_Status s;
  MPI_Init(&cnt,&args);

  MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  MPI_Comm_size(MPI_COMM_WORLD, &sizeofcluster);

  char* filename= new char [16];

  int gridsize, numbugs, howlong, seed; 
 

  ///////////////// BEGIN ACTION ///////////////////


  if(myrank==ROOT)
    StartUser(gridsize, numbugs,  howlong, seed, filename);

  MPI_Barrier(MPI_COMM_WORLD);


  int *variables;
  variables = new int [4];
  check(variables!=NULL, "Pointer Initialization failed.");

  if(myrank==ROOT){
    variables[0] = gridsize;
    variables[1] = numbugs;
    variables[2] = howlong;
    variables[3] = seed;
  }

  MPI_Bcast(variables, 4, MPI_INT, ROOT, MPI_COMM_WORLD);

 
  MPI_Bcast(filename, 16,MPI_INT,ROOT,MPI_COMM_WORLD);

  gridsize = variables[0];
  numbugs = variables[1];
  howlong = variables[2];
  seed = variables[3];


  ofstream fout(filename);

  HBug h(gridsize, numbugs, seed);

  h.SetVar(20,20,.075);

  fout<< gridsize <<' '<<howlong <<endl<<endl;
  MPI_Barrier(MPI_COMM_WORLD);

  h.Display(h.HEAT, fout, true);
  h.Display(h.BUGS, fout, true);
  for(int n=0; n<howlong; n++){ 
    h.TimeStep(); 
    h.Display(h.HEAT, fout, true);
    h.Display(h.BUGS, fout, true);
  }

  if(myrank==ROOT)
    EndUser(filename);

  ///////////////// END ACTION ///////////////////


  MPI_Finalize();

  return 0;

}




void StartUser(int& size, int& numbugs, int& howlong, int& seed, char* &ofilename){
  printf("\n\nWelcome to my Heat Bug Simulator for Parallel Computers!\n\n");
  printf("Please enter the following variables and press enter:\n");

  printf("Size of Grid (n x n)   ");
  cin>>size;

  printf("Number of Bugs   ");
  cin>>numbugs;

  printf("How long to run   ");
  cin>>howlong;

  printf("Number to seed random generator   ");
  cin>>seed;

  char* userfilename = new char [11];
  printf("Name of output file (!!LESS THAN 10 CHARACTERS!!)   ");
  cin>>userfilename;
  strcpy(ofilename,userfilename);
  delete [] userfilename;

  int cnt=0;
  while(ofilename[cnt]!='\0'&&cnt<10) cnt++;

  strcpy(ofilename+cnt,".data");
  ofilename[cnt+5]='\0';

  cerr<<"Output Filename: "<<ofilename<<endl;

  printf("\nRunning Simulation...\n\n");

}

void EndUser(char* filename){
  printf("Simulaton Done, find data in file '%s'. \n", filename);
  printf("Good Bye.\n");

}

void check(bool b, char* mess){
  if(!b) {
    printf("\n\nERROR[hbugmain.cc] - %s \n\n\n", mess);
    exit(0);
  }
}
