def test_trajectory_write_method(tmpdir): """ Test trajectory class write method """ ref_traj = os.path.join(mof_trial_dir, 'Run1', 'traj.xyz') traj = Trajectory(read=ref_traj) temp_file = tmpdir.join('traj-temp.xyz') traj.write(temp_file.strpath) assert filecmp.cmp(ref_traj, temp_file.strpath)
def test_trajectory_stretch_method(): """ Test trajectory class stretch method for non-interpenetrated ideal MOF """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) xyz_stretch1 = traj.stretch(1) assert len(traj.xyz) == len(xyz_stretch1) xyz_stretch3 = traj.stretch(3) assert len(traj.xyz[0]) == len(xyz_stretch3[0]) assert len(traj.xyz[-1]) == len(xyz_stretch3[-1]) assert len(traj.xyz) * 3 == len(xyz_stretch3)
def load_sample_trajectory(mof='single', tests_dir=tests_dir): """ Load a sample trajectory file from tests directory """ from thermof import Trajectory if mof == 'single': sample_traj = Trajectory(read=os.path.join( tests_dir, 'ideal-mof-trial', 'Run1', 'traj.xyz')) else: sample_traj = Trajectory( read=os.path.join(tests_dir, 'ip-mof-trial', 'Run1', 'traj.xyz')) return sample_traj
def test_trajectory_subdivide_method_example_case(): """ Test trajectory class subdivision for non-interpenetrated ideal MOF example case """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) div_traj = traj.subdivide(atoms=list(range(10, 20)), frames=list(range(5, 10)), dimensions=[1, 2]) assert (div_traj.n_frames, div_traj.n_atoms, div_traj.n_dimensions) == (5, 10, 2) assert div_traj.atoms == [frame[10:20] for frame in traj.atoms[5:10]] assert np.allclose(div_traj.coordinates[0][0], traj.coordinates[5][10][1:]) assert np.allclose(div_traj.coordinates[4][3], traj.coordinates[9][13][1:])
def test_trajectory_stretch_method_with_write(tmpdir): """ Test trajectory class stretch method for non-interpenetrated ideal MOF """ ref_traj = os.path.join(mof_trial_dir, 'Run1', 'traj.xyz') traj = Trajectory(read=ref_traj) with open(ref_traj, 'r') as rt: ref_traj_lines = rt.readlines() temp_file = tmpdir.join('traj-temp.xyz') xyz_stretch = traj.stretch(2, temp_file.strpath) with open(temp_file.strpath, 'r') as tf: tmp_traj_lines = tf.readlines() assert len(tmp_traj_lines) == len(ref_traj_lines) * 2
def test_calculate_mean_squared_disp_for_single_ideal_mof(): """ Test trajectory class calculate_mean_squared_disp function for single ideal mof """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) traj.set_cell([80, 80, 80]) traj.calculate_distances() traj.calculate_mean_squared_disp() assert np.all(traj.mean_squared_disp > 0.0) assert np.all(traj.mean_squared_disp < 0.4)
def test_trajectory_read_method_for_interpenetrated_ideal_mof(): """ Test trajectory class read method for interpenetrated ideal MOF """ traj = Trajectory(read=os.path.join(ipmof_trial_dir, 'Run1', 'traj.xyz')) assert len(traj.xyz[0]) == 7170 assert traj.n_atoms == len(traj.atoms[0]) == 7168 assert len(traj.coordinates[0]) == 7168 assert len(traj) == traj.n_frames == 61 assert traj.coordinates[0][0] == [0, 0, 0] assert np.allclose(traj.coordinates[-1][-1], [74.1941, 74.2373, 0.916302])
def test_trajectory_read_method_for_single_ideal_mof(): """ Test trajectory class read method for non-interpenetrated ideal MOF """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) assert len(traj.xyz[0]) == 3586 assert traj.n_atoms == len(traj.atoms[0]) == 3584 assert len(traj.coordinates[0]) == 3584 assert len(traj) == traj.n_frames == 61 assert traj.coordinates[0][0] == [0, 0, 0] assert np.allclose(traj.coordinates[-1][-1], [69.4517, 69.960, 76.5111])
def test_trajectory_change_atoms_for_interpenetrated_ideal_mof(): """ Test trajectory class change atoms method for interpenetrated ideal MOF """ traj = Trajectory(read=os.path.join(ipmof_trial_dir, 'Run1', 'traj.xyz')) assert Counter(traj.get_unique_atoms()) == Counter(['1', '2']) traj.change_atoms({'1': 'C', '2': 'O'}) assert Counter(traj.get_unique_atoms()) == Counter(['C', 'O']) ref_atoms = ['C'] * int(traj.n_atoms / 2) + ['O'] * int(traj.n_atoms / 2) assert [line.split()[0] for line in traj.xyz[0][2:]] == ref_atoms assert traj.atoms[0] == ref_atoms
def test_trajectory_change_atoms_for_single_ideal_mof(): """ Test trajectory class change atoms method for non-interpenetrated ideal MOF """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) assert traj.get_unique_atoms() == ['1'] traj.change_atoms({'1': 'C'}) assert traj.get_unique_atoms() == ['C'] ref_atoms = ['C'] * traj.n_atoms assert [line.split()[0] for line in traj.xyz[0][2:]] == ref_atoms assert traj.atoms[0] == ref_atoms
def test_calculate_distances_with_trajectory_calculate_distances(max_dist=5): """ Tests interatomic distance calculation for the Trajectory class - Test first frame is all zeros - Make sure all distances are less than max distance (5 A) - Test a known distance """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) traj.set_cell([80, 80, 80]) traj.calculate_distances() distances = calculate_distances(traj.coordinates, [80, 80, 80], reference_frame=0) assert np.allclose(traj.distances, distances)
def test_trajectory_calculate_distances_nonzero_reference_frame(max_dist=5): """ Tests interatomic distance calculation for the Trajectory class - Test seventh frame (reference frame) is all zeros - Make sure all distances are less than max distance (5 A) - Test a known distance """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) traj.set_cell([80, 80, 80]) traj.calculate_distances(reference_frame=7) assert np.allclose(traj.distances[7], np.zeros(traj.n_atoms)) for frame in traj.distances: assert np.all(frame < max_dist) assert np.isclose(traj.distances[0][3], 0.23030113908032163)
def test_trajectory_calculate_distances(max_dist=5): """ Tests interatomic distance calculation for the Trajectory class - Test first frame is all zeros - Make sure all distances are less than max distance (5 A) - Test a known distance """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) traj.set_cell([80, 80, 80]) traj.calculate_distances() assert np.allclose(traj.distances[0], np.zeros(traj.n_atoms)) for frame in traj.distances: assert np.all(frame < max_dist) assert np.isclose(traj.distances[3][3], 0.5159079779340517)
def test_trajectory_subdivide_method_returns_same_trajectory_without_kwargs(): """ Test trajectory class subdivision method for non-interpenetrated ideal MOF with no kwargs """ traj = Trajectory(read=os.path.join(mof_trial_dir, 'Run1', 'traj.xyz')) div_traj = traj.subdivide() assert traj == div_traj
def test_calculate_mean_disp_and_mean_squared_disp_for_linear_motion_of_particles(): """ Test trajectory class calculate_mean_disp and calculate_mean_squared_disp functions for 6 particles with linear motion with alternative calculation """ n_atoms, n_frames = 6, 6 traj = Trajectory() traj.coordinates = [[[i + j, i + j, i + j] for i in range(n_atoms)] for j in range(n_frames)] traj.n_atoms, traj.n_frames = n_atoms, n_frames traj.set_cell([10, 10, 10]) traj.calculate_distances() traj.calculate_mean_disp() traj.calculate_mean_squared_disp() assert np.allclose([np.sum(range(n_frames)) / n_frames * np.sqrt(3)] * n_atoms, traj.mean_disp) for i in range(traj.n_atoms): c0 = traj.coordinates[0][i] x2 = (np.array([f[i][0] for f in traj.coordinates]) - c0[0]) ** 2 y2 = (np.array([f[i][1] for f in traj.coordinates]) - c0[1]) ** 2 z2 = (np.array([f[i][2] for f in traj.coordinates]) - c0[2]) ** 2 mean_squared_disp = sum(x2 + y2 + z2) / n_frames mean_disp = sum(np.sqrt(x2 + y2 + z2)) / n_frames assert np.allclose(mean_disp, traj.mean_disp[i]) assert np.allclose(mean_squared_disp, traj.mean_squared_disp[i])
def test_center_of_mass_for_interpenetrated_ideal_mof(): """ Test trajectory class read method for interpenetrated ideal MOF """ traj = Trajectory(read=os.path.join(ipmof_trial_dir, 'Run1', 'traj.xyz')) traj.change_atoms({'1': 'C', '2': 'O'}) traj.calculate_com() assert np.isclose(traj.com[0][0], traj.com[0][1], traj.com[0][2])