/*************************************** fraga.c **************************************
*
* This program computes the effective AC resistance of a coil and the resulting Q,
* with or without a topload. It uses equations developed by Fraga and presented in a
* paper written by Dr.Gary Anderson, and the Les and Ces values computed by JAVATC.
* This method is most useful for close wound coils with height to diameter (H/D)
* ratios greater than 3:1  The methodology accounts for skin effects due to AC current
* and proximity effects caused by adjacent windings.  This methodology has increasing
* errors for coils with low H/D ratios or for coils that are space wound.  Inputs to
* the program are prompted.  Dimensions for the coil are in inches and wire size is
* the American Wire Guage.  Results are written to "output.txt" that is created in the
* current directory that the executable resides in.
*
* by Gerry Reynolds
*
* Revision 1.0   October 18, 2005
*          1.1   October 20, 2005 Clean up, Change freq input to KHz
*          1.2   October 22, 2005 Changed program to allow space wound.
*
**************************************************************************************/

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

int main(argc,argv)
int argc;
char *argv[];
{
  int       N,              /* Number of turns                        */
            awg;            /* American wire guage                    */

  double    D,              /* Coil diameter          - inches        */
            H,              /* Coil height            - inches        */
            Fmeas,          /* Measured frequency     - KHz           */
            Fjavatc,        /* Javatc's frequency     - KHz           */
            freq,           /* Frequency              - KHz           */
            phi,            /* Angle                  - radians       */
            effect_rho,     /* Effective resistivity  - ohm meters    */
            effect_sd,      /* Effective skin depth   - meters        */
            Les,            /* Effective Inductance   - heneries      */
            Ces,            /* Effective Capacitance  - fareds        */
            Res,            /* Effective Resistance   - ohms          */
            Q,              /* Fraga based Coil Q                     */
            pitch,          /* Resulting winding pitch                */
            space,          /* Space between copper   - (inches)      */
            wire[40];       /* Copper diameter vs awg - (inches)      */

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

#define PI 3.1415926535
#define RHO 1.724E-8        /* Resistivity of Copper - ohm meters    */

//*********************************** wire table ***********************************

  wire[1]  = .2893;
  wire[2]  = .2576;
  wire[3]  = .2294;
  wire[4]  = .2043;
  wire[5]  = .1819;
  wire[6]  = .1620;
  wire[7]  = .1443;
  wire[8]  = .1285;
  wire[9]  = .1144;
  wire[10] = .1019;
  wire[11] = .0907;
  wire[12] = .0808;
  wire[13] = .0720;
  wire[14] = .0641;
  wire[15] = .0571;
  wire[16] = .0508;
  wire[17] = .0453;
  wire[18] = .0403;
  wire[19] = .0359;
  wire[20] = .0320;
  wire[21] = .0285;
  wire[22] = .0253;
  wire[23] = .0226;
  wire[24] = .0201;
  wire[25] = .0179;
  wire[26] = .0159;
  wire[27] = .0142;
  wire[28] = .0126;
  wire[29] = .0113;
  wire[30] = .0100;
  wire[31] = .0089;
  wire[32] = .0080;
  wire[33] = .0071;
  wire[34] = .0063;
  wire[35] = .0056;
  wire[36] = .0050;
  wire[37] = .0045;
  wire[38] = .0040;
  wire[39] = .0035;

//******************************** Input Coil Parameters ******************************

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

  printf ("********************* INPUT COIL DATA *********************\n");
  printf ("\nCoil Winding Height (inches)?\n");
  scanf  ("%lf", &H);
  printf ("\nCoil Diameter (inches)?\n");
  scanf  ("%lf",&D);
  printf ("\nNumber of Turns?\n");
  scanf  ("%u",&N);
  printf ("\nWire Guage (awg)?\n");
  scanf  ("%u",&awg);
  printf ("\nMeasured Freqency (KHz)?    (zero if not available)\n");
  scanf  ("%lf",&Fmeas);

  fprintf (f,"\n\n********************* COIL DATA *********************\n\n");
  fprintf (f,"Coil Height        = %4.1lf inches\n",H);
  fprintf (f,"Coil Diameter      = %4.1lf inches\n",D);
  fprintf (f,"Number of Turns    = %4u\n",N);
  fprintf (f,"Wire Guage (awg)   =   %2u\n",awg);
  fprintf (f,"Measured Frequency = %4.1lf KHz\n\n",Fmeas);

  pitch = H/N;
  space = pitch - wire[awg];

  if (space <= 0)
  {
    fprintf (f, "ERROR: no spacing between copper\n");
    exit (-1);
  }

  printf ("\n\n******************** INPUT JAVATC DATA ********************\n");
  printf ("\nJavatc's Les (mh)?    (zero if not available)\n");
  scanf  ("%lf", &Les);
  printf ("\nJavatc's Ces (pf)?    (zero if not available)\n");
  scanf  ("%lf", &Ces);
  printf ("\nJavatc's Frequency (KHz)?    (zero if not available)\n");
  scanf  ("%lf", &Fjavatc);

  fprintf (f,"\n\n******************** JAVATC DATA ********************\n\n");
  fprintf (f,"Les  = %4.1lf mH\n",Les);
  fprintf (f,"Ces  = %4.1lf pf\n",Ces);
  fprintf (f,"Freq = %4.1lf KHz\n\n",Fjavatc);

  Les = Les/1000;                                     /* convert to heneries */
  Ces = Ces/1000000000000;                            /* convert to fareds   */

  if (Fjavatc == 0)
	if (Fmeas == 0)
	{
	  fprintf (f,"\nERROR: need Measured freq or Javatc freq to proceed\n");
	  exit (-1);
	}
	else
	  freq = Fmeas;
  else
  freq = Fjavatc;                       /* Javatc frequency takes precedence */

  fprintf (f,"Frequency used       = %3.1lf KHz\n",freq);

  effect_rho = 2*RHO*(1+space/wire[awg])/sqrt(PI);
  fprintf (f,"Effective RHO        = %12.11lf ohm meters\n",effect_rho);

  effect_sd = 0.0702*sqrt((1+space/wire[awg])/(1000*freq));
  fprintf (f,"Effective Skin Depth = %5.4lf inches\n",39.37*effect_sd);

  phi = 0.0254*wire[awg]*sqrt(PI)/effect_sd;

  Res = PI*N*N*D*effect_rho*(sinh(phi)+sin(phi))/(H*effect_sd*(cosh(phi)-cos(phi)));
  fprintf (f,"\nEffective AC Resistance of Coil = %5.1lf ohms\n",Res);

  if ((Les==0) || (Ces==0))
  {
	fprintf (f,"\nNo Javatc Data to compute Coil Q\n");
	exit (0);
  }


  Q = sqrt(Les/Ces) / Res;
  fprintf (f,"\nCoil Q = %4.0lf\n\n",Q);

  fclose (f);
  exit (0);

}
