Example #1
0
    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])
Example #2
0
 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])
Example #3
0
 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])
Example #4
0
    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 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)
Example #6
0
    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')))
Example #7
0
 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])
Example #8
0
 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])
Example #9
0
 def test_update(self):
     time = Time(3.0, 0.3247823)
     other_time = Time(6.0, 0.1231)
     time.update(other_time)
     self.assertEqual(time.quotient, 6.0)
     self.assertEqual(time.remainder, 0.1231)
     self.assertEqual(other_time.quotient, 6.0)
     self.assertEqual(other_time.remainder, 0.1231)
     self.assertIsNot(time, other_time)
Example #10
0
    def test_insert_into_global_state_branch_of_leaf_unit(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(0,), position=[0.2, 0.3], charge=None,
                           velocity=[0.5, 0], time_stamp=Time(0.0, 0.3)), weight=1)
        branch.add_child(Node(Unit(identifier=(0, 1), position=[0.4, 0.6],
                                   charge={"e": -1}, velocity=[1, 0], time_stamp=Time(0.0, 0.3)), weight=0.5))
        self._state_handler.insert_into_global_state([branch])
        all_root_cnodes = self._state_handler.extract_global_state()

        self.assertEqual(len(all_root_cnodes), 2)
        unit = all_root_cnodes[0].value
        self.assertEqual(unit.position, [0.2, 0.3])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (0,))
        self.assertEqual(unit.velocity, [0.5, 0])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.3))
        self.assertEqual(len(all_root_cnodes[0].children), 2)

        unit = all_root_cnodes[0].children[0].value
        self.assertEqual(unit.position, [0, 0])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (0, 0))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children[0].children), 0)

        unit = all_root_cnodes[0].children[1].value
        self.assertEqual(unit.position, [0.4, 0.6])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (0, 1))
        self.assertEqual(unit.velocity, [1, 0])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.3))
        self.assertEqual(len(all_root_cnodes[0].children[1].children), 0)

        unit = all_root_cnodes[1].value
        self.assertEqual(unit.position, [0.9, 0.775])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (1,))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[1].children), 2)

        unit = all_root_cnodes[1].children[0].value
        self.assertEqual(unit.position, [0.9, 0.8])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (1, 0))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[1].children[0].children), 0)

        unit = all_root_cnodes[1].children[1].value
        self.assertEqual(unit.position, [0.9, 0.75])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (1, 1))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[1].children[1].children), 0)
Example #11
0
    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)
Example #12
0
 def test_extract_from_global_state_copied_time_stamp(self):
     self._state_handler.initialize(self._root_nodes)
     branch = Node(Unit(identifier=(0,), position=[0.2, 0.3], charge=None,
                        velocity=[0.5, 0], time_stamp=Time(0.0, 0.3)), weight=1)
     branch.add_child(Node(Unit(identifier=(0, 1), position=[0.4, 0.6],
                                charge={"e": -1}, velocity=[1, 0], time_stamp=Time(0.0, 0.3)), weight=0.5))
     self._state_handler.insert_into_global_state([branch])
     root_cnode = self._state_handler.extract_from_global_state((0, 1))
     time_stamp = root_cnode.value.time_stamp
     time_stamp.update(Time(1.0, 0.2))
     all_root_cnodes = self._state_handler.extract_global_state()
     self.assertEqual(all_root_cnodes[0].value.time_stamp, Time(0.0, 0.3))
Example #13
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})
Example #14
0
 def test_insert_into_global_state_velocity_none_but_time_stamp_not_none_raises_error(self):
     self._state_handler.initialize(self._root_nodes)
     branch = Node(Unit(identifier=(0,), position=[0.2, 0.3], charge=None,
                        velocity=[0.5, 0], time_stamp=Time(0.0, 0.3)), weight=1)
     branch.add_child(Node(Unit(identifier=(0, 1), position=[0.4, 0.6], charge={"e": -1},
                                velocity=[1, 0], time_stamp=Time(0.0, 0.3)), weight=0.5))
     self._state_handler.insert_into_global_state([branch])
     branch = Node(Unit(identifier=(0,), position=[0.1, 0.2], charge=None,
                        velocity=None, time_stamp=Time(0.0, 0.5)), weight=1)
     branch.add_child(Node(Unit(identifier=(0, 1), position=[0.3, 0.4],
                                charge={"e": -1}, velocity=None, time_stamp=Time(0.0, 0.5)), weight=0.5))
     branch.add_child(Node(Unit(identifier=(0, 0), position=[0.5, 0.6],
                                charge={"e": 1}, velocity=None, time_stamp=Time(0.0, 0.5)), weight=0.5))
     with self.assertRaises(AssertionError):
         self._state_handler.insert_into_global_state([branch])
Example #15
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, dumping_interval: float, output_handler: str) -> None:
        """
        The constructor of the FixedIntervalDumpingEventHandler class.

        Parameters
        ----------
        dumping_interval : float
            The time interval of the dumping.
        output_handler :
            The name of the output handler.

        Raises
        ------
        base.exceptions.ConfigurationError:
            If the dumping interval is not greater than zero.
        """
        log_init_arguments(logging.getLogger(__name__).debug,
                           self.__class__.__name__,
                           dumping_interval=dumping_interval,
                           output_handler=output_handler)
        super().__init__(output_handler=output_handler)
        if not dumping_interval > 0.0:
            raise ConfigurationError(
                "The dumping_interval in the event handler {0} has to be > 0.0."
                .format(self.__class__.__name__))
        self._dumping_interval = dumping_interval
        self._event_time = Time(0.0, 0.0)
Example #17
0
    def __init__(self, warn_on_equal_event_times: bool = False) -> None:
        """
        The constructor of the HeapScheduler class.

        Parameters
        ----------
        warn_on_equal_event_times : bool, optional
            Whether this scheduler should log a warning when succeeding event times are equal.

        Raises
        ------
        MemoryError
            If the C code fails to allocate memory.
        """
        self._logger = logging.getLogger(__name__)
        self._logger_enabled_for_debug = self._logger.isEnabledFor(
            logging.DEBUG)
        log_init_arguments(self._logger.debug, self.__class__.__name__)
        super().__init__()
        c_heap = lib.construct_heap()
        if c_heap == ffi.NULL:
            raise MemoryError(
                "Could not allocate memory for the class {0}.".format(
                    self.__class__.__name__))
        self._heap = ffi.gc(c_heap,
                            lib.destroy_heap,
                            size=lib.estimated_size(c_heap))
        self._minimal_valid_counter = {}
        self._last_returned_event = (Time(-float("inf"), -float("inf")), None)
        self._allocated_memory_bytes = 0
        self._scheduler_handle = _new_handle(self)
        self._event_handler_handles = {}
        self._warn_on_equal_event_times = warn_on_equal_event_times
    def send_out_state(self, cnode_with_initially_active_unit: Node) -> Sequence[Node]:
        """
        Return the out-state.

        This method receives the branch of the initially active unit.

        Parameters
        ----------
        cnode_with_initially_active_unit : base.node.Node
            The branch of the initially active unit.

        Returns
        -------
        Sequence[base.node.Node]
            The out-state.
        """
        self._store_in_state([cnode_with_initially_active_unit])
        self._construct_leaf_cnodes()

        for leaf_cnode in self._leaf_cnodes:
            unit = leaf_cnode.value
            assert unit.velocity is None
            assert unit.time_stamp is None
            unit.velocity = self._initial_velocity.copy()
            unit.time_stamp = Time(0.0, 0.0)
            self._register_velocity_change_leaf_cnode(leaf_cnode, self._initial_velocity)

        self._commit_non_leaf_velocity_changes()
        return self._state
    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)
Example #20
0
 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])
Example #21
0
 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_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)
Example #23
0
    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})
Example #24
0
 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])
Example #25
0
 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()
Example #26
0
 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])
Example #27
0
    def test_almost_equal_comparison(self):
        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653266)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653367)
        self.assertAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233654267)
        self.assertNotAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924377.0, 0.3216781233653267)
        self.assertNotAlmostEqual(time, other_time)
Example #28
0
    def test_extract_active_global_state_root_node_active(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(1,), position=[0.2, 0.3], charge=None,
                           velocity=[0.1, 0.2], time_stamp=Time(0.0, 0.0)), weight=1)
        branch.add_child(Node(Unit(identifier=(1, 0), position=[0.4, 0.6], charge={"e": -1},
                                   velocity=[0.6, -0.1], time_stamp=Time(-1.0, 0.8)), weight=0.5))
        branch.add_child(Node(Unit(identifier=(1, 1), position=[0.4, 0.6], charge={"e": -1},
                                   velocity=[0.6, -0.1], time_stamp=Time(-1.0, 0.8)), weight=0.5))
        self._state_handler.insert_into_global_state([branch])

        branches = self._state_handler.extract_active_global_state()
        self.assertEqual(len(branches), 1)
        info = branches[0].value
        self.assertEqual(info.position, [0.2, 0.3])
        self.assertEqual(info.charge, None)
        self.assertEqual(info.identifier, (1,))
        self.assertEqual(info.velocity, [0.1, 0.2])
        self.assertEqual(info.time_stamp, Time(0.0, 0.0))
        self.assertEqual(len(branches[0].children), 2)
        info = branches[0].children[0].value
        self.assertEqual(info.position, [0.4, 0.6])
        self.assertEqual(info.charge, {"e": 1})
        self.assertEqual(info.identifier, (1, 0))
        self.assertEqual(info.velocity, [0.6, -0.1])
        self.assertEqual(info.time_stamp, Time(-1.0, 0.8))
        self.assertEqual(len(branches[0].children[0].children), 0)
        info = branches[0].children[1].value
        self.assertEqual(info.position, [0.4, 0.6])
        self.assertEqual(info.charge, {"e": -1})
        self.assertEqual(info.identifier, (1, 1))
        self.assertEqual(info.velocity, [0.6, -0.1])
        self.assertEqual(info.time_stamp, Time(-1.0, 0.8))
        self.assertEqual(len(branches[0].children[1].children), 0)
Example #29
0
    def test_insert_into_global_state_root_branch(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(1,), position=[0.5, 0.6], charge=None,
                           velocity=[1, 1], time_stamp=Time(0.0, 0.2)), weight=1)
        # There are no consistency checks within the state handler
        branch.add_child(Node(Unit(identifier=(1, 0), position=[0.4, 0.6], charge={"e": 1},
                                   velocity=[-0.3, 2.1], time_stamp=Time(0.0, 0.53)), weight=0.5))
        branch.add_child(Node(Unit(identifier=(1, 1), position=[0.1, 0.2], charge={"e": -1},
                                   velocity=[0.7, 0.7], time_stamp=Time(0.0, 0.1)), weight=0.5))
        self._state_handler.insert_into_global_state([branch])

        all_root_cnodes = self._state_handler.extract_global_state()
        self.assertEqual(len(all_root_cnodes), 2)
        unit = all_root_cnodes[0].value
        self.assertEqual(unit.position, [0.05, 0.025])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (0,))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children), 2)

        unit = all_root_cnodes[0].children[0].value
        self.assertEqual(unit.position, [0, 0])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (0, 0))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children[0].children), 0)

        unit = all_root_cnodes[0].children[1].value
        self.assertEqual(unit.position, [0.1, 0.05])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (0, 1))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children[1].children), 0)

        unit = all_root_cnodes[1].value
        self.assertEqual(unit.position, [0.5, 0.6])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (1,))
        self.assertEqual(unit.velocity, [1, 1])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.2))
        self.assertEqual(len(all_root_cnodes[1].children), 2)

        unit = all_root_cnodes[1].children[0].value
        self.assertEqual(unit.position, [0.4, 0.6])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (1, 0))
        self.assertEqual(unit.velocity, [-0.3, 2.1])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.53))
        self.assertEqual(len(all_root_cnodes[1].children[0].children), 0)

        unit = all_root_cnodes[1].children[1].value
        self.assertEqual(unit.position, [0.1, 0.2])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (1, 1))
        self.assertEqual(unit.velocity, [0.7, 0.7])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.1))
        self.assertEqual(len(all_root_cnodes[1].children[1].children), 0)
Example #30
0
    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)