#! /usr/bin/perl -w

# program to compute lengths of drift tubes (actually gap-gap centers) for various
# isotopes and RF parameters, with some assumptions.  First and last gaps at ground on one side.

#@@@ note, no explicit type/range checking on your inputs, garbage in....you know


use strict; # make perl tell me about typos and how dumb I am - built in



# globals

#keys = H,D,T,3He values are mass in eV, assume unit positive charge elsewhere
# I subtracted the mass of one electron from each before converting amu to eV
my %ions = ('H' =>9.382377378e+8,'D' => 1.870945442e+9,'T'=> 2.808818208e+9, '3He' => 2.808799672e+9);
# can always add more later, Li might be fun too

my $ion; # which one we're computing for, eV chosen from hash above
my $freq = 13560000; #ISM band @ 13.56 mhz
my $rfv	= 11500; # assume 10kv peak (20k pk-pk)
my $inj = 20000; # injection voltage
my $tubes = 4; # even number of RF driven tubes (plus first and last gaps to ground tubes)
my $netenergy; # total energy of resulting beam
my $factor = 5.93e7; # for cm/sec calc (maybe should be 5.92xxx due to more precise e mass later known)

my $emass = 510998.928; # electron mass in electon volts

# subs
########################
sub getinput
{
 my $tmp = join ':',keys(%ions);
 print "Choose an ion to accelerate, one of:$tmp ->";
 $tmp =<STDIN>;
 chomp($tmp);
 $tmp =~ tr/htd/HTD/; # make less case sensitive
 print "you chose $tmp, $ions{$tmp} eV mass\n";
 $ion = $ions{$tmp}; # save in global for computation later
 #
 print "Injection voltage? [$inj] ->";
 $tmp = <STDIN>;
 chomp ($tmp);
 if ($tmp ne "") {$inj = $tmp;}
 print "Injection volts = $inj\n";
 #
 print "RF Frequency? [$freq] ->";
 $tmp = <STDIN>;
 chomp ($tmp);
 if ($tmp ne "") {$freq = $tmp;}
 print "Frequency = $freq\n";
 #
 print "RF peak volts? [$rfv] ->";
 $tmp = <STDIN>;
 chomp ($tmp);
 if ($tmp ne "") {$rfv = $tmp;}
 print "peak volts = $rfv\n";
 #
 print "Number of RF driven tubes? (should be even #) [$tubes] ->";
 $tmp = <STDIN>;
 chomp ($tmp);
 if ($tmp ne "") {$tubes = $tmp;}
 if ($tubes < 2) {$tubes=2;}
 print "RF driven tubes = $tubes\n"; 
 
 # beam  ion    end gaps    first two(one gap diff) ...extra tubes (over two minimum)
 $tmp = $inj + (2 * $rfv) +  (2*$rfv) + ($tubes-2)*2*$rfv;
 print "\nNet beam energy = $tmp eV\n";
}
########################
sub velocity
{ # compute cm/sec for chosen ion at volts passed in
 my $volts = shift;
 my $vel;
 $vel = $factor * sqrt(($emass * $volts)/$ion);
 return $vel; # not actually needed, last eval is returned anyway
}


# main program
##############################################################################
my $tbi; # tube index
my $vit; # volts in tube - volts
my $vlit; # velocity in tube cm/sec
my $tit; # time in tube, sec
my $len; # length of tube for half wave prop time, cm
my $thalf; #time at current F for half wave
my $tlen = 0;
my $tleni; # inches for us slobs

#print "made it\n";

getinput(); # prompts for parameters
$thalf = .5/$freq; # time in seconds for half wave of RF frequency
print "\n\n";

$vit = $inj + $rfv; # first tube sum of inject volts and one peak RF
for ($tbi=1;$tbi<=$tubes;$tbi++) # yeah, c style for loop
{
 $vlit = velocity($vit); # velocity in tube function of volts in tube
 $len = $vlit * $thalf; # cm/sec * sec = cm (units only)
 $tlen += $len;
 print "rf tube $tbi, volts in tube: $vit, tube length $len cm\n";
 $vit += 2* $rfv; # add peak to peak volts for inter RF tube gaps
}
$vit -= $rfv; # last tube is ground, only half pk pk
$vlit = velocity($vit); # velocity in tube function of volts in tube
$len = $vlit * $thalf; # cm/sec * sec = cm (units only)
print "grounded tube, beam volts: $vit, tube length $len cm\n";
$tleni = $tlen/2.54;
print "total length (minus last) $tlen cm, $tleni inches\n";

__END__
# some raw data here I worked from

For example, an electron and a positron, each with a mass of 0.511 MeV/c2, can annihilate to yield 1.022 MeV of energy. The proton has a mass of 0.938 GeV/c2, making a gigaelectronvolt a very convenient unit of mass for particle physics.

        1 GeV/c2 = 1.783×10−27 kg

The atomic mass unit, 1 gram divided by Avogadro's number, is almost the mass of a hydrogen atom, which is mostly the mass of the proton. To convert to megaelectronvolts, use the formula:

        1 amu = 931.46 MeV/c2 = 0.93146 GeV/c2
        1 MeV/c2 = 1.074×10−3 amu

Mass of proton:
1.007276466812(90) amu[1] spin 1/2 isospin 1/2 parity 1

Mass of neutron:
1.00866491600(43) u[3] spin 1/2 isospin 1/2 parity 1

Mass of H:
1.00782503207(10) amu spin 1/2+ (I assume these are with the electron)

Mass of D:
2.01410178 amu spin 1+ parity 1 (mostly)

Mass of T:
	3.0160492 amu spin 1/2+

Mass of 3He:
3.0160293 spin 1/2+

Mass of 4He
4.00260325415(6) spin 0

Mass of electron
5.4857990946(22)×10−4 u[7] amu

5.4857990946e−4

9.10938291(40)×10−31 kg[7]
0.510998928(11) MeV

Equation for speed:

V = 5.93 10e7 sqrt(EnMe/M) 

cm per sec. E in volts, Me mass of electron, M mass of actual ion, n number of unit charges on the ion

T = 1/2F  F = 13.56 mhz for ISM use

Solve for length at speed equals T.

