示例#1
0
def phase_space_average(CMD, Matsubara, M, swarmobj, derpotential, tim, deltat,
                        rng_ind):
    rng = np.random.RandomState(rng_ind)
    tarr = np.arange(0, tim + 0.0001, tim / 100.0)

    thermalize(CMD, Matsubara, M, swarmobj, derpotential, deltat, 100, rng)
    n_approx = 1
    Quan_arr = np.zeros_like(tarr)
    print('thermalized')
    #plt.scatter(swarmobj.q[:,0],swarmobj.q[:,1])
    #plt.show()
    if (0):
        for j in range(n_approx):
            traj_data = Monodromy.Integration_instance(swarmobj.q, swarmobj.p)
            pool = mp.Pool(mp.cpu_count() - 2)
            for i in range(1, len(tarr)):
                time_evolve(CMD, Matsubara, M, swarmobj, derpotential, deltat,
                            tarr[i] - tarr[i - 1])
                traj_data = Monodromy.vector_integrate(pool,
                                                       tarr[i] - tarr[i - 1],
                                                       traj_data)
                Quan_arr[i] += np.sum(Monodromy.vector_mqq(pool, traj_data))
            pool.close()
            pool.join()
            #thermalize(CMD,Matsubara,M,swarmobj,derpotential,deltat,4,rng)
            print(time.time() - start_time)
        Quan_arr /= (n_approx * swarmobj.N)

    for j in range(n_approx):
        traj_arr = Monodromy.ode_instance(swarmobj.q, swarmobj.p, tarr)
        Quan_arr += np.sum(Monodromy.detmqq(traj_arr), axis=1)
        thermalize(CMD, Matsubara, M, swarmobj, derpotential, deltat, 10, rng)

    return Quan_arr
def corr_function(swarmobj, A, B, time_corr, deltat, rng_ind):
    """ 
    This function computes the time correlation function for one instance
    of the system prepared by seeding with rng_ind
    
    Arguments:
        swarmobj: Swarm class object
        A: First operator, to compute correlation (A function of p,q)
        B: Second operator, to compute correlation (A function of p,q)
        time_corr : Total duration of correlation (float)
        rng_ind : Integer, used to seed the Randomstate object
        
    """
    rng = np.random.RandomState(rng_ind)
    tcf_tarr = np.arange(0, time_corr + 0.0001, time_corr / 500.0)
    tcf = np.zeros_like(tcf_tarr)
    n_approx = 10
    thermalize(swarmobj, dpotential, deltat, 10 * np.pi, rng)

    for j in range(n_approx):
        A_0 = A(swarmobj.q, swarmobj.p)
        tcf[0] += np.sum(A_0 * A_0)
        # The above line have to be treated on a case to case basis
        for i in range(1, len(tcf)):
            time_evolve(swarmobj, dpotential, deltat,
                        tcf_tarr[i] - tcf_tarr[i - 1])
            B_t = B(swarmobj.q, swarmobj.p)
            tcf[i] += np.sum(A_0 * B_t)
        thermalize(swarmobj, dpotential, deltat, 2 * np.pi, rng)
    tcf /= (n_approx * swarmobj.N)  #Change when you increase dimension

    return tcf
示例#3
0
def mean_force_calculation(dpotential):
    print(MD_System.n_beads,MD_System.w_arr)
    N = 100
    deltat = 1e-2
    Q = np.linspace(-10,10,100)
    swarmobject = MD_System.swarm(N)
    rand_boltz = np.random.RandomState(1000)
    swarmobject.p = rand_boltz.normal(0.0,swarmobject.m/MD_System.beta,(swarmobject.N,MD_System.dimension,MD_System.n_beads))
    print(np.shape(swarmobject.p))
    n_sample = 10
    rng=np.random.RandomState(1000)
    CMD=1
    
    CMD_force = np.zeros((len(Q),MD_System.dimension))
    
    for j in range(len(Q)):
        
        swarmobject.q = np.zeros((swarmobject.N,MD_System.dimension,MD_System.n_beads)) + Q[j]
        #print(swarmobject.q)
        #print(np.shape(swarmobject.q))
        thermalize(CMD,0,0,swarmobject,dpotential,deltat,5,rng)
        
        for i in range(n_sample):
            CMD_force[j] += np.mean(dpotential(swarmobject.q))
            thermalize(CMD,0,0,swarmobject,dpotential,deltat,2,rng)
    
    
    CMD_force/=n_sample    
        
    f = open("CMD_Force_Morse_B_{}_NB_{}_100_10.dat".format(MD_System.beta,MD_System.n_beads),'w+')
    CMD_force = CMD_force[:,0]
    print(CMD_force)
    for x in zip(Q,CMD_force):
        f.write("{}\t{}\n".format(x[0],x[1]))
    f.close()
def corr_function_upgrade(CMD, Matsubara, M, swarmobj, derpotential, A, B,
                          time_corr, deltat, rng_ind):
    rng = np.random.RandomState(rng_ind)
    tcf_tarr = np.arange(0, time_corr + 0.0001, time_corr / 100.0)

    tcf = np.zeros_like(tcf_tarr)

    tcf_taux = np.arange(0, 2 * time_corr + 0.0001, time_corr / 100.0)
    A_arr = np.zeros((len(tcf_taux), ) + swarmobj.q.shape)
    B_arr = np.zeros_like(A_arr)

    thermalize(CMD, Matsubara, M, swarmobj, derpotential, deltat, 20, rng)
    n_approx = 10
    print('kin en', swarmobj.sys_kin())
    #print('time_corr',time_corr)
    #The above line have to be treated on a case to case basis
    for j in range(n_approx):
        #print('j',j)
        A_arr[0] = A(swarmobj.q, swarmobj.p)
        B_arr[0] = B(swarmobj.q, swarmobj.p)
        if (1):
            for i in range(1, len(tcf_taux)):

                time_evolve(CMD, Matsubara, M, swarmobj, derpotential, deltat,
                            tcf_taux[i] - tcf_taux[i - 1])
                #time_evolve_nc(CMD,Matsubara,M,swarmobj, derpotential, deltat, tcf_taux[i]-tcf_taux[i-1],rng)
                A_arr[i] = A(swarmobj.q, swarmobj.p)
                B_arr[i] = B(swarmobj.q, swarmobj.p)

        A_centroid = np.sum(A_arr, axis=3) / MD_System.n_beads
        B_centroid = np.sum(B_arr, axis=3) / MD_System.n_beads

        A_centroid[len(tcf):] = 0.0
        tcf_cr = convolution(A_centroid, B_centroid, 0)
        tcf_cr = tcf_cr[:len(tcf), :, :]  #Truncating the padded part
        tcf_cr = np.sum(tcf_cr,
                        axis=2)  #Summing over the dimension (Dot product)
        tcf += np.sum(tcf_cr, axis=1)  #Summing over the particles
        # --- This part above is working alright!
        #print('j ended',j)
        #        for i in range(len(tcf)):
        #            for k in range(len(tcf)):
        #                #print(A_arr[i+j]*B_arr[j] + B_arr[i+j]*A_arr[j])
        #                tcf[i] += np.sum(A_centroid[i+k]*B_centroid[k] + B_centroid[i+k]*A_centroid[k])
        #tcf[i] += np.sum(A_0*B_t)
        thermalize(CMD, Matsubara, M, swarmobj, derpotential, deltat, 5, rng)
    tcf /= (n_approx * swarmobj.N * len(tcf) * MD_System.dimension)

    return tcf
def corr_function_matsubara(CMD, Matsubara, M, swarmobj, derpotential, A, B,
                            time_corr, deltat, rng_ind):
    rng = np.random.RandomState(rng_ind)
    tcf_tarr = np.arange(0, time_corr + 0.0001, time_corr / 100.0)
    tcf = np.zeros_like(tcf_tarr) + 0j
    print('hereh')
    tcf_taux = np.arange(0, 2 * time_corr + 0.0001,
                         time_corr / 100.0)  #time_corr/10.0)
    A_arr = np.zeros((len(tcf_taux), ) + swarmobj.q.shape)
    B_arr = np.zeros_like(A_arr)

    A_centroid = np.zeros((len(tcf_taux), swarmobj.N, MD_System.dimension))
    B_centroid = np.zeros_like(A_centroid)
    A_tilde = np.zeros_like(A_centroid)
    B_tilde = np.zeros_like(A_tilde)
    tcf_tilde = np.zeros_like(B_tilde)

    tcf_cr1 = np.zeros_like(tcf_tilde)
    tcf_cr2 = np.zeros((len(tcf), swarmobj.N, MD_System.dimension))
    tcf_cr = np.zeros((len(tcf), swarmobj.N))
    theta = np.zeros(swarmobj.N) + 0j
    #~ Find a way to condense the above lines.

    thermalize(CMD, 0, M, swarmobj, derpotential, deltat, 8, rng)

    denom = 0.0j
    n_approx = 10
    rearr = rearrangearr(M)

    for j in range(n_approx):
        A_arr[0] = A(swarmobj.q, swarmobj.p)
        B_arr[0] = B(swarmobj.q, swarmobj.p)

        for i in range(1, len(tcf_taux)):

            time_evolve(CMD, Matsubara, M, swarmobj, derpotential, deltat,
                        tcf_taux[i] - tcf_taux[i - 1])

            A_arr[i] = A(swarmobj.q, swarmobj.p)
            B_arr[i] = B(swarmobj.q, swarmobj.p)

        A_centroid[:] = A_arr[:, :, :, 1]
        B_centroid[:] = B_arr[:, :, :, 1]

        A_centroid[len(tcf):] = 0.0  #Padding for Fourier tranform

        tcf_cr1 = convolution(A_centroid, B_centroid, 0)

        tcf_cr2 = tcf_cr1[:len(tcf), :, :]
        tcf_cr = np.sum(
            tcf_cr2,
            axis=2)  #Summing over the dimensions (Equivalently dot product)

        theta = matsubara_phase_factor(M, swarmobj, rearr)
        exponent = 1j * (MD_System.beta) * theta
        tcf_cr = tcf_cr * np.exp(
            exponent
        )  #-- Line to be added after killing off dimension, be extra careful.
        tcf += np.sum(tcf_cr, axis=1)  #-- Summing up over the particles

        denom += np.sum(np.exp(exponent))

        thermalize(CMD, 0, M, swarmobj, derpotential, deltat,
                   2 * MD_System.beta, rng)

    tcf /= (n_approx * swarmobj.N * len(tcf) * MD_System.dimension
            )  #*MD_System.n_beads)
    denom /= (swarmobj.N * n_approx)
    tcf /= denom
    #print(denom)
    return tcf
示例#6
0
def mean_force_calculation_2D(dpotential):
    print(MD_System.n_beads,MD_System.w_arr)
    N = 100
    deltat = 1e-2
    x = np.linspace(-5,5,101)
    y = np.linspace(-5,5,101)
    X,Y = np.meshgrid(x,y)
    
    swarmobject = MD_System.swarm(N)
    rand_boltz = np.random.RandomState(1000)
    swarmobject.p = rand_boltz.normal(0.0,swarmobject.m/MD_System.beta,(swarmobject.N,MD_System.dimension,MD_System.n_beads))
    print(np.amax(swarmobject.p))
    n_sample = 10
    rng=np.random.RandomState(1000)
    CMD=1
    
    
    CMD_force = np.zeros((len(x),len(y),MD_System.dimension))
    
    for i in range(len(x)):
        for j in range(len(y)):
            swarmobject.q = np.zeros((swarmobject.N,MD_System.dimension,MD_System.n_beads)) 
            
            swarmobject.q[:,0,:] = X[i][j]
            swarmobject.q[:,1,:] = Y[i][j]

        
            thermalize(CMD,0,0,swarmobject,dpotential,deltat,2,rng)
            for k in range(n_sample):
                #print(np.amax(swarmobject.p))
                force = np.mean(dpotential(swarmobject.q),axis=2)
                force = np.mean(force,axis=0)
                CMD_force[i][j] += force
                thermalize(CMD,0,0,swarmobject,dpotential,deltat,2,rng)
        print(time.time()-start_time)
    CMD_force/=n_sample
    
    f = open("CMD_Force_2D_B_{}_NB_{}_101_10.dat".format(MD_System.beta,MD_System.n_beads),'wb')
   
    plt.imshow(CMD_force[:,:,1],origin='lower')    
#    for i in range(len(x)):
#        for j in range(len(y)):
#            f.write("{}\t{}\t{}\n".format(X[i][j],Y[i][j],CMD_force[i][j]))
    

    pickle.dump(x,f)
    pickle.dump(y,f)
    pickle.dump(CMD_force,f)
    f.close()


#    l=64-15
#    u=64+15
#    plt.plot(Q[l:u],CMD_force[l:u])
#    ##plt.scatter(Q,CMD_force)
#    plt.plot(data[l:u,0],-data[l:u,1],color='r')
#    plt.show()
    #return CMF
#mean_force_calculation()
#print(mean_force(4.0))

#print(CMD_force)