Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a (minimal) test suite #5

Merged
merged 8 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.12"]
python-version: ["3.9", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -21,4 +21,4 @@ jobs:
pip install .
- name: Analysing the code with pylint
run: |
pylint $(git ls-files '*.py')
pylint $(git ls-files 'mctools/*.py')
27 changes: 27 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Pytest

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest flake8 flake8-pytest-style
pip install .
- name: Lint the test files with flake8-pytest-style
run: |
flake8 tests/
- name: Test with pytest
run: |
pytest
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ and developed further while at [Scanlon Materials Theory Group](https://github.c

The recommended way of installing is to clone the repository and make a local installation using pip:

``` shell
```bash
git clone https://github.com/ajjackson/mctools.git
cd mctools
pip install --user -e .
```

ase-convert (Convert structure files)
-------------------------------------
Use ASE to read a crystal structure file and write out to target format. Call with `-h` flag for usage information. ASE can also get this information from some output file formats, which is useful.

```
# EXAMPLE: read relaxed structure from aims output and write VASP input
bash> ase_convert.py aims.out POSCAR

# EXAMPLE: Convert between files with non-standard names
bash> ase_convert.py -f vasp MY_SUPER_POSCAR -t cif MY_SUPER_CIF
To run unit tests, install pytest and run with
```bash
python -m pytest
```

get-spacegroup (Spacegroup tolerances)
Expand Down
28 changes: 7 additions & 21 deletions mctools/generic/get_energy.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
#! /usr/bin/env python

###############################################################################
# Copyright 2017 Adam Jackson
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###############################################################################

from argparse import ArgumentParser
from typing import List, Optional

import ase.io


Expand All @@ -27,15 +10,18 @@ def get_energy(filename):
return atoms.get_total_energy()


def main():
def main(params: Optional[List[str]] = None):
"""Get calculated energy from output file using ASE"""

parser = ArgumentParser(description="Read energy from output")
parser.add_argument("filename", type=str, nargs='?',
default="vasprun.xml",
help="Path to ab initio output file")

args = parser.parse_args()
if params:
args = parser.parse_args(params)
else:
args = parser.parse_args()

energy = get_energy(args.filename)
print(energy)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_get_energy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ase.build
from ase.calculators.singlepoint import SinglePointCalculator
import ase.io
import pytest

from mctools.generic.get_energy import get_energy, main


ENERGY = 3.141
FILENAME = "methane.extxyz"


@pytest.fixture
def methane_with_energy() -> ase.Atoms:
atoms = ase.build.molecule("CH4")
atoms.calc = SinglePointCalculator(atoms, energy=ENERGY)
return atoms


def test_get_energy(methane_with_energy, tmp_path) -> None:
ase.io.write(tmp_path / FILENAME, methane_with_energy)

assert get_energy(tmp_path / FILENAME) == pytest.approx(ENERGY)


def test_main(methane_with_energy, tmp_path, capsys) -> None:
ase.io.write(tmp_path / FILENAME, methane_with_energy)

main([str(tmp_path / FILENAME)])
captured = capsys.readouterr()
assert float(captured.out) == pytest.approx(ENERGY)
Loading