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 equation of state task and flow #13

Merged
merged 1 commit into from
Oct 11, 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
3 changes: 3 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ jobs:
pip install "pynanoflann@git+https://github.com/dwastberg/pynanoflann#egg=af434039ae14bedcbb838a7808924d6689274168"

- name: Run tests
env:
PREFECT_API_KEY: ${{ secrets.PREFECT_API_KEY }}
PREFECT_API_URL: ${{ secrets.PREFECT_API_URL }}
run: |
pytest -v tests
768 changes: 384 additions & 384 deletions mlip_arena/tasks/combustion/H256O128.extxyz

Large diffs are not rendered by default.

226 changes: 100 additions & 126 deletions mlip_arena/tasks/combustion/water.ipynb

Large diffs are not rendered by default.

645 changes: 645 additions & 0 deletions mlip_arena/tasks/eos/run.ipynb

Large diffs are not rendered by default.

50 changes: 39 additions & 11 deletions mlip_arena/tasks/eos/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from __future__ import annotations

import asyncio
from typing import TYPE_CHECKING

import numpy as np
Expand All @@ -15,9 +14,9 @@
from ase.optimize import * # type: ignore
from ase.optimize.optimize import Optimizer
from prefect import flow
from pymatgen.analysis.eos import BirchMurnaghan

from prefect.futures import wait
from prefect.runtime import flow_run, task_run
from pymatgen.analysis.eos import BirchMurnaghan

from mlip_arena.models.utils import MLIPEnum
from mlip_arena.tasks.optimize import run as OPT
Expand All @@ -26,13 +25,36 @@
from ase.filters import Filter


@flow
def generate_flow_run_name():
flow_name = flow_run.flow_name

parameters = flow_run.parameters

atoms = parameters["atoms"]
calculator_name = parameters["calculator_name"]

return f"{flow_name}: {atoms.get_chemical_formula()} - {calculator_name}"


def generate_task_run_name():
task_name = task_run.task_name

parameters = task_run.parameters

atoms = parameters["atoms"]
calculator_name = parameters["calculator_name"]

return f"{task_name}: {atoms.get_chemical_formula()} - {calculator_name}"


# @task(task_run_name=generate_task_run_name)
@flow(flow_run_name=generate_flow_run_name, validate_parameters=False)
def fit(
atoms: Atoms,
calculator_name: str | MLIPEnum,
calculator_kwargs: dict | None,
device: str | None = None,
optimizer: Optimizer | str = BFGSLineSearch, # type: ignore
optimizer: Optimizer | str = "BFGSLineSearch", # type: ignore
optimizer_kwargs: dict | None = None,
filter: Filter | str | None = None,
filter_kwargs: dict | None = None,
Expand All @@ -59,7 +81,7 @@ def fit(
Returns:
A dictionary containing the EOS data and the bulk modulus.
"""
result = OPT(
first_relax = OPT(
atoms=atoms,
calculator_name=calculator_name,
calculator_kwargs=calculator_kwargs,
Expand All @@ -71,7 +93,7 @@ def fit(
criterion=criterion,
)

relaxed = result["atoms"]
relaxed = first_relax["atoms"]

# p0 = relaxed.get_positions()
c0 = relaxed.get_cell()
Expand Down Expand Up @@ -99,14 +121,20 @@ def fit(

wait(futures)

volumes = [f.result()["atoms"].get_volume() for f in futures]
energies = [f.result()["atoms"].get_potential_energy() for f in futures]
volumes = [f.result()["atoms"].get_volume() for f in futures if isinstance(f.result(), dict)]
energies = [f.result()["atoms"].get_potential_energy() for f in futures if isinstance(f.result(), dict)]

volumes, energies = map(
list,
zip(
*sorted(zip(volumes, energies, strict=True), key=lambda i: i[0]),
strict=True,
),
)

bm = BirchMurnaghan(volumes=volumes, energies=energies)
bm.fit()

volumes, energies = map(list, zip(*sorted(zip(volumes, energies, strict=False), key=lambda i: i[0]), strict=False))

return {
"eos": {"volumes": volumes, "energies": energies},
"K": bm.b0_GPa,
Expand Down
6 changes: 5 additions & 1 deletion mlip_arena/tasks/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
} # type: ignore


@task(cache_key_fn=task_input_hash, cache_expiration=timedelta(days=1))
# @task(
# cache_key_fn=task_input_hash,
# cache_expiration=timedelta(days=1),
# timeout_seconds=120)
@task(timeout_seconds=120, result_storage=None)
def run(
atoms: Atoms,
calculator_name: str | MLIPEnum,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@pytest.mark.parametrize("model", [MLIPEnum["MACE-MP(M)"]])
@pytest.mark.skipif(
sys.version_info != (3, 11), reason="requires Python 3.11 to use prefect"
tuple(sys.version_info)[:2] != (3, 11), reason="requires Python 3.11 to use prefect"
)
def test_eos(model: MLIPEnum):
"""
Expand Down
Loading