//-----------------------------------------------------------------------------
//
//  Peter Richter (prichter@princeton.edu)
//  Princeton Plasma Physics Laboratory
//  August 2000
//
//  chtable.cpp - Change the parameters of a particular reaction table.
//
//-----------------------------------------------------------------------------

#include <iostream>
#include <stdlib.h>
#include <string>
#include "preact/table.h"

#define M 180

using namespace std;

//-----------------------------------------------------------------------------

fp_64 f(char *prompt)              // return nonzero user response to prompt
{
  char response[M];
  fp_64 x;

  do {
    cout << prompt << '\n' << "> ";
    cin >> response;
    if (!strcmp(response, "q"))
      exit(0);
    else
      x = atof(response);
  } while (x == 0);
  return x;
}

//-----------------------------------------------------------------------------

fp_64 g(char *prompt)              // return user response to prompt
{
  char response[M];

  cout << prompt << '\n' << "> ";
  cin >> response;
  if (!strcmp(response, "q"))
    exit(0);
  else
    return fp_64(atof(response));
}

//-----------------------------------------------------------------------------

int main()
{
  char reaction[M], table[M];
  char response[M];
  fp_64 p[8];
  int n;

  // get reaction type
  do {
    cout << "Select a reaction type, or enter 'q' to quit:\n"
         << "  1 = charge exchange\n"
         << "  2 = ionization\n"
	 << "  3 = fusion\n"
	 << "> ";
    cin >> response;
    if (!strcmp(response, "q"))
      exit(0);
    else
      n = atoi(response);
  } while (n < 1 || n > 3);

  // charge exchange reaction
  if (n == 1) {
    do {
      cout << "Select a particular reaction, or enter 'q' to quit:\n"
	   << "  1 = H->H(cx)\n"
	   << "  2 = H+->He0x\n"
	   << "  3 = He++>Hex\n"
	   << "  4 = H+->Li0x\n"
	   << "  5 = He++>Lix\n"
	   << "  6 = Li+++>Lx\n"
	   << "> ";
      cin >> response;
      if (!strcmp(response, "q"))
	exit(0);
      else
	n = atoi(response);
    } while (n < 1 || n > 6);
    switch (n) {
    case 1: strcpy(reaction, "cx/cxtabl/"); break;
    case 2: strcpy(reaction, "cx/phecxt/"); break;
    case 3: strcpy(reaction, "cx/hextbl/"); break;
    case 4: strcpy(reaction, "cx/plicxt/"); break;
    case 5: strcpy(reaction, "cx/helixt/"); break;
    case 6: strcpy(reaction, "cx/lilixt/"); break;
    }
    do {
      cout << "Select the table to change, or enter 'q' to quit:\n"
	   << "  1 = sigma*v beam to cold target\n"
	   << "  2 = <sigma*v> beam to warm target\n"
	   << "  3 = <E/A> of target reagent\n"
	   << "  4 = <Vb dot Vtarg / |Vb|*|Vth|>\n"
	   << "> ";
      cin >> response;
      if (!strcmp(response, "q"))
	exit(0);
      else
	n = atoi(response);
    } while (n < 1 || n > 4);
    switch (n) {
    case 1: strcpy(table, "coldTarget"); break;
    case 2: strcpy(table, "warmTarget"); break;
    case 3: strcpy(table, "energyAvg_perAMU"); break;
    case 4: strcpy(table, "average_vDotNorm"); break;
    }
  }

  // ionization reaction
  else if (n == 2) {
    do {
      cout << "Select a particular reaction, or enter 'q' to quit:\n"
	   << "  1 = H->H(ii)\n"
	   << "  2 = H+->He0i\n"
	   << "  3 = He++>H0i\n"
	   << "  4 = He++>Hei\n"
	   << "  5 = H+->Li0i\n"
	   << "  6 = He++>Lii\n"
	   << "  7 = Li+++->H\n"
	   << "  8 = Li+++>He\n"
	   << "  9 = Li+++>Li\n"
	   << "> ";
      cin >> response;
      if (!strcmp(response, "q"))
	exit(0);
      else
	n = atoi(response);
    } while (n < 1 || n > 9);
    switch (n) {
    case 1: strcpy(reaction, "ii/iitabl/"); break;
    case 2: strcpy(reaction, "ii/pheiit/"); break;
    case 3: strcpy(reaction, "ii/hehixt/"); break;
    case 4: strcpy(reaction, "ii/heitbl/"); break;
    case 5: strcpy(reaction, "ii/pliiit/"); break;
    case 6: strcpy(reaction, "ii/heliit/"); break;
    case 7: strcpy(reaction, "ii/lihiit/"); break;
    case 8: strcpy(reaction, "ii/liheit/"); break;
    case 9: strcpy(reaction, "ii/liliit/"); break;
    }
    do {
      cout << "Select the table to change, or enter 'q' to quit:\n"
	   << "  1 = sigma*v beam to cold target\n"
	   << "  2 = <sigma*v> beam to warm target\n"
	   << "> ";
      cin >> response;
      if (!strcmp(response, "q"))
	exit(0);
      else
	n = atoi(response);
    } while (n < 1 || n > 2);
    switch (n) {
    case 1: strcpy(table, "coldTarget"); break;
    case 2: strcpy(table, "warmTarget"); break;
    }
  }

  // fusion reaction
  else {
    do {
      cout << "Select a particular reaction, or enter 'q' to quit:\n"
	   << "  1 = D->D(n)\n"
	   << "  2 = D->D(p)\n"
	   << "  3 = D->T\n"
	   << "  4 = T->D\n"
	   << "  5 = D->He3\n"
	   << "  6 = He3->D\n"
	   << "  7 = T->T\n"
	   << "  8 = T->He3\n"
	   << "  9 = He3->T\n"
	   << "> ";
      cin >> response;
      if (!strcmp(response, "q"))
	exit(0);
      else
	n = atoi(response);
    } while (n < 1 || n > 9);
    switch (n) {
    case 1: strcpy(reaction, "fs/ddntab/"); break;
    case 2: strcpy(reaction, "fs/ddptab/"); break;
    case 3: strcpy(reaction, "fs/dttabl/"); break;
    case 4: strcpy(reaction, "fs/tdtabl/"); break;
    case 5: strcpy(reaction, "fs/dh3tab/"); break;
    case 6: strcpy(reaction, "fs/h3dtab/"); break;
    case 7: strcpy(reaction, "fs/tttabl/"); break;
    case 8: strcpy(reaction, "fs/th3tab/"); break;
    case 9: strcpy(reaction, "fs/h3ttab/"); break;
    }
    do {
      cout << "Select the table to change, or enter 'q' to quit:\n"
	   << "  1 = sigma*v beam to cold target\n"
	   << "  2 = <sigma*v> beam to warm target\n"
	   << "  3 = <<sigma*v>> thermonuclear\n"
	   << "  4 = <sigma*v> gyro average\n"
	   << "> ";
      cin >> response;
      if (!strcmp(response, "q"))
	exit(0);
      else
	n = atoi(response);
    } while (n < 1 || n > 4);
    switch (n) {
    case 1: strcpy(table, "coldTarget"); break;
    case 2: strcpy(table, "warmTarget"); break;
    case 3: strcpy(table, "thermonuclear"); break;
    case 4: strcpy(table, "gyroAvg"); break;
    }
  }

  // get new table parameters
  if (!strcmp(table, "coldTarget")) {
    p[0] = f("Enter nonzero minimum energy value (keV), or 'q' to quit: ");
    p[1] = f("Enter nonzero maximum energy value (keV), or 'q' to quit: ");
    p[2] = f("Enter number of points on energy axis, or 'q' to quit: ");
    p[3] = g("Enter 0=log / 1=linear for energy spacing, or 'q' to quit: ");
    p[4] = -1;
    addtable(strcat(reaction, table), p);
  }
  else if (!strcmp(table, "warmTarget") || !strcmp(table, "energyAvg_perAMU")
	   || !strcmp(table, "average_vDotNorm")) {
    p[0] = f("Enter nonzero minimum energy value (keV), or 'q' to quit: ");
    p[1] = f("Enter nonzero maximum energy value (keV), or 'q' to quit: ");
    p[2] = f("Enter number of points on energy axis, or 'q' to quit: ");
    p[3] = g("Enter 0=log / 1=linear for energy spacing, or 'q' to quit: ");
    p[4] = f("Enter nonzero minimum temp value (keV), or 'q' to quit: ");
    p[5] = f("Enter nonzero maximum temp value (keV), or 'q' to quit: ");
    p[6] = f("Enter number of points on temp axis, or 'q' to quit: ");
    p[7] = g("Enter 0=log / 1=linear for temp spacing, or 'q' to quit: ");
    addtable(strcat(reaction, table), p);
  }
  else if (!strcmp(table, "thermonuclear")) {
    p[0] = f("Enter nonzero minimum temp value (keV), or 'q' to quit: ");
    p[1] = f("Enter nonzero maximum temp value (keV), or 'q' to quit: ");
    p[2] = f("Enter number of points on temp axis, or 'q' to quit: ");
    p[3] = g("Enter 0=log / 1=linear for temp spacing, or 'q' to quit: ");
    p[4] = -1;
    addtable(strcat(reaction, table), p);
  }
  else /* !strcmp(table, "gyroAvg") */ {
    p[0] = 1e3*f("Enter nonzero minimum energy value (keV), or 'q' to quit: ");
    p[1] = 1e3*f("Enter nonzero maximum energy value (keV), or 'q' to quit: ");
    p[2] = f("Enter number of points on energy axis, or 'q' to quit: ");
    p[3] = g("Enter 0=log / 1=linear for energy spacing, or 'q' to quit: ");
    p[4] = g("Enter dimensionless minimum energy variation, or 'q' to quit: ");
    p[5] = f("Enter dimensionless maximum energy variation, or 'q' to quit: ");
    p[6] = f("Enter number of points on variation axis, or 'q' to quit: ");
    p[7] = g("Enter 0=log / 1=linear for variation spacing, or 'q' to quit: ");
    addtable(strcat(reaction, table), p);
  }

  // done
  cout << "Table parameters changed successfully.\n";
}

//-----------------------------------------------------------------------------
