def test_send_out_state_leaf_units_initial_direction_one(self):
        # Call this to update the event time
        self._event_handler.send_event_time()
        active_cnode_one = Node(Unit(identifier=(0,), position=[0.1, 0.2, 0.3],
                                     velocity=[0.0, 1.0, 0.0], time_stamp=0.0), weight=1)
        active_cnode_two = Node(Unit(identifier=(3,), position=[0.7, 0.8, 0.9],
                                     velocity=[0.0, 1.0, 0.0], time_stamp=0.3), weight=1)
        out_state = self._event_handler.send_out_state([active_cnode_one, active_cnode_two], [])

        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, (0,))
        self.assertEqual(first_cnode.value.position, [0.1, 0.2 + 0.7, 0.3])
        self.assertEqual(first_cnode.weight, 1)
        self.assertEqual(first_cnode.value.velocity, [0.0, 0.0, 1.0])
        self.assertEqual(first_cnode.value.time_stamp, 0.7)
        self.assertIsNone(first_cnode.value.charge)

        second_cnode = out_state[1]
        self.assertIsNone(second_cnode.parent)
        self.assertEqual(second_cnode.children, [])
        self.assertEqual(second_cnode.value.identifier, (3,))
        self.assertEqual(second_cnode.value.position, [0.7, (0.8 + (0.7 - 0.3)*1.0) % 1.0, 0.9])
        self.assertEqual(second_cnode.weight, 1)
        self.assertEqual(second_cnode.value.velocity, [0.0, 0.0, 1.0])
        self.assertEqual(second_cnode.value.time_stamp, 0.7)
        self.assertIsNone(second_cnode.value.charge)
Esempio n. 2
0
    def add_node(self, prev, worker, gstate, do_hash=True):
        if do_hash:
            hash = gstate.compute_hash()
            if hash is not None:
                node = self.statespace.get_node_by_hash(hash)
                if node is not None:
                    return (node, False)
        else:
            hash = None
        uid = str(self.statespace.nodes_count)
        node = Node(uid, hash)
        logging.debug("New node %s", node.uid)

        if prev:
            node.prev = prev
        self.statespace.add_node(node)

        if self.debug_compare_states is not None \
                and node.uid in self.debug_compare_states:
            if self.debug_captured_states is None:
                self.debug_captured_states = []
            logging.debug("Capturing %s", node)
            self.debug_captured_states.append((gstate.copy(), worker))

        if self.statespace.nodes_count > self.max_states:
            logging.info("Maximal number of states reached")
            if self.debug_compare_states is not None:
                self.debug_compare()
            raise ErrorFound()

        if self.debug_state == uid:
            context = Context(self, node, None)
            context.add_error_and_throw(
                errormsg.StateCaptured(context, uid=uid))
        return (node, True)
Esempio n. 3
0
    def test_extract_active_global_state_leaf_unit_active(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(0, ),
                           position=[0.2, 0.3],
                           charge=None,
                           velocity=[0.2, 0],
                           time_stamp=0.1),
                      weight=1)
        branch.add_child(
            Node(Unit(identifier=(0, 0),
                      position=[0.4, 0.6],
                      charge={"e": -1},
                      velocity=[0.6, -0.1],
                      time_stamp=0.2),
                 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, (0, ))
        self.assertEqual(info.velocity, [0.2, 0])
        self.assertEqual(info.time_stamp, 0.1)
        self.assertEqual(len(branches[0].children), 1)
        info = branches[0].children[0].value
        self.assertEqual(info.position, [0.4, 0.6])
        self.assertEqual(info.charge, {"e": 1})
        self.assertEqual(info.identifier, (0, 0))
        self.assertEqual(info.velocity, [0.6, -0.1])
        self.assertEqual(info.time_stamp, 0.2)
        self.assertEqual(len(branches[0].children[0].children), 0)
Esempio n. 4
0
    def test_send_out_state_one(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=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=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.assertEqual(first_active_cnode.value.position,
                         [0.1 + 0.5 * 0.1, 0.2 + 0.5 * 0.2, 0.3 + 0.5 * 0.3])
        self.assertEqual(first_active_cnode.weight, 1)
        self.assertEqual(first_active_cnode.value.velocity, [0.1, 0.2, 0.3])
        self.assertEqual(first_active_cnode.value.time_stamp, 0.5)
        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.assertEqual(second_active_cnode.value.position,
                         [0.4 + 0.3 * 1.0, 0.5 + 0.3 * 0.6, 0.6 + 0.3 * 0.5])
        self.assertEqual(second_active_cnode.weight, 1)
        self.assertEqual(second_active_cnode.value.velocity, [1.0, 0.6, 0.5])
        self.assertEqual(second_active_cnode.value.time_stamp, 0.5)
        self.assertIsNone(second_active_cnode.value.charge)
Esempio n. 5
0
    def test_send_out_state_two(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=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=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.assertEqual(out_state_cnode.value.position, [0.1 + 0.5 * 0.3, 0.2, 0.3])
        self.assertEqual(out_state_cnode.weight, 1)
        self.assertEqual(out_state_cnode.value.velocity, [0.5, 0.0, 0.0])
        self.assertEqual(out_state_cnode.value.time_stamp, 1.3)
        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.assertEqual(child_cnode.value.position, [0.0, 0.8, 0.9])
        self.assertEqual(child_cnode.value.velocity, [1.0, 0.0, 0.0])
        self.assertEqual(child_cnode.value.time_stamp, 1.3)
        self.assertEqual(child_cnode.value.charge, {"charge": 1.0})
Esempio n. 6
0
    def test_infinite_displacement_returns_none(self, random_expovariate_mock,
                                                random_uniform_mock):
        self._setUpSendEventTimeInfiniteDisplacement(
            random_expovariate_mock,
            self._cell_bounding_potential_mock_with_charge, [0.5, 0.9],
            [0.5, 0.6])
        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=1.3,
                                 charge={"charge": 3.4}),
                            weight=1)

        self._event_handler_with_charge.send_event_time(
            [in_state_one, in_state_two])
        self._setUpSendOutStateAccept(
            random_uniform_mock,
            self._cell_bounding_potential_mock_with_charge,
            self._potential_mock_with_charge)
        out_state = self._event_handler_with_charge.send_out_state()
        self.assertIsNone(out_state)
        self._potential_mock_with_charge.derivative.assert_not_called()
        random_uniform_mock.assert_not_called()
        self._cell_bounding_potential_mock_with_charge.derivative.assert_not_called(
        )
Esempio n. 7
0
    def test_send_event_time_without_charge(self, random_expovariate_mock, _):
        self._setUpSendEventTime(
            random_expovariate_mock,
            self._cell_bounding_potential_mock_without_charge, [0.5, 0.9],
            [0.1, 0.3])
        in_state_one = Node(Unit(identifier=(0, ),
                                 position=[0.2, 0.8],
                                 velocity=[0.0, 0.5],
                                 time_stamp=1.3),
                            weight=1)
        in_state_one.add_child(
            Node(Unit(identifier=(0, 2),
                      position=[0.5, 0.9],
                      velocity=[0.0, 1.0],
                      time_stamp=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)
        # Arguments are checked in _setUpSendEventTime
        self.assertEqual(self._cells_mock.position_to_cell.call_count, 2)
        self._cells_mock.excluded_cells.assert_called_once_with(4)
        self._cells_mock.relative_cell.assert_called_once_with(12, 4)
        self._cell_bounding_potential_mock_without_charge.displacement.assert_called_once_with(
            1, 8, 2)
        self.assertAlmostEqual(event_time, 1.6)
Esempio n. 8
0
    def test_send_event_time_with_charge(self, random_expovariate_mock, _):
        self._setUpSendEventTime(
            random_expovariate_mock,
            self._cell_bounding_potential_mock_with_charge, [0.5, 0.9],
            [0.5, 0.6])
        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=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._cells_mock.position_to_cell.call_count, 2)
        self._cells_mock.excluded_cells.assert_called_once_with(4)
        self._cells_mock.relative_cell.assert_called_once_with(12, 4)
        self._cell_bounding_potential_mock_with_charge.displacement.assert_called_once_with(
            0, 8, -1.2, 3.4, 2)
        self.assertAlmostEqual(event_time, 1.45)
 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=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=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=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=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_send_event_time_without_charge(self, random_expovariate_mock, _):
        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.0, 0.5],
                                 time_stamp=1.2),
                            weight=1)
        in_state_one.add_child(
            Node(Unit(identifier=(0, 2),
                      position=[0.5, 0.9],
                      velocity=[0.0, 1.0],
                      time_stamp=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._bounding_potential_mock_without_charge.displacement.assert_called_once_with(
            1, [(0.1 - 0.5 + 0.5) % 1.0 - 0.5, (0.3 - 0.9 + 0.5) % 1.0 - 0.5],
            2)
        self.assertAlmostEqual(event_time, 1.6)
 def test_active_node_not_moving_raises_error(self):
     self._event_handler.send_event_time()
     cnode_with_active_unit = Node(Unit(identifier=(0,), position=[0.1, 0.2, 0.3],
                                        velocity=[1.0, 0.0, 0.0], time_stamp=0.0), weight=1)
     cnode_with_non_active_unit = Node(Unit(identifier=(3,), position=[0.4, 0.5, 0.6]), weight=1)
     with self.assertRaises(AssertionError):
         self._event_handler.send_out_state([cnode_with_active_unit, cnode_with_non_active_unit], [])
    def test_send_out_state_leaf_unit_branch(self):
        self._event_handler.send_event_time()

        cnode = Node(Unit(identifier=(0,), position=[0.1, 0.2, 0.3],
                          velocity=[0.25, 0.0, 0.0], time_stamp=0.5), weight=1)
        cnode.add_child(Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0},
                                  velocity=[0.5, 0.0, 0.0], time_stamp=0.4), weight=0.5))
        out_state = self._event_handler.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.7 - 0.5) * 0.25, 0.2, 0.3])
        self.assertEqual(out_state_cnode.weight, 1)
        self.assertEqual(out_state_cnode.value.velocity, [0.0, 0.25, 0.0])
        self.assertEqual(out_state_cnode.value.time_stamp, 0.7)
        self.assertIsNone(out_state_cnode.value.charge)

        self.assertEqual(len(out_state_cnode.children), 1)
        child_cnode = out_state_cnode.children[0]
        self.assertIs(child_cnode.parent, out_state_cnode)
        self.assertEqual(child_cnode.children, [])
        self.assertEqual(child_cnode.value.identifier, (0, 0))
        self.assertEqual(child_cnode.value.position, [(0.7 + (0.7 - 0.4) * 0.5) % 1.0, 0.8, 0.9])
        self.assertEqual(child_cnode.weight, 0.5)
        self.assertEqual(child_cnode.value.velocity, [0.0, 0.5, 0.0])
        self.assertEqual(child_cnode.value.time_stamp, 0.7)
        self.assertEqual(child_cnode.value.charge, {"charge": 1.0})
Esempio n. 13
0
    def test_active_cell_changed_returns_none(self, random_expovariate_mock,
                                              random_uniform_mock):
        self._setUpSendEventTime(
            random_expovariate_mock,
            self._cell_bounding_potential_mock_with_charge, [0.5, 0.9],
            [0.5, 0.6])
        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=1.3,
                                 charge={"charge": 3.4}),
                            weight=1)

        self._event_handler_with_charge.send_event_time(
            [in_state_one, in_state_two])
        self._setUpSendOutStateActiveCellChanged(
            random_uniform_mock,
            self._cell_bounding_potential_mock_with_charge,
            self._potential_mock_with_charge)
        out_state = self._event_handler_with_charge.send_out_state()
        self.assertIsNone(out_state)
        self._potential_mock_with_charge.derivative.assert_not_called()
        random_uniform_mock.assert_not_called()
        self._cell_bounding_potential_mock_with_charge.derivative.assert_not_called(
        )
        self._cells_mock.position_to_cell.assert_has_calls(
            [mock.call([(0.5 + (1.45 - 1.3) * 2.0) % 1.0, 0.9])])
Esempio n. 14
0
    def test_send_out_state_cell_for_composite_objects_leaf_unit_active(self):
        self._setUpCellForCompositeObjectsLeafUnitActive()
        in_state = Node(Unit(identifier=(0, ),
                             position=[0.2, 0.8],
                             velocity=[0.0, 0.5],
                             time_stamp=1.4),
                        weight=1)
        in_state.add_child(
            Node(Unit(identifier=(0, 2),
                      position=[0.5, 0.9],
                      velocity=[0.0, 1.0],
                      time_stamp=1.3),
                 weight=0.5))
        self._event_handler.send_event_time([in_state])
        out_state = self._event_handler.send_out_state()
        self.assertEqual(len(out_state), 1)
        cnode = out_state[0]
        self.assertIsNone(cnode.parent)
        self.assertEqual(cnode.value.identifier, (0, ))
        self.assertEqual(cnode.value.position, [0.2, 0.0])
        self.assertEqual(cnode.weight, 1)
        self.assertIsNone(cnode.value.charge)
        self.assertEqual(cnode.value.velocity, [0.0, 0.5])
        self.assertAlmostEqual(cnode.value.time_stamp, 1.8, places=13)

        self.assertEqual(len(cnode.children), 1)
        child_cnode = cnode.children[0]
        self.assertIs(child_cnode.parent, cnode)
        self.assertEqual(child_cnode.children, [])
        self.assertEqual(child_cnode.value.identifier, (0, 2))
        self.assertEqual(child_cnode.value.position, [0.5, (0.9 + 0.5) % 1.0])
        self.assertEqual(child_cnode.weight, 0.5)
        self.assertIsNone(child_cnode.value.charge)
        self.assertEqual(child_cnode.value.velocity, [0.0, 1.0])
        self.assertAlmostEqual(child_cnode.value.time_stamp, 1.8, places=13)
    def test_send_out_state_two(self):
        self._event_handler_two.send_event_time()

        cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3]),
                     weight=1)
        cnode.add_child(
            Node(Unit(identifier=(0, 1),
                      position=[0.4, 0.5, 0.6],
                      charge={"charge": 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.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, [0.0, 0.0, 0.25])
        self.assertEqual(out_state_cnode.value.time_stamp, 0.0)
        self.assertIsNone(out_state_cnode.value.charge)

        self.assertEqual(len(out_state_cnode.children), 1)
        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, 1))
        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, [0.0, 0.0, 0.5])
        self.assertEqual(first_child_cnode.value.time_stamp, 0.0)
        self.assertEqual(first_child_cnode.value.charge, {"charge": 1.0})
 def test_more_than_one_velocity_component_raises_error(self):
     self._event_handler.send_event_time()
     active_cnode_one = Node(Unit(identifier=(0,), position=[0.1, 0.2, 0.3],
                                  velocity=[1.0, 0.2, 0.3], time_stamp=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=0.2), weight=1)
     with self.assertRaises(AssertionError):
         self._event_handler.send_out_state([active_cnode_one, active_cnode_two], [])
 def test_no_leaf_unit_active_raises_error(self, random_expovariate_mock):
     self._setUpSendEventTime(random_expovariate_mock,
                              self._potential_mock_without_charge)
     in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6]),
                         weight=1)
     in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9]),
                         weight=1)
     with self.assertRaises(AssertionError):
         self._event_handler_without_charge.send_event_time(
             [in_state_one, in_state_two])
Esempio n. 18
0
    def test_send_out_state_with_charge_reject(self, random_expovariate_mock,
                                               random_uniform_mock):
        self._setUpSendEventTime(
            random_expovariate_mock,
            self._cell_bounding_potential_mock_with_charge, [0.5, 0.9],
            [0.5, 0.6])
        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=1.3,
                                 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._cell_bounding_potential_mock_with_charge,
            self._potential_mock_with_charge)
        out_state = self._event_handler_with_charge.send_out_state()
        new_position = (0.5 + (1.45 - 1.3) * 2.0) % 1.0
        self._cell_bounding_potential_mock_with_charge.derivative.assert_called_once_with(
            0, 8, -1.2, 3.4)
        self._potential_mock_with_charge.derivative.assert_called_once_with(
            0, [(0.5 - new_position + 0.5) % 1.0 - 0.5,
                (0.6 - 0.9 + 0.5) % 1.0 - 0.5], -1.2, 3.4)
        random_uniform_mock.assert_called_once_with(0, 0.7)
        self._cells_mock.position_to_cell.assert_has_calls(
            [mock.call([new_position, 0.9])])

        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.assertEqual(second_cnode.value.position,
                         [(0.5 + (1.45 - 1.3) * 2.0) % 1.0, 0.9])
        self.assertEqual(second_cnode.weight, 1)
        self.assertEqual(second_cnode.value.charge, {"charge": 3.4})
        self.assertEqual(second_cnode.value.velocity, [2.0, 0.0])
        self.assertEqual(second_cnode.value.time_stamp, 1.45)
 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=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=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=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])
Esempio n. 20
0
    def fill_root_node(self, node: Node) -> None:
        """
        Fill the root node with a random water composite object.

        Parameters
        ----------
        node : base.node.Node
            The root node.
        """
        molecule_center = setting.random_position()
        particles = self._create_random_water_molecule(molecule_center)
        for particle in particles:
            node.add_child(Node(particle))
        node.value = Particle(position=molecule_center)
    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=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=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=1.0),
                 weight=0.5))
        with self.assertRaises(AssertionError):
            self._event_handler_to_root_unit_motion.send_out_state([cnode])
 def populate_grid(self):
     '''
     Set/reset all nodes to new nodes with no state and populate neighbor
     nodes. Should only be used by initialization and set routines.
     '''
     for x in range(self.x_size):
         for y in range(self.y_size):
             self.grid[x, y] = Node()
             self.grid[x, y].position = (x, y)
     # Populate node neighbor lists
     for x in range(self.x_size):
         for y in range(self.y_size):
             for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                 nx = x + dx
                 ny = y + dy
                 if nx >= 0 and nx < self.x_size and \
                     ny >= 0 and ny < self.y_size:
                     self.grid[x, y].neighbors.append((self.grid[nx,
                                                                 ny], 1))
                 elif self.wrap:
                     if nx < 0:
                         nx = self.x_size - 1
                     elif nx >= self.x_size:
                         nx = 0
                     if ny < 0:
                         ny = self.y_size - 1
                     elif ny >= self.y_size:
                         ny = 0
                     self.grid[x, y].neighbors.append((self.grid[nx,
                                                                 ny], 1))
 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, 0.0, 0.0],
                       time_stamp=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=1.0),
              weight=0.5))
     self.assertEqual(
         self._event_handler_to_root_unit_motion.send_event_time([cnode]),
         1.5)
Esempio n. 24
0
 def test_more_than_one_in_state_raises_error(self):
     self._setUpCellNoCompositeObjectsLeafUnitActive()
     in_state = Node(Unit(identifier=(1, ),
                          position=[0.1, 0.2],
                          charge={"charge": 1.0},
                          velocity=[0.5, 0.0],
                          time_stamp=0.3),
                     weight=1)
     in_state_two = Node(Unit(identifier=(3, ),
                              position=[0.2, 0.3],
                              charge={"charge": 1.0},
                              velocity=[0.5, 0.0],
                              time_stamp=0.3),
                         weight=1)
     with self.assertRaises(AssertionError):
         self._event_handler.send_event_time([in_state, in_state_two])
 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=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])
Esempio n. 26
0
    def _construct_cnode_with_all_children_cnodes(
            self, starting_node: Node, starting_identifier: StateId,
            copy_method: Callable[[Sequence[float]], Sequence[float]] = lambda vector: vector) -> Node:
        """
        Construct the cnode for the given node and add all children after converting them to cnodes.

        The copy method will be applied to the position and the velocity. Per default this method does not copy them.
        """
        velocity, time_stamp = self._lifting_state.get(starting_identifier)
        unit = Unit(starting_identifier, copy_method(starting_node.value.position), starting_node.value.charge,
                    copy_method(velocity), time_stamp)
        cnode = Node(unit, starting_node.weight)
        for index, child in enumerate(starting_node.children):
            next_cnode = self._construct_cnode_with_all_children_cnodes(child, starting_identifier + (index,),
                                                                        copy_method)
            cnode.add_child(next_cnode)
        return cnode
Esempio n. 27
0
 def test_in_state_not_active_raises_error(self):
     self._setUpCellNoCompositeObjectsLeafUnitActive()
     in_state = Node(Unit(identifier=(1, ),
                          position=[0.1, 0.2],
                          charge={"charge": 1.0}),
                     weight=1)
     with self.assertRaises(AssertionError):
         self._event_handler.send_event_time([in_state])
Esempio n. 28
0
 def test_non_active_cell_in_excluded_cells_raises_error(self, _, __):
     in_state_one = Node(Unit(identifier=(3, ),
                              position=[0.1, 0.05],
                              velocity=[1.0, 0.0],
                              time_stamp=0.3),
                         weight=1)
     in_state_two = Node(Unit(identifier=(17, ), position=[0.3, 0.01]),
                         weight=1)
     # First index is active cell, second index is target cell
     self._cells_mock.position_to_cell.side_effect = [0, 1]
     # Excluded cells for 4 times 4 system, where 0 is the active cell
     self._cells_mock.excluded_cells.return_value = [
         0, 1, 3, 4, 5, 7, 12, 13, 15
     ]
     with self.assertRaises(AssertionError):
         self._event_handler_without_charge.send_event_time(
             [in_state_one, in_state_two])
 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=0.0),
                         weight=1)
     in_state_two = Node(Unit(identifier=(1, ),
                              position=[0.5, 0.9],
                              velocity=[1.0, 0.0],
                              time_stamp=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_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.0, 0.0],
                          time_stamp=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=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.0, 0.0],
                                 time_stamp=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.assertEqual(cnode.value.position,
                         [(0.1 + (2.2 - 1.5) * 1.0) % 1.0, 0.2, 0.3])
        self.assertEqual(cnode.value.velocity, [0.5, 0.0, 0.0])
        self.assertEqual(cnode.value.time_stamp, 2.2)
        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.assertEqual(first_child.value.position,
                         [(0.7 + (2.2 - 1.5) * 1.0) % 1.0, 0.8, 0.9])
        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.assertEqual(second_child.value.position,
                         [(0.5 + (2.2 - 1.5) * 1.0) % 1.0, 0.4, 0.3])
        self.assertEqual(second_child.value.velocity, [1.0, 0.0, 0.0])
        self.assertEqual(second_child.value.time_stamp, 2.2)
        self.assertEqual(len(second_child.children), 0)
Esempio n. 31
0
 def test_send_event_time_cell_for_leaf_units_leaf_unit_active(self):
     self._setUpCellForLeafUnitsLeafUnitActive()
     in_state = Node(Unit(identifier=(0, ),
                          position=[0.2, 0.8],
                          velocity=[0.0, 0.5],
                          time_stamp=1.2),
                     weight=1)
     in_state.add_child(
         Node(Unit(identifier=(0, 2),
                   position=[0.5, 0.9],
                   velocity=[0.0, 1.0],
                   time_stamp=1.3),
              weight=0.5))
     event_time = self._event_handler.send_event_time([in_state])
     self._cells_mock.position_to_cell.assert_called_once_with([0.5, 0.9])
     self._cells_mock.successor.assert_called_once_with(5, 1)
     self._cells_mock.cell_min.assert_called_once_with(6)
     self.assertAlmostEqual(event_time, 1.4)
Esempio n. 32
0
    def add_node(self, prev, gstate, do_hash=True):
        if do_hash:
            hash = gstate.compute_hash()
            if hash is not None:
                node = self.statespace.get_node_by_hash(hash)
                if node is not None:
                    return (node, False)
        else:
            hash = None
        uid = str(self.statespace.nodes_count)
        node = Node(uid, hash)
        logging.debug("New node %s", node.uid)

        if prev:
            node.prev = prev
        self.statespace.add_node(node)

        if self.debug_state == uid:
            context = Context(self, node, None)
            context.add_error_and_throw(errormsg.StateCaptured(context, uid=uid))
        return (node, True)