示例#1
0
def difference(delta):
    x = MC.runMC(MCcycles2, delta, idum, alpha)
    return x.accepted * 1.0 / MCcycles2 - .5  #We want 50% accepted moves
示例#2
0
#Loop over alpha values
for alpha in values:

    #Determination of optimal delta value (for each alpha), i.e.
    #finding the zero-point of the difference function by the bisection method
    minimum = delta_min
    maximum = delta_max
    while maximum - minimum > tolerance:
        if difference(minimum) * difference((minimum + maximum) / 2) < 0:
            maximum = (minimum + maximum) / 2
        else:
            minimum = (minimum + maximum) / 2
    delta = (minimum + maximum) / 2

    #Run MC calculation (store results in x)
    x = MC.runMC(MCcycles / nprocs, delta, idum, alpha)
    idum = x.idum

    #All nodes send their stats to the master
    if myid != 0:
        pypar.send((x.sum, x.squaresum, x.N, x.accepted), destination=0)

    #The master receives, calculates and writes to file
    if myid == 0:
        for i in xrange(1, nprocs):
            isum, isquaresum, iN, iaccepted = pypar.receive(source=i)
            x.sum += isum
            x.squaresum += isquaresum
            x.N += iN
            x.accepted += iaccepted
def difference(delta):
    x = MC.runMC(MCcycles2,delta,idum,alpha)
    return x.accepted*1.0/MCcycles2 - .5 #We want 50% accepted moves
#Loop over alpha values
for alpha in values:

    #Determination of optimal delta value (for each alpha), i.e.
    #finding the zero-point of the difference function by the bisection method
    minimum = delta_min
    maximum = delta_max
    while maximum - minimum > tolerance:
        if difference(minimum)*difference((minimum+maximum)/2) < 0:
            maximum = (minimum + maximum) / 2
        else:
            minimum = (minimum + maximum) / 2
    delta = (minimum + maximum) / 2
    
    #Run MC calculation (store results in x)
    x = MC.runMC(MCcycles/nprocs,delta,idum,alpha)
    idum = x.idum

    #All nodes send their stats to the master
    if myid != 0:
        pypar.send((x.sum,x.squaresum,x.N,x.accepted),destination=0)

    #The master receives, calculates and writes to file
    if myid == 0:
        for i in xrange(1,nprocs):
            isum,isquaresum,iN,iaccepted = pypar.receive(source=i)
            x.sum += isum
            x.squaresum += isquaresum
            x.N += iN
            x.accepted += iaccepted
        #If it isn't the termination signal, do the job and return the result
        if alpha != 0.0:

            #Determination of optimal delta value (for each alpha), i.e.
            #finding the zero-point of the difference function by the bisection method
            minimum = delta_min
            maximum = delta_max
            while maximum - minimum > tolerance:
                if difference(minimum)*difference((minimum+maximum)/2) < 0:
                    maximum = (minimum + maximum) / 2
                else:
                    minimum = (minimum + maximum) / 2
            delta = (minimum + maximum) / 2
    
            #Run MC calculation (store results in x)
            x = MC.runMC(MCcycles,delta,idum,alpha)
            idum = x.idum

            #Calculate statistics and store them in a result object
            thisresult = result()
            thisresult.alpha = alpha
            thisresult.id = myid
            
            thisresult.E = x.sum / x.N
            E2 = x.squaresum / x.N
            thisresult.sigma = sqrt(E2 - thisresult.E**2)
            thisresult.acceptance = x.accepted*1.0/MCcycles
            thisresult.error = thisresult.sigma / sqrt(x.N)

            #Send to master
            pypar.send(thisresult,destination=0)
#Loop over alpha values
for alpha in values:

    #Determination of optimal delta value (for each alpha), i.e.
    #finding the zero-point of the difference function by the bisection method
    minimum = delta_min
    maximum = delta_max
    while maximum - minimum > tolerance:
        if difference(minimum)*difference((minimum+maximum)/2) < 0:
            maximum = (minimum + maximum) / 2
        else:
            minimum = (minimum + maximum) / 2
    delta = (minimum + maximum) / 2
    
    #Run MC calculation (store results in x)
    x = MC.runMC(MCcycles,delta,idum,alpha)
    idum = x.idum

    #Calculate statistics
    E = x.sum / x.N
    E2 = x.squaresum / x.N
    sigma = sqrt(E2 - E**2)
    acceptance = x.accepted*1.0/MCcycles
    error = sigma / sqrt(x.N)

    #Print results to screen
    #print 'alpha = %f, <E> = %f, sigma = %f, error = %f, acceptance = %f' %(alpha,E,sigma,error,acceptance)

    #Print results to file
    outfile.write('%f %f %f %f %f\n' %(alpha,E,sigma,error,acceptance))