|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +import shutil |
| 4 | +from dftio.io.abacus.abacus_parser import AbacusParser |
| 5 | +from dftio.data import _keys |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def abacus_parser(tmp_path): |
| 9 | + """Fixture for AbacusParser that creates a temporary directory structure.""" |
| 10 | + # The parser expects a directory structure like: |
| 11 | + # root/ |
| 12 | + # prefix_001/ |
| 13 | + # OUT.ABACUS/ |
| 14 | + # ... |
| 15 | + # prefix_002/ |
| 16 | + # OUT.ABACUS/ |
| 17 | + # ... |
| 18 | + |
| 19 | + # Create a temporary directory structure that the parser expects |
| 20 | + calc_dir = tmp_path / "calculation" |
| 21 | + calc_dir.mkdir() |
| 22 | + out_abacus_dir = calc_dir / "OUT.ABACUS" |
| 23 | + out_abacus_dir.mkdir() |
| 24 | + |
| 25 | + # Copy the test data to the temporary directory |
| 26 | + test_data_src = "test/data/abacus/OUT.ABACUS" |
| 27 | + for item in os.listdir(test_data_src): |
| 28 | + s = os.path.join(test_data_src, item) |
| 29 | + d = os.path.join(out_abacus_dir, item) |
| 30 | + if os.path.isdir(s): |
| 31 | + shutil.copytree(s, d, symlinks=False, ignore=None) |
| 32 | + else: |
| 33 | + shutil.copy2(s, d) |
| 34 | + |
| 35 | + # Create a dummy kpoints file |
| 36 | + with open(out_abacus_dir / "kpoints", "w") as f: |
| 37 | + f.write("nkstot now = 2\n") |
| 38 | + f.write("KPOINTS \n") |
| 39 | + f.write("0.0 0.0 0.0 1.0\n") |
| 40 | + f.write("0.5 0.5 0.5 1.0\n") |
| 41 | + |
| 42 | + return AbacusParser( |
| 43 | + root=str(tmp_path), |
| 44 | + prefix='calculation' |
| 45 | + ) |
| 46 | + |
| 47 | +def test_abacus_parser_init(abacus_parser, tmp_path): |
| 48 | + """Test AbacusParser initialization.""" |
| 49 | + assert abacus_parser.root == str(tmp_path) |
| 50 | + assert abacus_parser.prefix == 'calculation' |
| 51 | + assert len(abacus_parser.raw_datas) == 1 |
| 52 | + assert abacus_parser.raw_datas[0] == str(tmp_path / "calculation") |
| 53 | + |
| 54 | +def test_get_structure(abacus_parser): |
| 55 | + """Test parsing of structure files.""" |
| 56 | + structure = abacus_parser.get_structure(0) |
| 57 | + assert structure is not None |
| 58 | + assert _keys.ATOMIC_NUMBERS_KEY in structure |
| 59 | + assert _keys.POSITIONS_KEY in structure |
| 60 | + assert _keys.CELL_KEY in structure |
| 61 | + assert _keys.PBC_KEY in structure |
| 62 | + assert len(structure[_keys.ATOMIC_NUMBERS_KEY]) == 1 |
| 63 | + assert structure[_keys.ATOMIC_NUMBERS_KEY][0] == 13 |
| 64 | + assert structure[_keys.POSITIONS_KEY].shape == (1, 1, 3) |
| 65 | + assert structure[_keys.CELL_KEY].shape == (1, 3, 3) |
| 66 | + |
| 67 | +def test_get_eigenvalue(abacus_parser): |
| 68 | + """Test parsing of eigenvalues.""" |
| 69 | + eigenvalues = abacus_parser.get_eigenvalue(0) |
| 70 | + assert eigenvalues is not None |
| 71 | + assert _keys.ENERGY_EIGENVALUE_KEY in eigenvalues |
| 72 | + assert _keys.KPOINT_KEY in eigenvalues |
| 73 | + assert eigenvalues[_keys.ENERGY_EIGENVALUE_KEY].shape == (1, 47, 16) |
| 74 | + assert eigenvalues[_keys.KPOINT_KEY].shape == (2, 3) |
| 75 | + |
| 76 | +def test_get_basis(abacus_parser): |
| 77 | + """Test parsing of basis set.""" |
| 78 | + basis = abacus_parser.get_basis(0) |
| 79 | + assert basis is not None |
| 80 | + assert isinstance(basis, dict) |
| 81 | + assert "Al" in basis |
| 82 | + |
| 83 | +def test_get_blocks(abacus_parser): |
| 84 | + """Test parsing of blocks (Hamiltonian/Overlap).""" |
| 85 | + # This test requires the sparse matrix files to be present. |
| 86 | + # The fixture copies 'test/data/abacus/OUT.ABACUS' which contains 'data-HR-sparse_SPIN0.csr' etc. |
| 87 | + |
| 88 | + # We need to check if the parser can read them. |
| 89 | + # Note: get_blocks reads 'running_scf.log' (or similar) to get orbital info first. |
| 90 | + # The fixture copies 'running_scf.log' as well. |
| 91 | + |
| 92 | + # However, the fixture creates a dummy kpoints file but copies other files. |
| 93 | + # We need to make sure 'running_scf.log' is consistent with the sparse matrices if the parser checks dimensions. |
| 94 | + # The parser reads 'Matrix Dimension of ...' from the csr file. |
| 95 | + |
| 96 | + # Let's try to run it. |
| 97 | + ham, ovp, dm = abacus_parser.get_blocks(0, hamiltonian=True, overlap=True, density_matrix=False) |
| 98 | + |
| 99 | + assert ham is not None |
| 100 | + assert isinstance(ham, list) |
| 101 | + assert len(ham) > 0 |
| 102 | + assert isinstance(ham[0], dict) |
| 103 | + |
| 104 | + assert ovp is not None |
| 105 | + assert isinstance(ovp, list) |
| 106 | + assert len(ovp) > 0 |
| 107 | + assert isinstance(ovp[0], dict) |
0 commit comments