예제 #1
0
 def _test_rate(self, i, j):
     reducer = TwoStateRates(self.rates, [i], [j])
     reducer.compute_rates()
     rAB = reducer.get_rate_AB() 
     self.assertAlmostEqual(rAB, self.final_rate, 7)
     
     reducer.compute_committors()
     rAB_ss = reducer.get_rate_AB_SS()
     print "kSS", rAB_ss
     self.assertAlmostEqual(rAB_ss, 1.5, 7)
예제 #2
0
def run(directory, T, A, B, out_prefix, tstart, reverse=False):
    if not reverse:
        source = "A"
        destination = "B"
        direction = "A->B"
    else:
        source = "B"
        destination = "A"
        direction = "B->A"

    rate_constants, Peq, knorm = make_rates(directory, T)
            

    if True:
        fname = "{}.rate_consts".format(out_prefix)
        print "saving rate constants to:", fname
        with open(fname, "w") as fout:
            fout.write("#starting_minimum ending_minimum rate_constant\n")
            for (u,v), k in sorted(rate_constants.iteritems()):
                fout.write("{} {} {}\n".format(u, v, k/knorm))

    print "checking and reducing the graph structure"
    try:
        rate_constants = reduce_rates(rate_constants, B, A=A)
    except Exception:
        analyze_graph_error(rate_constants, A, B)
        raise
    
    print "computing mean first passage times"
    calculator = TwoStateRates(rate_constants, A, B, weights=Peq, check_rates=False)
    calculator.compute_rates(use_umfpack=True)
    print "computing rates"
    kAB = calculator.get_rate_AB()
    print "k({}) {}".format(direction, kAB / knorm)
    
    if True:
        fname = "{}.rates".format(out_prefix)
        print "saving rates and mean first passage times for all minima to reach {} to file {}".format(destination, fname)
        mfpt = sorted([(m, t) for m, t in 
                       calculator.mfpt_computer.mfpt_dict.iteritems()])
        with open(fname, "w") as fout:
            fout.write("#Rates and mean first passage times for each node to reach {}\n".format(destination))
            fout.write("#The rate is just the inverse of the mean first passage time\n")
            fout.write("#minimum_index rate mean_first_passage_time\n")
            for i, t in mfpt:
                mt = t * knorm
                fout.write("{index} {rate} {mfpt}\n".format(index=i, rate=1./mt,
                                                            mfpt=mt))

    print "computing committor probabilities"
    sys.stdout.flush()
    calculator.compute_committors()
    print "computing steady state rate"
    kSS = calculator.get_rate_AB_SS() / knorm
    print "kSS({}) {}".format(direction, kSS)
    
    # print the committors
    # get the alternate definition of committors for the nodes in A and B
    Acomm = calculator.get_alternate_committors(A, B)
    Bcomm = calculator.get_alternate_committors(B, A)
    all_committors = calculator.committor_computer.committor_dict.copy()
    all_committors.update(Acomm)
    all_committors.update(Bcomm)
    
    fname = "{}.committors".format(out_prefix)
    print "saving committor probabilities for all minima to file", fname
    coms = sorted([(m, c) for m, c in all_committors.iteritems()])
    with open(fname, "w") as fout:
        fout.write("#probability a trajectory starting from each node ends up "
                   "in {B} before returning to {A}\n".format(B=destination, A=source))
        fout.write("#minimum_index committor_probability\n")
        for i, c in coms:
            fout.write("{} {}\n".format(i, c))
    
    time_solve = calculator.mfpt_computer.time_solve + calculator.committor_computer.time_solve
    print "time spent solving linear equations", time_solve, "seconds"
    print "total time", time.clock() - tstart