@@ -532,12 +532,14 @@ def mix_states(self):
532532
533533 # Update the water state in the GCMCSampler.
534534 self ._gcmc_samplers [i ].push ()
535- self ._gcmc_samplers [i ]._set_water_state (
536- self ._dynamics [i ].context (),
537- indices = water_idxs ,
538- states = self ._gcmc_states [state ][water_idxs ],
539- )
540- self ._gcmc_samplers [i ].pop ()
535+ try :
536+ self ._gcmc_samplers [i ]._set_water_state (
537+ self ._dynamics [i ].context (),
538+ indices = water_idxs ,
539+ states = self ._gcmc_states [state ][water_idxs ],
540+ )
541+ finally :
542+ self ._gcmc_samplers [i ].pop ()
541543
542544 # Update the swap matrix.
543545 old_state = self ._old_states [i ]
@@ -820,12 +822,14 @@ def __init__(self, system, config):
820822 # Reset the GCMC water state.
821823 if gcmc_sampler is not None :
822824 gcmc_sampler .push ()
823- gcmc_sampler ._set_water_state (
824- dynamics .context (),
825- states = self ._dynamics_cache ._gcmc_states [state ],
826- force = True ,
827- )
828- gcmc_sampler .pop ()
825+ try :
826+ gcmc_sampler ._set_water_state (
827+ dynamics .context (),
828+ states = self ._dynamics_cache ._gcmc_states [state ],
829+ force = True ,
830+ )
831+ finally :
832+ gcmc_sampler .pop ()
829833
830834 # Conversion factor for reduced potential.
831835 kT = (_sr .units .k_boltz * self ._config .temperature ).to (_sr .units .kcal_per_mol )
@@ -1277,13 +1281,13 @@ def _run_block(
12771281 if is_gcmc :
12781282 # Push the PyCUDA context on top of the stack.
12791283 gcmc_sampler .push ()
1280-
1281- # Perform the GCMC move.
1282- _logger .info (f"Performing GCMC move at { _lam_sym } = { lam :.5f} " )
1283- gcmc_sampler .move (dynamics .context ())
1284-
1285- # Remove the PyCUDA context from the stack.
1286- gcmc_sampler .pop ()
1284+ try :
1285+ # Perform the GCMC move.
1286+ _logger .info (f"Performing GCMC move at { _lam_sym } = { lam :.5f} " )
1287+ gcmc_sampler .move (dynamics .context ())
1288+ finally :
1289+ # Remove the PyCUDA context from the stack.
1290+ gcmc_sampler .pop ()
12871291
12881292 # Save the GCMC state.
12891293 self ._dynamics_cache .save_gcmc_state (index )
@@ -1340,15 +1344,15 @@ def _minimise(self, index):
13401344 if gcmc_sampler is not None :
13411345 # Push the PyCUDA context on top of the stack.
13421346 gcmc_sampler .push ()
1343-
1344- _logger .info (
1345- f"Pre-equilibrating with GCMC moves at { _lam_sym } = { self ._lambda_values [index ]:.5f} "
1346- )
1347- for i in range (100 ):
1348- gcmc_sampler .move (dynamics .context ())
1349-
1350- # Remove the PyCUDA context from the stack.
1351- gcmc_sampler .pop ()
1347+ try :
1348+ _logger .info (
1349+ f"Pre-equilibrating with GCMC moves at { _lam_sym } = { self ._lambda_values [index ]:.5f} "
1350+ )
1351+ for i in range (100 ):
1352+ gcmc_sampler .move (dynamics .context ())
1353+ finally :
1354+ # Remove the PyCUDA context from the stack.
1355+ gcmc_sampler .pop ()
13521356
13531357 # Minimise.
13541358 dynamics .minimise (timeout = self ._config .timeout )
@@ -1433,15 +1437,15 @@ def _equilibrate(self, index):
14331437 if gcmc_sampler is not None :
14341438 # Push the PyCUDA context on top of the stack.
14351439 gcmc_sampler .push ()
1436-
1437- _logger .info (
1438- f"Equilibrating with GCMC moves at { _lam_sym } = { self ._lambda_values [index ]:.5f} "
1439- )
1440- for i in range (100 ):
1441- gcmc_sampler .move (dynamics .context ())
1442-
1443- # Remove the PyCUDA context from the stack.
1444- gcmc_sampler .pop ()
1440+ try :
1441+ _logger .info (
1442+ f"Equilibrating with GCMC moves at { _lam_sym } = { self ._lambda_values [index ]:.5f} "
1443+ )
1444+ for i in range (100 ):
1445+ gcmc_sampler .move (dynamics .context ())
1446+ finally :
1447+ # Remove the PyCUDA context from the stack.
1448+ gcmc_sampler .pop ()
14451449
14461450 # Store the current water state.
14471451 water_state = gcmc_sampler .water_state ()
@@ -1693,14 +1697,14 @@ def _checkpoint(self, index, lambdas, block, num_blocks, is_final_block=False):
16931697 if gcmc_sampler is not None :
16941698 # Push the PyCUDA context on top of the stack.
16951699 gcmc_sampler .push ()
1696-
1697- _logger .info (
1698- f"Current number of waters in GCMC volume at { _lam_sym } = { lam :.5f} "
1699- f"is { gcmc_sampler .num_waters ()} "
1700- )
1701-
1702- # Remove the PyCUDA context from the stack.
1703- gcmc_sampler .pop ()
1700+ try :
1701+ _logger .info (
1702+ f"Current number of waters in GCMC volume at { _lam_sym } = { lam :.5f} "
1703+ f"is { gcmc_sampler .num_waters ()} "
1704+ )
1705+ finally :
1706+ # Remove the PyCUDA context from the stack.
1707+ gcmc_sampler .pop ()
17041708
17051709 if is_final_block :
17061710 _logger .success (f"{ _lam_sym } = { lam :.5f} complete" )
@@ -1823,12 +1827,12 @@ def _reset_gcmc_sampler(gcmc_sampler, dynamics):
18231827
18241828 # Push the PyCUDA context on top of the stack.
18251829 gcmc_sampler .push ()
1826-
1827- # Set the water state.
1828- gcmc_sampler ._set_water_state (dynamics .context (), force = True )
1829-
1830- # Remove the PyCUDA context from the stack.
1831- gcmc_sampler .pop ()
1830+ try :
1831+ # Set the water state.
1832+ gcmc_sampler ._set_water_state (dynamics .context (), force = True )
1833+ finally :
1834+ # Remove the PyCUDA context from the stack.
1835+ gcmc_sampler .pop ()
18321836
18331837 # Re-bind the GCMC sampler to the dynamics object.
18341838 gcmc_sampler .bind_dynamics (dynamics )
0 commit comments