def test_emit_mass_stored_3(self): """ Corner case: emitting 0 mass does not affect mass stored. """ emitter = MarbleEmitter(self.__mock_prototype, 0, stored_mass=13) emitter.emit() self.assertEqual(emitter._Emitter__stored_mass.item(), 13)
def test_can_emit_massfull_3(self): """ Base case: can emit negative mass when negative mass stored. """ neg_mass_prototype = MockPrototype(-1) emitter = MarbleEmitter(neg_mass_prototype, 0, stored_mass=-1) self.assertTrue(emitter.can_emit())
def test_emit_mass_stored_2(self): """ Base case: emitting negative mass increases mass stored. """ emitter = MarbleEmitter(MockPrototype(-1), 0, stored_mass=-1) emitter.emit() self.assertEqual(emitter._Emitter__stored_mass.item(), 0)
def test_spawn_location_2(self): """ Base case: stationary Node. Using a normal MarbleEmitter. """ vel = torch.tensor([1.0]) radius = 10 eps = 10e-5 prototype = Marble(ZERO + radius + eps, ZERO, ZERO, 0, None, None, 0, 0, 0, 0) emitter = MarbleEmitter(prototype, 0, 10) emitter_node = MarbleEmitterNode(ZERO, vel, ZERO, 0, None, 0, 0, 0, 0, radius=radius, emitter=emitter) self.assertTrue(check_close(emitter_node.pos, ZERO)) self.assertTrue(check_close(emitter.emit().pos, ZERO + radius + eps))
def test_can_emit_massfull_2(self): """ Base case: can emit after adding sufficient mass. """ emitter = MarbleEmitter(self.__massfull_prototype, 0, stored_mass=0) emitter.eat_mass(self.__mass) self.assertTrue(emitter.can_emit())
def bit_write_expetiment_emitters(): locker_nodes = __generate_locker_nodes(LOCKER_POSITIONS) # Unity vector from lower-left-corner towards upper-right-corner incoming_direction = 0.5 * np.sqrt(2) * np.array([1, -1]) outer_emitter_pos = BIT_POS - 50 * incoming_direction inner_emitter_pos = BIT_POS - 0.1 * incoming_direction outer_radius = 5 signal_marble_pos = outer_emitter_pos + outer_radius * (incoming_direction) signal_marble_mass = BIT_MARBLE_MASS signal_marble_prototype = Marble(pos=signal_marble_pos, vel=10 * incoming_direction, acc=ZERO, mass=signal_marble_mass, attraction_function=ZERO_ATTRACTION, datum=None, **READER_MARBLE_STIFFNESS) outer_emitter = MarbleEmitter(signal_marble_prototype, 0, 0) outer_emitter_node = MarbleEmitterNode(outer_emitter_pos, ZERO, ZERO, 1, ZERO_ATTRACTION, radius=outer_radius, emitter=outer_emitter, **EMITTER_STIFFNESSES) inner_emitter_pos = BIT_POS - 2 * (incoming_direction) inner_emitter_radius = 0.01 bit_spawn_pos = inner_emitter_pos + \ incoming_direction*inner_emitter_radius bit_marble = Marble(pos=bit_spawn_pos, vel=np.array([1, 0.5]), acc=ZERO, mass=BIT_MARBLE_MASS, attraction_function=ATTRACTION_FUNCTION, datum=None, **BIT_MARBLE_STIFFNESS) inner_emitter = MarbleEmitter(bit_marble, 0) inner_emitter_node = MarbleEmitterNode(inner_emitter_pos, ZERO, ZERO, 1, ZERO_ATTRACTION, radius=inner_emitter_radius, emitter=inner_emitter, **EMITTER_STIFFNESSES) activation_marble = Marble(pos=outer_emitter_pos + np.array([0, 100]), vel=np.array([0, -10]), acc=ZERO, mass=signal_marble_mass, attraction_function=ATTRACTION_FUNCTION, datum=None, **READER_MARBLE_STIFFNESS) run([activation_marble], locker_nodes + [outer_emitter_node, inner_emitter_node])
def test_can_emit_massfull_4(self): """ Base case: can emit if initial mass is sufficient. """ emitter = MarbleEmitter(self.__massfull_prototype, 0, stored_mass=self.__mass) self.assertTrue(emitter.can_emit())
def test_delay_5(self): """ Base case: cannot emit at start if initial_time_passed is too low. """ emitter = MarbleEmitter(self.__mock_prototype, 10, initial_time_passed=0) self.assertFalse(emitter.can_emit())
def test_delay_1(self): """ Base case: can always emit at start if initial_time_passed high enough. """ emitter = MarbleEmitter(self.__mock_prototype, 10, initial_time_passed=10) self.assertTrue(emitter.can_emit())
def test_delay_4(self): """ Base case: can emit second time after *more* than enough time passed. """ delay = 10 emitter = MarbleEmitter(self.__mock_prototype, delay) emitter.register_time_passed(delay + 10) self.assertTrue(emitter.can_emit())
def test_emit_output_type(self): """ Output should be a EmittedMarble, not just a normal Marble. """ prototype = Marble(ZERO, ZERO, ZERO, 0, None, None) delay = 0 emitter = MarbleEmitter(prototype, delay) output = emitter.emit() self.assertIsInstance(output, EmittedMarble)
def test_emit_mass_stored_1(self): """ Base case: emitting positive mass decreases mass stored. """ emitter = MarbleEmitter(self.__massfull_prototype, 0, stored_mass=self.__mass) emitter.emit() self.assertEqual(emitter._Emitter__stored_mass.item(), 0)
def test_emit_massfull_3(self): """ Base case: can emit negative massfull Marble when negative mass stored. (Should not raise any errors) """ neg_mass_prototype = MockPrototype(-1) emitter = MarbleEmitter(neg_mass_prototype, 0, stored_mass=-1) try: emitter.emit() except: self.fail("Operation should not raise errors")
def test_emit_massfull_2(self): """ Base case: can emit massfull Marble when sufficient mass stored. """ emitter = MarbleEmitter(self.__massfull_prototype, 0, stored_mass=self.__mass) try: emitter.emit() except: self.fail("Operation should not raise errors")
def setup_full_emitter( self) -> Tuple[Emitter, Marble, float, float, float]: """ Create an instance of a MarbleEmitter with a full prototype Marble (not a mock Marble). Also returns the delay, stored_mass and initial_time_passed, which have arbitrary nonzero floating-point values. Returns: * A MarbleEmitter instance * A Marble, the prototype associated with the emitter * The delay of the emitter * The initally stored mass of the emitter * The initial time passed of the emitter """ pos = torch.tensor([0], dtype=torch.float) vel = torch.tensor([1], dtype=torch.float) acc = torch.tensor([2], dtype=torch.float) mass = 3.0 attraction_function = NewtonianGravity() marble_stiffness = 0.4 node_stiffness = 0.5 marble_attraction = 0.6 node_attraction = 0.7 prototype = Marble(pos, vel, acc, mass, attraction_function, None, marble_stiffness, node_stiffness, marble_attraction, node_attraction) delay = 8.1 stored_mass = 9.1 initial_time_passed = 11.1 emitter = MarbleEmitter(prototype, delay, stored_mass, initial_time_passed) return emitter, prototype, delay, stored_mass, initial_time_passed
def test_emit_1(self): """ Corner case: throw error when trying to emit too early after previous emit. """ delay = 10 emitter = MarbleEmitter(self.__mock_prototype, delay) emitter.register_time_passed(delay) emitter.emit() emitter.register_time_passed(delay / 2) self.assertRaises(RuntimeError, emitter.emit)
def test_delay_2(self): """ Base case: cannot emit right after previous emit, when delay > 0. """ delay = 10 emitter = MarbleEmitter(self.__mock_prototype, delay) emitter.register_time_passed(delay) emitter.emit() self.assertFalse(emitter.can_emit())
def test_repr_1(self): """ Base case: no mass absorbed or time passed. """ emitter, prototype, delay, stored_mass, initial_time_passed = \ self.setup_full_emitter() expected = f"MarbleEmitter({repr(prototype)}," \ + f"{convert_scalar_param_to_repr(delay)},"\ + f"{convert_scalar_param_to_repr(stored_mass)},"\ + f"{convert_scalar_param_to_repr(initial_time_passed)})" emitter = MarbleEmitter(prototype, delay, stored_mass, initial_time_passed) result = repr(emitter) self.assertEqual(expected, result)
def gen_emitter_node_with_delay(self, delay: float): emitter = MarbleEmitter(self.__prototype, delay) emitter_node = MarbleEmitterNode(np.array([1]), ZERO, ZERO, 1, ATTRACT_FUNCT, 0, 0, 0, 0, 0.1, emitter) return emitter, emitter_node
def test_can_emit_massfull_1(self): """ Base case: cannot emit massfull Marble when no mass stored. """ emitter = MarbleEmitter(self.__massfull_prototype, 0, stored_mass=0) self.assertFalse(emitter.can_emit())
def test_can_emit_massfull_5(self): """ Corner case: cannot emit negative mass if own mass is positive. """ emitter = MarbleEmitter(MockPrototype(-1), 0, stored_mass=1) self.assertFalse(emitter.can_emit())
def test_emit_massfull_1(self): """ Base case: cannot emit massfull Marble when no mass stored. """ emitter = MarbleEmitter(self.__massfull_prototype, 0, stored_mass=0) self.assertRaises(RuntimeError, emitter.emit)