-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
78 lines (57 loc) · 2.61 KB
/
Copy pathinterface.py
File metadata and controls
78 lines (57 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""High-level interface for model potentials and external calculations.
Provides a small, test-friendly API surface that wraps:
- Rosenbrock function and gradient (via `potentials.py`)
- Müller–Brown potential and gradient (via `potentials.py`)
- Turbomole preparation / (optional) run helpers
- Hummr template insertion / (optional) run helpers
Design notes:
- This module avoids running external binaries by default. `run_*` helpers take
`run=False` by default; when `run=True` they attempt to call subprocess, and
will raise a friendly exception if the executable is not available.
- The Molecule class from `molecule.py` is used to prepare coordinates and
to insert the molecule into Hummr templates.
"""
from __future__ import annotations
from typing import Sequence, Optional
import subprocess
import shlex
import os
from potentials import rosenbrock, rosenbrock_grad, muller_brown, muller_brown_grad
from molecule import Molecule
class ExternalProgramNotFound(RuntimeError):
"""Raised when a requested external program is not found on PATH."""
# -------------------- potentials wrappers --------------------
def rosenbrock_value(x: Sequence[float], a: float = 1.0, b: float = 100.0) -> float:
"""Compute Rosenbrock function value.
Thin wrapper around `potentials.rosenbrock` kept for consistent interface.
"""
return rosenbrock(x, a=a, b=b)
def rosenbrock_gradient(x: Sequence[float], a: float = 1.0, b: float = 100.0):
"""Return gradient of Rosenbrock as numpy array.
Requires numpy; the underlying function will raise a RuntimeError if numpy
is not available.
"""
return rosenbrock_grad(x, a=a, b=b)
def muller_brown_value(xy: Sequence[float]) -> float:
"""Evaluate Müller–Brown potential at 2D point (x,y)."""
return muller_brown(xy)
def muller_brown_gradient(xy: Sequence[float]):
"""Return gradient of Müller–Brown potential at 2D point (x,y)."""
return muller_brown_grad(xy)
# -------------------- turbomole and hummr helpers (re-exports) --------------------
# Import and re-export the helper functions from the respective modules so tests
# can import them from this high-level `interface` module.
from turbomole_interface import (
prepare_turbomole_coord,
run_turbomole,
turbomole_prepare_and_maybe_run,
ExternalProgramNotFound as _TURBO_EXTERNAL,
)
from hummr_interface import (
prepare_hummr_input,
run_hummr,
hummr_prepare_and_maybe_run,
)
# Expose a consistent ExternalProgramNotFound name from the turbomole module
# so callers importing it from `interface` get the same class used by helpers.
ExternalProgramNotFound = _TURBO_EXTERNAL