from numpy import * from time import time #%% Utility functions class stopwatch(object): def __init__(self,name='That'): self.name = name def __enter__(self): self.startTime = time() def __exit__(self,*args): print '{0} took {1:.2f}s'.format(self.name, time()-self.startTime) def s2z(Gamma): return Zc * (1+Gamma)/(1-Gamma) def z2s(Z): return (Z-Zc)/(Z+Zc) def alignAlong(vector,axisNumber): sizes = ones(dimensions) sizes[axisNumber] = -1 return vector.reshape(sizes) def sweepImpedance(startDimension): rho = alignAlong(linspace(0.001,1,10,endpoint=False),startDimension) argGamma = alignAlong(linspace(0,2*pi,16),startDimension+1) Gamma = rho * exp(1j*argGamma) return s2z(Gamma) def equals(title,equation1,equation2): errors = (abs(equation2 - equation1)) threshold = finfo(errors.dtype).eps * 1000 assert len(errors.shape) == dimensions print title print 'Shape of the swept space:',errors.shape print 'Numerical equality: {0} (Error between {1:.1e} and {2:.1e} < {3:.1e}, average absolute value {4:.1e})'.format( all(errors < threshold), errors.min(), errors.max(), threshold, abs(equation1).mean()) print '' #%% Definition of the problem space dimensions = 1+2+2+1+1 Zc = alignAlong(logspace(-2,2,5),0) Zne = sweepImpedance(1) Zfe = sweepImpedance(3) betaEll = alignAlong(linspace(0,4*pi,29,endpoint=False),5) phi = alignAlong(linspace(0,2*pi,20,endpoint=False),6) #%% Input of Paul's formulation paulD = cos(betaEll)*(Zne+Zfe) + 1j*sin(betaEll)*(Zc + (Zne*Zfe)/Zc) Vne_final_Paul = -Zne * 2.0 * (cos(betaEll) + 1j*sin(betaEll)*Zfe/Zc - exp(-1j*betaEll*cos(phi))) / paulD #%% Input of Op 't Land's formulation Vne = +1 * (exp(1j*(-1.0-cos(phi))*betaEll) -1.0) Vfe = -1 * (exp(1j*(+1.0-cos(phi))*betaEll) -1.0) * exp(-1j*betaEll) Zfe_ne = s2z(z2s(Zfe)*exp(-2j*betaEll)) Vne_final_OptLand = (Vne + z2s(Zfe)*Vfe*exp(-1j*betaEll)) * (1 + ((Zne-Zc)/Zc) * Zfe_ne/(Zne+Zfe_ne)) #%% Attempt to prove their equivalence with stopwatch('Solution using numpy sweep over {0} samples'.format(Vne_final_OptLand.size)): equals('Grazing incidence on bifilar line with (Zne,Zfe)', Vne_final_OptLand,Vne_final_Paul)