'''Calculate the loss of fiber'''
from sympy import *
import math
#Variable declaration
L = 400./1000. #distance(km)
Pi = Symbol("Pi")
Po = 0.25*Pi
#Calculations
Loss = (10*math.log10(Pi/Po))/L
#Result
print "Loss =",round(Loss),"dB/km"
'''Calculate power level'''
#Variable declaration
L = 3 #distance(km)
a = 0.5 #attenuation loss(dB/km)
lamda = 0.82 #wavelength(um)
Pi = 1 #input power(mW)
#Calculation
Po = Pi*10**((-a*L)/10)
#Result
print "Power level =",round(Po,2),"mW"
'''Determine maximum possible link length'''
#Variable declaration
a = 0.5 #attenuation loss(dB/km)
Pi = 1.5*10**-3 #input power(uW)
Po = 2.*10**-6 #output power(uW)
#Calculation
L = (10*math.log10(Pi/Po))/a
#Result
print "Maximum possible link length =",round(L,2),"km"
'''Determine pulse dispersion per unit length'''
#Variable declaration
pd = 1.2*10**-3 #pulse broadening(ns)
d = 30 #distance
#Calculation
D = pd/d
#Result
print "Pulse dispersion per unit length =",D,"ns/m"
'''Determine the optical output power'''
#Variable declaration
L = 30 #distance(km)
a = 0.8 #attenuation loss(dB/km)
lamda = 1300 #wavelength(nm)
Pi = 200 #input power(uW)
#Calculation
Po = Pi*10**((-a*L)/10)
#Result
print "Optical output power =",round(Po,3),"uW"
'''Determine
a)Overall signal attenuation
b)signal attenuation per km
c)signal attenuation for the link
d)input output power ratio'''
import math
#Variable declaration
L = 8 #distance(km)
Pi = 120*10**-6 #input power(W)
Po = 3*10**-6 #output power(W)
#Calculations
#Part a
a = 10*math.log10(Pi/Po)
#Part b
adb = a/L
#Part c
l = 10
x = adb*l #loss occured along 10km of fiber
y = 9 #loss due to splices(dB)
dB = x+y
#Part d
pi_by_po = 10**(round(dB)/10)
#Results
print "a) Overall signal attenuation =",round(a),"dB"
print "b) Signal attenuation per km =",round(adb),"dB/km"
print "c) Signal attenuation for the link =",round(dB),"dB"
print "d) Input output power ratio =",round(pi_by_po,1)
'''Determine theoretical attenuation for two given wavelengths'''
import math
#Variable declaration
Tf = 1400 #fictive temperature(K)
p = 0.286 #photoelastic coeeficient for silica
n = 1.46 #refractive index
Bo = 7*10**-11 #isothermal compressibility
K = 1.381*10**-23 #Boltzman's constant(J/k)
L = 10**3
#Calculation
#For lamda = 0.63 um
lamda1 = 0.63*10**-6 #wavelength(m)
Yr1 = (8*math.pi**3*n**8*p**2*Bo*K*Tf)/(3*lamda1**4)
E1 = math.exp(-Yr1*L)
a1 = 10.*math.log10(1/E1)
#For lamda = 1.30 um
lamda2 = 1.30*10**-6 #wavelength(m)
Yr2 = (8*math.pi**3*n**8*p**2*Bo*K*Tf)/(3*lamda2**4)
E2 = math.exp(-Yr2*L)
a2 = 10.*math.log10(1/E2)
#Result
print "(solutions of example 4.8 and 4.7 are interchanged in the textbook)"
print "Theoretical attenuation =",round(a1,3),"dB/km"
print "Theoretical attenuation =",round(a2,3),"dB/km"
'''Calculating the refractive index of glass'''
import math
#Variable declaration
lamda = 1*10**-6 #wavelength(m)
p = 0.245 #photoelastic coefficient
Bc = 8.4*10**-11 #isothermal compressebility(m^2/N)
Tf = 758 #fictive temperature(K)
K = 1.381*10**-23 #Boltzman's constant(J/k)
alpha = 0.46 #attenuation(Db/Km)
L = 1.*10**3 #distance(km)
#Calculations
Yr = (8*math.pi**3*p**2*Bc*K*Tf)/(3*lamda**4)
n= (alpha/(4.34*Yr*10**3))**(1./8.)
#Result
print "Refractive index =",round(n,2)
'''Calculating the operating wavelength of laser and attenuation of fiber'''
#Variable declaration
Pb = 150.*10**-3 #threshold optical power for brillouin(W)
Pr = 1.5 #threshold optical power for raman(W)
d = 8.0 #core diameter(um)
v = 1 #bandwidth(GHz)
#Calculations
'''
Pb is given by,
Pb = 4.4*10**-3*d^2*lamda^2*alpha*v ----(1)
Pr is given by,
Pr = 5.9*10**-2*d^2*lamda*alpha ----(2)
Dividing (1) by (2), we get,
'''
lamda = (Pb*5.9*10**-2*d**2)/(Pr*4.4*10**-3*d**2)
alpha = Pr/(5.9*10**-2*d**2*lamda)
#Results
print "Operating wavelength =",round(lamda,2),"um"
print "Attenuation =",round(alpha,3),"dB/km"
'''Estimating the threshold optical powers for Brillowin and raman scattering'''
#Variable declaration
d = 6 #core diameter(um)
v = 0.8 #bandwidth(GHz)
lamda = 1.5 #wavelength(um)
alpha = 0.5 #attenuation(dB/Km)
#Calculations
Pb = 4.4*10**-3*d**2*lamda**2*alpha*v
Pr = 5.9*10**-3*d**2*lamda*alpha
#Results
print "Threshold optical power for SBS =",round((Pb/1E-3),2),"mW"
print "Threshold optical power for SRS =",round((Pr/1E-1),2),"W"
'''Determing the critical radius of curvature'''
import math
#Variable declaration
delta = 3./100 #relative refracive index difference
n1 = 1.5 #refractive index of core
lamda = 0.82*10**-6 #operating wavelength(m)
#Calculations
n2 = math.sqrt(n1**2-2*delta*n1**2)
Rc = (3*n1**2*lamda)/(4*math.pi*(n1**2-n2**2)**0.5)
#Results
print "The critical radius of curvature is",round((Rc/1E-6),3),"um(Calculation mistake in textbook)"
'''Calculating the crtitical radius of curvature'''
#Variable declaration
delta = 3./100 #relative refracive index difference
n1 = 2.0 #refractive index of core
lamda = 1.55*10**-6 #operating wavelength(m)
d = 8 #core diameter(um)
#Calculations
a = d/2
n2 = math.sqrt(n1**2-2*delta*n1**2)
lamda_c = (2*math.pi*a*10**-6*n1*((2*delta)**0.5))/2.405
Rc = (20*lamda*(2.748-0.996*(lamda/lamda_c))**-3)/((n1**2-n2**2)**0.5)
#Result
print "The critical radius of curvature is",round((Rc/1E-6),3),"um"
#note There is a calculation mistake in calculating 'n' in the textbook. Hence, the difference in results
'''Determine-
a)Maximum possible bandwidth
b)pulse dispersion per unit length
c)bandwidth length product'''
#Variable declaration
t = 0.1*10**-6 #pulse broadening(sec)
L = 10 #distance(km)
#Calculatons
#Part a
Bt = 1/(2*t)
#Part b
D = t/L
#Part c
Bl = Bt*L
#Results
print "a)Maximum possible bandwidth =",Bt/1E+6,"MHz"
print "b)Pulse dispersion per unit length =",D/1E-6,"us/km"
print "c)Bandwidth length product =",Bl/1E+6,"Mhz km"
'''Estimating the wavelength of transmitted light'''
#Variable declaration
Rc = 84*10**-6 #radius of curvature(m)
n1 = 1.46 #core refractive index
n2 = 1.45 #cladding refractive index
#Calculations
lamda = (Rc*4*math.pi*(n1**2-n2**2)**0.5)/(3*n1**2)
#Result
print "Wavelength =",round(lamda/1e-6,2),"um(Calculation mistake in textbook)"
'''Calculating the pulse broadening due to dispersion'''
#Variable declaration
lamda = 1.5 #wavelength(um)
M = 20 #dispersion parameter(ps n/m-km)
L = 30 #length of fiber(km)
s_l = 2 #spectral width(nm)
#Calculations
s_m = s_l*L*M
#Result
print "Pulse broadening =",s_m,"ps"
'''Calculating material dispersion parameter and rms pulse broadening'''
#Variable declaration
lamda = 850 #wavelength(nm)
s_l = 20 #spectral width(nm)
Dh = 0.025 #material dispersion
c = 2.998*10**5 #speed of light(km/s)
L = 1 #length of fiber(km)
#Calculations
M = Dh/(c*lamda)
s_m = s_l*L*M
#Results
print "Material dispersion parameter =",round((M/1E-12),2),"ps n/m km^-1"
print "RMS pulse broadening =",round((s_m/1E-9),2),"ns/km"
'''Determing the pulse boradening per km'''
#Variable declaration
lamda = 0.9 #wavelength(um)
s_l = 45 #spectral width(um)
Dh = 4*10**-2 #material dispersion
c = 3*10**8 #speed of light(m/s)
L = 1 #length of fiber(km)
#Calculations
s_m = (s_l*L*Dh*lamda)/c
#Result
print "Pulse boradening =",s_m/1E-9,"ns/km"
'''Determing the pulse boradening per km'''
#Variable declaration
lamda = 0.85 #wavelength(nm)
M = 95 #material dispersion parameter(ps n/m km^-1)
#Calculations
s_l = 0.0012*lamda
s_m = s_l*L*M
#Result
print "Pulse boradening =",s_m,"ns/km"
'''Calculating the pulse broadening'''
#Variable declaration
NA = 0.275 #numerical aperture
n1 = 1.48 #core refractive index
L = 5 #length of fiber(km)
c = 3.*10**5 #speed of light(km/sec)
#Calculations
STs = (L*NA**2)/(2*n1*c)
#Result
print "Pulse broadening =",round((STs/1E-9),2),"ns(Calculation mistake in textbook)"
'''Calculate -
a)delay difference between modes
b)rms pulse broadening
c)maximum bit rate
d)bandwidth length product'''
from math import sqrt
#Variable declaration
L = 3*10**3 #length of link(m)
n1 = 1.5 #refractive index of core
D = 2./100 #refractive index difference
c = 2.998*10**8 #speed of light(m/s)
#Calculations
#Part a
Sts = (L*n1*D)/c
#Part b
s_s = (L*n1*D)/(2*sqrt(3)*c)
#Part c
Bt = 1/(2*Sts)
#Part d
Bl = Bt*L
#Results
print "Delay difference is",round(Sts/1E-9),"ns"
print "rms pulse broadening due to intermodal dispersion is",round((s_s/1E-9),1),"ns"
print "Maximum bit rate is",round((Bt/1E+6),1),"Mbits/sec"
print "Bandwidth length product is",round((Bl/1E+9),1),"MHz km"
'''Calculating the rms pulse broadening per km for multimode step index fiber and graded index fiber'''
#Variable declaration
s_s = 86.7 #pulse broadening(ns)
L = 6 #length of link(km)
n1 = 1.5 #core refractive index
delta = 1./100 #refractive index difference
c = 2.998*10**8 #speed of light(m/s)
#Calculations
s_m = s_s/L
L1 = 10**3
s_g = (L1*n1*delta**2)/(20*sqrt(3)*c)
#Result
print "The rms pulse broadening per km for multimode step index fiber is",s_m,"ns/km"
print "The rms pulse broadening per km for graded index fiber is",round((s_g/1E-12),2),"ps/km"
'''Estimating the total pulse broadening per km '''
#Variable declaration
NA = 0.4 #numerical aperture
n1 = 1.48 #core refractive index
n2 = 1.47 #cladding refractive index
M = 30 #material dispersion parameter(ps n/m /km)
s_l = 25 #spectral width(ns)
L = 1 #length of fiber(km)
delta = 1./100 #refractive index difference
#Calculations
s_m = M*L*s_l
s_s = (L1*n1*delta**2)/(20*sqrt(3)*c)
s_t = math.sqrt(s_m**2+s_s**2)
#Results
print "Total pulse broadening per km is",s_t,"ps"
'''Calculate-
a)total pulse broadening per km
b)bandwidth length product'''
import math
#Variable declaration
NA = 0.3 #numerical aperture
n1 = 1.45 #core refractive index
M = 250 #material dispersion parameter(ps n/m /km)
s_l = 50 #spectral width(ns)
L = 1 #length of fiber(km)
c = 2.998*10**8 #speed of light(m/s)
#Calculations
#Part a
s_m = (M*L*s_l)*10**-3 #ns/km
s_s = ((L*10**3*NA**2)/(4*math.sqrt(3)*n1*c*10**3))*10**12 #ns/km
s_t = math.sqrt(s_m**2+s_s**2)
#Part b
Bl = 0.2/s_t
#Results
print "a)total pulse broadening =",round(s_t,2),"ns/km" #Answer varies due to rounding-off errors
print "b)bandwidth length product =",round((Bl/1E-3),1),"Mhz km"
'''Finding the beat length'''
#Variable declaration
lamda = 1.32 #wavelength(um)
Lbc = 100 #length of fiber(km)
Sf = 1.5 #spectral width(nm)
#Calculations
Bf = lamda**2/(Lbc*Sf)
Lb = lamda/Bf
#Result
print "Beat length =",round(Lb,1),"km"
'''Calculate modal birefringence'''
#Variable declaration
lamda = 1.3*10**-6 #wavelength(um)
Lb1 = 0.5*10**-3 #length of fiber(mm)
Lb2 = 60 #(m)
#Calculations
#for beat length of 0.5mm,
Bf1 = lamda/Lb1
#for beat length of 60m,
Bf2 = lamda/Lb2
#Result
print "The modal birefringences are ",Bf1/1E-3, "*10^-3 and",round((Bf2/1E-8),2),"*10^-4"
'''Calculate the modal birefringence, coherence length and difference between propagation constants'''
#Variable declaration
lamda = 0.5*10**-6 #wavelength(um)
Lb = 5*10**-2 #length of fiber(km)
S_l = 1*10**-9 #spectral width(nm)
#Calculations
Bf = lamda/Lb
Lbc = lamda**2/(Bf*S_l)
B = (2*math.pi)/Lb #Bx-By
#Results
print "Modal birefringence =",Bf
print "Coherence length =",Lbc,"m"
print "Difference between propagation constants =",round(B,1)
'''Calculating the maximum bit rate'''
#Variable declaration
L = 10*10**3 #length of fiber(m)
St2 = 600*10**-12 #mode dispersion(s/km)
#Calculations
Bt = 0.9/(0.55*St2*L)
#Result
print "Maximum bit rate =",round(Bt/1E+3),"Kbps"