def optimize(self, state): mch_mol = mch.Molecule( atoms=(mch.Atom( id=atom.get_id(), element_string=atom.__class__.__name__, ) for atom in state.get_atoms()), bonds=get_mch_bonds(state), position_matrix=state.get_position_matrix(), ) mch_mol, result = self._optimizer.get_result( mol=mch_mol, bond_pair_ids=tuple(get_long_bond_ids(state)), subunits=get_subunits(state), ) old_pos_mat = state.get_position_matrix() new_pos_mat = mch_mol.get_position_matrix() old_extents = (abs(max(old_pos_mat[:, i]) - min(old_pos_mat[:, i])) for i in range(3)) new_extents = (abs(max(new_pos_mat[:, i]) - min(new_pos_mat[:, i])) for i in range(3)) ratios = (n / o for n, o in zip(new_extents, old_extents)) old_lattice = state.get_lattice_constants() new_lattice = tuple(old_lattice[i] * ratio for i, ratio in enumerate(ratios)) state = state.with_lattice_constants(new_lattice) return state.with_position_matrix( position_matrix=mch_mol.get_position_matrix())
def atoms(): return [ mch.Atom(0, 'C'), mch.Atom(1, 'C'), mch.Atom(2, 'C'), mch.Atom(3, 'C'), mch.Atom(4, 'C'), mch.Atom(5, 'C'), ]
def optimize(self, state): # Define MCHammer molecule to optimize. mch_mol = mch.Molecule( atoms=(mch.Atom( id=atom.get_id(), element_string=atom.__class__.__name__, ) for atom in state.get_atoms()), bonds=tuple(get_mch_bonds(state)), position_matrix=state.get_position_matrix(), ) # Run optimization. mch_mol, results = self._optimizer.get_result( mol=mch_mol, bond_pair_ids=tuple(get_long_bond_ids(state)), subunits=get_subunits(state), ) return state.with_position_matrix( position_matrix=mch_mol.get_position_matrix(), )
import stk import mchammer as mch benzene = stk.BuildingBlock.init_from_file('benzene.mol') benzene_atoms = [(atom.get_id(), atom.__class__.__name__) for atom in benzene.get_atoms()] benzene_bonds = [] for i, bond in enumerate(benzene.get_bonds()): # Must ensure that bond atom ids are ordered by atom id. b_ids = ((bond.get_atom1().get_id(), bond.get_atom2().get_id()) if bond.get_atom1().get_id() < bond.get_atom2().get_id() else (bond.get_atom2().get_id(), bond.get_atom1().get_id())) benzene_bonds.append((i, b_ids)) mch_mol = mch.Molecule( atoms=(mch.Atom(id=i[0], element_string=i[1]) for i in benzene_atoms), bonds=(mch.Bond(id=i[0], atom1_id=i[1][0], atom2_id=i[1][1]) for i in benzene_bonds), position_matrix=benzene.get_position_matrix(), ) target_bond_length = 1.2 optimizer = mch.Optimizer( step_size=0.25, target_bond_length=target_bond_length, num_steps=100, ) subunits = mch_mol.get_subunits(bond_pair_ids=((2, 3), (1, 5)), ) # Get all steps. mch_result = optimizer.get_trajectory( mol=mch_mol,
# on. You can use ranges to specify the ids. building_blocks={ bb1: range(2), bb2: (2, 3), bb3: 4, bb4: 5, bb5: range(6, 10), }, ), ) cage.write('poc.mol') stk_long_bond_ids = get_long_bond_ids(cage) mch_mol = mch.Molecule( atoms=( mch.Atom( id=atom.get_id(), element_string=atom.__class__.__name__, ) for atom in cage.get_atoms() ), bonds=( mch.Bond( id=i, atom1_id=bond.get_atom1().get_id(), atom2_id=bond.get_atom2().get_id() ) for i, bond in enumerate(cage.get_bonds()) ), position_matrix=cage.get_position_matrix(), ) mch_mol_nci = deepcopy(mch_mol) optimizer = mch.Optimizer( step_size=0.25,
import pytest import numpy as np import mchammer as mch @pytest.fixture( params=( (mch.Atom(id=0, element_string='N'), 0, 'N'), (mch.Atom(id=65, element_string='P'), 65, 'P'), (mch.Atom(id=2, element_string='C'), 2, 'C'), ) ) def atom_info(request): return request.param @pytest.fixture( params=( (mch.Bond(id=0, atom_ids=(0, 1)), 0, 0, 1), (mch.Bond(id=65, atom_ids=(2, 3)), 65, 2, 3), (mch.Bond(id=2, atom_ids=(3, 4)), 2, 3, 4), (mch.Bond(id=3, atom_ids=(0, 9)), 3, 0, 9), ) ) def bond_info(request): return request.param @pytest.fixture def atoms(): return [