Ejemplo n.º 1
0
def test_mbar_computeEffectiveSampleNumber():
    """ testing computeEffectiveSampleNumber """

    for system_generator in system_generators:
        name, test = system_generator()
        x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn')
        eq(N_k, N_k_output)
        mbar = MBAR(u_kn, N_k)

        # one mathematical effective sample numbers should be between N_k and sum_k N_k
        N_eff = mbar.computeEffectiveSampleNumber()
        sumN = np.sum(N_k)
        assert all(N_eff > N_k)
        assert all(N_eff < sumN)
Ejemplo n.º 2
0
def test_mbar_computeEffectiveSampleNumber():
    """ testing computeEffectiveSampleNumber """

    for system_generator in system_generators:
        name, test = system_generator()
        x_n, u_kn, N_k_output, s_n = test.sample(N_k, mode='u_kn')
        eq(N_k, N_k_output)
        mbar = MBAR(u_kn, N_k)

        # one mathematical effective sample numbers should be between N_k and sum_k N_k
        N_eff = mbar.computeEffectiveSampleNumber()
        sumN = np.sum(N_k)
        assert all(N_eff > N_k)
        assert all(N_eff < sumN)
u_02 = U_to_u(U_02, pV_02)
u_22 = U_to_u(U_11, pV_22)

# Using just the Model 0 mdrun samples
N_k = np.array([len(u_00), 0, 0])  # The number of samples from the two states

u_kn = np.array([u_00, u_01, u_02])
U_kn = U_00

mbar = MBAR(u_kn, N_k)

(Deltaf_ij, dDeltaf_ij,
 Theta_ij) = mbar.getFreeEnergyDifferences(return_theta=True)
print "effective sample numbers"

print mbar.computeEffectiveSampleNumber()
#MRS: 1001 for state 0, 1.00 for state 2, 985 for state 3

# MRS: The observable we are interested in is U, internal energy.  The
# question is, WHICH internal energy.  We are interested in the
# internal energy generated from the ith potential.  So there are
# actually _three_ observables.

# Now, the confusing thing, we can estimate the expectation of the
# three observables in three different states. We can estimate the
# observable of U_0 in states 0, 1 and 2, the observable of U_1 in
# states 0, 1, and 2, etc.

EAk = np.zeros([3, 3])
dEAk = np.zeros([3, 3])
(EAk[:, 0], dEAk[:, 0]) = mbar.computeExpectations(
Ejemplo n.º 4
0
    if (numpy.abs(numpy.sum(O_ij[k, :]) - 1) < 1.0e-10):
        print("looks like it is.")
    else:
        print("but it's not.")

print("Overlap eigenvalue output")
print(O_i)

print("Overlap scalar output")
print(O)

print("============================================")
print("    Testing computeEffectiveSampleNumber    ")
print("============================================")

N_eff = mbar.computeEffectiveSampleNumber(verbose=True)
print("Effective Sample number")
print(N_eff)
print("Compare stanadrd estimate of <x> with the MBAR estimate of <x>")
print(
    "We should have that with MBAR, err_MBAR = sqrt(N_k/N_eff)*err_standard,")
print("so standard (scaled) results should be very close to MBAR results.")
print("No standard estimate exists for states that are not sampled.")
A_kn = x_kn
results = mbar.computeExpectations(A_kn)
val_mbar = results['mu']
err_mbar = results['sigma']
err_standard = numpy.zeros([K], dtype=numpy.float64)
err_scaled = numpy.zeros([K], dtype=numpy.float64)

for k in range(K):
u_kn[0,:len(u00)] = u00
u_kn[0,len(u00):] = u01     
u_kn[1,:len(u00)] = u10
u_kn[1,len(u00):] = u11    
u_kn[2,:len(u00)] = u20
u_kn[2,len(u00):] = u21   
     
N_kn = np.zeros(len(u00)+len(u11))
N_kn[:len(u00)] = N0
N_kn[len(u00):] = N1     
              
mbar = MBAR(u_kn,N_k)

Deltaf_ij = mbar.getFreeEnergyDifferences(return_theta=False)
print "effective sample numbers"
print (mbar.computeEffectiveSampleNumber())
print('\nWhich is approximately '+str(mbar.computeEffectiveSampleNumber()/N_K*100.)+'%')

NAk, dNAk = mbar.computeExpectations(N_kn) # Average number of molecules
NAk_alt = np.zeros(len(N_k))
for i in range(len(N_k)):
    NAk_alt[i] = np.sum(mbar.W_nk[:,i]*N_kn)
    
print(NAk)

Nscan = np.arange(60,100)

sqdeltaW0 = np.zeros(len(Nscan))

for iN, Ni in enumerate(Nscan):
    print "looks like it is."
  else:
    print "but it's not."

print "Overlap eigenvalue output"
print O_i


print "Overlap scalar output"
print O

print "============================================"
print "    Testing computeEffectiveSampleNumber    "
print "============================================"

N_eff = mbar.computeEffectiveSampleNumber(verbose = True)
print "Effective Sample number"
print N_eff
print "Compare stanadrd estimate of <x> with the MBAR estimate of <x>"
print "We should have that with MBAR, err_MBAR = sqrt(N_k/N_eff)*err_standard,"
print "so standard (scaled) results should be very close to MBAR results."
print "No standard estimate exists for states that are not sampled."
A_kn = x_kn
(val_mbar, err_mbar) = mbar.computeExpectations(A_kn)
err_standard = numpy.zeros([K],dtype = numpy.float64)
err_scaled = numpy.zeros([K],dtype = numpy.float64)

for k in range(K):
  if N_k[k] != 0:
    # use position
    err_standard[k] = numpy.std(A_kn[k,0:N_k[k]])/numpy.sqrt(N_k[k]-1)
plt.ylabel(r'$U$')
plt.show()

# Using just the Model 0 mdrun samples
N_k = np.array([len(u0), 0, 0, 0,
                0])  # The number of samples from the different states

u_kn = np.array([u0, u1, u2, u3, u4])
Nmol_kn = N0

mbar = MBAR(u_kn, N_k)

(Deltaf_ij, dDeltaf_ij,
 Theta_ij) = mbar.getFreeEnergyDifferences(return_theta=True)
print "effective sample numbers"
print(mbar.computeEffectiveSampleNumber() / N_k[0] * 100.)
print('\nWhich is approximately ' +
      str(mbar.computeEffectiveSampleNumber() / N_k[0] * 100.) + '%')

NAk, dNAk = mbar.computeExpectations(N0)  # Average number of molecules
NAk_alt = np.zeros(len(N_k))
for i in range(len(N_k)):
    NAk_alt[i] = np.sum(mbar.W_nk[:, i] * N0)

print(NAk)

Nscan = np.arange(60, 100)

sqdeltaW0 = np.zeros(len(Nscan))

for iN, Ni in enumerate(Nscan):
plt.plot(Temp_sim, mu_sim, 'ro', mfc='None', label='Simulation')
plt.plot(Temp_VLE, mu_VLE_guess, 'b--', label=r'$\mu_{\rm guess}$')
plt.xlabel(r'$T$ (K)')
plt.ylabel(r'$\mu_{\rm opt}$ (K)')
plt.xlim([300, 550])
plt.ylim([-4200, -3600])
plt.legend()
plt.show()

plt.plot(Temp_VLE, sqdeltaW_opt, 'ko')
plt.xlabel(r'$T$ (K)')
plt.ylabel(r'$(\Delta W^{\rm sat})^2$')
plt.show()

print("Effective sample numbers")
print(mbar.computeEffectiveSampleNumber())
print('\nWhich is approximately ' +
      str(mbar.computeEffectiveSampleNumber() / sumN_k * 100.) +
      '% of the total snapshots')

###Scan of mu
#mu_scan = np.linspace(-4120,-4000,20)
#mu_scan = np.linspace(-3750,-3680,10)
#sqdeltaW_all = np.zeros([len(Temp_VLE),len(mu_scan)])
#
#for imu, mui in enumerate(mu_scan):
#
#    mu_array = [mui]*len(Temp_VLE)
#    sqdeltaW_all[:,imu] = sqdeltaW(mu_array)
##    print(imu)
#