Skip to content

Commit 67caa5b

Browse files
authored
Merge pull request #286 from pmrv/runner-context-manager
Close a stage's LAMMPS session when the stage raises
2 parents c07ac2a + a0f5bc4 commit 67caa5b

11 files changed

Lines changed: 224 additions & 28 deletions

File tree

‎calphy/alchemy.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _run_integration_coupling(self, iteration=1):
175175
"""Coupling-mode integration: ramp ONLY the last component 0->1
176176
(forward) and 1->0 (backward) over the fixed base.
177177
W = int <U_last>_lam dlam; dF = (W_f + W_b)/2 per iteration."""
178-
lmp = ph.create_object(self.calc, self.simfolder)
178+
lmp = self._open_lammps()
179179
conf = os.path.join(self.simfolder, "conf.equilibration.data")
180180
# style only before read_data (pair_coeff needs the box)
181181
ns = self.calc._pair_style_with_options
@@ -253,7 +253,7 @@ def run_averaging(self):
253253
Fix lattice option is not implemented at present.
254254
At the end of the run, the averaged box dimensions are calculated.
255255
"""
256-
lmp = ph.create_object(self.calc, self.simfolder)
256+
lmp = self._open_lammps()
257257

258258
if self.calc.alchemy_coupling:
259259
# equilibrate on the BASE system (lam = 0): full base, ramped
@@ -345,7 +345,7 @@ def run_integration(self, iteration=1):
345345
initial, final = self._end_states()
346346

347347
# create lammps object
348-
lmp = ph.create_object(self.calc, self.simfolder)
348+
lmp = self._open_lammps()
349349

350350
# Adiabatic switching parameters.
351351
lmp.command("variable li equal 1.0")

‎calphy/dcci.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ def equilibrate_cells(self):
345345
"""
346346
def equilibrate(phase):
347347
self.logger.info("Equilibrating the %s cell at %.2f K, %.2f bar" % (phase, self.t0, self.p_start))
348-
self.jobs[phase].run_averaging()
348+
with self.jobs[phase]:
349+
self.jobs[phase].run_averaging()
349350
self.logger.info("%s cell: %.4f A^3/atom" % (phase, self.jobs[phase].volatom))
350351

351352
self._for_cells(equilibrate)

‎calphy/helpers.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"""
2525

2626
import os
27+
import sys
2728
import shutil
2829
import warnings
2930
import logging
@@ -96,7 +97,7 @@ def create_object(calc, directory):
9697
cmdargs=calc.md.cmdargs,
9798
directory=directory,
9899
)
99-
return emit_init_commands(lmp, calc)
100+
return _emit_init_commands_or_close(lmp, calc)
100101

101102
binary = resolve_lammps_executable(calc.lammps_executable)
102103
mpi_command = (
@@ -113,7 +114,20 @@ def create_object(calc, directory):
113114
directory=directory,
114115
dry_run=False,
115116
)
116-
return emit_init_commands(lmp, calc)
117+
return _emit_init_commands_or_close(lmp, calc)
118+
119+
120+
def _emit_init_commands_or_close(lmp, calc):
121+
"""Run :func:`emit_init_commands`, closing ``lmp`` if it raises.
122+
123+
The caller only gets hold of the runner once :func:`create_object`
124+
returns, so a failure here would otherwise leave the session (in library
125+
mode, the executor behind it) open."""
126+
try:
127+
return emit_init_commands(lmp, calc)
128+
except BaseException:
129+
lmp.__exit__(*sys.exc_info())
130+
raise
117131

118132

119133
def emit_init_commands(lmp, calc):

‎calphy/library_runner.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
other part of calphy) never requires it.
3838
"""
3939
import os
40+
import sys
4041
import logging
4142

4243
from calphy.runner import BaseRunner, _normalize_cmdargs
@@ -81,7 +82,13 @@ def __init__(self, *, cores, cmdargs, directory):
8182
self.lmp = LammpsLibrary(
8283
cores=cores, working_directory=directory, cmdargs=cmdargs
8384
)
84-
self._activate_mliap()
85+
# the executor is live from here on; nobody else holds a reference to
86+
# this runner yet, so close it ourselves if the rest of setup fails
87+
try:
88+
self._activate_mliap()
89+
except BaseException:
90+
self.__exit__(*sys.exc_info())
91+
raise
8592

8693
def _activate_mliap(self):
8794
"""Register the mliappy coupling in the live session when available.

‎calphy/liquid.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def run_averaging(self):
162162
At the end of the run, the averaged box dimensions are calculated.
163163
"""
164164
# create lammps object
165-
lmp = ph.create_object(self.calc, self.simfolder)
165+
lmp = self._open_lammps()
166166

167167
lmp = ph.set_pair_style(lmp, self.calc)
168168

@@ -237,7 +237,7 @@ def run_integration(self, iteration=1):
237237
Run the integration routine where the initial and final systems are connected using
238238
the lambda parameter. See algorithm 4 in publication.
239239
"""
240-
lmp = ph.create_object(self.calc, self.simfolder)
240+
lmp = self._open_lammps()
241241

242242
# Adiabatic switching parameters.
243243
lmp.command("variable li equal 1.0")

‎calphy/phase.py‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ def _reversible_scaling_forward(self, iteration: int = 1) -> None:
14971497
iteration, t0, tf, li, lf, pi, pf,
14981498
)
14991499

1500-
lmp = ph.create_object(self.calc, self.simfolder)
1500+
lmp = self._open_lammps()
15011501

15021502
lmp.command("echo log")
15031503
lmp.command("variable li equal %f" % li)
@@ -1705,7 +1705,7 @@ def _reversible_scaling_backward(self, iteration: int = 1) -> None:
17051705
iteration, tf, t0, lf, li, pf, pi,
17061706
)
17071707

1708-
lmp = ph.create_object(self.calc, self.simfolder)
1708+
lmp = self._open_lammps()
17091709

17101710
lmp.command("echo log")
17111711
lmp.command("variable li equal %f" % li)
@@ -1983,7 +1983,7 @@ def scan_temperature_range(self):
19831983
)
19841984

19851985
# ── Build the LAMMPS object and load the equilibrated configuration ──
1986-
lmp = ph.create_object(self.calc, self.simfolder)
1986+
lmp = self._open_lammps()
19871987

19881988
lmp.command("echo log")
19891989
lmp = ph.set_pair_style(lmp, self.calc)
@@ -2173,7 +2173,7 @@ def temperature_scaling(self, iteration=1):
21732173
p0 = self.calc._pressure
21742174

21752175
# create lammps object
2176-
lmp = ph.create_object(self.calc, self.simfolder)
2176+
lmp = self._open_lammps()
21772177

21782178
lmp.command("echo log")
21792179
lmp.command("variable li equal %f" % li)
@@ -2323,7 +2323,7 @@ def pressure_scaling(self, iteration=1):
23232323
pf = self.calc._pressure_stop
23242324

23252325
# create lammps object
2326-
lmp = ph.create_object(self.calc, self.simfolder)
2326+
lmp = self._open_lammps()
23272327

23282328
lmp.command("echo log")
23292329
lmp.command("variable li equal %f" % li)
@@ -2482,5 +2482,25 @@ def clean_up(self):
24822482
with open(os.path.join(self.simfolder, "metadata.yaml"), "w") as fout:
24832483
yaml.safe_dump(metadata, fout)
24842484

2485+
def _open_lammps(self):
2486+
"""Create a stage's LAMMPS session and remember it, so that a
2487+
surrounding ``with job:`` can close it if the stage raises."""
2488+
self._lmp = ph.create_object(self.calc, self.simfolder)
2489+
return self._lmp
2490+
2491+
def __enter__(self):
2492+
return self
2493+
2494+
def __exit__(self, exc_type, exc, tb):
2495+
"""Close the session opened by the last stage, exception or not.
2496+
2497+
On success the stage has already closed it itself; closing again is a
2498+
no-op. The exception is never suppressed.
2499+
"""
2500+
lmp, self._lmp = getattr(self, "_lmp", None), None
2501+
if lmp is not None:
2502+
lmp.__exit__(exc_type, exc, tb)
2503+
return False
2504+
24852505
def lammps_close(self, lmp):
24862506
lmp.close()

‎calphy/routines.py‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ def run_jobs(self):
271271
self.logger.info("Starting solid reversible scaling run")
272272
for i in range(self.soljob.calc.n_iterations):
273273
try:
274-
self.soljob.reversible_scaling(iteration=(i + 1))
274+
with self.soljob:
275+
self.soljob.reversible_scaling(iteration=(i + 1))
275276
except MeltedError:
276277
self.logger.info("Solid system melted during reversible scaling run")
277278
return 2
@@ -290,7 +291,8 @@ def run_jobs(self):
290291
self.logger.info("Starting liquid reversible scaling calculation")
291292
for i in range(self.lqdjob.calc.n_iterations):
292293
try:
293-
self.lqdjob.reversible_scaling(iteration=(i + 1))
294+
with self.lqdjob:
295+
self.lqdjob.reversible_scaling(iteration=(i + 1))
294296
except SolidifiedError:
295297
self.logger.info("Liquid froze during reversible scaling calculation")
296298
return 3
@@ -527,14 +529,16 @@ def routine_fe(job):
527529
Perform an FE calculation routine
528530
"""
529531
ts = time.time()
530-
job.run_averaging()
532+
with job:
533+
job.run_averaging()
531534
te = time.time() - ts
532535
job.logger.info("Averaging routine finished in %f s" % te)
533536

534537
# now run integration loops
535538
for i in range(job.calc.n_iterations):
536539
ts = time.time()
537-
job.run_integration(iteration=(i + 1))
540+
with job:
541+
job.run_integration(iteration=(i + 1))
538542
te = time.time() - ts
539543
job.logger.info("Integration cycle %d finished in %f s" % (i + 1, te))
540544

@@ -556,14 +560,16 @@ def routine_ts(job):
556560
# in the single-phase region. Disabled by default (mode='none').
557561
if job.calc.phase_transition_detection.mode != "none":
558562
ts = time.time()
559-
job.scan_temperature_range()
563+
with job:
564+
job.scan_temperature_range()
560565
te = time.time() - ts
561566
job.logger.info("Pre-flight temperature-range scan finished in %f s" % te)
562567

563568
# now do rev scale steps
564569
for i in range(job.calc.n_iterations):
565570
ts = time.time()
566-
job.reversible_scaling(iteration=(i + 1))
571+
with job:
572+
job.reversible_scaling(iteration=(i + 1))
567573
te = time.time() - ts
568574
job.logger.info("TS integration cycle %d finished in %f s" % (i + 1, te))
569575

@@ -581,7 +587,8 @@ def routine_tscale(job):
581587
# now do rev scale steps
582588
for i in range(job.calc.n_iterations):
583589
ts = time.time()
584-
job.temperature_scaling(iteration=(i + 1))
590+
with job:
591+
job.temperature_scaling(iteration=(i + 1))
585592
te = time.time() - ts
586593
job.logger.info("Temperature scaling cycle %d finished in %f s" % (i + 1, te))
587594

@@ -599,7 +606,8 @@ def routine_pscale(job):
599606
# now do rev scale steps
600607
for i in range(job.calc.n_iterations):
601608
ts = time.time()
602-
job.pressure_scaling(iteration=(i + 1))
609+
with job:
610+
job.pressure_scaling(iteration=(i + 1))
603611
te = time.time() - ts
604612
job.logger.info("Pressure scaling cycle %d finished in %f s" % (i + 1, te))
605613

@@ -613,14 +621,16 @@ def routine_alchemy(job):
613621
Perform an FE calculation routine
614622
"""
615623
ts = time.time()
616-
job.run_averaging()
624+
with job:
625+
job.run_averaging()
617626
te = time.time() - ts
618627
job.logger.info("Averaging routine finished in %f s" % te)
619628

620629
# now run integration loops
621630
for i in range(job.calc.n_iterations):
622631
ts = time.time()
623-
job.run_integration(iteration=(i + 1))
632+
with job:
633+
job.run_integration(iteration=(i + 1))
624634
te = time.time() - ts
625635
job.logger.info("Alchemy integration cycle %d finished in %f s" % (i + 1, te))
626636

@@ -742,14 +752,16 @@ def routine_composition_scaling(job):
742752

743753
# now start cycle
744754
ts = time.time()
745-
job.run_averaging()
755+
with job:
756+
job.run_averaging()
746757
te = time.time() - ts
747758
job.logger.info("Averaging routine finished in %f s" % te)
748759

749760
# now run integration loops
750761
for i in range(job.calc.n_iterations):
751762
ts = time.time()
752-
job.run_integration(iteration=(i + 1))
763+
with job:
764+
job.run_integration(iteration=(i + 1))
753765
te = time.time() - ts
754766
job.logger.info("Alchemy integration cycle %d finished in %f s" % (i + 1, te))
755767

‎calphy/runner.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,34 @@ def close(self):
366366
"""End the session and release backend resources."""
367367
raise NotImplementedError
368368

369+
def __enter__(self):
370+
"""Enter a runner scope; the runner itself is the ``as`` target."""
371+
return self
372+
373+
def __exit__(self, exc_type, exc, tb):
374+
"""Close the session on the way out, exception or not.
375+
376+
This is what makes a stage's LAMMPS session -- and, in library mode,
377+
the executorlib executor behind it -- go away deterministically even
378+
when an exception escapes the driver. The driver's exception is never
379+
suppressed.
380+
381+
If :meth:`close` itself fails *while an exception is propagating*, the
382+
close error is logged and swallowed rather than replacing the original
383+
one: the interesting failure is the physics error that got us here, not
384+
the fallout of shutting a half-dead session down.
385+
"""
386+
try:
387+
self.close()
388+
except Exception:
389+
if exc_type is None:
390+
raise
391+
logger.exception(
392+
"closing the LAMMPS session failed while handling %s; the "
393+
"original exception is propagating", exc_type.__name__,
394+
)
395+
return False
396+
369397
def rotate_logs(self, stage_name):
370398
"""Collect the LAMMPS log output produced since the last rotation into
371399
``<stage_name>.log.lammps`` in the sim folder."""

‎calphy/solid.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def run_averaging(self):
192192
At the end of the run, the averaged box dimensions are calculated.
193193
"""
194194

195-
lmp = ph.create_object(self.calc, self.simfolder)
195+
lmp = self._open_lammps()
196196

197197
# set up potential
198198
lmp = ph.set_pair_style(lmp, self.calc)
@@ -268,7 +268,7 @@ def run_integration(self, iteration=1):
268268
Run the integration routine where the initial and final systems are connected using
269269
the lambda parameter. See algorithm 4 in publication.
270270
"""
271-
lmp = ph.create_object(self.calc, self.simfolder)
271+
lmp = self._open_lammps()
272272

273273
# set up potential
274274
lmp = ph.set_pair_style(lmp, self.calc)

0 commit comments

Comments
 (0)