def test_sqrted(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) molecules.append_updater(AddInertiaMoment(atoms)) molecules.append_updater(AddGyrationRadius().with_sqrted()) data = molecules.get_data()[0] self.assertAlmostEqual(data["Rg"], 1.0) self.assertAlmostEqual(data["Rg(x+y)"], sqrt(2 / 3)) self.assertAlmostEqual(data["Rg(x)"], sqrt(1 / 3)) self.assertEqual(data["Rg(x+y)"], data["Rg(y+z)"]) self.assertEqual(data["Rg(x+y)"], data["Rg(z+x)"]) self.assertEqual(data["Rg(x)"], data["Rg(y)"]) self.assertEqual(data["Rg(x)"], data["Rg(z)"]) self.assertEqual( molecules.get_keys(), { "id", "atom-ids", "mass", "xu", "yu", "zu", "I_xx", "I_yy", "I_zz", "I_xy", "I_xz", "I_yz", "Rg^2", "Rg^2(y+z)", "Rg^2(z+x)", "Rg^2(x+y)", "Rg^2(x)", "Rg^2(y)", "Rg^2(z)", "Rg", "Rg(y+z)", "Rg(z+x)", "Rg(x+y)", "Rg(x)", "Rg(y)", "Rg(z)" })
def test_error01(self): molecules = create(StaMolecules( create(StaDumpBox("dumps_bead/bead.2990000.dump", 2990000)))) self.check_error_msg( "RuntimeError: Missing key(s) 'id', 'mol' in StaMolecules", molecules.get_data)
def _test_positions(self, atoms): mols = create(StaMolecules(atoms)) mols.append_updater(AddCoMPosition(atoms)) mol_data = mols.get_data() target = mol_data[randrange(len(mol_data))] atom_ids = set(target["atom-ids"]) atom_data = atoms.get_data() tmpm = 0.0 tmpx = 0.0 tmpy = 0.0 tmpz = 0.0 for atom in atom_data: if atom["id"] in atom_ids: m = atom["mass"] tmpm += m tmpx += m * atom["xu"] tmpy += m * atom["yu"] tmpz += m * atom["zu"] tmpx /= tmpm tmpy /= tmpm tmpz /= tmpm self.assertEqual(target["xu"], tmpx) self.assertEqual(target["yu"], tmpy) self.assertEqual(target["zu"], tmpz)
def test_squared(self): custom_data = deepcopy(self.custom_data) custom_data[0]["xu"] = 3.0 custom_data[1]["xu"] = -1.0 atoms = create(StaCustom(custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) molecules.append_updater(AddInertiaMoment(atoms)) molecules.append_updater(AddGyrationRadius()) data = molecules.get_data()[0] self.assertAlmostEqual(data["Rg^2"], 2.0) self.assertAlmostEqual(data["Rg^2(x+y)"], 5 / 3) self.assertAlmostEqual(data["Rg^2(y+z)"], 2 / 3) self.assertAlmostEqual(data["Rg^2(z+x)"], 5 / 3) self.assertAlmostEqual(data["Rg^2(x)"], 4 / 3) self.assertAlmostEqual(data["Rg^2(y)"], 1 / 3) self.assertAlmostEqual(data["Rg^2(z)"], 1 / 3) self.assertEqual( molecules.get_keys(), { "id", "atom-ids", "mass", "xu", "yu", "zu", "I_xx", "I_yy", "I_zz", "I_xy", "I_xz", "I_yz", "Rg^2", "Rg^2(y+z)", "Rg^2(z+x)", "Rg^2(x+y)", "Rg^2(x)", "Rg^2(y)", "Rg^2(z)" })
def test_atom_id(self): atoms = create(StaDumpAtoms("dumps_atom/atom.0.dump", 0)) moles = create(StaMolecules(atoms)) mappings = [[0, 1, 2, 12, 13, 14, 15, 16, 17, 18], [3, 4, 5, 19, 20, 21, 22, 23, 24], [6, 7, 8, 25, 26, 27, 28, 29, 30], [9, 10, 11, 31, 32, 33, 34, 35, 36, 37]] abst_beads = [{ "type": 1, "indices-in-mol": mapping, "weights": [1.0] * len(mapping) } for mapping in mappings] beads = create(StaBeads(moles, abst_beads)) self.assertEqual(beads.get_keys(), {"id", "mol", "type", "atom-ids", "atom-weights"}) data = beads.get_data() for d in data: offset = (d["mol"] - 1) * 38 in_mol = (d["id"] - 1) % 4 self.assertEqual(d["atom-ids"], [offset + i + 1 for i in mappings[in_mol]])
def _test_get_data(self, arguments, expected_id, expected_atoms): molecules = create(StaMolecules(create(StaDumpAtoms(*arguments)))) data = molecules.get_data() self.assertEqual(data[expected_id-1]["id"], expected_id) self.assertEqual(set(data[expected_id-1]["atom-ids"]), expected_atoms)
def test_gyration_radius(self): n_samples = 2000 rs = 100*np.random.random_sample((n_samples, 3)) - 50 abst_atoms = [ { "id": i+1, "mol": i//20 + 1, "mass": 12.011, "xu": rs[i,0], "yu": rs[i,1], "zu": rs[i,2] } for i in range(2000)] atoms = create(StaCustom(abst_atoms)) moles = create(StaMolecules(atoms)) moles.append_updater(AddCoMPosition(atoms)) moles.append_updater(AddChildPositions(atoms, "atom")) moles.append_updater(AddInertiaMoment(atoms)) moles.append_updater(AddGyrationRadius()) data = moles.get_data() for d in data: xyzs = d["atom-xs"] + d["atom-ys"] + d["atom-zs"] self.assertAlmostEqual( d["Rg^2"], sum([a*a for a in xyzs])/len(d["atom-ids"]))
def test_error03(self): atoms_traj = [] base_atoms = deepcopy(self.base_data) for i in range(2, 12): atoms_tmp = [] for j in range(1, i): for k, atom in enumerate(base_atoms): atom["id"] = (j-1) * len(base_atoms) + k + 1 atom["mol"] = j atoms_tmp.extend(deepcopy(base_atoms)) 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))) self.check_error_msg( "RuntimeError: Number of molecules and molecular types must be unchanged", execute_omp, pro)
def test_cubic_isotropic(self): abst_atoms = [] mol_id = 0 for ix in range(10): for iy in range(10): for iz in range(10): mol_id += 1 abst_atoms.extend( self.create_rotated_abst_atoms_in_mol( uniform(-0.5, 0.5), 6*(mol_id-1)+1, mol_id, 1.0*ix, 1.0*iy, 1.0*iz)) atoms = create(StaCustom(abst_atoms)) mols = create(StaMolecules(atoms)) mols.append_updater(AddCoMPosition(atoms)) mols.append_updater(AddInertiaMoment(atoms)) box = create(StaCustom({ "lo_x": 0.0, "hi_x": 10.0, "pbc_x": True, "lo_y": 0.0, "hi_y": 10.0, "pbc_y": True, "lo_z": 0.0, "hi_z": 10.0, "pbc_z": True})) mols.append_updater(AddWrappedPosition(box)) pro = ProRDFWD(mols, box) num_bins = 20 bin_width = 0.1 pro.set_bin(bin_width, num_bins) execute_omp(pro) self.assertTrue(np.allclose( pro.get_r_axis(), np.arange(0.0, num_bins*bin_width, bin_width))) expected_rdf = np.zeros(num_bins) data = [ {"index": 10, "num": 6}, {"index": 14, "num": 12}, {"index": 17, "num": 8}] for d in data: r_inner = (d["index"]-0.5) * bin_width r_outer = (d["index"]+0.5) * bin_width dV = (4.0*np.pi/3.0) * ( np.power(r_outer, 3) - np.power(r_inner, 3)) expected_rdf[d["index"]] = (d["num"]/dV)/1.0 self.assertTrue(np.allclose(pro.get_rdf(), expected_rdf))
def test_with_and_without_modification(self): n_traj = 100 n_mols = 100 abst_atoms_traj = [] mol_id = 0 for i in range(n_traj): abst_atoms = [] mol_id = 0 for i in range(n_mols): mol_id += 1 abst_atoms.extend( self.create_rotated_abst_atoms_in_mol( uniform(-1.0, 1.0), 6*(mol_id-1)+1, mol_id, uniform(-50.0, 50.0), uniform(-50.0, 50.0), uniform(-50.0, 50.0))) abst_atoms_traj.append(abst_atoms) atomses = [ create(StaCustom(abst_atoms)) for abst_atoms in abst_atoms_traj] boxes = [ create(StaCustom({ "lo_x": 0.0, "hi_x": 10.0, "pbc_x": True, "lo_y": 0.0, "hi_y": 10.0, "pbc_y": True, "lo_z": 0.0, "hi_z": 10.0, "pbc_z": True })) for i in range(n_traj)] molses = [ create(StaMolecules(atoms)) .append_updater(AddCoMPosition(atoms)) .append_updater(AddInertiaMoment(atoms)) .append_updater(AddWrappedPosition(box)) for atoms, box in zip(atomses, boxes)] pro_with_modify = ProRDFWD(list(zip(molses, boxes))) pro_without_modify = ProRDF(list(zip(molses, boxes))) num_bins = 20 bin_width = 0.1 pro_with_modify.set_bin(bin_width, num_bins) pro_without_modify.set_bin(bin_width, num_bins) execute_omp([pro_with_modify, pro_without_modify]) self.assertTrue(np.allclose( pro_with_modify.get_rdf(), pro_without_modify.get_rdf())) self.assertTrue(np.allclose( np.array(pro_with_modify.get_rdf_traj()), np.array(pro_without_modify.get_rdf_traj())))
def test_error02(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddInertiaMoment(atoms)) check_error_msg( self, "RuntimeError: Missing key(s) 'xu', 'yu', 'zu' in AddInertiaMoment", molecules.get_data)
def test_error01(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddGyrationRadius()) self.check_error_msg( "RuntimeError: Missing key(s) 'I_xx', 'I_yy', 'I_zz', 'mass' in AddGyrationRadius", molecules.get_data)
def test_error01(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddMolecularOrientation()) self.check_error_msg( "RuntimeError: Missing key(s) 'I_xx', 'I_xy', 'I_xz', 'I_yy', 'I_yz', 'I_zz' in AddMolecularOrientation", molecules.get_data)
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_error01(self): atoms = create(StaDumpAtoms("dumps_bead/bead.2990000.dump", 2990000)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) self.check_error_msg( "RuntimeError: Missing key(s) 'mass' in AddCoMPosition", molecules.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_error01(self): atoms = create(StaDumpAtoms("dumps_atom/atom.0.dump", 0)) moles = create(StaMolecules(atoms)) self.check_error_msg( "RuntimeError: Mapping to beads must be specified by 'indices-in-mol'", create, StaBeads(moles, [{ "foo": [1, 2, 3] }]))
def test_for_beads(self): n_samples = 50 interval = 1000 atomses = [ create(StaDumpAtoms("dumps_atom/atom.{}.dump".format(i), i)) for i in range(0, n_samples * interval + 1, interval) ] boxes = [ create(StaDumpBox("dumps_atom/atom.{}.dump".format(i), i)) for i in range(0, n_samples * interval + 1, interval) ] molses = [create(StaMolecules(atoms)) for atoms in atomses] mappings = [[0, 1, 2, 12, 13, 14, 15, 16, 17, 18], [3, 4, 5, 19, 20, 21, 22, 23, 24], [6, 7, 8, 25, 26, 27, 28, 29, 30], [9, 10, 11, 31, 32, 33, 34, 35, 36, 37]] abst_beads = [{ "type": 1, "indices-in-mol": mapping } for mapping in mappings] beadses = [ create(StaBeads(mols, abst_beads)).append_updater( AddCoMPosition(atoms)).append_updater( AddInertiaMoment(atoms)).append_updater( AddWrappedPosition(box)) for atoms, box, mols in zip(atomses, boxes, molses) ] pro = ProRDFWD(list(zip(beadses, boxes))) num_bins = 150 bin_width = 0.1 pro.set_bin(bin_width, num_bins) pro.set_margin(2.0) execute_omp(pro) Rg2s = pro.get_squared_gyration_radius() self.assertTrue( np.allclose(Rg2s["isotropic"], (1 / 3) * Rg2s["parallel"] + (2 / 3) * Rg2s["perpendicular"])) Rg2s_traj = pro.get_squared_gyration_radius_traj() self.assertTrue( np.allclose(np.array(Rg2s_traj["isotropic"]), (1 / 3) * np.array(Rg2s_traj["parallel"]) + (2 / 3) * np.array(Rg2s_traj["perpendicular"])))
def test_error01(self): atoms = create(StaCustom(self.custom_data)) molecules = create(StaMolecules(atoms)) molecules.append_updater(AddCoMPosition(atoms)) molecules.append_updater( AddInertiaMoment(create(StaCustom({"foo": 0, "bar": 1})))) check_error_msg( self, "RuntimeError: Missing key(s) 'id', 'mass', 'xu', 'yu', 'zu' in AddInertiaMoment", molecules.get_data)
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_error02(self): atoms = create(StaDumpAtoms("dumps_atom/atom.0.dump", 0)) moles = create(StaMolecules(atoms)) self.check_error_msg( "RuntimeError: The numbers of elements in 'indices-in-mol' and 'weights' are inconsistent", create, StaBeads(moles, [{ "indices-in-mol": [1, 2, 3, 4], "weights": [1.0, 1.0, 1.0] }]))
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_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.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_error03(self): atoms = create(StaDumpAtoms("dumps_atom/atom.0.dump", 0)) moles = create(StaMolecules(atoms)) check_error_msg( self, "RuntimeError: The number of 'type' is invalid", create, StaBeads(moles, [{ "indices-in-mol": [1, 2], "weights": [1.0, 1.0] }, { "indices-in-mol": [3, 4], "type": 1, "weights": [1.0, 1.0] }]))
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)