-
Notifications
You must be signed in to change notification settings - Fork 5
Add Lammps function for backwards compatibility to pyiron_atomistics #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a2e7327
5f29115
e8d3465
381a624
79c6cb3
520f1ea
289123c
2fcdedb
aa892fe
7fc139f
e946f96
a2b6303
b48ebb1
0196abb
98cb97a
8a53bd9
2a4e10c
fd034b0
4d2101f
8c91424
cc39ac5
47a1ee7
c5ae8e3
6cc5e0a
601a76b
0a2c502
232995e
6a614f9
5f32e2c
fdc415c
800c2d7
abf5590
8e9ebc0
f3bda4c
7adda1d
37b2379
4dd26b2
fc80c8e
81a583b
98e73cd
46ec6a0
86da131
318f5a6
38ba25d
06c94af
f82bb3d
1587277
ee4d7e5
99512a1
f376116
dffaf72
c815c16
6d0220a
a263de3
8ca0644
19c5a01
87a577f
bb9a8b6
192006b
8b2d4a2
dee5eb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import os | ||
| import subprocess | ||
| from typing import Optional | ||
|
|
||
| from ase.atoms import Atoms | ||
|
|
||
| from pyiron_lammps.compatibility.calculate import ( | ||
| calc_md, | ||
| calc_minimize, | ||
| calc_static, | ||
| ) | ||
| from pyiron_lammps.output import parse_lammps_output | ||
| from pyiron_lammps.potential import get_potential_by_name | ||
| from pyiron_lammps.structure import write_lammps_datafile | ||
|
|
||
|
|
||
| def lammps_file_interface_function( | ||
| working_directory: str, | ||
| structure: Atoms, | ||
| potential: str, | ||
| calc_mode: str = "static", | ||
| calc_kwargs: Optional[dict] = None, | ||
| units: str = "metal", | ||
| lmp_command: str = "mpiexec -n 1 --oversubscribe lmp_mpi -in lmp.in", | ||
| resource_path: Optional[str] = None, | ||
| ): | ||
| """ | ||
| A single function to execute a LAMMPS calculation based on the LAMMPS job implemented in pyiron | ||
|
|
||
| Examples: | ||
|
|
||
| >>> import os | ||
| >>> from ase.build import bulk | ||
| >>> from pyiron_atomistics.lammps.lammps import lammps_function | ||
| >>> | ||
| >>> shell_output, parsed_output, job_crashed = lammps_function( | ||
| ... working_directory=os.path.abspath("lmp_working_directory"), | ||
| ... structure=bulk("Al", cubic=True), | ||
| ... potential='2009--Mendelev-M-I--Al-Mg--LAMMPS--ipr1', | ||
| ... calc_mode="md", | ||
| ... calc_kwargs={"temperature": 500.0, "pressure": 0.0, "n_ionic_steps": 1000, "n_print": 100}, | ||
| ... cutoff_radius=None, | ||
| ... units="metal", | ||
| ... bonds_kwargs={}, | ||
| ... enable_h5md=False, | ||
| ... ) | ||
|
|
||
| Args: | ||
| working_directory (str): directory in which the LAMMPS calculation is executed | ||
| structure (Atoms): ase.atoms.Atoms - atomistic structure | ||
| potential (str): Name of the LAMMPS potential based on the NIST database and the OpenKIM database | ||
| calc_mode (str): select mode of calculation ["static", "md", "minimize", "vcsgc"] | ||
| calc_kwargs (dict): key-word arguments for the calculate function, the input parameters depend on the calc_mode: | ||
| "static": No parameters | ||
| "md": "temperature", "pressure", "n_ionic_steps", "time_step", "n_print", "temperature_damping_timescale", | ||
| "pressure_damping_timescale", "seed", "tloop", "initial_temperature", "langevin", "delta_temp", | ||
| "delta_press", job_name", "rotation_matrix" | ||
| "minimize": "ionic_energy_tolerance", "ionic_force_tolerance", "max_iter", "pressure", "n_print", "style", | ||
| "rotation_matrix" | ||
| "vcsgc": "mu", "ordered_element_list", "target_concentration", "kappa", "mc_step_interval", "swap_fraction", | ||
| "temperature_mc", "window_size", "window_moves", "temperature", "pressure", "n_ionic_steps", | ||
| "time_step", "n_print", "temperature_damping_timescale", "pressure_damping_timescale", "seed", | ||
| "initial_temperature", "langevin", "job_name", "rotation_matrix" | ||
| cutoff_radius (float): cut-off radius for the interatomic potential | ||
| units (str): Units for LAMMPS | ||
| bonds_kwargs (dict): key-word arguments to create atomistic bonds: | ||
| "species", "element_list", "cutoff_list", "max_bond_list", "bond_type_list", "angle_type_list", | ||
| server_kwargs (dict): key-word arguments to create server object - the available parameters are: | ||
| "user", "host", "run_mode", "queue", "qid", "cores", "threads", "new_h5", "structure_id", "run_time", | ||
| "memory_limit", "accept_crash", "additional_arguments", "gpus", "conda_environment_name", | ||
| "conda_environment_path" | ||
| enable_h5md (bool): activate h5md mode for LAMMPS | ||
| write_restart_file (bool): enable writing the LAMMPS restart file | ||
| read_restart_file (bool): enable loading the LAMMPS restart file | ||
| restart_file (str): file name of the LAMMPS restart file to copy | ||
| executable_version (str): LAMMPS version to for the execution | ||
| executable_path (str): path to the LAMMPS executable | ||
| input_control_file (str|list|dict): Option to modify the LAMMPS input file directly | ||
|
|
||
|
Comment on lines
+48
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainFunction docstring is out of sync with the actual signature and supported modes
Currently, only: (working_directory, structure, potential, calc_mode, calc_kwargs, units, lmp_command, resource_path)are accepted, and To avoid confusion for users of this new entry point:
Similarly, remove or clarify Also applies to: 87-105, 107-121 🏁 Script executed: #!/bin/bash
# Extract the function definition, docstring, and implementation
cat pyiron_lammps/compatibility/file.py | head -150 | tail -110Length of output: 5353 🏁 Script executed: #!/bin/bash
# Search for calc_mode validation and ValueError handling
rg -n -A5 -B5 'calc_mode' pyiron_lammps/compatibility/file.pyLength of output: 2890 🏁 Script executed: #!/bin/bash
# Look for parameter handling in the file (cutoff_radius, bonds_kwargs, etc.)
rg -n 'cutoff_radius|bonds_kwargs|server_kwargs|enable_h5md|restart_file|executable' pyiron_lammps/compatibility/file.pyLength of output: 793 Update docstring to match actual function signature and supported calc_modes
Similarly, the docstring lists Users following the docstring will encounter immediate errors. Either:
Also remove |
||
| Returns: | ||
| str, dict, bool: Tuple consisting of the shell output (str), the parsed output (dict) and a boolean flag if | ||
| the execution raised an accepted error. | ||
| """ | ||
| if calc_kwargs is None: | ||
| calc_kwargs = {} | ||
|
|
||
| os.makedirs(working_directory, exist_ok=True) | ||
| potential_dataframe = get_potential_by_name( | ||
| potential_name=potential, resource_path=resource_path | ||
| ) | ||
| lmp_str_lst = lammps_file_initialization(structure=structure) | ||
| lmp_str_lst += potential_dataframe["Config"] | ||
| lmp_str_lst += ["variable dumptime equal {} ".format(calc_kwargs.get("n_print", 1))] | ||
| lmp_str_lst += [ | ||
| "dump 1 all custom ${dumptime} dump.out id type xsu ysu zsu fx fy fz vx vy vz", | ||
| 'dump_modify 1 sort id format line "%d %d %20.15g %20.15g %20.15g %20.15g %20.15g %20.15g %20.15g %20.15g %20.15g"', | ||
| ] | ||
|
Comment on lines
+87
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You accept
but you call: lmp_str_lst = lammps_file_initialization(structure=structure)
even if the caller requested a different unit system. This desynchronises the simulation units from the datafile/output parsing. You probably want: - lmp_str_lst = lammps_file_initialization(structure=structure)
+ lmp_str_lst = lammps_file_initialization(structure=structure, units=units)so all components (input, datafile, parsing) agree on the unit system. Also applies to: 123-124 🤖 Prompt for AI Agents |
||
|
|
||
| if calc_mode == "static": | ||
| lmp_str_lst += calc_static() | ||
| elif calc_mode == "md": | ||
| if "n_ionic_steps" in calc_kwargs.keys(): | ||
| n_ionic_steps = int(calc_kwargs.pop("n_ionic_steps")) | ||
| else: | ||
| n_ionic_steps = 1 | ||
| calc_kwargs["units"] = units | ||
| lmp_str_lst += calc_md(**calc_kwargs) | ||
| lmp_str_lst += ["run {} ".format(n_ionic_steps)] | ||
| elif calc_mode == "minimize": | ||
| calc_kwargs["units"] = units | ||
| lmp_str_tmp_lst, structure = calc_minimize(structure=structure, **calc_kwargs) | ||
| lmp_str_lst += lmp_str_tmp_lst | ||
| else: | ||
| raise ValueError( | ||
| f"calc_mode must be one of: static, md or minimize, not {calc_mode}" | ||
| ) | ||
|
|
||
| with open(os.path.join(working_directory, "lmp.in"), "w") as f: | ||
| f.writelines([l + "\n" for l in lmp_str_lst]) | ||
|
|
||
| write_lammps_datafile( | ||
| structure=structure, | ||
| potential_elements=potential_dataframe["Species"], | ||
| bond_dict=None, | ||
| units=units, | ||
| file_name="lammps.data", | ||
| working_directory=working_directory, | ||
| ) | ||
|
|
||
| shell = subprocess.check_output( | ||
| lmp_command, | ||
| cwd=working_directory, | ||
| shell=True, | ||
| universal_newlines=True, | ||
| env=os.environ.copy(), | ||
| ) | ||
|
Comment on lines
+130
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain
The function executes: shell = subprocess.check_output(
lmp_command,
cwd=working_directory,
shell=True,
universal_newlines=True,
env=os.environ.copy(),
)Using If
Given this is a low‑level interface, tightening it or documenting the expectation would be enough. I'll verify this security concern by examining the code context to determine if 🏁 Script executed: #!/bin/bash
# Examine the full function context around the subprocess call
cat pyiron_lammps/compatibility/file.py | head -150 | tail -50Length of output: 1799 🏁 Script executed: #!/bin/bash
# Get broader context of the file to understand function signature and parameters
grep -n "def " pyiron_lammps/compatibility/file.pyLength of output: 113 🏁 Script executed: #!/bin/bash
# Search for lmp_command usage and origin in the file
rg -n "lmp_command" pyiron_lammps/compatibility/file.py -B 5 -A 5Length of output: 748 🏁 Script executed: #!/bin/bash
# Check for any imports and the overall structure of this module
head -50 pyiron_lammps/compatibility/file.pyLength of output: 1609 I need to examine where this function is called to understand the actual risk profile. Length of output: 624 🏁 Script executed: #!/bin/bash
# Check if the function is exported or documented as public API
grep -r "lammps_file_interface_function" --include="*.py" --include="*.md" . 2>/dev/null | head -20Length of output: 137 🏁 Script executed: #!/bin/bash
# Look for __init__.py or __all__ exports to see if function is in public API
find . -name "__init__.py" -type f -exec grep -l "lammps_file_interface_function" {} \;Length of output: 27 Tighten The security concern is valid. Recommended fixes:
🧰 Tools🪛 Ruff (0.14.5)126-126: (S602) |
||
| output = parse_lammps_output( | ||
| working_directory=working_directory, | ||
| structure=structure, | ||
| potential_elements=potential_dataframe["Species"], | ||
| units=units, | ||
| prism=None, | ||
| dump_h5_file_name="dump.h5", | ||
| dump_out_file_name="dump.out", | ||
| log_lammps_file_name="log.lammps", | ||
| ) | ||
| return shell, output, False | ||
|
|
||
|
|
||
| def lammps_file_initialization(structure, dimension=3, units="metal"): | ||
| boundary = " ".join(["p" if coord else "f" for coord in structure.pbc]) | ||
| init_commands = [ | ||
| "units " + units, | ||
| "dimension " + str(dimension), | ||
| "boundary " + boundary + "", | ||
| "atom_style atomic", | ||
| "read_data lammps.data", | ||
| ] | ||
| return init_commands | ||
Uh oh!
There was an error while loading. Please reload this page.