def runtest(X, pot, natoms = 100, iprint=-1): from _lbfgs_py import PrintEvent tol = 1e-5 maxstep = 0.005 Xinit = np.copy(X) e, g = pot.getEnergyGradient(X) print "energy", e lbfgs = LBFGS(X, pot, maxstep = 0.1, nsteps=10000, tol=tol, iprint=iprint, H0=2.) printevent = PrintEvent( "debugout.xyz") lbfgs.attachEvent(printevent) ret = lbfgs.run() print ret print "" print "now do the same with scipy lbfgs" from pele.optimize import lbfgs_scipy as quench ret = quench(Xinit, pot, tol = tol) print ret #print ret[1], ret[2], ret[3] if False: print "now do the same with scipy bfgs" from pele.optimize import bfgs as oldbfgs ret = oldbfgs(Xinit, pot, tol = tol) print ret if False: print "now do the same with gradient + linesearch" import _bfgs gpl = _bfgs.GradientPlusLinesearch(Xinit, pot, maxstep = 0.1) ret = gpl.run(1000, tol = 1e-6) print ret if False: print "calling from wrapper function" from pele.optimize import lbfgs_py ret = lbfgs_py(Xinit, pot, tol = tol) print ret if True: print "" print "now do the same with lbfgs_py" from pele.optimize import lbfgs_py ret = lbfgs_py(Xinit, pot, tol = tol) print ret try: import pele.utils.pymolwrapper as pym pym.start() for n, coords in enumerate(printevent.coordslist): coords=coords.reshape(natoms, 3) pym.draw_spheres(coords, "A", n) except ImportError: print "error loading pymol"
def test(natoms=40, boxl=4.0): import pele.potentials.ljpshiftfast as ljpshift from pele.optimize import mylbfgs from pele.utils.neighbor_list import makeBLJNeighborListPot ntypeA = int(natoms * 0.8) ntypeB = natoms - ntypeA rcut = 2.5 freezelist = range(ntypeA / 2) + range(ntypeA, ntypeA + ntypeB / 2) nfrozen = len(freezelist) print "nfrozen", nfrozen coords = np.random.uniform(-1, 1, natoms * 3) * (natoms) ** (1.0 / 3) / 2 NLblj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) blj = FreezePot(NLblj, freezelist, natoms) pot = makeBLJNeighborListPotFreeze(natoms, freezelist, ntypeA=ntypeA, rcut=rcut, boxl=boxl) # pot = FreezePot(NLpot, freezelist) eblj = blj.getEnergy(coords) print "blj energy", eblj epot = pot.getEnergy(coords) print "mcpot energy", epot print "difference", (epot - eblj) / eblj pot.test_potential(coords) print "\n" ret1 = mylbfgs(coords, blj, iprint=-11) np.savetxt("out.coords", ret1.coords) print "energy from quench1", ret1.energy ret2 = mylbfgs(coords, pot, iprint=-1) print "energy from quench2", ret2.energy print "ret1 evaluated in both potentials", pot.getEnergy(ret1.coords), blj.getEnergy(ret1.coords) print "ret2 evaluated in both potentials", pot.getEnergy(ret2.coords), blj.getEnergy(ret2.coords) coords = ret1.coords e1, g1 = blj.getEnergyGradient(coords) e2, g2 = pot.getEnergyGradient(coords) print "energy difference from getEnergyGradient", (e2 - e1) print "largest gradient difference", np.max(np.abs(g2 - g1)) print "rms gradients", np.linalg.norm(g1) / np.sqrt(len(g1)), np.linalg.norm(g2) / np.sqrt(len(g1)) if True: for subpot in pot.pot.potentials: nl = subpot print "number of times neighbor list was remade:", nl.buildcount, "out of", nl.count if False: try: import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(np.reshape(coords, [-1, 3]), "A", 1) pym.draw_spheres(np.reshape(ret1.coords, [-1, 3]), "A", 2) pym.draw_spheres(np.reshape(ret2.coords, [-1, 3]), "A", 3) except ImportError: print "Could not draw using pymol, skipping this step"
def test(natoms=40, boxl=4.): # pragma: no cover import pele.potentials.ljpshiftfast as ljpshift from pele.optimize import mylbfgs from pele.utils.neighbor_list import makeBLJNeighborListPot ntypeA = int(natoms * 0.8) ntypeB = natoms - ntypeA rcut = 2.5 freezelist = list(range(old_div(ntypeA, 2))) + list(range(ntypeA, ntypeA + old_div(ntypeB, 2))) nfrozen = len(freezelist) print("nfrozen", nfrozen) coords = old_div(np.random.uniform(-1, 1, natoms * 3) * natoms ** (1. / 3), 2) NLblj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) blj = FreezePot(NLblj, freezelist, natoms) pot = makeBLJNeighborListPotFreeze(natoms, freezelist, ntypeA=ntypeA, rcut=rcut, boxl=boxl) eblj = blj.getEnergy(coords) print("blj energy", eblj) epot = pot.getEnergy(coords) print("mcpot energy", epot) print("difference", old_div((epot - eblj), eblj)) pot.test_potential(coords) print("\n") ret1 = mylbfgs(coords, blj, iprint=-11) np.savetxt("out.coords", ret1.coords) print("energy from quench1", ret1.energy) ret2 = mylbfgs(coords, pot, iprint=-1) print("energy from quench2", ret2.energy) print("ret1 evaluated in both potentials", pot.getEnergy(ret1.coords), blj.getEnergy(ret1.coords)) print("ret2 evaluated in both potentials", pot.getEnergy(ret2.coords), blj.getEnergy(ret2.coords)) coords = ret1.coords e1, g1 = blj.getEnergyGradient(coords) e2, g2 = pot.getEnergyGradient(coords) print("energy difference from getEnergyGradient", (e2 - e1)) print("largest gradient difference", np.max(np.abs(g2 - g1))) print("rms gradients", old_div(np.linalg.norm(g1), np.sqrt(len(g1))), old_div(np.linalg.norm(g2), np.sqrt(len(g1)))) if True: for subpot in pot.pot.potentials: nl = subpot print("number of times neighbor list was remade:", nl.buildcount, "out of", nl.count) if False: try: import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(np.reshape(coords, [-1, 3]), "A", 1) pym.draw_spheres(np.reshape(ret1.coords, [-1, 3]), "A", 2) pym.draw_spheres(np.reshape(ret2.coords, [-1, 3]), "A", 3) except ImportError: print("Could not draw using pymol, skipping this step")
def takeStep(self, coords, **kwargs): # make a new monte carlo class mc = MonteCarlo(coords, self.potential, self.mcstep, temperature=self.T, outstream=None) mc.run(self.nsteps) coords[:] = mc.coords[:] def updateStep(self, acc, **kwargs): pass natoms = 12 # random initial coordinates coords = np.random.random(3 * natoms) potential = lj.LJ() step = TakeStepMonteCarlo(potential) opt = bh.BasinHopping(coords, potential, takeStep=step) opt.run(100) # some visualization try: import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(opt.coords, "A", 1) except: print("Could not draw using pymol, skipping this step")
def takeStep(self, coords, **kwargs): # make a new monte carlo class mc = MonteCarlo(coords, self.potential, self.mcstep, temperature=self.T, outstream=None) mc.run(self.nsteps) coords[:] = mc.coords[:] def updateStep(self, acc, **kwargs): pass natoms = 12 # random initial coordinates coords = np.random.random(3 * natoms) potential = lj.LJ() step = TakeStepMonteCarlo(potential) opt = bh.BasinHopping(coords, potential, takeStep=step) opt.run(100) # some visualization try: import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(opt.coords, "A", 1) except: print "Could not draw using pymol, skipping this step"
def draw_pymol(coords): import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(coords, "A", 1)
potential = lj.LJ() step = displace.RandomDisplacement(stepsize=0.5) storage = saveit(nsave=10) opt = bh.BasinHopping(coords, potential, step, storage=storage.insert) opt.run(100) with open("lowest", "w") as fout: fout.write(str(natoms) + "\n") fout.write(str(storage.data[0].energy) + "\n") atoms = storage.data[0].coords.reshape(natoms, 3) for a in atoms: fout.write("LA " + str(a[0]) + " " + str(a[1]) + " " + str(a[2]) + "\n") ############################################################ # some visualization ############################################################ try: import pele.utils.pymolwrapper as pym pym.start() frame = 1 print storage.data for minimum in storage.data: coords = minimum.coords.reshape(natoms, 3) pym.draw_spheres(coords, "A", frame) frame += 1 except: print "Could not draw using pymol, skipping this step"
""" Example 1: Simple basin hopping """ from pele.systems import LJCluster natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() bh = system.get_basinhopping(database=db) bh.run(niter) print "the lowest energy found after", niter, " basinhopping steps is", db.minima()[0].energy print "" # some visualization try: import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(bh.coords, "A", 1) except: print "Could not draw using pymol, skipping this step"
from __future__ import print_function from builtins import str from pele.systems import LJCluster from pele.utils.xyz import write_xyz natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() bh = system.get_basinhopping(database=db) bh.run(niter) with open("lowest", "w") as fout: for minimum in db.minima(): title = "energy = ", str(minimum.energy) write_xyz(fout, minimum.coords, title) ############################################################ # some visualization ############################################################ try: import pele.utils.pymolwrapper as pym pym.start() frame = 1 for minimum in db.minima(): pym.draw_spheres(minimum.coords.reshape(-1, 3), "A", frame) frame += 1 except: print("Could not draw using pymol, skipping this step")
""" Example 1: Simple basin hopping """ from __future__ import print_function from pele.systems import LJCluster natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() bh = system.get_basinhopping(database=db) bh.run(niter) print("the lowest energy found after", niter, " basinhopping steps is", db.minima()[0].energy) print("") # some visualization try: import pele.utils.pymolwrapper as pym pym.start() pym.draw_spheres(bh.coords, "A", 1) except: print("Could not draw using pymol, skipping this step")
Example 3: Saving all minima found to an xyz file """ from pele.systems import LJCluster from pele.utils.xyz import write_xyz natoms = 12 niter = 100 system = LJCluster(natoms) db = system.create_database() bh = system.get_basinhopping(database=db) bh.run(niter) with open("lowest", "w") as fout: for minimum in db.minima(): title = "energy = ", str(minimum.energy) write_xyz(fout, minimum.coords, title) ############################################################ # some visualization ############################################################ try: import pele.utils.pymolwrapper as pym pym.start() frame=1 for minimum in db.minima(): pym.draw_spheres(minimum.coords.reshape(-1, 3), "A", frame) frame=frame+1 except: print "Could not draw using pymol, skipping this step"