Engine¶
The engine module provides interfaces to external simulation engines.
Base¶
Engine base classes for molecular simulation engines.
This module provides abstract base classes for running external computational chemistry programs like LAMMPS, CP2K, etc. It integrates with the core Script class for input file management.
Engine ¶
Engine(executable, *, check_executable=True)
Bases: ABC
Abstract base class for computational chemistry engines.
Provides a common interface for running external programs like LAMMPS, CP2K, etc. Each engine handles setup, execution, and output processing for its specific program.
The Engine class integrates with the core Script class for input file management. Scripts can be created from text, loaded from files, or loaded from URLs.
Attributes:
| Name | Type | Description |
|---|---|---|
executable |
Path or command to the executable |
|
work_dir |
Working directory for calculations |
|
scripts |
List of Script objects for input files |
|
input_script |
Primary input script (first script or script with 'input' tag) |
|
output_file |
Primary output file from the calculation |
Example
from molpy.core.script import Script from molpy.engine import LAMMPSEngine
Create input script¶
script = Script.from_text( ... name="input", ... text="units real\natom_style full\n", ... language="other" ... )
Create engine and prepare¶
engine = LAMMPSEngine(executable="lmp") engine.prepare(work_dir="./calc", scripts=[script])
Run calculation¶
result = engine.run() print(result.returncode) 0
Initialize the engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
executable
|
str
|
Path or command to the executable |
required |
check_executable
|
bool
|
Whether to check if executable exists in PATH (default: True) |
True
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If check_executable is True and executable not found |
Source code in src/molpy/engine/base.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
check_executable ¶
check_executable()
Check if the executable is available in the system PATH.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the executable is not found |
Source code in src/molpy/engine/base.py
82 83 84 85 86 87 88 89 90 91 92 93 | |
clean ¶
clean(keep_scripts=True)
Clean up calculation files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keep_scripts
|
bool
|
Whether to keep input scripts (default: True) |
True
|
Source code in src/molpy/engine/base.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
get_output_file ¶
get_output_file(name=None)
Get the output file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Name of the output file. If None, returns default output file. |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the output file or None if not found |
Source code in src/molpy/engine/base.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
get_script ¶
get_script(name=None, tag=None)
Get a script by name or tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Name of the script (logical name or filename) |
None
|
tag
|
str | None
|
Tag to search for |
None
|
Returns:
| Type | Description |
|---|---|
Script | None
|
Script object or None if not found |
Example
script = engine.get_script(name="input") script = engine.get_script(tag="input")
Source code in src/molpy/engine/base.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
list_output_files ¶
list_output_files()
List all output files in the working directory.
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of output file paths |
Source code in src/molpy/engine/base.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
prepare ¶
prepare(work_dir, scripts, *, auto_save=True)
Prepare the engine for execution by setting up the working directory and scripts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
work_dir
|
str | Path
|
Path to the working directory |
required |
scripts
|
Script | Sequence[Script]
|
Single Script object or sequence of Script objects |
required |
auto_save
|
bool
|
Whether to automatically save scripts to the working directory (default: True) |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The engine instance for method chaining |
Example
script = Script.from_text("input", "units real\natom_style full\n") engine.prepare("./calc", script)
Or with multiple scripts¶
engine.prepare("./calc", [script1, script2])
Source code in src/molpy/engine/base.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
run
abstractmethod
¶
run(**kwargs)
Execute the engine calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments for the specific engine (e.g., input_file, output_file, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
CompletedProcess object with execution results |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If engine is not prepared (prepare() not called) |
Source code in src/molpy/engine/base.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
CP2K¶
CP2K quantum chemistry engine.
This module provides the CP2KEngine class for running CP2K calculations.
CP2KEngine ¶
CP2KEngine(executable, *, check_executable=True)
Bases: Engine
CP2K quantum chemistry engine.
This engine runs CP2K calculations with input scripts.
Example
from molpy.core.script import Script from molpy.engine import CP2KEngine
Create input script¶
script = Script.from_text( ... name="input", ... text="&GLOBAL\n PROJECT water\n&END GLOBAL\n", ... language="other" ... )
Create engine and prepare¶
engine = CP2KEngine(executable="cp2k.psmp") engine.prepare(work_dir="./calc", scripts=script)
Run calculation¶
result = engine.run() print(result.returncode) 0
Source code in src/molpy/engine/base.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
get_output_file ¶
get_output_file(name=None)
Get the CP2K output file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Name of the output file. If None, uses default "cp2k.out". |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the output file or None if not found |
Source code in src/molpy/engine/cp2k.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
run ¶
run(input_file=None, output_file=None, **kwargs)
Execute CP2K calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
str | Path | None
|
Name of the input file. If None, uses the input script from prepare() (default: None) |
None
|
output_file
|
str | Path | None
|
Name of the output file. If None, uses default "cp2k.out" (default: None) |
None
|
**kwargs
|
Any
|
Additional arguments passed to subprocess.run (e.g., capture_output, text, check, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
CompletedProcess object with execution results |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If engine is not prepared (prepare() not called) |
Source code in src/molpy/engine/cp2k.py
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
LAMMPS¶
LAMMPS molecular dynamics engine.
This module provides the LAMMPSEngine class for running LAMMPS calculations.
LAMMPSEngine ¶
LAMMPSEngine(executable, *, check_executable=True)
Bases: Engine
LAMMPS molecular dynamics engine.
This engine runs LAMMPS calculations with input scripts.
Example
from molpy.core.script import Script from molpy.engine import LAMMPSEngine
Create input script¶
script = Script.from_text( ... name="input", ... text="units real\natom_style full\n", ... language="other" ... )
Create engine and prepare¶
engine = LAMMPSEngine(executable="lmp") engine.prepare(work_dir="./calc", scripts=script)
Run calculation¶
result = engine.run() print(result.returncode) 0
Source code in src/molpy/engine/base.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
get_log_file ¶
get_log_file()
Get the LAMMPS log file path.
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the log file or None if not found |
Source code in src/molpy/engine/lammps.py
108 109 110 111 112 113 114 115 116 117 118 | |
run ¶
run(input_file=None, log_file=None, **kwargs)
Execute LAMMPS calculation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_file
|
str | Path | None
|
Name of the input script file. If None, uses the input script from prepare() (default: None) |
None
|
log_file
|
str | Path | None
|
Name of the log file. If None, uses default "log.lammps" (default: None) |
None
|
**kwargs
|
Any
|
Additional arguments passed to subprocess.run (e.g., capture_output, text, check, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
CompletedProcess
|
CompletedProcess object with execution results |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If engine is not prepared (prepare() not called) |
Source code in src/molpy/engine/lammps.py
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |