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_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_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_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): 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_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(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_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_error02(self): atoms = create( StaCustom([{ "id": i, "mol": i // 10 } for i in range(100)])) moles = create(StaCustom([{"index": i} for i in range(10)])) moles.append_updater(AddChildIDs(atoms, "atom", "mol")) self.check_error_msg( "RuntimeError: Missing key(s) 'id' in AddChildIDs", moles.get_data)
def test_20x30_with_shift(self): atoms = [ create( StaCustom([{ "x": 1.0 * ix, "y": 1.0 * iy - 10.0, "z": 1.0 * i, "radius": 1.0 } for ix in range(20) for iy in range(30)])) for i in range(10) ] box = [ create( StaCustom({ "lo_x": 0.0, "hi_x": 20.0, "lo_y": -10.0, "hi_y": 20.0 })) for i in range(10) ] pro = ProThicknessProfile(list(zip(atoms, box))) pro.set_grid(20, 30) pro.shift_half_delta() execute_omp(pro) conditions = pro.get_conditions() for c in conditions: self.assertEqual( c, { "origin_x": 0.5, "origin_y": -9.5, "delta_x": 1.0, "delta_y": 1.0, "nx": 20, "ny": 30 }) profiles = pro.get_profiles() addition = 0.5 * sqrt(2) for i, p in enumerate(profiles): self.assertTrue( np.allclose(p, np.full((20, 30), float(i) + addition)))
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 = ProRadialDistributionFunction(atoms, box) self.check_error_msg( "RuntimeError: Missing key(s) 'id' in ProRadialDistributionFunction", execute_omp, pro)
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 = ProThicknessProfile(atoms, box) self.check_error_msg( "RuntimeError: Missing key(s) 'radius' in ProThicknessProfile", execute_omp, pro)
def test_error03(self): elem = create(StaCustom({"foo": 0, "bar": 1})) elem.append_updater(AddRename("foo", "id")) self.check_error_msg( "RuntimeError: Adder cannot add 'id'", elem.get_data)
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_select_two(self): ids = list(range(1000)) shuffle(ids) data = [[ {"id": j, "A": float(i+j), "B": float(i*j)} for j in ids] for i in range(100)] elems = [create(StaCustom(d)) for d in data] pro = ProValueArray(elems) pro.select("A", "B") execute_omp(pro) arrs = pro.get_results() for i, elem in enumerate(elems): ds = elem.get_data() for d in ds: self.assertEqual(arrs["A"][i][d["id"]], d["A"]) self.assertEqual(arrs["B"][i][d["id"]], d["B"])
def test_error01(self): elem = create(StaCustom({"foo": 0, "bar": 1})) elem.append_updater(AddRename("dummy", "new")) check_error_msg( self, "RuntimeError: Missing key(s) 'dummy' in AddRename", elem.get_data)
def test_error01(self): elem = create(StaCustom({"foo": 0, "bar": 1})) elem.append_updater(AddMap("dummy", "new", {1: 0})) self.check_error_msg("RuntimeError: Missing key(s) 'dummy' in AddMap", elem.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_error02(self): elem = create(StaCustom({"foo": 0, "bar": 1})) elem.append_updater(AddMap("foo", "bar", {0: 1})) self.check_error_msg("RuntimeError: Key 'bar' already exists", elem.get_data)
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_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_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_error02(self): atoms = create( StaCustom([{ "id": i + 1, "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 = ProRadialDistributionFunction(atoms, box) check_error_msg( self, "RuntimeError: Missing key(s) 'hi_x', 'hi_y', 'hi_z', 'lo_x', 'lo_y', 'lo_z' in ProRadialDistributionFunction", execute_omp, pro)
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): 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): elems = [create(StaCustom({"A": i, "B": i * i})) for i in range(1000)] pro = ProData(elems) pro.select("C") check_error_msg(self, "RuntimeError: Missing key(s) 'C' in ProData", execute_omp, pro)
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_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_error01(self): elems = [ create(StaCustom({"id": i, "A": i*i})) for i in range(1000)] pro = ProValueArray(elems) check_error_msg( self, "RuntimeError: Selected value(s) for ProValueArray", execute_omp, pro)