#!/usr/bin/env perl

# Approximate Inductance of a Wire Parallel to Ground

sub Print {
print "\33[H\33[J".
      "\t\tApproximate Inductance of a Wire Parallel to Ground\n\n".
      "                                                       \n". 
      "      <---------- length -------->                     \n".
      "      ___________________________         _            \n".
      "     \@_________________________  |        _| Diameter \n".
      "                               ) |  <--+               \n".
      "                               | |     |               \n".
      "                               | |     | Height        \n".
      " ______________________________| |_____|____           \n".
      "                                                       \n".
      "               Ground                                  \n";
}

sub Get {
undef $_; undef $val; undef $unit; undef $rad;
while (!m/^([0-9.]+)(mm|MM|cm|CM|in|IN+)$/) {
	print "\nmm = millimeters\n".
	       "cm = centimeters\n".
	       "in = inches\n\n".
	       "Enter wire $VAL  [value][mm,cm,in]: ";
	chomp($_ = <STDIN>);
}

if (m/^([0-9.]+)(mm|MM|cm|CM|in|IN+)$/) {
        $val = $1;
        $unit = $2;
}

if ($unit =~ /cm/i) {
	$unit = "centimeters";
        $rad = $val / 2.54;
}

elsif ($unit =~ /mm/i) {
	$unit = "millimeters";
	$rad = $val / 25.4;
}

elsif ($unit =~ /in/i)  {
	$unit = "inches";
	$rad = $val;
 }
}

&Print;

&Get($VAL = "RADIUS");
$a = $rad;
$radu = $unit;
$radv = $val;

&Get($VAL = "LENGTH");
$b = $rad;
$lenu = $unit;
$lenv = $val;

&Get($VAL = "HEIGHT");
$h = $rad;
$hu = $unit;
$hv = $val;

$A =  $b + (sqrt (($b ** 2) + ($a ** 2)));
$Aa = sqrt (($b ** 2) + ($a ** 2));
$B =  $b + (sqrt (($b ** 2) + (4 * ($h ** 2))));
$Bb = sqrt (($b ** 2) + (4 * ($h ** 2)));
$L1 =  0.0117 * $b * (log (((2 * $h) / $a) * ($A / $B))) / (log 10);
$L2 = 0.00508 * (($Bb - $Aa) + (($b / 4) - ((2 * $h) + $a)));
$L = $L1 + $L2;
$dia = 2 * $radv;

&Print;

print  "\n\n            Wire radius : $radv $radu\n".
       "          Wire diameter : $dia $radu\n".
       "            Wire length : $lenv $lenu\n".
       "            Wire height : $hv $hu\n\n";          
printf " Approximate inductance : %.6f uH\n".
       "                        : %.6f nH\n\n", $L, $L * 1000;