def test_persistence(topology, specs, seed, avg_step, expected): topology.persistences = specs nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([15., 15., 15.])) sample_end_to_end_distances(topology, nb_engine, seed=seed) mol_count = 0 for batch_count, batch in enumerate(specs): print(batch_count) for mol_idx in batch.mol_idxs: mol = topology.molecules[mol_idx] mol_copy = polyply.src.meta_molecule.MetaMolecule() mol_copy.add_edges_from(mol.edges) distance = expected[mol_count] avg_step_length = avg_step[batch_count] polyply.src.restraints.set_distance_restraint(mol_copy, batch.stop, batch.start, distance, avg_step_length, tolerance=0.0) for node in mol.nodes: print(node) if "distance_restraints" in mol_copy.nodes[node]: restr = mol.nodes[node]["distance_restraints"] ref_restr = mol_copy.nodes[node]["distance_restraints"] for new, ref in zip(restr, ref_restr): assert new == pytest.approx(ref, rel=1e-3) mol_count += 1 batch_count += 1
def nonbond_matrix(): toppath = TEST_DATA + "/struc_build/system.top" topology = Topology.from_gmx_topfile(name="test", path=toppath) topology.preprocess() topology.volumes = {"PEO": 0.43} return NonBondEngine.from_topology(topology.molecules, topology, box=np.array([10., 10., 10.]))
def test_get_interaction(topology, mol_idx_a, mol_idx_b, node_a, node_b, expected): # initiate the nb_engine nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([10., 10., 10.])) # check get interactions works properly value = nb_engine.get_interaction(mol_idx_a=mol_idx_a, mol_idx_b=mol_idx_b, node_a=node_a, node_b=node_b) assert value == (expected, 1.0)
def test_error(topology): specs = [ PersistenceSpecs(model="WCM", lp=1.5, start=0, stop=21, mol_idxs=list(range(0, 10))) ] seed = 63594 avg_step = 0.6533 topology.persistences = specs nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([1., 1., 1.])) with pytest.raises(IOError): sample_end_to_end_distances(topology, nb_engine, seed=seed)
def test_nb_engine_from_top_file(topology, n_pos): # we add positions to the topology skipping some # positins depending on how many are set in n_pos positions = np.random.random(n_pos * 3).reshape(-1, 3) for mol_idx, mol in enumerate(topology.molecules): for node in mol.nodes: try: mol.nodes[node]["position"] = positions[mol_idx + node] except IndexError: continue # initiate the nb_engine nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([10., 10., 10.])) # these are the expected interactions in the nb_engine nb_interactions = { frozenset(["GLY", "GLY"]): 0.53, frozenset(["GLY", "GLU"]): (0.53 + 0.67) / 2.0, frozenset(["GLY", "ASP"]): (0.43 + 0.53) / 2.0, frozenset(["ASP", "ASP"]): 0.43, frozenset(["ASP", "GLU"]): (0.43 + 0.67) / 2.0, frozenset(["GLU", "GLU"]): 0.67, } # make sure the cut_off is set dynamically and accurate assert nb_engine.cut_off == 2 * 0.67 # check if all interactions are created properly for res_combo, interaction in nb_interactions.items(): assert nb_engine.interaction_matrix[res_combo] == ( nb_interactions[res_combo], 1.0) # make sure that all positions are in the global position matrix # and positions which are undefined have a infinity set for mol_idx, mol in enumerate(topology.molecules): for node in mol.nodes: if "position" in mol.nodes[node]: assert all( nb_engine.get_point(mol_idx, node) == mol.nodes[node] ["position"]) else: assert all(nb_engine.positions[mol_idx, node] == np.array( [np.inf, np.inf, np.inf]))
def test_remove_positions(topology): # we add 5 positions to the nb_matrix positions = np.random.random(5 * 3).reshape(-1, 3) for mol_idx, mol in enumerate(topology.molecules): for node in mol.nodes: try: mol.nodes[node]["position"] = positions[mol_idx + node] except IndexError: continue nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([10., 10., 10.])) # now we check if the 3rd is removed correctly nb_engine.remove_positions(mol_idx=2, node_keys=[0]) assert all(nb_engine.get_point(2, 0) == np.array([np.inf, np.inf, np.inf]))
def test_update_positions_in_molecules(topology): # we add 15 random positions to all molecules positions = np.random.random(5 * 3).reshape(-1, 3) for mol_idx, mol in enumerate(topology.molecules): for node in mol.nodes: print(mol_idx, node) mol.nodes[node]["position"] = positions[mol_idx + node] nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([10., 10., 10.])) # now we overwrite those positoins one nb_engine.positions[:, :] = np.ones((5, 3), dtype=float)[:, :] # then we update them in the molecules and check if they are actually updated nb_engine.update_positions_in_molecules(topology.molecules) for mol_idx, mol in enumerate(topology.molecules): for node in mol.nodes: assert all(mol.nodes[node]["position"] == np.array([1., 1., 1.]))
def test_add_positions(topology): # we add 2 positions to the nb_matrix positions = np.random.random(2 * 3).reshape(-1, 3) for mol_idx, mol in enumerate(topology.molecules): for node in mol.nodes: try: mol.nodes[node]["position"] = positions[mol_idx + node] except IndexError: continue nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([10., 10., 10.])) # now we check if the 3rd is added correctly nb_engine.add_positions(np.array([5.0, 5.0, 5.0]), mol_idx=2, node_key=0, start=False) assert all(nb_engine.get_point(2, 0) == np.array([5.0, 5.0, 5.0]))
def test_warning(caplog, topology): caplog.set_level(logging.WARNING) specs = [ PersistenceSpecs(model="WCM", lp=1.5, start=0, stop=21, mol_idxs=list(range(0, 10))) ] seed = 63594 avg_step = 0.6533 topology.persistences = specs nb_engine = NonBondEngine.from_topology(topology.molecules, topology, box=np.array([9.0, 9., 9.])) with caplog.at_level(logging.WARNING): sample_end_to_end_distances(topology, nb_engine, seed=seed) for record in caplog.records: assert record.levelname == "WARNING" break else: assert False