system.update() # draw the boundary plot = pv.Plotter() plot.add_axes() first_drawer = drawing.TriangleDrawer(plot, color="cyan", show_edges=True) second_drawer = drawing.TriangleDrawer(plot, color="cyan", show_edges=True) first_drawer.surface = lens.surfaces[0] first_drawer.draw() second_drawer.surface = lens.surfaces[1] second_drawer.draw() # build the engine the trace trace_engine = engine.OpticalEngine( 3, [operation.StandardReaction()], simple_ray_inheritance={"wavelength", "rank"}) trace_engine.optical_system = system trace_engine.validate_system() trace_engine.ray_trace(6) # define various optimization steps def process_gradient(engine, lr1, lr2, parameters, grad_clip, accumulator): """ Calculate and process the gradient. Returns the processed gradient and the error. """ with tf.GradientTape() as tape: engine.optical_system.update()
source = sources.PointSource(3, (0, 0, 0), (1, 0, 0), sphere, [drawing.YELLOW]) source.frozen = True print(f"Minimum phi generated: {np.degrees(tf.reduce_min(sphere.ranks[:,0]))}") print(f"Maximum phi generated: {np.degrees(tf.reduce_max(sphere.ranks[:,0]))}") # Set up the target angular_step_size = 5 plane_mesh = pv.Plane((1, 0, 0), (1, 0, 0), i_size=.1, j_size=.1).triangulate() target = boundaries.ManualTriangleBoundary(mesh=plane_mesh) # set up the system system = engine.OpticalSystem3D() system.sources = [source] system.targets = [target] eng = engine.OpticalEngine(3, [], optical_system=system) def rotate_target(): target.mesh.rotate_y(angular_step_size) if True: # plot the histogram fig, ax = plt.subplots(figsize=(12, 10)) # scroll through the angles ray_counts = [] angles = range(0, 95, angular_step_size) for angle in angles: system.update()
beam_points = distributions.RandomUniformBeam(-.09, .09, sample_count) source = sources.AngularSource(2, (0, -4.001), PI / 2, angles, beam_points, [drawing.YELLOW] * sample_count, rank_type=None, dense=False) # build the system system = eng.OpticalSystem2D() system.optical_segments = [boundary] system.sources = [source] system.materials = [{"n": materials.vacuum}, {"n": materials.acrylic}] trace_engine = eng.OpticalEngine(2, [op.StandardReaction()], compile_dead_rays=True, dead_ray_length=10, simple_ray_inheritance={"wavelength"}) trace_engine.optical_system = system system.update() trace_engine.validate_system() trace_engine.ray_trace(max_iterations=50) # set up drawers segment_drawer = drawing.SegmentDrawer(ax, color="cyan", draw_norm_arrows=True, norm_arrow_visibility=True) segment_drawer.segments = system.optical_segments segment_drawer.draw() ray_drawer = drawing.RayDrawer2D(ax)
# draw the boundary plot = pv.Plotter() plot.add_axes() first_drawer = drawing.TriangleDrawer(plot, color="cyan", show_edges=True) second_drawer = drawing.TriangleDrawer(plot, color="cyan", show_edges=True) first_drawer.surface = lens.surfaces[0] first_drawer.draw() second_drawer.surface = lens.surfaces[1] second_drawer.draw() # build the trace engine trace_engine = engine.OpticalEngine(3, [operation.StandardReaction()], simple_ray_inheritance={ "wavelength", "object_coords", "aperature_polar_ranks" }) trace_engine.optical_system = system trace_engine.validate_system() # define various optimization steps def process_gradient(engine, lr1, lr2, parameters, grad_clip, accumulator): """ Calculate and process the gradient. Returns the processed gradient and the error. """ with tf.GradientTape() as tape: engine.optical_system.update()
# build the source rays beam_points = distributions.StaticUniformBeam(-1.5, 1.5, 10) angles = distributions.StaticUniformAngularDistribution(0, 0, 1) source = sources.AngularSource((-1.0, 0.0), 0.0, angles, beam_points, drawing.RAINBOW_6) # build the system system = eng.OpticalSystem2D() system.optical_segments = [segments] system.sources = [source] system.target_segments = [target] system.materials = [{"n": materials.vacuum}, {"n": materials.acrylic}] trace_engine = eng.OpticalEngine( 2, [op.StandardReaction()], simple_ray_inheritance={"angle_ranks", "wavelength"}) trace_engine.optical_system = system system.update() trace_engine.validate_system() def test_trace(): system.update() trace_engine.ray_trace(2) trace_engine.clear_ray_history() # seems like the first trace takes much longer test_trace() # test various ammounts of rays for rays, segs in ((10, 11), (10000, 11), (10, 1001), (10000, 1001)):
if __name__ == "__main__": drawing.disable_figure_key_commands() # set up the figure and axes fig, ax = plt.subplots(1, 1, figsize=(9, 9)) # configure axes ax.set_aspect("equal") ax.set_xbound(-2, 2) ax.set_ybound(-2, 2) angles = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 5) source = sources.PointSource((0.0, 0.0), 0.0, angles, [drawing.YELLOW]) engine = eng.OpticalEngine(2, [op.OldestAncestor(), op.StandardReaction()]) system = eng.OpticalSystem2D() system.sources = [source] system.update() # set up drawer drawer = drawing.RayDrawer2D(ax) drawer.rays = source drawer.draw() # hand over to user fig.canvas.mpl_connect( "key_press_event", lambda event: on_key(event, drawer, source, system), ) plt.show()
system.update() first_drawer = drawing.TriangleDrawer(plot, color="cyan") second_drawer = drawing.TriangleDrawer(plot, color="green") third_drawer = drawing.TriangleDrawer(plot, color="brown") first_drawer.surface = surface1 first_drawer.draw() second_drawer.surface = surface2 second_drawer.draw() third_drawer.surface = surface3 third_drawer.draw() # test the trace trace_engine = engine.OpticalEngine(3, [operation.StandardReaction()], compile_dead_rays=True, dead_ray_length=10) trace_engine.optical_system = system trace_engine.validate_system() trace_engine.ray_trace(6) # draw the rays drawer = drawing.RayDrawer3D(plot) def source_rays(): drawer.rays = system.sources drawer.draw() def traced_rays():
import tfrt.drawing as drawing import tfrt.engine as eng import tfrt.operation as op PI = math.pi a1 = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 5) s1 = sources.PointSource((0.0, 0.0), 0.0, a1, [drawing.YELLOW]) a2 = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 1) s2 = sources.PointSource((0.0, 1.0), 0.0, a2, [drawing.YELLOW]) a3 = distributions.StaticUniformAngularDistribution(-PI/4.0, PI/4.0, 7) s3 = sources.PointSource((0.0, 2.0), 0.0, a3, [drawing.YELLOW]) engine = eng.OpticalEngine(2, [op.OldestAncestor()]) system = eng.OpticalSystem2D() engine.optical_system = system system.sources = [s1, s2, s3] engine.annotate() engine.update() print(f"system validation:") engine.validate_system() print("passed") print("source[oldest_ancestor] after annotation:") print(f"system: {system.sources['oldest_ancestor']}") print(f"s1: {s1['oldest_ancestor']}") print(f"s2: {s2['oldest_ancestor']}") print(f"s3: {s3['oldest_ancestor']}")