def path(self): interfaces = [ g.Points.from_xyz(np.random.rand(n), np.random.rand(n), np.random.rand(n), "A{}".format(i)) for (i, n) in enumerate(self.numpoints) ] path = ray.FermatPath((interfaces[0], 2.0, interfaces[1])) return path
def test_fermat_path(): s1 = ray.FermatPath(("frontwall", 1.234, "backwall")) s1_bis = ray.FermatPath(("frontwall", 1.234, "backwall")) assert s1 == s1_bis # check hashability s2 = ray.FermatPath(("backwall", 2.0, "points1")) s12 = ray.FermatPath(("frontwall", 1.234, "backwall", 2.0, "points1")) assert s12 == (s1 + s2) assert isinstance(s1 + s2, ray.FermatPath) s1_rev = ray.FermatPath(("backwall", 1.234, "frontwall")) assert s1.reverse() == s1_rev with pytest.raises(ValueError): s2 + s1 with pytest.raises(ValueError): ray.FermatPath((1, 2)) with pytest.raises(ValueError): ray.FermatPath((1, )) s3 = ray.FermatPath(("A", 1.0, "B", 2.0, "C", 3.0, "D")) head, tail = s3.split_head() assert head == ray.FermatPath(("A", 1.0, "B")) assert tail == ray.FermatPath(("B", 2.0, "C", 3.0, "D")) head, tail = s3.split_queue() assert head == ray.FermatPath(("A", 1.0, "B", 2.0, "C")) assert tail == ray.FermatPath(("C", 3.0, "D")) assert tuple(s1.points) == ("frontwall", "backwall") assert s1.velocities == (1.234, ) assert s3.velocities == (1.0, 2.0, 3.0) assert s1.num_points_sets == 2 assert s12.num_points_sets == 3
def test_fermat_solver(): """ Test Fermat solver by comparing it against a naive implementation. Check three and four interfaces. """ n = 5 m = 12 # number of points of interfaces B and C v1 = 99.0 v2 = 130.0 v3 = 99.0 v4 = 50.0 x_n = np.arange(n, dtype=float) x_m = np.linspace(-n, 2 * n, m) standoff = 11.1 z = 66.6 theta = np.deg2rad(30.0) interface_a = g.Points.from_xyz(x_n, standoff + x_n * np.sin(theta), np.full(n, z), "Interface A") interface_b = g.Points.from_xyz(x_m, np.zeros(m), np.full(m, z), "Interface B") interface_c = g.Points.from_xyz(x_m, -((x_m - 5)**2) - 10.0, np.full(m, z), "Interface C") path_1 = ray.FermatPath((interface_a, v1, interface_b, v2, interface_c)) path_2 = ray.FermatPath( (interface_a, v1, interface_b, v3, interface_c, v4, interface_b)) # The test function must return a dictionary of Rays: solver = ray.FermatSolver([path_1, path_2]) rays_dict = solver.solve() assert len(rays_dict) == 2 for path in [path_1, path_2]: # Check Rays.path attribute: assert path in rays_dict assert rays_dict[path].fermat_path is path assert rays_dict[path].indices.shape == (path.num_points_sets, n, m) assert rays_dict[path].times.shape == (n, m) # Check the first and last points of the rays: indices = rays_dict[path].indices assert np.all(indices[0, ...] == np.fromfunction(lambda i, j: i, (n, m))) assert np.all(indices[-1, ...] == np.fromfunction(lambda i, j: j, (n, m))) # Check rays for path_1: for i in range(n): for j in range(m): min_tof = np.inf best_index = 0 for k in range(m): tof = (g.norm2( interface_a.x[i] - interface_b.x[k], interface_a.y[i] - interface_b.y[k], interface_a.z[i] - interface_b.z[k], ) / v1 + g.norm2( interface_c.x[j] - interface_b.x[k], interface_c.y[j] - interface_b.y[k], interface_c.z[j] - interface_b.z[k], ) / v2) if tof < min_tof: min_tof = tof best_index = k assert np.isclose( min_tof, rays_dict[path_1].times[i, j] ), "Wrong time of flight for ray (start={}, end={}) in path 1 ".format( i, j) assert ( best_index == rays_dict[path_1].indices[1, i, j] ), "Wrong indices for ray (start={}, end={}) in path 1 ".format( i, j) # Check rays for path_2: for i in range(n): for j in range(m): min_tof = np.inf best_index_1 = 0 best_index_2 = 0 for k1 in range(m): for k2 in range(m): tof = (g.norm2( interface_a.x[i] - interface_b.x[k1], interface_a.y[i] - interface_b.y[k1], interface_a.z[i] - interface_b.z[k1], ) / v1 + g.norm2( interface_c.x[k2] - interface_b.x[k1], interface_c.y[k2] - interface_b.y[k1], interface_c.z[k2] - interface_b.z[k1], ) / v3 + g.norm2( interface_b.x[j] - interface_c.x[k2], interface_b.y[j] - interface_c.y[k2], interface_b.z[j] - interface_c.z[k2], ) / v4) if tof < min_tof: min_tof = tof best_index_1 = k1 best_index_2 = k2 assert np.isclose( min_tof, rays_dict[path_2].times[i, j] ), "Wrong time of flight for ray (start={}, end={}) in path 2 ".format( i, j) assert (best_index_1, best_index_2) == tuple( rays_dict[path_2].indices[1:3, i, j] ), "Wrong indices for ray (start={}, end={}) in path 2 ".format( i, j)