Skip to content

Commit

Permalink
Implement string conversion for workfunction class
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-schlipf committed Sep 29, 2023
1 parent 5d8b61d commit 272d21d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/py4vasp/_data/workfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class Workfunction(base.Refinery, graph.Mixin):
In VASP you can compute the workfunction by setting the IDIPOL flag in the INCAR file.
This class provides then the functionality to analyze the resulting potential."""

@base.data_access
def __str__(self):
data = self.to_dict()
return f"""workfunction along {data["direction"]}:
vacuum potential: {data["vacuum_potential"][0]:.3f} {data["vacuum_potential"][1]:.3f}
valence band maximum: {data["valence_band_maximum"]:.3f}
conduction band minimum: {data["conduction_band_minimum"]:.3f}
Fermi energy: {data["fermi_energy"]:.3f}"""

@base.data_access
def to_dict(self):
bandgap = data.Bandgap.from_data(self._raw_data.reference_potential)
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def _workfunction(direction):
average_potential=_make_arbitrary_data(shape),
vacuum_potential=_make_arbitrary_data(shape=(2,)),
reference_potential=_bandgap("nonpolarized"),
fermi_energy=1.0,
fermi_energy=1.234,
)


Expand Down
20 changes: 20 additions & 0 deletions tests/data/test_workfunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
from unittest.mock import patch

import numpy as np
import pytest

from py4vasp import data
Expand Down Expand Up @@ -66,6 +67,25 @@ def check_to_image(workfunction, filename_argument, expected_filename):
fig.write_image.assert_called_once_with(workfunction._path / expected_filename)


def test_print(workfunction, format_):
actual, _ = format_(workfunction)
reference = """\
workfunction along {lattice_vector}:
vacuum potential: {vacuum1:.3f} {vacuum2:.3f}
valence band maximum: {vbm:.3f}
conduction band minimum: {cbm:.3f}
Fermi energy: {fermi_energy:.3f}"""
reference = reference.format(
lattice_vector=workfunction.ref.lattice_vector,
vacuum1=workfunction.ref.vacuum_potential[0],
vacuum2=workfunction.ref.vacuum_potential[1],
vbm=workfunction.ref.vbm,
cbm=workfunction.ref.cbm,
fermi_energy=workfunction.ref.fermi_energy,
)
assert actual == {"text/plain": reference}


def test_factory_methods(raw_data, check_factory_methods):
raw_workfunction = raw_data.workfunction("1")
check_factory_methods(data.Workfunction, raw_workfunction)

0 comments on commit 272d21d

Please sign in to comment.