CCPC Transmission Prediction Program
Below is a Python program I wrote to calculate the transmission coeffient of a coaxial cable photonic crystal. The formula used was found in an article in the American Journal of Physics, Volume 72, Number 7, July 2004, entitled "A model coaxial photonic crystal for studying band structures, dispersion, field localization, and superluminal effects." The authors were Alain Hache and Abderrahim Slimani. The program below assumes no attenuation. Feel free to use it. It works fine, but as my first attempt at Python programming, may be rather inefficient coding.
print
print "----------------------------------------------"
print
print "Welcome to the Coaxial Cable Photonic Crystal"
print "Transmission Prediction Program. This Program"
print "Assumes a Phase Velocity of 0.66c."
print
#First we create the empty lists to store the impedance
#and length values in for each of the cables.
impedances = []
lengths = []
print "What is the impedance of your oscilloscope?"
#These next two lines add the first entry
#to impedances.
newentry = input("Impedance (ohms) = ")
impedances.append(newentry)
print
print "How many sections of cable does your crystal"
print "consist of?"
numberofcables = input("Number of cables = ")
print
print "You will now need to enter the impedance"
print "and length of each cable, starting with the"
print "cable connected to the oscilloscope."
#This loop will run until the data for all of the
#cables has been entered. It appends new entries
#in impedances and lengths.
while numberofcables > len(impedances) - 1:
if len(impedances) == 1:
print
print "First Cable:"
else:
print
print "Next Cable:"
newentry = input("Impedance (ohms) = ")
impedances.append(newentry)
newentry = input("Length (meters) = ")
lengths.append(newentry)
print
print "What is the impedance of your transmitter?"
newentry = input("Impedance (ohms) = ")
impedances.append(newentry)
print
print "Now the overall transmission coefficient"
print "for the crystal will be calculated."
print "Please enter the range of frequencies you"
print "would like to program to sweep through."
#100000 is subtracted from the startfrequency because
#the first thing out calculating loop will do is add
#100000, and then add that again every time it goes
#through the loop.
startfrequency = input("Starting frequency (Hz) : ") - 100000
endingfrequency = input("Ending frequency (Hz) : " )
f = 0 + startfrequency
print
print "Calculating"
print
import string
true = 1
false = 0
def addcalc(calculation,frequency,output):
calculation[frequency] = output
#i will be our imaginary number.
i = 0 + 1J
#The output_list will contain the data we will, um, output.
output_list = {}
#The first while loop runs through the frequencies. The
#second will perform the recursive calculation on the
#cables if there is more than 1.
while f < endingfrequency:
f = f + 100000
n = 0
ttwoplus = []
rtwoplus = []
if len(impedances) == 3:
toneplus = (2 * impedances[n]) / (impedances[n] + impedances[n+1])
toneminus = 2 * impedances[n+1] / (impedances[n] + impedances[n+1])
roneminus = (impedances[n+1] - impedances[n]) / (impedances[n+1] + impedances[n])
roneplus = (impedances[n] - impedances[n+1]) / (impedances[n] + impedances[n+1])
ttwoplus = 2 * impedances[n+1] / (impedances[n+1] + impedances[n+2])
rtwoplus = (impedances[n+1] - impedances[n+2]) / (impedances[n+1] + impedances[n+2])
reading = (toneplus * ttwoplus * 2.71828 ** ((i * f * 2 * 3.141592654 * lengths[n]) / (0.66 * 300000000))) / (1 - roneminus * rtwoplus * 2.71828 ** ((i * f * 2 * 3.141592654 * lengths[n]) / (0.66 * 300000000)))
absreading = abs(reading)
print
print "Frequency: ",f
print "Transmission Coefficient: ",absreading
print
f = repr(f)
absreading = repr(absreading)
addcalc(output_list,f,absreading)
f = float(f)
absreading = float(absreading)
else:
while n < len(lengths):
toneplus = (2 * impedances[n+2]) / (impedances[n+1] + impedances[n+2])
toneminus = (2 * impedances[n+1]) / (impedances[n+1] + impedances[n+2])
roneminus = (impedances[n+1] - impedances[n+2]) / (impedances[n+1] + impedances[n+2])
roneplus = (impedances[n+2] - impedances[n+1]) / (impedances[n+2] + impedances[n+1])
ttwoplusinit = (2 * impedances[1]) / (impedances[0] + impedances[1])
rtwoplusinit = (impedances[1] - impedances[0]) / (impedances[1] + impedances[0])
if n == 0:
readingt = (toneplus * ttwoplusinit * 2.71828 ** ((i * 2 * 3.141592654 * f * lengths[n]) / (0.66 * 300000000))) / (1 - roneminus * rtwoplusinit * 2.71828 ** ((i * 4 * 3.141592654 * f * lengths[n]) / (0.66 * 300000000)))
ttwoplus.append(readingt)
readingr = roneplus + (toneplus * toneminus * rtwoplusinit * 2.71828 ** ((i * 4 * 3.141592654 * f * lengths[n]) / (0.66 * 300000000))) / (1 - roneminus * rtwoplusinit * 2.71828 ** ((i * 3.141592654 * 4 * f * lengths[n]) / (0.66 * 300000000)))
rtwoplus.append(readingr)
else:
readingt = (toneplus * ttwoplus[n-1] * 2.71828 ** ((i * 2 * 3.141592654 * f * lengths[n]) / (0.66 * 300000000))) / (1 - roneminus * rtwoplus[n-1] * 2.71828 ** ((i * 3.141592654 * 4 * f * lengths[n]) / (0.66 * 300000000)))
ttwoplus.append(readingt)
readingr = roneplus + (toneplus * toneminus * rtwoplus[n-1] * 2.71828 ** ((i * 4 * 3.141592654 * f * lengths[n]) / (0.66 * 300000000))) / (1 - roneminus * rtwoplus[n-1] * 2.71828 ** ((i * 3.141592654 * 4 * f * lengths[n]) / (0.66 * 300000000)))
rtwoplus.append(readingr)
n = n + 1
absreading = abs(readingt)
print
print "Frequency: ",f
print "Transmission Coefficient: ",absreading
print
f = repr(f)
absreading = repr(absreading)
addcalc(output_list,f,absreading)
f = float(f)
absreading = float(absreading)
print
print "What would you like the output file to be"
print "named?"
print
#The following section outputs the file.
outputfilename = raw_input("Name (recommend .txt): ")
out_file = open(outputfilename,"w")
for x in output_list.keys():
out_file.write(x+","+output_list[x]+"\n")
out_file.close()
print
print "Your file is located in the same directory"
print "as this program."
blog comments powered by Disqus