/*
 * Title: indequ.c
 * Author: Jed Marti KI7NNP
 * Description: Genetic algorithm optimization of inductor equation.
 * Revision History: (Created Tue Jan 24 06:50:22 2023)
 *   Sun Jan 29 16:54:28 2023 - JBM Remove single proc, tweaking.
 *   Wed Oct 11 20:05:13 2023 - JBM length experiment.
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "equations.h"
#include "indequ.h"

anInductor *theInductors = NULL;
extern symtab S[];


int32_t main(int32_t argc, char *argv[])
{
  FILE *tfh;

  #ifdef RMSERROR
    printf("indequ V%d.%02d.%03d %s RMS goodness\n", VERSION, RELEASE, PATCH, EQUATION);
  #else
    printf("indequ V%d.%02d.%03d %s\n", VERSION, RELEASE, PATCH, EQUATION);
  #endif

  // Command lione check.
  if (argc < 2)
  {
    printf("Usage: indequ <testinductors> [<ranges>] [outf]\n");
    printf("  Coefficients: ");
#define X(nme) printf(" %s ", #nme);
    VARS
#undef X
    printf("\n");
    exit(0);
  }

  readTestInductors(argv[1]);
  if (argc > 2)
  {
    if ((tfh = fopen(argv[2], "rb")) != NULL)
    {
      fclose(tfh);
      readSymtab(argv[2]);
      if (argc > 3) S[s_resultf].value.sv = strdup(argv[3]);
    }

    else if (argc == 3) S[s_resultf].value.sv = strdup(argv[2]);
  }

  dumpRangeTable(stdout, "test range\n");
  
  // Set the seed from the input file or default from table.
  init_genrand64((uint64_t) S[s_seed].value.lv);

  // Initialize process vector.
  initProcs();

  // Initialize the result library.
  initLibrary();

  // Fire up the optimizer.
  optimizeProc();

  // Terminate the processes by writing a 1 byte file. They're waiting for it.
  terminateProcesses();

  // Exit correctly as just falling off the end seems to keep it running.
  exit(0);
}


// EOF
