|
6 | 6 | class to hold the simulation data. |
7 | 7 | """ |
8 | 8 |
|
| 9 | +from importlib import import_module |
| 10 | + |
9 | 11 | from pysages.typing import Any, Callable, JaxArray, NamedTuple, Optional |
| 12 | +from pysages.utils import is_file |
10 | 13 |
|
11 | 14 | JaxMDState = Any |
12 | 15 |
|
@@ -58,3 +61,46 @@ class JaxMDContext(NamedTuple): |
58 | 61 | step_fn: Callable[..., JaxMDContextState] |
59 | 62 | box: JaxArray |
60 | 63 | 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