def test_send_out_state_to_active_leaf_unit_different_leaf_unit_velocities_raises_error( self): cnode = Node(Unit(identifier=(1, ), position=[0.1, 0.2, 0.3], velocity=[1.5, 0.0, 0.0], time_stamp=Time.from_float(1.5)), weight=1) first_child = Node(Unit(identifier=(1, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.5)), weight=0.5) cnode.add_child(first_child) second_child = Node(Unit(identifier=(1, 1), position=[0.5, 0.4, 0.3], charge={"charge": -1.0}, velocity=[2.0, 0.0, 0.0], time_stamp=Time.from_float(1.5)), weight=0.5) cnode.add_child(second_child) # Call this to update the event time self._event_handler_to_leaf_unit_motion.send_event_time([cnode]) with self.assertRaises(AssertionError): self._event_handler_to_leaf_unit_motion.send_out_state([cnode])
def test_send_out_state_to_active_root_unit_root_unit_active_raises_error( self): cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) # Call this to update the event time self._event_handler_to_root_unit_motion.send_event_time([cnode]) cnode.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.6, 0.1], charge={"charge": -1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) with self.assertRaises(AssertionError): self._event_handler_to_root_unit_motion.send_out_state([cnode])
def test_trash(self): not_comparable_instances = [NotComparableClass() for _ in range(5)] times = [Time.from_float(time) for time in [-0.3, 0, -1, 4.3, 8.777]] for index, not_comparable_instance in enumerate( not_comparable_instances): self._scheduler.push_event(times[index], not_comparable_instance) self.assertIs(self._scheduler.get_succeeding_event(), not_comparable_instances[2]) self._scheduler.trash_event(not_comparable_instances[2]) self.assertIs(self._scheduler.get_succeeding_event(), not_comparable_instances[0]) self._scheduler.trash_event(not_comparable_instances[0]) self._scheduler.trash_event(not_comparable_instances[1]) self.assertIs(self._scheduler.get_succeeding_event(), not_comparable_instances[3]) self._scheduler.trash_event(not_comparable_instances[3]) new_not_comparable_instances = [NotComparableClass() for _ in range(2)] new_times = [Time.from_float(time) for time in [9.3, 5.6]] for index, not_comparable_instance in enumerate( new_not_comparable_instances): self._scheduler.push_event(new_times[index], not_comparable_instance) self.assertIs(self._scheduler.get_succeeding_event(), new_not_comparable_instances[1]) self._scheduler.trash_event(new_not_comparable_instances[1]) self._scheduler.trash_event(new_not_comparable_instances[0]) self.assertIs(self._scheduler.get_succeeding_event(), not_comparable_instances[4])
def test_send_event_time_without_charge(self, random_expovariate_mock): self._setUpSendEventTime(random_expovariate_mock, self._potential_mock_without_charge) in_state_one = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.25, 0.5], time_stamp=Time.from_float(1.2)), weight=1) in_state_one.add_child( Node(Unit(identifier=(0, 2), position=[0.5, 0.9], velocity=[-0.5, 1.0], time_stamp=Time.from_float(1.3)), weight=0.5)) in_state_two = Node(Unit(identifier=(3, ), position=[0.5, 0.6]), weight=1) in_state_two.add_child( Node(Unit(identifier=(3, 1), position=[0.1, 0.3]), weight=0.5)) event_time = self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two]) # Should be called with beta random_expovariate_mock.assert_called_once_with(1) self.assertEqual( self._potential_mock_without_charge.displacement.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._potential_mock_without_charge.displacement.call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-0.5, 1.0], [-0.4, 0.4]], places=13, expected_kwargs={"potential_change": 2}) self.assertAlmostEqual(event_time, Time.from_float(1.6), places=13)
def test_send_event_time_infinite_displacement(self, random_expovariate_mock, _): # Bounding potential returns infinite time displacement. self._setUpSendEventTimeInfiniteDisplacement( random_expovariate_mock, self._bounding_potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[2.0, 0.0], time_stamp=Time.from_float(1.3), charge={"charge": 3.4}), weight=1) event_time = self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two]) # Should be called with beta random_expovariate_mock.assert_called_once_with(1) self.assertEqual( self._bounding_potential_mock_with_charge.displacement.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_with_charge.displacement. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[2.0, 0.0], [0.0, -0.3], -1.2, 3.4, 2], places=13, expected_kwargs={}) self.assertEqual(event_time, Time.from_float(float('inf')))
def test_send_event_time_to_active_root_unit_two_root_nodes_raises_error( self): cnode_one = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode_one.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) cnode_two = Node(Unit(identifier=(5, ), position=[0.4, 0.5, 0.6], velocity=[0.5, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode_two.add_child( Node(Unit(identifier=(5, 0), position=[0.3, 0.2, 0.1], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) with self.assertRaises(AssertionError): self._event_handler_to_root_unit_motion.send_event_time( [cnode_one, cnode_two])
def test_more_than_two_leaf_units_raises_error(self, random_expovariate_mock, _): self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(1, ), position=[0.2, 0.4], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.7)), weight=1) in_state_one.add_child( Node(Unit(identifier=(1, 0), position=[0.5, 0.6], velocity=[2.0, 0.0], time_stamp=Time.from_float(0.7)), weight=0.5)) in_state_one.add_child( Node(Unit(identifier=(1, 1), position=[0.3, 0.8]), weight=0.5)) in_state_two = Node(Unit(identifier=(2, ), position=[0.2, 0.4], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.7)), weight=1) in_state_two.add_child( Node(Unit(identifier=(2, 0), position=[0.5, 0.6], velocity=[2.0, 0.0], time_stamp=Time.from_float(0.7)), weight=0.5)) in_state_two.add_child( Node(Unit(identifier=(2, 1), position=[0.3, 0.8]), weight=0.5)) with self.assertRaises(AssertionError): self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two])
def test_send_out_state_to_active_root_unit(self): cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 1.0, 1.5], time_stamp=Time.from_float(1.0)), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 2.0, 3.0], time_stamp=Time.from_float(1.0)), weight=0.5)) # Call this to update the event time self._event_handler_to_root_unit_motion.send_event_time([cnode]) cnode.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.6, 0.1], charge={"charge": -1.0}), weight=0.5)) out_state = self._event_handler_to_root_unit_motion.send_out_state( [cnode]) self.assertEqual(len(out_state), 1) cnode = out_state[0] self.assertIsNone(cnode.parent) self.assertEqual(cnode.weight, 1) self.assertEqual(cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(cnode.value.position, [0.35, 0.7, 0.05], places=13) self.assertEqual(cnode.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(cnode.value.time_stamp, Time.from_float(1.5), places=13) self.assertEqual(len(cnode.children), 2) first_child = cnode.children[0] self.assertIs(first_child.parent, cnode) self.assertEqual(first_child.weight, 0.5) self.assertEqual(first_child.value.identifier, (0, 0)) self.assertAlmostEqualSequence(first_child.value.position, [0.2, 0.8, 0.4], places=13) self.assertEqual(first_child.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(first_child.value.time_stamp, Time.from_float(1.5), places=13) self.assertEqual(len(first_child.children), 0) second_child = cnode.children[1] self.assertIs(second_child.parent, cnode) self.assertEqual(second_child.weight, 0.5) self.assertEqual(second_child.value.identifier, (0, 1)) self.assertEqual(second_child.value.position, [0.5, 0.6, 0.1]) self.assertEqual(second_child.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(second_child.value.time_stamp, Time.from_float(1.5), places=13) self.assertEqual(len(second_child.children), 0)
def test_send_out_state_one_root_unit_active(self): self._event_handler_one.send_event_time() cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3]), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.4, 0.5, 0.6], charge={"charge": 1.0}), weight=0.5)) cnode.add_child( Node(Unit(identifier=(0, 1), position=[0.7, 0.8, 0.9], charge={"charge": -1.0}), weight=0.5)) out_state = self._event_handler_one.send_out_state(cnode) self.assertEqual(len(out_state), 1) out_state_cnode = out_state[0] self.assertIsNone(out_state_cnode.parent) self.assertEqual(out_state_cnode.value.identifier, (0, )) self.assertEqual(out_state_cnode.value.position, [0.1, 0.2, 0.3]) self.assertEqual(out_state_cnode.weight, 1) self.assertEqual(out_state_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(out_state_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertIsNone(out_state_cnode.value.charge) self.assertEqual(len(out_state_cnode.children), 2) first_child_cnode = out_state_cnode.children[0] self.assertIs(first_child_cnode.parent, out_state_cnode) self.assertEqual(first_child_cnode.children, []) self.assertEqual(first_child_cnode.value.identifier, (0, 0)) self.assertEqual(first_child_cnode.value.position, [0.4, 0.5, 0.6]) self.assertEqual(first_child_cnode.weight, 0.5) self.assertEqual(first_child_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(first_child_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertEqual(first_child_cnode.value.charge, {"charge": 1.0}) second_child_cnode = out_state_cnode.children[1] self.assertIs(second_child_cnode.parent, out_state_cnode) self.assertEqual(second_child_cnode.children, []) self.assertEqual(second_child_cnode.value.identifier, (0, 1)) self.assertEqual(second_child_cnode.value.position, [0.7, 0.8, 0.9]) self.assertEqual(second_child_cnode.weight, 0.5) self.assertEqual(second_child_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(second_child_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertEqual(second_child_cnode.value.charge, {"charge": -1.0})
def test_send_out_state_event_handler_one_general_velocity(self): # Call this to update the event time self._event_handler_one.send_event_time() active_cnode_one = Node(Unit(identifier=(0,), position=[0.1, 0.2, 0.3], velocity=[0.1, 0.2, 0.3], time_stamp=Time.from_float(0.0)), weight=1) active_cnode_two = Node(Unit(identifier=(3,), position=[0.4, 0.5, 0.6], velocity=[1.0, 0.6, 0.5], time_stamp=Time.from_float(0.2)), weight=1) out_state = self._event_handler_one.send_out_state([active_cnode_one, active_cnode_two]) self.assertEqual(len(out_state), 2) first_active_cnode = out_state[0] self.assertEqual(len(first_active_cnode.children), 0) self.assertIsNone(first_active_cnode.parent) self.assertEqual(first_active_cnode.value.identifier, (0,)) self.assertAlmostEqualSequence(first_active_cnode.value.position, [0.15, 0.3, 0.45], places=13) self.assertEqual(first_active_cnode.weight, 1) self.assertEqual(first_active_cnode.value.velocity, [0.1, 0.2, 0.3]) self.assertAlmostEqual(first_active_cnode.value.time_stamp, Time.from_float(0.5), places=13) self.assertIsNone(first_active_cnode.value.charge) second_active_cnode = out_state[1] self.assertEqual(len(second_active_cnode.children), 0) self.assertIsNone(second_active_cnode.parent) self.assertEqual(second_active_cnode.value.identifier, (3,)) self.assertAlmostEqualSequence(second_active_cnode.value.position, [0.7, 0.68, 0.75], places=13) self.assertEqual(second_active_cnode.weight, 1) self.assertEqual(second_active_cnode.value.velocity, [1.0, 0.6, 0.5]) self.assertAlmostEqual(second_active_cnode.value.time_stamp, Time.from_float(0.5), places=13) self.assertIsNone(second_active_cnode.value.charge) self._event_handler_one.send_event_time() active_cnode_one = Node(Unit(identifier=(0,), position=[0.8, 0.4, 0.3], velocity=[1.0, 2.0, 3.0], time_stamp=Time.from_float(0.7)), weight=1) active_cnode_two = Node(Unit(identifier=(3,), position=[0.7, 0.2, 0.3], velocity=[-1.0, -0.6, -0.5], time_stamp=Time.from_float(0.2)), weight=1) out_state = self._event_handler_one.send_out_state([active_cnode_one, active_cnode_two]) self.assertEqual(len(out_state), 2) first_active_cnode = out_state[0] self.assertEqual(len(first_active_cnode.children), 0) self.assertIsNone(first_active_cnode.parent) self.assertEqual(first_active_cnode.value.identifier, (0,)) self.assertAlmostEqualSequence(first_active_cnode.value.position, [0.1, 0.0, 0.2], places=13) self.assertEqual(first_active_cnode.weight, 1) self.assertEqual(first_active_cnode.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(first_active_cnode.value.time_stamp, Time.from_float(1.0), places=13) self.assertIsNone(first_active_cnode.value.charge) second_active_cnode = out_state[1] self.assertEqual(len(second_active_cnode.children), 0) self.assertIsNone(second_active_cnode.parent) self.assertEqual(second_active_cnode.value.identifier, (3,)) self.assertAlmostEqualSequence(second_active_cnode.value.position, [0.9, 0.72, 0.9], places=13) self.assertEqual(second_active_cnode.weight, 1) self.assertEqual(second_active_cnode.value.velocity, [-1.0, -0.6, -0.5]) self.assertAlmostEqual(second_active_cnode.value.time_stamp, Time.from_float(1.0), places=13) self.assertIsNone(second_active_cnode.value.charge)
def __init__(self, sampling_interval: float, output_handler: str, first_event_time_zero: bool = False) -> None: """ The constructor of the FixedIntervalSamplingEventHandler class. Parameters ---------- sampling_interval : float The time interval of the sampling. output_handler : str The name of the output handler. first_event_time_zero : bool, optional If the first returned candidate event time is zero. Raises ------ base.exceptions.ConfigurationError: If the sampling interval is not greater than zero. """ log_init_arguments(logging.getLogger(__name__).debug, self.__class__.__name__, sampling_interval=sampling_interval, output_handler=output_handler) super().__init__(output_handler=output_handler) if not sampling_interval > 0.0: raise ConfigurationError("The sampling_interval in the event handler {0} has to be > 0.0." .format(self.__class__.__name__)) self._sampling_interval = sampling_interval self._event_time = Time(0.0, 0.0) if not first_event_time_zero else Time.from_float(-sampling_interval)
def __init__(self, end_of_run_time: float, output_handler: str = None) -> None: """ The constructor of the FinalTimeEndOfRunEventHandler class. Parameters ---------- end_of_run_time : float The event time at which the run is ended. output_handler : str or None, optional The name of the output handler. Raises ------ base.exceptions.ConfigurationError If the end of run time is not greater than or equal to zero (the latter case logs a warning). """ log_init_arguments(logging.getLogger(__name__).debug, self.__class__.__name__, end_of_run_time=end_of_run_time, output_handler=output_handler) super().__init__(output_handler=output_handler) if not end_of_run_time >= 0.0: raise ConfigurationError("The end_of_run_time in the event handler {0} has to be >= 0.0." .format(self.__class__.__name__)) if end_of_run_time == 0.0: logging.getLogger(__name__).warning("The end_of_run_time in the event handler {0} is equal to 0.0. The " "simulation will stop immediately once the run is started." .format(self.__class__.__name__)) self._event_time = Time.from_float(end_of_run_time)
def test_both_leaf_units_active_raises_error(self, random_expovariate_mock, _): self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.0)), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.0)), weight=1) with self.assertRaises(AssertionError): self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two])
def test_send_event_time_to_active_root_unit(self): cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 1.0, 1.5], time_stamp=Time.from_float(1.0)), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 2.0, 3.0], time_stamp=Time.from_float(1.0)), weight=0.5)) self.assertAlmostEqual( self._event_handler_to_root_unit_motion.send_event_time([cnode]), Time.from_float(1.5), places=13)
def test_send_out_state_event_handler_two_cartesian_velocity(self): self._event_handler_two.send_event_time() cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) out_state = self._event_handler_two.send_out_state([cnode]) self.assertEqual(len(out_state), 1) out_state_cnode = out_state[0] self.assertIsNone(out_state_cnode.parent) self.assertEqual(out_state_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(out_state_cnode.value.position, [0.25, 0.2, 0.3], places=13) self.assertEqual(out_state_cnode.weight, 1) self.assertEqual(out_state_cnode.value.velocity, [0.5, 0.0, 0.0]) self.assertAlmostEqual(out_state_cnode.value.time_stamp, Time.from_float(1.3), places=13) self.assertIsNone(out_state_cnode.value.charge) self.assertEqual(len(out_state_cnode.children), 1) child_cnode = out_state_cnode.children[0] self.assertEqual(len(child_cnode.children), 0) self.assertIs(child_cnode.parent, out_state_cnode) self.assertEqual(child_cnode.value.identifier, (0, 0)) self.assertAlmostEqualSequence(child_cnode.value.position, [0.0, 0.8, 0.9], places=13) self.assertEqual(child_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(child_cnode.value.time_stamp, Time.from_float(1.3), places=13) self.assertEqual(child_cnode.value.charge, {"charge": 1.0})
def test_send_out_state_with_charge(self, random_expovariate_mock): self._setUpSendEventTime(random_expovariate_mock, self._potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[-2.0, -3.0], time_stamp=Time.from_float(3.1), charge={"charge": 3.4}), weight=1) self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two]) out_state = self._event_handler_with_charge.send_out_state() # Displacement method was called in event handler's send_event_time method. self.assertEqual( self._potential_mock_with_charge.displacement.call_count, 1) self._potential_mock_with_charge.derivative.assert_not_called() self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.children, []) self.assertEqual(first_cnode.value.identifier, (3, )) self.assertEqual(first_cnode.value.position, [0.5, 0.6]) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.charge, {"charge": -1.2}) self.assertEqual(first_cnode.value.velocity, [-2.0, -3.0]) self.assertAlmostEqual(first_cnode.value.time_stamp, Time.from_float(3.4), places=13) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.children, []) self.assertEqual(second_cnode.value.identifier, (1, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.9, 0.0], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.charge, {"charge": 3.4}) self.assertIsNone(second_cnode.value.velocity) self.assertIsNone(second_cnode.value.time_stamp)
def test_scheduler_simple_ordering(self): # Use not comparable classes as objects which are pushed to the scheduler # since in the application non comparable event handlers are usually pushed not_comparable_instances = [NotComparableClass() for _ in range(5)] times = [Time.from_float(time) for time in [-0.3, 0, -1, 4.3, 8.777]] for index, not_comparable_instance in enumerate( not_comparable_instances): self._scheduler.push_event(times[index], not_comparable_instance) self.assertIs(self._scheduler.get_succeeding_event(), not_comparable_instances[2])
def test_get_succeeding_event_from_empty_scheduler_raises_exception(self): with self.assertRaises(SchedulerError): self._scheduler.get_succeeding_event() not_comparable_instance = NotComparableClass() self._scheduler.push_event(Time.from_float(0.1), not_comparable_instance) self._scheduler.get_succeeding_event() self._scheduler.trash_event(not_comparable_instance) with self.assertRaises(SchedulerError): self._scheduler.get_succeeding_event()
def test_scheduler_same_times(self): not_comparable_instances = [NotComparableClass() for _ in range(5)] times = [Time.from_float(time) for time in [-0.3, 0, -0.3, 4.3, 8.777]] for index, not_comparable_instance in enumerate( not_comparable_instances): self._scheduler.push_event(times[index], not_comparable_instance) # If two objects with the same time are inside of scheduler, we do not care which one is returned self.assertTrue(self._scheduler.get_succeeding_event() is not_comparable_instances[0] or self._scheduler.get_succeeding_event() is not_comparable_instances[2])
def test_send_event_time_leaf_units_in_same_composite_object( self, random_expovariate_mock, _): # Bounding potential returns time displacement 0.3. self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.5, 0.5], time_stamp=Time.from_float(1.8)), weight=1) in_state_one.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.9], velocity=[-1.0, 1.0], time_stamp=Time.from_float(1.8)), weight=0.5)) in_state_two = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.5, 0.5], time_stamp=Time.from_float(1.8)), weight=1) in_state_two.add_child( Node(Unit(identifier=(0, 0), position=[0.1, 0.3]), weight=0.5)) event_time = self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two]) # Should be called with beta random_expovariate_mock.assert_called_once_with(1) self.assertEqual( self._bounding_potential_mock_without_charge.displacement. call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_without_charge.displacement. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-1.0, 1.0], [-0.4, 0.4], 2], places=13, expected_kwargs={}) self.assertAlmostEqual(event_time, Time.from_float(2.1), places=13)
def test_from_float(self): self.assertEqual(Time.from_float(1.23171327853541341).quotient, 1.0) # We can only check twelve places because the input float has this precision. self.assertAlmostEqual(Time.from_float(1.23171327853541341).remainder, 0.23171327853541341, places=12) self.assertEqual(Time.from_float(10.0).quotient, 10.0) self.assertEqual(Time.from_float(10.0).remainder, 0.0) self.assertEqual(Time.from_float(-4528.213891789).quotient, -4529.0) # We can only check nine places because the input float has this precision. self.assertAlmostEqual(Time.from_float(-4528.213891789).remainder, 1.0 - 0.213891789, places=9) self.assertEqual(Time.from_float(float("inf")).quotient, float("inf")) self.assertEqual(Time.from_float(float("inf")).remainder, float("inf")) self.assertEqual( Time.from_float(-float("inf")).quotient, -float("inf")) self.assertEqual( Time.from_float(-float("inf")).remainder, -float("inf"))
def test_send_event_time_to_active_leaf_unit(self): cnode = Node(Unit(identifier=(1, ), position=[0.1, 0.2, 0.3], velocity=[-1.0, -0.3, -0.8], time_stamp=Time.from_float(1.5)), weight=1) cnode.add_child( Node(Unit(identifier=(1, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[-1.0, -0.3, -0.8], time_stamp=Time.from_float(1.5)), weight=0.5)) cnode.add_child( Node(Unit(identifier=(1, 1), position=[0.5, 0.4, 0.3], charge={"charge": -1.0}, velocity=[-1.0, -0.3, -0.8], time_stamp=Time.from_float(1.5)), weight=0.5)) self.assertAlmostEqual( self._event_handler_to_leaf_unit_motion.send_event_time([cnode]), Time.from_float(2.2), places=13)
def test_missing_charges_raises_error(self, random_expovariate_mock, _): self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"other_charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[2.0, 0.0], time_stamp=Time.from_float(1.3), charge={"other_charge": 3.4}), weight=1) with self.assertRaises(KeyError): self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two])
def test_send_out_state_one_leaf_unit_active(self): # Call this to set event time in event handler self._event_handler_one.send_event_time() cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3]), weight=1) out_state = self._event_handler_one.send_out_state(cnode) self.assertEqual(len(out_state), 1) out_state_cnode = out_state[0] self.assertEqual(out_state_cnode.children, []) self.assertIsNone(out_state_cnode.parent) self.assertEqual(out_state_cnode.value.identifier, (0, )) self.assertEqual(out_state_cnode.value.position, [0.1, 0.2, 0.3]) self.assertEqual(out_state_cnode.weight, 1) self.assertEqual(out_state_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(out_state_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertIsNone(out_state_cnode.value.charge)
def test_send_out_state_to_active_leaf_unit(self, random_choice_mock): cnode = Node(Unit(identifier=(1, ), position=[0.1, 0.2, 0.3], velocity=[-1.0, -0.5, -0.3], time_stamp=Time.from_float(1.5)), weight=1) first_child = Node(Unit(identifier=(1, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[-1.0, -0.5, -0.3], time_stamp=Time.from_float(1.5)), weight=0.5) cnode.add_child(first_child) second_child = Node(Unit(identifier=(1, 1), position=[0.5, 0.4, 0.3], charge={"charge": -1.0}, velocity=[-1.0, -0.5, -0.3], time_stamp=Time.from_float(1.5)), weight=0.5) cnode.add_child(second_child) # Call this to update the event time self._event_handler_to_leaf_unit_motion.send_event_time([cnode]) random_choice_mock.return_value = second_child out_state = self._event_handler_to_leaf_unit_motion.send_out_state( [cnode]) random_choice_mock.assert_called_once_with([first_child, second_child]) self.assertEqual(len(out_state), 1) cnode = out_state[0] self.assertIsNone(cnode.parent) self.assertEqual(cnode.weight, 1) self.assertEqual(cnode.value.identifier, (1, )) self.assertAlmostEqualSequence(cnode.value.position, [0.4, 0.85, 0.09], places=13) self.assertEqual(cnode.value.velocity, [-0.5, -0.25, -0.15]) self.assertAlmostEqual(cnode.value.time_stamp, Time.from_float(2.2), places=13) self.assertEqual(len(cnode.children), 2) first_child = cnode.children[0] self.assertIs(first_child.parent, cnode) self.assertEqual(first_child.weight, 0.5) self.assertEqual(first_child.value.identifier, (1, 0)) self.assertAlmostEqualSequence(first_child.value.position, [0.0, 0.45, 0.69], places=13) self.assertIsNone(first_child.value.velocity) self.assertIsNone(first_child.value.time_stamp) self.assertEqual(len(first_child.children), 0) second_child = cnode.children[1] self.assertIs(second_child.parent, cnode) self.assertEqual(second_child.weight, 0.5) self.assertEqual(second_child.value.identifier, (1, 1)) self.assertAlmostEqualSequence(second_child.value.position, [0.8, 0.05, 0.09], places=13) self.assertEqual(second_child.value.velocity, [-1.0, -0.5, -0.3]) self.assertAlmostEqual(second_child.value.time_stamp, Time.from_float(2.2), places=13) self.assertEqual(len(second_child.children), 0)
def test_send_event_time_event_handler_one(self): self.assertAlmostEqual(self._event_handler_one.send_event_time(), Time.from_float(0.5), places=13)
def test_send_event_time_event_handler_two(self): self.assertAlmostEqual(self._event_handler_two.send_event_time(), Time.from_float(1.3), places=13)
def test_send_out_state_leaf_units_in_same_composite_object( self, random_expovariate_mock): self._setUpSendEventTime(random_expovariate_mock, self._potential_mock_without_potential_change) in_state_one = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[0.3, -0.2], time_stamp=Time.from_float(1.2)), weight=1) in_state_one.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.9], velocity=[0.6, -0.4], time_stamp=Time.from_float(1.2)), weight=0.5)) in_state_two = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[0.3, -0.2], time_stamp=Time.from_float(1.2)), weight=1) in_state_two.add_child( Node(Unit(identifier=(0, 0), position=[0.1, 0.3]), weight=0.5)) self._event_handler_without_potential_change.send_event_time( [in_state_one, in_state_two]) out_state = self._event_handler_without_potential_change.send_out_state( ) # Displacement method was called in event handler's send_event_time method. self.assertEqual( self._potential_mock_without_potential_change.displacement. call_count, 1) self._potential_mock_without_potential_change.derivative.assert_not_called( ) self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(first_cnode.value.position, [0.29, 0.74], places=13) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.velocity, [0.3, -0.2]) self.assertAlmostEqual(first_cnode.value.time_stamp, Time.from_float(1.5), places=13) self.assertIsNone(first_cnode.value.charge) self.assertEqual(len(first_cnode.children), 1) first_child = first_cnode.children[0] self.assertIs(first_child.parent, first_cnode) self.assertEqual(first_child.children, []) self.assertEqual(first_child.value.identifier, (0, 1)) self.assertAlmostEqualSequence(first_child.value.position, [0.68, 0.78], places=13) self.assertEqual(first_child.weight, 0.5) self.assertIsNone(first_child.value.velocity) self.assertIsNone(first_child.value.time_stamp) self.assertIsNone(first_child.value.charge) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.29, 0.74], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.velocity, [0.3, -0.2]) self.assertAlmostEqual(second_cnode.value.time_stamp, Time.from_float(1.5), places=13) self.assertIsNone(second_cnode.value.charge) self.assertEqual(len(second_cnode.children), 1) second_child = second_cnode.children[0] self.assertIs(second_child.parent, second_cnode) self.assertEqual(second_child.children, []) self.assertEqual(second_child.value.identifier, (0, 0)) self.assertEqual(second_child.value.position, [0.1, 0.3]) self.assertEqual(second_child.weight, 0.5) self.assertEqual(second_child.value.velocity, [0.6, -0.4]) self.assertAlmostEqual(second_child.value.time_stamp, Time.from_float(1.5), places=13) self.assertIsNone(second_child.value.charge)
def test_send_out_state_leaf_units_in_same_composite_object_reject( self, random_expovariate_mock, random_uniform_mock): # Bounding potential returns time displacement 0.3. self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.5, 0.5], time_stamp=Time.from_float(1.8)), weight=1) in_state_one.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.9], velocity=[-1.0, 1.0], time_stamp=Time.from_float(1.8)), weight=0.5)) in_state_two = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.5, 0.5], time_stamp=Time.from_float(1.8)), weight=1) in_state_two.add_child( Node(Unit(identifier=(0, 0), position=[0.1, 0.3]), weight=0.5)) self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two]) self._setUpSendOutStateReject( random_uniform_mock, self._bounding_potential_mock_without_charge, self._potential_mock_without_charge) out_state = self._event_handler_without_charge.send_out_state() # New position of active leaf unit is [0.2, 0.2]. self.assertEqual( self._bounding_potential_mock_without_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_without_charge.derivative. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-1.0, 1.0], [-0.1, 0.1]], places=13, expected_kwargs={}) self.assertEqual( self._potential_mock_without_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._potential_mock_without_charge.derivative.call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-1.0, 1.0], [-0.1, 0.1]], places=13, expected_kwargs={}) random_uniform_mock.assert_called_once_with(0, 0.7) self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(first_cnode.value.position, [0.05, 0.95], places=13) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.velocity, [-0.5, 0.5]) self.assertAlmostEqual(first_cnode.value.time_stamp, Time.from_float(2.1), places=13) self.assertIsNone(first_cnode.value.charge) self.assertEqual(len(first_cnode.children), 1) first_child = first_cnode.children[0] self.assertIs(first_child.parent, first_cnode) self.assertEqual(first_child.children, []) self.assertEqual(first_child.value.identifier, (0, 1)) self.assertAlmostEqualSequence(first_child.value.position, [0.2, 0.2], places=13) self.assertEqual(first_child.weight, 0.5) self.assertEqual(first_child.value.velocity, [-1.0, 1.0]) self.assertAlmostEqual(first_child.value.time_stamp, Time.from_float(2.1), places=13) self.assertIsNone(first_child.value.charge) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.05, 0.95], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.velocity, [-0.5, 0.5]) self.assertAlmostEqual(second_cnode.value.time_stamp, Time.from_float(2.1), places=13) self.assertIsNone(second_cnode.value.charge) self.assertEqual(len(second_cnode.children), 1) second_child = second_cnode.children[0] self.assertIs(second_child.parent, second_cnode) self.assertEqual(second_child.children, []) self.assertEqual(second_child.value.identifier, (0, 0)) self.assertEqual(second_child.value.position, [0.1, 0.3]) self.assertEqual(second_child.weight, 0.5) self.assertIsNone(second_child.value.velocity) self.assertIsNone(second_child.value.time_stamp) self.assertIsNone(second_child.value.charge)
def test_send_out_state_with_charge_reject(self, random_expovariate_mock, random_uniform_mock): # Bounding potential returns time displacement 0.3. self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[-2.0, -3.0], time_stamp=Time.from_float(1.1), charge={"charge": 3.4}), weight=1) self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two]) self._setUpSendOutStateReject( random_uniform_mock, self._bounding_potential_mock_with_charge, self._potential_mock_with_charge) out_state = self._event_handler_with_charge.send_out_state() # New position of active leaf unit is [0.9, 1.0]. self.assertEqual( self._bounding_potential_mock_with_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_with_charge.derivative. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-2.0, -3.0], [-0.4, -0.4], -1.2, 3.4], places=13, expected_kwargs={}) self.assertEqual( self._potential_mock_with_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._potential_mock_with_charge.derivative.call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-2.0, -3.0], [-0.4, -0.4], -1.2, 3.4], places=13, expected_kwargs={}) random_uniform_mock.assert_called_once_with(0, 0.7) self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.children, []) self.assertEqual(first_cnode.value.identifier, (3, )) self.assertEqual(first_cnode.value.position, [0.5, 0.6]) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.charge, {"charge": -1.2}) self.assertIsNone(first_cnode.value.velocity) self.assertIsNone(first_cnode.value.time_stamp) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.children, []) self.assertEqual(second_cnode.value.identifier, (1, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.9, 0.0], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.charge, {"charge": 3.4}) self.assertEqual(second_cnode.value.velocity, [-2.0, -3.0]) self.assertAlmostEqual(second_cnode.value.time_stamp, Time.from_float(1.4), places=13)