def test_ne(): xs1 = np.linspace(0, 1, 10) xs2 = np.linspace(0, 1, 11) ys1 = np.linspace(0.1, 1, 10) ys2 = np.linspace(0.1, 0.9, 11) def f1(x, y): return x + y def f2(x, y): return x - y zs1 = f1(*np.meshgrid(xs1, ys1)) zs2 = f2(*np.meshgrid(xs1, ys1)) objs = [ batoid.Bicubic(xs1, ys1, zs1), batoid.Bicubic(xs1, ys1, zs2), batoid.Bicubic(xs2, ys1, zs1), batoid.Bicubic(xs1, ys2, zs1), batoid.Bicubic(xs1, ys2, zs1, zs1, zs1, zs1), batoid.Bicubic(xs1, ys2, zs1, zs1, zs1, zs2), batoid.Bicubic(xs1, ys2, zs1, zs1, zs2, zs2), batoid.Bicubic(xs1, ys2, zs1, zs2, zs2, zs2) ] all_obj_diff(objs)
def test_sag(): np.random.seed(57) # Make some functions that should be trivially interpolatable. def f1(x, y): return x+y def f2(x, y): return x-y def f3(x, y): return x**2 def f4(x, y): return x*y + x - y + 2 def f5(x, y): return x**2 + y**2 + x*y - x - y - 3 def f6(x, y): return x**2*y - y**2*x + 3*x xs = np.linspace(0, 10, 1000) ys = np.linspace(0, 10, 1000) # Ought to be able to interpolate anywhere in [1,8]x[1,8] xtest = np.random.uniform(1, 8, size=1000) ytest = np.random.uniform(1, 8, size=1000) for f in [f1, f2, f3, f4, f5, f6]: zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) np.testing.assert_allclose( f(xtest, ytest), bc.sag(xtest, ytest), atol=0, rtol=1e-10 ) # sag returns nan outside of grid domain assert np.isnan(bc.sag(-1, -1))
def test_approximate_asphere(): np.random.seed(57721) xs = np.linspace(-1, 1, 1000) ys = np.linspace(-1, 1, 1000) xtest = np.random.uniform(-0.9, 0.9, size=1000) ytest = np.random.uniform(-0.9, 0.9, size=1000) for i in range(50): R = np.random.normal(20.0, 1.0) conic = np.random.uniform(-2.0, 1.0) ncoef = np.random.randint(0, 4) coefs = [np.random.normal(0, 1e-10) for i in range(ncoef)] asphere = batoid.Asphere(R, conic, coefs) zs = asphere.sag(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) np.testing.assert_allclose(asphere.sag(xtest, ytest), bc.sag(xtest, ytest), atol=1e-9, rtol=0.0) np.testing.assert_allclose(asphere.normal(xtest, ytest), bc.normal(xtest, ytest), atol=1e-8, rtol=0)
def test_sum_bicubic(): import os import yaml fn = os.path.join(batoid.datadir, "LSST", "LSST_i.yaml") config = yaml.safe_load(open(fn)) telescope = batoid.parse.parse_optic(config['opticalSystem']) xcos, ycos, zcos = batoid.utils.gnomonicToDirCos(np.deg2rad(0.8), np.deg2rad(0.8)) rays = batoid.circularGrid( telescope.dist, telescope.pupilSize / 2, telescope.pupilSize * telescope.pupilObscuration / 2, xcos, ycos, -zcos, 50, 50, 750e-9, 1.0, telescope.inMedium) out, _ = telescope.trace(rays) m2 = telescope.itemDict['LSST.M2'] xs = np.linspace(-m2.outRadius, m2.outRadius, 200) ys = xs zs = np.zeros((200, 200), dtype=float) bicubic = batoid.Bicubic(xs, ys, zs) m2.surface = batoid.Sum([m2.surface, bicubic]) out2, _ = telescope.trace(rays) # Don't expect exact equality, but should be very similar assert rays_allclose(out, out2, atol=1e-13)
def test_intersect(): np.random.seed(5772) def f(x, y): return x**2*y - y**2*x + 3*x - 2 + np.sin(y)*np.cos(x)**2 xs = np.linspace(0, 1, 1000) ys = np.linspace(0, 1, 1000) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) for _ in range(1000): # If we shoot rays straight up, then it's easy to predict the # intersection points. x = np.random.uniform(0.1, 0.9) y = np.random.uniform(0.1, 0.9) r0 = batoid.Ray(x, y, -10, 0, 0, 1, 0) r = bc.intersect(r0) np.testing.assert_allclose(r.r[0], x) np.testing.assert_allclose(r.r[1], y) np.testing.assert_allclose(r.r[2], bc.sag(x, y), rtol=0, atol=1e-9) # intersect should fail, but gracefully, outside of grid domain r0 = batoid.Ray(-1, -1, -10, 0, 0, 1, 0) assert bc.intersect(r0).failed
def test_approximate_zernike(): np.random.seed(577215) xs = np.linspace(-1, 1, 1000) ys = np.linspace(-1, 1, 1000) xtest = np.random.uniform(-0.9, 0.9, size=1000) ytest = np.random.uniform(-0.9, 0.9, size=1000) jmaxmax=22 for _ in range(10): jmax = np.random.randint(1, jmaxmax) coef = np.random.normal(size=jmax+1)*1e-5 R_inner = np.random.uniform(0.0, 0.65) zsurf = batoid.Zernike(coef, R_inner=R_inner) zs = zsurf.sag(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) np.testing.assert_allclose( zsurf.sag(xtest, ytest), bc.sag(xtest, ytest), atol=1e-10, rtol=0.0 ) np.testing.assert_allclose( zsurf.normal(xtest, ytest), bc.normal(xtest, ytest), atol=1e-7, rtol=0 )
def test_sum_bicubic(): telescope = batoid.Optic.fromYaml("LSST_i.yaml") xcos, ycos, zcos = batoid.utils.gnomonicToDirCos( np.deg2rad(0.8), np.deg2rad(0.8) ) rays = batoid.circularGrid( telescope.backDist, telescope.pupilSize/2, telescope.pupilSize*telescope.pupilObscuration/2, xcos, ycos, -zcos, 50, 50, 750e-9, 1.0, telescope.inMedium ) out = telescope.trace(rays) m2 = telescope['LSST.M2'] xs = np.linspace(-m2.outRadius, m2.outRadius, 200) ys = xs zs = np.zeros((200, 200), dtype=float) bicubic = batoid.Bicubic(xs, ys, zs) m2.surface = batoid.Sum([m2.surface, bicubic]) out2 = telescope.trace(rays) # Don't expect exact equality, but should be very similar assert rays_allclose(out, out2, atol=1e-13)
def test_asphere_approximation(): rng = np.random.default_rng(5772156) xs = np.linspace(-1, 1, 1000) ys = np.linspace(-1, 1, 1000) xtest = np.random.uniform(-0.9, 0.9, size=1000) ytest = np.random.uniform(-0.9, 0.9, size=1000) for i in range(10): R = rng.normal(20.0, 1.0) conic = rng.uniform(-2.0, 1.0) ncoef = rng.choice(4) coefs = [rng.normal(0, 1e-10) for i in range(ncoef)] asphere = batoid.Asphere(R, conic, coefs) zs = asphere.sag(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) np.testing.assert_allclose( asphere.sag(xtest, ytest), bc.sag(xtest, ytest), atol=1e-12, rtol=0.0 ) np.testing.assert_allclose( asphere.normal(xtest, ytest), bc.normal(xtest, ytest), atol=1e-9, rtol=0 )
def test_properties(): np.random.seed(5) for _ in range(100): xs = np.arange(10) ys = np.arange(10) zs = np.random.uniform(0, 1, size=(10, 10)) bc = batoid.Bicubic(xs, ys, zs) np.testing.assert_array_equal(xs, bc.xs) np.testing.assert_array_equal(ys, bc.ys) np.testing.assert_array_equal(zs, bc.zs) do_pickle(bc)
def test_refract(): rng = np.random.default_rng(577215) size = 10_000 for _ in range(10): def f(x, y): a = rng.uniform(size=5) return ( a[0]*x**2*y - a[1]*y**2*x + a[2]*3*x - a[3] + a[4]*np.sin(y)*np.cos(x)**2 ) xs = np.linspace(0, 1, 1000) ys = np.linspace(0, 1, 1000) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) m0 = batoid.ConstMedium(rng.normal(1.2, 0.01)) m1 = batoid.ConstMedium(rng.normal(1.3, 0.01)) x = rng.uniform(0.1, 0.9, size=size) y = rng.uniform(0.1, 0.9, size=size) z = np.full_like(x, -10.0) vx = rng.uniform(-1e-5, 1e-5, size=size) vy = rng.uniform(-1e-5, 1e-5, size=size) vz = np.sqrt(1-vx*vx-vy*vy)/m0.n rv = batoid.RayVector(x, y, z, vx, vy, vz) rvr = batoid.refract(bc, rv.copy(), m0, m1) rvr2 = bc.refract(rv.copy(), m0, m1) np.testing.assert_array_equal(rvr.failed, rvr2.failed) rays_allclose(rvr, rvr2) # print(f"{np.sum(rvr.failed)/len(rvr)*100:.2f}% failed") normal = bc.normal(rvr.x, rvr.y) # Test Snell's law s0 = np.sum(np.cross(normal, rv.v*m0.n)[~rvr.failed], axis=-1) s1 = np.sum(np.cross(normal, rvr.v*m1.n)[~rvr.failed], axis=-1) np.testing.assert_allclose( m0.n*s0, m1.n*s1, rtol=0, atol=1e-9 ) # Test that rv.v, rvr.v and normal are all in the same plane np.testing.assert_allclose( np.einsum( "ad,ad->a", np.cross(normal, rv.v), rv.v )[~rvr.failed], 0.0, rtol=0, atol=1e-12 )
def test_intersect(): rng = np.random.default_rng(5772) size = 10_000 for _ in range(10): def f(x, y): a = rng.uniform(size=5) return ( a[0]*x**2*y - a[1]*y**2*x + a[2]*3*x - a[3] + a[4]*np.sin(y)*np.cos(x)**2 ) xs = np.linspace(0, 1, 1000) ys = np.linspace(0, 1, 1000) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) bcCoordSys = batoid.CoordSys(origin=[0, 0, -1]) x = rng.uniform(0.1, 0.9, size=size) y = rng.uniform(0.1, 0.9, size=size) z = np.full_like(x, -10.0) # If we shoot rays straight up, then it's easy to predict the intersection vx = np.zeros_like(x) vy = np.zeros_like(x) vz = np.ones_like(x) rv = batoid.RayVector(x, y, z, vx, vy, vz) np.testing.assert_allclose(rv.z, -10.0) rv2 = batoid.intersect(bc, rv.copy(), bcCoordSys) assert rv2.coordSys == bcCoordSys rv2 = rv2.toCoordSys(batoid.CoordSys()) np.testing.assert_allclose(rv2.x, x) np.testing.assert_allclose(rv2.y, y) np.testing.assert_allclose( rv2.z, bc.sag(x, y)-1, rtol=0, atol=1e-12 ) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(bcCoordSys) batoid.intersect(bc, rv2) assert rv2.coordSys == bcCoordSys rv2 = rv2.toCoordSys(batoid.CoordSys()) np.testing.assert_allclose(rv2.x, x) np.testing.assert_allclose(rv2.y, y) np.testing.assert_allclose( rv2.z, bc.sag(x, y)-1, rtol=0, atol=1e-12 )
def test_reflect(): rng = np.random.default_rng(57721) size = 10_000 for _ in range(10): def f(x, y): a = rng.uniform(size=5) return ( a[0]*x**2*y - a[1]*y**2*x + a[2]*3*x - a[3] + a[4]*np.sin(y)*np.cos(x)**2 ) xs = np.linspace(0, 1, 1000) ys = np.linspace(0, 1, 1000) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) x = rng.uniform(0.1, 0.9, size=size) y = rng.uniform(0.1, 0.9, size=size) z = np.full_like(x, -10.0) vx = rng.uniform(-1e-5, 1e-5, size=size) vy = rng.uniform(-1e-5, 1e-5, size=size) vz = np.full_like(x, 1) rv = batoid.RayVector(x, y, z, vx, vy, vz) rvr = batoid.reflect(bc, rv.copy()) rvr2 = bc.reflect(rv.copy()) rays_allclose(rvr, rvr2) # print(f"{np.sum(rvr.failed)/len(rvr)*100:.2f}% failed") normal = bc.normal(rvr.x, rvr.y) # Test law of reflection a0 = np.einsum("ad,ad->a", normal, rv.v)[~rvr.failed] a1 = np.einsum("ad,ad->a", normal, -rvr.v)[~rvr.failed] np.testing.assert_allclose( a0, a1, rtol=0, atol=1e-12 ) # Test that rv.v, rvr.v and normal are all in the same plane np.testing.assert_allclose( np.einsum( "ad,ad->a", np.cross(normal, rv.v), rv.v )[~rvr.failed], 0.0, rtol=0, atol=1e-12 )
def update(self, deltax): """ Update the telescope based on the provided update to the optical state. Parameters ---------- deltax: aos.state.BendingState The change in the optical state to apply to the telescope. """ super().update(deltax) self.m1m3res.applyBending(deltax.m1m3modes) self.m2res.applyBending(deltax.m2modes) m1m3bicubic = batoid.Bicubic(self.m1m3res.x, self.m1m3res.y, self.m1m3res.surfResidual) m2bicubic = batoid.Bicubic(self.m2res.x, self.m2res.y, self.m2res.surfResidual) m1surf = self.optic.itemDict['LSST.M1'] if isinstance(m1surf, batoid.Sum): m1nominal = m1surf.surfaces[0] else: m1nominal = m1surf.surface self.optic.itemDict['LSST.M1'].surface = batoid.Sum([m1nominal, m1m3bicubic]) m3surf = self.optic.itemDict['LSST.M3'] if isinstance(m1surf, batoid.Sum): m3nominal = m3surf.surfaces[0] else: m3nominal = m3surf.surface self.optic.itemDict['LSST.M3'].surface = batoid.Sum([m3nominal, m1m3bicubic]) m2surf = self.optic.itemDict['LSST.M2'] if isinstance(m1surf, batoid.Sum): m2nominal = m2surf.surfaces[0] else: m2nominal = m2surf.surface self.optic.itemDict['LSST.M2'].surface = batoid.Sum([m2nominal, m2bicubic])
def test_fail(): xs = np.linspace(-1, 1, 10) ys = np.linspace(-1, 1, 10) def f(x, y): return x+y zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) rv = batoid.RayVector(0, 10, 0, 0, 0, -1) # Too far to side rv2 = batoid.intersect(bc, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # This one passes rv = batoid.RayVector(0, 0, 0, 0, 0, -1) rv2 = batoid.intersect(bc, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_LSST_M1_zernike(): """See how much a ~100 nm zernike perturbation to M1 affects wavefront zernikes """ np.random.seed(5772156) telescope = batoid.Optic.fromYaml("LSST_r.yaml") theta_x = np.deg2rad(1.185) theta_y = np.deg2rad(0.45) fiducialZernikes = batoid.psf.zernike(telescope, theta_x, theta_y, 750e-9) N = 256 xs = np.linspace(-8.36/2, 8.36/2, N) ys = np.linspace(-8.36/2, 8.36/2, N) jmax = 22 for _ in range(10): coef = np.random.normal(size=jmax+1)*1e-7/np.sqrt(jmax) # aim for ~100 nm rms R_inner = np.random.uniform(0.0, 0.65) zsurf = batoid.Zernike(coef, R_outer=8.36/2, R_inner=0.61*8.36/2) zs = zsurf.sag(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) # Add Zernike perturbation to M1 zTelescope = batoid.Optic.fromYaml("LSST_r.yaml") zPerturbedM1 = batoid.Sum([ zTelescope['LSST.M1'].surface, zsurf ]) zTelescope['LSST.M1'].surface = zPerturbedM1 zZernikes = batoid.psf.zernike(zTelescope, theta_x, theta_y, 750e-9) # Repeat with bicubic perturbation bcTelescope = batoid.Optic.fromYaml("LSST_r.yaml") bcPerturbedM1 = batoid.Sum([ bcTelescope['LSST.M1'].surface, bc ]) bcTelescope['LSST.M1'].surface = bcPerturbedM1 bcZernikes = batoid.psf.zernike(bcTelescope, theta_x, theta_y, 750e-9) np.testing.assert_allclose(zZernikes, bcZernikes, rtol=0, atol=1e-3)
def parallel_trace_timing(nside=1024, nthread=None, minChunk=None): if nthread is not None: print("setting to nthread to {}".format(nthread)) batoid._batoid.setNThread(nthread) print("Using {} threads".format(batoid._batoid.getNThread())) if minChunk is not None: print("setting to minChunk to {}".format(minChunk)) batoid._batoid.setMinChunk(minChunk) print("Using minChunk of {}".format(batoid._batoid.getMinChunk())) # 0.3, 0.3 should be in bounds for current wide-field telescopes theta_x = np.deg2rad(0.3) theta_y = np.deg2rad(0.3) dirCos = np.array([theta_x, theta_y, -1.0]) dirCos = batoid.utils.normalized(dirCos) rays = batoid.circularGrid( 20, 4.2, 0.5, dirCos[0], dirCos[1], dirCos[2], nside, nside, 700e-9, 1.0, batoid.ConstMedium(1.0) ) nrays = len(rays) print("Tracing {} rays.".format(nrays)) print() if args.lsst: fn = os.path.join(batoid.datadir, "LSST", "LSST_r.yaml") pm = 'LSST.M1' else: fn = os.path.join(batoid.datadir, "HSC", "HSC.yaml") pm = 'SubaruHSC.PM' config = yaml.load(open(fn)) telescope = batoid.parse.parse_optic(config['opticalSystem']) # Optionally perturb the primary mirror using Zernike polynomial if args.perturbZ != 0: orig = telescope.itemDict[pm].surface coefs = np.random.normal(size=args.perturbZ+1)*1e-6 # micron perturbations perturbation = batoid.Zernike(coefs, R_outer=telescope.pupilSize) telescope.itemDict[pm].surface = batoid.Sum([orig, perturbation]) # Optionally perturb primary mirror using bicubic spline if args.perturbBC != 0: orig = telescope.itemDict[pm].surface xs = np.linspace(-5, 5, 100) ys = np.linspace(-5, 5, 100) def f(x, y): return args.perturbBC*(np.cos(x) + np.sin(y)) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) telescope.itemDict[pm].surface = batoid.Sum([orig, bc]) print("Immutable trace") t0 = time.time() rays_out, _ = telescope.trace(rays) t1 = time.time() print("{} rays per second".format(int(nrays/(t1-t0)))) print() print("Trace in place") t0 = time.time() telescope.traceInPlace(rays) t1 = time.time() print("{} rays per second".format(int(nrays/(t1-t0)))) assert rays == rays_out if args.plot: import matplotlib.pyplot as plt rays.trimVignettedInPlace() x = rays.x y = rays.y x -= np.mean(x) y -= np.mean(y) x *= 1e6 y *= 1e6 plt.scatter(x, y, s=1, alpha=0.01) plt.xlim(np.std(x)*np.r_[-3,3]) plt.ylim(np.std(y)*np.r_[-3,3]) plt.xlabel("x (microns)") plt.ylabel("y (microns)") plt.show()
def parallel_trace_timing(nside=1024, nthread=None, minChunk=None): if nthread is not None: print("setting nthread to {}".format(nthread)) batoid._batoid.setNThread(nthread) print("Using {} threads".format(batoid._batoid.getNThread())) if minChunk is not None: print("setting minChunk to {}".format(minChunk)) batoid._batoid.setMinChunk(minChunk) print("Using minChunk of {}".format(batoid._batoid.getMinChunk())) # 0.3, 0.3 should be in bounds for current wide-field telescopes dirCos = batoid.utils.gnomicToDirCos(np.deg2rad(0.3), np.deg2rad(0.3)) if args.lsst: fn = os.path.join(batoid.datadir, "LSST", "LSST_i.yaml") pm = 'LSST.M1' elif args.decam: fn = os.path.join(batoid.datadir, "DECam", "DECam.yaml") pm = 'BlancoDECam.PM' else: fn = os.path.join(batoid.datadir, "HSC", "HSC.yaml") pm = 'SubaruHSC.PM' config = yaml.load(open(fn)) telescope = batoid.parse.parse_optic(config['opticalSystem']) rays = batoid.circularGrid( telescope.dist, 0.5 * telescope.pupilSize, 0.5 * telescope.pupilObscuration * telescope.pupilSize, dirCos[0], dirCos[1], -dirCos[2], nside, nside, 750e-9, 1.0, telescope.inMedium) nrays = len(rays) print("Tracing {} rays.".format(nrays)) print() # Optionally perturb the primary mirror using Zernike polynomial if args.perturbZ != 0: orig = telescope.itemDict[pm].surface coefs = np.random.normal(size=args.perturbZ + 1) * 1e-6 # micron perturbations perturbation = batoid.Zernike(coefs, R_outer=telescope.pupilSize) telescope.itemDict[pm].surface = batoid.Sum([orig, perturbation]) # Optionally perturb primary mirror using bicubic spline if args.perturbBC != 0: orig = telescope.itemDict[pm].surface rad = telescope.pupilSize / 2 * 1.1 xs = np.linspace(-rad, rad, 100) ys = np.linspace(-rad, rad, 100) def f(x, y): return args.perturbBC * (np.cos(x) + np.sin(y)) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) telescope.itemDict[pm].surface = batoid.Sum([orig, bc]) if args.immutable: print("Immutable trace") t0 = time.time() for _ in range(args.nrepeat): rays_in = batoid.RayVector(rays) rays_out, _ = telescope.trace(rays_in) t1 = time.time() print("{} rays per second".format(int(nrays * args.nrepeat / (t1 - t0)))) print() else: print("Trace in place") t0 = time.time() for _ in range(args.nrepeat): rays_out = batoid.RayVector(rays) telescope.traceInPlace(rays_out) t1 = time.time() print("{} rays per second".format(int(nrays * args.nrepeat / (t1 - t0)))) if args.plot: import matplotlib.pyplot as plt rays_out.trimVignettedInPlace() x = rays_out.x y = rays_out.y x -= np.mean(x) y -= np.mean(y) x *= 1e6 y *= 1e6 plt.scatter(x, y, s=1, alpha=0.01) plt.xlim(np.std(x) * np.r_[-5, 5]) plt.ylim(np.std(y) * np.r_[-5, 5]) plt.xlabel("x (microns)") plt.ylabel("y (microns)") plt.show()
def parallel_trace_timing(args): print("Using nrad of {:_d}".format(args.nrad)) if args.lsst: print("Tracing through LSST optics") telescope = batoid.Optic.fromYaml("LSST_r.yaml") pm = 'M1' elif args.decam: print("Tracing through DECam optics") telescope = batoid.Optic.fromYaml("DECam.yaml") pm = 'PM' else: print("Tracing through HSC optics") telescope = batoid.Optic.fromYaml("HSC.yaml") pm = 'PM' building = [] for _ in range(args.nrepeat): t0 = time.time() rays = batoid.RayVector.asPolar(optic=telescope, wavelength=620e-9, theta_x=np.deg2rad(0.3), theta_y=np.deg2rad(0.3), inner=0.5 * telescope.pupilSize * telescope.pupilObscuration, nrad=args.nrad, naz=int(2 * np.pi * args.nrad)) t1 = time.time() building.append(t1 - t0) building = np.array(building) nrays = len(rays) print("Tracing {:_d} rays.".format(nrays)) print(f"Minimum CPU RAM: {2*nrays*74/1024**3:.2f} GB") print(f"Minimum GPU RAM: {nrays*74/1024**3:.2f} GB") print() print() if args.nrepeat > 1: print("Generating: {:_} +/- {:_} rays per second".format( int(np.mean(nrays / building)), int(np.std(nrays / building) / np.sqrt(args.nrepeat)))) else: print("Generating: {:_} rays per second".format( int(nrays / building[0]))) # Optionally perturb the primary mirror using Zernike polynomial if args.perturbZ != 0: orig = telescope[pm].surface coefs = np.random.normal(size=args.perturbZ + 1) * 1e-6 # micron perturbations perturbation = batoid.Zernike(coefs, R_outer=telescope.pupilSize) telescope[pm].surface = batoid.Sum([orig, perturbation]) # Optionally perturb primary mirror using bicubic spline if args.perturbBC != 0: orig = telescope[pm].surface rad = telescope.pupilSize / 2 * 1.1 xs = np.linspace(-rad, rad, 100) ys = np.linspace(-rad, rad, 100) def f(x, y): return args.perturbBC * (np.cos(x) + np.sin(y)) zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) telescope[pm].surface = batoid.Sum([orig, bc]) copying = [] tracing = [] overall = [] for _ in range(args.nrepeat): t1 = time.time() rays_out = rays.copy() t2 = time.time() telescope.trace(rays_out) rays_out.r # force copy back to host if doing gpu rays_out.v rays_out.t rays_out.flux rays_out.vignetted rays_out.failed t3 = time.time() copying.append(t2 - t1) tracing.append(t3 - t2) overall.append(t3 - t1) copying = np.array(copying) tracing = np.array(tracing) overall = np.array(overall) if args.nrepeat > 1: print() print("copying: {:_} +/- {:_} rays per second".format( int(np.mean(nrays / copying)), int(np.std(nrays / copying) / np.sqrt(args.nrepeat)))) print() print("tracing: {:_} +/- {:_} rays per second".format( int(np.mean(nrays / tracing)), int(np.std(nrays / tracing) / np.sqrt(args.nrepeat)))) print() print("overall") print("-------") print("{:_} +/- {:_} rays per second".format( int(np.mean(nrays / overall)), int(np.std(nrays / overall) / np.sqrt(args.nrepeat)))) print() else: print() print("copying: {:_} rays per second".format(int(nrays / copying))) print() print("tracing: {:_} rays per second".format(int(nrays / tracing))) print() print("overall") print("-------") print("{:_} rays per second".format(int(nrays / overall))) print() if args.plot or args.show: import matplotlib.pyplot as plt w = ~rays_out.vignetted x = rays_out.x[w] y = rays_out.y[w] x -= np.mean(x) y -= np.mean(y) x *= 1e6 y *= 1e6 plt.scatter(x, y, s=1, alpha=0.01) plt.xlim(np.std(x) * np.r_[-5, 5]) plt.ylim(np.std(y) * np.r_[-5, 5]) plt.xlabel("x (microns)") plt.ylabel("y (microns)") plt.savefig("parallel_trace_timing.png") if args.show: plt.show()
def test_normal(): np.random.seed(577) # Work out some normals that are trivially interpolatable def f1(x, y): return x + y def df1dx(x, y): return np.ones_like(x) def df1dy(x, y): return np.ones_like(x) def d2f1dxdy(x, y): return np.zeros_like(x) def f2(x, y): return x - y def df2dx(x, y): return np.ones_like(x) def df2dy(x, y): return -np.ones_like(x) def d2f2dxdy(x, y): return np.zeros_like(x) def f3(x, y): return x**2 def df3dx(x, y): return 2 * x def df3dy(x, y): return np.zeros_like(x) def d2f3dxdy(x, y): return np.zeros_like(x) def f4(x, y): return x**2 * y + 2 * y def df4dx(x, y): return 2 * x * y def df4dy(x, y): return x**2 + 2 def d2f4dxdy(x, y): return 2 * x def f5(x, y): return x**2 * y - y**2 * x + 3 * x - 2 def df5dx(x, y): return 2 * x * y - y**2 + 3 def df5dy(x, y): return x**2 - 2 * y * x def d2f5dxdy(x, y): return 2 * x - 2 * y xs = np.linspace(0, 10, 1000) ys = np.linspace(0, 10, 1000) xtest = np.random.uniform(1, 8, size=1000) ytest = np.random.uniform(1, 8, size=1000) for f, dfdx, dfdy in zip([f1, f2, f3, f4, f5], [df1dx, df2dx, df3dx, df4dx, df5dx], [df1dy, df2dy, df3dy, df4dy, df5dy]): zs = f(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs) bcn = bc.normal(xtest, ytest) arr = np.vstack( [-dfdx(xtest, ytest), -dfdy(xtest, ytest), np.ones(len(xtest))]).T arr /= np.sqrt(np.sum(arr**2, axis=1))[:, None] np.testing.assert_allclose(bcn, arr, atol=1e-12, rtol=0) # Ought to be able to interpolate cubics if asserting derivatives def f6(x, y): return x**3 * y - y**3 * x + 3 * x - 2 def df6dx(x, y): return 3 * x**2 * y - y**3 + 3 def df6dy(x, y): return x**3 - 3 * y**2 * x def d2f6dxdy(x, y): return 3 * x**2 - 3 * y**2 for f, dfdx, dfdy, d2fdxdy in zip( [f1, f2, f3, f4, f5, f6], [df1dx, df2dx, df3dx, df4dx, df5dx, df6dx], [df1dy, df2dy, df3dy, df4dy, df5dy, df6dy], [d2f1dxdy, d2f2dxdy, d2f3dxdy, d2f4dxdy, d2f5dxdy, d2f6dxdy]): zs = f(*np.meshgrid(xs, ys)) dzdxs = dfdx(*np.meshgrid(xs, ys)) dzdys = dfdy(*np.meshgrid(xs, ys)) d2zdxdys = d2fdxdy(*np.meshgrid(xs, ys)) bc = batoid.Bicubic(xs, ys, zs, dzdxs=dzdxs, dzdys=dzdys, d2zdxdys=d2zdxdys) bcn = bc.normal(xtest, ytest) arr = np.vstack( [-dfdx(xtest, ytest), -dfdy(xtest, ytest), np.ones(len(xtest))]).T arr /= np.sqrt(np.sum(arr**2, axis=1))[:, None] np.testing.assert_allclose(bcn, arr, atol=1e-12, rtol=0)