|
| 1 | +"""Test phonon document models.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from monty.serialization import loadfn |
| 5 | +from pymatgen.core import Structure |
| 6 | +import pytest |
| 7 | + |
| 8 | +from emmet.core.phonon import PhononDOS, PhononBS, PhononBSDOSDoc |
| 9 | +from tests.conftest import assert_schemas_equal |
| 10 | + |
| 11 | +try: |
| 12 | + import pyarrow.parquet as pq |
| 13 | +except ImportError: |
| 14 | + pq = None |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="module") |
| 18 | +def legacy_ph_task(test_dir): |
| 19 | + return loadfn(test_dir / "mp-23302_phonon.json.gz", cls=None) |
| 20 | + |
| 21 | + |
| 22 | +def test_legacy_migration(legacy_ph_task): |
| 23 | + # ensure that legacy phonon data can be migrated to current schema |
| 24 | + |
| 25 | + assert all(legacy_ph_task.get(k) for k in ("ph_bs", "ph_dos")) |
| 26 | + ph_doc = PhononBSDOSDoc.from_structure( |
| 27 | + Structure.from_dict(legacy_ph_task["ph_bs"]["structure"]), **legacy_ph_task |
| 28 | + ) |
| 29 | + assert_schemas_equal(ph_doc, PhononBSDOSDoc.model_config) |
| 30 | + |
| 31 | + # check remap of phonon DOS |
| 32 | + for k in ("densities", "frequencies"): |
| 33 | + assert np.all( |
| 34 | + np.abs( |
| 35 | + np.array(getattr(ph_doc.phonon_dos, k, [])) |
| 36 | + - np.array(legacy_ph_task["ph_dos"].get(k, [])) |
| 37 | + ) |
| 38 | + < 1e-6 |
| 39 | + ) |
| 40 | + |
| 41 | + # check remap of phonon bandstructure |
| 42 | + for k in ("qpoints", "frequencies"): |
| 43 | + assert np.all( |
| 44 | + np.abs( |
| 45 | + np.array(getattr(ph_doc.phonon_bandstructure, k, [])) |
| 46 | + - np.array(legacy_ph_task["ph_bs"].get(k, [])) |
| 47 | + ) |
| 48 | + < 1e-6 |
| 49 | + ) |
| 50 | + |
| 51 | + temps = [5, 100, 300, 500, 800] |
| 52 | + ref_data = { |
| 53 | + "entropy": [ |
| 54 | + 0.10690825310989084, |
| 55 | + 64.92923008995054, |
| 56 | + 118.49759120562243, |
| 57 | + 143.87894281334866, |
| 58 | + 167.29013558233882, |
| 59 | + ], |
| 60 | + "heat_capacity": [ |
| 61 | + 0.3096320518171239, |
| 62 | + 47.157121712113714, |
| 63 | + 49.56965404227773, |
| 64 | + 49.77072796011147, |
| 65 | + 49.8399369784691, |
| 66 | + ], |
| 67 | + "internal_energy": [ |
| 68 | + 1988.2321308731728, |
| 69 | + 5269.03083181402, |
| 70 | + 15060.035206972329, |
| 71 | + 24999.080865250606, |
| 72 | + 39943.07283219206, |
| 73 | + ], |
| 74 | + "free_energy": [ |
| 75 | + 1987.697589607624, |
| 76 | + -1223.8921771810346, |
| 77 | + -20489.242154714408, |
| 78 | + -46940.39054142372, |
| 79 | + -93889.03563367906, |
| 80 | + ], |
| 81 | + } |
| 82 | + tprops = ph_doc.compute_thermo_quantites(temps) |
| 83 | + assert all( |
| 84 | + t == pytest.approx(tprops["temperature"][i]) for i, t in enumerate(temps) |
| 85 | + ) |
| 86 | + for k, ref_vals in ref_data.items(): |
| 87 | + assert all( |
| 88 | + tprops[k][i] == pytest.approx(ref_val) for i, ref_val in enumerate(ref_vals) |
| 89 | + ) |
| 90 | + |
| 91 | + # check population of structure metadata fields |
| 92 | + assert ph_doc.composition == ph_doc.structure.composition |
| 93 | + assert ph_doc.volume == ph_doc.structure.volume |
| 94 | + assert ph_doc.nelements == len(ph_doc.structure.composition.elements) |
| 95 | + assert sorted(ph_doc.elements) == sorted(ph_doc.structure.composition.elements) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.skipif(pq is None, reason="pyarrow must be installed to run this test.") |
| 99 | +def test_arrow(tmp_dir, legacy_ph_task): |
| 100 | + # test to parquet and rehydration |
| 101 | + ph_doc = PhononBSDOSDoc(**legacy_ph_task) |
| 102 | + arrow_table = ph_doc.objects_to_arrow() |
| 103 | + pq.write_table(arrow_table, "test.parquet") |
| 104 | + |
| 105 | + rehyd = pq.read_table("test.parquet") |
| 106 | + |
| 107 | + dos_from_table = PhononDOS.from_arrow(arrow_table, col_prefix="dos_") |
| 108 | + dos_from_parquet = PhononDOS.from_arrow(rehyd, col_prefix="dos_") |
| 109 | + |
| 110 | + assert ph_doc.phonon_dos == dos_from_table |
| 111 | + assert ph_doc.phonon_dos == dos_from_parquet |
| 112 | + |
| 113 | + bs_from_table = PhononBS.from_arrow(arrow_table, col_prefix="bs_") |
| 114 | + bs_from_parquet = PhononBS.from_arrow(rehyd, col_prefix="bs_") |
| 115 | + |
| 116 | + assert ph_doc.phonon_bandstructure == bs_from_table |
| 117 | + assert ph_doc.phonon_bandstructure == bs_from_parquet |
0 commit comments