def setUp(self): from pygmin.potentials.rigid_bodies.molecule import Molecule, setupLWOTP from pygmin.potentials.rigid_bodies.sandbox import RBSandbox from pygmin.potentials.lj import LJ from pygmin.optimize import lbfgs_py as quench #set up system nmol = 5 self.nmol = nmol otp = setupLWOTP() #set up a list of molecules mols = [otp for i in range(nmol)] # define the interaction matrix for the system. # for LWOTP there is only one atom type, so this is trivial lj = LJ() interaction_matrix = [[lj]] #set up the RBSandbox object mysys = RBSandbox(mols, interaction_matrix) self.pot = mysys self.nsites = mysys.nsites self.permlist = [range(nmol)] self.coords1 = testmindist.randomCoordsAA(nmol) ret = quench(self.coords1, self.pot.getEnergyGradient) self.coords1 = ret[0]
def dumbbell(sig1=0.35, sig2=0.65): """ return a dumbell molecule. i.e. two lj sites attached rigidly """ pos1 = [0., 0., 0.5] pos2 = [0., 0., -0.5] dbel = Molecule() dbel.insert_site(0, pos1) dbel.insert_site(1, pos2) dbel.correctCoM() from pygmin.potentials.lj import LJ lj1 = LJ(sig=2. * sig1) lj2 = LJ(sig=2. * sig2) lj12 = LJ(sig=sig1 + sig2) interaction_matrix = [[lj1, lj12], [lj12, lj2]] return dbel, interaction_matrix
def test(): natoms = 100 tol = 1e-6 from pygmin.potentials.lj import LJ pot = LJ() X = getInitialCoords(natoms, pot) X += np.random.uniform(-1,1,[3*natoms]) * 0.3 #do some steepest descent steps so we don't start with a crazy structure #from optimize.quench import _steepest_descent as steepestDescent #ret = steepestDescent(X, pot.getEnergyGradient, iprint = 1, dx = 1e-4, nsteps = 100, gtol = 1e-3, maxstep = .5) #X = ret[0] #print X Xinit = np.copy(X) e, g = pot.getEnergyGradient(X) print "energy", e lbfgs = BFGS(X, pot, maxstep = 0.1) ret = lbfgs.run(100, tol = tol, iprint=1) print "done", ret[1], ret[2], ret[3] print "now do the same with scipy lbfgs" from pygmin.optimize import lbfgs_scipy as quench ret = quench(Xinit, pot.getEnergyGradient, tol = tol) print ret[1], ret[2], ret[3] print "now do the same with scipy bfgs" from pygmin.optimize import bfgs as oldbfgs ret = oldbfgs(Xinit, pot.getEnergyGradient, tol = tol) print ret[1], ret[2], ret[3] print "now do the same with old gradient + linesearch" gpl = GradientPlusLinesearch(Xinit, pot, maxstep = 0.1) ret = gpl.run(100, tol = 1e-6) print ret[1], ret[2], ret[3]
def test_LJ(natoms=12, **kwargs): from pygmin.potentials.lj import LJ from pygmin.optimize import mylbfgs import pygmin.utils.rotations as rot from pygmin.mindist.permutational_alignment import permuteArray import random quench = mylbfgs lj = LJ() X1 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3) #quench X1 ret = quench(X1, lj) X1 = ret.coords X2 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3) #make X2 a rotation of X1 print "testing with", natoms, "atoms, with X2 a rotated and permuted isomer of X1" aa = rot.random_aa() rot_mx = rot.aa2mx(aa) for j in range(natoms): i = 3 * j X2[i:i + 3] = np.dot(rot_mx, X1[i:i + 3]) perm = range(natoms) random.shuffle(perm) print perm X2 = permuteArray(X2, perm) #X1 = np.array( [ 0., 0., 0., 1., 0., 0., 0., 0., 1.,] ) #X2 = np.array( [ 0., 0., 0., 1., 0., 0., 0., 1., 0.,] ) import copy X1i = copy.copy(X1) X2i = copy.copy(X2) print "******************************" print "testing normal LJ ISOMER" print "******************************" test(X1, X2, lj, **kwargs) print "******************************" print "testing normal LJ non isomer" print "******************************" X2 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3) ret = quench(X2, lj) X2 = ret.coords Y = X1.reshape([-1, 3]) Y += np.random.random(3) X1[:] = Y.flatten() test(X1, X2, lj, **kwargs) distinit = np.linalg.norm(X1 - X2) print "distinit", distinit
def test_LWOTP(nmol=5): from pygmin.potentials.rigid_bodies.molecule import Molecule, setupLWOTP from pygmin.potentials.rigid_bodies.sandbox import RBSandbox from pygmin.potentials.lj import LJ from pygmin.optimize import lbfgs_py as quench printlist = [] #set up system otp = setupLWOTP() #set up a list of molecules mols = [otp for i in range(nmol)] # define the interaction matrix for the system. # for LWOTP there is only one atom type, so this is trivial lj = LJ() interaction_matrix = [[lj]] #set up the RBSandbox object mysys = RBSandbox(mols, interaction_matrix) nsites = mysys.nsites permlist = [range(nmol)] #define initial coords coords1 = randomCoords(nmol) printlist.append((coords1.copy(), "very first")) #quench X1 ret = quench(coords1, mysys.getEnergyGradient) coords1 = ret[0] #define initial coords2 coords2 = randomCoords(nmol) printlist.append((coords2.copy(), "very first")) #quench X2 ret = quench(coords2, mysys.getEnergyGradient) coords2 = ret[0] coords2in = coords2.copy() printlist.append((coords1.copy(), "after quench")) printlist.append((coords2.copy(), "after quench")) print "******************************" print "testing for OTP not isomer" print "******************************" d, c1new, c2new = test(coords1, coords2, mysys, permlist) print "******************************" print "testing for OTP ISOMER" print "******************************" coords1 = coords2in.copy() coords2 = c2new.copy() #try to reverse the permutations and symmetry operations we just applied test(coords1, coords2, mysys, permlist)
def testpot2(): from pygmin.potentials.lj import LJ import itertools pot = LJ() a = 1.12 #2.**(1./6.) theta = 20. / 360 * np.pi coords = [ 0., 0., 0., \ -a, 0., 0., \ a*np.cos(theta), a*np.sin(theta), 0. ] c = np.reshape(coords, [3, 3]) for i, j in itertools.combinations(range(3), 2): r = np.linalg.norm(c[i, :] - c[j, :]) print i, j, r
def test(): natoms = 100 tol = 1e-6 from pygmin.potentials.lj import LJ pot = LJ() X = getInitialCoords(natoms, pot) X += np.random.uniform(-1, 1, [3 * natoms]) * 0.3 #do some steepest descent steps so we don't start with a crazy structure #from optimize.quench import _steepest_descent as steepestDescent #ret = steepestDescent(X, pot.getEnergyGradient, iprint = 1, dx = 1e-4, nsteps = 100, gtol = 1e-3, maxstep = .5) #X = ret[0] #print X Xinit = np.copy(X) e, g = pot.getEnergyGradient(X) print "energy", e lbfgs = BFGS(X, pot, maxstep=0.1) ret = lbfgs.run(100, tol=tol, iprint=1) print "done", ret[1], ret[2], ret[3] print "now do the same with scipy lbfgs" from pygmin.optimize import lbfgs_scipy as quench ret = quench(Xinit, pot.getEnergyGradient, tol=tol) print ret[1], ret[2], ret[3] print "now do the same with scipy bfgs" from pygmin.optimize import bfgs as oldbfgs ret = oldbfgs(Xinit, pot.getEnergyGradient, tol=tol) print ret[1], ret[2], ret[3] print "now do the same with old gradient + linesearch" gpl = GradientPlusLinesearch(Xinit, pot, maxstep=0.1) ret = gpl.run(100, tol=1e-6) print ret[1], ret[2], ret[3]
def getSetOfMinLJ(natoms = 11): #for testing purposes from pygmin.potentials.lj import LJ pot = LJ() coords = np.random.uniform(-1,1,natoms*3) from pygmin.basinhopping import BasinHopping from pygmin.takestep.displace import RandomDisplacement from pygmin.takestep.adaptive import AdaptiveStepsize from pygmin.storage.savenlowest import SaveN saveit = SaveN(10) takestep1 = RandomDisplacement() takestep = AdaptiveStepsize(takestep1, frequency=15) bh = BasinHopping(coords, pot, takestep, storage=saveit, outstream=None) bh.run(100) return pot, saveit
def test_LJ(natoms=12, **kwargs): from pygmin.potentials.lj import LJ import pygmin.defaults import pygmin.utils.rotations as rot from pygmin.mindist.permutational_alignment import permuteArray import random quench = pygmin.defaults.quenchRoutine lj = LJ() X1 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3) #quench X1 ret = quench(X1, lj.getEnergyGradient) X1 = ret[0] X2 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3) #make X2 a rotation of X1 print "testing with", natoms, "atoms, with X2 a rotated and permuted isomer of X1" aa = rot.random_aa() rot_mx = rot.aa2mx(aa) for j in range(natoms): i = 3 * j X2[i:i + 3] = np.dot(rot_mx, X1[i:i + 3]) perm = range(natoms) random.shuffle(perm) print perm X2 = permuteArray(X2, perm) #X1 = np.array( [ 0., 0., 0., 1., 0., 0., 0., 0., 1.,] ) #X2 = np.array( [ 0., 0., 0., 1., 0., 0., 0., 1., 0.,] ) import copy X1i = copy.copy(X1) X2i = copy.copy(X2) print "******************************" print "testing normal LJ ISOMER" print "******************************" test(X1, X2, lj, **kwargs) print "******************************" print "testing normal LJ non isomer" print "******************************" X2 = np.random.uniform(-1, 1, [natoms * 3]) * (float(natoms))**(1. / 3) ret = quench(X2, lj.getEnergyGradient) X2 = ret[0] test(X1, X2, lj, **kwargs) distinit = np.linalg.norm(X1 - X2) print "distinit", distinit
def guesstsLJ(): from pygmin.potentials.lj import LJ pot = LJ() natoms = 9 coords = np.random.uniform(-1, 1, natoms * 3) from pygmin.basinhopping import BasinHopping from pygmin.takestep.displace import RandomDisplacement from pygmin.takestep.adaptive import AdaptiveStepsize from pygmin.storage.savenlowest import SaveN saveit = SaveN(10) takestep1 = RandomDisplacement() takestep = AdaptiveStepsize(takestep1, frequency=15) bh = BasinHopping(coords, pot, takestep, storage=saveit, outstream=None) bh.run(100) coords1 = saveit.data[0].coords coords2 = saveit.data[1].coords return guessts(coords1, coords2, pot)
def testpot1(): from pygmin.potentials.lj import LJ import itertools pot = LJ() a = 1.12 #2.**(1./6.) theta = 60. / 360 * np.pi coords = [ 0., 0., 0., \ -a, 0., 0., \ -a/2, a*np.cos(theta), 0., \ -a/2, -a*np.cos(theta), 0.1 \ ] natoms = len(coords) / 3 c = np.reshape(coords, [-1, 3]) for i, j in itertools.combinations(range(natoms), 2): r = np.linalg.norm(c[i, :] - c[j, :]) print i, j, r e, g = pot.getEnergyGradient(coords) print "initial E", e print "initial G", g, np.linalg.norm(g) eigpot = LowestEigPot(coords, pot) vec = np.random.rand(len(coords)) e, g = eigpot.getEnergyGradient(vec) print "eigenvalue", e print "eigenvector", g if True: e, g, hess = pot.getEnergyGradientHessian(coords) print "shape hess", np.shape(hess) print "hessian", hess u, v = np.linalg.eig(hess) print "max imag value", np.max(np.abs(u.imag)) print "max imag vector", np.max(np.abs(v.imag)) u = u.real v = v.real print "eigenvalues", u for i in range(len(u)): print "eigenvalue", u[i], "eigenvector", v[:, i] #find minimum eigenvalue, vector imin = 0 umin = 10. for i in range(len(u)): if np.abs(u[i]) < 1e-10: continue if u[i] < umin: umin = u[i] imin = i print "lowest eigenvalue ", umin, imin print "lowest eigenvector", v[:, imin] from pygmin.optimize import lbfgs_py as quench ret = quench(vec, eigpot.getEnergyGradient, iprint=10, tol = 1e-5, maxstep = 1e-3, \ rel_energy = True) print ret print "lowest eigenvalue " print umin, imin print "lowest eigenvector" print v[:, imin] print "now the estimate" print ret[1] print ret[0]
# from potentials.soft_sphere import SoftSphere, putInBox from pygmin.potentials.lj import LJ as SoftSphere np.random.seed(0) natoms = 120 rho = 1.6 boxl = 1.0 meandiam = boxl / (float(natoms) / rho) ** (1.0 / 3) print "mean diameter", meandiam # set up potential # diams = np.array([meandiam for i in range(natoms)]) #make them all the same # pot = SoftSphere(diams = diams) pot = SoftSphere() # initial coordinates coords = np.random.uniform(-1, 1, [natoms * 3]) * (natoms) ** (1.0 / 3) E = pot.getEnergy(coords) print "initial energy", E printlist = [] # list of coordinates saved for printing printlist.append((coords.copy(), "intial coords")) # test a quench with default lbfgs # from optimize.quench import quench from pygmin.optimize import lbfgs_ase as quench
def test_sandbox(nmol=6): import copy from pygmin.potentials.lj import LJ #define the molecule types. #here use only one type, LWOTP otp = molecule.setupLWOTP() # define the interaction matrix for the system. # for LWOTP there is only one atom type, so this is trivial lj = LJ() interaction_matrix = [[lj]] #set up a list of molecules mols = [otp for i in range(nmol)] #set up the RBSandbox object mysys = RBSandbox(mols, interaction_matrix) nsites = mysys.nsites #get an initial set of coordinates comcoords = np.random.uniform(-1, 1, [nmol * 3]) * 1.3 * (nsites)**(1. / 3) aacoords = np.array([copy.copy(rot.random_aa()) for i in range(nmol)]) aacoords = aacoords.reshape(3 * nmol) coords = np.zeros(2 * 3 * nmol, np.float64) coords[0:3 * nmol] = comcoords[:] coords[3 * nmol:2 * 3 * nmol] = aacoords[:] print "lencoords, len aacoords", len(coords), len(aacoords), len(comcoords) #print "xyz coords", mysys.transformToXYZ(coords) #save the initial set of coords printlist = [] xyz = mysys.getxyz(coords) printlist.append((xyz.copy(), "initial")) #calculate the initial energy Einit = mysys.getEnergy(coords) print "initial energy", Einit #test the gradient numericV = mysys.NumericalDerivative(coords, 1e-10) numericV = numericV.copy() print "numeric V", numericV E, V = mysys.getEnergyGradient(coords) print "energy from gradient", E, "difference", E - Einit #print "analytic gradient", V maxgrad_relative = np.max(np.abs(V - numericV) / np.abs(V)) maxgraddiff = np.max(np.abs(V - numericV)) print "max error in gradient", maxgraddiff, "max relative", maxgrad_relative #do a quench to make sure everything is working well from pygmin.optimize import lbfgs_scipy coords, E, rms, funcalls = lbfgs_scipy(coords, mysys.getEnergyGradient, iprint=-1) print "postquench E", E, "rms", rms, "funtion calls", funcalls xyz = mysys.getxyz(coords) printlist.append((xyz.copy(), "post quench")) #print the saved coords fname = "otp.xyz" print "saving xyz coords to", fname from pygmin.printing.print_atoms_xyz import printAtomsXYZ as printxyz with open(fname, "w") as fout: for xyz, line2 in printlist: printxyz(fout, xyz, line2=line2, atom_type=["N", "O", "O"]) test_symmetries(coords, mysys)
def testpot1(): from pygmin.potentials.lj import LJ import itertools pot = LJ() a = 1.12 #2.**(1./6.) theta = 60./360*np.pi coords = [ 0., 0., 0., \ -a, 0., 0., \ -a/2, a*np.cos(theta), 0., \ -a/2, -a*np.cos(theta), 0.1 \ ] natoms = len(coords)/3 c = np.reshape(coords, [-1,3]) for i, j in itertools.combinations(range(natoms), 2): r = np.linalg.norm(c[i,:] - c[j,:]) print i, j, r e, g = pot.getEnergyGradient(coords) print "initial E", e print "initial G", g, np.linalg.norm(g) eigpot = LowestEigPot(coords, pot) vec = np.random.rand(len(coords)) e, g = eigpot.getEnergyGradient(vec) print "eigenvalue", e print "eigenvector", g if True: e, g, hess = pot.getEnergyGradientHessian(coords) print "shape hess", np.shape(hess) print "hessian", hess u, v = np.linalg.eig(hess) print "max imag value", np.max(np.abs(u.imag)) print "max imag vector", np.max(np.abs(v.imag)) u = u.real v = v.real print "eigenvalues", u for i in range(len(u)): print "eigenvalue", u[i], "eigenvector", v[:,i] #find minimum eigenvalue, vector imin = 0 umin = 10. for i in range(len(u)): if np.abs(u[i]) < 1e-10: continue if u[i] < umin: umin = u[i] imin = i print "lowest eigenvalue ", umin, imin print "lowest eigenvector", v[:,imin] from pygmin.optimize import lbfgs_py as quench ret = quench(vec, eigpot.getEnergyGradient, iprint=10, tol = 1e-5, maxstep = 1e-3, \ rel_energy = True) print ret print "lowest eigenvalue " print umin, imin print "lowest eigenvector" print v[:,imin] print "now the estimate" print ret[1] print ret[0]
""" import numpy as np import os from pygmin.potentials.lj import LJ from pygmin.optimize import lbfgs_py from pygmin.landscape import DoubleEndedConnect, smoothPath from pygmin.mindist import MinPermDistAtomicCluster from pygmin.transition_states import orthogopt from pygmin.storage import Database, Minimum from pygmin.printing import printAtomsXYZ 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)
def printwrapper(self, E, coords, accepted): if self.count % self.printfrq == 0: self.center(coords) printxyz(self.fout, coords, line2=str(E)) self.count += 1 #def center(E, coords, accepted): # c = np.reshape() natoms = 40 coords = np.random.rand(natoms * 3) lj = LJ() ret = quench(coords, lj) coords = ret.coords takestep = TakeStep(stepsize=0.1) #do equilibration steps, adjusting stepsize tsadapt = AdaptiveStepsize(takestep) mc = MonteCarlo(coords, lj, tsadapt, temperature=0.5) equilout = open("equilibration", "w") #mc.outstream = equilout mc.setPrinting(equilout, 1) mc.run(10000) #fix stepsize and do production run
import numpy as np #from potentials.soft_sphere import SoftSphere, putInBox from pygmin.potentials.lj import LJ as SoftSphere np.random.seed(0) natoms = 120 rho = 1.6 boxl = 1. meandiam = boxl / (float(natoms) / rho)**(1. / 3) print "mean diameter", meandiam #set up potential #diams = np.array([meandiam for i in range(natoms)]) #make them all the same #pot = SoftSphere(diams = diams) pot = SoftSphere() #initial coordinates coords = np.random.uniform(-1, 1, [natoms * 3]) * (natoms)**(1. / 3) E = pot.getEnergy(coords) print "initial energy", E printlist = [] #list of coordinates saved for printing printlist.append((coords.copy(), "intial coords")) #test a quench with default lbfgs #from optimize.quench import quench from pygmin.optimize import lbfgs_ase as quench coords, E, rms, funcalls = quench(coords, pot.getEnergyGradient, iprint=1) printlist.append((coords.copy(), "intial coords"))