def test_scf_base_calculation_settings():
    pwig = PwxInputGenerator(crystal_structure=al_fcc_struct)
    pwig.calculation_presets = "scf"
    cs = pwig.calculation_settings
    assert cs["calculation"] == "scf"
    assert cs["namelists"] == ["control", "system", "electrons"]
    assert cs["kpoints"]["scheme"] == "automatic"
    assert cs["kpoints"]["shift"] == [0, 0, 0]
    assert cs["pseudo_dir"] == os.path.join("~", "pseudos", "qe", "default")
def test_write_input_files():
    import tempfile

    _tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=True)
    filename = _tmp_file.name
    write_location = os.path.dirname(filename)
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.calculation_presets = "scf"
    pwig.specify_potentials = True
    pwig.custom_sett_dict["pseudo_dir"] = pseudo_dir
    pwig.write_location = write_location
    pwig.pwx_input_file = filename
    pwig.write_input_files()
    with open(filename, "r") as fr:
        assert fr.read() == feo_scf_in.rstrip("\n")
def test_kpoints_card():
    # unknown scheme: error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.custom_sett_dict = {"kpoints": {"scheme": "monkhorst-pack"}}
    with pytest.raises(NotImplementedError):
        print(pwig.kpoints_card)
    # default scheme from presets
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.calculation_presets = "scf"
    assert pwig.kpoints_card == "K_POINTS {automatic}\n9 9 9 0 0 0"
    pwig = PwxInputGenerator(
        crystal_structure=al_fcc_struct,
        custom_sett_dict={"kpoints": {
            "scheme": "gamma"
        }},
    )
    assert pwig.kpoints_card == "K_POINTS {gamma}"
def test_control_namelist_to_str():
    # control namelist without pseudo, settings: error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    pwig.specify_potentials = True
    with pytest.raises(PwxInputGeneratorError):
        pwig._namelist_to_str("control")
    # specify_potentials = False: no error
    pwig.specify_potentials = False
    nl = pwig._namelist_to_str("control")
    assert nl == "&CONTROL\n/"
    # with no pseudo, with settings
    pwig.custom_sett_dict.update({"calculation": "scf"})
    nl = pwig._namelist_to_str("control")
    assert nl == '&CONTROL\n    calculation = "scf"\n/'
    # specify_potentials = True: throw error
    pwig.specify_potentials = True
    with pytest.raises(PwxInputGeneratorError):
        pwig._namelist_to_str("control")
    # normal functionality
    pwig.custom_sett_dict.update({"pseudo_dir": pseudo_dir})
    pwig.calculation_presets = "scf"
    control = "\n".join(feo_scf_in.splitlines()[:7])
    assert pwig._namelist_to_str("control") == control
def test_write_pwx_input():
    # no input settings: error
    pwig = PwxInputGenerator(crystal_structure=feo_struct)
    with pytest.raises(PwxInputGeneratorError, match="input settings"):
        pwig.write_pwx_input()
    # no `write_location` input: error
    pwig.calculation_presets = "scf"
    with pytest.raises(PwxInputGeneratorError, match="Location to write"):
        pwig.write_pwx_input()
    # no input filename: error
    with pytest.raises(PwxInputGeneratorError, match="file to write"):
        pwig.write_pwx_input(write_location="/path/to/write_location")

    # all ok
    import tempfile

    _tmp_file = tempfile.NamedTemporaryFile(mode="w", delete=True)
    filename = _tmp_file.name
    write_location = os.path.dirname(filename)
    pwig.specify_potentials = True
    pwig.custom_sett_dict = {"pseudo_dir": pseudo_dir}
    pwig.write_pwx_input(write_location=write_location, filename=filename)
    with open(filename, "r") as fr:
        assert fr.read() == feo_scf_in.rstrip("\n")
def test_relax_base_calculation_settings():
    pwig = PwxInputGenerator(crystal_structure=al_fcc_struct)
    pwig.calculation_presets = "relax"
    cs = pwig.calculation_settings
    assert cs["calculation"] == "relax"
    assert cs["namelists"] == ["control", "system", "electrons", "ions"]