def test_make_ensemble_with_forbidden(self): forbidden = paths.CVDefinedVolume(self.cv_inc, 0.55, 0.65) transitions = self.network.sampling_transitions # TODO: switch once network is working ensemble = self.post_network.make_ensemble(transitions, forbidden) #ensemble = self.ms_outer.make_ensemble(transitions, forbidden) test_AA = make_1d_traj([-0.1, 0.2, -0.2]) test_AXA = make_1d_traj([-0.1, 0.7, -0.2]) test_AFA = make_1d_traj([-0.1, 0.6, -0.2]) test_BB = make_1d_traj([1.1, 0.9, 1.2]) test_BXB = make_1d_traj([1.1, 0.5, 1.2]) test_BFB = make_1d_traj([1.1, 0.6, 1.2]) test_AXB = make_1d_traj([-0.1, 0.7, 1.1]) test_AFB = make_1d_traj([-0.1, 0.6, 1.1]) test_BXA = make_1d_traj([1.1, 0.5, -0.1]) test_BFA = make_1d_traj([1.1, 0.6, -0.1]) assert_equal(ensemble(test_AA), False) assert_equal(ensemble(test_AXA), True) assert_equal(ensemble(test_BB), False) assert_equal(ensemble(test_BXB), True) assert_equal(ensemble(test_BXA), True) assert_equal(ensemble(test_AXB), True) assert_equal(ensemble(test_AFA), False) assert_equal(ensemble(test_BFB), False) assert_equal(ensemble(test_AFB), False) assert_equal(ensemble(test_BFA), False)
def setup(self): self.dyn = CalvinistDynamics([-0.1, 0.1, 0.3, 0.5, 0.7, -0.1, 0.2, 0.4, 0.6, 0.8, ]) self.dyn.initialized = True SampleMover.engine = self.dyn op = CV_Function("myid", f=lambda snap : snap.coordinates[0][0]) stateA = CVRangeVolume(op, -100, 0.0) stateB = CVRangeVolume(op, 0.65, 100) volX = CVRangeVolume(op, -100, 0.25) volY = CVRangeVolume(op, -100, 0.40) self.ens1 = paths.TISEnsemble(stateA, stateB, volX, op) self.ens2 = paths.TISEnsemble(stateA, stateB, volY, op) init_traj1 = make_1d_traj( coordinates=[-0.1, 0.1, 0.2, 0.3, 0.24, 0.15, 0.06, -0.07], velocities=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ) init_traj2 = make_1d_traj( coordinates=[-0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], velocities=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ) self.samp1 = Sample(trajectory=init_traj1, replica=0, ensemble=self.ens1) self.samp2 = Sample(trajectory=init_traj2, replica=1, ensemble=self.ens2) self.shooter = ForwardShootMover(selector=UniformSelector(), ensemble=self.ens2) self.pathrev = PathReversalMover(ensemble=self.ens1) ens_dict = {self.ens1 : self.pathrev, self.ens2 : self.shooter} # self.mover = EnsembleDictionaryMover(ens_dict) self.mover = RandomAllowedChoiceMover([self.shooter, self.pathrev])
def setup(self): self.dyn = CalvinistDynamics([-0.1, 0.1, 0.3, 0.5, 0.7, -0.1, 0.2, 0.4, 0.6, 0.8, ]) self.dyn.initialized = True SampleGeneratingMover.engine = self.dyn op = CV_Function("myid", fcn=lambda snap : snap.coordinates[0][0]) stateA = CVRangeVolume(op, -100, 0.0) stateB = CVRangeVolume(op, 0.65, 100) volX = CVRangeVolume(op, -100, 0.25) volY = CVRangeVolume(op, -100, 0.40) self.ens1 = paths.TISEnsemble(stateA, stateB, volX, op) self.ens2 = paths.TISEnsemble(stateA, stateB, volY, op) init_traj1 = make_1d_traj( coordinates=[-0.1, 0.1, 0.2, 0.3, 0.24, 0.15, 0.06, -0.07], velocities=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ) init_traj2 = make_1d_traj( coordinates=[-0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], velocities=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ) self.samp1 = Sample(trajectory=init_traj1, replica=0, ensemble=self.ens1) self.samp2 = Sample(trajectory=init_traj2, replica=1, ensemble=self.ens2) self.shooter = ForwardShootMover(selector=UniformSelector(), ensemble=self.ens2) self.pathrev = PathReversalMover(ensemble=self.ens1) ens_dict = {self.ens1 : self.pathrev, self.ens2 : self.shooter} # self.mover = EnsembleDictionaryMover(ens_dict) self.mover = RandomAllowedChoiceMover([self.shooter, self.pathrev])
def test_successful_move(self): init_innermost = make_1d_traj(self.list_innermost, [1.0]*5) init_sample = Sample( replica=0, trajectory=init_innermost, ensemble=self.innermost ) gs = SampleSet([init_sample, self.minus_sample]) extend_forward = self.list_innermost + [0.12, 0.32, -0.131] extend_backward = [-0.13, 0.13, 0.33] + self.list_innermost assert_equal(self.minus(make_1d_traj(extend_forward)), True) assert_equal(self.minus(make_1d_traj(extend_backward)), True) seg_dir = {} for i in range(100): change = self.mover.move(gs) samples = change.results sub_samples = change.subchange.subchange.results assert_equal(len(samples), 2) assert_equal(len(sub_samples), 4) s_inner = [s for s in sub_samples if s.ensemble==self.innermost] s_minus = [s for s in sub_samples if s.ensemble==self.minus] s_sub = [s for s in sub_samples if s.ensemble==self.minus._segment_ensemble] assert_equal(len(s_inner), 1) assert_equal(len(s_minus), 1) assert_equal(len(s_sub), 2) for c in change: assert_equal(c.accepted, True) assert_equal(change.canonical.mover, self.mover) key = "" s_inner0_xvals = [s.coordinates[0,0] for s in s_inner[0].trajectory] if items_equal(s_inner0_xvals, self.first_segment): key += "1" elif items_equal(s_inner0_xvals, self.second_segment): key += "2" else: print "s_inner0_xvals:", s_inner0_xvals raise RuntimeError("Chosen segment neither first nor last!") # final sample s_minus is accepted s_minus_xvals = [s.coordinates[0,0] for s in s_minus[-1].trajectory] if items_equal(s_minus_xvals, extend_forward): key += "f" elif items_equal(s_minus_xvals, extend_backward): key += "b" else: print "s_minus_xvals:", s_minus_xvals raise RuntimeError("Unexpected minus extension result!") try: seg_dir[key] += 1 except KeyError: seg_dir[key] = 1 assert_equal(len(seg_dir.keys()), 4)
def test_first_traj_ensemble(self): traj_starts_in = make_1d_traj([-0.2, -0.1, 0.1, -0.1]) traj_starts_out = make_1d_traj([0.1, -0.1, 0.1, -0.1]) traj_not_good = make_1d_traj([0.1, -0.1, 0.1]) first_traj_ens = self.noforbid_noextra_AB.first_traj_ensemble assert_equal(first_traj_ens(traj_starts_in), True) assert_equal(first_traj_ens(traj_starts_out), True) assert_equal(first_traj_ens(traj_not_good), False)
def setup(self): xval = paths.CV_Function(name="xA", f=lambda s: s.xyz[0][0]) self.stateA = paths.CVRangeVolume(xval, float("-inf"), -0.5) self.stateB = paths.CVRangeVolume(xval, -0.1, 0.1) self.stateC = paths.CVRangeVolume(xval, 0.5, float("inf")) ifacesA = vf.CVRangeVolumeSet(xval, float("-inf"), [-0.5, -0.4, -0.3]) ifacesB = vf.CVRangeVolumeSet(xval, [-0.2, -0.15, -0.1], [0.2, 0.15, 0.1]) ifacesC = vf.CVRangeVolumeSet(xval, [0.5, 0.4, 0.3], float("inf")) self.traj = {} self.traj['AA'] = make_1d_traj(coordinates=[-0.51, -0.49, -0.52], velocities=[1.0] * 3) self.traj['AB'] = make_1d_traj(coordinates=[-0.51, -0.25, 0.0], velocities=[1.0] * 3) self.traj['BA'] = make_1d_traj(coordinates=[0.0, -0.15, -0.35, -0.52], velocities=[-1.0] * 4) self.traj['BB'] = make_1d_traj(coordinates=[0.0, -0.25, 0.25, 0.02], velocities=[1.0] * 4) self.traj['BC'] = make_1d_traj(coordinates=[0.01, 0.16, 0.25, 0.53], velocities=[1.0] * 4) self.traj['CB'] = make_1d_traj(coordinates=[0.52, 0.25, -0.01], velocities=[-1.0] * 3) self.traj['CC'] = make_1d_traj(coordinates=[0.51, 0.35, 0.55], velocities=[1.0] * 3) # A->C magically jumps over B self.traj['AC'] = make_1d_traj(coordinates=[-0.51, -0.25, 0.25, 0.51], velocities=[1.0] * 4) self.traj['CA'] = make_1d_traj(coordinates=[0.52, 0.22, -0.22, -0.52], velocities=[1.0] * 4) self.mstis = MSTISNetwork([(self.stateA, ifacesA, xval), (self.stateB, ifacesB, xval), (self.stateC, ifacesC, xval)])
def setup(self): id_cv = paths.FunctionCV("Id", lambda snap: snap.xyz[0][0]) sin_cv = paths.FunctionCV("sin", lambda snap: np.sin(snap.xyz[0][0])) square_cv = paths.FunctionCV("x^2", lambda snap: snap.xyz[0][0]**2) self.cvs = [id_cv, sin_cv, square_cv] self.left_bin_edges = (0, 0, 0) self.bin_widths = (0.25, 0.5, 0.4) self.traj1 = make_1d_traj([0.1, 0.51, 0.61]) # [(0, 0, 0), (2, 0, 0), (2, 1, 0)] # interpolate: (1, 0, 0) self.traj2 = make_1d_traj([0.6, 0.7])
def setup(self): xval = paths.CV_Function(name="xA", fcn=lambda s : s.xyz[0][0]) self.stateA = paths.CVRangeVolume(xval, float("-inf"), -0.5) self.stateB = paths.CVRangeVolume(xval, -0.1, 0.1) self.stateC = paths.CVRangeVolume(xval, 0.5, float("inf")) ifacesA = vf.CVRangeVolumeSet(xval, float("-inf"), [-0.5, -0.4, -0.3]) ifacesB = vf.CVRangeVolumeSet(xval, [-0.2, -0.15, -0.1], [0.2, 0.15, 0.1]) ifacesC = vf.CVRangeVolumeSet(xval, [0.5, 0.4, 0.3], float("inf")) self.traj = {} self.traj['AA'] = make_1d_traj( coordinates=[-0.51, -0.49, -0.52], velocities=[1.0]*3 ) self.traj['AB'] = make_1d_traj( coordinates=[-0.51, -0.25, 0.0], velocities=[1.0]*3 ) self.traj['BA'] = make_1d_traj( coordinates=[0.0, -0.15, -0.35, -0.52], velocities=[-1.0]*4 ) self.traj['BB'] = make_1d_traj( coordinates=[0.0, -0.25, 0.25, 0.02], velocities=[1.0]*4 ) self.traj['BC'] = make_1d_traj( coordinates=[0.01, 0.16, 0.25, 0.53], velocities=[1.0]*4 ) self.traj['CB'] = make_1d_traj( coordinates=[0.52, 0.25, -0.01], velocities=[-1.0]*3 ) self.traj['CC'] = make_1d_traj( coordinates=[0.51, 0.35, 0.55], velocities=[1.0]*3 ) # A->C magically jumps over B self.traj['AC'] = make_1d_traj( coordinates=[-0.51, -0.25, 0.25, 0.51], velocities=[1.0]*4 ) self.traj['CA'] = make_1d_traj( coordinates=[0.52, 0.22, -0.22, -0.52], velocities=[1.0]*4 ) self.mstis = MSTISNetwork([ (self.stateA, ifacesA, "A", xval), (self.stateB, ifacesB, "B", xval), (self.stateC, ifacesC, "C", xval) ])
def setup(self): id_cv = paths.FunctionCV("Id", lambda snap : snap.xyz[0][0]) sin_cv = paths.FunctionCV("sin", lambda snap : np.sin(snap.xyz[0][0])) square_cv = paths.FunctionCV("x^2", lambda snap : snap.xyz[0][0]**2) self.cvs = [id_cv, sin_cv, square_cv] self.left_bin_edges = (0,0,0) self.bin_widths = (0.25, 0.5, 0.4) self.traj1 = make_1d_traj([0.1, 0.51, 0.61]) # [(0, 0, 0), (2, 0, 0), (2, 1, 0)] # interpolate: (1, 0, 0) self.traj2 = make_1d_traj([0.6, 0.7])
def test_extension_fails(self): innermost_bad_extension = [-0.25, 0.1, 0.5, 0.1, -0.25] traj_bad_extension = make_1d_traj(innermost_bad_extension, [1.0]*5) samp_bad_extension = Sample( replica=0, trajectory=traj_bad_extension, ensemble=self.innermost ) assert_equal(self.innermost(traj_bad_extension), True) gs = SampleSet([self.minus_sample, samp_bad_extension]) change = self.mover.move(gs) assert_equal(change.accepted, False) # whole minus has failed sub = change.subchange.subchange assert_equal(len(sub.trials), 4) # after filtering there are only 2 trials assert_equal(len(change.trials), 2) assert_subchanges_set_accepted(sub, [True] * 2 + [False]) # first two work and the extention fails # this only happens due to length assert_equal( len(sub[-1][0].trials[0].trajectory), len(traj_bad_extension)+self.dyn.n_frames_max-1 )
def test_sampling_ensembles(self): traj1 = make_1d_traj([-0.2, -0.1, 0.1, -0.1]) traj2 = make_1d_traj([-0.1, 0.1, -0.1]) traj3 = make_1d_traj([-0.1, 0.1, 0.3, -0.1]) traj4 = make_1d_traj([0.1, 0.3, 0.1]) all_ensembles = self.noforbid_noextra_AB.all_ensembles assert_equal(len(all_ensembles), 3) for ens in all_ensembles: assert_equal(ens(traj1), False) assert_equal(ens(traj4), False) assert_equal(all_ensembles[0](traj2), True) assert_equal(all_ensembles[0](traj3), True) assert_equal(all_ensembles[1](traj2), False) assert_equal(all_ensembles[1](traj3), True) assert_equal(all_ensembles[2](traj2), False) assert_equal(all_ensembles[2](traj3), False)
def _make_traj(self, traj_str): sequence = [] char_to_parameters = { 'a': { 'lo': 0.1, 'hi': 0.5 }, 'b': { 'lo': 2.0, 'hi': 2.5 }, 'i': { 'lo': 0.5, 'hi': 0.7 }, 'x': { 'lo': 0.7, 'hi': 2.0 } } delta = 0.05 for char in traj_str: params = char_to_parameters[char] n_max = int((params['hi'] - params['lo']) / delta) sequence.append(params['lo'] + delta * random.randint(1, n_max - 1)) return make_1d_traj(coordinates=sequence, velocities=[1.0] * len(sequence))
def test_extension_fails(self): innermost_bad_extension = [-0.25, 0.1, 0.5, 0.1, -0.25] traj_bad_extension = make_1d_traj(innermost_bad_extension, [1.0]*5) samp_bad_extension = Sample( replica=0, trajectory=traj_bad_extension, ensemble=self.innermost ) assert_equal(self.innermost(traj_bad_extension), True) gs = SampleSet([self.minus_sample, samp_bad_extension]) change = self.mover.move(gs) assert_equal(change.accepted, False) # whole minus has failed # Minus : Filter :ChooseFB : CondSeq sub = change.subchange.subchange.subchange assert_equal(len(sub.trials), 2) assert_equal(len(change.trials), 0) # no trials survive filtering assert_subchanges_set_accepted(sub, [True, False]) # first two work and the extention fails # this only happens due to length assert_equal( len(sub[-1].trials[0].trajectory), len(traj_bad_extension)+self.dyn.n_frames_max-1 )
def setup(self): self.cv = paths.FunctionCV("Id", lambda snap: snap.xyz[0][0]) cv_neg = paths.FunctionCV("Neg", lambda snap: -snap.xyz[0][0]) self.stateA = paths.CVDefinedVolume(self.cv, -1.0, 0.0) self.stateB = paths.CVDefinedVolume(self.cv, 1.0, 2.0) self.stateC = paths.CVDefinedVolume(self.cv, 3.0, 4.0) interfacesAB = paths.VolumeInterfaceSet( self.cv, -1.0, [0.0, 0.2, 0.4] ) interfacesBC = paths.VolumeInterfaceSet( self.cv, 1.0, [2.0, 2.2, 2.4] ) interfacesBA = paths.VolumeInterfaceSet( cv_neg, -1.0, [-1.0, -0.8, -0.6] ) network = paths.MISTISNetwork([ (self.stateA, interfacesAB, self.stateB), (self.stateB, interfacesBC, self.stateC), (self.stateB, interfacesBA, self.stateA) ]) self.tisAB = network.input_transitions[(self.stateA, self.stateB)] self.tisBC = network.input_transitions[(self.stateB, self.stateC)] self.tisBA = network.input_transitions[(self.stateB, self.stateA)] self.network = network self.snapA = make_1d_traj([-0.5])[0] self.noforbid_noextra_AB = paths.FullBootstrapping( transition=self.tisAB, snapshot=self.snapA )
def test_AB_path(self): trajAXB = make_1d_traj(coordinates=[-0.2, 0.75, 1.8]) sampAXB = Sample(trajectory=trajAXB, ensemble=self.tis, replica=0) gs_AXB = SampleSet([sampAXB]) change = self.move.move(gs_AXB) assert_equal(change.accepted, False)
def test_A_A_path(self): trajA_A = make_1d_traj(coordinates=[-0.3, 0.1, -0.4]) sampA_A = Sample(trajectory=trajA_A, ensemble=self.tis, replica=0) gs_A_A = SampleSet([sampA_A]) change = self.move.move(gs_A_A) assert_equal(change.accepted, False)
def test_assert_initial_conditions(self): scheme = DefaultScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) init_cond = scheme.initial_conditions_from_trajectories(traj3) init_cond.append_as_new_replica( paths.Sample(trajectory=traj3, ensemble=paths.LengthEnsemble(4), replica=None)) scheme.assert_initial_conditions(init_cond)
def test_check_initial_conditions(self): scheme = OneWayShootingMoveScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) init_cond = scheme.initial_conditions_from_trajectories(traj3) assert_equal(len(scheme.list_initial_ensembles()), 6) assert_equal(len(init_cond), 6) scheme.assert_initial_conditions(init_cond) assert_equal(scheme.initial_conditions_report(init_cond), "No missing ensembles.\nNo extra ensembles.\n")
def setup(self): op = CV_Function("myid", f=lambda snap : snap.coordinates[0][0]) state1 = CVRangeVolume(op, -100, 0.0) state2 = CVRangeVolume(op, 1, 100) volA = CVRangeVolume(op, -100, 0.25) volB = CVRangeVolume(op, -100, 0.50) self.tisA = paths.TISEnsemble(state1, state2, volA) self.tisB = paths.TISEnsemble(state1, state2, volB) self.traj0 = make_1d_traj([-0.1, 0.2, 0.3, 0.1, -0.2]) self.traj1 = make_1d_traj([-0.1, 0.1, 0.4, 0.6, 0.3, 0.2, -0.15]) self.traj2 = make_1d_traj([-0.1, 0.2, 0.3, 0.7, 0.6, 0.4, 0.1, -0.15]) self.sampA0 = Sample(replica=0, trajectory=self.traj0, ensemble=self.tisA) self.sampB1 = Sample(replica=1, trajectory=self.traj1, ensemble=self.tisB) self.sampA2 = Sample(replica=2, trajectory=self.traj2, ensemble=self.tisA) self.gs_B1A2 = SampleSet([self.sampB1, self.sampA2]) self.gs_A0B1 = SampleSet([self.sampA0, self.sampB1])
def test_BA_path(self): trajBXA = make_1d_traj(coordinates=[1.2, 0.7, -0.25]) sampBXA = Sample(trajectory=trajBXA, ensemble=self.tis, replica=0) gs_BXA = SampleSet([sampBXA]) change = self.move.move(gs_BXA) # print [[v.coordinates[0] for v in t.trajectory] for t in change.trials] assert_equal(change.accepted, True)
def setup(self): op = CV_Function("myid", fcn=lambda snap : snap.coordinates[0][0]) state1 = CVRangeVolume(op, -100, 0.0) state2 = CVRangeVolume(op, 1, 100) volA = CVRangeVolume(op, -100, 0.25) volB = CVRangeVolume(op, -100, 0.50) self.tisA = paths.TISEnsemble(state1, state2, volA) self.tisB = paths.TISEnsemble(state1, state2, volB) self.traj0 = make_1d_traj([-0.1, 0.2, 0.3, 0.1, -0.2]) self.traj1 = make_1d_traj([-0.1, 0.1, 0.4, 0.6, 0.3, 0.2, -0.15]) self.traj2 = make_1d_traj([-0.1, 0.2, 0.3, 0.7, 0.6, 0.4, 0.1, -0.15]) self.sampA0 = Sample(replica=0, trajectory=self.traj0, ensemble=self.tisA) self.sampB1 = Sample(replica=1, trajectory=self.traj1, ensemble=self.tisB) self.sampA2 = Sample(replica=2, trajectory=self.traj2, ensemble=self.tisA) self.gs_B1A2 = SampleSet([self.sampB1, self.sampA2]) self.gs_A0B1 = SampleSet([self.sampA0, self.sampB1])
def test_AXA_path(self): trajAXA = make_1d_traj(coordinates=[-0.1, 0.75, -0.6], velocities=[0.1, 0.05, -0.05]) assert_equal(self.tis(trajAXA), True) sampAXA = Sample(trajectory=trajAXA, ensemble=self.tis, replica=0) gs_AXA = SampleSet([sampAXA]) change = self.move.move(gs_AXA) assert_equal(change.accepted, True)
def test_assert_initial_conditions(self): scheme = DefaultScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) init_cond = scheme.initial_conditions_from_trajectories(traj3) init_cond.append_as_new_replica( paths.Sample(trajectory=traj3, ensemble=paths.LengthEnsemble(4), replica=None) ) scheme.assert_initial_conditions(init_cond)
def test_flux_from_calvinist_dynamics(self): # To check for the multiple interface set case, we need to have two # dimensions. We can hack two "independent" dimensions from a one # dimensional system by making the second CV non-monotonic with the # first. For the full trajectory, we need snapshots `S` (in the # state); `I` (interstitial: outside the state, but not outside # either interface); `X_a` (outside interface alpha, not outside # interface beta); `X_b` (outside interface beta, not outside # interface alpha); and `X_ab` (outside interface alpha and beta). cv1 = self.cv cv2 = paths.FunctionCV("abs_sin", lambda snap : np.abs(np.sin(snap.xyz[0][0]))) state = paths.CVDefinedVolume(cv1, -np.pi/8.0, np.pi/8.0) other_state = paths.CVDefinedVolume(cv1, -5.0/8.0*np.pi, -3.0/8.0*np.pi) alpha = paths.CVDefinedVolume(cv1, float("-inf"), 3.0/8.0*np.pi) beta = paths.CVDefinedVolume(cv2, float("-inf"), np.sqrt(2)/2.0) # approx alpha: x < 1.17 beta: abs(sin(x)) < 0.70 S = 0 # cv1 = 0.00; cv2 = 0.00 I = np.pi/5.0 # cv1 = 0.63; cv2 = 0.59 X_a = np.pi # cv1 = 3.14; cv2 = 0.00 X_b = -np.pi/3.0 # cv1 = -1.05; cv2 = 0.87 X_ab = np.pi/2.0 # cv1 = 1.57; cv2 = 1.00 other = -np.pi/2.0 # cv1 = -1.57; cv2 = 1.00 # That hack is utterly crazy, but I'm kinda proud of it! predetermined = [S, S, I, X_a, # (2) first exit S, X_a, # (4) cross A S, X_ab, # (6) cross A & B I, S, X_b, # (9) cross B S, I, X_b, # (12) cross B other, I, X_b, # (15) cross to other state S, X_b, # (17) first cross B S, X_a, # (19) first cross A S, S, X_ab, # (22) cross A & B I, X_ab, # (24) recrossing test S, I, # (26) false crossing test S, S] engine = CalvinistDynamics(predetermined) init = make_1d_traj([S]) sim = DirectSimulation(storage=None, engine=engine, states=[state, other_state], flux_pairs=[(state, alpha), (state, beta)], initial_snapshot=init[0]) sim.run(len(predetermined)-1) # subtract 1 from the indices in `predetermined`, b/c 0 index of the # traj comes after the found initial step expected_flux_events = { (state, alpha): [(4, 2), (6, 4), (22, 19)], (state, beta): [(9, 6), (12, 9), (22, 17)] } assert_equal(len(sim.flux_events), 2) assert_equal(sim.flux_events[(state, alpha)], expected_flux_events[(state, alpha)]) assert_equal(sim.flux_events[(state, beta)], expected_flux_events[(state, beta)])
def test_initial_conditions_report(self): scheme = DefaultScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) init_cond = scheme.initial_conditions_from_trajectories(traj3) init_cond.append_as_new_replica( paths.Sample(trajectory=traj3, ensemble=paths.LengthEnsemble(4), replica=None) ) expected = "Missing ensembles:\n" expected += "* [[MinusInterfaceEnsemble]]\n"*2 expected += "Extra ensembles:\n* [LengthEnsemble]\n" assert_equal(scheme.initial_conditions_report(init_cond), expected)
def setup(self): op = paths.FunctionCV("Id", lambda snap: snap.coordinates[0][0]) self.vol1 = paths.CVDefinedVolume(op, 0.1, 0.5) self.vol3 = paths.CVDefinedVolume(op, 2.0, 2.5) self.trajectory = make_1d_traj(coordinates=[ 0.2, 0.3, 0.6, 2.1, 2.2, 0.7, 0.4, 0.35, 2.4, 0.33, 0.32, 0.31 ], velocities=[0.0] * 12) all_in_1 = paths.AllInXEnsemble(self.vol1) self.segments = all_in_1.split(self.trajectory) self.container = paths.TrajectorySegmentContainer(self.segments, dt=0.5)
def setup(self): op = paths.FunctionCV("Id", lambda snap : snap.coordinates[0][0]) self.vol1 = paths.CVDefinedVolume(op, 0.1, 0.5) self.vol3 = paths.CVDefinedVolume(op, 2.0, 2.5) self.trajectory = make_1d_traj(coordinates=[0.2, 0.3, 0.6, 2.1, 2.2, 0.7, 0.4, 0.35, 2.4, 0.33, 0.32, 0.31], velocities=[0.0]*12) all_in_1 = paths.AllInXEnsemble(self.vol1) self.segments = all_in_1.split(self.trajectory) self.container = paths.TrajectorySegmentContainer(self.segments, dt=0.5)
def test_repex_fails_minus_crosses_to_state(self): minus_crosses_to_state = make_1d_traj( [-0.11, 0.5, 1.8, 0.6, -0.12, 0.7, 1.7, 0.4, -0.13] ) badminus_sample = Sample( replica=-1, trajectory=minus_crosses_to_state, ensemble=self.minus ) init_sample = Sample( replica=0, trajectory=make_1d_traj(self.list_innermost, [1.0]*5), ensemble=self.innermost ) gs = SampleSet([badminus_sample, init_sample]) assert_equal(self.minus(minus_crosses_to_state), True) change = self.mover.move(gs) sub = change.subchange.subchange assert_equal(len(sub.trials), 3) # stop after failed repex assert_equal(len(change.trials), 1) assert_subchanges_set_accepted(sub, [True, False, False])
def test_first_hop_fails(self): crossing_traj = make_1d_traj([-0.11, 0.11, 0.31, 1.01], [1.0]*4) crossing_samp = Sample(replica=0, trajectory=crossing_traj, ensemble=self.innermost) gs = SampleSet([crossing_samp]) gs.sanity_check() change = self.mover.move(gs) assert_equal(change.accepted, False) assert_equal(len(change.results), 0) sub_trials = change.subchange.subchange.subchange.trials assert_equal(len(sub_trials), 1) assert_equal(sub_trials[0].trajectory, crossing_traj) assert_equal(sub_trials[0].ensemble, self.minus._segment_ensemble)
def _make_traj(self, traj_str): sequence = [] char_to_parameters = { 'a' : {'lo' : 0.1, 'hi' : 0.5}, 'b' : {'lo' : 2.0, 'hi' : 2.5}, 'i' : {'lo' : 0.5, 'hi' : 0.7}, 'x' : {'lo' : 0.7, 'hi' : 2.0} } delta = 0.05 for char in traj_str: params = char_to_parameters[char] n_max = int((params['hi'] - params['lo'])/delta) sequence.append(params['lo'] + delta*random.randint(1, n_max-1)) return make_1d_traj(coordinates=sequence, velocities=[1.0]*len(sequence))
def test_repex_fails_innermost_crosses_state(self): innermost_crosses_to_state = make_1d_traj([-0.11, 0.5, 1.8]) samp_crosses_to_state = Sample( replica=0, trajectory=innermost_crosses_to_state, ensemble=self.innermost ) gs = SampleSet([samp_crosses_to_state, self.minus_sample]) change = self.mover.move(gs) assert_equal(len(change.trials), 1) # stop after failed repex sub = change.subchange.subchange assert_equal(self.innermost(innermost_crosses_to_state), True) assert_equal(len(sub.trials), 3) # stop after failed repex assert_subchanges_set_accepted(sub, [True, False, False])
def test_repex_fails_other_ensemble(self): innermost_other_ensemble = make_1d_traj([-0.11, 0.1, -0.12]) samp_other_ensemble = Sample( replica=0, trajectory=innermost_other_ensemble, ensemble=self.innermost ) gs = SampleSet([samp_other_ensemble, self.minus_sample]) change = self.mover.move(gs) assert_equal(len(change.trials), 1) sub = change.subchange.subchange assert_equal(self.innermost(innermost_other_ensemble), False) assert_equal(sub[0].accepted, True) assert_equal(sub[1].accepted, False) assert_equal(len(sub.trials), 3) # stop after failed repex
def setup(self): from test_helpers import CallIdentity xval = paths.FunctionCV("xval", lambda snap: snap.xyz[0][0]) self.stateA = paths.CVDefinedVolume(xval, float("-inf"), -0.5) self.stateB = paths.CVDefinedVolume(xval, -0.1, 0.1) self.stateC = paths.CVDefinedVolume(xval, 0.5, float("inf")) self.states = [self.stateA, self.stateB, self.stateC] self.traj = {} self.traj['AA'] = make_1d_traj([-0.51, -0.49, -0.49, -0.52]) self.traj['AB'] = make_1d_traj([-0.51, -0.25, -0.25, 0.0]) self.traj['BA'] = make_1d_traj([0.0, -0.15, -0.35, -0.52]) self.traj['BB'] = make_1d_traj([0.0, -0.25, 0.25, 0.02]) self.traj['BC'] = make_1d_traj([0.01, 0.16, 0.25, 0.53]) self.traj['CC'] = make_1d_traj([0.51, 0.35, 0.36, 0.55]) self.traj['CA'] = make_1d_traj([0.52, 0.22, -0.22, -0.52])
def test_subtrajectory_indices(self): # simplify more complicated expressions stateA = self.stateA stateB = self.stateB pretraj = [ 0.20, 0.30, 0.60, 0.40, 0.65, 2.10, 2.20, 2.60, 2.10, 0.80, 0.55, 0.40, 0.20 ] # 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12 # A, A, I, A, I, B, B, X, B, X, I, A, A trajectory = make_1d_traj(coordinates=pretraj, velocities=[1.0] * len(pretraj)) ensemble_A = paths.AllInXEnsemble(stateA) ensemble_B = paths.AllInXEnsemble(stateB) ensemble_ABA = paths.SequentialEnsemble([ paths.AllInXEnsemble(stateA) & paths.LengthEnsemble(1), paths.PartInXEnsemble(stateB) & paths.AllOutXEnsemble(stateA), paths.AllInXEnsemble(stateA) & paths.LengthEnsemble(1) ]) subtrajectoriesA = ensemble_A.split(trajectory, overlap=0) subtrajectoriesB = ensemble_B.split(trajectory, overlap=0) subtrajectoriesABA = ensemble_ABA.split(trajectory) # make sure we have the trajectories we expect assert_equal(len(subtrajectoriesA), 3) assert_equal(len(subtrajectoriesB), 2) assert_equal(len(subtrajectoriesABA), 1) # the following assertions check that the subtrajectories are the # ones that we expect; the numbers here are linked to the indices # we'll test next assert_equal(subtrajectoriesA[0], trajectory[0:2]) assert_equal(subtrajectoriesA[1], trajectory[3:4]) assert_equal(subtrajectoriesA[2], trajectory[11:13]) assert_equal(subtrajectoriesB[0], trajectory[5:7]) assert_equal(subtrajectoriesB[1], trajectory[8:9]) assert_equal(subtrajectoriesABA[0], trajectory[3:12]) # now we run the subtrajectory_indices function and test it indicesA = trajectory.subtrajectory_indices(subtrajectoriesA) indicesB = trajectory.subtrajectory_indices(subtrajectoriesB) indicesABA = trajectory.subtrajectory_indices(subtrajectoriesABA) assert_equal(indicesA, [[0, 1], [3], [11, 12]]) assert_equal(indicesB, [[5, 6], [8]]) assert_equal(indicesABA, [[3, 4, 5, 6, 7, 8, 9, 10, 11]])
def test_check_initial_conditions(self): scheme = DefaultScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) # cheating a bit, since we know what this gives init_cond = scheme.initial_conditions_from_trajectories(traj3) assert_equal(len(init_cond), 7) assert_equal(len(scheme.list_initial_ensembles()), 9) (missing, extra) = scheme.check_initial_conditions(init_cond) assert_equal(len(missing), 2) assert_equal(len(extra), 0) for ens in self.network.special_ensembles['minus'].keys(): assert_in([ens], missing) init_cond.append_as_new_replica( paths.Sample(trajectory=traj3, ensemble=paths.LengthEnsemble(4), replica=None)) (missing, extra) = scheme.check_initial_conditions(init_cond) assert_equal(len(missing), 2) assert_equal(len(extra), 1)
def test_initial_conditions_report(self): scheme = DefaultScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) init_cond = scheme.initial_conditions_from_trajectories(traj3) init_cond.append_as_new_replica( paths.Sample(trajectory=traj3, ensemble=paths.LengthEnsemble(4), replica=None)) start = "Missing ensembles:\n" missing_A = "* [Out A minus]\n" missing_B = "* [Out B minus]\n" finish = "Extra ensembles:\n* [LengthEnsemble]\n" expected_AB = start + missing_A + missing_B + finish expected_BA = start + missing_B + missing_A + finish result = scheme.initial_conditions_report(init_cond) try: assert_equal(result, expected_AB) except AssertionError: assert_equal(result, expected_BA)
def setup(self): self.dyn = CalvinistDynamics([-0.1, 0.1, 0.3, 0.5, 0.7, -0.1, 0.2, 0.4, 0.6, 0.8, ]) SampleMover.engine = self.dyn op = CV_Function("myid", f=lambda snap : snap.coordinates[0][0]) stateA = CVRangeVolume(op, -100, 0.0) stateB = CVRangeVolume(op, 0.65, 100) self.tps = ef.A2BEnsemble(stateA, stateB) init_traj = make_1d_traj( coordinates=[-0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], velocities=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ) self.init_samp = SampleSet([Sample( trajectory=init_traj, replica=0, ensemble=self.tps )])
def setup(self): # create the network xval = paths.FunctionCV(name="xA", f=lambda s: s.xyz[0][0]) self.stateA = paths.CVDefinedVolume(xval, -1.0, -0.5).named("A") self.stateB = paths.CVDefinedVolume(xval, 0.5, float("inf")).named("B") ifacesA = paths.VolumeInterfaceSet(xval, float(-1.0), [-0.5, -0.4, -0.3, -0.2]) self.network = paths.MISTISNetwork([(self.stateA, ifacesA, self.stateB) ]) transition = self.network.transitions[(self.stateA, self.stateB)] ensembles = transition.ensembles self.xval = xval self.ifacesA = ifacesA # create the biases bias_table = {} bias_table[ensembles[0]] = 1.0 bias_table[ensembles[1]] = 0.5 bias_table[ensembles[2]] = 0.2 self.bias = BiasEnsembleTable.ratios_from_dictionary(bias_table) # samples, moves, changes traj = make_1d_traj( [-0.55, -0.45, -0.35, -0.25, -0.15, -0.26, -0.36, -0.46, -0.56]) s0 = paths.Sample(replica=0, ensemble=ensembles[0], trajectory=traj) s1 = paths.Sample(replica=1, ensemble=ensembles[1], trajectory=traj) s2 = paths.Sample(replica=2, ensemble=ensembles[2], trajectory=traj) self.sample_set = paths.SampleSet([s0, s1, s2]) self.sample_set.sanity_check() move_01 = paths.EnsembleHopMover(ensembles[0], ensembles[1]) move_02 = paths.EnsembleHopMover(ensembles[0], ensembles[2]) move_12 = paths.EnsembleHopMover(ensembles[1], ensembles[2]) move_21 = paths.EnsembleHopMover(ensembles[2], ensembles[1]) move_20 = paths.EnsembleHopMover(ensembles[2], ensembles[0]) move_10 = paths.EnsembleHopMover(ensembles[1], ensembles[0]) # NOTE: all changes here are accepted self.change_01 = move_01.move(self.sample_set) self.change_02 = move_02.move(self.sample_set) self.change_12 = move_12.move(self.sample_set) self.change_21 = move_21.move(self.sample_set) self.change_20 = move_20.move(self.sample_set) self.change_10 = move_10.move(self.sample_set) # convenience lists for changes going outward vs. inward self.out_changes = [self.change_01, self.change_02, self.change_12] self.in_changes = [self.change_10, self.change_20, self.change_21]
def test_subtrajectory_indices(self): # simplify more complicated expressions stateA = self.stateA stateB = self.stateB pretraj = [0.20, 0.30, 0.60, 0.40, 0.65, 2.10, 2.20, 2.60, 2.10, 0.80, 0.55, 0.40, 0.20] # 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12 # A, A, I, A, I, B, B, X, B, X, I, A, A trajectory = make_1d_traj( coordinates=pretraj, velocities=[1.0]*len(pretraj) ) ensemble_A = paths.AllInXEnsemble(stateA) ensemble_B = paths.AllInXEnsemble(stateB) ensemble_ABA = paths.SequentialEnsemble([ paths.AllInXEnsemble(stateA) & paths.LengthEnsemble(1), paths.PartInXEnsemble(stateB) & paths.AllOutXEnsemble(stateA), paths.AllInXEnsemble(stateA) & paths.LengthEnsemble(1) ]) subtrajectoriesA = ensemble_A.split(trajectory, overlap=0) subtrajectoriesB = ensemble_B.split(trajectory, overlap=0) subtrajectoriesABA = ensemble_ABA.split(trajectory) # make sure we have the trajectories we expect assert_equal(len(subtrajectoriesA), 3) assert_equal(len(subtrajectoriesB), 2) assert_equal(len(subtrajectoriesABA), 1) # the following assertions check that the subtrajectories are the # ones that we expect; the numbers here are linked to the indices # we'll test next assert_equal(subtrajectoriesA[0], trajectory[0:2]) assert_equal(subtrajectoriesA[1], trajectory[3:4]) assert_equal(subtrajectoriesA[2], trajectory[11:13]) assert_equal(subtrajectoriesB[0], trajectory[5:7]) assert_equal(subtrajectoriesB[1], trajectory[8:9]) assert_equal(subtrajectoriesABA[0], trajectory[3:12]) # now we run the subtrajectory_indices function and test it indicesA = trajectory.subtrajectory_indices(subtrajectoriesA) indicesB = trajectory.subtrajectory_indices(subtrajectoriesB) indicesABA = trajectory.subtrajectory_indices(subtrajectoriesABA) assert_equal(indicesA, [[0, 1], [3], [11, 12]]) assert_equal(indicesB, [[5, 6], [8]]) assert_equal(indicesABA, [[3, 4, 5, 6, 7, 8, 9, 10, 11]])
def setup(self): self.dyn = CalvinistDynamics([-0.1, 0.1, 0.3, 0.5, 0.7, -0.1, 0.2, 0.4, 0.6, 0.8, ]) SampleGeneratingMover.engine = self.dyn op = CV_Function("myid", fcn=lambda snap : snap.coordinates[0][0]) stateA = CVRangeVolume(op, -100, 0.0) stateB = CVRangeVolume(op, 0.65, 100) self.tps = ef.A2BEnsemble(stateA, stateB) init_traj = make_1d_traj( coordinates=[-0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], velocities=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] ) self.init_samp = SampleSet([Sample( trajectory=init_traj, replica=0, ensemble=self.tps )])
def test_check_initial_conditions(self): scheme = DefaultScheme(self.network) traj3 = make_1d_traj([-0.6, -0.2, 0.2, 0.6]) # cheating a bit, since we know what this gives init_cond = scheme.initial_conditions_from_trajectories(traj3) assert_equal(len(init_cond), 7) assert_equal(len(scheme.list_initial_ensembles()), 9) (missing, extra) = scheme.check_initial_conditions(init_cond) assert_equal(len(missing), 2) assert_equal(len(extra), 0) for ens in self.network.special_ensembles['minus'].keys(): assert_in([ens], missing) init_cond.append_as_new_replica( paths.Sample(trajectory=traj3, ensemble=paths.LengthEnsemble(4), replica=None) ) (missing, extra) = scheme.check_initial_conditions(init_cond) assert_equal(len(missing), 2) assert_equal(len(extra), 1)
def setup(self): # create the network xval = paths.FunctionCV(name="xA", f=lambda s: s.xyz[0][0]) self.stateA = paths.CVDefinedVolume(xval, -1.0, -0.5).named("A") self.stateB = paths.CVDefinedVolume(xval, 0.5, float("inf")).named("B") ifacesA = paths.VolumeInterfaceSet(xval, float(-1.0), [-0.5, -0.4, -0.3, -0.2]) self.network = paths.MISTISNetwork([(self.stateA, ifacesA, self.stateB)]) transition = self.network.transitions[(self.stateA, self.stateB)] ensembles = transition.ensembles self.xval = xval self.ifacesA = ifacesA # create the biases bias_table = {} bias_table[ensembles[0]] = 1.0 bias_table[ensembles[1]] = 0.5 bias_table[ensembles[2]] = 0.2 self.bias = BiasEnsembleTable.ratios_from_dictionary(bias_table) # samples, moves, changes traj = make_1d_traj([-0.55, -0.45, -0.35, -0.25, -0.15, -0.26, -0.36, -0.46, -0.56]) s0 = paths.Sample(replica=0, ensemble=ensembles[0], trajectory=traj) s1 = paths.Sample(replica=1, ensemble=ensembles[1], trajectory=traj) s2 = paths.Sample(replica=2, ensemble=ensembles[2], trajectory=traj) self.sample_set = paths.SampleSet([s0, s1, s2]) self.sample_set.sanity_check() move_01 = paths.EnsembleHopMover(ensembles[0], ensembles[1]) move_02 = paths.EnsembleHopMover(ensembles[0], ensembles[2]) move_12 = paths.EnsembleHopMover(ensembles[1], ensembles[2]) move_21 = paths.EnsembleHopMover(ensembles[2], ensembles[1]) move_20 = paths.EnsembleHopMover(ensembles[2], ensembles[0]) move_10 = paths.EnsembleHopMover(ensembles[1], ensembles[0]) # NOTE: all changes here are accepted self.change_01 = move_01.move(self.sample_set) self.change_02 = move_02.move(self.sample_set) self.change_12 = move_12.move(self.sample_set) self.change_21 = move_21.move(self.sample_set) self.change_20 = move_20.move(self.sample_set) self.change_10 = move_10.move(self.sample_set) # convenience lists for changes going outward vs. inward self.out_changes = [self.change_01, self.change_02, self.change_12] self.in_changes = [self.change_10, self.change_20, self.change_21]
def test_storage(self): import os fname = data_filename("interface_set_storage_test.nc") if os.path.isfile(fname): os.remove(fname) template = make_1d_traj([0.0])[0] storage_w = paths.Storage(fname, "w", template) storage_w.save(self.increasing_set) storage_w.sync_all() storage_r = paths.AnalysisStorage(fname) reloaded = storage_r.interfacesets[0] assert_items_equal(reloaded.lambdas, self.increasing_set.lambdas) for (truth, beauty) in zip(self.increasing_set, reloaded): assert_equal(truth, beauty) for (v, l) in zip(reloaded.volumes, reloaded.lambdas): assert_equal(reloaded.get_lambda(v), l) if os.path.isfile(fname): os.remove(fname)
def setup(self): op = CV_Function("myid", fcn=lambda snap : snap.coordinates[0][0]) volA = CVRangeVolume(op, -100, 0.0) volB = CVRangeVolume(op, 1.0, 100) volX = CVRangeVolume(op, -100, 0.25) self.dyn = CalvinistDynamics([ # successful move: (backward extension then forward) -0.13, 0.13, 0.33, -0.11, -0.12, 0.12, 0.32, -0.131, # never leaves state: -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.25, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, -0.15, # goes to other state: 1.16, 1.26, 1.16, -0.16, 1.16, 1.26, 1.16 ]) SampleGeneratingMover.engine = self.dyn self.dyn.initialized = True self.innermost = paths.TISEnsemble(volA, volB, volX) self.minus = paths.MinusInterfaceEnsemble(volA, volX) self.mover = MinusMover( minus_ensemble=self.minus, innermost_ensembles=self.innermost ) self.first_segment = [-0.1, 0.1, 0.3, 0.1, -0.15] self.list_innermost = [-0.11, 0.11, 0.31, 0.11, -0.12] self.second_segment = [-0.25, 0.2, 0.4, 0.2, -0.2] init_minus = make_1d_traj( coordinates=self.first_segment + [-0.35] + self.second_segment, velocities=[1.0]*11 ) self.minus_sample = Sample( replica=-1, trajectory=init_minus, ensemble=self.minus )
def test_make_ensemble(self): transitions = self.network.sampling_transitions # TODO: switch once network is working ensemble = self.post_network.make_ensemble(transitions) #ensemble = self.ms_outer.make_ensemble(transitions) test_AA = make_1d_traj([-0.1, 0.2, -0.2]) test_AXA = make_1d_traj([-0.1, 0.6, -0.2]) test_BB = make_1d_traj([1.1, 0.9, 1.2]) test_BXB = make_1d_traj([1.1, 0.5, 1.2]) test_AXB = make_1d_traj([-0.1, 0.6, 1.1]) test_BXA = make_1d_traj([1.1, 0.5, -0.1]) assert_equal(ensemble(test_AA), False) assert_equal(ensemble(test_AXA), True) assert_equal(ensemble(test_BB), False) assert_equal(ensemble(test_BXB), True) assert_equal(ensemble(test_BXA), True) assert_equal(ensemble(test_AXB), True)