Continuing simulations#
This notebook will show you:
how to continue an existing ANN assisted TPS simulation from storage
Note: This notebook depends on files created in one of the first notebooks (1_Toy_pytorch_simple_setup.ipynb or 1_Toy_pytorch_simple_setup_XYDiagpot.ipynb).
Please execute one of them first if you have not already.
%matplotlib inline
import os
import aimmd
import openpathsampling as paths
WARNING:asyncmd.slurm.process:Could not initialize SLURM cluster handling. If you are sure SLURM (sinfo/sacct/etc) is available try calling `asyncmd.config.set_all_slurm_settings()` or `asyncmd.config.set_slurm_setting()` with the appropriate arguments.
WARNING:aimmd:dCGPy not found. SymReg will not be available.
# change to the working directory of choice
# (should be the same as in the first notebook)
wdir = 'SimData_pytorch_XYDiagpot_toy_22dim'
The easy way#
By continuing an openpathsampling TPS simulation that has been set up with aimmd you will autmatically load the RCModel saved automagically after the simulation. This will furhtermore repopulate the attached TrainSet with the shooting results from the openpathsampling.Storage.
To do this, simply continue a TPS simulation the OPS way.
# reimport any custom potential functions under the same names as for the simulation
from toy_potentials import XYDiagpot
# open old ops storage for appending
storage = paths.Storage(os.path.join(wdir, 'pytorch_toy_22dim.nc'), 'a')
# get the PathSampling from storage and set its state to the last MCStep in storage
sampler = storage.pathsimulators[0]
sampler.restart_at_step(storage.steps[-1])
WARNING:aimmd.ops.selector:`density_collection_hook` is None, no density collection will be performed.
WARNING:aimmd.ops.selector:Restoring RCModelSelector without model and density_collection_hook. Please take care of resetting the model and hook yourself.
# lets get the model and the trainset from the aimmd storage
aimmd_store = aimmd.Storage(os.path.join(wdir, 'aimmd_storage.h5'), mode='a')
model = aimmd_store.rcmodels["most_recent"] # the last model will alwyas be saved as "most_recent"
# this will restore any ops collective variables used as descriptor transform for the model
model = model.complete_from_ops_storage(storage)
# for the traisnet passing an ops storage automatically resets all ops objects to the values they had at save time
trainset = aimmd_store.load_trainset()
# create our hooks
trainhook = aimmd.ops.TrainingHook(model, trainset)
densityhook = aimmd.ops.DensityCollectionHook(model)
storehook = aimmd.ops.AimmdStorageHook(aimmd_store, model, trainset)
# and attach them
sampler.attach_hook(trainhook)
sampler.attach_hook(densityhook)
sampler.attach_hook(storehook)
# the only thing left is to take care of the waring above:
# we need to put the model into the RCModel selector
# (because it can not be saved to ops storages together with the selector)
# if you want to (re)set all RCModels in all RCModelSelectors of a ops simulation to the same model,
# you can use one of the aimmd utility functions
# Note: I think this is what you most likely want, since most people will either use only one RCModelSelector or the same Model in all Selectors (?)
aimmd.ops.set_rcmodel_and_hook_in_all_selectors(model=model,
density_collection_hook=densityhook,
simulation=sampler,
)
# now we can simply run the simulation again and it will start where we left of
sampler.run(10)
Working on Monte Carlo cycle number 1010
Running for 5 minutes 59 seconds - 39.99 seconds per step
Estimated time remaining: 39 seconds
DONE! Completed 1010 Monte Carlo cycles.
# close the storages
#storage.sync_all()
storage.close()
aimmd_store.close()
WARNING:asyncmd.config:Deregistered global writeable h5py cache. No TrajectoryFunction values will be cached until a new h5py cache has been registered.