#!/usr/bin/env perl

## Approximate inductance of a straight wire

## This file Copyright 2000 <contact@gbppr.org> under the GPL
## NO WARRANTY. Please send bug reports / patches / reports.

print "\n\n\t\tApproximate Straight Wire Inductance\n\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;
 }
}


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

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

$ansr = abs ((0.00508 * $len) * ((log ((2 * $len) / $radi)) - 0.75));
$dia = 2 * $radv;

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