#define REV " V-1.00  May 18, 2005  Terry Fritz "

/* This program has no copyright, GPL, copyleft, trademarks, or real owner
   of this Public Domain base version. */

/* Anyone can copy, steal, plagiarize, change, and use it as they wish. */

/* The general guide to the program is at http://www.drsstc.com/~terrell/modeling/ScanTesla.pdf */

/* This program iterates through and tests a large number of Tesla coil
   parameters in search of those parameters that give the best output sparks */

/* HISTORY

	V-1.00	May 14, 2005	Terry Fritz		Original starting version
*/

/* FUTURE IMPROVMENTS

Jim Lux suggest using matrix math to solve.
Marco suggests using random parameters to scan large areas.
*/

/* NOTES

	= 0.0 + 0.0i might have to be = 0.0 + 0.0 * I in some versions?
*/


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


int main(void)

{

/* INPUTS */

	double Cp = 0.0;  				/* Cprimary in Farads */
	double	Cp_start = 37.5E-9;
	double	Cp_stop = 37.5E-9;
	double  Cp_inc = 1.0E-15;
	double Rp = 0.0;				/* Rprimary in Ohms */
	double	Rp_start = 0.1;
	double	Rp_stop = 0.1;
	double	Rp_inc = 1.0E-15;
	double Lp = 0.0;				/* Lprimary in Heneries */
	double	Lp_start = 74.0E-6;
	double	Lp_stop = 74.0E-6;
	double	Lp_inc = 1.0E-6;
	double Ls = 0.0;				/* Lsecondary in Heneries */
	double	Ls_start = 100.0E-3;
	double	Ls_stop = 100.0E-3;
	double	Ls_inc = 1.0E-15;
	double K = 0.0;					/* primary to secondary coupling coefficient */
	double	K_start = 0.15;
	double  K_stop = 0.15;
	double	K_inc = 0.01;
	double Rs = 0.0;				/* Rsecondary in Ohms */
	double 	Rs_start = 500.0;
	double 	Rs_stop = 500.0;
	double	Rs_inc = 1.0;
	double Ct = 0.0;				/* Cterminal in Farads */
	double	Ct_start = 25E-12;
	double 	Ct_stop = 25E-12;
	double 	Ct_inc = 1.0E-15;
	double Cl = 0.0;				/* Cload in Farads */
	double	Cl_start = 3E-12;
	double	Cl_stop = 3E-12;
	double	Cl_inc = 1.0E-15;
	double Rl = 0.0;				/* Rload in Ohms */
	double	Rl_start = 220E3;
	double 	Rl_stop = 220E3;
	double 	Rl_inc = 1.0;
	double T1 = 0.0;				/* DRSSTC T1 time in Seconds */
	double	T1_start = 0.0;
	double	T1_stop = 1.0E-3;
	double	T1_inc = 1.0E-9;
	double Vin = 340.0;			/* DRSSTC input rail voltage in Volts */
	double Vn1 = 10.0;			/* Node 1 voltage in Volts - For conventional coil case
								   Cp initial condition.  DRSSTC case = 10 Volts to start
							       the system */
/* OUTPUTS */

	double WRl = 0.0;			/* Power to load in Watts */
	double WRl_max = 0.0;
	double WRp = 0.0;			/* Power to Rprimary in Watts */
	double WRs = 0.0;			/* Power to Rsecondary in Watts */
	double Win = 0.0;			/* Power in to system in Watts */
	double ICp_peak = 0.0;		/* Cprimary peak current in Amps */
	double ICp_peak_max = 0.0;
	double VCp_peak = 0.0;		/* Cprimary peak voltage in Volts */
	double VCp_peak_max = 0.0;
	double VCt_peak = 0.0;		/* Vterminal peak voltage in Volts */
	double VCt_peak_max = 0.0;


/* INTERNAL VARIABLES */

	double M = 0.0;		/* Mutual inductance */
	double Lep = 0.0;	/* Equivalent Lp-M */
	double Les = 0.0;	/* Equivalent Ls-M */
	double Le = 0.0;  	/* Equivalent Lm */

	double VCp = 0.0;	/* VCp stored voltage */
	double VCp_old =0.0;
	double VCt = 0.0;	/* VCt stored voltage */
	double VCt_old = 0.0;
	double VCl = 0.0;	/* VCl stored voltage */
	double VCl_old = 0.0;

	complex Ip = 0.0 + 0.0i;			/* node 0 to 1 current in Amps */
	complex Ip_old = 0.0 + 0.0i;		/* node 10 old current */
	complex Is = 0.0 + 0.0i;			/* node 1 to 2 current in Amps */
	complex Is_old = 0.0 + 0.0i;		/* node 12 old current */
	complex It = 0.0 + 0.0i;			/* node 2 to e current in Amps */
	complex It_old = 0.0 + 0.0i;		/* node 2e old current */
	complex Il = 0.0 + 0.0i;			/* node e to g current in Amps */
 	complex Il_old = 0.0 + 0.0i;		/* node eg old current */


/* CONSTANTS */

	double pi = 3.14159265359; 	/* define pi */



/* PROGRAM START */

printf (" ScanTesla - Tesla Coil Parameter Search Program\n" REV "\n\n");

/* Main iteration loops */

for (Cp = Cp_start; Cp <= Cp_stop; Cp = Cp + Cp_inc)
		{
for (Rp = Rp_start; Rp <= Rp_stop; Rp = Rp + Rp_inc)
		{
for (Lp = Lp_start; Lp <= Lp_stop; Lp = Lp + Lp_inc)
		{
for (Ls = Ls_start; Ls <= Ls_stop; Ls = Ls + Ls_inc)
		{
for (K = K_start; K <= K_stop; K = K + K_inc)
		{

	M = K*sqrt(Lp*Ls);	/* Mutual inductance */
	Lep = Lp-M;			/* Equivalent Lp-M */
	Les = Ls-M;			/* Equivalent Ls-M */
	Le = M;				/* Equivalent Lm */

for (Rs = Rs_start; Rs <= Rs_stop; Rs = Rs + Rs_inc)
		{
for (Ct = Ct_start; Ct <= Ct_stop; Ct = Ct + Ct_inc)
		{
for (Cl = Cl_start; Cl <= Cl_stop; Cl = Cl + Cl_inc)
		{
for (Rl = Rl_start; Rl <= Rl_stop; Rl = Rl + Rl_inc)
		{


/* Discharge coil nodes and set to starting condiditons*/

WRl=0.0; WRp=0.0; WRs=0.0; Win=0.0; ICp_peak=0.0; VCp_peak=0.0; VCt_peak=0.0;


/* Run model */

for (T1 = T1_start; T1 <= T1_stop; T1 = T1 + T1_inc)
		{

			if (cabs (Ip_old) > 0.0) {Vin = abs(Vin);}		/* WRONG!!!Input voltage fuction */
			if (cabs (Ip_old) < 0.0) {Vin = -abs(Vin);}		/* WRONG!!!if Vin = 0 then there is no input voltage other than
									   	the initial voltage on Cp */

			/* NOT DONE!!!! Figure out loop currents */

			Ip = 0.0 + 0.0i;

			Is = 0.0 + 0.0i;

			It = 0.0 + 0.0i;

			Il = -Is - It;

			VCp = 0.0;

			VCt = 0.0;

			VCl = 0.0;


			WRl = cabs(Il * Il) * Rl * T1_inc + WRl;


		}  	/* end single run of model */


/* Test for significance */

	if (WRl >= WRl_max)
		{
			WRl_max = WRl;

		    printf (" WRl_max = %4.15f\n",WRl_max);
			printf (" Cp = %4.15f\n",Cp);
   			printf (" Rp = %4.15f\n",Rp);
   			printf (" Lp = %4.15f\n",Lp);
   			printf (" Ls = %4.15f\n",Ls);
   			printf (" K = %4.15f\n",K);
   			printf (" Rs = %4.15f\n",Rs);
   			printf (" Ct = %4.15f\n",Ct);
   			printf (" Cl = %4.15f\n",Cl);
   			printf (" Rl = %4.15f\n",Rl);
    	 }

/* Set old node data */
	Ip_old=Ip; Is_old=Is; It_old=It; It_old=It; Il_old=Il;


}}}}}}}}}  /* Iterate till Hell freezes over */

}
