Skip to navigation

Gamma Point Energy

View as MarkdownOpen in Claude

Calculate the energy of a system using the gamma-point-only version of VASP.

This example demonstrates the config/version field of a request.

Code

run-gamma-only.py
"""Example script to run a series of VASP calculations with different ENCUT values"""
from argparse import ArgumentParser
from pathlib import Path
import os
import asyncio
import time
import httpx
_file_dir = Path(__file__).parent / "files"
def _auth_headers() -> dict[str, str]:
"""Default headers with the bearer token from ALCHEMI_SERVICE_TOKEN"""
token = os.environ.get("ALCHEMI_SERVICE_TOKEN")
if not token:
raise RuntimeError("Please set the ALCHEMI_SERVICE_TOKEN environment variable.")
return {"Authorization": f"Bearer {token}"}
async def _wait_for_service_ready(
url: str, timeout: float = 240.0, poll_interval: float = 2.0
) -> bool:
"""Poll the /v1/health/ready endpoint until status is 'ready' or timeout is reached."""
start = time.monotonic()
async with httpx.AsyncClient(headers=_auth_headers()) as client:
while True:
try:
resp = await client.get(f"{url}/v1/health/ready", timeout=5.0)
if resp.status_code == 200:
data = resp.json()
if data.get("status") == "ready":
return True
except httpx.HTTPError:
pass # Only ignore timeout and connection exceptions
if time.monotonic() - start > timeout:
return False
await asyncio.sleep(poll_interval)
async def _run(url: str) -> None:
"""Run vasp_gam"""
# Wait for service readiness before submitting jobs
await _wait_for_service_ready(url)
# Read in the base VASP configuration and atomic structure
incar = (_file_dir / "INCAR").read_text()
kpoints = (_file_dir / "KPOINTS").read_text()
poscar = (_file_dir / "POSCAR").read_text()
# Submit tasks for each ENCUT value
async with httpx.AsyncClient(headers=_auth_headers()) as client:
# Format the request
request = {
"config": {
"version": "gam",
},
"files": {
"POSCAR": poscar,
"KPOINTS": kpoints,
"INCAR": incar,
},
}
# Send the request to the microservice
response = await client.post(f"{url}/infer", json=request, timeout=None)
result = response.json()
if result["success"]:
e_tot = result["results"]["e_tot"]
print(
f"e_tot: {e_tot} eV "
f"- stress_xx: {result['results']['stress'][0]} kBar "
f"- Converged: {result['results']['scf_converged']} "
f"- Memory: {result['memory'].get('peak_gpu_mem')} MB"
)
else:
print(f"FAILED - {result['failure_info']['traceback']}")
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"--url",
type=str,
default="http://127.0.0.1:8000",
help="URL of the ALCHEMI microservice",
)
args = parser.parse_args()
asyncio.run(_run(args.url))

Run the example by first launching the VASP microservice, then calling:

python run-gamma-only.py