File: test_conversion.py

package info (click to toggle)
phonopy 2.48.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,556 kB
  • sloc: python: 44,403; xml: 12,080; ansic: 3,227; cpp: 525; sh: 213; makefile: 20
file content (127 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Test Conversion between calculator formats."""

import pathlib
import sys
import tempfile

import pytest

from phonopy.interface.calculator import calculator_info, convert_crystal_structure
from phonopy.scripts.phonopy_calc_convert import run

cwd = pathlib.Path(__file__).parent

require_extra_info = ["wien2k", "cp2k", "fleur"]


def test_conversion_turbomole():
    """Conversion test for Turbomole."""
    poscar_file = cwd / ".." / "POSCAR_NaCl"
    with tempfile.TemporaryDirectory() as td:
        convert_crystal_structure(poscar_file, "vasp", td, "turbomole")


@pytest.mark.parametrize("calc", require_extra_info)
def test_conversion_require_extra_info(calc):
    """Calcs that can use extra info are below."""
    poscar_file = cwd / ".." / "POSCAR_NaCl"
    with tempfile.TemporaryDirectory() as td:
        name = str(pathlib.Path(td) / f"crystal_structure_{calc}")
        with pytest.raises(RuntimeError):
            # These calcs need additional info to write their input files
            convert_crystal_structure(poscar_file, "vasp", name, calc)


def test_conversion_expected_warnings():
    """Calcs that can use extra info are below."""
    poscar_file = cwd / ".." / "POSCAR_NaCl"
    expected_warnings = ["qe", "abacus", "crystal", "fleur", "wien2k"]
    calcs = calculator_info.keys()
    for calc in calcs:
        if calc in require_extra_info:
            continue
        with tempfile.TemporaryDirectory() as td:
            name = str(pathlib.Path(td) / f"crystal_structure_{calc}")
            if calc in expected_warnings:
                with pytest.warns(UserWarning):
                    convert_crystal_structure(poscar_file, "vasp", name, calc)
            else:
                convert_crystal_structure(poscar_file, "vasp", name, calc)


def simulate_calc_convert_script(
    input_file, output_file, calc_in, calc_out, addinfo=None
):
    """Simulate running the phonopy_calc_convert script with given arguments."""
    # Convert PosixPath objects to strings
    input_file = str(input_file)
    output_file = str(output_file)

    # Build the argument list
    args = [
        "phonopy_calc_convert.py",  # Script name
        "-i",
        input_file,  # Input file
        "-o",
        output_file,  # Output file
        "--calcin",
        calc_in,  # Input calculator
        "--calcout",
        calc_out,  # Output calculator
    ]

    # Add additional info if provided
    if addinfo:
        args.extend(["--additional-info"] + [str(info) for info in addinfo])

    # Backup the original sys.argv
    original_argv = sys.argv
    try:
        # Replace sys.argv with the simulated arguments
        sys.argv = args
        # Call the script's run function
        run()
    finally:
        # Restore the original sys.argv
        sys.argv = original_argv


def test_calc_convert():
    """
    Test parsing of additional info kwarg.

    Converts to/from vasp for calculators that accept
    additional info into phonopy-calc-convert script.
    """
    # Simulate command-line arguments
    extra_inps = {
        "qe": [
            "Na",
            "Na.pbe-spn-kjpaw_psl.0.2.UPF",
            "Cl",
            "Cl.pbe-n-kjpaw_psl.0.1.UPF",
        ],
        "wien2k": ["781 781 781", "1e-05 5e-05 5e-05", "2.5 2.28 2.28"],
        "elk": ["alternate_si_file.in"],
        "crystal": [14, 14],
        "fleur": [13.1, "Title \n other stuff"],
        "abacus": ["Al", "Al.PD04.PBE.UPF"],
    }
    extra_files = {
        "qe": cwd / "NaCl-pwscf.in",
        "wien2k": cwd / "BaGa2.struct",
        "elk": cwd / "elk.in",
        "crystal": cwd / "Si-CRYSTAL.o",
        "fleur": cwd / "fleur_inpgen",
        "abacus": cwd / "STRU.in",
    }

    for calc in extra_inps.keys():
        with tempfile.TemporaryDirectory() as temp_dir:
            vasp_tmp = pathlib.Path(temp_dir) / "vasp_tmp_file"
            simulate_calc_convert_script(extra_files[calc], vasp_tmp, calc, "vasp")
            with tempfile.TemporaryDirectory() as temp2:
                name = pathlib.Path(temp2) / (calc + "_tmp_file")
                simulate_calc_convert_script(
                    vasp_tmp, name, "vasp", calc, addinfo=extra_inps[calc]
                )