def test_send_event_time_infinite_displacement(self, random_expovariate_mock, _): # Bounding potential returns infinite time displacement. self._setUpSendEventTimeInfiniteDisplacement( random_expovariate_mock, self._bounding_potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[2.0, 0.0], time_stamp=Time.from_float(1.3), charge={"charge": 3.4}), weight=1) event_time = self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two]) # Should be called with beta random_expovariate_mock.assert_called_once_with(1) self.assertEqual( self._bounding_potential_mock_with_charge.displacement.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_with_charge.displacement. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[2.0, 0.0], [0.0, -0.3], -1.2, 3.4, 2], places=13, expected_kwargs={}) self.assertEqual(event_time, Time.from_float(float('inf')))
def test_send_event_time_to_active_root_unit_two_root_nodes_raises_error( self): cnode_one = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode_one.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) cnode_two = Node(Unit(identifier=(5, ), position=[0.4, 0.5, 0.6], velocity=[0.5, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode_two.add_child( Node(Unit(identifier=(5, 0), position=[0.3, 0.2, 0.1], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) with self.assertRaises(AssertionError): self._event_handler_to_root_unit_motion.send_event_time( [cnode_one, cnode_two])
def test_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_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 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_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])
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_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_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_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 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_missing_charges_raises_error(self, random_expovariate_mock, _): self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"other_charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[2.0, 0.0], time_stamp=Time.from_float(1.3), charge={"other_charge": 3.4}), weight=1) with self.assertRaises(KeyError): self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two])
def test_send_out_state_to_active_root_unit(self): cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 1.0, 1.5], time_stamp=Time.from_float(1.0)), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 2.0, 3.0], time_stamp=Time.from_float(1.0)), weight=0.5)) # Call this to update the event time self._event_handler_to_root_unit_motion.send_event_time([cnode]) cnode.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.6, 0.1], charge={"charge": -1.0}), weight=0.5)) out_state = self._event_handler_to_root_unit_motion.send_out_state( [cnode]) self.assertEqual(len(out_state), 1) cnode = out_state[0] self.assertIsNone(cnode.parent) self.assertEqual(cnode.weight, 1) self.assertEqual(cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(cnode.value.position, [0.35, 0.7, 0.05], places=13) self.assertEqual(cnode.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(cnode.value.time_stamp, Time.from_float(1.5), places=13) self.assertEqual(len(cnode.children), 2) first_child = cnode.children[0] self.assertIs(first_child.parent, cnode) self.assertEqual(first_child.weight, 0.5) self.assertEqual(first_child.value.identifier, (0, 0)) self.assertAlmostEqualSequence(first_child.value.position, [0.2, 0.8, 0.4], places=13) self.assertEqual(first_child.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(first_child.value.time_stamp, Time.from_float(1.5), places=13) self.assertEqual(len(first_child.children), 0) second_child = cnode.children[1] self.assertIs(second_child.parent, cnode) self.assertEqual(second_child.weight, 0.5) self.assertEqual(second_child.value.identifier, (0, 1)) self.assertEqual(second_child.value.position, [0.5, 0.6, 0.1]) self.assertEqual(second_child.value.velocity, [1.0, 2.0, 3.0]) self.assertAlmostEqual(second_child.value.time_stamp, Time.from_float(1.5), places=13) self.assertEqual(len(second_child.children), 0)
def test_send_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_both_leaf_units_active_raises_error(self, random_expovariate_mock, _): self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.0)), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.0)), weight=1) with self.assertRaises(AssertionError): self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two])
def extract_from_global_state(self, identifier: StateId) -> Node: """ Extract a part of the global state based on a global state identifier. For the given identifier, this method constructs a branch of cnodes. When constructing the units, the positions, velocities, and time stamps are copied, so that event handlers may modify the branch. This method then returns the root cnode of the branch. The identifier is returned by the activator and the extracted part of the global state will be sent to the event handlers by the mediator. Parameters ---------- identifier : StateId The global state identifier. Returns ------- base.node.Node The root cnode of the branch corresponding the the global state identifier. """ sliced_identifier = (identifier[0], ) old_node = self._physical_state.get(sliced_identifier) velocity, time_stamp = self._lifting_state.get(sliced_identifier) unit = Unit(sliced_identifier, copy(old_node.value.position), old_node.value.charge, copy(velocity), copy(time_stamp)) parent_cnode = Node(unit, old_node.weight) old_cnode = parent_cnode for identifier_level in range(1, len(identifier)): sliced_identifier = identifier[:identifier_level + 1] next_node = old_node.children[identifier[identifier_level]] velocity, time_stamp = self._lifting_state.get(sliced_identifier) unit = Unit(sliced_identifier, copy(next_node.value.position), next_node.value.charge, copy(velocity), copy(time_stamp)) next_cnode = Node(unit, next_node.weight) old_cnode.add_child(next_cnode) old_cnode = next_cnode old_node = next_node for index, child in enumerate(old_node.children): next_cnode = self._construct_cnode_with_all_children_cnodes( child, identifier + (index, ), copy) old_cnode.add_child(next_cnode) return parent_cnode
def test_send_out_state_with_charge(self, random_expovariate_mock): self._setUpSendEventTime(random_expovariate_mock, self._potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[-2.0, -3.0], time_stamp=Time.from_float(3.1), charge={"charge": 3.4}), weight=1) self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two]) out_state = self._event_handler_with_charge.send_out_state() # Displacement method was called in event handler's send_event_time method. self.assertEqual( self._potential_mock_with_charge.displacement.call_count, 1) self._potential_mock_with_charge.derivative.assert_not_called() self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.children, []) self.assertEqual(first_cnode.value.identifier, (3, )) self.assertEqual(first_cnode.value.position, [0.5, 0.6]) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.charge, {"charge": -1.2}) self.assertEqual(first_cnode.value.velocity, [-2.0, -3.0]) self.assertAlmostEqual(first_cnode.value.time_stamp, Time.from_float(3.4), places=13) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.children, []) self.assertEqual(second_cnode.value.identifier, (1, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.9, 0.0], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.charge, {"charge": 3.4}) self.assertIsNone(second_cnode.value.velocity) self.assertIsNone(second_cnode.value.time_stamp)
def test_send_out_state_event_handler_two_cartesian_velocity(self): self._event_handler_two.send_event_time() cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3], velocity=[0.5, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.7, 0.8, 0.9], charge={"charge": 1.0}, velocity=[1.0, 0.0, 0.0], time_stamp=Time.from_float(1.0)), weight=0.5)) out_state = self._event_handler_two.send_out_state([cnode]) self.assertEqual(len(out_state), 1) out_state_cnode = out_state[0] self.assertIsNone(out_state_cnode.parent) self.assertEqual(out_state_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(out_state_cnode.value.position, [0.25, 0.2, 0.3], places=13) self.assertEqual(out_state_cnode.weight, 1) self.assertEqual(out_state_cnode.value.velocity, [0.5, 0.0, 0.0]) self.assertAlmostEqual(out_state_cnode.value.time_stamp, Time.from_float(1.3), places=13) self.assertIsNone(out_state_cnode.value.charge) self.assertEqual(len(out_state_cnode.children), 1) child_cnode = out_state_cnode.children[0] self.assertEqual(len(child_cnode.children), 0) self.assertIs(child_cnode.parent, out_state_cnode) self.assertEqual(child_cnode.value.identifier, (0, 0)) self.assertAlmostEqualSequence(child_cnode.value.position, [0.0, 0.8, 0.9], places=13) self.assertEqual(child_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(child_cnode.value.time_stamp, Time.from_float(1.3), places=13) self.assertEqual(child_cnode.value.charge, {"charge": 1.0})
def test_send_out_state_one_root_unit_active(self): self._event_handler_one.send_event_time() cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3]), weight=1) cnode.add_child( Node(Unit(identifier=(0, 0), position=[0.4, 0.5, 0.6], charge={"charge": 1.0}), weight=0.5)) cnode.add_child( Node(Unit(identifier=(0, 1), position=[0.7, 0.8, 0.9], charge={"charge": -1.0}), weight=0.5)) out_state = self._event_handler_one.send_out_state(cnode) self.assertEqual(len(out_state), 1) out_state_cnode = out_state[0] self.assertIsNone(out_state_cnode.parent) self.assertEqual(out_state_cnode.value.identifier, (0, )) self.assertEqual(out_state_cnode.value.position, [0.1, 0.2, 0.3]) self.assertEqual(out_state_cnode.weight, 1) self.assertEqual(out_state_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(out_state_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertIsNone(out_state_cnode.value.charge) self.assertEqual(len(out_state_cnode.children), 2) first_child_cnode = out_state_cnode.children[0] self.assertIs(first_child_cnode.parent, out_state_cnode) self.assertEqual(first_child_cnode.children, []) self.assertEqual(first_child_cnode.value.identifier, (0, 0)) self.assertEqual(first_child_cnode.value.position, [0.4, 0.5, 0.6]) self.assertEqual(first_child_cnode.weight, 0.5) self.assertEqual(first_child_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(first_child_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertEqual(first_child_cnode.value.charge, {"charge": 1.0}) second_child_cnode = out_state_cnode.children[1] self.assertIs(second_child_cnode.parent, out_state_cnode) self.assertEqual(second_child_cnode.children, []) self.assertEqual(second_child_cnode.value.identifier, (0, 1)) self.assertEqual(second_child_cnode.value.position, [0.7, 0.8, 0.9]) self.assertEqual(second_child_cnode.weight, 0.5) self.assertEqual(second_child_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(second_child_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertEqual(second_child_cnode.value.charge, {"charge": -1.0})
def test_send_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 fill_root_node(self, node: Node) -> None: """ Fill the root node with a random point mass. Parameters ---------- node : base.node.Node The root node. """ node.value = Particle( setting.random_position(), { charge_value.charge_name: charge_value[0] for charge_value in self._charge_values })
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 read(self) -> List[Node]: """ Return the initial global physical state. This method creates the number of root nodes which was specified on initialization and fills them using the random node creator. Returns ------- List[base.node.Node] The initial global physical state. """ all_nodes = [Node() for _ in range(setting.number_of_root_nodes)] for node in all_nodes: self._random_node_creator.fill_root_node(node) return all_nodes
def test_send_out_state_one_leaf_unit_active(self): # Call this to set event time in event handler self._event_handler_one.send_event_time() cnode = Node(Unit(identifier=(0, ), position=[0.1, 0.2, 0.3]), weight=1) out_state = self._event_handler_one.send_out_state(cnode) self.assertEqual(len(out_state), 1) out_state_cnode = out_state[0] self.assertEqual(out_state_cnode.children, []) self.assertIsNone(out_state_cnode.parent) self.assertEqual(out_state_cnode.value.identifier, (0, )) self.assertEqual(out_state_cnode.value.position, [0.1, 0.2, 0.3]) self.assertEqual(out_state_cnode.weight, 1) self.assertEqual(out_state_cnode.value.velocity, [1.0, 0.0, 0.0]) self.assertAlmostEqual(out_state_cnode.value.time_stamp, Time.from_float(0.0), places=13) self.assertIsNone(out_state_cnode.value.charge)
def test_send_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 setUp(self) -> None: # Build up two dipoles to test everything self._root_nodes = [Node(), Node()] self._root_nodes[0].add_child(Node(Particle(position=[0, 0], charge={"e": 1}))) self._root_nodes[0].add_child(Node(Particle(position=[0.1, 0.05], charge={"e": -1}))) self._root_nodes[1].add_child(Node(Particle(position=[0.9, 0.8], charge={"e": 1}))) self._root_nodes[1].add_child(Node(Particle(position=[0.9, 0.75], charge={"e": -1}))) for node in self._root_nodes: position = [sum(leaf_node.value.position[index] * leaf_node.weight for leaf_node in node.children) for index in range(2)] node.value = Particle(position=position) hypercubic_setting.HypercubicSetting(beta=1.0, dimension=3, system_length=1.0) setting.number_of_node_levels = 2 setting.number_of_root_nodes = 2 setting.number_of_nodes_per_root_node = 2 # Test the combination used throughout the application in JFV 1.0.0.0 physical_state = TreePhysicalState() lifting_state = TreeLiftingState() self._state_handler = TreeStateHandler(physical_state, lifting_state)
def test_more_than_two_leaf_units_raises_error(self, random_expovariate_mock, _): self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(1, ), position=[0.2, 0.4], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.7)), weight=1) in_state_one.add_child( Node(Unit(identifier=(1, 0), position=[0.5, 0.6], velocity=[2.0, 0.0], time_stamp=Time.from_float(0.7)), weight=0.5)) in_state_one.add_child( Node(Unit(identifier=(1, 1), position=[0.3, 0.8]), weight=0.5)) in_state_two = Node(Unit(identifier=(2, ), position=[0.2, 0.4], velocity=[1.0, 0.0], time_stamp=Time.from_float(0.7)), weight=1) in_state_two.add_child( Node(Unit(identifier=(2, 0), position=[0.5, 0.6], velocity=[2.0, 0.0], time_stamp=Time.from_float(0.7)), weight=0.5)) in_state_two.add_child( Node(Unit(identifier=(2, 1), position=[0.3, 0.8]), weight=0.5)) with self.assertRaises(AssertionError): self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two])
def test_send_out_state_leaf_units_in_same_composite_object_reject( self, random_expovariate_mock, random_uniform_mock): # Bounding potential returns time displacement 0.3. self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_without_charge) in_state_one = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.5, 0.5], time_stamp=Time.from_float(1.8)), weight=1) in_state_one.add_child( Node(Unit(identifier=(0, 1), position=[0.5, 0.9], velocity=[-1.0, 1.0], time_stamp=Time.from_float(1.8)), weight=0.5)) in_state_two = Node(Unit(identifier=(0, ), position=[0.2, 0.8], velocity=[-0.5, 0.5], time_stamp=Time.from_float(1.8)), weight=1) in_state_two.add_child( Node(Unit(identifier=(0, 0), position=[0.1, 0.3]), weight=0.5)) self._event_handler_without_charge.send_event_time( [in_state_one, in_state_two]) self._setUpSendOutStateReject( random_uniform_mock, self._bounding_potential_mock_without_charge, self._potential_mock_without_charge) out_state = self._event_handler_without_charge.send_out_state() # New position of active leaf unit is [0.2, 0.2]. self.assertEqual( self._bounding_potential_mock_without_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_without_charge.derivative. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-1.0, 1.0], [-0.1, 0.1]], places=13, expected_kwargs={}) self.assertEqual( self._potential_mock_without_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._potential_mock_without_charge.derivative.call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-1.0, 1.0], [-0.1, 0.1]], places=13, expected_kwargs={}) random_uniform_mock.assert_called_once_with(0, 0.7) self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(first_cnode.value.position, [0.05, 0.95], places=13) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.velocity, [-0.5, 0.5]) self.assertAlmostEqual(first_cnode.value.time_stamp, Time.from_float(2.1), places=13) self.assertIsNone(first_cnode.value.charge) self.assertEqual(len(first_cnode.children), 1) first_child = first_cnode.children[0] self.assertIs(first_child.parent, first_cnode) self.assertEqual(first_child.children, []) self.assertEqual(first_child.value.identifier, (0, 1)) self.assertAlmostEqualSequence(first_child.value.position, [0.2, 0.2], places=13) self.assertEqual(first_child.weight, 0.5) self.assertEqual(first_child.value.velocity, [-1.0, 1.0]) self.assertAlmostEqual(first_child.value.time_stamp, Time.from_float(2.1), places=13) self.assertIsNone(first_child.value.charge) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.value.identifier, (0, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.05, 0.95], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.velocity, [-0.5, 0.5]) self.assertAlmostEqual(second_cnode.value.time_stamp, Time.from_float(2.1), places=13) self.assertIsNone(second_cnode.value.charge) self.assertEqual(len(second_cnode.children), 1) second_child = second_cnode.children[0] self.assertIs(second_child.parent, second_cnode) self.assertEqual(second_child.children, []) self.assertEqual(second_child.value.identifier, (0, 0)) self.assertEqual(second_child.value.position, [0.1, 0.3]) self.assertEqual(second_child.weight, 0.5) self.assertIsNone(second_child.value.velocity) self.assertIsNone(second_child.value.time_stamp) self.assertIsNone(second_child.value.charge)
def test_send_out_state_with_charge_reject(self, random_expovariate_mock, random_uniform_mock): # Bounding potential returns time displacement 0.3. self._setUpSendEventTime(random_expovariate_mock, self._bounding_potential_mock_with_charge) in_state_one = Node(Unit(identifier=(3, ), position=[0.5, 0.6], charge={"charge": -1.2}), weight=1) in_state_two = Node(Unit(identifier=(1, ), position=[0.5, 0.9], velocity=[-2.0, -3.0], time_stamp=Time.from_float(1.1), charge={"charge": 3.4}), weight=1) self._event_handler_with_charge.send_event_time( [in_state_one, in_state_two]) self._setUpSendOutStateReject( random_uniform_mock, self._bounding_potential_mock_with_charge, self._potential_mock_with_charge) out_state = self._event_handler_with_charge.send_out_state() # New position of active leaf unit is [0.9, 1.0]. self.assertEqual( self._bounding_potential_mock_with_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._bounding_potential_mock_with_charge.derivative. call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-2.0, -3.0], [-0.4, -0.4], -1.2, 3.4], places=13, expected_kwargs={}) self.assertEqual( self._potential_mock_with_charge.derivative.call_count, 1) self.assertCallArgumentsEqualWithAlmostEqualSequence( self._potential_mock_with_charge.derivative.call_args_list[0], positions_of_sequences_in_args=[1], expected_args=[[-2.0, -3.0], [-0.4, -0.4], -1.2, 3.4], places=13, expected_kwargs={}) random_uniform_mock.assert_called_once_with(0, 0.7) self.assertEqual(len(out_state), 2) first_cnode = out_state[0] self.assertIsNone(first_cnode.parent) self.assertEqual(first_cnode.children, []) self.assertEqual(first_cnode.value.identifier, (3, )) self.assertEqual(first_cnode.value.position, [0.5, 0.6]) self.assertEqual(first_cnode.weight, 1) self.assertEqual(first_cnode.value.charge, {"charge": -1.2}) self.assertIsNone(first_cnode.value.velocity) self.assertIsNone(first_cnode.value.time_stamp) second_cnode = out_state[1] self.assertIsNone(second_cnode.parent) self.assertEqual(second_cnode.children, []) self.assertEqual(second_cnode.value.identifier, (1, )) self.assertAlmostEqualSequence(second_cnode.value.position, [0.9, 0.0], places=13) self.assertEqual(second_cnode.weight, 1) self.assertEqual(second_cnode.value.charge, {"charge": 3.4}) self.assertEqual(second_cnode.value.velocity, [-2.0, -3.0]) self.assertAlmostEqual(second_cnode.value.time_stamp, Time.from_float(1.4), places=13)