From 919c224cf35d87670b81b81222a256a4cb85f625 Mon Sep 17 00:00:00 2001 From: evenmn Date: Mon, 30 Aug 2021 13:49:06 +0200 Subject: [PATCH 1/2] Found a way to kill LAMMPS automatically when killing the Python process --- lammps_simulator/__init__.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lammps_simulator/__init__.py b/lammps_simulator/__init__.py index ee1a167..762c9ad 100644 --- a/lammps_simulator/__init__.py +++ b/lammps_simulator/__init__.py @@ -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 @@ -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 @@ -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 From 06c698ffc2a5c312ab2c1bf732bd0e0f67cf816d Mon Sep 17 00:00:00 2001 From: evenmn Date: Thu, 10 Mar 2022 13:59:27 +0100 Subject: [PATCH 2/2] Added slurm CPU example --- examples/slurm_cpu.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 examples/slurm_cpu.py diff --git a/examples/slurm_cpu.py b/examples/slurm_cpu.py new file mode 100644 index 0000000..a56c26f --- /dev/null +++ b/examples/slurm_cpu.py @@ -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)