def main(plot=True): # make a graph representing a lattice in two dimensions dim = [10,10] grid_graph = nx.grid_graph(dim, periodic=False) A = [(0,0)] B = [(dim[0]-1, dim[1]-1)] # make some random rates for each of the edges rates = dict() for u, v in grid_graph.edges_iter(): rates[(u,v)] = np.random.rand() rates[(v,u)] = np.random.rand() # set up the graph reduction object reducer = GraphReduction(rates, A, B) # make the kmc graph from the rates kmc_graph = kmcgraph_from_rates(rates) com_prob = reducer.compute_committor_probabilities(kmc_graph.nodes()) if plot: # put it into matrix form and plot P = np.zeros(dim) for node, p in com_prob.iteritems(): x, y = node P[x,y] = p import matplotlib.pyplot as plt plt.imshow(P, cmap="BrBG", vmin=0, vmax=1) plt.title("probability of a trajectory reaching the lower right corner\n before reaching the upper left") plt.colorbar() plt.show()
def main(): nnodes = 100 # an easy way to set up the graph is to make # a dictionary of rates rates = dict() for i in range(nnodes): for j in range(i+1,nnodes): rates[(i,j)] = float(i+j) / (i+1) rates[(j,i)] = float(i+j) / (j+1) # we want to compute rates from node 0 to node 1 A = [0 ,1, 2] B = [3, 4] x = 2 weights=dict([(a,1.) for a in A+B]) weights[2] = 5e3 ngt = NGT(rates, A, B) ngt.compute() kAB = ngt.get_rate_AB() kBA = ngt.get_rate_BA() print "rate AB", kAB print "rate BA", kBA graph = kmcgraph_from_rates(rates) pyngt = GraphReduction(graph, A, B) pyngt.compute_rates() print "pyngt rate AB", pyngt.get_rate_AB() print "pyngt rate AB", pyngt.get_rate_BA()
def main(): nnodes = 3 # make matrix of transition rates with three states transition_matrix = [ [0., 1., 1.], [1., 0., 1.], [1., 1., 0.] ] print "Computing rates and committor probabilities for transition matrix" print transition_matrix # an easy way to set up the graph is to make # a dictionary of rates rates = dict() for i in range(nnodes): for j in range(nnodes): if i != j: rates[(i,j)] = transition_matrix[i][j] # we want to compute rates from node 0 to node 1 A = [0] B = [1] x = 0 # set up the class which will compute rates for us reducer = GraphReduction(rates, A, B) # now to check the values do a kinetic monte carlo run # use the utility function to build the rate graph with the correct formatting kmc_graph = kmcgraph_from_rates(rates) kmc = KineticMonteCarlo(kmc_graph) niterlist = [10, 100, 1000, 10000] print "" print "computing the probability for a trajectory to start at", x, "and reach", B, "before reaching", A PxB = reducer.compute_committor_probability(x) print "the committor probability computed by graph transformation is", PxB for niter in niterlist: PxB_KMC = kmc.committor_probability(x, A, B, niter=niter) print "the KMC committor probability averaged over %8d trajectories is %s. abs(PxB-PxB_KMC) = %s" % (niter, PxB_KMC, abs(PxB-PxB_KMC)) print "" print "computing the transition rate from nodes", A, "to nodes", B print "the exact rate for this network is 1.0" reducer.compute_rates() rAB = reducer.get_rate_AB() print "the transition rate computed by graph transformation is", rAB for niter in niterlist: rAB_KMC = kmc.mean_rate(A, B, niter=niter) print "the KMC rate averaged over %8d trajectories is %s. abs(rAB-rAB_KMC) = %s" % (niter, rAB_KMC, abs(rAB-rAB_KMC))
def _test_rate(self, i, j): reducer = GraphReduction(self.rates, [i], [j], debug=True) reducer.check_graph() reducer.compute_rates() rAB = reducer.get_rate_AB() rBA = reducer.get_rate_BA() reducer.check_graph() self.assertEqual(reducer.graph.number_of_nodes(), 2) self.assertEqual(reducer.graph.number_of_edges(), 4) self.assertAlmostEqual(rAB, self.final_rate, 7) self.assertAlmostEqual(rBA, self.final_rate, 7)
def main(): nnodes = 6 # the graph need not be made from a transition matrix, but it's an # easy way to make a random graph ensuring that everything is connected transition_matrix = np.random.uniform(0,1,[nnodes, nnodes]) print "Computing rates and committor probabilities for transition matrix" print transition_matrix # an easy way to set up the graph is to make # a dictionary of rates rates = dict() for i in range(nnodes): for j in range(nnodes): if i != j: rates[(i,j)] = transition_matrix[i][j] # we want to compute rates from node 0 to node 1 A = [0,1] B = [4,5] x = 2 # set up the class which will compute rates for us reducer = GraphReduction(rates, A, B) # now to check the values do a kinetic monte carlo run # use the utility function to build the rate graph with the correct formatting kmc_graph = kmcgraph_from_rates(rates) kmc = KineticMonteCarlo(kmc_graph) niterlist = [10, 100, 1000, 10000] print "" print "computing the probability for a trajectory to start at", x, "and reach", B, "before reaching", A PxB = reducer.compute_committor_probability(x) print "the committor probability computed by graph transformation is", PxB for niter in niterlist: PxB_KMC = kmc.committor_probability(x, A, B, niter=niter) print "the KMC committor probability averaged over %8d trajectories is %s. abs(PxB-PxB_KMC) = %s" % (niter, PxB_KMC, abs(PxB-PxB_KMC)) print "" print "computing the transition rate from nodes", A, "to nodes", B reducer.compute_rates() rAB = reducer.get_rate_AB() print "the transition rate computed by graph transformation is", rAB for niter in niterlist: rAB_KMC = kmc.mean_rate(A, B, niter=niter) print "the KMC rate averaged over %8d trajectories is %s. abs(rAB-rAB_KMC) = %s" % (niter, rAB_KMC, abs(rAB-rAB_KMC))
def do_check(self, A, B, nnodes=20, nedges=20): maker = _MakeRandomGraph(nnodes=20, nedges=20, node_set=A+B) graph = maker.run() reducer = GraphReduction(maker.rates, A, B, debug=False) reducer.check_graph() reducer.compute_rates() rAB = reducer.get_rate_AB() rBA = reducer.get_rate_BA() reducer.check_graph() self.assertEqual(reducer.graph.number_of_nodes(), len(A) + len(B)) if len(A) == 1 and len(B) == 1: self.assertLessEqual(reducer.graph.number_of_edges(), 4)
def compare(self, A, B, nnodes=10, nedges=20, weights=None, x=1): print "" maker = _MakeRandomGraph(nnodes=nnodes, nedges=nedges, node_set=A+B+[x]) graph = maker.run() graph_backup = graph.copy() reducer = GraphReduction(maker.rates, A, B, weights=weights) kmc = KineticMonteCarlo(graph_backup, debug=False) # test compute_committor_probability() PxB = reducer.compute_committor_probability(x) PxB_kmc = kmc.committor_probability(x, A, B, niter=1000) print "committor probability ", x, "->", B, "=", PxB print "committor probability kmc", x, "->", B, "=", PxB_kmc self.assertAlmostEqual(PxB, PxB_kmc, delta=.1) reducer.compute_rates() rAB = reducer.get_rate_AB() rBA = reducer.get_rate_BA() rAB_SS = reducer.get_rate_AB_SS() # compute rate via linalg lin = TwoStateRates(maker.rates, A, B, weights=weights) lin.compute_rates() rAB_LA = lin.get_rate_AB() lin.compute_committors() rAB_SS_LA = lin.get_rate_AB_SS() self.assertAlmostEqual(rAB_SS, rAB_SS_LA, 5) PxB_LA = lin.get_committor(x) if x not in A and x not in B: self.assertAlmostEqual(PxB, PxB_LA, 5) rAB_KMC = kmc.mean_rate(A, B, niter=1000, weights=weights) print "NGT rate A->B", rAB print "KMC rate A->B", rAB_KMC print "normalized difference", (rAB - rAB_KMC)/rAB print "normalized difference to linalg", (rAB - rAB_LA)/rAB self.assertLess(abs(rAB - rAB_KMC)/rAB, .1) self.assertLess(abs(rAB - rAB_LA)/rAB, .00001) rBA_KMC = kmc.mean_rate(B, A, niter=1000, weights=weights) print "NGT rate B->A", rBA print "KMC rate B->A", rBA_KMC print "normalized difference", (rBA - rBA_KMC)/rBA self.assertLess(abs(rBA - rBA_KMC)/rBA, .1) paB = kmc.committor_probability(A[0], [A[0]], B, niter=1000) print "the committor probability a->B", paB print "graph reduction committor prob", reducer.get_committor_probabilityAB(A[0]) self.assertAlmostEqual(paB, reducer.get_committor_probabilityAB(A[0]), delta=.1)
def readme_example(): from kmc_rates import GraphReduction, kmcgraph_from_rates nnodes = 4 # create a dictionary of transition rates rates = dict() for i in range(nnodes): for j in range(nnodes): if i != j: rates[(i,j)] = np.random.rand() # set up the calculation of the transition rate from node 0 to node 1 A = [0] B = [1] reducer = GraphReduction(rates, A, B) reducer.compute_rates() rAB = reducer.get_rate_AB() print "the transition rate from nodes", A, "to nodes", B, "is", rAB
def test_committor_probabilities(self, nnodes=10, nedges=20): A = [0,1,2,3] B = [8,9] xx = 5 maker = _MakeRandomGraph(nnodes=nnodes, nedges=nedges, node_set=A+B) graph = maker.run() graph_backup = graph.copy() kmc = KineticMonteCarlo(graph_backup, debug=False) reducer = GraphReduction(maker.rates, A, B) nodes = set(A + B + [xx]) PxB = reducer.compute_committor_probabilities(nodes) for x in nodes: self.assertIn(x, PxB) for x in nodes: PxB_kmc = kmc.committor_probability(x, A, B, niter=1000) self.assertAlmostEqual(PxB[x], PxB_kmc, delta=.1)