Skip to content

Commit

Permalink
TiO2 tests and tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
wolearyc committed Aug 3, 2024
1 parent efc3f50 commit 4d8772c
Show file tree
Hide file tree
Showing 36 changed files with 520 additions and 78 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"newaxis",
"NIONS",
"outcar",
"outcars",
"pathify",
"phonopy",
"polarizabilities",
Expand Down
415 changes: 415 additions & 0 deletions docs/Examples - ramannoodle.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/coverage-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion ramannoodle/polarizability/interpolation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def add_dof( # pylint: disable=too-many-locals
f"due to symmetry, amplitude {duplicate} should not be specified"
)

if len(interpolation_x) <= interpolation_order:
if len(interpolation_x) - 1 <= interpolation_order:
raise InvalidDOFException(
f"insufficient amplitudes ({len(interpolation_x)}) available for"
f"{interpolation_order}-order interpolation"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added test/data/TiO2/known_spectrum.npz
Binary file not shown.
File renamed without changes.
File renamed without changes.
74 changes: 2 additions & 72 deletions test/tests/test_polarizability.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from ramannoodle.polarizability.interpolation import InterpolationPolarizabilityModel
from ramannoodle.io.vasp import read_structural_symmetry_from_outcar
from ramannoodle.exceptions import InvalidDOFException
from ramannoodle.symmetry import StructuralSymmetry
from ramannoodle import io

# pylint: disable=protected-access

Expand All @@ -33,7 +31,7 @@ def test_find_duplicates(vectors: list[NDArray[np.float64]], known: bool) -> Non
"outcar_path_fixture,displaced_atom_index, amplitudes, known_dof_added",
[
("test/data/STO_RATTLED_OUTCAR", 0, np.array([-0.05, 0.05, 0.01, -0.01]), 1),
("test/data/TiO2/PHONON_OUTCAR", 0, np.array([0.01]), 72),
("test/data/TiO2/phonons_OUTCAR", 0, np.array([0.01]), 72),
],
indirect=["outcar_path_fixture"],
)
Expand All @@ -58,7 +56,7 @@ def test_add_dof(
"outcar_path_fixture,displaced_atom_index, amplitudes",
[
("test/data/STO_RATTLED_OUTCAR", 0, np.array([0.01, 0.01])),
("test/data/TiO2/PHONON_OUTCAR", 0, np.array([-0.01, 0.01])),
("test/data/TiO2/phonons_OUTCAR", 0, np.array([-0.01, 0.01])),
],
indirect=["outcar_path_fixture"],
)
Expand All @@ -77,71 +75,3 @@ def test_overspecified_dof(
model.add_dof(displacement, amplitudes, polarizabilities, 1)

assert "should not be specified" in str(error.value)


@pytest.mark.parametrize(
"outcar_symmetry_fixture,ref_eps,to_add,additional_references",
[
(
"test/data/TiO2/PHONON_OUTCAR",
"test/data/TiO2/ref_OUTCAR",
[
["test/data/TiO2/Ti5_0.1z_OUTCAR", "test/data/TiO2/Ti5_0.2z_OUTCAR"],
["test/data/TiO2/Ti5_0.1x_OUTCAR", "test/data/TiO2/Ti5_0.2x_OUTCAR"],
[
"test/data/TiO2/O43_0.1z_OUTCAR",
"test/data/TiO2/O43_0.2z_OUTCAR",
"test/data/TiO2/O43_m0.1z_OUTCAR",
"test/data/TiO2/O43_m0.2z_OUTCAR",
],
[
"test/data/TiO2/O43_0.1x_OUTCAR",
"test/data/TiO2/O43_0.2x_OUTCAR",
],
[
"test/data/TiO2/O43_0.1y_OUTCAR",
"test/data/TiO2/O43_0.2y_OUTCAR",
],
],
[
"test/data/TiO2/Ti5_m0.2z_OUTCAR",
"test/data/TiO2/Ti5_m0.1z_OUTCAR",
"test/data/TiO2/O43_m0.1x_OUTCAR",
"test/data/TiO2/O43_m0.2x_OUTCAR",
"test/data/TiO2/O43_m0.1y_OUTCAR",
"test/data/TiO2/O43_m0.2y_OUTCAR",
],
),
],
indirect=["outcar_symmetry_fixture"],
)
def test_get_polarizability(
outcar_symmetry_fixture: StructuralSymmetry,
ref_eps: str,
to_add: list[str],
additional_references: list[str],
) -> None:
"""Test."""
symmetry = outcar_symmetry_fixture
_, polarizability = io.read_positions_and_polarizability(
ref_eps, file_format="outcar"
)
model = InterpolationPolarizabilityModel(symmetry, polarizability)
for outcar_path_list in to_add:
model.add_dof_from_files(
outcar_path_list, file_format="outcar", interpolation_order=2
)

# Tests
total_outcar_paths = additional_references
for outcar_path in to_add:
total_outcar_paths += outcar_path
for outcar_path in total_outcar_paths:
positions, known_polarizability = io.read_positions_and_polarizability(
outcar_path, file_format="outcar"
)
cartesian_displacement = symmetry.get_cartesian_displacement(
positions - symmetry.get_fractional_positions()
)
model_polarizability = model.get_polarizability(cartesian_displacement)
assert np.isclose(model_polarizability, known_polarizability, atol=1e-4).all()
96 changes: 96 additions & 0 deletions test/tests/test_spectrum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""Testing for spectra."""

from pathlib import Path

import numpy as np

import pytest

from ramannoodle.polarizability.interpolation import InterpolationPolarizabilityModel
from ramannoodle.symmetry import StructuralSymmetry
from ramannoodle import io

# pylint: disable=protected-access


def _get_all_eps_outcars(directory: str) -> list[str]:
"""Return name of all eps OUTCARs in a directory."""
path = Path(directory)
return [str(item) for item in path.glob("*eps_OUTCAR")]


def _validate_polarizabilities(
model: InterpolationPolarizabilityModel, data_directory: str
) -> None:
for outcar_path in _get_all_eps_outcars(data_directory):
positions, known_polarizability = io.read_positions_and_polarizability(
f"{outcar_path}", file_format="outcar"
)
cartesian_displacement = model._structural_symmetry.get_cartesian_displacement(
positions - model._structural_symmetry.get_fractional_positions()
)
model_polarizability = model.get_polarizability(cartesian_displacement)
assert np.isclose(model_polarizability, known_polarizability, atol=1e-4).all()


@pytest.mark.parametrize(
"outcar_symmetry_fixture,data_directory,dof_eps_outcars",
[
(
"test/data/TiO2/phonons_OUTCAR",
"test/data/TiO2/",
[
["Ti5_0.1z_eps_OUTCAR", "Ti5_0.2z_eps_OUTCAR"],
["Ti5_0.1x_eps_OUTCAR", "Ti5_0.2x_eps_OUTCAR"],
[
"O43_0.1z_eps_OUTCAR",
"O43_0.2z_eps_OUTCAR",
"O43_m0.1z_eps_OUTCAR",
"O43_m0.2z_eps_OUTCAR",
],
["O43_0.1x_eps_OUTCAR", "O43_0.2x_eps_OUTCAR"],
["O43_0.1y_eps_OUTCAR", "O43_0.2y_eps_OUTCAR"],
],
),
],
indirect=["outcar_symmetry_fixture"],
)
def test_spectrum(
outcar_symmetry_fixture: StructuralSymmetry,
data_directory: str,
dof_eps_outcars: list[str],
) -> None:
"""Test a spectrum calculation."""
# Setup model
symmetry = outcar_symmetry_fixture
_, polarizability = io.read_positions_and_polarizability(
f"{data_directory}/ref_eps_OUTCAR", file_format="outcar"
)
model = InterpolationPolarizabilityModel(symmetry, polarizability)
for outcar_names in dof_eps_outcars:
model.add_dof_from_files(
[f"{data_directory}/{name}" for name in outcar_names],
file_format="outcar",
interpolation_order=2,
)

_validate_polarizabilities(model, data_directory)

# Spectrum test
with np.load(f"{data_directory}/known_spectrum.npz") as known_spectrum:
phonons = io.read_phonons(
f"{data_directory}/phonons_OUTCAR", file_format="outcar"
)
spectrum = phonons.get_raman_spectrum(model)
wavenumbers, intensities = spectrum.measure(
laser_correction=True,
laser_wavelength=532,
bose_einstein_correction=True,
temperature=300,
)

known_wavenumbers = known_spectrum["wavenumbers"]
known_intensities = known_spectrum["intensities"]

assert np.isclose(wavenumbers, known_wavenumbers).all()
assert np.isclose(intensities, known_intensities, atol=1e-4).all()
2 changes: 1 addition & 1 deletion test/tests/test_symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_check_orthogonal(
"outcar_symmetry_fixture, known_nonequivalent_atoms,"
"known_orthogonal_displacements, known_displacements_shape",
[
("test/data/TiO2/PHONON_OUTCAR", 2, 36, [2] * 36),
("test/data/TiO2/phonons_OUTCAR", 2, 36, [2] * 36),
("test/data/STO_RATTLED_OUTCAR", 135, 1, [1]),
("test/data/LLZO_OUTCAR", 9, 32, [1] * 32),
],
Expand Down
2 changes: 1 addition & 1 deletion test/tests/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"known_first_displacement, known_last_displacement",
[
(
"test/data/TiO2/PHONON_OUTCAR",
"test/data/TiO2/phonons_OUTCAR",
PHONONS_OUTCAR_NUM_ATOMS,
np.array([811.691808, 811.691808, 811.691808, 811.691808]),
np.array([-0.068172, 0.046409, 0.000000]) / np.sqrt(ATOMIC_WEIGHTS["Ti"]),
Expand Down
4 changes: 2 additions & 2 deletions test/tests/test_vasp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_fail_get_atomic_symbol_from_potcar_line(potcar_line: str) -> None:

@pytest.mark.parametrize(
"outcar_file_fixture, known",
[("test/data/TiO2/PHONON_OUTCAR", ["Ti"] * 36 + ["O"] * 72)],
[("test/data/TiO2/phonons_OUTCAR", ["Ti"] * 36 + ["O"] * 72)],
indirect=["outcar_file_fixture"],
)
def test_read_atomic_symbols_from_outcar(
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_read_polarizability_from_outcar(
"outcar_file_fixture, known_lattice",
[
(
"test/data/TiO2/PHONON_OUTCAR",
"test/data/TiO2/phonons_OUTCAR",
np.array(
[
[11.3768434223, 0.0000000000, 0.0000000000],
Expand Down

0 comments on commit 4d8772c

Please sign in to comment.