Example #1
0
    def minimize(self):
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where(visitsT != 0, np.log(visitsT), 0)
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis, :] / (
            self.Tlist[:, np.newaxis] * self.k_B)

        self.whampot = WhamPotential(self.logP, self.reduced_energy)

        X = np.random.rand(nreps + nbins)
        E = self.whampot.getEnergy(X)
        #print "energy", E

        #print "quenching"
        try:
            from pygmin.optimize import mylbfgs as quench
            ret = quench(X, self.whampot, iprint=-1, maxstep=1e4)
        except ImportError:
            from pygmin.optimize import lbfgs_scipy as quench
            ret = quench(X, self.whampot)
        #print "quench energy", ret.energy
        X = ret.coords

        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]
    def minimize(self):
        #shape(visits2d) is now (nqbins, nebins, nreps)
        #we need it to be (nreps, nqbins*nebins)
        #first reorder indices
        nreps = self.nrep
        nebins = self.nebins
        nqbins = self.nqbins
        nbins = self.nebins * self.nqbins

        reduced_energy = np.zeros([nreps, nebins, nqbins])
        for j in range(self.nqbins):
            reduced_energy[:, :, j] = self.binenergy[np.newaxis, :] / (
                self.Tlist[:, np.newaxis] * self.k_B)

        visits = self.visits2d
        visits = np.reshape(visits, [nreps, nbins])
        reduced_energy = np.reshape(reduced_energy, [nreps, nbins])
        self.logP = np.where(visits != 0, np.log(visits.astype(float)), 0)

        from wham_potential import WhamPotential
        whampot = WhamPotential(visits, reduced_energy)

        nvar = nbins + nreps
        if False:
            X = np.random.rand(nvar)
        else:
            # estimate an initial guess for the offsets and density of states
            # so the minimizer converges more rapidly
            offsets_estimate, log_dos_estimate = wham_utils.estimate_dos(
                visits, reduced_energy)
            X = np.concatenate((offsets_estimate, log_dos_estimate))
            assert X.size == nvar

        E0, grad = whampot.getEnergyGradient(X)
        rms0 = np.linalg.norm(grad) / np.sqrt(grad.size)

        from wham_utils import lbfgs_scipy
        ret = lbfgs_scipy(X, whampot)
        #        print "initial energy", whampot.getEnergy(X)
        #        try:
        #            from pele.optimize import mylbfgs as quench
        #            ret = quench(X, whampot, iprint=10, maxstep = 1e4)
        #        except ImportError:
        #            from pele.optimize import lbfgs_scipy as quench
        #            ret = quench(X, whampot)

        if self.verbose:
            print "chi^2 went from %g (rms %g) to %g (rms %g) in %d iterations" % (
                E0, rms0, ret.energy, ret.rms, ret.nfev)

        #self.logn_Eq = zeros([nebins,nqbins], float64)
        X = ret.coords
        self.logn_Eq = X[nreps:]
        self.w_i_final = X[:nreps]

        self.logn_Eq = np.reshape(self.logn_Eq, [nebins, nqbins])
        self.logn_Eq = np.where(
            self.visits2d.sum(0) == 0, self.LOGMIN, self.logn_Eq)
Example #3
0
    def minimize(self):
        #shape(visits2d) is now (nqbins, nebins, nreps)
        #we need it to be (nreps, nqbins*nebins)
        #first reorder indices
        nreps = self.nrep
        nebins = self.nebins
        nqbins = self.nqbins
        nbins = self.nebins * self.nqbins
        #visits = np.zeros([nreps, nebins, nqbins], np.integer)
        reduced_energy = np.zeros([nreps, nebins, nqbins])
#        for k in range(self.nrep):
#            for j in range(self.nqbins):
#                for i in range(self.nebins):
#                    #visits[k,i,j] = self.visits2d[i,j,k]
#                    reduced_energy[k,i,j] = self.binenergy[i] / (self.Tlist[k]*self.k_B)
        for j in range(self.nqbins):
            reduced_energy[:,:,j] = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis]*self.k_B)
                    
        visits = self.visits2d
        visits = np.reshape( visits, [nreps, nbins ]) 
        reduced_energy = np.reshape( reduced_energy, [nreps, nbins])           
        self.logP = np.where( visits != 0, np.log( visits.astype(float) ), 0 )

        
        from wham_potential import WhamPotential
        whampot = WhamPotential( self.logP, reduced_energy )
        
        nvar = nbins + nreps
        X = np.random.rand(nvar)
        print "initial energy", whampot.getEnergy(X)
        try: 
            from pygmin.optimize import mylbfgs as quench
            ret = quench(X, whampot, iprint=10, maxstep = 1e4)
        except ImportError:
            from pygmin.optimize import lbfgs_scipy as quench
            ret = quench(X, whampot)            

        print "quenched energy", ret.energy
        
        global_min = False
        if global_min:
            from pygmin.basinhopping import BasinHopping
            from pygmin.takestep.displace import RandomDisplacement
            takestep = RandomDisplacement(stepsize=10.)
            takestep.useAdaptiveStep()
            takestep.adaptive_class.f = 2.
            bh = BasinHopping(X, whampot, takestep)
            bh.run(1000)
        
        
        #self.logn_Eq = zeros([nebins,nqbins], float64)
        X = ret.coords
        self.logn_Eq = X[nreps:]
        self.w_i_final = X[:nreps]
        
        self.logn_Eq = np.reshape(self.logn_Eq, [nebins, nqbins])
        self.logn_Eq = np.where( self.visits2d.sum(0) == 0, self.LOGMIN, self.logn_Eq )
Example #4
0
 def minimize(self):
     nreps = self.nrep
     nbins = self.nebins
     visitsT = (self.visits1d)
     #print "min vis", np.min(visitsT)
     self.logP = np.where( visitsT != 0, np.log( visitsT ), 0 )
     #print "minlogp", np.min(self.logP)
     self.reduced_energy = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis] * self.k_B)
     
     self.whampot = WhamPotential(self.logP, self.reduced_energy)
     
     
     X = np.random.rand( nreps + nbins )
     E = self.whampot.getEnergy(X)
     #print "energy", E 
     
     #print "quenching"
     try: 
         from pygmin.optimize import mylbfgs as quench
         ret = quench(X, self.whampot, iprint=-1, maxstep=1e4)
     except ImportError:
         from pygmin.optimize import lbfgs_scipy as quench
         ret = quench(X, self.whampot)            
     #print "quench energy", ret.energy
     X = ret.coords
     
     self.logn_E = X[nreps:]
     self.w_i_final = X[:nreps]
Example #5
0
    def globalMinimization(self):
        """
        in experimentation i've never been able to find more than
        one minimum
        """
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where(visitsT != 0, np.log(visitsT), 0)
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis, :] / (
            self.Tlist[:, np.newaxis] * self.k_B)

        self.whampot = WhamPotential(self.logP, self.reduced_energy)

        X = np.random.rand(nreps + nbins)
        E = self.whampot.getEnergy(X)
        print "energy", E

        print "quenching"
        from pygmin.optimize import lbfgs_scipy as quench
        ret = quench(X, self.whampot)
        print "quench energy", ret.energy

        from pygmin.basinhopping import BasinHopping
        from pygmin.takestep.displace import RandomDisplacement
        takestep = RandomDisplacement(stepsize=10)
        takestep.useAdaptiveStep()
        takestep.adaptive_class.f = 1.5  #i have no idea what a good stepsize should be
        bh = BasinHopping(X, self.whampot, takestep)

        import matplotlib.pyplot as plt
        for i in range(10):
            bh.run(2000)
            self.logn_E = bh.coords[nreps:]
            cvdata = self.calc_Cv(400)
            plt.plot(cvdata[:, 0], cvdata[:, 5], '-')
        plt.show()

        X = bh.coords
        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]
    def minimize(self):
        #shape(visits2d) is now (nqbins, nebins, nreps)
        #we need it to be (nreps, nqbins*nebins)
        #first reorder indices
        nreps = self.nrep
        nebins = self.nebins
        nqbins = self.nqbins
        nbins = self.nebins * self.nqbins
        #visits = np.zeros([nreps, nebins, nqbins], np.integer)
        reduced_energy = np.zeros([nreps, nebins, nqbins])
        #        for k in range(self.nrep):
        #            for j in range(self.nqbins):
        #                for i in range(self.nebins):
        #                    #visits[k,i,j] = self.visits2d[i,j,k]
        #                    reduced_energy[k,i,j] = self.binenergy[i] / (self.Tlist[k]*self.k_B)
        for j in range(self.nqbins):
            reduced_energy[:, :, j] = self.binenergy[np.newaxis, :] / (
                self.Tlist[:, np.newaxis] * self.k_B)

        visits = self.visits2d
        visits = np.reshape(visits, [nreps, nbins])
        reduced_energy = np.reshape(reduced_energy, [nreps, nbins])
        self.logP = np.where(visits != 0, np.log(visits.astype(float)), 0)

        from wham_potential import WhamPotential
        whampot = WhamPotential(self.logP, reduced_energy)

        nvar = nbins + nreps
        X = np.random.rand(nvar)
        from wham_utils import lbfgs_scipy
        ret = lbfgs_scipy(X, self.whampot)
        #        print "initial energy", whampot.getEnergy(X)
        #        try:
        #            from pele.optimize import mylbfgs as quench
        #            ret = quench(X, whampot, iprint=10, maxstep = 1e4)
        #        except ImportError:
        #            from pele.optimize import lbfgs_scipy as quench
        #            ret = quench(X, whampot)

        print "quenched energy", ret.energy

        #self.logn_Eq = zeros([nebins,nqbins], float64)
        X = ret.coords
        self.logn_Eq = X[nreps:]
        self.w_i_final = X[:nreps]

        self.logn_Eq = np.reshape(self.logn_Eq, [nebins, nqbins])
        self.logn_Eq = np.where(
            self.visits2d.sum(0) == 0, self.LOGMIN, self.logn_Eq)
Example #7
0
 def globalMinimization(self):
     """
     in experimentation i've never been able to find more than
     one minimum
     """
     nreps = self.nrep
     nbins = self.nebins
     visitsT = (self.visits1d)
     #print "min vis", np.min(visitsT)
     self.logP = np.where( visitsT != 0, np.log( visitsT ), 0 )
     #print "minlogp", np.min(self.logP)
     self.reduced_energy = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis] * self.k_B)
     
     self.whampot = WhamPotential(self.logP, self.reduced_energy)
     
     X = np.random.rand( nreps + nbins )
     E = self.whampot.getEnergy(X)
     print "energy", E 
     
     print "quenching"
     from pygmin.optimize import lbfgs_scipy as quench
     ret = quench(X, self.whampot)
     print "quench energy", ret.energy
     
     from pygmin.basinhopping import BasinHopping
     from pygmin.takestep.displace import RandomDisplacement
     takestep = RandomDisplacement(stepsize=10)
     takestep.useAdaptiveStep()
     takestep.adaptive_class.f = 1.5 #i have no idea what a good stepsize should be
     bh = BasinHopping(X, self.whampot, takestep )
     
     import matplotlib.pyplot as plt
     for i in range(10):
         bh.run(2000)
         self.logn_E = bh.coords[nreps:]
         cvdata = self.calc_Cv(400)
         plt.plot(cvdata[:,0], cvdata[:,5], '-')
     plt.show()
         
         
     
     X = bh.coords
     self.logn_E = X[nreps:]
     self.w_i_final = X[:nreps]
Example #8
0
class wham1d:
    """ class to combine 1d histograms of energy E at
    multiple temperatures into one best estimate for the histogram

    input will be:
    filenames : list of filenames where the data can be found
    Tlist # Tlist[k] is the temperature of the simulation in filenames[k] 

    binenergy = zeros(nebins, float64) #lower edge of bin energy
    visits1d =  zeros([nrep,nebins], integer) #1d histograms of data
    """
    #=============================================================================================
    # Constructor.
    #=============================================================================================
    def __init__(self, Tlist, binenergy, visits1d):

        #define some parameters
        self.k_B=1.

        self.nrep = len(Tlist)
        self.nebins = len(binenergy)
        self.Tlist = np.array(Tlist, dtype = np.float64)
        self.binenergy = np.array(binenergy, dtype = np.float64)
        self.visits1d = np.array(visits1d, dtype = np.integer)

    def minimize(self):
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where( visitsT != 0, np.log( visitsT ), 0 )
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis] * self.k_B)
        
        self.whampot = WhamPotential(self.logP, self.reduced_energy)
        
        
        X = np.random.rand( nreps + nbins )
        E = self.whampot.getEnergy(X)
        #print "energy", E 
        
        #print "quenching"
        try: 
            from pygmin.optimize import mylbfgs as quench
            ret = quench(X, self.whampot, iprint=-1, maxstep=1e4)
        except ImportError:
            from pygmin.optimize import lbfgs_scipy as quench
            ret = quench(X, self.whampot)            
        #print "quench energy", ret.energy
        X = ret.coords
        
        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]
        
    def globalMinimization(self):
        """
        in experimentation i've never been able to find more than
        one minimum
        """
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where( visitsT != 0, np.log( visitsT ), 0 )
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis] * self.k_B)
        
        self.whampot = WhamPotential(self.logP, self.reduced_energy)
        
        X = np.random.rand( nreps + nbins )
        E = self.whampot.getEnergy(X)
        print "energy", E 
        
        print "quenching"
        from pygmin.optimize import lbfgs_scipy as quench
        ret = quench(X, self.whampot)
        print "quench energy", ret.energy
        
        from pygmin.basinhopping import BasinHopping
        from pygmin.takestep.displace import RandomDisplacement
        takestep = RandomDisplacement(stepsize=10)
        takestep.useAdaptiveStep()
        takestep.adaptive_class.f = 1.5 #i have no idea what a good stepsize should be
        bh = BasinHopping(X, self.whampot, takestep )
        
        import matplotlib.pyplot as plt
        for i in range(10):
            bh.run(2000)
            self.logn_E = bh.coords[nreps:]
            cvdata = self.calc_Cv(400)
            plt.plot(cvdata[:,0], cvdata[:,5], '-')
        plt.show()
            
            
        
        X = bh.coords
        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]

#    def calc_Cv_no_wham(self):
#        """ """

    def calc_Cv_new(self, NDOF, TRANGE=None, NTEMP=100):
        from pygmin.thermodynamics import dos_to_cv
        dT = (self.Tlist[-1] - self.Tlist[0]) / NTEMP
        Tlist = np.arange(self.Tlist[0], self.Tlist[-1], dT)
#        print self.logn_E
        lZ, U, U2, Cv = dos_to_cv(self.binenergy, self.logn_E, Tlist, K=NDOF)
        cvdata = np.zeros([len(Tlist), 6])
        cvdata[:,0] = Tlist
        cvdata[:,1] = lZ
        cvdata[:,2] = U # average potential energy
        cvdata[:,3] = U2
        cvdata[:,5] = Cv
        
        eavg = U + float(NDOF) / 2 * Tlist # average energy including the kinetic degrees of freedom
        cvdata[:,4] = eavg
        return cvdata
        

    def calc_Cv(self, NDOF, TRANGE=None, NTEMP=100, use_log_sum=None):
#        return self.calc_Cv_new(NDOF, TRANGE, NTEMP)
        return wham_utils.calc_Cv(self.logn_E, self.visits1d, self.binenergy,
                NDOF, self.Tlist, self.k_B, TRANGE, NTEMP, use_log_sum=use_log_sum)
Example #9
0
class wham1d:
    """ class to combine 1d histograms of energy E at
    multiple temperatures into one best estimate for the histogram

    input will be:
    filenames : list of filenames where the data can be found
    Tlist # Tlist[k] is the temperature of the simulation in filenames[k] 

    binenergy = zeros(nebins, float64) #lower edge of bin energy
    visits1d =  zeros([nrep,nebins], integer) #1d histograms of data
    """

    #=============================================================================================
    # Constructor.
    #=============================================================================================
    def __init__(self, Tlist, binenergy, visits1d):

        #define some parameters
        self.k_B = 1.

        self.nrep = len(Tlist)
        self.nebins = len(binenergy)
        self.Tlist = np.array(Tlist, dtype=np.float64)
        self.binenergy = np.array(binenergy, dtype=np.float64)
        self.visits1d = np.array(visits1d, dtype=np.integer)

    def minimize(self):
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where(visitsT != 0, np.log(visitsT), 0)
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis, :] / (
            self.Tlist[:, np.newaxis] * self.k_B)

        self.whampot = WhamPotential(self.logP, self.reduced_energy)

        X = np.random.rand(nreps + nbins)
        E = self.whampot.getEnergy(X)
        #print "energy", E

        #print "quenching"
        try:
            from pygmin.optimize import mylbfgs as quench
            ret = quench(X, self.whampot, iprint=-1, maxstep=1e4)
        except ImportError:
            from pygmin.optimize import lbfgs_scipy as quench
            ret = quench(X, self.whampot)
        #print "quench energy", ret.energy
        X = ret.coords

        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]

    def globalMinimization(self):
        """
        in experimentation i've never been able to find more than
        one minimum
        """
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where(visitsT != 0, np.log(visitsT), 0)
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis, :] / (
            self.Tlist[:, np.newaxis] * self.k_B)

        self.whampot = WhamPotential(self.logP, self.reduced_energy)

        X = np.random.rand(nreps + nbins)
        E = self.whampot.getEnergy(X)
        print "energy", E

        print "quenching"
        from pygmin.optimize import lbfgs_scipy as quench
        ret = quench(X, self.whampot)
        print "quench energy", ret.energy

        from pygmin.basinhopping import BasinHopping
        from pygmin.takestep.displace import RandomDisplacement
        takestep = RandomDisplacement(stepsize=10)
        takestep.useAdaptiveStep()
        takestep.adaptive_class.f = 1.5  #i have no idea what a good stepsize should be
        bh = BasinHopping(X, self.whampot, takestep)

        import matplotlib.pyplot as plt
        for i in range(10):
            bh.run(2000)
            self.logn_E = bh.coords[nreps:]
            cvdata = self.calc_Cv(400)
            plt.plot(cvdata[:, 0], cvdata[:, 5], '-')
        plt.show()

        X = bh.coords
        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]

#    def calc_Cv_no_wham(self):
#        """ """

    def calc_Cv_new(self, NDOF, TRANGE=None, NTEMP=100):
        from pygmin.thermodynamics import dos_to_cv
        dT = (self.Tlist[-1] - self.Tlist[0]) / NTEMP
        Tlist = np.arange(self.Tlist[0], self.Tlist[-1], dT)
        #        print self.logn_E
        lZ, U, U2, Cv = dos_to_cv(self.binenergy, self.logn_E, Tlist, K=NDOF)
        cvdata = np.zeros([len(Tlist), 6])
        cvdata[:, 0] = Tlist
        cvdata[:, 1] = lZ
        cvdata[:, 2] = U  # average potential energy
        cvdata[:, 3] = U2
        cvdata[:, 5] = Cv

        eavg = U + float(
            NDOF
        ) / 2 * Tlist  # average energy including the kinetic degrees of freedom
        cvdata[:, 4] = eavg
        return cvdata

    def calc_Cv(self, NDOF, TRANGE=None, NTEMP=100, use_log_sum=None):
        #        return self.calc_Cv_new(NDOF, TRANGE, NTEMP)
        return wham_utils.calc_Cv(self.logn_E,
                                  self.visits1d,
                                  self.binenergy,
                                  NDOF,
                                  self.Tlist,
                                  self.k_B,
                                  TRANGE,
                                  NTEMP,
                                  use_log_sum=use_log_sum)
    def minimize(self):
        # shape(visits2d) is now (nqbins, nebins, nreps)
        # we need it to be (nreps, nqbins*nebins)
        # first reorder indices
        nreps = self.nrep
        nebins = self.nebins
        nqbins = self.nqbins
        nbins = self.nebins * self.nqbins

        reduced_energy = np.zeros([nreps, nebins, nqbins])
        for j in range(self.nqbins):
            reduced_energy[:, :, j] = self.binenergy[np.newaxis, :] / (self.Tlist[:, np.newaxis] * self.k_B)

        visits = self.visits2d
        visits = np.reshape(visits, [nreps, nbins])
        reduced_energy = np.reshape(reduced_energy, [nreps, nbins])
        self.logP = np.where(visits != 0, np.log(visits.astype(float)), 0)

        from wham_potential import WhamPotential

        whampot = WhamPotential(visits, reduced_energy)

        nvar = nbins + nreps
        if False:
            X = np.random.rand(nvar)
        else:
            # estimate an initial guess for the offsets and density of states
            # so the minimizer converges more rapidly
            offsets_estimate, log_dos_estimate = wham_utils.estimate_dos(visits, reduced_energy)
            X = np.concatenate((offsets_estimate, log_dos_estimate))
            assert X.size == nvar

        E0, grad = whampot.getEnergyGradient(X)
        rms0 = np.linalg.norm(grad) / np.sqrt(grad.size)

        from wham_utils import lbfgs_scipy

        ret = lbfgs_scipy(X, whampot)
        #        print "initial energy", whampot.getEnergy(X)
        #        try:
        #            from pele.optimize import mylbfgs as quench
        #            ret = quench(X, whampot, iprint=10, maxstep = 1e4)
        #        except ImportError:
        #            from pele.optimize import lbfgs_scipy as quench
        #            ret = quench(X, whampot)

        if self.verbose:
            print "chi^2 went from %g (rms %g) to %g (rms %g) in %d iterations" % (
                E0,
                rms0,
                ret.energy,
                ret.rms,
                ret.nfev,
            )

        # self.logn_Eq = zeros([nebins,nqbins], float64)
        X = ret.coords
        self.logn_Eq = X[nreps:]
        self.w_i_final = X[:nreps]

        self.logn_Eq = np.reshape(self.logn_Eq, [nebins, nqbins])
        self.logn_Eq = np.where(self.visits2d.sum(0) == 0, self.LOGMIN, self.logn_Eq)
Example #11
0
class wham1d:
    """ class to combine 1d histograms of energy E at
    multiple temperatures into one best estimate for the histogram

    input will be:
    filenames : list of filenames where the data can be found
    Tlist # Tlist[k] is the temperature of the simulation in filenames[k] 

    binenergy = zeros(nebins, float64) #lower edge of bin energy
    visits1d =  zeros([nrep,nebins], integer) #1d histograms of data
    """

    #=============================================================================================
    # Constructor.
    #=============================================================================================
    def __init__(self, Tlist, binenergy, visits1d):

        #define some parameters
        self.k_B = 1.

        self.nrep = len(Tlist)
        self.nebins = len(binenergy)
        self.Tlist = np.array(Tlist, dtype=np.float64)
        self.binenergy = np.array(binenergy, dtype=np.float64)
        self.visits1d = np.array(visits1d, dtype=np.integer)

    def minimize(self):
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where(visitsT != 0, np.log(visitsT), 0)
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis, :] / (
            self.Tlist[:, np.newaxis] * self.k_B)

        self.whampot = WhamPotential(self.logP, self.reduced_energy)

        X = np.random.rand(nreps + nbins)
        E = self.whampot.getEnergy(X)
        #print "energy", E

        #print "quenching"
        from wham_utils import lbfgs_scipy
        ret = lbfgs_scipy(X, self.whampot)
        #        try:
        #            from pele.optimize import mylbfgs as quench
        #            ret = quench(X, self.whampot, iprint=-1, maxstep=1e4)
        #        except ImportError:
        #            from pele.optimize import lbfgs_scipy as quench
        #            ret = quench(X, self.whampot)
        #print "quench energy", ret.energy
        X = ret.coords

        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]

#    def calc_Cv_new(self, NDOF, TRANGE=None, NTEMP=100):
#        from pele.thermodynamics import dos_to_cv
#        dT = (self.Tlist[-1] - self.Tlist[0]) / NTEMP
#        Tlist = np.arange(self.Tlist[0], self.Tlist[-1], dT)
##        print self.logn_E
#        lZ, U, U2, Cv = dos_to_cv(self.binenergy, self.logn_E, Tlist, K=NDOF)
#        cvdata = np.zeros([len(Tlist), 6])
#        cvdata[:,0] = Tlist
#        cvdata[:,1] = lZ
#        cvdata[:,2] = U # average potential energy
#        cvdata[:,3] = U2
#        cvdata[:,5] = Cv
#
#        eavg = U + float(NDOF) / 2 * Tlist # average energy including the kinetic degrees of freedom
#        cvdata[:,4] = eavg
#        return cvdata

    def calc_Cv(self, NDOF, TRANGE=None, NTEMP=100, use_log_sum=None):
        #        return self.calc_Cv_new(NDOF, TRANGE, NTEMP)
        return wham_utils.calc_Cv(self.logn_E,
                                  self.visits1d,
                                  self.binenergy,
                                  NDOF,
                                  self.Tlist,
                                  self.k_B,
                                  TRANGE,
                                  NTEMP,
                                  use_log_sum=use_log_sum)
class wham1d:
    """ class to combine 1d histograms of energy E at
    multiple temperatures into one best estimate for the histogram

    input will be:
    filenames : list of filenames where the data can be found
    Tlist # Tlist[k] is the temperature of the simulation in filenames[k] 

    binenergy = zeros(nebins, float64) #lower edge of bin energy
    visits1d =  zeros([nrep,nebins], integer) #1d histograms of data
    """
    #=============================================================================================
    # Constructor.
    #=============================================================================================
    def __init__(self, Tlist, binenergy, visits1d):

        #define some parameters
        self.k_B=1.

        self.nrep = len(Tlist)
        self.nebins = len(binenergy)
        self.Tlist = np.array(Tlist, dtype = np.float64)
        self.binenergy = np.array(binenergy, dtype = np.float64)
        self.visits1d = np.array(visits1d, dtype = np.integer)

    def minimize(self):
        nreps = self.nrep
        nbins = self.nebins
        visitsT = (self.visits1d)
        #print "min vis", np.min(visitsT)
        self.logP = np.where( visitsT != 0, np.log( visitsT ), 0 )
        #print "minlogp", np.min(self.logP)
        self.reduced_energy = self.binenergy[np.newaxis,:] / (self.Tlist[:,np.newaxis] * self.k_B)
        
        self.whampot = WhamPotential(self.logP, self.reduced_energy)
        
        
        X = np.random.rand( nreps + nbins )
        E = self.whampot.getEnergy(X)
        #print "energy", E 
        
        #print "quenching"
        from wham_utils import lbfgs_scipy
        ret = lbfgs_scipy(X, self.whampot)
#        try: 
#            from pele.optimize import mylbfgs as quench
#            ret = quench(X, self.whampot, iprint=-1, maxstep=1e4)
#        except ImportError:
#            from pele.optimize import lbfgs_scipy as quench
#            ret = quench(X, self.whampot)            
        #print "quench energy", ret.energy
        X = ret.coords
        
        self.logn_E = X[nreps:]
        self.w_i_final = X[:nreps]
        

#    def calc_Cv_new(self, NDOF, TRANGE=None, NTEMP=100):
#        from pele.thermodynamics import dos_to_cv
#        dT = (self.Tlist[-1] - self.Tlist[0]) / NTEMP
#        Tlist = np.arange(self.Tlist[0], self.Tlist[-1], dT)
##        print self.logn_E
#        lZ, U, U2, Cv = dos_to_cv(self.binenergy, self.logn_E, Tlist, K=NDOF)
#        cvdata = np.zeros([len(Tlist), 6])
#        cvdata[:,0] = Tlist
#        cvdata[:,1] = lZ
#        cvdata[:,2] = U # average potential energy
#        cvdata[:,3] = U2
#        cvdata[:,5] = Cv
#        
#        eavg = U + float(NDOF) / 2 * Tlist # average energy including the kinetic degrees of freedom
#        cvdata[:,4] = eavg
#        return cvdata
        

    def calc_Cv(self, NDOF, TRANGE=None, NTEMP=100, use_log_sum=None):
#        return self.calc_Cv_new(NDOF, TRANGE, NTEMP)
        return wham_utils.calc_Cv(self.logn_E, self.visits1d, self.binenergy,
                NDOF, self.Tlist, self.k_B, TRANGE, NTEMP, use_log_sum=use_log_sum)