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_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_de_morgan(self, arguments, key1, lim1, key2, lim2): atoms = create(StaDumpAtoms(*arguments)) num_total = len(atoms.get_data()) filtered_atoms1 = create( StaCopy(atoms)).append_updater(FilComparison((key1, "<", lim1))) vec1 = filtered_atoms1.get_1d_float(key1) num1 = len(vec1) self.assertTrue(np.all(vec1 < lim1)) filtered_atoms2 = create( StaCopy(atoms)).append_updater(FilComparison((key2, "<", lim2))) vec2 = filtered_atoms2.get_1d_float(key2) num2 = len(vec2) self.assertTrue(np.all(vec2 < lim2)) filtered_atoms = create( StaCopy(atoms)).append_updater( FilComparison([(key1, "<", lim1), (key2, "<", lim2)])) arr = filtered_atoms.get_2d_float(key1, key2) num = arr.shape[0] self.assertTrue(np.all(arr.T[0] < lim1) and np.all(arr.T[1] < lim2)) atoms.append_updater( FilComparison([(key1, ">=", lim1), (key2, ">=", lim2)])) self.assertEqual( len(atoms.get_data()) + num1 + num2 - num, num_total)
def test_single_angle(self): right_angle = 45.0 angles_py = [{"id": 1, "atom1-id": 1, "atom2-id": 2, "atom3-id": 3}] atoms_py = [ { "id": 1, "xu": 1.0, "yu": 1.0, "zu": 0.0 }, { "id": 2, "xu": 1.0, "yu": 0.0, "zu": 0.0 }, { "id": 3, "xu": 1.0, "yu": 1.0, "zu": 1.0 }, ] angles = create(StaCustom(angles_py)) atoms = create(StaCustom(atoms_py)) angles.append_updater(AddBondAngle(atoms)) self.assertTrue(np.allclose(right_angle, angles.get_data()[0]["angle"]))
def test_single_length(self): right_length = 121.0 bonds_py = [{"atom1-id": 1, "atom2-id": 2}] atoms_py = [ { "id": 1, "xu": 0.0, "yu": 0.0, "zu": 0.0 }, { "id": 2, "xu": 0.0, "yu": 0.0, "zu": 121.0 }, ] bonds = create(StaCustom(bonds_py)) atoms = create(StaCustom(atoms_py)) bonds.append_updater(AddBondLength(atoms)) self.assertTrue( np.allclose(bonds.get_data()[0]["length"], right_length))
def test_with_select(self): data = [{"A": i, "B": i * i} for i in range(1000)] data2 = [{"A": i, "B": i * i, "C": i * i * i} for i in range(1000)] elems = [ create(StaCustom({ "A": d["A"], "B": d["B"], "C": d["A"] + d["B"] })) for d in data ] pro = ProData(elems) pro.select("A", "B") 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)] data2 = [[{ "A": j, "B": i * j, "C": i + j } for j in range(100)] for i in range(100)] elems = [create(StaCustom(d)) for d in data] pro = ProData(elems) pro.select("A", "B") execute_omp(pro) self.assertEqual(pro.get_results(), data)
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_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_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_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_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_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(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_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_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)) self.check_error_msg( "RuntimeError: Missing key(s) 'xu', 'yu', 'zu' in AddWrappedPosition", atoms.get_data)
def test_error02(self): atomses = [create(StaCustom(self.base_data)) for i in range(10)] molses = [create(StaCustom([{"id": 1}])) for i in range(10)] pro = ProTimeCorrelationInMolecule(list(zip(molses, atomses))) self.check_error_msg( "RuntimeError: Missing key(s) 'atom-ids' in ProTimeCorrelationInMolecule", 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)) self.check_error_msg( "RuntimeError: Missing key(s) 'hi_x', 'hi_y', 'hi_z', 'lo_x', 'lo_y', 'lo_z' in AddWrappedPosition", atoms.get_data)
def test_error02(self): atoms = create(StaCustom(self.base_data)) mols = create(StaCustom([{"id": 1, "foo": "var"}])) pro = ProDistanceInMolecule(mols, atoms) self.check_error_msg( "RuntimeError: Missing key(s) 'atom-ids' in ProDistanceInMolecule", execute_omp, pro)
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_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_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_positions(self): atoms = create(StaDumpAtoms("dumps_bead/bead.2990000.dump", 2990000)) atoms.append_updater(AddMap("type", "mass", {1: 147.28})) self._test_positions(atoms) atoms = create(StaDumpAtoms("dumps_atom/atom.0.dump", 0)) self._test_positions(atoms)
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_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_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_error01(self): atoms = create(StaCustom([ {"x": 1.0*i, "y": 1.0*i, "z": 1.0*i} for i in range(100)])) box = create(StaCustom({"foo": 0.0, "bar": 1.0})) pro = ProRDFWD(atoms, box) self.check_error_msg( "RuntimeError: Missing key(s) 'I_xx', 'I_xy', 'I_xz', 'I_yy', 'I_yz', 'I_zz', 'id', 'mass' in ProRadialDistributionFunctionWithDeformation", execute_omp, pro)
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_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] }]))