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_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_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)
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)
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_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_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)
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_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))
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_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 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_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_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_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 _construct_cnode_with_all_children_cnodes( self, starting_node: Node, starting_identifier: StateId, copy_method: Callable[[Any], Any] = lambda object_to_copy: object_to_copy ) -> 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, the velocity, and the time stamp. 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), copy_method(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
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])
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_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_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)