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 = 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 _three_state_graph(): return kmcgraph_from_rates(_three_state_rates())
def run(self): self.make_rates() return kmcgraph_from_rates(self.rates)