def bpm_p(r1, dn1, r2=None, dn2=None, lam=.5, n0=1.): if dn2 is None: dn2 = dn1 if r2 is None: r2 = r1 r1 = .5 * r2 Nz, Nx = 256, 256 dx = .1 dn0 = zeros((Nz, Nx, Nx), float32) add_coated_sphere(dn0, [Nz / 2, Nx / 2, Nx / 2], r1=int(r1 / dx), r2=int(r2 / dx), dn1=dn1, dn2=dn2) u, _, p, g = bpm_3d((Nx, Nx, Nz), units=(dx, ) * 3, lam=lam, n0=n0, dn=dn0, return_scattering=True, return_g=True) return p, dn0, g
def test_focus(size, units, NA = .3, n0 = 1.): """ propagates a focused wave freely to the center """ Nx, Ny, Nz = size dx, dy , dz = .1, .1, .1 lam = .5 _, u_debye, _, _ = psf(size, units, n0= n0, lam=lam, NA=NA, return_field = True) u0 = u_debye[0] u0 = psf_u0(size[:2],units[:2], zfoc = .5*units[-1]*(size[-1]-1), n0 = n0, lam = lam, NA = NA) u = bpm_3d(size,units= units, lam = lam, n0 = n0, u0 = u0, absorbing_width = 0) return u, u_debye
def bpm_sca(r1, dn1, r2=None, dn2=None, lam=.5, n0=1., return_g=False): if dn2 is None: dn2 = dn1 if r2 is None: r2 = r1 r1 = .5 * r2 Nz, Nx = 256, 256 dx = .1 dn0 = zeros((Nz, Nx, Nx), float32) add_coated_sphere(dn0, [Nz / 2, Nx / 2, Nx / 2], r1=int(r1 / dx), r2=int(r2 / dx), dn1=dn1, dn2=dn2) res = bpm_3d((Nx, Nx, Nz), units=(dx, ) * 3, lam=lam, n0=n0, dn=dn0, return_scattering=True, return_g=return_g) if return_g: p, g = res[-2:] return p[-1] / r**2 / pi, g[-1] else: p = res[-1] return p[-1] / r**2 / pi
def test_focus(size, units, NA=.3, n0=1.): """ propagates a focused wave freely to the center """ Nx, Ny, Nz = size dx, dy, dz = .1, .1, .1 lam = .5 _, u_debye, _, _ = psf(size, units, n0=n0, lam=lam, NA=NA, return_field=True) u0 = u_debye[0] u0 = psf_u0(size[:2], units[:2], zfoc=.5 * units[-1] * (size[-1] - 1), n0=n0, lam=lam, NA=NA) u = bpm_3d(size, units=units, lam=lam, n0=n0, u0=u0, absorbing_width=0) return u, u_debye
def bpm_sca(r1,dn1,r2 = None,dn2 = None, lam= .5, n0 = 1., return_g = False): if dn2 is None: dn2 = dn1 if r2 is None: r2 = r1 r1 = .5*r2 Nz, Nx = 256,256 dx = .1 dn0 = zeros((Nz,Nx,Nx),float32) add_coated_sphere(dn0,[Nz/2,Nx/2,Nx/2],r1=int(r1/dx),r2 = int(r2/dx),dn1 = dn1,dn2 = dn2) res = bpm_3d((Nx,Nx,Nz),units= (dx,)*3, lam = lam, n0 = n0, dn = dn0, return_scattering = True, return_g = return_g ) if return_g: p, g = res[-2:] return p[-1]/r**2/pi, g[-1] else: p = res[-1] return p[-1]/r**2/pi
def test_plane(size = (256,)*3, n_x_comp = 0, n0 = 1., n = None): """ propagates a plane wave freely n_x_comp is the tilt in x """ Nx, Ny, Nz = size dx, dz = .05, 0.05 if n is None: n = n0 lam = .5 units = (dx,dx,dz) x = dx*np.arange(Nx) y = dx*np.arange(Ny) z = dz*np.arange(Nz) Z,Y,X = np.meshgrid(z,y,x,indexing="ij") k_x = 1.*n_x_comp/(dx*(Nx-1.)) k_y = k_x k_z = np.sqrt(1.*n**2/lam**2-k_x**2-k_y**2) print (k_x,k_z), np.sqrt(k_x**2+k_y**2+k_z**2) u_plane = np.exp(-2.j*np.pi*(k_z*Z+k_y*Y+k_x*X)) u = 0 dn = (n-n0)*np.ones_like(Z) u = bpm_3d((Nx,Ny,Nz),units= units, lam = lam, n0 = n0, dn = dn, n_volumes = 1, absorbing_width=0, u0 = u_plane[0,...]) print "L2 difference = %.4f"%np.mean(np.abs(u_plane-u)**2) #npt.assert_almost_equal(np.mean(np.abs(u_plane-u)**2),0,decimal = 2) return u, u_plane
def test_plane(size=(256, ) * 3, n_x_comp=0, n0=1., n=None): """ propagates a plane wave freely n_x_comp is the tilt in x """ Nx, Ny, Nz = size dx, dz = .05, 0.05 if n is None: n = n0 lam = .5 units = (dx, dx, dz) x = dx * np.arange(Nx) y = dx * np.arange(Ny) z = dz * np.arange(Nz) Z, Y, X = np.meshgrid(z, y, x, indexing="ij") k_x = 1. * n_x_comp / (dx * (Nx - 1.)) k_y = k_x k_z = np.sqrt(1. * n**2 / lam**2 - k_x**2 - k_y**2) print(k_x, k_z), np.sqrt(k_x**2 + k_y**2 + k_z**2) u_plane = np.exp(-2.j * np.pi * (k_z * Z + k_y * Y + k_x * X)) u = 0 dn = (n - n0) * np.ones_like(Z) u = bpm_3d((Nx, Ny, Nz), units=units, lam=lam, n0=n0, dn=dn, n_volumes=1, absorbing_width=0, u0=u_plane[0, ...]) print "L2 difference = %.4f" % np.mean(np.abs(u_plane - u)**2) #npt.assert_almost_equal(np.mean(np.abs(u_plane-u)**2),0,decimal = 2) return u, u_plane
def bpm_p(r1, dn1, r2=None, dn2=None, lam=0.5, n0=1.0): if dn2 is None: dn2 = dn1 if r2 is None: r2 = r1 r1 = 0.5 * r2 Nz, Nx = 256, 256 dx = 0.1 dn0 = zeros((Nz, Nx, Nx), float32) add_coated_sphere(dn0, [Nz / 2, Nx / 2, Nx / 2], r1=int(r1 / dx), r2=int(r2 / dx), dn1=dn1, dn2=dn2) u, _, p, g = bpm_3d((Nx, Nx, Nz), units=(dx,) * 3, lam=lam, n0=n0, dn=dn0, return_scattering=True, return_g=True) m = Bpm3d((Nx, Nx, Nz), units=(dx,) * 3, lam=lam, n0=n0) return u, p, dn0, g, m
def test_plane_complex(n_x_comp = 0, n0 = 1., n = None): """ propagates a plane wave freely n_x_comp is the tilt in x """ Nx, Nz = 128,128 dx, dz = .05, 0.05 if n is None: n = n0 n -= 0.01j lam = .5 units = (dx,dx,dz) x = dx*np.arange(Nx) y = dx*np.arange(Nx) z = dz*np.arange(Nz) Z,Y,X = np.meshgrid(z,y,x,indexing="ij") k_x = 1.*n_x_comp/(dx*(Nx-1.)) k_z = np.sqrt(1.*n**2/lam**2-k_x**2) print np.sqrt(k_x**2+k_z**2) u_plane = np.exp(-2.j*np.pi*(k_z*Z+k_x*X)) u = 0 dn = (n-n0)*np.ones_like(Z) print n,n0, np.mean(dn) u, dn = bpm_3d((Nx,Nx,Nz),units= units, lam = lam, n0 = n0, dn = dn, subsample = 2, u0 = u_plane[0,...]) print "difference", np.mean(np.abs(u_plane-u)**2) npt.assert_almost_equal(np.mean(np.abs(u_plane-u)**2),0,decimal = 2) return u_plane, u
def test_gaussian(size, units, NA = .4, tilt =0.): """ propagates a gaussian wave freely to the center """ Nx, Ny, Nz = size dx, dy, dz = units lam = .5 x = dx*(np.arange(-Nx/2, Nx/2)+.5) y = dy*(np.arange(-Ny/2, Ny/2)+.5) z = dz*(np.arange(-Nz/2, Nz/2)+.5) Z,Y,X = np.meshgrid(z,y,x,indexing="ij") u0 = gaussian_tilt(X,Y,Z,lam = lam, NA= NA, tilt = 0) u = bpm_3d(size, units=units, lam=lam, n0=n0, u0=u0[0], #use_fresnel_approx=True ) return u, u0
dx, dz = .05, 0.05 lam = .5 units = (dx, dx, dz) x = dx * np.arange(-Nx / 2, Nx / 2) y = dx * np.arange(-Ny / 2, Ny / 2) z = dz * np.arange(-Nz / 2, Nz / 2) Z, Y, X = np.meshgrid(z, y, x, indexing="ij") R = np.sqrt(X**2 + Y**2 + Z**2) dn = .05 * (R < 1.) u1 = bpm_3d((Nx, Ny, Nz), units=units, lam=lam, dn=dn, n_volumes=2, return_field=True) u1 = abs(u1)**2 u2 = bpm_3d((Nx, Ny, Nz), units=units, lam=lam, dn=dn, n_volumes=2, return_field=False) is_close = np.allclose(u1, u2) print is_close assert is_close
Nx, Ny, Nz = 256,256,512 dx, dz = .05, 0.05 lam = .5 units = (dx,dx,dz) x = dx*np.arange(-Nx/2,Nx/2) y = dx*np.arange(-Ny/2,Ny/2) z = dz*np.arange(-Nz/2,Nz/2) Z,Y,X = np.meshgrid(z,y,x,indexing="ij") R = np.sqrt(X**2+Y**2+Z**2) dn = .05*(R<1.) u1 = bpm_3d((Nx,Ny,Nz),units= units, lam = lam, dn = dn, n_volumes = 2, return_field=True) u1 = abs(u1)**2 u2 = bpm_3d((Nx,Ny,Nz),units= units, lam = lam, dn = dn, n_volumes = 2, return_field=False) is_close = np.allclose(u1,u2) print is_close assert is_close
""" check whether we conserve energy along z """ import bpm import numpy as np import pylab if __name__ == '__main__': N = 256 dn = .1*np.random.uniform(-1,1,(N,)*3) #plane wave through empty space and through dn u0 = bpm.bpm_3d((N,)*3,(.1,)*3) u = bpm.bpm_3d((N,)*3,(.1,)*3,dn = dn) pylab.ioff() pylab.plot(np.mean(np.abs(u0),(1,2))) pylab.plot(np.mean(np.abs(u),(1,2))) pylab.show()
y = dx * (np.arange(-Nx / 2, Nx / 2) + .5) z = dx * (np.arange(-Nz / 2, Nz / 2) + .5) Z, Y, X = np.meshgrid(z, y, x, indexing="ij") Z, X = np.cos(w) * Z - np.sin(w) * X, np.cos(w) * X + np.sin(w) * Z dn = dn0 * (Z > 0) u0 = psf_u0((Nx, Nx), units=(dx, ) * 2, zfoc=dx * (Nz - 1.) / 2., lam=lam, NA=NA) u = bpm_3d((Nx, Nx, Nz), units=(dx, ) * 3, lam=lam, dn=dn, u0=u0) u1 = np.abs(u[:Nz / 2, ...])[::-1, ...]**2 u2 = gputools.rotate(abs(u[Nz / 2:, ...])**2, center=(0, Nx / 2, Nx / 2), axis=(0., 1., 0), angle=phi(dn0, w), mode="linear") pylab.figure(1) pylab.clf() pylab.plot(u1[Nz / 10, Nx / 2, :]) pylab.plot(u2[Nz / 10, Nx / 2, :]) # return u, u0 #
from bpm import bpm_3d, focus_field_debye, psf_focus_u0 if __name__ == '__main__': # u1,u2 = test_plane(n_x_comp=1, n0 = 1.1) Nx = 256 Nz = 512 NA = .7 _,u1, _, _ = focus_field_debye((Nx, Nx, 2 * Nz), (.1,) * 3, lam = .5, NAs = [0., NA]) u1 = u1[Nz:] u2 = bpm_3d((Nx,Nx,Nz),(.1,)*3,u0 = u1[0]) u3 = bpm_3d((Nx,Nx,Nz),(.1,)*3,u0 = u1[0],dn = np.zeros((Nz,Nx,Nx)), absorbing_width=10) import pylab pylab.figure() pylab.clf() for i,u in enumerate([u1,u2,u3]): pylab.subplot(1,3,i+1) pylab.imshow(abs(u[...,Nx/2])**.6,cmap = "hot") pylab.axis("off") pylab.show()
w = .8 dn0 = 0.1 x = dx*(np.arange(-Nx/2, Nx/2)+.5) y = dx*(np.arange(-Nx/2, Nx/2)+.5) z = dx*(np.arange(-Nz/2, Nz/2)+.5) Z,Y,X = np.meshgrid(z,y,x,indexing="ij") Z,X = np.cos(w)*Z-np.sin(w)*X, np.cos(w)*X+np.sin(w)*Z dn = dn0*(Z>0) u0 = psf_u0((Nx,Nx), units = (dx,)*2,zfoc = dx*(Nz-1.)/2.,lam = lam, NA= NA) u = bpm_3d((Nx,Nx,Nz), units=(dx,)*3, lam=lam, dn =dn, u0=u0) u1 = np.abs(u[:Nz/2,...])[::-1,...]**2 u2 = gputools.rotate(abs(u[Nz/2:,...])**2, center = (0,Nx/2,Nx/2), axis = (0.,1.,0), angle = phi(dn0,w), mode = "linear") pylab.figure(1) pylab.clf() pylab.plot(u1[Nz/10,Nx/2,:]) pylab.plot(u2[Nz/10,Nx/2,:]) # return u, u0 # #
if __name__ == '__main__': # u1,u2 = test_plane(n_x_comp=1, n0 = 1.1) Nx = 256 Nz = 512 NA = .7 _, u1, _, _ = focus_field_debye((Nx, Nx, 2 * Nz), (.1, ) * 3, lam=.5, NAs=[0., NA]) u1 = u1[Nz:] u2 = bpm_3d((Nx, Nx, Nz), (.1, ) * 3, u0=u1[0]) u3 = bpm_3d((Nx, Nx, Nz), (.1, ) * 3, u0=u1[0], dn=np.zeros((Nz, Nx, Nx)), absorbing_width=10) import pylab pylab.figure() pylab.clf() for i, u in enumerate([u1, u2, u3]): pylab.subplot(1, 3, i + 1) pylab.imshow(abs(u[..., Nx / 2])**.6, cmap="hot") pylab.axis("off")