Exemplo n.º 1
0
 def setUp(self):
     """
     Prepare an assembly with two subassemblies: one assembly representing
     a spherical lens behind a flat screen, and one asssembly representing a
     perfect mirror.
     The mirror will be placed at the two subassemblies' focus, so a paraxial
     ray will come back on the other side of the optical axis.
     
     Reference:
     In [1], the lensmaker equation
     """
     # focal length = 1, thickness = 1/6
     R = 1./6.
     back_surf = Surface(HemisphereGM(R), opt.RefractiveHomogenous(1., 1.5),
         location=N.r_[0., 0., -R/2.])
     front_surf = Surface(HemisphereGM(R), opt.RefractiveHomogenous(1., 1.5),
         location=N.r_[0., 0., R/2.], rotation=rotx(N.pi/2.)[:3,:3])
     front_lens = AssembledObject(surfs=[back_surf, front_surf])
     
     back_surf = Surface(FlatGeometryManager(), opt.RefractiveHomogenous(1., 1.5),
         location=N.r_[0., 0., -0.01])
     front_surf = Surface(FlatGeometryManager(), opt.RefractiveHomogenous(1., 1.5),
         location=N.r_[0., 0., 0.01])
     glass_screen = AssembledObject(surfs=[back_surf, front_surf],
         transform=translate(0., 0., 0.5))
     
     lens_assembly = Assembly(objects=[glass_screen, front_lens])
     lens_assembly.set_transform(translate(0., 0., 1.))
     full_assembly = Assembly(objects=[rect_one_sided_mirror(1., 1., 0.)],
         subassemblies = [lens_assembly])
     
     self.engine = TracerEngine(full_assembly)
Exemplo n.º 2
0
class TestObjectBuilding2(unittest.TestCase):
    """Tests an object composed of two flat surfaces"""
    flat_only = True
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(flat_surface.FlatGeometryManager(),
            optics_callables.RefractiveHomogenous(1., 1.5),
            location=N.array([0,0,-1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(),
            optics_callables.RefractiveHomogenous(1.5, 1.),
            location=N.array([0,0,1.]))
        
        self.object = AssembledObject(surfs=[surface1, surface2])
        self.assembly.add_object(self.object)
        
        x = 1/(math.sqrt(2))
        dir = N.c_[[0,-x,x]]
        position = N.c_[[0,1,-2.]]
        self._bund = RayBundle(position, dir, energy=N.r_[1.], ref_index=N.r_[1.])

    def test_refraction1(self):
        """Tests the refractive functions after a single intersection"""
        self.engine = TracerEngine(self.assembly)
        ans =  self.engine.ray_tracer(self._bund,1,.05)
        params = N.arctan(ans[1][1]/ans[1][2])
        correct_params = N.r_[0.785398163, -.4908826]
        N.testing.assert_array_almost_equal(params, correct_params)

    def test_refraction2(self):
        """Tests the refractive functions after two intersections"""
        self.engine = TracerEngine(self.assembly)
        ans = self.engine.ray_tracer(self._bund,2,.05)
        params = N.arctan(ans[1][1]/ans[1][2])
        correct_params = N.r_[-0.7853981]
        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 3
0
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, -1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, 1.]))

        self.object1 = AssembledObject()
        self.object1.add_surface(surface1)
        self.object1.add_surface(surface2)

        boundary = BoundarySphere(location=N.r_[0, 0., 3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary),
                           optics_callables.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)

        self.transform = generate_transform(N.r_[1, 0., 0], 0.,
                                            N.c_[[0., 0, 2]])
        self.assembly.add_object(self.object1)
        self.assembly.add_object(self.object2, self.transform)

        x = 1. / (math.sqrt(2))
        dir = N.c_[[0, 1., 0.], [0, x, x], [0, 0, 1.]]
        position = N.c_[[0, 0, 2.], [0, 0, 2.], [0, 0., 2.]]
        self._bund = RayBundle(position,
                               dir,
                               energy=N.ones(3),
                               ref_index=N.ones(3))
Exemplo n.º 4
0
class TestTraceProtocol1(unittest.TestCase):
    """ 
    Tests intersect_ray and the bundle driver with a single flat surface, not rotated, with 
    a single interation 
    """
    def setUp(self):
        dir = N.array([[1,1,-1],[-1,1,-1],[-1,-1,-1],[1,-1,-1]]).T/math.sqrt(3)
        position = N.c_[[0,0,1],[1,-1,1],[1,1,1],[-1,1,1]]
        self._bund = RayBundle(position, dir, energy=N.ones(4))
        
        self.assembly = Assembly()
        object = AssembledObject()
        object.add_surface(Surface(FlatGeometryManager(), opt.perfect_mirror))
        self.assembly.add_object(object)
        self.engine = TracerEngine(self.assembly)
        
    def test_intersect_ray1(self):
        surfaces = self.assembly.get_surfaces()
        objects = self.assembly.get_objects()
        surfs_per_obj = [len(obj.get_surfaces()) for obj in objects]
        surf_ownership = N.repeat(N.arange(len(objects)), surfs_per_obj)
        ray_ownership = -1*N.ones(self._bund.get_num_rays())
        surfs_relevancy = N.ones((len(surfaces), self._bund.get_num_rays()), dtype=N.bool)
        
        params = self.engine.intersect_ray(self._bund, surfaces, objects, \
            surf_ownership, ray_ownership, surfs_relevancy)[0]
        self.failUnless(params.all())

    def test_ray_tracer(self):
        """Ray tracer after one iteration returns what the surface would have"""
        params = self.engine.ray_tracer(self._bund,1,.05)[0]
        correct_pts = N.zeros((3,4))
        correct_pts[:2,0] = 1
        
        N.testing.assert_array_almost_equal(params, correct_pts)
Exemplo n.º 5
0
    def setUp(self):
        surface1 = Surface(HemisphereGM(2.),
                           opt.perfect_mirror,
                           rotation=general_axis_rotation(
                               N.r_[1, 0, 0], N.pi / 2.))
        surface2 = Surface(HemisphereGM(2.),
                           opt.perfect_mirror,
                           location=N.array([0, -2, 0]),
                           rotation=general_axis_rotation(
                               N.r_[1, 0, 0], -N.pi / 2.))

        self._bund = RayBundle()
        self._bund.set_directions(N.c_[[0, 1, 0]])
        self._bund.set_vertices(N.c_[[0, -1, 0]])
        self._bund.set_energy(N.r_[[1]])
        self._bund.set_ref_index(N.r_[[1]])

        assembly = Assembly()
        object1 = AssembledObject()
        object2 = AssembledObject()
        object1.add_surface(surface1)
        object2.add_surface(surface2)
        assembly.add_object(object1)
        assembly.add_object(object2)

        self.engine = TracerEngine(assembly)
Exemplo n.º 6
0
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(FlatGeometryManager(),
                           opt.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, -1.]))
        surface2 = Surface(FlatGeometryManager(),
                           opt.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, 1.]))
        object1 = AssembledObject(surfs=[surface1, surface2])

        boundary = BoundarySphere(location=N.r_[0, 0., 3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), opt.perfect_mirror)
        object2 = AssembledObject(surfs=[surface3],
                                  transform=translate(0., 0., 2.))

        self.assembly = Assembly(objects=[object1, object2])

        x = 1. / (math.sqrt(2))
        dir = N.c_[[0, 1., 0.], [0, x, x], [0, 0, 1.]]
        position = N.c_[[0, 0, 2.], [0, 0, 2.], [0, 0., 2.]]
        self._bund = RayBundle(position,
                               dir,
                               ref_index=N.ones(3),
                               energy=N.ones(3))

        self.engine = TracerEngine(self.assembly)
Exemplo n.º 7
0
class TestAssemblyBuilding3(unittest.TestCase):
    """Tests an assembly composed of objects that are transformed rel. the assembly"""
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, -1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, 1.]))

        self.object1 = AssembledObject()
        self.object1.add_surface(surface1)
        self.object1.add_surface(surface2)

        boundary = BoundarySphere(location=N.r_[0, 0., 3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary),
                           optics_callables.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)

        self.transform = generate_transform(N.r_[1, 0., 0], 0.,
                                            N.c_[[0., 0, 2]])
        self.assembly.add_object(self.object1)
        self.assembly.add_object(self.object2, self.transform)

        x = 1. / (math.sqrt(2))
        dir = N.c_[[0, 1., 0.], [0, x, x], [0, 0, 1.]]
        position = N.c_[[0, 0, 2.], [0, 0, 2.], [0, 0., 2.]]
        self._bund = RayBundle(position,
                               dir,
                               energy=N.ones(3),
                               ref_index=N.ones(3))

    def test_assembly1(self):
        """Tests the assembly after one iteration"""
        self.engine = TracerEngine(self.assembly)
        ans = self.engine.ray_tracer(self._bund, 1, .05)
        params = N.arctan(ans[1][1] / ans[1][2])
        correct_params = N.r_[0.7853981, 0]

        N.testing.assert_array_almost_equal(params, correct_params)

    def test_assembly2(self):
        """Tests the assembly after two iterations"""
        self.engine = TracerEngine(self.assembly)
        params = self.engine.ray_tracer(self._bund, 2, .05)[0]
        correct_params = N.c_[[0, -1, 1], [0, -1, 1], [0, 0, 1]]
        N.testing.assert_array_almost_equal(params, correct_params)

    def test_assembly3(self):
        """Tests the assembly after three iterations"""
        self.engine = TracerEngine(self.assembly)
        params = self.engine.ray_tracer(self._bund, 3, .05)[0]
        correct_params = N.c_[[0, -2.069044, -1], [0, 0, -1]]

        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 8
0
    def setUp(self):
        dir = N.array([[1, 1, -1], [-1, 1, -1], [-1, -1, -1], [1, -1, -1]
                       ]).T / math.sqrt(3)
        position = N.c_[[0, 0, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]]
        self._bund = RayBundle(position, dir, energy=N.ones(4))

        self.assembly = Assembly()
        object = AssembledObject()
        object.add_surface(Surface(FlatGeometryManager(), opt.perfect_mirror))
        self.assembly.add_object(object)
        self.engine = TracerEngine(self.assembly)
Exemplo n.º 9
0
 def setUp(self):
     self.eighth_circle_trans = generate_transform(N.r_[1., 0, 0], N.pi/4, 
         N.c_[[0., 1, 0]])
     
     self.surf = Surface(flat_surface.FlatGeometryManager(), \
         optics_callables.perfect_mirror)
     self.obj = AssembledObject(surfs=[self.surf])
     self.sub_assembly = Assembly()
     self.sub_assembly.add_object(self.obj, self.eighth_circle_trans)
     self.assembly = Assembly()
     self.assembly.add_assembly(self.sub_assembly, self.eighth_circle_trans)
Exemplo n.º 10
0
    def setUp(self):
        self.eighth_circle_trans = generate_transform(N.r_[1., 0, 0], N.pi / 4,
                                                      N.c_[[0., 1, 0]])

        self.surf = Surface(flat_surface.FlatGeometryManager(), \
            optics_callables.perfect_mirror)
        self.obj = AssembledObject(surfs=[self.surf])
        self.sub_assembly = Assembly()
        self.sub_assembly.add_object(self.obj, self.eighth_circle_trans)
        self.assembly = Assembly()
        self.assembly.add_assembly(self.sub_assembly, self.eighth_circle_trans)
Exemplo n.º 11
0
class TestAssemblyBuilding3(unittest.TestCase):
    """Tests an assembly composed of objects that are transformed rel. the assembly"""
    def setUp(self):  
        self.assembly = Assembly()

        surface1 = Surface(flat_surface.FlatGeometryManager(), 
            optics_callables.RefractiveHomogenous(1., 1.5),
            location=N.array([0,0,-1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(), 
            optics_callables.RefractiveHomogenous(1., 1.5),
            location=N.array([0,0,1.]))
        
        self.object1 = AssembledObject() 
        self.object1.add_surface(surface1)
        self.object1.add_surface(surface2)
        
        boundary = BoundarySphere(location=N.r_[0,0.,3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), optics_callables.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)
    
        self.transform = generate_transform(N.r_[1,0.,0],0.,N.c_[[0.,0,2]])
        self.assembly.add_object(self.object1)
        self.assembly.add_object(self.object2, self.transform)
        
        x = 1./(math.sqrt(2))
        dir = N.c_[[0,1.,0.],[0,x,x],[0,0,1.]]
        position = N.c_[[0,0,2.],[0,0,2.],[0,0.,2.]]
        self._bund = RayBundle(position, dir, energy=N.ones(3), ref_index=N.ones(3))
        
    def test_assembly1(self):
        """Tests the assembly after one iteration"""
        self.engine = TracerEngine(self.assembly)
        ans =  self.engine.ray_tracer(self._bund,1,.05)
        params = N.arctan(ans[1][1]/ans[1][2])
        correct_params = N.r_[0.7853981, 0]

        N.testing.assert_array_almost_equal(params, correct_params)

    def test_assembly2(self):
        """Tests the assembly after two iterations"""
        self.engine = TracerEngine(self.assembly)
        params = self.engine.ray_tracer(self._bund,2,.05)[0]
        correct_params = N.c_[[0,-1,1], [0,-1,1],[0,0,1]]
        N.testing.assert_array_almost_equal(params, correct_params)

    def test_assembly3(self):      
        """Tests the assembly after three iterations"""  
        self.engine = TracerEngine(self.assembly)
        params = self.engine.ray_tracer(self._bund, 3,.05)[0]
        correct_params = N.c_[[0,-2.069044,-1],[0,0,-1]]

        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 12
0
    def setUp(self):
        surface = Surface(HemisphereGM(1.), opt.perfect_mirror,
            rotation=general_axis_rotation(N.r_[1,0,0], N.pi))
        self._bund = RayBundle(energy=N.ones(3))
        self._bund.set_directions(N.c_[[0,1,0],[0,1,0],[0,-1,0]])
        self._bund.set_vertices(N.c_[[0,-2.,0.001],[0,0,0.001],[0,2,0.001]])

        assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surface)
        assembly.add_object(object)
        
        self.engine = TracerEngine(assembly)
Exemplo n.º 13
0
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(Paraboloid(), optics_callables.perfect_mirror)
        self.object = AssembledObject()
        self.object.add_surface(surface1)
        self.assembly.add_object(self.object)

        x = 1. / (math.sqrt(2))
        dir = N.c_[[0, 0, -1.], [0, x, -x]]
        position = N.c_[[0, 0, 1.], [0, 0, 1.]]
        self._bund = RayBundle(position,
                               dir,
                               energy=N.ones(2),
                               ref_index=N.ones(2))
Exemplo n.º 14
0
    def setUp(self):
        surface = Surface(HemisphereGM(1.),
                          opt.perfect_mirror,
                          rotation=general_axis_rotation(N.r_[1, 0, 0], N.pi))
        self._bund = RayBundle(energy=N.ones(3))
        self._bund.set_directions(N.c_[[0, 1, 0], [0, 1, 0], [0, -1, 0]])
        self._bund.set_vertices(N.c_[[0, -2., 0.001], [0, 0, 0.001],
                                     [0, 2, 0.001]])

        assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surface)
        assembly.add_object(object)

        self.engine = TracerEngine(assembly)
Exemplo n.º 15
0
class TestTree(unittest.TestCase):
    """Tests an assembly composed of objects"""
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(FlatGeometryManager(), opt.perfect_mirror)
        self.object1 = AssembledObject()
        self.object1.add_surface(surface1)

        boundary = BoundarySphere(location=N.r_[0, 0., 3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), opt.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)

        self.transform1 = generate_transform(N.r_[1., 0, 0], N.pi / 4,
                                             N.c_[[0, 0, -1.]])
        self.transform2 = translate(0., 0., 2.)
        self.assembly.add_object(self.object1, self.transform1)
        self.assembly.add_object(self.object2, self.transform2)

    def test_tree1(self):
        """Tests that the tracing tree works, with three rays"""
        x = 1. / (math.sqrt(2))
        dir = N.c_[[0, x, x], [0, -x, x], [0, 0, 1.]]
        position = N.c_[[0, 0, 2.], [0, 0, 2.], [0, 0., 2.]]
        bund = RayBundle(position, dir, energy=N.ones(3))

        self.engine = TracerEngine(self.assembly)

        self.engine.ray_tracer(bund, 3, .05)[0]
        params = self.engine.tree.ordered_parents()
        correct_params = [N.r_[0, 1, 2], N.r_[1, 2], N.r_[0]]
        N.testing.assert_equal(params, correct_params)

    def test_tree2(self):
        """Tests that the tracing tree works, with a new set of rays"""
        x = 1. / (math.sqrt(2))
        position = N.c_[[0, 0., -5.], [0, 0., 2.], [0, 2., -5.], [0, 0., 0],
                        [0, 0, 2.]]
        dir = N.c_[[0, 0, 1.], [0, x, -x], [0, 0, -1.], [0, 0, 1.], [0, -x, x]]
        bund = RayBundle(position, dir, energy=N.ones(5))

        self.engine = TracerEngine(self.assembly)
        self.engine.ray_tracer(bund, 3, .05)[0]

        params = self.engine.tree.ordered_parents()
        correct_params = [N.r_[0, 1, 3, 4], N.r_[2, 3, 1], N.r_[2, 1]]
        N.testing.assert_equal(params, correct_params)
Exemplo n.º 16
0
    def setUp(self):  
        self.assembly = Assembly()

        surface1 = Surface(flat_surface.FlatGeometryManager(), 
            optics_callables.RefractiveHomogenous(1., 1.5),
            location=N.array([0,0,-1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(), 
            optics_callables.RefractiveHomogenous(1., 1.5),
            location=N.array([0,0,1.]))
        
        self.object1 = AssembledObject() 
        self.object1.add_surface(surface1)
        self.object1.add_surface(surface2)
        
        boundary = BoundarySphere(location=N.r_[0,0.,3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), optics_callables.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)
    
        self.transform = generate_transform(N.r_[1,0.,0],0.,N.c_[[0.,0,2]])
        self.assembly.add_object(self.object1)
        self.assembly.add_object(self.object2, self.transform)
        
        x = 1./(math.sqrt(2))
        dir = N.c_[[0,1.,0.],[0,x,x],[0,0,1.]]
        position = N.c_[[0,0,2.],[0,0,2.],[0,0.,2.]]
        self._bund = RayBundle(position, dir, energy=N.ones(3), ref_index=N.ones(3))
Exemplo n.º 17
0
    def gen_plant(self,
                  width=6.1,
                  height=6.1,
                  absorptivity=0.04,
                  aim_height=60.,
                  sigma_xy=1e-3,
                  rec_w=11.,
                  rec_h=11.):

        self.pos[:,
                 1] = self.pos[:,
                               1] - 4.  # correction for the true position of the plate on the tower.
        self.width = width
        self.height = height
        self.absorptivity = absorptivity
        self.field = HeliostatField(self.pos, width, height, absorptivity,
                                    aim_height, sigma_xy)
        self.rec_w = rec_w
        self.rec_h = rec_h
        rec, recobj = one_sided_receiver(self.rec_w, self.rec_h)
        rec_trans = rotx(N.pi / -2)
        rec_trans[2, 3] = self.field._th

        # Evaluating just the receiver
        recobj.set_transform(rec_trans)

        self.plant = Assembly(objects=[recobj], subassemblies=[self.field])
Exemplo n.º 18
0
    def test_pyramid(self):
        """A simple right-pyramid triangular mesh"""
        # Face set:
        verts = np.vstack(
            (np.zeros(3), np.eye(3)))  # origin + unit along each axis
        faces = np.array([[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]])
        assembly = Assembly(
            objects=[TriangulatedSurface(verts, faces, perfect_mirror)])

        # Ray bundle:
        pos = np.c_[[1.5, 0.5, 0.5], [-0.5, 0.5, 0.5], [0.5, 1.5, 0.5],
                    [0.5, -0.5, 0.5], [0.5, 0.5, -0.5], [0.5, 0.5, 1.5]]
        direct = np.c_[[-1., 0., 0.], [1., 0., 0.], [0., -1., 0.],
                       [0., 1., 0.], [0., 0., 1.], [0., 0., -1.]]
        rayb = RayBundle(pos, direct, energy=np.ones(6))

        engine = TracerEngine(assembly)
        verts = engine.ray_tracer(rayb, 1, .05)[0]

        p = engine.tree[-1].get_parents()
        zrays = (p >= 4)
        np.testing.assert_array_equal(verts[:, zrays],
                                      np.tile(np.c_[[0.5, 0.5, 0.]], (1, 4)))
        yrays = (p == 2) | (p == 3
                            )  # Only 2 rays here. Edge degeneracy? maybe.
        np.testing.assert_array_equal(verts[:, yrays],
                                      np.tile(np.c_[[0.5, 0., 0.5]], (1, 4)))
        xrays = (p < 2)
        np.testing.assert_array_equal(verts[:, xrays],
                                      np.tile(np.c_[[0., 0.5, 0.5]], (1, 4)))
Exemplo n.º 19
0
    def test_tetrahedron(self):
        """Triangular mesh with oblique triangles"""
        # Face set:
        theta = np.arange(np.pi / 2., np.pi * 2, 2 * np.pi / 3)
        base_verts = np.vstack((np.cos(theta), np.sin(theta), np.ones(3))).T
        verts = np.vstack((np.zeros(3), base_verts))

        faces = np.array([[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]])
        fset = TriangulatedSurface(verts, faces, perfect_mirror)

        # Flat floor:
        floor = rect_one_sided_mirror(5., 5., 1.)
        floor.set_location(np.r_[0., 0., 1.])
        assembly = Assembly(objects=[fset, floor])

        # Ray bundle of 3 rays starting at equal angles around the tetrahedron:
        theta -= np.pi / 3.
        pos = np.vstack((np.cos(theta), np.sin(theta), np.ones(3) * 0.2)) * 0.2
        direct = np.vstack((np.zeros((2, 3)), np.ones(3)))
        rayb = RayBundle(pos, direct, energy=np.ones(6))

        # Check that the points on the floor describe an isosceles.
        engine = TracerEngine(assembly)
        engine.ray_tracer(rayb, 2, .05)[0]
        verts = engine.tree[-1].get_vertices()
        sizes = np.sqrt(np.sum((verts - np.roll(verts, 1, axis=1))**2, axis=0))

        self.assertAlmostEqual(sizes[0], sizes[1])
        self.assertAlmostEqual(sizes[2], sizes[1])
Exemplo n.º 20
0
class TestTraceProtocol3(unittest.TestCase):
    """
    Tests intersect_ray and the bundle driver with two rotated planes, with a single iteration
    """
    def setUp(self):
        self.x = 1 / (math.sqrt(2))
        dir = N.c_[[0, self.x, -self.x], [0, 1, 0]]
        position = N.c_[[0, 0, 1], [0, 0, 1]]
        self._bund = RayBundle(position, dir, energy=N.ones(2))

        rot1 = general_axis_rotation([1, 0, 0], N.pi / 4)
        rot2 = general_axis_rotation([1, 0, 0], N.pi / (-4))
        surf1 = Surface(FlatGeometryManager(),
                        opt.perfect_mirror,
                        rotation=rot1)
        surf2 = Surface(FlatGeometryManager(),
                        opt.perfect_mirror,
                        rotation=rot2)

        self.assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surf1)
        object.add_surface(surf2)
        self.assembly.add_object(object)

        self.engine = TracerEngine(self.assembly)

    def test_intersect_ray(self):
        surfaces = self.assembly.get_surfaces()
        objects = self.assembly.get_objects()
        surfs_per_obj = [len(obj.get_surfaces()) for obj in objects]
        surf_ownership = N.repeat(N.arange(len(objects)), surfs_per_obj)
        ray_ownership = -1 * N.ones(self._bund.get_num_rays())
        surfs_relevancy = N.ones((len(surfaces), self._bund.get_num_rays()),
                                 dtype=N.bool)

        params = self.engine.intersect_ray(self._bund, surfaces, objects, \
            surf_ownership, ray_ownership, surfs_relevancy)[0]
        correct_params = N.array([[True, True], [False, False]])

        N.testing.assert_array_almost_equal(params, correct_params)

    def test_ray_tracer1(self):
        params = self.engine.ray_tracer(self._bund, 1, .05)[0]
        correct_params = N.c_[[0, .5, .5], [0, 1, 1]]

        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 21
0
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(FlatGeometryManager(), opt.perfect_mirror)
        self.object1 = AssembledObject()
        self.object1.add_surface(surface1)

        boundary = BoundarySphere(location=N.r_[0, 0., 3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), opt.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)

        self.transform1 = generate_transform(N.r_[1., 0, 0], N.pi / 4,
                                             N.c_[[0, 0, -1.]])
        self.transform2 = translate(0., 0., 2.)
        self.assembly.add_object(self.object1, self.transform1)
        self.assembly.add_object(self.object2, self.transform2)
Exemplo n.º 22
0
    def setUp(self):

        self.x = 1/(math.sqrt(2))
        dir = N.c_[[0,-self.x,self.x],[0,0,-1]]
        position = N.c_ [[0,2,1],[0,2,1]]
        self._bund = RayBundle(position, dir, energy=N.ones(2))
        
        rot1 = general_axis_rotation([1,0,0],N.pi/4)
        surf1 = Surface(FlatGeometryManager(), opt.perfect_mirror, rotation=rot1)
        surf2 = Surface(FlatGeometryManager(), opt.perfect_mirror)
        assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surf1)
        object.add_surface(surf2)
        assembly.add_object(object)
        
        self.engine = TracerEngine(assembly)
Exemplo n.º 23
0
    def gen_plant(self):
        """Generates the entire plant"""
        # set heliostat field characteristics: 6.09m*6.09m, abs = 0.04, aim_location_xyz =60
        self.field = HeliostatGenerator(self.helio_w,
                                        self.helio_h,
                                        self.helio_abs,
                                        self.helio_sigmaxy,
                                        slope='normal',
                                        curved=True,
                                        pos=self.pos,
                                        foc=self.helio_focal)
        self.field(
            KnownField(self.heliostatcsv, self.pos,
                       N.array([self.helio_focal]).T))  #field(layout)
        heliostats = Assembly(objects=self.field._heliostats)
        aiming = SinglePointAiming(self.pos, self.recv_centre, False)
        if self.helio_tracking == 'TiltRoll':
            tracking = TiltRoll(solar_vector(self.azimuth, self.zenith), False)
        elif self.helio_tracking == 'AzEl':
            tracking = AzElTrackings(solar_vector(self.azimuth, self.zenith),
                                     False)
        tracking.aim_to_sun(-self.pos, aiming.aiming_points, False)
        tracking(self.field)

        self.plant = Assembly(objects=[self.recv_obj],
                              subassemblies=[heliostats])

        ##### Calculate Total Recv area #####
        areacount = 0.
        corners = self.recv_surf.mesh(
            0)  #corners is an array of all corners of the plate
        # BLC is bottom left corner "origin" of the histogram plot
        # BRC is the bottom right corner "x-axis" used for vector u
        # TLC is the top right corner "y-axis" used for vector v
        BLC = N.array([corners[0][1][1], corners[1][1][1], corners[2][1][1]])
        BRC = N.array([corners[0][0][1], corners[1][0][1], corners[2][0][1]])
        TLC = N.array([corners[0][1][0], corners[1][1][0], corners[2][1][0]])
        # Get vectors u and v in array form of array([x,y,z])
        u = BRC - BLC
        v = TLC - BLC
        # Get width(magnitude of u) and height(magnitude of v) in float form
        w = (sum(u**2))**0.5
        h = (sum(v**2))**0.5
        areacount += w * h
        self.recv_area = areacount
        print("Reciver area", self.recv_area)
Exemplo n.º 24
0
class TestTree(unittest.TestCase):
    """Tests an assembly composed of objects"""
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(FlatGeometryManager(), opt.perfect_mirror)
        self.object1 = AssembledObject()
        self.object1.add_surface(surface1)  
        
        boundary = BoundarySphere(location=N.r_[0,0.,3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), opt.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)

        self.transform1 = generate_transform(N.r_[1.,0,0], N.pi/4, N.c_[[0,0,-1.]])
        self.transform2 = translate(0., 0., 2.)
        self.assembly.add_object(self.object1, self.transform1)
        self.assembly.add_object(self.object2, self.transform2)
      
    def test_tree1(self):
        """Tests that the tracing tree works, with three rays"""
        x = 1./(math.sqrt(2))
        dir = N.c_[[0,x,x],[0,-x,x],[0,0,1.]]
        position = N.c_[[0,0,2.],[0,0,2.],[0,0.,2.]]
        bund = RayBundle(position, dir, energy=N.ones(3))

        self.engine = TracerEngine(self.assembly)

        self.engine.ray_tracer(bund,3,.05)[0]
        params = self.engine.tree.ordered_parents()
        correct_params = [N.r_[0,1,2],N.r_[1,2],N.r_[0]]
        N.testing.assert_equal(params, correct_params)

    def test_tree2(self):
        """Tests that the tracing tree works, with a new set of rays"""
        x = 1./(math.sqrt(2))
        position = N.c_[[0,0.,-5.],[0,0.,2.],[0,2.,-5.],[0,0.,0],[0,0,2.]]
        dir = N.c_[[0,0,1.],[0,x,-x],[0,0,-1.],[0,0,1.],[0,-x,x]]
        bund = RayBundle(position, dir, energy=N.ones(5))

        self.engine = TracerEngine(self.assembly)
        self.engine.ray_tracer(bund,3,.05)[0]
        
        params = self.engine.tree.ordered_parents()
        correct_params = [N.r_[0,1,3,4],N.r_[2,3,1],N.r_[2,1]]
        N.testing.assert_equal(params, correct_params)
Exemplo n.º 25
0
    def gen_plant(self):

        # set heliostat field characteristics: 6.09m*6.09m, abs = 0, aim_h = 61
        self.pos[:,
                 1] = self.pos[:,
                               1] - 4.  # correct6ion for the true position of the plate on the tower.
        self.field = HeliostatField(self.pos,
                                    6.09,
                                    6.09,
                                    absorptivity=0.04,
                                    aim_height=60,
                                    sigma_xy=1e-3,
                                    option=None)
        self.rec_w = 11
        self.rec_h = 11
        self.rec, recobj = one_sided_receiver(self.rec_w, self.rec_h)
        rec_trans = rotx(N.pi / -2)
        rec_trans[2, 3] = self.field._th

        #=================
        ground_rec = False
        #=================

        if ground_rec:
            # Evaluating missed rays in the field along with receiver
            radius = 1.10 * math.sqrt((self.x_dist / 2)**2 +
                                      (self.y_dist / 2)**2)
            self.ground_rec, ground_recobj = one_sided_receiver(
                3 * radius, 3 * radius)

            ground_rec_trans = rotz(0)
            ground_rec_trans[0, 3] = self.field_centre[0]
            ground_rec_trans[1, 3] = self.field_centre[1]

            recobj.set_transform(rec_trans)
            ground_recobj.set_transform(ground_rec_trans)
            self.plant = Assembly(objects=[recobj, ground_recobj],
                                  subassemblies=[self.field])
        else:
            # Evaluating just the receiver
            recobj.set_transform(rec_trans)

            self.plant = Assembly(objects=[recobj], subassemblies=[self.field])
Exemplo n.º 26
0
 def setUp(self):
     dir = N.array([[1,1,-1],[-1,1,-1],[-1,-1,-1],[1,-1,-1]]).T/math.sqrt(3)
     position = N.c_[[0,0,1],[1,-1,1],[1,1,1],[-1,1,1]]
     self._bund = RayBundle(position, dir, energy=N.ones(4))
     
     self.assembly = Assembly()
     object = AssembledObject()
     object.add_surface(Surface(FlatGeometryManager(), opt.perfect_mirror))
     self.assembly.add_object(object)
     self.engine = TracerEngine(self.assembly)
Exemplo n.º 27
0
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, -1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1.5, 1.),
                           location=N.array([0, 0, 1.]))

        self.object = AssembledObject(surfs=[surface1, surface2])
        self.assembly.add_object(self.object)

        x = 1 / (math.sqrt(2))
        dir = N.c_[[0, -x, x]]
        position = N.c_[[0, 1, -2.]]
        self._bund = RayBundle(position,
                               dir,
                               energy=N.r_[1.],
                               ref_index=N.r_[1.])
Exemplo n.º 28
0
    def setUp(self):

        self.x = 1 / (math.sqrt(2))
        dir = N.c_[[0, -self.x, self.x], [0, 0, -1]]
        position = N.c_[[0, 2, 1], [0, 2, 1]]
        self._bund = RayBundle(position, dir, energy=N.ones(2))

        rot1 = general_axis_rotation([1, 0, 0], N.pi / 4)
        surf1 = Surface(FlatGeometryManager(),
                        opt.perfect_mirror,
                        rotation=rot1)
        surf2 = Surface(FlatGeometryManager(), opt.perfect_mirror)
        assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surf1)
        object.add_surface(surf2)
        assembly.add_object(object)

        self.engine = TracerEngine(assembly)
Exemplo n.º 29
0
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(HemisphereGM(3.),
                           optics_callables.perfect_mirror,
                           location=N.array([0, 0, -1.]),
                           rotation=general_axis_rotation(N.r_[1, 0, 0], N.pi))
        surface2 = Surface(HemisphereGM(3.),
                           optics_callables.perfect_mirror,
                           location=N.array([0, 0, 1.]))

        self.object = AssembledObject()
        self.object.add_surface(surface1)
        self.object.add_surface(surface2)
        self.assembly.add_object(self.object)

        dir = N.c_[[0, 0, 1.], [0, 0, 1.]]
        position = N.c_[[0, 0, -3.], [0, 0, -1.]]

        self._bund = RayBundle(position, dir, energy=N.ones(2))
Exemplo n.º 30
0
    def test_cylinder_height(self):
        """The bounding cylinder exists for planoconvex lens"""
        f = self.lens.focal_length()
        rb = RayBundle(N.c_[[0., 0., -0.01]], N.c_[[1., 0., 0.]],
            energy=N.r_[1.], ref_index=N.r_[1.5])

        e = TracerEngine(Assembly([self.lens]))
        verts, dirs = e.ray_tracer(rb, 1, 1e-6)

        N.testing.assert_array_equal(verts, N.array([]).reshape(3,0))
Exemplo n.º 31
0
class TestTraceProtocol3(unittest.TestCase):
    """
    Tests intersect_ray and the bundle driver with two rotated planes, with a single iteration
    """
    def setUp(self):
        self.x = 1/(math.sqrt(2))
        dir = N.c_[[0,self.x,-self.x],[0,1,0]]
        position = N.c_[[0,0,1],[0,0,1]]
        self._bund = RayBundle(position, dir, energy=N.ones(2))

        rot1 = general_axis_rotation([1,0,0],N.pi/4)
        rot2 = general_axis_rotation([1,0,0],N.pi/(-4))
        surf1 = Surface(FlatGeometryManager(), opt.perfect_mirror, rotation=rot1)
        surf2 = Surface(FlatGeometryManager(), opt.perfect_mirror, rotation=rot2)
        
        self.assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surf1)
        object.add_surface(surf2)
        self.assembly.add_object(object)

        self.engine = TracerEngine(self.assembly)
        
    def test_intersect_ray(self):
        surfaces = self.assembly.get_surfaces()
        objects = self.assembly.get_objects()
        surfs_per_obj = [len(obj.get_surfaces()) for obj in objects]
        surf_ownership = N.repeat(N.arange(len(objects)), surfs_per_obj)
        ray_ownership = -1*N.ones(self._bund.get_num_rays())
        surfs_relevancy = N.ones((len(surfaces), self._bund.get_num_rays()), dtype=N.bool)
        
        params = self.engine.intersect_ray(self._bund, surfaces, objects, \
            surf_ownership, ray_ownership, surfs_relevancy)[0]
        correct_params = N.array([[True, True],[False, False]])

        N.testing.assert_array_almost_equal(params,correct_params)    

    def test_ray_tracer1(self):
        params = self.engine.ray_tracer(self._bund, 1,.05)[0]
        correct_params = N.c_[[0,.5,.5],[0,1,1]]

        N.testing.assert_array_almost_equal(params,correct_params)
Exemplo n.º 32
0
    def test_cylinder(self):
        """The bounding cylinder exists for biconcave lens"""
        f = self.lens.focal_length()
        rb = RayBundle(N.c_[[0., 0., 0.08]], N.c_[[1., 0., 0.]],
            energy=N.r_[1.], ref_index=N.r_[1.5])
        
        e = TracerEngine(Assembly([self.lens]))
        verts, dirs = e.ray_tracer(rb, 1, 1e-6)

        N.testing.assert_array_equal(verts, N.tile(N.c_[[0.5, 0., 0.08]], (1,2)))
        N.testing.assert_array_equal(dirs, N.c_[[-1., 0., 0.], [1., 0., 0.]])
Exemplo n.º 33
0
 def setUp(self):
     self.assembly = Assembly()
     surface1 = Surface(Paraboloid(), optics_callables.perfect_mirror)
     self.object = AssembledObject()
     self.object.add_surface(surface1)
     self.assembly.add_object(self.object)
    
     x = 1./(math.sqrt(2))  
     dir = N.c_[[0,0,-1.],[0,x,-x]]
     position = N.c_[[0,0,1.],[0,0,1.]]
     self._bund = RayBundle(position, dir, energy=N.ones(2), ref_index=N.ones(2))
Exemplo n.º 34
0
    def gen_plant(self):
        xy = radial_stagger(-N.pi/4, N.pi/4 + 0.0001, self.ang_res, 5, 20, self.radial_res)
        self.pos = N.hstack((xy, N.zeros((xy.shape[0], 1))))
        self.field = HeliostatField(self.pos, 0.5, 0.5, 0, 10)

        self.rec, recobj = one_sided_receiver(1., 1.)
        rec_trans = roty(N.pi/2)
        rec_trans[2,3] = 10
        recobj.set_transform(rec_trans)

        self.plant = Assembly(objects=[recobj], subassemblies=[self.field])
Exemplo n.º 35
0
class TestAssemblyBuilding4(unittest.TestCase):
    """Tests an assembly composed of objects"""
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(Paraboloid(), optics_callables.perfect_mirror)
        self.object = AssembledObject()
        self.object.add_surface(surface1)
        self.assembly.add_object(self.object)
       
        x = 1./(math.sqrt(2))  
        dir = N.c_[[0,0,-1.],[0,x,-x]]
        position = N.c_[[0,0,1.],[0,0,1.]]
        self._bund = RayBundle(position, dir, energy=N.ones(2), ref_index=N.ones(2))

    def test_paraboloid1(self):  
        """Tests a paraboloid"""
        
        self.engine = TracerEngine(self.assembly)
        params =  self.engine.ray_tracer(self._bund,1,.05)[0]
        correct_params = N.c_[[0,0,0],[0,0.618033989, 0.381966011]]
        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 36
0
    def test_intersect_ray2(self):
        rot = general_axis_rotation([1,0,0],N.pi/4)
        surface = Surface(FlatGeometryManager(), opt.perfect_mirror, rotation=rot)
        assembly = Assembly()
        object = AssembledObject()
        object.add_surface(surface)
        assembly.add_object(object)
        
        engine = TracerEngine(assembly)
        surfaces = assembly.get_surfaces()
        objects = assembly.get_objects()
        surfs_per_obj = [len(obj.get_surfaces()) for obj in objects]
        surf_ownership = N.repeat(N.arange(len(objects)), surfs_per_obj)
        ray_ownership = -1*N.ones(self._bund.get_num_rays())
        surfs_relevancy = N.ones((len(surfaces), self._bund.get_num_rays()), dtype=N.bool)
        
        params = engine.intersect_ray(self._bund, surfaces, objects, \
            surf_ownership, ray_ownership, surfs_relevancy)[0]
        correct_params = N.array([[False, True, False]])

        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 37
0
    def test_regular(self):
        """One-sided plate without rotation"""
        e = TracerEngine(Assembly(objects=[self.mirror]))
        e.ray_tracer(self.bund, 1, 0.05)
        outg = e.tree[-1]

        correct_verts = N.zeros((3, 2))
        correct_verts[0] = N.r_[0, 0.5]
        N.testing.assert_array_equal(
            outg.get_vertices()[:, outg.get_energy() > 0], correct_verts)
        N.testing.assert_array_almost_equal(outg.get_energy(), N.r_[100., 100.,
                                                                    0, 0])
Exemplo n.º 38
0
 def test_paraxial_ray(self):
     """A paraxial ray reaches the focus of a planoconvex lens"""
     rb = RayBundle(N.c_[[0., 0.001, 1.]], N.c_[[0., 0., -1.]], 
         energy=N.r_[1.], ref_index=N.r_[1.])
     screen = rect_one_sided_mirror(5, 5)
     f = self.lens.focal_length()
     screen.set_transform(translate(0, 0, -f))
     
     e = TracerEngine(Assembly([self.lens, screen]))
     vert, _ = e.ray_tracer(rb, 3, 1e-6)
     
     self.failUnlessAlmostEqual(vert[1,2], 0, 4)
Exemplo n.º 39
0
    def setUp(self):
        """
        Prepare an assembly with two subassemblies: one assembly representing
        a spherical lens behind a flat screen, and one asssembly representing a
        perfect mirror.
        The mirror will be placed at the two subassemblies' focus, so a paraxial
        ray will come back on the other side of the optical axis.
        
        Reference:
        In [1], the lensmaker equation
        """
        # focal length = 1, thickness = 1/6
        R = 1. / 6.
        back_surf = Surface(HemisphereGM(R),
                            opt.RefractiveHomogenous(1., 1.5),
                            location=N.r_[0., 0., -R / 2.])
        front_surf = Surface(HemisphereGM(R),
                             opt.RefractiveHomogenous(1., 1.5),
                             location=N.r_[0., 0., R / 2.],
                             rotation=rotx(N.pi / 2.)[:3, :3])

        front_lens = AssembledObject(surfs=[back_surf, front_surf])

        back_surf = Surface(RoundPlateGM(R),
                            opt.RefractiveHomogenous(1., 1.5),
                            location=N.r_[0., 0., -0.01])
        front_surf = Surface(RoundPlateGM(R),
                             opt.RefractiveHomogenous(1., 1.5),
                             location=N.r_[0., 0., 0.01])

        glass_screen = AssembledObject(surfs=[back_surf, front_surf],
                                       transform=translate(0., 0., 0.5))

        lens_assembly = Assembly(objects=[glass_screen, front_lens])
        lens_assembly.set_transform(translate(0., 0., 1.))
        full_assembly = Assembly(objects=[rect_one_sided_mirror(1., 1., 0.)],
                                 subassemblies=[lens_assembly])

        self.engine = TracerEngine(full_assembly)
Exemplo n.º 40
0
class TestObjectBuilding2(unittest.TestCase):
    """Tests an object composed of two flat surfaces"""
    flat_only = True

    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1., 1.5),
                           location=N.array([0, 0, -1.]))
        surface2 = Surface(flat_surface.FlatGeometryManager(),
                           optics_callables.RefractiveHomogenous(1.5, 1.),
                           location=N.array([0, 0, 1.]))

        self.object = AssembledObject(surfs=[surface1, surface2])
        self.assembly.add_object(self.object)

        x = 1 / (math.sqrt(2))
        dir = N.c_[[0, -x, x]]
        position = N.c_[[0, 1, -2.]]
        self._bund = RayBundle(position,
                               dir,
                               energy=N.r_[1.],
                               ref_index=N.r_[1.])

    def test_refraction1(self):
        """Tests the refractive functions after a single intersection"""
        self.engine = TracerEngine(self.assembly)
        ans = self.engine.ray_tracer(self._bund, 1, .05)
        params = N.arctan(ans[1][1] / ans[1][2])
        correct_params = N.r_[0.785398163, -.4908826]
        N.testing.assert_array_almost_equal(params, correct_params)

    def test_refraction2(self):
        """Tests the refractive functions after two intersections"""
        self.engine = TracerEngine(self.assembly)
        ans = self.engine.ray_tracer(self._bund, 2, .05)
        params = N.arctan(ans[1][1] / ans[1][2])
        correct_params = N.r_[-0.7853981]
        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 41
0
    def setUp(self):
        surface1 = Surface(HemisphereGM(2.), opt.perfect_mirror,
            rotation=general_axis_rotation(N.r_[1,0,0], N.pi/2.))
        surface2 = Surface(HemisphereGM(2.), opt.perfect_mirror, 
            location=N.array([0,-2,0]), 
            rotation=general_axis_rotation(N.r_[1,0,0], -N.pi/2.))
        
        self._bund = RayBundle()
        self._bund.set_directions(N.c_[[0,1,0]])
        self._bund.set_vertices(N.c_[[0,-1,0]])
        self._bund.set_energy(N.r_[[1]]) 
        self._bund.set_ref_index(N.r_[[1]])

        assembly = Assembly()
        object1 = AssembledObject()
        object2 = AssembledObject()
        object1.add_surface(surface1)
        object2.add_surface(surface2)
        assembly.add_object(object1)
        assembly.add_object(object2)

        self.engine = TracerEngine(assembly)
Exemplo n.º 42
0
class TestObjectBuilding1(unittest.TestCase):
    """Tests an object composed of sphere surfaces"""
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(HemisphereGM(3.), optics_callables.perfect_mirror, 
            location=N.array([0,0,-1.]),
            rotation=general_axis_rotation(N.r_[1,0,0], N.pi))
        surface2 = Surface(HemisphereGM(3.), optics_callables.perfect_mirror, 
            location=N.array([0,0,1.]))
        
        self.object = AssembledObject()
        self.object.add_surface(surface1)
        self.object.add_surface(surface2)
        self.assembly.add_object(self.object)

        dir = N.c_[[0,0,1.],[0,0,1.]]
        position = N.c_[[0,0,-3.],[0,0,-1.]]
    
        self._bund = RayBundle(position, dir, energy=N.ones(2))
    
    def test_object(self):
        """Tests that the assembly heirarchy works at a basic level"""
        self.engine = TracerEngine(self.assembly)

        inters = self.engine.ray_tracer(self._bund,1,.05)[0]
        correct_inters = N.c_[[0,0,2],[0,0,-2]]

        N.testing.assert_array_almost_equal(inters, correct_inters)
    
    def test_translation(self):
        """Tests an assembly that has been translated"""
        trans = N.array([[1,0,0,0],[0,1,0,0],[0,0,1,1],[0,0,0,1]])
        self.assembly.transform_children(trans)

        self.engine = TracerEngine(self.assembly)

        params =  self.engine.ray_tracer(self._bund,1,.05)[0]
        correct_params = N.c_[[0,0,3],[0,0,-1]]

        N.testing.assert_array_almost_equal(params, correct_params)

    def test_rotation_and_translation(self):
        """Tests an assembly that has been translated and rotated"""
        self._bund = RayBundle()
        self._bund.set_vertices(N.c_[[0,-5,1],[0,5,1]])
        self._bund.set_directions(N.c_[[0,1,0],[0,1,0]])
        self._bund.set_energy(N.r_[[1,1]])
        self._bund.set_ref_index(N.r_[[1,1]])

        trans = generate_transform(N.r_[[1,0,0]], N.pi/2, N.c_[[0,0,1]])
        self.assembly.transform_children(trans)

        self.engine = TracerEngine(self.assembly)

        params =  self.engine.ray_tracer(self._bund,1,.05)[0]
        correct_params = N.c_[[0,-2,1]]

        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 43
0
    def test_rotated(self):
        """One-sided plate with rotation"""
        rot = sp.roty(N.pi / 4.)
        self.mirror.set_transform(rot)

        e = TracerEngine(Assembly(objects=[self.mirror]))
        e.ray_tracer(self.bund, 1, 0.05)
        outg = e.tree[-1]

        correct_verts = N.array([[0., 0.5], [0., 0.], [0., -0.5]])
        N.testing.assert_array_almost_equal(
            outg.get_vertices()[:, outg.get_energy() > 0], correct_verts)
        N.testing.assert_array_almost_equal(outg.get_energy(), N.r_[100., 100.,
                                                                    0, 0])
Exemplo n.º 44
0
class TestTraceProtocol1(unittest.TestCase):
    """ 
    Tests intersect_ray and the bundle driver with a single flat surface, not rotated, with 
    a single interation 
    """
    def setUp(self):
        dir = N.array([[1, 1, -1], [-1, 1, -1], [-1, -1, -1], [1, -1, -1]
                       ]).T / math.sqrt(3)
        position = N.c_[[0, 0, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]]
        self._bund = RayBundle(position, dir, energy=N.ones(4))

        self.assembly = Assembly()
        object = AssembledObject()
        object.add_surface(Surface(FlatGeometryManager(), opt.perfect_mirror))
        self.assembly.add_object(object)
        self.engine = TracerEngine(self.assembly)

    def test_intersect_ray1(self):
        surfaces = self.assembly.get_surfaces()
        objects = self.assembly.get_objects()
        surfs_per_obj = [len(obj.get_surfaces()) for obj in objects]
        surf_ownership = N.repeat(N.arange(len(objects)), surfs_per_obj)
        ray_ownership = -1 * N.ones(self._bund.get_num_rays())
        surfs_relevancy = N.ones((len(surfaces), self._bund.get_num_rays()),
                                 dtype=N.bool)

        params = self.engine.intersect_ray(self._bund, surfaces, objects, \
            surf_ownership, ray_ownership, surfs_relevancy)[0]
        self.failUnless(params.all())

    def test_ray_tracer(self):
        """Ray tracer after one iteration returns what the surface would have"""
        params = self.engine.ray_tracer(self._bund, 1, .05)[0]
        correct_pts = N.zeros((3, 4))
        correct_pts[:2, 0] = 1

        N.testing.assert_array_almost_equal(params, correct_pts)
Exemplo n.º 45
0
class TestAssemblyBuilding4(unittest.TestCase):
    """Tests an assembly composed of objects"""
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(Paraboloid(), optics_callables.perfect_mirror)
        self.object = AssembledObject()
        self.object.add_surface(surface1)
        self.assembly.add_object(self.object)

        x = 1. / (math.sqrt(2))
        dir = N.c_[[0, 0, -1.], [0, x, -x]]
        position = N.c_[[0, 0, 1.], [0, 0, 1.]]
        self._bund = RayBundle(position,
                               dir,
                               energy=N.ones(2),
                               ref_index=N.ones(2))

    def test_paraboloid1(self):
        """Tests a paraboloid"""

        self.engine = TracerEngine(self.assembly)
        params = self.engine.ray_tracer(self._bund, 1, .05)[0]
        correct_params = N.c_[[0, 0, 0], [0, 0.618033989, 0.381966011]]
        N.testing.assert_array_almost_equal(params, correct_params)
Exemplo n.º 46
0
    def setUp(self):
        absorptive = Surface(RectPlateGM(1., 1.),
                             opt.Reflective(1.),
                             location=N.r_[0.5, 0., 1.])
        reflective = Surface(RectPlateGM(1., 1.),
                             opt.Reflective(0.),
                             location=N.r_[-0.5, 0., 1.])
        self.assembly = Assembly(
            objects=[AssembledObject(surfs=[absorptive, reflective])])

        # 4 rays: two toward absorptive, two toward reflective.
        pos = N.zeros((3, 4))
        pos[0] = N.r_[0.5, 0.25, -0.25, -0.5]
        direct = N.zeros((3, 4))
        direct[2] = 1.
        self.bund = RayBundle(pos, direct, energy=N.ones(4))
Exemplo n.º 47
0
    def setUp(self):
        self.assembly = Assembly()

        surface1 = Surface(FlatGeometryManager(), opt.perfect_mirror)
        self.object1 = AssembledObject()
        self.object1.add_surface(surface1)  
        
        boundary = BoundarySphere(location=N.r_[0,0.,3], radius=3.)
        surface3 = Surface(CutSphereGM(2., boundary), opt.perfect_mirror)
        self.object2 = AssembledObject()
        self.object2.add_surface(surface3)

        self.transform1 = generate_transform(N.r_[1.,0,0], N.pi/4, N.c_[[0,0,-1.]])
        self.transform2 = translate(0., 0., 2.)
        self.assembly.add_object(self.object1, self.transform1)
        self.assembly.add_object(self.object2, self.transform2)
Exemplo n.º 48
0
 def setUp(self):
     self.assembly = Assembly()
     surface1 = Surface(flat_surface.FlatGeometryManager(),
         optics_callables.RefractiveHomogenous(1., 1.5),
         location=N.array([0,0,-1.]))
     surface2 = Surface(flat_surface.FlatGeometryManager(),
         optics_callables.RefractiveHomogenous(1.5, 1.),
         location=N.array([0,0,1.]))
     
     self.object = AssembledObject(surfs=[surface1, surface2])
     self.assembly.add_object(self.object)
     
     x = 1/(math.sqrt(2))
     dir = N.c_[[0,-x,x]]
     position = N.c_[[0,1,-2.]]
     self._bund = RayBundle(position, dir, energy=N.r_[1.], ref_index=N.r_[1.])
Exemplo n.º 49
0
	def gen_plant(self):
		"""Generates the entire plant"""
		# set heliostat field characteristics: 0.52m*0.52m, abs = 0, aim_h =61
		self.field = HeliostatField(self.pos, 6.09e-1, 6.09e-1, 0, 6.1, 1e-3)
		# generates a transformation matrix of the receiver rec_trans for rotations
		rx_M = N.matrix(rotx(self.rx))
		ry_M = N.matrix(rotx(self.ry))
		rz_M = N.matrix(rotx(self.rz))
		rec_trans = N.array((rx_M)*(ry_M)*(rz_M))
		# applies translations to the rotation matrix to get the final transformation
		rec_trans[0,3] = self.dx
		rec_trans[1,3] = self.dy
		rec_trans[2,3] = self.dz
		# applies the transformation to the receiver object
		self.rec_obj.set_transform(rec_trans)
		# combines all objects into a single plant
		self.plant = Assembly(objects = [self.rec_obj], subassemblies=[self.field])
Exemplo n.º 50
0
    def setUp(self):
        self.assembly = Assembly()
        surface1 = Surface(HemisphereGM(3.), optics_callables.perfect_mirror, 
            location=N.array([0,0,-1.]),
            rotation=general_axis_rotation(N.r_[1,0,0], N.pi))
        surface2 = Surface(HemisphereGM(3.), optics_callables.perfect_mirror, 
            location=N.array([0,0,1.]))
        
        self.object = AssembledObject()
        self.object.add_surface(surface1)
        self.object.add_surface(surface2)
        self.assembly.add_object(self.object)

        dir = N.c_[[0,0,1.],[0,0,1.]]
        position = N.c_[[0,0,-3.],[0,0,-1.]]
    
        self._bund = RayBundle(position, dir, energy=N.ones(2))
Exemplo n.º 51
0
class TestNestedAssemblies(unittest.TestCase):
    """
    Create an assembly within an assembly, with an object, and check that 
    all transformation activities are handled correctly.
    """
    flat_only = True
    def setUp(self):
        self.eighth_circle_trans = generate_transform(N.r_[1., 0, 0], N.pi/4, 
            N.c_[[0., 1, 0]])
        
        self.surf = Surface(flat_surface.FlatGeometryManager(), \
            optics_callables.perfect_mirror)
        self.obj = AssembledObject(surfs=[self.surf])
        self.sub_assembly = Assembly()
        self.sub_assembly.add_object(self.obj, self.eighth_circle_trans)
        self.assembly = Assembly()
        self.assembly.add_assembly(self.sub_assembly, self.eighth_circle_trans)
    
    def test_initial_transforms(self):
        """Initial consrtruction yielded correct permanent and temporary transforms"""
        quarter_circle_trans = N.dot(self.eighth_circle_trans, self.eighth_circle_trans)
        
        # Surface transforms:
        N.testing.assert_array_almost_equal(self.surf._transform, N.eye(4))
        N.testing.assert_array_almost_equal(self.surf._temp_frame, quarter_circle_trans)
        
        # Object transform:
        N.testing.assert_array_almost_equal(self.obj.get_transform(),
            self.eighth_circle_trans)
        
        # Subassembly transform:
        N.testing.assert_array_almost_equal(self.sub_assembly.get_transform(),
            self.eighth_circle_trans)
    
    def test_retransform_object(self):
        """Changing an object's transform yield's correct resaults after retransform"""
        self.obj.set_transform(N.eye(4))
        self.assembly.transform_children()
        
        # Surface transforms:
        N.testing.assert_array_almost_equal(self.surf._transform, N.eye(4))
        N.testing.assert_array_almost_equal(self.surf._temp_frame, 
            self.eighth_circle_trans)
        
        # Object transform:
        N.testing.assert_array_almost_equal(self.obj.get_transform(),
            N.eye(4))
        
        # Subassembly transform:
        N.testing.assert_array_almost_equal(self.sub_assembly.get_transform(),
            self.eighth_circle_trans)
    
    def test_retransform_subassembly(self):
        """Changing an assembly's transform yield's correct resaults after retransform"""
        self.sub_assembly.set_transform(N.eye(4))
        self.assembly.transform_children()

        # Surface transforms:
        N.testing.assert_array_almost_equal(self.surf._transform, N.eye(4))
        N.testing.assert_array_almost_equal(self.surf._temp_frame,
            self.eighth_circle_trans)
        
        # Object transform:
        N.testing.assert_array_almost_equal(self.obj.get_transform(),
            self.eighth_circle_trans)
        
        # Subassembly transform:
        N.testing.assert_array_almost_equal(self.sub_assembly.get_transform(),
            N.eye(4))
    
    def test_interface(self):
        """Can call getters on an assembly etc."""
        subs = self.assembly.get_assemblies()
        self.assertEqual(len(subs), 1)
        self.assertTrue(subs[0] is self.sub_assembly)
Exemplo n.º 52
0
class TowerScene():
	""" Creates a scene of the heliostats, tower and receiver """
	# recobj is an assembled receiver object
	# surf_ls is a list of all surfaces used in the receiver
	# crit_ls is a list of all surfaces to be viewed in a histogram
	# heliostat is a csv file of coordinates (Example: sandia_hstat_coordinates.csv)
	# dx,dy,dz are x,y,z offsets from the origin (default dz is 6.1 metres)
	# rx,ry,rz are rotations about the x,y,z axes in radians (default 0)
	def __init__(self,rec_obj,surf_ls,crit_ls,heliostat,sun_az = 0.,sun_elev = 34.9,\
	dx = 0., dy = 0., dz = 6.1, rx = 0, ry = 0, rz = 0):
		self.sun_az = sun_az
		self.sun_elev = sun_elev
		self.rec_obj = rec_obj
		self.surf_ls = surf_ls
		self.crit_ls = crit_ls
		# add offset properties
		self.dx = dx
		self.dy = dy
		self.dz = dz
		# add rotation properties
		self.rx = rx
		self.ry = ry
		self.rz = rz
		# add the heliostat coordinates
		self.pos = N.loadtxt(heliostat, delimiter=",")
		self.pos *= 0.1
		# generate the entire plant now
		self.gen_plant()
		# creates an attribute which shows number of rays used, start at zero
		self.no_of_rays = 0
		self.helio_hits = 0

	def gen_rays(self):
		sun_vec = solar_vector(self.sun_az*degree, self.sun_elev*degree)
        	rpos = (self.pos + sun_vec).T
        	direct = N.tile(-sun_vec, (self.pos.shape[0], 1)).T
        	rays = RayBundle(rpos, direct, energy=N.ones(self.pos.shape[0]))

		return rays

	def gen_plant(self):
		"""Generates the entire plant"""
		# set heliostat field characteristics: 0.52m*0.52m, abs = 0, aim_h =61
		self.field = HeliostatField(self.pos, 6.09e-1, 6.09e-1, 0, 6.1, 1e-3)
		# generates a transformation matrix of the receiver rec_trans for rotations
		rx_M = N.matrix(rotx(self.rx))
		ry_M = N.matrix(rotx(self.ry))
		rz_M = N.matrix(rotx(self.rz))
		rec_trans = N.array((rx_M)*(ry_M)*(rz_M))
		# applies translations to the rotation matrix to get the final transformation
		rec_trans[0,3] = self.dx
		rec_trans[1,3] = self.dy
		rec_trans[2,3] = self.dz
		# applies the transformation to the receiver object
		self.rec_obj.set_transform(rec_trans)
		# combines all objects into a single plant
		self.plant = Assembly(objects = [self.rec_obj], subassemblies=[self.field])


	def aim_field(self):
		"""Aims the field to the sun?"""
		self.field.aim_to_sun(self.sun_az*degree, self.sun_elev*degree)

	def trace(self, rph, iters = 10000, minE = 1e-9, render = False):
		"""Commences raytracing using (rph) number of rays per heliostat, for a maximum of 
		   (iters) iterations, discarding rays with energy less than (minE). If render is
		   True, a 3D scene will be displayed which would need to be closed to proceed."""
		# Get the solar vector using azimuth and elevation
		sun_vec = solar_vector(self.sun_az*degree, self.sun_elev*degree)
        	# Calculate number of rays used. Rays per heliostat * number of heliostats.
        	num_rays = rph*len(self.field.get_heliostats())
		self.no_of_rays += num_rays
		# Generates the ray bundle
        	rot_sun = rotation_to_z(-sun_vec)
        	direct = N.dot(rot_sun, pillbox_sunshape_directions(num_rays, 0.00465))
        
        	xy = N.random.uniform(low=-0.25, high=0.25, size=(2, num_rays))
        	base_pos = N.tile(self.pos, (rph, 1)).T #Check if its is rph or num_rays
       		base_pos += N.dot(rot_sun[:,:2], xy)
        	
        	base_pos -= direct
        	rays = RayBundle(base_pos, direct, energy=N.ones(num_rays))

		# Perform the raytracing
		e = TracerEngine(self.plant)
		e.ray_tracer(rays, iters, minE, tree=True)
		e.minener = minE
		rays_in = sum(e.tree._bunds[0].get_energy())
		self.helio_hits = sum(e.tree._bunds[1].get_energy())


		# Optional rendering
		if render == True:
			trace_scene = Renderer(e)
			trace_scene.show_rays()
			
	def hist_comb(self, no_of_bins=100):
		"""Returns a combined histogram of all critical surfaces and relevant data"""
		# H is the histogram array
		# boundlist is a list of plate boundaries given in x coordinates
		# extent is a list of [xmin,xmax,ymin,ymax] values
		# binarea is the area of each bin. Used to estimate flux concentration

		# Define empty elements
		X_offset = 0	# Used to shift values to the right for each subsequent surface
		all_X = []	# List of all x-coordinates
		all_Y = []	# List of all y-coordinates
		all_E = []	# List of all energy values
		boundlist = [0]	# List of plate boundaries, starts with x=0

		#print("length here"+str(len((self.plant.get_local_objects()[0]).get_surfaces())))

		#for plate in self.crit_ls:	#For each surface within the list of critical surfs
		crit_length = len(self.crit_ls)
		count = 0
		while count < crit_length: # count is one less than crit_length for indexing convention
			surface = (self.plant.get_local_objects()[0]).get_surfaces()[count]
			# returns all coordinates where a hit occured and its energy absorbed
			energy, pts = surface.get_optics_manager().get_all_hits()
			corners = surface.mesh(1) #corners is an array of all corners of the plate
			# BLC is bottom left corner "origin" of the histogram plot
			# BRC is the bottom right corner "x-axis" used for vector u
			# TLC is the top right corner "y-axis" used for vector v
			BLC = N.array([corners[0][1][1],corners[1][1][1],corners[2][1][1]])
			BRC = N.array([corners[0][0][1],corners[1][0][1],corners[2][0][1]])
			TLC = N.array([corners[0][1][0],corners[1][1][0],corners[2][1][0]])
			# Get vectors u and v in array form of array([x,y,z])
			u = BRC - BLC
			v = TLC - BLC
			# Get width(magnitude of u) and height(magnitude of v) in float form
			w = (sum(u**2))**0.5
			h = (sum(v**2))**0.5
			# Get unit vectors of u and v in form of array([x,y,z])
			u_hat = u/w
			v_hat = v/h
			# Local x-position determined using dot product of each point with direction
			# Returns a list of local x and y coordinates
			origin = N.array([[BLC[0]],[BLC[1]],[BLC[2]]])
			local_X = list((N.array(N.matrix(u_hat)*N.matrix(pts-origin))+X_offset)[0])
			#local_Y = list((N.array(N.matrix(v_hat)*N.matrix(pts-origin)))[0])
			local_Y = list((((N.array(N.matrix(v_hat)*N.matrix(pts-origin)))[0])*-1)+h)
			# Adds to the lists
			all_X += local_X
			all_Y += local_Y
			all_E += list(energy)
			X_offset += w
			boundlist.append(X_offset)
			count += 1
		# Now time to build a histogram
		rngy = h
		rngx = X_offset
		bins = [no_of_bins,int(no_of_bins*X_offset)]
		H,ybins,xbins = N.histogram2d(all_Y,all_X,bins,range=([0,rngy],[0,rngx]), weights=all_E)
		extent = [xbins[0],xbins[-1],ybins[0],ybins[-1]]
		binarea = (float(h)/no_of_bins)*(float(X_offset)/int(no_of_bins*X_offset))
		return H, boundlist, extent, binarea

	def energies(self):
		"""Returns the total number of hits on the heliostats, receiver and the total energy absorbed"""
		
		totalenergy = 0.0
		totalhits = 0
		heliohits = self.helio_hits
		#length = 0
		#for surface in self.plant.get_local_objects()[0].get_surfaces():
		for surface in (self.plant.get_local_objects()[0]).get_surfaces():
			energy, pts = surface.get_optics_manager().get_all_hits()
			absorp = surface._opt._opt._abs
			#length += len(energy)
			#plt.plot(range(0,len(energy)),energy,'ro')
			#plt.show()
			totalenergy += sum(energy)
			totalhits += sum(energy == absorp)
		#print("Length is"+str(length))
		return totalenergy, totalhits, heliohits