def assertHasAllNeighbors(self, box, dimensions): ''' Given a box and world dimensions, assert that box has all required neighbors connected to it. ''' segment = Segment(Coord(0,0,0), Coord(dimensions.x-1,dimensions.y-1, dimensions.z-1)) for p in box.position.surroundingCoords(): if segment.contains(p): self.assertTrue(box.neighbors.has_key(p) and box.neighbors[p] != None, "Box at %s does not have neighbor %s" % (box.position, p))
def testGenerateTall(self): ''' Assert split for skinny tall world occurs along Z axis ''' rMap = self.rankMapGen.generate(2, Dimensions(4, 4, 100)) self.assertEqual(rMap.getRank(Coord(0,0,0)), 0) self.assertEqual(rMap.getRank(Coord(3,3,49)), 0) self.assertEqual(rMap.getRank(Coord(0,0,99)), 1) self.assertEqual(rMap.getRank(Coord(3,3,50)), 1)
def testGenerateLong(self): ''' Assert split for short long world occurs along X axis ''' rMap = self.rankMapGen.generate(2, Dimensions(100, 4, 4)) self.assertEqual(rMap.getRank(Coord(0,0,0)), 0) self.assertEqual(rMap.getRank(Coord(49,3,3)), 0) self.assertEqual(rMap.getRank(Coord(99,0,0)), 1) self.assertEqual(rMap.getRank(Coord(50,3,3)), 1)
def testParseFile1(self): file = "./testworld.txt" factory = WorldSegmentFactory(2, file) self.assertEqual(factory.worldSize, 2) self.assertEqual(factory.fileName, file) self.assertEqual(factory.timeStart, 0) self.assertEqual(factory.timeEnd, 1000) self.assertEqual(factory.timeDelta, 1) self.assertEqual(factory.cubeSize, 1) self.assertEqual(factory.dimensions, Dimensions(2,2,2)) self.assertEqual(factory.dimensions.volume(), 8) self.assertEqual(len(factory.sources), 3) self.assertEqual(factory.sources[1].light, 5) self.assertEqual(factory.sources[1].temperature, 4) self.assertEqual(factory.sources[1].e0, 10) self.assertEqual(len(factory.sinks), 3) #7,4,5,1,4 self.assertEqual(factory.sinks[1].terpene_concentration,7) self.assertEqual(factory.sinks[1].temperature, 277.15) self.assertEqual(factory.sinks[1].ozone, 5) self.assertEqual(factory.sinks[1].nox,1) self.assertEqual(factory.sinks[1].oh, 4) self.assertEqual(len(factory.lightFuncs), 2) self.assertEqual(len(factory.tempFuncs), 2) self.assertEqual(len(factory.boxes), 8) for rank in range(2): segment = factory.getWorldSegment(0) self.assertNotEqual(segment, None) self.assertEqual(len(segment.boxes), 4) for box in segment.boxes.values(): self.assertHasAllNeighbors(box, Dimensions(2,2,2)) segment = factory.getWorldSegment(0) for coord,terp in [(Coord(0,0,0), 0), (Coord(0,1,0), 2), (Coord(1,0,0), 4), (Coord(1,1,0), 6)]: self.assertTrue(segment.boxes.has_key(coord)) self.assertEquals(segment.boxes[coord].terpene_concentration, terp) segment = factory.getWorldSegment(1) for coord,terp in [(Coord(0,0,1), 1), (Coord(0,1,1), 3), (Coord(1,0,1), 5), (Coord(1,1,1), 7)]: self.assertTrue(segment.boxes.has_key(coord)) self.assertEquals(segment.boxes[coord].terpene_concentration, terp)
def testRankMap3(self): rMap = RankMap(2, Dimensions(2,2,2), Split(1,1,2)) # split half along Z axis self.assertEquals(len(rMap.entries()), 2, "Entries are %s" % rMap.entries()) self.assertEqual(rMap.getRank(Coord(0,0,0)), 0) self.assertEqual(rMap.getRank(Coord(0,0,1)), 1) self.assertEqual(rMap.getRank(Coord(0,1,0)), 0) self.assertEqual(rMap.getRank(Coord(0,1,1)), 1) self.assertEqual(rMap.getRank(Coord(1,0,0)), 0) self.assertEqual(rMap.getRank(Coord(1,0,1)), 1) self.assertEqual(rMap.getRank(Coord(1,1,0)), 0) self.assertEqual(rMap.getRank(Coord(1,1,1)), 1)
def testCanonical(self): ''' Test 1 box with 1 source for 10 time steps. Assert concentration is 1 at the end of the run. ''' box = Box( position=Coord(0, 0, 0), cube_length=1, light_time_function=lambda t: 1, #always return 1 temperature_time_function=lambda t: 1, #always return 1 timedelta=1, initial_time=0, end_time=10, initial_terpene=1, sources=(Source(baseEmission=1), ), sinks=()) box.run().join() self.assertEqual(box.terpene_concentration, 15.730507560710043)
def testCanonical(self): ''' Test 1 box with 1 sink for 10 time steps. Assert concentration is 1 at the end of the run. ''' nair = 101325 * 6.022e23 / (8.314 * (25 + 273.15)) fnair = nair * 1e-6 * 1e-9 box = Box( position=Coord(0, 0, 0), cube_length=1, light_time_function=lambda t: 1, #always return 1 temperature_time_function=lambda t: 1, #always return 1 timedelta=0.01, initial_time=0, end_time=1.0, initial_terpene=100, sinks=(Sink(25, 15, 2, 1e-4), )) box.run().join() self.assertEqual(box.terpene_concentration, 1)
def testRankMap2(self): rMap = RankMap(1, Dimensions(2,2,2), Split(1,1,1)) self.assertEquals(len(rMap.entries()), 1, "Entries are %s" % rMap.entries()) self.assertEqual(rMap.getRank(Coord(0,0,0)), 0)
def testCoord3(self): self.assertTrue(Segment(Coord(0,0,0), Coord(100,100,100)).contains(Coord(50,50,50)))
def testCoord2(self): self.assertFalse(Segment(Coord(0,0,0), Coord(0,0,0)).contains(Coord(1,1,1)))
def testCoord(self): self.assertTrue(Segment(Coord(0,0,0), Coord(0,0,0)).contains(Coord(0,0,0)))
return 30 #max_x = 2 #max_y = 0 #max_z = 2 world = {} # Coordinate tuple => box map # Create one box and all its neighbors # (x, y, z) points = ((0, 0, 0), (1, 0, 0), (-1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, 1), (0, 0, -1)) for p in points: world[p] = box.Box(Coord(p[0], p[1], p[2]), 1, ltf, ttf, 10, 0, end_time, random.randint(0, 10)) # Connect all the boxes together # Use 'right-hand-rule' for orientation for (point, box) in world.iteritems(): up = (point[0], point[1] + 1, point[2]) down = (point[0], point[1] - 1, point[2]) left = (point[0] - 1, point[1], point[2]) right = (point[0] + 1, point[1], point[2]) front = (point[0], point[1], point[2] - 1) back = (point[0], point[1], point[2] + 1) if world.has_key(up): box.connect_up(world[up])
#light time function def ltf(time): return 1500 # temperature time function def ttf(time): return 30 end_time = 1000 time_delta = 1 box1 = Box(Coord(0,0,0),1,ltf,ttf,time_delta,0, end_time, 10) box2 = Box(Coord(1,0,0),1,ltf,ttf,time_delta, 0, end_time, 100) box1.connect_right(box2) box2.connect_left(box1) t1 = box1.run() t2 = box2.run() t1.join() t2.join() print "Done: Box 1, terp_level= %d" % box1.terpene_concentration print "Done: Box 2, terp_level= %d" % box2.terpene_concentration