Program SpirlInd;  {  Program to Calculate Inductance of Spirals   }


var      No_turns            : integer; { Number of Turns in Spiral Inductor }
         Ro                  : real;    { Beginning Inside Radius            }
         Cond_Wid            : real;    { Width of Conductor of Inductor     }
         Spac_Wid            : real;    { Space-Width of Inductor            }
         Sum,Sum1            : real;
         a,b,k1,k2,k3        : real;    { Arbitrary Variables for Calculations}
         i,j,k               : integer; { Looping Parameters                 }
         Mt                  : real;    { Mutual Inductance for n Loops      }
         c                   : real;    { Distance Variable Used in Ext Ind C}
         LSI                 : real;    { External Self Inductance           }
         aR,bR               : real;    { Distance Variables in Ground Plane }
         Sub_thick           : real;    { Substrate Thickness                }
         Mr                  : real;    { Negative Mutual Inductance Due to  }
                                        { Presence of Ground Plane           }
         L_tot               : real;    { Total Inductance of Spiral Inductor}
         Uo                  : real;
         w1                  : real;

Function Elp1(X: real): real;  { Complete Elliptic Integral of First Kind  }

var      a0,a1,a2,a3,a4      : real;
         b0,b1,b2,b3,b4      : real;
         x1,x2,x3,x4         : real;
         ab,ac               : real;

begin
         X:=  1 - X;
         a0:= 1.38629436112;   a1:= 0.09666344259;   a2:= 0.03590092383;
         a3:= 0.03742563713; a4:= 0.01451196212;
         b0:= 0.5;   b1:= 0.12498593597;   b2:= 0.06880248576;  b3:= 0.03328355346;
         b4:= 0.00441787012;
         x1:= X;
         x2:= X*x1;
         x3:= X*x2;
         x4:= X*x3;
         ab:= a0 + a1 * x1 + a2 * x2 + a3 * x3 + a4 * x4;
         ac:= (b0+b1*x1+b2*x2+b3*x3+b4*x4)*Ln(1/x1);
         ab:= ab + ac;
         Elp1:= ab;
end;

Function Elp2(X: real): real;  { Complete Elliptic Integral of Second Kind  }

var      a1,a2,a3,a4         : real;
         b1,b2,b3,b4         : real;
         x1,x2,x3,x4         : real;

begin
    X:=  1 - X;
    a1:= 0.44325141463;   a2:= 0.0626060122;   a3:= 0.04757383546;
    a4:= 0.01736506451;
    b1:= 0.2499836831;    b2:= 0.09200180037;  b3:= 0.04069697526;
    b4:= 0.00526449639;
    x1:= X;
    x2:= X*x1;
    x3:= X*x2;
    x4:= X*x3;
    Elp2:=1.0+a1*x1+a2*x2+a3*x3+a4*x4 + (b1*x1+b2*x2+b3*x3+b4*x4)*Ln(1/x1);
end;
begin
clrscr;
writeln;
writeln('                         Enter All Dimensions in Mils ');
writeln;
write('           Enter Number of Spirals ');
readln(No_turns);
write('           Enter Beginning Inside Radius of Spiral,  Conductor Width ');
readln(Ro,Cond_Wid);
write('           Enter The Space Width Between Turns,  Substrate Thickness ');
readln(Spac_Wid,Sub_thick);
Ro:= Ro*0.0254/1000.0;
Cond_Wid:= Cond_Wid*0.0254/1000.0;
Spac_Wid:= Spac_Wid*0.0254/1000.0;
Sub_thick:= Sub_thick*0.0254/1000.0;
Uo:= 4*Pi*1.E-7;
Sum1:= 0.0;            {  Calculation of Mutual Inductance for n Loops  }
for k:= 1 to No_turns-1  do
    begin
    Sum:= 0.0;
    a:= Ro + (k-0.5)*(Cond_Wid + Spac_Wid);
    for j:= k+1 to No_turns do
         begin
         b:= Ro + (j-0.5)*(Cond_Wid + Spac_Wid);
         k1:= sqrt(4*a*b)/(a+b);
         Sum:= Sum + Uo*sqrt(a*b)*((2/k1-1)*Elp1(k1) - 2/k1*Elp2(k1));
         end;
    Sum1:= Sum1 + Sum;
    end;
Mt:= 2*Sum1;
writeln;
writeln('               Mutual Inductance = ',Mt);
writeln;
                          { Calculation of External Self Inductance       }
w1:= Cond_Wid/2;
Sum:= 0.0;
for k:= 1 to No_turns do
    begin
    c:= Ro + (k-0.5)*(Cond_Wid + SPac_Wid);
    k2:= sqrt(4*c*(c-w1))/(2*c-w1);
    Sum:= Sum + Uo*(2*c-w1)*((1-k2*k2/2)*Elp1(k2) - Elp2(k2));
    end;
LSI:= Sum;
writeln('               External Self-Inductance = ',LSI);
writeln;
                          {  Calculation of Ground Plane Effect           }
Sum1:= 0.0;
for i:= 1 to No_turns do
    begin
    Sum:= 0.0;
    aR:= Ro + (i-0.5)*(Cond_Wid + Spac_Wid);
    for j:= 1 to No_turns do
         begin
         bR:= Ro + (j-0.5)*(Cond_WId + Spac_Wid);
         k3:= sqrt(4*aR*bR/(sqr(2*Sub_thick) + sqr(aR + bR)));
         Sum:= Sum + Uo*sqrt(aR*bR)*((2/k3-1)*Elp1(k3) - 2/k3*Elp2(k3));
         end;
    Sum1:= Sum1 + Sum;
    end;
Mr:= Sum1;
writeln('               Ground Plane Effect  = -',Mr);
writeln;
writeln;
writeln('                    Spiral Inductor Calculation Results ');
writeln;
writeln;
writeln;
writeln('            Spiral Inductor w/Ground Plane =   ',Mt+LSI-Mr);
end.
