Skip to content

Commit

Permalink
Merge pull request #11 from Quantum-Accelerators/Andrew-S-Rosen-patch-1
Browse files Browse the repository at this point in the history
Use the actual RASPA executable in the GitHub Actions test suite
  • Loading branch information
Andrew-S-Rosen authored Jan 14, 2024
2 parents 3400c73 + 0cf10dd commit 57ca812
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ jobs:
pip install -r tests/requirements.txt
pip install .[dev]
- name: Install conda packages
run: conda install -c conda-forge raspa2

- name: Set up environment
run: echo "RASPA_DIR=/usr/share/miniconda" >> "$GITHUB_ENV"

- name: Run tests with pytest
run: pytest --cov=raspa_ase --cov-report=xml
run: pytest --noconftest --cov=raspa_ase --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
17 changes: 12 additions & 5 deletions src/raspa_ase/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, argv: list[str] | None = None) -> None:
def run(
self,
directory: Path | str,
output_filename: Path | str,
output_filename: str,
) -> None:
"""
Run the RASPA calculation.
Expand All @@ -80,10 +80,16 @@ class RaspaTemplate(CalculatorTemplate):
RASPA template, used to define how to read and write RASPA files.
"""

def __init__(self) -> None:
def __init__(self, frameworks: list[Atoms] | None = None) -> None:
"""
Initialize the RASPA template.
Parameters
----------
frameworks
The frameworks to use, to be used in place of the `Atoms` object
the calculator is applied to.
Returns
-------
None
Expand All @@ -96,6 +102,7 @@ def __init__(self) -> None:

self.input_file = SIMULATION_INPUT
self.output_file = f"{label}.out"
self.frameworks = frameworks

def execute(self, directory: Path | str, profile: RaspaProfile) -> None:
"""
Expand All @@ -114,8 +121,8 @@ def execute(self, directory: Path | str, profile: RaspaProfile) -> None:
"""
profile.run(directory, self.output_file)

@staticmethod
def write_input(
self,
directory: Path | str,
atoms: Atoms,
parameters: dict[str, Any],
Expand All @@ -142,7 +149,7 @@ def write_input(
-------
None
"""
frameworks = [atoms]
frameworks = self.frameworks if self.frameworks else [atoms]
parameters = merge_parameters(parameters, get_framework_params([atoms]))

write_simulation_input(parameters, directory / SIMULATION_INPUT)
Expand Down Expand Up @@ -369,7 +376,7 @@ def __init__(
parameters = merge_parameters(parameters, {f"Box {i}": box})

super().__init__(
template=RaspaTemplate(),
template=RaspaTemplate(frameworks=multiple_frameworks),
profile=profile,
directory=directory,
parameters=parameters,
Expand Down
40 changes: 34 additions & 6 deletions tests/test_calculator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from pathlib import Path

import pytest
from ase import Atoms
from ase.build import bulk
from ase.io import read

from raspa_ase.calculator import Raspa, RaspaProfile, RaspaTemplate

Expand All @@ -15,7 +17,10 @@ def test_profile_bad(monkeypatch):

def test_profile():
profile = RaspaProfile()
assert profile.argv == ["./bin/simulate", "simulation.input"]
assert profile.argv == [
f"{os.getenv('RASPA_DIR')}/bin/simulate",
"simulation.input",
]


def test_profile2():
Expand All @@ -25,7 +30,6 @@ def test_profile2():

def test_run(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("RASPA_DIR", "bad_dir")
RaspaProfile().run(tmp_path, tmp_path / "simulation.input")


Expand All @@ -37,8 +41,10 @@ def test_template():

def test_template_execute(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
with Path(tmp_path / "simulation.input").open(mode="w") as fd:
fd.write("")
template = RaspaTemplate()
template.execute("bad_dir", RaspaProfile())
template.execute(tmp_path, RaspaProfile())


def test_template_write_input(monkeypatch, tmp_path):
Expand Down Expand Up @@ -134,10 +140,32 @@ def test_multi_frameworks(tmp_path):
atoms1.info = {"HeliumVoidFraction": 0.75}
atoms2 = bulk("Fe")
atoms = Atoms()
atoms.calc = Raspa(directory=tmp_path, multiple_frameworks=[atoms1, atoms2])
atoms.calc = Raspa(
directory=tmp_path,
multiple_frameworks=[atoms1, atoms2],
boxes=[{"BoxLengths": [1, 2, 3]}, {"BoxLengths": [4, 5, 6]}],
parameters={"CutOff": 12.8},
components=[
{"MoleculeName": "N2", "MoleculeDefinition": "ExampleDefinition"},
{
"MoleculeName": "CO2",
"MoleculeDefinition": "ExampleDefinition",
"TranslationProbability": 1.0,
},
],
)
atoms.get_potential_energy()
assert Path(tmp_path, "simulation.input").exists()
input_str = Path(tmp_path / "simulation.input").read_text()
assert (
Path(tmp_path / "simulation.input").read_text()
== "Framework 0\n FrameworkName framework0\n UnitCells 12 12 12\n HeliumVoidFraction 0.75\nFramework 1\n FrameworkName framework1\n UnitCells 12 12 12\n"
"Framework 0\n FrameworkName framework0\n UnitCells 12 12 12\n HeliumVoidFraction 0.75\n"
in input_str
)
assert (
"Framework 1\n FrameworkName framework1\n UnitCells 12 12 12\n"
in input_str
)
assert Path(tmp_path / "framework0.cif").exists()
assert Path(tmp_path / "framework1.cif").exists()
assert read(tmp_path / "framework0.cif")[0].symbol == "Cu"
assert read(tmp_path / "framework1.cif")[0].symbol == "Fe"

0 comments on commit 57ca812

Please sign in to comment.