def write(self, extracted_global_state: Sequence[Node]) -> None: """ Extract the shortest separations between all leaf unit pairs in different composite objects and write them to the temporary files. Parameters ---------- extracted_global_state : Sequence[base.node.Node] The extracted global state. """ super().write(extracted_global_state) for first_root_cnode_index, first_root_cnode in enumerate( extracted_global_state): for second_root_cnode_index in range(first_root_cnode_index + 1, len(extracted_global_state)): second_root_cnode = extracted_global_state[ second_root_cnode_index] for first_leaf_node in yield_leaf_nodes(first_root_cnode): for second_leaf_node in yield_leaf_nodes( second_root_cnode): first_leaf_identifier = first_leaf_node.value.identifier[ -1] second_leaf_identifier = second_leaf_node.value.identifier[ -1] identifier_distance = ( abs(first_leaf_identifier - second_leaf_identifier) if setting.number_of_node_levels > 1 else 0) separation = setting.periodic_boundaries.separation_vector( first_leaf_node.value.position, second_leaf_node.value.position) print(vectors.norm(separation), file=self._files[identifier_distance])
def initialize(self, cells: Cells, extracted_global_state: Sequence[node.Node], charge: Union[str, None]) -> None: """ Initialize the cell bounding potential. This is done by handing the cells to the potential and also by extracting the maximum charge from the extracted global state. If the charge is None, the potential is initialized without a charge. Extends the initialize method of the abstract Initializer class. This method is called once in the beginning of the run by the activator. Only after a call of this method, other public methods of this class can be called without raising an error. Parameters ---------- cells : activator.internal_state.cell_occupancy.cells.Cells The cell system. extracted_global_state : Sequence[base.node.Node] The extracted global state of the tree state handler. charge : str or None The relevant charge for this event handler. """ super().initialize() self._cells = cells max_charge = (max(abs(leaf_node.value.charge[charge]) for root_cnode in extracted_global_state for leaf_node in node.yield_leaf_nodes(root_cnode)) if charge is not None else 1.0) self._bounding_potential.initialize(cells, max_charge, charge is not None)
def send_out_state(self, target_unit_root_cnode: Union[Node, None]) -> Sequence[Node]: """ Return the out-state. This method receives the branch of the leaf unit in the sampled target cell. If it is None, the time-sliced active leaf unit branch which was transmitted in the send_event_time method is returned. Otherwise, first the event is confirmed. If it is confirmed, the velocities are exchanged and the branches are kept consistent in the out-state. Parameters ---------- target_unit_root_cnode : Node or None The branch of the leaf unit in the sampled target cell. Returns ------- Sequence[base.node.Node] The out-state. """ if target_unit_root_cnode is None: return self._state else: self._state.append(target_unit_root_cnode) for leaf_cnode in yield_leaf_nodes(target_unit_root_cnode): self._leaf_cnodes.append(leaf_cnode) self._leaf_units.append(leaf_cnode.value) assert len(self._leaf_units) == 2 self._calculate_out_state_of_two_leaf_unit_bounding_potential( setting.periodic_boundaries.separation_vector( self._leaf_units[self._active_leaf_unit_index].position, self._leaf_units[self._active_leaf_unit_index ^ 1].position), self._charges(self._leaf_units[0], self._leaf_units[1]),) return self._state
def write(self, extracted_global_state: Sequence[Node]) -> None: """ Write the extracted global state to a .pdb file. Parameters ---------- extracted_global_state : Sequence[base.node.Node] The extracted global state. """ super().write(extracted_global_state) for root_cnode in extracted_global_state: for leaf_node in yield_leaf_nodes(root_cnode): self._get_atom(leaf_node.value.identifier ).position = leaf_node.value.position with Writer(self._filename_without_ending + str(self._current_file_index) + ".pdb", self._number_of_atoms, multiframe=False, remarks="RUN IDENTIFICATION HASH: {0}".format( get_uuid())) as writer: # MDAnalysis prints UserWarnings that not so important attributes weren't set which we ignore with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) writer.write(self._universe.atoms) self._current_file_index += 1
def _construct_leaf_cnodes(self) -> None: """Extract the _leaf_cnodes and _leaf_units from the _state attribute.""" assert self._state is not None self._leaf_cnodes = [] self._leaf_units = [] for cnode in self._state: for leaf_cnode in yield_leaf_nodes(cnode): self._leaf_cnodes.append(leaf_cnode) self._leaf_units.append(leaf_cnode.value)
def test_yield_leaf_nodes(self): root_node = node.Node("RootNode", weight=0.01) node_one = node.Node("One", weight=0.1) node_two = node.Node("Two", weight=0.2) node_three = node.Node("Three", weight=0.3) node_four = node.Node("Four", weight=0.4) node_five = node.Node("Five", weight=0.5) root_node.children = [node_one, node_two, node_three] node_one.parent = root_node node_two.parent = root_node node_three.parent = root_node node_one.children = [node_four, node_five] node_four.parent = node_one node_five.parent = node_one leaf_nodes_of_root_node = list(node.yield_leaf_nodes(root_node)) self.assertEqual(len(leaf_nodes_of_root_node), 4) self.assertEqual(leaf_nodes_of_root_node[0].value, "Four") self.assertEqual(leaf_nodes_of_root_node[1].value, "Five") self.assertEqual(leaf_nodes_of_root_node[2].value, "Two") self.assertEqual(leaf_nodes_of_root_node[3].value, "Three")
def send_out_state( self, cnodes_with_active_units: Sequence[Node], cnodes_with_new_active_units: Sequence[Node]) -> Sequence[Node]: """ Return the out-state. In the out-state, the previously independent active units have a zero velocity and the new independent active units have the same speed, but the direction of motion has changed. Parameters ---------- cnodes_with_active_units : Sequence[base.node.Node] The root cnode of the branch of the independent active unit. cnodes_with_new_active_units : Sequence[base.node.Node] The root cnode of the branch of the unit which should get active. Returns ------- Sequence[base.node.Node] The out-state. Raises ------ AssertionError If not all leaf units in cnodes_with_active_units have the same velocity. AssertionError If the leaf units in cnodes_with_new_active_units have a non-zero velocity and time stamp but they do not appear in cnodes_with_active_units. """ self._store_in_state(cnodes_with_active_units) self._time_slice_all_units_in_state() self._construct_leaf_cnodes() for index in range(1, len(self._leaf_units)): assert self._leaf_units[index].velocity == self._leaf_units[ 0].velocity old_direction_of_motion, speed = analyse_velocity( self._leaf_units[0].velocity) new_direction_of_motion = self._get_new_direction_of_motion( old_direction_of_motion) old_active_identifiers = set() velocity_changes = {} for index, unit in enumerate(self._leaf_units): velocity_changes[unit.identifier] = ([ 0.0 for _ in range(setting.dimension) ], index) unit.velocity[old_direction_of_motion] = 0.0 velocity_changes[ unit.identifier][0][old_direction_of_motion] -= speed old_active_identifiers.add(unit.identifier) new_leaf_cnodes = [] new_leaf_units = [] for cnode in cnodes_with_new_active_units: for leaf_cnode in yield_leaf_nodes(cnode): new_leaf_cnodes.append(leaf_cnode) new_leaf_units.append(leaf_cnode.value) for index, unit in enumerate(new_leaf_units): if unit.identifier in old_active_identifiers: velocity_changes[ unit.identifier][0][new_direction_of_motion] += speed self._leaf_units[velocity_changes[unit.identifier] [1]].velocity[new_direction_of_motion] = speed else: assert unit.velocity is None assert unit.time_stamp is None cnode = new_leaf_cnodes[index] self._leaf_units.append(unit) self._leaf_cnodes.append(cnode) while cnode.parent: cnode = cnode.parent self._state.append(cnode) velocity_changes[unit.identifier] = ([ 0.0 for _ in range(setting.dimension) ], len(self._leaf_units) - 1) velocity_changes[ unit.identifier][0][new_direction_of_motion] += speed unit.velocity = [0.0 for _ in range(setting.dimension)] unit.velocity[new_direction_of_motion] = speed unit.time_stamp = self._event_time for old_leaf_unit in self._leaf_units: # noinspection PyTypeChecker if all(velocity_component < 1e-6 for velocity_component in old_leaf_unit.velocity): old_leaf_unit.velocity = None old_leaf_unit.time_stamp = None for velocity_change, index in velocity_changes.values(): self._register_velocity_change_leaf_cnode(self._leaf_cnodes[index], velocity_change) self._commit_non_leaf_velocity_changes() return self._state