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 setUp1(self, verbose=False, **kwargs): np.random.seed(0) natoms = 18 self.system = LJCluster(natoms) self.pot = self.system.get_potential() x = self.system.get_random_configuration() ret = lbfgs_py(x, self.pot, tol=10) self.x = ret.coords self.kwargs = kwargs self.verbose = verbose self.M = 4 if self.verbose: iprint = 1 else: iprint = -1 self.myo = MYLBFGS(self.x, self.pot, iprint=iprint, debug=True, M=self.M) self.o = LBFGS(self.x, self.pot, iprint=iprint, debug=True, M=self.M, **self.kwargs)
def setUp1(self, verbose=False, **kwargs): np.random.seed(0) natoms = 18 self.system = LJCluster(natoms) self.pot = self.system.get_potential() x = self.system.get_random_configuration() ret = lbfgs_py(x, self.pot, tol=10) self.x = ret.coords self.kwargs = kwargs self.verbose = verbose self.M = 4 if self.verbose: iprint=1 else: iprint = -1 self.myo = MYLBFGS(self.x, self.pot, iprint=iprint, debug=True, M=self.M) self.o = LBFGS(self.x, self.pot, iprint=iprint, debug=True, M=self.M, **self.kwargs)
def test(): system = NNSystem() t = system.get_potential() print "get_energy_gradient" newparams = np.random.uniform(-.05, .05, t.nparams) e, g = t.getEnergyGradient(newparams) print "cost", e print "\n\nagain\nget_energy_gradient" newparams = np.random.uniform(-.05, .05, t.nparams) e, g = t.getEnergyGradient(newparams) print "cost", e params = t.get_params() print params dx = np.max(np.abs(params - newparams)) print dx assert dx < 1e-8 # do minimization from pele.optimize import lbfgs_py res = lbfgs_py(newparams, t, iprint=10, tol=1e-4) print res
between two lennard jones clusters """ from __future__ import print_function from builtins import range import numpy as np from pele.potentials.lj import LJ from pele.optimize import lbfgs_py from pele.mindist import MinPermDistAtomicCluster pot = LJ() natoms = 40 # get two random quenched structures to compare coords1 = np.random.rand(natoms * 3) * natoms ** (1. / 3) * 1.5 coords2 = np.random.rand(natoms * 3) * natoms ** (1. / 3) * 1.5 ret1 = lbfgs_py(coords1, pot) ret2 = lbfgs_py(coords2, pot) coords1 = ret1.coords coords2 = ret2.coords # all the atoms are permutable permlist = [list(range(natoms))] mindist = MinPermDistAtomicCluster(niter=100, permlist=permlist, verbose=False) dist, newcoords1, newcoords2 = mindist(coords1, coords2) print("") print("dist =", dist)
for rb in rb_sites: rb.finalize_setup() # define a new rigid body system rbsystem = rigidbody.RBSystem() rbsystem.add_sites(rb_sites) print(len(rbsystem.sites), len(rbsystem.indices)) rbcoords = rbsystem.coords_adapter(np.zeros(len(rbsystem.sites) * 6)) for site, com in zip(rbsystem.sites, rbcoords.posRigid): com[:] = ref.coords[site.indices[0]] - site.atom_positions[0] # for simplicity just use a lj potential here pot = LJ(sigma=2.) # get the flattened coordinate array print(pot.getEnergy(ref.coords.flatten())) rbpot = rigidbody.RBPotentialWrapper(rbsystem, pot) print(rbpot.getEnergy(rbcoords.coords)) e, g = rbpot.getEnergyGradient(rbcoords.coords) g_n = rbpot.NumericalDerivative(rbcoords.coords, eps=1e-4) cg = rbsystem.coords_adapter(g - g_n) print(cg.posRigid) print(cg.rotRigid) ret = lbfgs_py(rbcoords.coords, rbpot) print(ret.energy) xyz.write_xyz(open("quenched.xyz", "w"), rbsystem.to_atomistic(ret.coords), atomtypes=ref.atomtypes)
In the above we used three imports. We used the `numpy` library to construct a random one dimensional array. We used the Lennard-Jones potential :class:`.LJ`, and we used the minimization routine :func:`.lbfgs_py` which is just a wrapper for the class :class:`.LBFGS`. The return value is an optimization result, which is just a container (:class:`.Result`) that stores the final energy, final coordinates, the number of function calls, etc. If we want to then save the minimized coordinates in an xyz file we can use the function :func:`.write_xyz` :: from pele.utils.xyz import write_xyz with open("out.xyz", "w") as fout: title = "energy = " + str(result.energy) write_xyz(fout, result.coords, title=title) """ import numpy as np from pele.potentials import LJ from pele.optimize import lbfgs_py natoms = 5 x = np.random.uniform(-2, 2, natoms * 3) pot = LJ() result = lbfgs_py(x, pot) print result from pele.utils.xyz import write_xyz with open("out.xyz", "w") as fout: title = "energy = " + str(result.energy) write_xyz(fout, result.coords, title=title)
# finalize the rigid body setup for rb in rb_sites: rb.finalize_setup() # define a new rigid body system rbsystem = rigidbody.RBSystem() rbsystem.add_sites(rb_sites) print len(rbsystem.sites), len(rbsystem.indices) rbcoords = rbsystem.coords_adapter(np.zeros(len(rbsystem.sites)*6)) for site, com in zip(rbsystem.sites, rbcoords.posRigid): com[:] = ref.coords[site.indices[0]] - site.atom_positions[0] # for simplicity just use a lj potential here pot = LJ(sigma=2.) # get the flattened coordinate array print pot.getEnergy(ref.coords.flatten()) rbpot = rigidbody.RBPotentialWrapper(rbsystem, pot) print rbpot.getEnergy(rbcoords.coords) e, g = rbpot.getEnergyGradient(rbcoords.coords) g_n = rbpot.NumericalDerivative(rbcoords.coords, eps=1e-4) cg = rbsystem.coords_adapter(g-g_n) print cg.posRigid print cg.rotRigid ret = lbfgs_py(rbcoords.coords, rbpot) print ret.energy xyz.write_xyz(open("quenched.xyz", "w"), rbsystem.to_atomistic(ret.coords), atomtypes=ref.atomtypes)
In the above we used three imports. We used the `numpy` library to construct a random one dimensional array. We used the Lennard-Jones potential :class:`.LJ`, and we used the minimization routine :func:`.lbfgs_py` which is just a wrapper for the class :class:`.LBFGS`. The return value is an optimization result, which is just a container (:class:`.Result`) that stores the final energy, final coordinates, the number of function calls, etc. If we want to then save the minimized coordinates in an xyz file we can use the function :func:`.write_xyz` :: from pele.utils.xyz import write_xyz with open("out.xyz", "w") as fout: title = "energy = " + str(result.energy) write_xyz(fout, result.coords, title=title) """ import numpy as np from pele.potentials import LJ from pele.optimize import lbfgs_py natoms = 5 x = np.random.uniform(-2, 2, natoms*3) pot = LJ() result = lbfgs_py(x, pot) print result from pele.utils.xyz import write_xyz with open("out.xyz", "w") as fout: title = "energy = " + str(result.energy) write_xyz(fout, result.coords, title=title)
""" an example for finding the minimum distance and best alignment between two lennard jones clusters """ import numpy as np from pele.potentials.lj import LJ from pele.optimize import lbfgs_py from pele.mindist import MinPermDistAtomicCluster pot = LJ() natoms = 40 # get two random quenched structures to compare coords1 = np.random.rand(natoms * 3) * natoms ** (1. / 3) * 1.5 coords2 = np.random.rand(natoms * 3) * natoms ** (1. / 3) * 1.5 ret1 = lbfgs_py(coords1, pot) ret2 = lbfgs_py(coords2, pot) coords1 = ret1.coords coords2 = ret2.coords # all the atoms are permutable permlist = [range(natoms)] mindist = MinPermDistAtomicCluster(niter=100, permlist=permlist, verbose=False) dist, newcoords1, newcoords2 = mindist(coords1, coords2) print "" print "dist =", dist
from pele.potentials.lj import LJ from pele.optimize import lbfgs_py from pele.landscape import DoubleEndedConnect, smoothPath from pele.mindist import MinPermDistAtomicCluster from pele.transition_states import orthogopt from pele.storage import Database, Minimum from pele.utils.xyz import write_xyz np.random.seed(0) #set up the potential pot = LJ() #import the starting and ending points and quench them, coords1 = np.genfromtxt("coords.A") coords2 = np.genfromtxt("coords.B") res1 = lbfgs_py(coords1.reshape(-1), pot) res2 = lbfgs_py(coords2.reshape(-1), pot) coords1 = res1.coords coords2 = res2.coords E1 = res1.energy E2 = res2.energy natoms = len(coords1)/3 #add the minima to a database dbfile = "database.sqlite" database = Database(dbfile) database.addMinimum(E1, coords1) database.addMinimum(E2, coords2) min1 = database.minima()[0] min2 = database.minima()[1]
from pele.optimize import lbfgs_py from pele.landscape import DoubleEndedConnect, smoothPath from pele.mindist import MinPermDistAtomicCluster from pele.transition_states import orthogopt from pele.storage import Database from pele.utils.xyz import write_xyz np.random.seed(0) # set up the potential pot = LJ() # import the starting and ending points and quench them, coords1 = np.genfromtxt("coords.A") coords2 = np.genfromtxt("coords.B") res1 = lbfgs_py(coords1.reshape(-1), pot) res2 = lbfgs_py(coords2.reshape(-1), pot) coords1 = res1.coords coords2 = res2.coords E1 = res1.energy E2 = res2.energy natoms = len(coords1) / 3 # add the minima to a database dbfile = "database.sqlite" database = Database(dbfile) database.addMinimum(E1, coords1) database.addMinimum(E2, coords2) min1 = database.minima()[0] min2 = database.minima()[1]