from seeing import *
propMethodsCartesian = Formulary.loadFromFile('Propagation100.frm')
approximations = ["Rayleigh-Sommerfeld", "Approximate Rayleigh-Sommerfeld", "Near Fresnel"]
propMethodsCartesian.display(approximations)
Rayleigh-Sommerfeld
Approximate Rayleigh-Sommerfeld
Near Fresnel
propMethodsCartesian.display(["Rayleigh-Sommerfeld Arg"])
Rayleigh-Sommerfeld Arg
waveLength = 10e-6
apertureRadius = waveLength*10
mCalc = Calculator(cp, cp.float64, 'intensity')
subdiv_points = 512
subsDictC = {'E_0': 1, 'z_1': 2*apertureRadius, 'x_0': 0, 'y_0': apertureRadius, 'lambda': waveLength, 'a':apertureRadius}
paramAndRanges = [( 'x_1', -apertureRadius, apertureRadius, subdiv_points, 'linear' ),
( 'y_1', -apertureRadius, apertureRadius, subdiv_points, 'linear' )]
propMethodsCartesian.plotFormula("Rayleigh-Sommerfeld Arg", subsDictC, paramAndRanges, mCalc)
subdiv_points = 256
ez0_c = subsParamsByName( propMethodsCartesian.getFormula('xyCircle'), {'a':apertureRadius} )
subsDictC = {'E_0': ez0_c, 'x_1': 0, 'y_1': 0, 'lambda': waveLength, 'a':apertureRadius}
paramAndRange = ( 'z_1', 0.01*apertureRadius, apertureRadius*100, subdiv_points, 'geometric' )
fig, ax = plt.subplots(figsize=(10,5))
plt.xscale('log')
plt.yscale('linear')
for appr in reversed(approximations):
eeq = propMethodsCartesian.getFormula(appr)
xplot2, zplot2 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDictC), [paramAndRange], None, 'trap')
plt.plot(xplot2[0]/apertureRadius, np.clip(zplot2, -4.5, 4.5), label=appr)
plt.legend(loc=(1.0, 0.78))
plt.show()
approximationsF = ["Rayleigh-Sommerfeld", "Far Fresnel"]
fig, ax = plt.subplots(figsize=(10,5))
plt.xscale('log')
plt.yscale('linear')
paramAndRange = ( 'z_1', apertureRadius, apertureRadius*10, 500, 'geometric' )
subsDictC = {'E_0': ez0_c, 'x_1': 0, 'y_1': 0, 'lambda': waveLength, 'a':apertureRadius}
for appr in reversed(approximationsF):
eeq = propMethodsCartesian.getFormula(appr)
xplot2, zplot2 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDictC), [paramAndRange], [(subdiv_points, 'linear'), (subdiv_points, 'linear')], 'rect')
plt.plot(xplot2[0]/apertureRadius, np.clip(zplot2, -4.5, 4.5), label=appr)
plt.legend(loc=(1.0, 0.78))
plt.show()
subdiv_points = 256
subsDict = {'E_0': ez0_c, 'y_1': 0, 'lambda': waveLength, 'a':apertureRadius}
paramsAndRanges = [ ('x_1', -1.5*apertureRadius, 1.5*apertureRadius, subdiv_points, 'linear'), ('z_1', waveLength, 15*apertureRadius, subdiv_points, 'linear')]
eeq = propMethodsCartesian.getFormula("Rayleigh-Sommerfeld")
xplot, fplot1 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDict), paramsAndRanges)
fig, ax = plt.subplots(figsize=(10,10))
ax.imshow( fplot1, cmap='hot' )
<matplotlib.image.AxesImage at 0x7f200bdd1e50>
subsDict = {'E_0': ez0_c, 'z_1': 2*apertureRadius, 'lambda': waveLength, 'a':apertureRadius}
paramsAndRanges = [ ('x_1', -2*apertureRadius, 2*apertureRadius, subdiv_points, 'linear'), ('y_1', -2*apertureRadius, 2*apertureRadius, subdiv_points, 'linear')]
eeq = propMethodsCartesian.getFormula("Rayleigh-Sommerfeld")
xplot, fplot1 = mCalc.IntegralEvalE(subsParamsByName(eeq, subsDict), paramsAndRanges)
fig, ax = plt.subplots(figsize=(10,10))
ax.imshow( np.log(fplot1+1), cmap='hot' )
<matplotlib.image.AxesImage at 0x7f200be0ffd0>
We are using the approach described so far and the SEEING library to develop simulations related to the development of the MAVIS instrument for the VLT telescope
Quite a lot, quite huge expressions!
from seeing import *
from zernike import *
mf = Formulary.loadFromFile('Mavis.frm')
mIt = Integrator(cp, cp.float64, 'none')
covIntegral = mf['ZernikeCovarianceI']
display(covIntegral)
r0_Value = 0.15 # [m]
L0_Value = 25.0 # [m]
TelescopeDiameter = 8.0 #[m]
DM_height = 10000 # [m]
rho_max = 120.0/206265.0
paramsDict = {'f_min':0.001, 'f_max':10, 'theta': np.pi/4.0, 'L_0': L0_Value,
'r_0': r0_Value, 'R_1': TelescopeDiameter/2.0, 'R_2': TelescopeDiameter/2.0, 'h': DM_height }
_integral1 = subsParamsByName( cov_expr_jk(covIntegral, 2, 3), paramsDict)
display(_integral1)
xplot1, zplot1 = mIt.IntegralEvalE(_integral1, [('rho', 0.0, rho_max, 1000, 'linear')], [(1024, 'linear')], method='rect')
fig, ax = plt.subplots(figsize=(10,5))
ax.plot(xplot1[0], zplot1)
plt.show()
import inspect
from seeing import *
x = sp.symbols('x')
fsympy = sp.sin(x) * sp.exp(x) / (1+sp.sqrt(sp.Abs(x)) )
display(fsympy)
fnumpy = sp.lambdify(x, fsympy, "numpy" )
print(inspect.getsource(fnumpy))
print(fnumpy.__globals__['sin'])
fcupy = sp.lambdify(x, fsympy, gpulib )
print(inspect.getsource(fcupy))
print(fcupy.__globals__['sin'])
def _lambdifygenerated(x): return (exp(x)*sin(x)/(sqrt(abs(x)) + 1)) <ufunc 'sin'> def _lambdifygenerated(x): return (exp(x)*sin(x)/(sqrt(abs(x)) + 1)) <ufunc 'cupy_sin'>
# somewhere we defined:
def besselj__n(n, z):
if n==0:
return cupyx.scipy.special.j0(z)
elif n==1:
return cupyx.scipy.special.j1(z)
elif n>=2:
return 2*(n-1)*besselj__n(n-1, z)/z - besselj__n(int(n)-2, z)
# and in gpulib dictionary we have: ... 'besselj: 'besselj__n' ...
from seeing import *
n = sp.symbols('n', integer=True)
fsympy = sp.besselj(n, x)
display(fsympy)
fnumpy = sp.lambdify((x, n), fsympy, "scipy" )
print(inspect.getsource(fnumpy))
print(fnumpy.__globals__['jv'])
fcupy = sp.lambdify((x,n), fsympy, gpulib )
print(inspect.getsource(fcupy))
print(fcupy.__globals__['besselj'])
def _lambdifygenerated(x, n): return (jv(n, x)) <ufunc 'jv'> def _lambdifygenerated(x, n): return (besselj(n, x)) <function besselj__n at 0x7f202eeefa70>
f = lambda a,b,c : a+b*c
print(f(5, 6, 2))
17
# also useful when you want to do have a function that returns a function:
def myfunc(n):
return lambda a:a*n
mydoubler = myfunc(2)
print(mydoubler(11))
22
# lets say we have this function returning a SymPy expression defined somewhere
def niceExpr():
x0, y0, ss = sp.symbols('x_0 y_0 sigma')
return sp.exp(-x0/ss) + sp.log(1+sp.Abs(y0)*ss)
# then in out main program we could have:
anExpr=niceExpr()
display(anExpr)
print(anExpr.free_symbols)
# cannot do this: ss = anExpr.free_symbols[1]
# we can do this:
ss = sp.symbols('sigma')
anExprSpec = anExpr.subs(ss, 0.5)
display(anExprSpec)
# ugly, what if sigma was complex?
{y_0, sigma, x_0}
import sympy as sp
#Define some symbols
x, y, a, b = sp.symbols("x y a b")
# Define a function
f1 = sp.exp(-a*x**2-b*y**2)
# see how nicely it is displayed
display(f1)
# Or define an integral
i1 = sp.Integral(f1, (x,-sp.oo,+sp.oo), (y,-sp.oo,+sp.oo))
display(i1)
# Specialize
i2 = i1.subs([(a, 1), (b,2)])
# Evaluate
display(i2.doit())
#You could also do real symbolic computations… :
fdev = sp.diff(f1, y)
display(fdev)
import numpy as np
import cupy as cp
npoints = int(1e6)
fcpu = lambda x : np.sum( np.sin(x) * np.exp(x) / (1+np.sqrt(np.abs(x)) ) ) / npoints
x_cpu = np.linspace(-1.0, 1.0, npoints)
result = fcpu(x_cpu)
print(type(result), result)
x_gpu = cp.linspace(-1.0, 1.0, npoints)
fgpu = lambda x : cp.sum( cp.sin(x) * cp.exp(x) / (1+cp.sqrt(cp.abs(x)) ) ) / npoints
result = fgpu(x_gpu)
print(type(result), result)
<class 'numpy.float64'> 0.17955110645016475 <class 'cupy.core.core.ndarray'> 0.1795511064501647
x_cpu = np.array([1,2,3])
print(type(x_cpu), x_cpu)
x_gpu = cp.asarray(x_cpu) # move the data to the current device.
print(type(x_gpu), x_gpu)
<class 'numpy.ndarray'> [1 2 3] <class 'cupy.core.core.ndarray'> [1 2 3]
x_gpu = cp.array([1, 2, 3]) # create an array in the current device
print(type(x_gpu), x_gpu)
x_cpu = cp.asnumpy(x_gpu) # move the array to the host.
print(type(x_cpu), x_cpu)
<class 'cupy.core.core.ndarray'> [1 2 3] <class 'numpy.ndarray'> [1 2 3]
import numpy as np
import cupy as cp
import math
def mySquaredSum(xx):
r = 0.0
for i in range(xx.shape[0]):
r += xx[i]*xx[i]*math.exp(x[i])*math.sin(x[i])**3
return r
npoints = int(1e5)
x_gpu = cp.linspace(0, 1, npoints)
x = np.linspace(0, 1, npoints)
print(mySquaredSum(x))
26717.919072457928
f = lambda x: np.sum(np.square(x)*np.exp(x)*np.sin(x)**3)
print(f(x))
26717.919072458004
fgpu = lambda x: cp.sum(cp.square(x)*cp.exp(x)*cp.sin(x)**3)
print(fgpu(x_gpu))
26717.919072458
%%timeit
y = mySquaredSum(x)
143 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
y = f(x)
9.28 ms ± 21.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
y = fgpu(x_gpu)
189 µs ± 109 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
paramAndRanges = [( 'x_1', -apertureRadius, apertureRadius, subdiv_points, 'linear' )]
subsDictC = {'E_0': 1, 'z_1': 2*apertureRadius, 'y_1':0.0, 'x_0': 0, 'y_0': apertureRadius, 'lambda': waveLength, 'a':apertureRadius}
propMethodsCartesian.plotFormula("Rayleigh-Sommerfeld Arg", subsDictC, paramAndRanges, mCalc)