def test_run(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") inp = InputParams({"dft": {"rmult": [5, 7], "hgrids": 0.55}}) base = Job(posinp=pos, inputparams=inp, name="N2", run_dir="tests/rmult_convergence_N2") rmc = RmultConvergence(base, [8, 11], [-1, -1], n_jobs=4, precision_per_atom=0.01 * EV_TO_HA) assert not rmc.is_completed rmc.run(nmpi=6, nomp=3) assert rmc.is_completed # The correct minimum rmult is found assert rmc.converged.param == [6., 9.] # The output energies are correct expected_energies = [-19.871104, -19.871032, -19.870892, -19.869907] energies = [job.logfile.energy for job in rmc.queue] np.testing.assert_array_almost_equal(energies, expected_energies) # Print the summary of the workflow rmc.summary() # Test that running the workflow again warns a UserWarning with pytest.warns(UserWarning): rmc.run()
def test_run(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") inp = InputParams({"dft": {"rmult": [5, 7]}}) base = Job(inputparams=inp, posinp=pos, name="N2", run_dir="tests/hgrids_convergence_N2") hgc = HgridsConvergence(base, 0.36, 0.02, n_jobs=8, precision_per_atom=0.01 * EV_TO_HA) assert not hgc.is_completed hgc.run(nmpi=6, nomp=3) assert hgc.is_completed # The correct maximum hgrids is found assert hgc.converged.param == [0.38] * 3 # The output energies are correct expected_energies = [-19.888197, -19.887715, -19.887196, 0, 0, 0, 0, 0] energies = [ job.logfile.energy if job.is_completed else 0 for job in hgc.queue ] np.testing.assert_array_almost_equal(energies, expected_energies) # Print the summary of the workflow hgc.summary() # Test that running the workflow again warns a UserWarning with pytest.warns(UserWarning): hgc.run()
def test___eq__(self): atom1 = Atom('N', [0.0, 0.0, 0.0]) atom2 = Atom('N', [0.0, 0.0, 1.1]) pos1 = Posinp([atom1, atom2], 'angstroem', 'free') pos2 = Posinp([atom2, atom1], 'angstroem', 'free') assert pos1 == pos2 # The order of the atoms in the list do not count assert pos1 != 1 # No error if other object is not a posinp
def test_run(self): frag1 = Posinp([Atom('N', [0.0, 0.0, 0.0])], units="angstroem", boundary_conditions="free") frag2 = frag1 distances = np.arange(0.95, 1.25, 0.05) dc = Dissociation(frag1, frag2, distances, name="N2", run_dir="tests/dissociation_N2") assert not dc.is_completed dc.run(nmpi=6, nomp=3) assert dc.is_completed # The first job has the correct posinp atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0.95, 0])] expected_pos = Posinp( atoms, units="angstroem", boundary_conditions="free") assert expected_pos == dc.queue[0].posinp # The correct minimum distance is found assert dc.minimum.distance == 1.1 # The output energies are correct expected_energies = [ -19.805444659275025, -19.85497382791818, -19.878933352041976, -19.884549270710195, -19.87716483741823, -19.86087438302968, -19.838574516454962 ] np.testing.assert_array_almost_equal(dc.energies, expected_energies) # Test that running the workflow again warns a UserWarning with pytest.warns(UserWarning): dc.run()
def test_to_barycenter(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") expected_atoms = [Atom('N', [0, 0, -0.55]), Atom('N', [0, 0, 0.55])] expected_pos = Posinp(expected_atoms, units="angstroem", boundary_conditions="free") assert pos.to_barycenter() == expected_pos
def test_init_raises_ValueError(self): frag1 = Posinp([Atom('N', [0.0, 0.0, 0.0])], units="angstroem", boundary_conditions="free") frag2 = Posinp([Atom('N', [0.0, 0.0, 0.0])], cell=[8, 8, 8], units="angstroem", boundary_conditions="periodic") distances = np.arange(0.95, 1.25, 0.05) with pytest.raises(ValueError): Dissociation(frag1, frag2, distances)
def dry_gwf(self): new_pos = Posinp([Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])], units="angstroem", boundary_conditions="free") base_job = Job(posinp=new_pos, name="N2", run_dir="tests/geopt_N2/dry") gwf = Geopt(base_job, maxrise=0.5) yield gwf with gwf.queue[0] as job: job.clean() os.rmdir(job.run_dir)
def test_init(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") base = Job(posinp=pos, name="N2", run_dir="tests/rmult_convergence_N2") rmc = RmultConvergence(base, [6, 8], [0.5, 0.5], n_jobs=3, precision_per_atom=0.01 * EV_TO_HA) rmults = [job.param for job in rmc.queue] expected = [[6.0, 8.0], [5.5, 7.5], [5.0, 7.0]] np.testing.assert_array_almost_equal(rmults, expected)
def test_run_warns_UserWarning_too_low_energy(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") inp = InputParams({"dft": {"rmult": [5, 7], "hgrids": 0.55}}) base = Job(posinp=pos, inputparams=inp, name="N2", run_dir="tests/rmult_convergence_N2") rmc = RmultConvergence(base, [9, 12], [-1, -1], n_jobs=2, precision_per_atom=0.01 * EV_TO_HA) with pytest.warns(UserWarning): rmc.run(nmpi=6, nomp=3)
def test_init(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") base = Job(posinp=pos, name="N2", run_dir="tests/hgrids_convergence_N2") hgc = HgridsConvergence(base, 0.36, 0.02, n_jobs=8, precision_per_atom=0.01 * EV_TO_HA) hgrids = [job.param for job in hgc.queue] expected = [[0.36 + i * 0.02] * 3 for i in range(8)] np.testing.assert_array_almost_equal(hgrids, expected)
def test_set_posinp_key(self): inp = InputParams() inp["posinp"] = { "units": "angstroem", "positions": [{ 'N': [0.0, 0.0, 0.0] }, { 'N': [0.0, 0.0, 1.1] }] } assert inp.posinp == Posinp( [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])], units="angstroem", boundary_conditions="free")
def test_run(self): atoms = [Atom('N', [3.571946174, 3.571946174, 3.620526682]), Atom('N', [3.571946174, 3.571946174, 4.71401439])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") gs = Job(posinp=pos, name='N2', run_dir='tests/phonons_N2') ph = Phonons(gs) ir = InfraredSpectrum(ph) assert not ir.is_completed ir.run() assert ir.is_completed # Test the only physically relevant infrared intensity i = np.argmax(ir.energies) np.testing.assert_almost_equal(ir.intensities[i], 1.100446469749e-06) # Test that running the workflow again warns a UserWarning with pytest.warns(UserWarning): ir.run()
def place_first_nitrogen(posinp): distances_to_middle = np.linalg.norm(posinp.positions - np.array(posinp.cell) / 2, axis=1) idx = np.argmin(distances_to_middle) posinp.atoms[idx] = Atom("N", posinp.atoms[idx].position) return posinp, idx
def test_positions(self): expected = [7.327412521, 0.0, 3.461304757] pos1 = Posinp([Atom('C', expected)], units="angstroem", boundary_conditions="free") pos2 = pos1.translate_atom(0, [-7.327412521, 0.0, -3.461304757]) assert np.allclose(pos1.positions, expected) assert np.allclose(pos2.positions, [0, 0, 0])
def test_run(self): new_pos = Posinp([Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])], units="angstroem", boundary_conditions="free") base_job = Job(posinp=new_pos, name="N2", run_dir="tests/geopt_N2") gwf = Geopt(base_job, maxrise=0.5) assert not gwf.is_completed gwf.run(nomp=3, nmpi=6) assert gwf.is_completed expected_pos = Posinp( [Atom('N', [-3.5879386957696453e-22, -2.564986669721281e-21, 0.0032486860088205526]), Atom('N', [2.5251356228246713e-22, 2.423609962870671e-21, 1.0967513378330371])], "angstroem", "free", cell=None) assert gwf.final_posinp == expected_pos # Test that running the workflow again warns a UserWarning with pytest.warns(UserWarning): gwf.run()
def place_second_nitrogen(initpos, theta, r, first_idx): posinp = deepcopy(initpos) point = np.array(posinp.cell) / 2 + np.array( [r * np.cos(theta), 0, r * np.sin(theta)]) distances_to_point = np.linalg.norm(posinp.positions - point, axis=1) weigths = np.exp(-(distances_to_point**2) / 2) weigths[first_idx] = 0 weigths = weigths / np.sum(weigths) second_idx = int(np.random.choice(len(posinp), 1, p=weigths)) posinp.atoms[second_idx] = Atom("N", posinp.atoms[second_idx].position) return posinp, second_idx
def test_run(self): atoms = [Atom('N', [3.571946174, 3.571946174, 3.620526682]), Atom('N', [3.571946174, 3.571946174, 4.71401439])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") gs = Job(posinp=pos, name='N2', run_dir='tests/phonons_N2') ph = Phonons(gs) ir = InfraredSpectrum(ph) vpt = VibPolTensor(ir) assert not vpt.is_completed vpt.run() assert vpt.is_completed # Test the mean vibrational polarizability np.testing.assert_almost_equal(vpt.mean_polarizability, 1.092296473682357e-08) # Changing the value of e_cut changes the value of the mean # vibrational polarizability vpt.e_cut = 0 np.testing.assert_almost_equal(vpt.mean_polarizability, 0.0007775548377767935) # Test that running the workflow again warns a UserWarning with pytest.warns(UserWarning): vpt.run()
class TestPosinp: # Posinp with surface boundary conditions surface_filename = os.path.join(tests_fol, "surface.xyz") pos = Posinp.from_file(surface_filename) # Posinp with free boundary conditions free_filename = os.path.join(tests_fol, "free.xyz") free_pos = Posinp.from_file(free_filename) # Posinp read from a string string = """\ 4 atomic free C 0.6661284109 0.000000000 1.153768252 C 3.330642055 0.000000000 1.153768252 C 4.662898877 0.000000000 3.461304757 C 7.327412521 0.000000000 3.461304757""" str_pos = Posinp.from_string(string) # Posinp read from an N2 calculation of bad quality log_pos = Logfile.from_file(logname).posinp # Posinp read from an InputParams instance with surface BC surf_inp = InputParams({ 'posinp': { 'units': 'angstroem', 'cell': [8.0, '.inf', 8.0], 'positions': [{ 'C': [0.0, 0.0, 0.0] }] } }) surf_pos = surf_inp.posinp # Posinp read from an InputParams instance with periodic BC per_inp = InputParams({ 'posinp': { 'units': 'angstroem', 'cell': [8.0, 1.0, 8.0], 'positions': [{ 'C': [0.0, 0.0, 0.0] }] } }) per_pos = per_inp.posinp @pytest.mark.parametrize("value, expected", [ (len(pos), 4), (pos.units, "reduced"), (len(pos), 4), (pos.boundary_conditions, "surface"), (pos.cell, [8.07007483423, 'inf', 4.65925987792]), (pos[0], Atom('C', [0.08333333333, 0.5, 0.25])), ]) def test_from_file(self, value, expected): assert value == expected @pytest.mark.parametrize("value, expected", [ (len(log_pos), 2), (log_pos.units, "angstroem"), (len(log_pos), 2), (log_pos.boundary_conditions, "free"), (log_pos.cell, None), (log_pos[0], Atom('N', [ 2.9763078243490115e-23, 6.872205952043537e-23, 0.01071619987487793 ])), ]) def test_from_Logfile(self, value, expected): assert value == expected @pytest.mark.parametrize("value, expected", [ (len(surf_pos), 1), (surf_pos.units, "angstroem"), (len(surf_pos), 1), (surf_pos.boundary_conditions, "surface"), (surf_pos.cell, [8, "inf", 8]), (surf_pos[0], Atom('C', [0, 0, 0])), ]) def test_from_surface_InputParams(self, value, expected): assert value == expected @pytest.mark.parametrize("value, expected", [ (len(per_pos), 1), (per_pos.units, "angstroem"), (len(per_pos), 1), (per_pos.boundary_conditions, "periodic"), (per_pos.cell, [8, 1.0, 8]), (per_pos[0], Atom('C', [0, 0, 0])), ]) def test_from_periodic_InputParams(self, value, expected): assert value == expected def test_from_string(self): assert self.str_pos == self.free_pos def test_repr(self): atoms = [Atom('C', [0, 0, 0]), Atom('N', [0, 0, 1])] new_pos = Posinp(atoms, units="angstroem", boundary_conditions="free") msg = "Posinp([Atom('C', [0.0, 0.0, 0.0]), Atom('N', [0.0, 0.0, "\ "1.0])], 'angstroem', 'free', cell=None)" assert repr(new_pos) == msg def test_write(self): fname = os.path.join(tests_fol, "test.xyz") self.pos.write(fname) assert self.pos == Posinp.from_file(fname) os.remove(fname) def test_free_boundary_conditions_has_no_cell(self): assert self.free_pos.cell is None def test_translate_atom(self): new_pos = self.pos.translate_atom(0, [0.5, 0, 0]) assert new_pos != self.pos assert new_pos[0] == Atom("C", [0.58333333333, 0.5, 0.25]) @pytest.mark.parametrize("fname", [ "free_reduced.xyz", "missing_atom.xyz", "additional_atom.xyz", ]) def test_init_raises_ValueError(self, fname): with pytest.raises(ValueError): Posinp.from_file(os.path.join(tests_fol, fname)) @pytest.mark.parametrize("to_evaluate", [ "Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic')", "Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic', cell=[1, 1])", "Posinp([Atom('C', [0, 0, 0])], 'bohr', 'periodic', cell=[1,'inf',1])", ]) def test_init_raises_ValueError2(self, to_evaluate): with pytest.raises(ValueError): eval(to_evaluate) def test_positions(self): expected = [7.327412521, 0.0, 3.461304757] pos1 = Posinp([Atom('C', expected)], units="angstroem", boundary_conditions="free") pos2 = pos1.translate_atom(0, [-7.327412521, 0.0, -3.461304757]) assert np.allclose(pos1.positions, expected) assert np.allclose(pos2.positions, [0, 0, 0]) def test___eq__(self): atom1 = Atom('N', [0.0, 0.0, 0.0]) atom2 = Atom('N', [0.0, 0.0, 1.1]) pos1 = Posinp([atom1, atom2], 'angstroem', 'free') pos2 = Posinp([atom2, atom1], 'angstroem', 'free') assert pos1 == pos2 # The order of the atoms in the list do not count assert pos1 != 1 # No error if other object is not a posinp def test_with_surface_boundary_conditions(self): # Two Posinp instances with surface BC are the same even if they # have a different cell size along y-axis pos_with_inf = Posinp([ Atom('N', [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ]), Atom('N', [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ]) ], 'angstroem', 'surface', cell=[40, ".inf", 40]) pos_wo_inf = Posinp([ Atom('N', [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ]), Atom('N', [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ]) ], 'angstroem', 'surface', cell=[40, 40, 40]) assert pos_with_inf == pos_wo_inf # They are obviously different if the cell size along the other # directions are not the same pos2_wo_inf = Posinp([ Atom('N', [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ]), Atom('N', [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ]) ], 'angstroem', 'surface', cell=[20, "inf", 40]) assert pos_with_inf != pos2_wo_inf # They still have the same BC assert \ pos2_wo_inf.boundary_conditions == pos_with_inf.boundary_conditions # You can only have a cell with ".inf" in 2nd positiion to # initialize a calculation with surface BC without using a # Posinp instance inp_with_inf = InputParams({ "posinp": { "units": "angstroem", "cell": [40, ".inf", 40], "positions": [ { 'N': [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ] }, { 'N': [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ] }, ] } }) assert pos_with_inf == inp_with_inf.posinp def test_to_centroid(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") expected_atoms = [Atom('N', [0, 0, -0.55]), Atom('N', [0, 0, 0.55])] expected_pos = Posinp(expected_atoms, units="angstroem", boundary_conditions="free") assert pos.to_centroid() == expected_pos def test_to_barycenter(self): atoms = [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])] pos = Posinp(atoms, units="angstroem", boundary_conditions="free") expected_atoms = [Atom('N', [0, 0, -0.55]), Atom('N', [0, 0, 0.55])] expected_pos = Posinp(expected_atoms, units="angstroem", boundary_conditions="free") assert pos.to_barycenter() == expected_pos
def test_translate_atom(self): new_pos = self.pos.translate_atom(0, [0.5, 0, 0]) assert new_pos != self.pos assert new_pos[0] == Atom("C", [0.58333333333, 0.5, 0.25])
from __future__ import absolute_import import os import pytest import numpy as np from mybigdft import Atom, Posinp, Job, InputParams from mybigdft.workflows import ( PolTensor, Phonons, RamanSpectrum, Geopt, Dissociation, InfraredSpectrum, VibPolTensor, ) from mybigdft.workflows.workflow import Workflow pos = Posinp( [Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.095])], 'angstroem', 'free') class TestWorkflow: def test_init(self): wf = Workflow() assert wf.queue == [] def test_run(self): wf = Workflow() wf.run() assert wf.completed assert wf.is_completed class TestPolTensor: # Polarizibility tensor workflow which is not run
def test_with_surface_boundary_conditions(self): # Two Posinp instances with surface BC are the same even if they # have a different cell size along y-axis pos_with_inf = Posinp([ Atom('N', [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ]), Atom('N', [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ]) ], 'angstroem', 'surface', cell=[40, ".inf", 40]) pos_wo_inf = Posinp([ Atom('N', [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ]), Atom('N', [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ]) ], 'angstroem', 'surface', cell=[40, 40, 40]) assert pos_with_inf == pos_wo_inf # They are obviously different if the cell size along the other # directions are not the same pos2_wo_inf = Posinp([ Atom('N', [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ]), Atom('N', [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ]) ], 'angstroem', 'surface', cell=[20, "inf", 40]) assert pos_with_inf != pos2_wo_inf # They still have the same BC assert \ pos2_wo_inf.boundary_conditions == pos_with_inf.boundary_conditions # You can only have a cell with ".inf" in 2nd positiion to # initialize a calculation with surface BC without using a # Posinp instance inp_with_inf = InputParams({ "posinp": { "units": "angstroem", "cell": [40, ".inf", 40], "positions": [ { 'N': [ 2.97630782434901e-23, 6.87220595204354e-23, 0.0107161998748779 ] }, { 'N': [ -1.10434491945017e-23, -4.87342174483075e-23, 1.10427379608154 ] }, ] } }) assert pos_with_inf == inp_with_inf.posinp
def test_repr(self): atoms = [Atom('C', [0, 0, 0]), Atom('N', [0, 0, 1])] new_pos = Posinp(atoms, units="angstroem", boundary_conditions="free") msg = "Posinp([Atom('C', [0.0, 0.0, 0.0]), Atom('N', [0.0, 0.0, "\ "1.0])], 'angstroem', 'free', cell=None)" assert repr(new_pos) == msg