def test_intersect(): rng = np.random.default_rng(5772) size = 10_000 for i in range(100): R = 1./rng.normal(0.0, 0.3) conic = rng.uniform(-2.0, 1.0) quad = batoid.Quadric(R, conic) quadCoordSys = batoid.CoordSys(origin=[0, 0, -1]) lim = min(0.7*abs(R)/np.sqrt(1+conic) if conic > -1 else 10, 10) x = rng.uniform(-lim, lim, size=size) y = rng.uniform(-lim, lim, size=size) z = np.full_like(x, -100.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, -100.0) rv2 = batoid.intersect(quad, rv.copy(), quadCoordSys) assert rv2.coordSys == quadCoordSys 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, quad.sag(x, y)-1, rtol=0, atol=1e-9) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(quadCoordSys) batoid.intersect(quad, rv2) assert rv2.coordSys == quadCoordSys 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, quad.sag(x, y)-1, rtol=0, atol=1e-9)
def test_intersect(): rng = np.random.default_rng(577) tanx = rng.uniform(-0.1, 0.1) tany = rng.uniform(-0.1, 0.1) size = 10_000 tiltedCoordSys = batoid.CoordSys(origin=[0, 0, -1]) tilted = batoid.Tilted(tanx, tany) x = rng.normal(0.0, 1.0, size=size) y = rng.normal(0.0, 1.0, size=size) z = np.full_like(x, -100.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, -100.0) rv2 = batoid.intersect(tilted, rv.copy(), tiltedCoordSys) assert rv2.coordSys == tiltedCoordSys 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, tilted.sag(x, y) - 1, rtol=0, atol=1e-12) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(tiltedCoordSys) batoid.intersect(tilted, rv2) assert rv2.coordSys == tiltedCoordSys 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, tilted.sag(x, y) - 1, rtol=0, atol=1e-12)
def test_intersect(): rng = np.random.default_rng(577) size = 10_000 planeCoordSys = batoid.CoordSys(origin=[0, 0, -1]) plane = batoid.Plane() x = rng.normal(0.0, 1.0, size=size) y = rng.normal(0.0, 1.0, size=size) z = np.full_like(x, -100.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, -100.0) rv2 = batoid.intersect(plane, rv.copy(), planeCoordSys) assert rv2.coordSys == planeCoordSys 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, -1, rtol=0, atol=1e-12) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(planeCoordSys) batoid.intersect(plane, rv2) assert rv2.coordSys == planeCoordSys 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, -1, rtol=0, atol=1e-12)
def test_fail(): zernike = batoid.Zernike([0,0,0,0,1]) # basically a paraboloid rv = batoid.RayVector(0, 0, -10, 0, 0.99, np.sqrt(1-0.99**2)) # Too shallow rv2 = batoid.intersect(zernike, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # This one passes rv = batoid.RayVector(0, 0, -1, 0, 0, -1) rv2 = batoid.intersect(zernike, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_fail(): sum = batoid.Sum([batoid.Plane(), batoid.Sphere(1.0)]) rv = batoid.RayVector(0, 10, 0, 0, 0, -1) # Too far to side rv2 = batoid.intersect(sum, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # This one passes rv = batoid.RayVector(0, 0, -1, 0, 0, +1) rv2 = batoid.intersect(sum, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_fail(): quad = batoid.Quadric(1.0, 0.0) rv = batoid.RayVector(0, 10, 0, 0, 0, -1) # Too far to side rv2 = batoid.intersect(quad, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # This one passes rv = batoid.RayVector(0, 0, -1, 0, 0, -1) rv2 = batoid.intersect(quad, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_fail(): asphere = batoid.Asphere(1.0, 0.0, []) rv = batoid.RayVector(0, 10, 0, 0, 0, -1) # Too far to the side rv2 = batoid.intersect(asphere, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # This one passes rv = batoid.RayVector(0, 0, -1, 0, 0, 1) rv2 = batoid.intersect(asphere, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_fail(): para = batoid.Paraboloid(1.0) rv = batoid.RayVector(0, 0, -10, 0, 0.99, np.sqrt(1 - 0.99**2)) # Too shallow rv2 = batoid.intersect(para, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # This one passes rv = batoid.RayVector(0, 0, -1, 0, 0, -1) rv2 = batoid.intersect(para, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_fail(): tilted = batoid.Tilted(0, 0) # Fail if vz == 0 rv = batoid.RayVector(0, 0, 0, 0, 1, 0) rv2 = batoid.intersect(tilted, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # Otherwise, succeeds rv = batoid.RayVector(0, 0, 0, 0, 0, -1) rv2 = batoid.intersect(tilted, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
def test_fail(): plane = batoid.Plane() # Only fail if vz == 0 rv = batoid.RayVector(0, 0, 0, 0, 1, 0) rv2 = batoid.intersect(plane, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([True])) # Otherwise, succeeds rv = batoid.RayVector(0, 0, 0, 0, 0, -1) rv2 = batoid.intersect(plane, rv.copy()) np.testing.assert_equal(rv2.failed, np.array([False]))
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_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_intersect(): rng = np.random.default_rng(57721) size = 10_000 for i in range(10): jmax = rng.integers(4, 100) coef = rng.normal(size=jmax+1)*1e-3 R_outer = rng.uniform(0.5, 5.0) R_inner = rng.uniform(0.0, 0.65*R_outer) zernike = batoid.Zernike(coef, R_outer=R_outer, R_inner=R_inner) lim = 0.7*R_outer zernikeCoordSys = batoid.CoordSys(origin=[0, 0, -1]) x = rng.uniform(-lim, lim, size=size) y = rng.uniform(-lim, lim, 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(zernike, rv.copy(), zernikeCoordSys) assert rv2.coordSys == zernikeCoordSys 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, zernike.sag(x, y)-1, rtol=0, atol=1e-12 ) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(zernikeCoordSys) batoid.intersect(zernike, rv2) assert rv2.coordSys == zernikeCoordSys 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, zernike.sag(x, y)-1, rtol=0, atol=1e-12 )
def test_intersect(): rng = np.random.default_rng(5772) size = 10_000 for _ in range(100): s1 = batoid.Sphere(1. / rng.normal(0., 0.2)) s2 = batoid.Paraboloid(rng.uniform(1, 3)) sum = batoid.Sum([s1, s2]) sumCoordSys = batoid.CoordSys(origin=[0, 0, -1]) x = rng.uniform(-1, 1, size=size) y = rng.uniform(-1, 1, 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(sum, rv.copy(), sumCoordSys) assert rv2.coordSys == sumCoordSys 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, sum.sag(x, y) - 1, rtol=0, atol=1e-12) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(sumCoordSys) batoid.intersect(sum, rv2) assert rv2.coordSys == sumCoordSys 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, sum.sag(x, y) - 1, rtol=0, atol=1e-12)
def test_intersect(): rng = np.random.default_rng(5772) size = 10_000 for i in range(100): R = 1. / rng.normal(0.0, 0.3) sphereCoordSys = batoid.CoordSys(origin=[0, 0, -1]) sphere = batoid.Sphere(R) x = rng.uniform(-0.3 * abs(R), 0.3 * abs(R), size=size) y = rng.uniform(-0.3 * abs(R), 0.3 * abs(R), size=size) z = np.full_like(x, -2 * abs(R)) # 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, -2 * abs(R)) rv2 = batoid.intersect(sphere, rv.copy(), sphereCoordSys) assert rv2.coordSys == sphereCoordSys 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, sphere.sag(x, y) - 1, rtol=0, atol=1e-9) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(sphereCoordSys) batoid.intersect(sphere, rv2) assert rv2.coordSys == sphereCoordSys 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, sphere.sag(x, y) - 1, rtol=0, atol=1e-9)
def test_intersect(): rng = np.random.default_rng(5772) size = 10_000 for i in range(100): zmax = np.inf while zmax > 3.0: R = 0.0 while abs(R) < 15.0: # Don't allow too small radius of curvature R = 1. / rng.normal(0.0, 0.3) # negative allowed conic = rng.uniform(-2.0, 1.0) ncoef = rng.choice(5) coefs = [rng.normal(0, 1e-8) for i in range(ncoef)] asphere = batoid.Asphere(R, conic, coefs) lim = min(0.7 * abs(R) / np.sqrt(1 + conic) if conic > -1 else 5, 5) zmax = abs(asphere.sag(lim, lim)) asphereCoordSys = batoid.CoordSys(origin=[0, 0, -1]) x = rng.uniform(-lim, lim, size=size) y = rng.uniform(-lim, lim, 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(asphere, rv.copy(), asphereCoordSys) assert rv2.coordSys == asphereCoordSys 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, asphere.sag(x, y) - 1, rtol=0, atol=1e-12) # Straight down works too. rv2 = rv.copy() rv2.vz[:] *= -1 rv2 = batoid.intersect(asphere, rv.copy(), asphereCoordSys) assert rv2.coordSys == asphereCoordSys 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, asphere.sag(x, y) - 1, rtol=0, atol=1e-12) # Check default intersect coordTransform rv2 = rv.copy().toCoordSys(asphereCoordSys) batoid.intersect(asphere, rv2) assert rv2.coordSys == asphereCoordSys 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, asphere.sag(x, y) - 1, rtol=0, atol=1e-12)