/***********************************************************************************
*
* toroid_ballast.c
*
* This program is useful for designing a toroid ballast for limiting current when
* using a non current limiting power source for Tesla Coils.  Examples of such power
* sources include PIGs, PTs, and MOTs.  Given user supplied input line voltage,
* limiting current, and line frequency, the program computes the required ballast
* inductance. The user is then asked for toroid physical parameters, GAP size, and
* material properties to compute the number of turns required, the volts per turn,
* and the resulting flux density for the design.
*
* METHODOLOGY:
*
*     Reqired L = [line_voltage/limiting_current] / [2*pi*line_freq]             (1)
*
* Using Maxwell's equation:
*
*     integral{H.dl} = N*I
*
* and the boundary continuity equation Bcore = Bgap, (B=uH implies Hgap = ur*Hcore)
* the following equation results:
*
*     H = N*I/[pi*D + (ur-1)G]
*
*     where H = Magnetic field intensity in the core (amps per inch)
*           N = Number of turns
*           I = Ballast limiting current (amps)
*           D = Diameter of the toroid at the center of the core (inches)
*               (average of inside and outside diameters)
*          ur = Relative magnetic permeability of the core
*           G = Gap distance (inches)
*
* H is then used to compute the flux density B in the core:
*
*     B = ur*uo*N*I/[pi*D + (ur-1)*G]                                            (2)
*
*     where B = Magnetic flux density (B) in Gauss
*          uo = Magnetic permeability of free space
*
* Since inductance (L) is given by:
*
*     L = N*total_flux/I  and total_flux = B*A
*     L = N*B*A/I
*
*     where A = Cross sectional area of the toroid core
*
* Therefore:
*
*     B = L*I/[N*A]                                                              (3)
*
* By combining (2) and (3) and solving for N:
*
*     N = sqrt{L*[pi*D + (ur-1)*G] / [A*ur*uo]}
*
*
*
* by Gerry Reynolds
*
* Revision 1.0  October 28, 2005
*
***********************************************************************************/

#include <stdio.h>
#include <math.h>

int main(argc,argv)
int argc;
char *argv[];
{
  int      retry;      /* Retry request                                        */

  double   V,          /* Line voltage                     - volts rms         */
           I,          /* Ballast current                  - amps rms          */
           freq,       /* Line frequency                   - Hz                */
		   L,          /* Design inductance                - heneries          */
		   Dout,       /* Outside toroid diameter          - inches            */
		   Din,        /* Inside toroid diameter           - inches            */
           D,          /* Average toroid diameter          - inches            */
		   G,          /* Gap distance                     - inches            */
		   A,          /* Cross sectional core area        - square inches     */
		   B,          /* Magnetic flux density            - gauss             */
		   uo,         /* Magnetic permeability free space - heneries per inch */
		   ur,         /* Relative magnetic permeability                       */
		   N,          /* Number of turns                                      */
		   VT;         /* Volts per turn                                       */

  FILE     *f;         /* Pointer to output file                               */

#define PI 3.1415926535

  f = fopen("ballast_out.txt","w");

  uo = 4*PI*1E-7/39.37;                      /* converted to heneries per inch */

  printf ("*********************** INPUT BALLAST DATA ***********************\n");
  printf ("\nBallast voltage (rms volts)?\n");
  scanf  ("%lf", &V);
  printf ("\nBallast current (rms amps)?\n");
  scanf  ("%lf", &I);
  printf ("\nLine Frequency (Hz)?\n");
  scanf  ("%lf", &freq);

  printf ("\nOutside toroid diameter (inches)?\n");
  scanf  ("%lf", &Dout);
  printf ("\nInside toroid diameter (inches)?\n");
  scanf  ("%lf", &Din);
  printf ("\nCross sectional area of toroid core (square inches)?\n");
  scanf  ("%lf", &A);
  printf ("\nRelative magnetic permeability of core material?\n");
  scanf  ("%lf", &ur);

  D = (Dout + Din)/2;

Repeat:

  printf ("\nGAP size (inches)?\n");
  scanf  ("%lf", &G);

  L = (V/I)/(2*PI*freq);
  printf ("\nRequired Inductance      = %5.1lf mh\n",1000*L);

  N = sqrt(L*(PI*D + (ur-1)*G) / (A*ur*uo));
  printf ("Required Number of Turns = %5.0lf\n",N);

  VT=V/N;
  printf ("Volts per Turn           = %5.1lf volts\n",VT);

                                   /* convert to gauss, peak current, and meters */
  B = 10000*L*(1.414*I)/(N*A*.0254*.0254);
  printf ("Peak Flux Density        = %5.0lf gauss\n",B);


  printf ("\nTry Again?    (0=NO   1=YES)\n");
  scanf  ("%d", &retry);

  if (retry)
    goto Repeat;

  fprintf (f,"\n\n******************** BALLAST DATA ********************\n\n");
  fprintf (f,"Ballast Voltage         = %4.0lf Volts rms\n",V);
  fprintf (f,"Ballast Current         = %4.0lf Amps  rms\n",I);
  fprintf (f,"Line Frequency          = %4.0lf Hz\n",freq);
  fprintf (f,"Outside Toroid Diameter = %4.1lf inches\n",Dout);
  fprintf (f,"Inside  Toroid Diameter = %4.1lf inches\n",Din);
  fprintf (f,"Toroid Core Area        = %4.1lf square inches\n",A);
  fprintf (f,"Relative Permeability   = %4.0lf\n\n",ur);
  fprintf (f,"GAP Size                = %4.2lf inches\n",G);

  fprintf (f,"\n********************** RESULTS **********************\n\n");
  fprintf (f,"Required Inductance      = %5.1lf mh\n", 1000*L);
  fprintf (f,"Required Number of Turns = %5.0lf\n",N);
  fprintf (f,"Volts per Turn           = %5.1lf volts\n",VT);
  fprintf (f,"Peak Flux Density        = %5.0lf gauss\n",B);

  fclose (f);
  exit (0);
}


