示例#1
0
def test_get_dft_results_at_gcut_socorrofail():
    """
    Reads energies and forces correctly frm socorro diaryf
    files for every run at corrent gcut
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # set up two mock dft runs
        main_pp_path_list = []
        main_argvf_template_path = ''
        main_crystal_template_path = ''
        pos = []
        gcut = -1

        run1 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut)
        mock_run_dir_1 = 'dir_1'
        os.mkdir(mock_run_dir_1)
        run1.run_dir = mock_run_dir_1

        run2 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut)
        mock_run_dir_2 = 'dir_2'
        os.mkdir(mock_run_dir_2)
        run2.run_dir = mock_run_dir_2

        # move mock diaryf files to run dirs
        shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_1'), 
                    os.path.join(mock_run_dir_1, 'diaryf'))
        shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_none'), 
                    os.path.join(mock_run_dir_2, 'diaryf'))

        with pytest.raises(eval_pp.SocorroFail):
            _,_ = eval_pp.get_dft_results_at_gcut([run1, run2])
示例#2
0
def test_run_socorro():
    """
    Set up socorro input files, run socorro, check force/energy results

    note: this might fail if socorro versions change because different
    versions could give different reults even with the same input,
    especially since total E is relative. Could I mock socorro instead?
    Alternatively, I could run socorro twice, the second time with pre-
    setup input files, and compare results
    """
    inputs = test_inputs_dir + '/SiGe_single_run'
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # create object to test
        pp_path_list = [inputs + '/PAW.Si', inputs + '/PAW.Ge']
        argvf_template_path = inputs + '/argvf.template'
        crystal_template_path = inputs + '/crystal.template'
        pos = [[0.0, 0, '0.1'], [0.24, 0.25, 0.26]]
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path,
                                 crystal_template_path, pos, 10.)
        testrun.setup_files()
        with open('socorro.log', 'w') as fout:
            p = testrun.run_socorro(fout)
        p.wait()

        #assert force and energy reults
        correct_energy = -312.593586340
        correct_forces = np.array(
            [0.246519, 0.247743, 0.243064, -0.246519, -0.247743, -0.243064])
        assert np.isclose(testrun.read_energy(), correct_energy)
        assert np.isclose(testrun.read_forces(), correct_forces,
                          atol=1.e-5).all()
示例#3
0
def test_setup_files():
    """ 
    test: setup_dir method makes files for socorro run

    Expected behavior: calling setup_dir method will create correctly
    preprocessed argvf file in current directory, directory named data/
    and correctly preprocessed data/crystal file.
    """
    correct_argvf = test_inputs_dir+'/argvf.example1'
    correct_crystal = test_inputs_dir+'/crystal.example1'
    correct_Si = test_inputs_dir+'/PAW.Si'
    correct_Ge = test_inputs_dir+'/PAW.Ge'
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # create object to test
        pp_path_list = [test_inputs_dir+'/PAW.Si',
                        test_inputs_dir+'/PAW.Ge']
        argvf_template_path = test_inputs_dir+'/argvf.template.example1'
        crystal_template_path = test_inputs_dir+'/crystal.template.example1'
        pos = [[0.0, 0, '0.1'], [0.5, 0.6, 0.7]]
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, 
                                 crystal_template_path, pos, 30.)
        testrun.setup_files()
        # compare preprocessed files with correct file examples
        with open('argvf') as f1, open(correct_argvf) as f2:
            assert f1.read() == f2.read()
        with open('data/crystal') as f1, open(correct_crystal) as f2:
            assert map(str.split, f1.readlines()) == map(str.split, f2.readlines())
        with open('data/PAW.Si') as f1, open(correct_Si) as f2:
            assert f1.read() == f2.read()
        with open('data/PAW.Ge') as f1, open(correct_Ge) as f2:
            assert f1.read() == f2.read()
示例#4
0
def test_symlink_pseudopotentials():
    """
    symlinks pseudopotentials into data/ directory and compares to 
    original pseudopotential files. Obviously they'll be the same
    but this makes sure the file operations work
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # create object to test
        pp_path_list = [test_inputs_dir+'/PAW.Si', 
                        test_inputs_dir+'/PAW.Ge']
        argvf_template_path = ''
        crystal_template_path = ''
        correct_argvf = ''
        correct_crystal = ''
        pos = [] 
        gcut = -1
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, 
                                 crystal_template_path, pos, gcut)
        # call _symlink_pseudopotentials method
        os.mkdir('data')
        testrun._symlink_pseudopotentials()
        # check pseudopotential files have been copied to data/
        with open('data/PAW.Si') as f1, open(pp_path_list[0]) as f2:
            assert f1.read() == f2.read()
        with open('data/PAW.Ge') as f1, open(pp_path_list[1]) as f2:
            assert f1.read() == f2.read()
示例#5
0
def test_preproc_argvf():
    """ make sure {gcut} and {4gcut} are replaced in text correctly """
    gcut = 40.
    example_template = 'tests/test_inputs/argvf.template.example1'
    testrun = eval_pp.DftRun([], example_template, '', [], gcut)
    with open(example_template) as fin:
        tmplt_txt = fin.readlines()
    assert testrun._preproc_argvf(tmplt_txt) == ['asdfsd\n', 
     'asdf 40.0\n', 'asdfas 160.0\n', '\n', '\n', 'lkjlj\n']
示例#6
0
def test_get_dft_results_at_gcut():
    """
    Reads energies and forces correctly frm socorro diaryf
    files for every run at corrent gcut
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # set up two mock dft runs
        main_pp_path_list = []
        main_argvf_template_path = ''
        main_crystal_template_path = ''
        pos = []
        gcut = -1

        run1 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut)
        mock_run_dir_1 = 'dir_1'
        os.mkdir(mock_run_dir_1)
        run1.run_dir = mock_run_dir_1

        run2 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut)
        mock_run_dir_2 = 'dir_2'
        os.mkdir(mock_run_dir_2)
        run2.run_dir = mock_run_dir_2

        # move mock diaryf files to run dirs
        shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_1'), 
                    os.path.join(mock_run_dir_1, 'diaryf'))
        shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_2'), 
                    os.path.join(mock_run_dir_2, 'diaryf'))

        dft_results = eval_pp.get_dft_results_at_gcut([run1, run2])
        correct_energy_list = [-20000., -738.821147137]
        correct_forces_1 = np.array([0.1, -20.0, -0.08, -1000., 0.3, 0.9])
        correct_forces_2 = np.array([0.007170, -0.015092, -0.069756, -0.007170, 0.015092, 0.069756])
        assert np.isclose(dft_results['energies'], correct_energy_list).all()
        assert np.isclose(dft_results['forces'][0], correct_forces_1).all()
        assert np.isclose(dft_results['forces'][1], correct_forces_2).all()
示例#7
0
def test_get_energy_none():
    """
    should return None because no cell energy found in socorro output
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # set up fake dft_run
        pp_path_list = []
        argvf_template_path = ''
        crystal_template_path = '' 
        pos = []
        gcut = -1
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path,
                                 crystal_template_path, pos, gcut)
        energy_in = testrun.read_energy(test_inputs_dir+'/diaryf.test_get_energy_none')
        assert energy_in is None
示例#8
0
def test_get_forces_none():
    """
    Read forces from example socorro ouput and check value is correct.
    Run inside temporary directory.
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # set up fake dft_run
        pp_path_list = []
        argvf_template_path = ''
        crystal_template_path = '' 
        pos = []
        gcut = -1
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path,
                                 crystal_template_path, pos, gcut)
        forces_in = testrun.read_forces(test_inputs_dir+'/diaryf.test_get_forces_none')
        assert forces_in is None
示例#9
0
def test_get_energy():
    """
    Read energy from example socorro ouput and check value is correct.
    Run inside temporary directory.
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # set up fake dft_run
        pp_path_list = []
        argvf_template_path = ''
        crystal_template_path = '' 
        pos = []
        gcut = -1
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path,
                                 crystal_template_path, pos, gcut)
        energy_in = testrun.read_energy(test_inputs_dir+'/diaryf.test_get_energy')
        assert isclose(energy_in, -738.821147137)
示例#10
0
def test_get_forces():
    """
    Read forces from example socorro ouput and check value is correct.
    Run inside temporary directory.
    """
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        # set up fake dft_run
        pp_path_list = []
        argvf_template_path = ''
        crystal_template_path = '' 
        pos = []
        gcut = -1
        testrun = eval_pp.DftRun(pp_path_list, argvf_template_path,
                                 crystal_template_path, pos, gcut)
        forces_in = testrun.read_forces(test_inputs_dir+'/diaryf.test_get_forces')
        correct_forces = np.array([0.007170, -0.015092, -0.069756, -0.007170, 0.015092, 0.069756])
        assert np.isclose(forces_in, correct_forces, rtol=1e-9, atol=0.0).all()
示例#11
0
def test_position_sweep():
    """
    Four instances of socorro run in parallel, results correct

    Sets up four DftRun instances with different atomic coordinates.
    The socorro runs should run simulataneously in different threads.

    note: this might fail if socorro versions change because different
    versions could give different reults even with the same input,
    especially since total E is relative. Could I mock socorro instead?
    Alternatively, I could run socorro twice, the second time with pre-
    setup input files, and compare results
    """
    inputs = test_inputs_dir + '/SiGe_single_run'
    with tools_for_tests.TemporaryDirectory() as tmp_dir:
        positions = [[[0.0, 0.0, 0.1], [0.24, 0.25, 0.26]],
                     [[0.0, 0.0, 0.0], [0.50, 0.50, 0.50]],
                     [[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]],
                     [[0.1, 0.0, 0.0], [0.49, 0.50, 0.51]]]

        # create objects to test
        pp_path_list = [inputs + '/PAW.Si', inputs + '/PAW.Ge']
        argvf_template_path = inputs + '/argvf.template'
        crystal_template_path = inputs + '/crystal.template'
        testruns = []
        for pos in positions:
            testruns.append(
                eval_pp.DftRun(pp_path_list, argvf_template_path,
                               crystal_template_path, pos, 10.))

        # run position_sweep
        eval_pp.position_sweep(testruns)

        # get results from socorro runs
        energies = []
        forces = []
        for run in testruns:
            # get results and append to results array
            os.chdir(run.run_dir)
            energies.append(run.read_energy())
            forces.append(run.read_forces())
            os.chdir(tmp_dir)

        print energies
        print forces

        # correct results
        correct_en = [
            -315.52682125, -314.613501131, -315.33638574, -314.797573776
        ]
        correct_forces = [
            np.array([
                0.337666, 0.218549, 0.321221, -0.337666, -0.218549, -0.321221
            ]),
            np.array([0., -0., -0., -0., 0., 0.]),
            np.array([-0., 0., -0., 0., 0., 0.]),
            np.array(
                [0.29504, 0.341381, 0.008144, -0.29504, -0.341381, -0.008144])
        ]

        assert np.isclose(energies, correct_en).all()
        assert np.isclose(forces, correct_forces, atol=1.e-5).all()
示例#12
0
def test_run_socorro_no_files():
    """
    test: run_socorro prints warning if files not created
    """
    testrun = eval_pp.DftRun([], [], [], [], 0) 
    assert testrun.run_socorro('') is False