def test_error01(self): atoms = create( StaCustom([{ "id": i, "mol": i // 10 } for i in range(100)])) moles = create(StaCustom([{"id": i} for i in range(10)])) abst_special_bonds = [[ j for j in range(10) if abs(i - j) < 3 and i != j ] for i in range(10)] atoms.append_updater(AddSpecialBonds(moles, abst_special_bonds)) self.check_error_msg( "RuntimeError: Missing key(s) 'atom-ids' in AddSpecialBonds", atoms.get_data)
def test_error01(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddMolecularOrientation()) check_error_msg( self, "RuntimeError: Missing key(s) 'I_xx', 'I_xy', 'I_xz', 'I_yy', 'I_yz', 'I_zz' in AddMolecularOrientation", molecules.get_data)
def test_error01(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddGyrationRadius()) check_error_msg( self, "RuntimeError: Missing key(s) 'I_xx', 'I_yy', 'I_zz', 'mass' in AddGyrationRadius", molecules.get_data)
def test_error02(self): box = create(StaDumpBox("dumps_bead/bead.2990000.dump", 2990000)) atoms = create(StaCustom([{"A": i, "B": i * i} for i in range(1000)])) atoms.append_updater(AddWrappedPosition(box)) check_error_msg( self, "RuntimeError: Missing key(s) 'xu', 'yu', 'zu' in AddWrappedPosition", atoms.get_data)
def test_error01(self): atoms = create(StaCustom( [{"id": i, "mol": i//10} for i in range(100)])) moles = create(StaMolecules(atoms)) moles.append_updater(AddChildPositions(atoms, "child")) self.check_error_msg( "RuntimeError: Missing key(s) 'child-ids', 'xu', 'yu', 'zu' in AddChildPositions", moles.get_data)
def test_error02(self): elems = [ create(StaCustom({"id": i, "A": i*i})) for i in range(1000)] pro = ProValueArray(elems) pro.select("B") check_error_msg( self, "RuntimeError: Missing key(s) 'B' in ProValueArray", execute_omp, pro)
def test_error01(self): box = create(StaCustom({"foo": 0, "bar": 1})) atoms = create(StaDumpAtoms("dumps_bead/bead.2990000.dump", 2990000)) atoms.append_updater(AddWrappedPosition(box)) check_error_msg( self, "RuntimeError: Missing key(s) 'hi_x', 'hi_y', 'hi_z', 'lo_x', 'lo_y', 'lo_z' in AddWrappedPosition", atoms.get_data)
def test_nonarray(self): data_old = {"A": 1, "B": 2} data_new = {"A": 1, "B": 2, "C": 3} elem = create(StaCustom(data_old)) elem.append_updater(AddMap("A", "C", {1: 3, 2: 2, 3: 1})) self.assertEqual(data_new, elem.get_data()) self.assertEqual({"A", "B", "C"}, elem.get_keys())
def _test_2way_molecules(self, arguments, mol_ids): atoms = create(StaDumpAtoms(*arguments)) mols1 = create(StaMolecules(atoms)) mols2 = create(StaCustom([{"id": i} for i in mol_ids])) mols2.append_updater(AddChildIDs(atoms, "atom", "mol")) self.assertEqual(mols1.get_data(), mols2.get_data())
def test_without_select(self): data = [{"A": i, "B": i * i} for i in range(1000)] elems = [create(StaCustom(d)) for d in data] pro = ProData(elems) execute_omp(pro) self.assertEqual(pro.get_results(), data) data = [[{"A": j, "B": i * j} for j in range(100)] for i in range(100)] elems = [create(StaCustom(d)) for d in data] pro = ProData(elems) execute_omp(pro) self.assertEqual(pro.get_results(), data)
def test_overwrite(self): data_old = [{"A": i, "B": i*i, "C": i%3} for i in range(1000)] data_new = [{"A": i, "B": i%3} for i in range(1000)] elem = create(StaCustom(data_old)) elem.append_updater(AddRename("C", "B").overwrite()) self.assertEqual(data_new, elem.get_data()) self.assertEqual({"A", "B"}, elem.get_keys())
def test_error03(self): data = [ [{"id": j, "A": float(i*j)} for j in range(10*i+1)] for i in range(100)] elems = [create(StaCustom(d)) for d in data] pro = ProValueArray(elems) pro.select("A") check_error_msg( self, "RuntimeError: Data sizes must be the same", execute_omp, pro)
def test_rotate_stick(self): n_mols = 10 rotate_data = [] initial_atoms = [] for imol in range(n_mols): ps = np.random.uniform(-10.0, 10.0, (3,3)) m_point = 0.5 * (ps[0] + ps[1]) cross = np.cross(ps[1] - ps[0], ps[2] - ps[0]) n_vector = cross / np.linalg.norm(cross) rotate_data.append({"normal": n_vector, "middle": m_point}) initial_atoms.extend([ {"id": 2*imol+i+1, "mol": imol+1, "xyz": ps[i]} for i in range(2) ]) atoms_traj = [] for i in range(181): atoms_tmp = deepcopy(initial_atoms) for atom in atoms_tmp: m = rotate_data[atom["mol"]-1]["middle"] n = rotate_data[atom["mol"]-1]["normal"] rotated_xyz = np.array(rot(i, n, atom["xyz"]-m)) + m atom["xu"] = rotated_xyz[0] atom["yu"] = rotated_xyz[1] atom["zu"] = rotated_xyz[2] del atom["xyz"] atoms_traj.append(create(StaCustom(atoms_tmp))) mols_traj = [create(StaMolecules(atoms)) for atoms in atoms_traj] pro = ProTimeCorrelationInMolecule(list(zip(mols_traj, atoms_traj))) pro.set_indices(0, 1) execute_omp(pro) expects = np.array([np.cos(i*np.pi/180) for i in range(181)]) self.assertTrue(np.allclose(pro.get_time_correlation(), expects))
def test_array(self): elem = create(StaCustom([{"A": i, "B": i*i} for i in range(1000)])) vec_old = elem.get_1d_int("A") elem.append_updater(AddRename("B", "C")) elem.append_updater(AddRename("A", "B")) vec_new = elem.get_1d_int("B") self.assertTrue(np.allclose(vec_old, vec_new)) self.assertEqual({"B", "C"}, elem.get_keys())
def test_dimension_2d(self): abst_atoms_traj = [] for itraj in range(10): tmp = deepcopy(self.abst_atoms) shift_x = uniform(-100.0, 100.0) shift_y = uniform(-100.0, 100.0) shift_z = uniform(-100.0, 100.0) for atom in tmp: atom["xu"] *= itraj + 1 atom["yu"] *= itraj + 1 atom["zu"] *= itraj + 1 atom["xu"] += shift_x atom["yu"] += shift_y atom["zu"] += shift_z abst_atoms_traj.append(tmp) atomses = [create(StaCustom(a)) for a in abst_atoms_traj] pro_xyz = ProMeanSquareDisplacement(atomses) pro_xy = ProMeanSquareDisplacement(atomses) pro_xy.set_dimension(True, True, False) pro_yz = ProMeanSquareDisplacement(atomses) pro_yz.set_dimension(False, True, True) pro_xz = ProMeanSquareDisplacement(atomses) pro_xz.set_dimension(True, False, True) execute_omp([pro_xyz, pro_xy, pro_yz, pro_xz]) self.assertTrue( np.allclose(pro_xy.get_mean_square_displacement(), pro_yz.get_mean_square_displacement())) self.assertTrue( np.allclose(pro_xy.get_mean_square_displacement(), pro_xz.get_mean_square_displacement())) self.assertTrue( np.allclose( 2 * pro_xyz.get_mean_square_displacement(), pro_xz.get_mean_square_displacement() + pro_yz.get_mean_square_displacement() + pro_xz.get_mean_square_displacement()))
def test_overwrite(self): data_old = [{"A": i, "B": i * i} for i in range(1000)] data_new = [{"A": i, "B": i % 3} for i in range(1000)] mapping = {i: i % 3 for i in range(1000)} elem = create(StaCustom(data_old)) elem.append_updater(AddMap("A", "B", mapping).overwrite()) self.assertEqual(data_new, elem.get_data()) self.assertEqual({"A", "B"}, elem.get_keys())
def test_single_dihedral_angle(self): right_dihedral_angle = 135.0 dihedrals_py = [{ "atom1-id": 1, "atom2-id": 2, "atom3-id": 3, "atom4-id": 4 }] atoms_py = [{ "id": 1, "xu": -1.0, "yu": 0.0, "zu": 0.0 }, { "id": 2, "xu": 0.0, "yu": 0.0, "zu": 0.0 }, { "id": 3, "xu": 0.0, "yu": 1.0, "zu": 0.0 }, { "id": 4, "xu": 1.0, "yu": 1.0, "zu": -1.0 }] dihedrals = create(StaCustom(dihedrals_py)) atoms = create(StaCustom(atoms_py)) dihedrals.append_updater(AddDihedralAngle(atoms)) self.assertTrue( np.allclose(right_dihedral_angle, dihedrals.get_data()[0]["dihedral-angle"]))
def test_dimension_3d(self): abst_atoms_traj = [] for itraj in range(10): tmp = deepcopy(self.abst_atoms) shift_x = uniform(-100.0, 100.0) shift_y = uniform(-100.0, 100.0) shift_z = uniform(-100.0, 100.0) for atom in tmp: atom["xu"] *= itraj + 1 atom["yu"] *= itraj + 1 atom["zu"] *= itraj + 1 atom["xu"] += shift_x atom["yu"] += shift_y atom["zu"] += shift_z abst_atoms_traj.append(tmp) atomses = [create(StaCustom(a)) for a in abst_atoms_traj] pro = ProMeanSquareDisplacement(atomses) execute_omp(pro) expect = np.array([2.0 * i * i for i in range(10)]) self.assertTrue(np.allclose(pro.get_mean_square_displacement(), expect)) expect_array = [] for itraj in range(10): tmp = [] for i in range(-1, 2): for j in range(-1, 2): for k in range(-1, 2): tmp.append( float(i * i + j * j + k * k) * itraj * itraj) expect_array.append(tmp) self.assertTrue( np.allclose(pro.get_displacement2_array(), np.array(expect_array)))
def test_isotropic(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) molecules.append_updater(AddInertiaMoment(atoms)) data = molecules.get_data()[0] self.assertEqual(data["I_xx"], data["I_yy"]) self.assertEqual(data["I_yy"], data["I_zz"]) self.assertEqual(data["I_xy"], data["I_xz"]) self.assertEqual(data["I_xy"], data["I_yz"])
def test_short_form(self): keys = [chr(i) for i in range(65, 65 + 26)] array = [] for i in range(100): random.shuffle(keys) array.append(dict(zip(keys, np.random.uniform(size=26) * i))) elem = create(StaCustom(array)) elem2 = create(elem) self.assertEqual(elem.get_data(), elem2.get_data()) self.assertEqual(elem.get_keys(), elem2.get_keys())
def test_sequence_lengths(self): right_lengths = np.random.uniform(0.5, 1.5, 1000) lo = -100.0 hi = 100.0 bonds_py = [] atoms_py = [{ "id": 1, "xu": np.random.uniform(lo, hi), "yu": np.random.uniform(lo, hi), "zu": np.random.uniform(lo, hi), }] for i, length in enumerate(right_lengths): vec = generate_random_unit_vector() atoms_py.append({ "id": i + 2, "xu": atoms_py[i]["xu"] + length * vec[0], "yu": atoms_py[i]["yu"] + length * vec[1], "zu": atoms_py[i]["zu"] + length * vec[2], }) bonds_py.append({ "id": i + 1, "atom1-id": i + 1, "atom2-id": i + 2, }) bonds = create(StaCustom(bonds_py)) atoms = create(StaCustom(atoms_py)) bonds.append_updater(AddBondLength(atoms)) self.assertTrue( np.allclose(right_lengths, bonds.get_1d_float("length")))
def test_random(self): rs = 10*np.random.random_sample((10, 3)) - 5 ms = np.random.random_sample(10) + 1 com = (rs.T*ms).T.sum(axis=0)/ms.sum() expected = rs - com abst_atoms_in_mol = [ { "id": i+1, "mol": 1, "mass": ms[i], "xu": rs[i,0], "yu": rs[i,1], "zu": rs[i,2] } for i in range(10)] abst_atoms = [] for imol in range(100): tmp = deepcopy(abst_atoms_in_mol) shift_x = uniform(-100.0, 100.0) shift_y = uniform(-100.0, 100.0) shift_z = uniform(-100.0, 100.0) for atom in tmp: atom["id"] += 10*imol atom["mol"] += imol atom["xu"] += shift_x atom["yu"] += shift_y atom["zu"] += shift_z abst_atoms.extend(tmp) atoms = create(StaCustom(abst_atoms)) moles = create(StaMolecules(atoms)) moles.append_updater(AddCoMPosition(atoms)) moles.append_updater(AddChildPositions(atoms, "atom")) data = moles.get_data() for d in data: self.assertTrue(np.allclose(expected, np.array( [d["atom-xs"], d["atom-ys"], d["atom-zs"]]).T))
def test_error01(self): dummy_data = deepcopy(self.abst_atoms) for i in range(len(dummy_data)): del dummy_data[i]["mass"] atoms = create(StaCustom(dummy_data)) pro = ProMeanSquareDisplacement([atoms]) self.check_error_msg( "RuntimeError: Missing key(s) 'mass' in ProMeanSquareDisplacement", execute_omp, pro)
def test_error01(self): dummy_data = deepcopy(self.base_data) for i in range(len(dummy_data)): del dummy_data[i]["xu"] atomses = [create(StaCustom(dummy_data)) for i in range(10)] molses = [create(StaMolecules(atoms)) for atoms in atomses] pro = ProTimeCorrelationInMolecule(list(zip(molses, atomses))) self.check_error_msg( "RuntimeError: Missing key(s) 'xu' in ProTimeCorrelationInMolecule", execute_omp, pro)
def test_beads(self): abst_atoms = [] atom_id = 0 for imol in range(10): for iatom in range(20): atom_id += 1 abst_atoms.append({ "id": atom_id, "mol": imol + 1, "mass": 1.0, "xu": float(iatom // 4) }) if iatom % 4 == 0: abst_atoms[-1]["yu"] = 1.0 abst_atoms[-1]["zu"] = float(imol) * 10 elif iatom % 4 == 1: abst_atoms[-1]["yu"] = 0.0 abst_atoms[-1]["zu"] = -1.0 + float(imol) * 10 elif iatom % 4 == 2: abst_atoms[-1]["yu"] = -1.0 abst_atoms[-1]["zu"] = float(imol) * 10 elif iatom % 4 == 3: abst_atoms[-1]["yu"] = 0.0 abst_atoms[-1]["zu"] = 1.0 + float(imol) * 10 atoms = create(StaCustom(abst_atoms)) moles = create(StaMolecules(atoms)) beads = create( StaBeads(moles, [{ "indices-in-mol": list(range(4 * i, 4 * (i + 1))) } for i in range(5)])) beads.append_updater(AddCoMPosition(atoms)) rs = beads.get_2d_float("xu", "yu", "zu") expected_rs = [] for imol in range(10): for ibead in range(5): expected_rs.append([float(ibead), 0.0, 10.0 * imol]) self.assertTrue(np.allclose(rs, np.array(expected_rs)))
def test_error01(self): dummy_data = deepcopy(self.base_data) for i in range(len(dummy_data)): del dummy_data[i]["xu"] atoms = create(StaCustom(dummy_data)) mols = create(StaMolecules(atoms)) pro = ProDistanceInMolecule(mols, atoms) check_error_msg( self, "RuntimeError: Missing key(s) 'xu' in ProDistanceInMolecule", execute_omp, pro)
def test_squared_distance(self): abst_atoms = [] n_atoms_in_mol = len(self.base_data) for imol in range(10): tmp = deepcopy(self.base_data) shift_x = uniform(-100.0, 100.0) shift_y = uniform(-100.0, 100.0) shift_z = uniform(-100.0, 100.0) for atom in tmp: atom["id"] += imol * n_atoms_in_mol atom["mol"] += imol atom["xu"] += shift_x atom["yu"] += shift_y atom["zu"] += shift_z abst_atoms.extend(tmp) abst_atoms_traj = [] for itraj in range(10): tmp = deepcopy(abst_atoms) for atom in tmp: atom["xu"] *= itraj + 1 atom["yu"] *= itraj + 1 atom["zu"] *= itraj + 1 abst_atoms_traj.append(tmp) atomses = [create(StaCustom(a)) for a in abst_atoms_traj] molses = [create(StaMolecules(a)) for a in atomses] pro = ProDistanceInMolecule(list(zip(molses, atomses))) pro.set_indices(0, 5) execute_omp(pro) expects = np.array([[20.0 * (i + 1) * (i + 1)] * 10 for i in range(10)]) self.assertTrue(np.allclose(pro.get_distance2_array(), expects))
def test_isotropic(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) molecules.append_updater(AddInertiaMoment(atoms)) molecules.append_updater(AddMolecularOrientation()) data = molecules.get_data()[0] evals = data["I_values"] self.assertEqual(evals[0], evals[1]) self.assertEqual(evals[0], evals[2]) evecs = data["I_vectors"] self.assertTrue(np.allclose(np.array(evecs), np.identity(3)))
def test_x_oriented(self): atoms = create( StaCustom([ { "id": 1, "mol": 1, "mass": 1.0, "xu": 1.0, "yu": 1.0, "zu": 1.0 }, { "id": 2, "mol": 1, "mass": 1.0, "xu": 2.0, "yu": 1.0, "zu": 1.0 }, { "id": 3, "mol": 1, "mass": 1.0, "xu": 4.0, "yu": 1.0, "zu": 1.0 }, { "id": 4, "mol": 1, "mass": 1.0, "xu": 8.0, "yu": 1.0, "zu": 1.0 }, ])) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) molecules.append_updater(AddInertiaMoment(atoms)) molecules.append_updater(AddMolecularOrientation()) data = molecules.get_data()[0] self.assertEqual(data["S_x"], 1.0) self.assertEqual(data["S_y"], -0.5) self.assertEqual(data["S_z"], -0.5)
def test_select_one(self): data = [[ {"id": j, "A": float(i+j), "B": float(i*j)} for j in range(1000)] for i in range(100)] elems = [create(StaCustom(d)) for d in data] pro = ProValueArray(elems) pro.select("A") execute_omp(pro) arr_A = pro.get_results()["A"] for i, elem in enumerate(elems): ds = elem.get_data() for d in ds: self.assertEqual(arr_A[i][d["id"]], d["A"])