Skip to content

Commit e48cc33

Browse files
committed
Add a context generator class for Qbox
1 parent d4d25a9 commit e48cc33

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pysages/backends/contexts.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
class to hold the simulation data.
77
"""
88

9+
from importlib import import_module
10+
911
from pysages.typing import Any, Callable, JaxArray, NamedTuple, Optional
12+
from pysages.utils import is_file
1013

1114
JaxMDState = Any
1215

@@ -58,3 +61,46 @@ class JaxMDContext(NamedTuple):
5861
step_fn: Callable[..., JaxMDContextState]
5962
box: JaxArray
6063
dt: float
64+
65+
66+
class QboxContextGenerator:
67+
"""
68+
Provides an interface for setting up Qbox-backed simulations.
69+
70+
Arguments
71+
---------
72+
launch_command: str
73+
Specifies the command that will be used to run Qbox in interactive mode,
74+
e.g. `qb` or `mpirun -n 4 qb`.
75+
76+
input_script: str
77+
Path to the Qbox input script.
78+
79+
output_filename: Union[Path, str]
80+
Name for the output file. It must not exist on the working directory.
81+
Defaults to `qb.r`.
82+
"""
83+
84+
def __init__(self, launch_command, input_script, output_filename="qb.r"):
85+
self.cmd = launch_command
86+
self.script = input_script
87+
self.logfile = output_filename
88+
89+
def __call__(self, **kwargs):
90+
if not is_file(self.script):
91+
raise FileNotFoundError(f"Unable to find or open {self.script}")
92+
93+
if is_file(self.logfile):
94+
msg = f"Delete {self.logfile} or choose a different output file name"
95+
raise FileExistsError(msg)
96+
97+
pexpect = import_module("pexpect")
98+
99+
qb = pexpect.spawn(self.cmd)
100+
qb.logfile_read = open(self.logfile, "wb")
101+
qb.expect(r"\[qbox\] ")
102+
103+
qb.sendline(self.script)
104+
qb.expect(r"\[qbox\] ")
105+
106+
return qb

0 commit comments

Comments
 (0)