Skip to content

Commit 7631d80

Browse files
Exception handling (#343)
* Handle Lammps crashing * Add test case * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * bug fixes * break after error * clean up after test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 33d72e9 commit 7631d80

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

pylammpsmpi/mpi/lmpmpi.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,16 @@ def _run_lammps_mpi(argument_lst):
482482
interface_send(socket=socket, result_dict={"result": True})
483483
interface_shutdown(socket=socket, context=context)
484484
break
485-
output = select_cmd(input_dict["command"])(
486-
job=job, funct_args=input_dict["args"]
487-
)
488-
if MPI.COMM_WORLD.rank == 0 and output is not None:
489-
interface_send(socket=socket, result_dict={"result": output})
485+
try:
486+
output = select_cmd(input_dict["command"])(
487+
job=job, funct_args=input_dict["args"]
488+
)
489+
except Exception as error:
490+
if MPI.COMM_WORLD.rank == 0:
491+
interface_send(socket=socket, result_dict={"error": error})
492+
else:
493+
if MPI.COMM_WORLD.rank == 0 and output is not None:
494+
interface_send(socket=socket, result_dict={"result": output})
490495

491496

492497
if __name__ == "__main__":

pylammpsmpi/wrapper/concurrent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ def execute_async(
6868
elif "command" in task_dict and "future" in task_dict:
6969
f = task_dict.pop("future")
7070
if f.set_running_or_notify_cancel():
71-
f.set_result(interface.send_and_receive_dict(input_dict=task_dict))
71+
try:
72+
f.set_result(interface.send_and_receive_dict(input_dict=task_dict))
73+
except Exception as error:
74+
f.set_exception(error)
75+
break
7276

7377

7478
class LammpsConcurrent:

tests/test_exception.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os.path
2+
from unittest import TestCase
3+
from pylammpsmpi import LammpsLibrary
4+
5+
6+
lmp_str = """\
7+
# 3d Lennard-Jones melt
8+
9+
units lj
10+
atom_style atomic
11+
atom_modify map array
12+
13+
lattice fcc 0.8442
14+
region box block 0 4 0 4 0 4
15+
create_box 1 box
16+
create_atoms 1 box
17+
mass 1 1.0
18+
19+
velocity all create 100.44 87287 loop geom
20+
21+
pair_style lj/cut 2.5
22+
pair_coeff 1 1 1.0 1.0 2.5
23+
24+
neighbor 0.3 bin
25+
neigh_modify delay 0 every 20 check no
26+
27+
compute 1 all temp
28+
compute 2 all pressure 1
29+
compute ke all ke/atom
30+
compute msd all msd
31+
compute v all property/atom vx vy vz
32+
33+
variable fx atom fx
34+
variable tt equal temp
35+
variable test string "25"
36+
fix 1 all nve
37+
fix 2 all ave/time 10 1 10 c_1 c_2
38+
39+
40+
41+
run 1000
42+
"""
43+
44+
45+
class TestException(TestCase):
46+
def test_overlapping_atoms(self):
47+
with open("in.error", "w") as f:
48+
f.writelines(lmp_str)
49+
with self.assertRaises(Exception):
50+
lmp = LammpsLibrary(cores=2, mode="local")
51+
lmp.file("in.error")
52+
53+
def tearDown(self):
54+
for f in ["in.error", "log.lammps"]:
55+
if os.path.exists(f):
56+
os.remove(f)

0 commit comments

Comments
 (0)