Skip to content
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

Found a way to kill LAMMPS automatically when killing the Python process #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/slurm_cpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lammps_simulator import Simulator
from lammps_simulator.computer import SlurmCPU

computer = SlurmCPU(1, lmp_exec="lmp")

sim = Simulator(directory="simulation")
sim.set_input_script("in.lammps", temp=300)
sim.run(computer=computer)
19 changes: 19 additions & 0 deletions lammps_simulator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import os
import sys
import signal
import shutil
#import psutil
import subprocess


def kill_lammps(pid):
def signal_handler(sig, frame):
#p = psutil.Process(pid)
#p.terminate()
os.kill(pid, signal.SIGTERM)
raise KeyboardInterrupt

signal.signal(signal.SIGINT, signal_handler)




class Simulator:
"""Initialize class

Expand Down Expand Up @@ -77,6 +92,8 @@ def run(self, computer=CPU(num_procs=4), stdout=subprocess.DEVNULL,
main_path = os.getcwd()
os.chdir(self.wd)
job_id = computer(self.lmp_script, self.var, stdout, stderr)
if computer.slurm == False:
kill_lammps(job_id)
os.chdir(main_path)
return job_id

Expand All @@ -95,5 +112,7 @@ def run_custom(self, stdout=subprocess.DEVNULL,
main_path = os.getcwd()
os.chdir(self.wd)
job_id = computer(self.lmp_script, self.var, stdout, stderr)
if computer.slurm == False:
kill_lammps(job_id)
os.chdir(main_path)
return job_id