/* Title: arrl.c
 * Author: Jed Marti
 * Description: Better coefficients for the ARLL equation for various subsets. Exhaustive search
 *   in the area.
 * Revision History: (Created Mon Jan 22 20:54:43 2024)
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "math.h"

#include "arrl.h"

// The list of inductors to test against.
extern anInductor *theInductors;

// Range limits - a symbol table.
extern symtab S[];

// The number of subtasks we can run.
extern int32_t procCount;

// The values from the control file.
double numl, numh, numinc;
double dial, diah, diainc;
double lenl, lenh, leninc;

// The "optimum" values.
double bestgoodness;
double bestdia;
double bestlen;
double bestnum;


int32_t main(int32_t argc, char *argv[])
{
  double     num;
  
  printf("arrl V%d.%02d.%03d\n", VERSION, RELEASE, PATCH);

  // Punt if not the right number of arguments.
  if (argc != 4)
  {
    fprintf(stderr, "Usage: arrl coils.csv vars procs\n");
    exit(-1);
  }

  // These are the test inductors and their measurements.
  readTestInductors(argv[1]);

  // The range limits - primitive cotntrol table.
  readVariables(argv[2]);

  // How may processes to grab for work.
  procCount = atoi(argv[3]);
  if (procCount < 1 || procCount > 48)
  {
    fprintf(stderr, "Process count %d illegal\n", procCount);
    exit(-1);
  }

  // Look uyp the values.
  numl = lookup("lownum");
  numh = lookup("highnum");
  numinc = lookup("incnum");
  dial = lookup("lowdia");
  diah = lookup("highdia");
  diainc = lookup("incdia");
  lenl = lookup("lowlen");
  lenh = lookup("highlen");
  leninc = lookup("inclen");

  
  // This is the value to beat.
  printf("RMS errors\n");
  printf("Original equation %g%%\n", round(originalARRL() * 10000.0) / 100.0);
  
  initProcs();

  // Run based on the numerator value.
  bestgoodness = 1.0E208;
  for (num = numl; num <= numh; num += numinc) runNum(num);

  // Kill all the subprocesses.
  terminateProcs();

  // Write the results.
  printf("Best RMS error %g%% \n", round(bestgoodness * 10000.0) / 100.0);
  printf("  numerator  %12g\n", bestnum);
  printf("  diameter   %12g\n", bestdia);
  printf("  length     %12g\n", bestlen);
}


// EOF
