def test(natoms=40, boxl=None): import pygmin.potentials.ljpshiftfast as ljpshift from pygmin.optimize import mylbfgs ntypeA = int(natoms * 0.8) rcut = 2.5 coords = np.random.uniform(-1, 1, natoms * 3) * (natoms)**(1. / 3) / 2 blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) pot = makeBLJNeighborListPot(natoms, ntypeA=ntypeA, rcut=rcut, boxl=boxl) eblj = blj.getEnergy(coords) print "blj energy", eblj epot = pot.getEnergy(coords) print "mcpot energy", epot print "difference", (epot - eblj) / eblj 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)) for subpot in pot.potentials: nl = subpot.neighborList print "number of times neighbor list was remade", nl.buildcount, "out of", nl.count if False: try: import pygmin.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 makeBLJNeighborListPot(natoms, ntypeA=None, rcut=2.5, boxl=None): """ recreate the binary lj with atom typea A,B from 3 interaction lists AA, BB, AB """ print "making BLJ neighborlist potential", natoms, ntypeA, rcut, boxl #rcut = 2.5 #natoms = 40 if ntypeA is None: ntypeA = int(natoms * 0.8) Alist = range(ntypeA) Blist = range(ntypeA, natoms) blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut) ljAA = LJ(eps=blj.AA.eps, sig=blj.AA.sig, rcut=rcut * blj.AA.sig, boxl=boxl) ljBB = LJ(eps=blj.BB.eps, sig=blj.BB.sig, rcut=rcut * blj.BB.sig, boxl=boxl) ljAB = LJ(eps=blj.AB.eps, sig=blj.AB.sig, rcut=rcut * blj.AB.sig, boxl=boxl) nlAA = NeighborListSubset(natoms, rcut, Alist, boxl=boxl) nlBB = NeighborListSubset(natoms, rcut, Blist, boxl=boxl) nlAB = NeighborListSubset(natoms, rcut, Alist, Blist, boxl=boxl) potlist = [ NeighborListPotential(nlAA, ljAA), NeighborListPotential(nlBB, ljBB), NeighborListPotential(nlAB, ljAB) ] mcpot = MultiComponentSystem(potlist) return mcpot
def makeBLJNeighborListPotFreeze(natoms, frozenlist, ntypeA=None, rcut=2.5, boxl=None): """ create the potential object for the kob andersen binary lennard jones with frozeen particles Parameters ---------- natoms : number of atoms in the system frozenlist : list of frozen atoms ntypeA : number of atoms of type A. It is assumed that they have indices in [0,ntypeA] rcut : the cutoff for the lj potential in units of sigA boxl : the box length for periodic box. None for no periodic boundary conditions """ print "making BLJ neighborlist potential", natoms, ntypeA, rcut, boxl #rcut = 2.5 #natoms = 40 if ntypeA is None: ntypeA = int(natoms * 0.8) Alist = range(ntypeA) Blist = range(ntypeA, natoms) frozenA = np.array([i for i in Alist if i in frozenlist]) mobileA = np.array([i for i in Alist if i not in frozenlist]) frozenB = np.array([i for i in Blist if i in frozenlist]) mobileB = np.array([i for i in Blist if i not in frozenlist]) blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut) ljAA = LJCut(eps=blj.AA.eps, sig=blj.AA.sig, rcut=rcut * blj.AA.sig, boxl=boxl) ljBB = LJCut(eps=blj.BB.eps, sig=blj.BB.sig, rcut=rcut * blj.BB.sig, boxl=boxl) ljAB = LJCut(eps=blj.AB.eps, sig=blj.AB.sig, rcut=rcut * blj.AB.sig, boxl=boxl) #ten lists in total #nlAA_ff #nlAA_mm #nlAA_mf #nlBB_ff #nlBB_mm #nlBB_mf #nlAB_ff #nlAB_mm #nlAB_mf #nlAB_fm nlAA_ff = NeighborListSubsetBuild(natoms, rcut, frozenA, boxl=boxl) nlAA_mm = NeighborListSubsetBuild(natoms, rcut, mobileA, boxl=boxl) nlAA_mf = NeighborListSubsetBuild(natoms, rcut, mobileA, Blist=frozenA, boxl=boxl) nlBB_ff = NeighborListSubsetBuild(natoms, rcut, frozenB, boxl=boxl) nlBB_mm = NeighborListSubsetBuild(natoms, rcut, mobileB, boxl=boxl) nlBB_mf = NeighborListSubsetBuild(natoms, rcut, mobileB, Blist=frozenB, boxl=boxl) nlAB_ff = NeighborListSubsetBuild(natoms, rcut, frozenA, Blist=frozenB, boxl=boxl) nlAB_mm = NeighborListSubsetBuild(natoms, rcut, mobileA, Blist=mobileB, boxl=boxl) nlAB_mf = NeighborListSubsetBuild(natoms, rcut, mobileA, Blist=frozenB, boxl=boxl) nlAB_fm = NeighborListSubsetBuild(natoms, rcut, mobileB, Blist=frozenA, boxl=boxl) potlist_frozen = [ NeighborListPotentialBuild(nlAA_ff, ljAA), NeighborListPotentialBuild(nlBB_ff, ljBB), NeighborListPotentialBuild(nlAB_ff, ljAB) ] potlist_mobile = [ NeighborListPotentialBuild(nlAA_mm, ljAA), NeighborListPotentialBuild(nlAA_mf, ljAA), NeighborListPotentialBuild(nlBB_mm, ljBB), NeighborListPotentialBuild(nlBB_mf, ljBB), NeighborListPotentialBuild(nlAB_mm, ljAB), NeighborListPotentialBuild(nlAB_mf, ljAB), NeighborListPotentialBuild(nlAB_fm, ljAB), ] #wrap the mobile potentials so the check for whether coords needs to be updated #can be done all at once mobile_pot = NeighborListPotentialMulti(potlist_mobile, natoms, rcut, boxl=boxl) #wrap the mobile and frozen potentials together mcpot = MultiComponentSystemFreeze([mobile_pot], potlist_frozen) #finally, wrap it once more in a class that will zero the gradients of the frozen atoms frozenpot = FreezePot(mcpot, frozenlist, natoms) return frozenpot
if False: coords = np.array(np.loadtxt(fname)) coords = coords.reshape(-1) boxl = 11.05209 else: natoms = 200 coords = np.random.uniform(-1,1,natoms*3)*(natoms)**(1./3)/2 print "max, min coords", coords.max(), coords.min() boxl = 4 natoms = len(coords) /3 ntypeA = int(natoms*0.8) rcut = 2.5 print "natoms", natoms, "ntypea", ntypeA bljslow = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) blj = LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) ebljslow = bljslow.getEnergy(coords) print "blj energy slow", ebljslow eblj = blj.getEnergy(coords) print "blj energy ", eblj blj.test_potential(coords) print "" print "partially quenching coords" ret1 = mylbfgs(coords, blj, iprint=-11, tol=1.) coords = ret1.coords ebljslow = bljslow.getEnergy(coords) print "blj energy slow", ebljslow eblj = blj.getEnergy(coords)
def test2(): import pygmin.potentials.ljpshiftfast as ljpshiftfast import pygmin.potentials.ljpshift as ljpshift from pygmin.optimize import mylbfgs fname = "/scratch/scratch2/js850/library/cluster/spherical/1620/PTMC/q4/oneatom/cavity200-8/ts/coords1.quench" fname = "/scratch/scratch2/js850/library/cluster/spherical/1620/PTMC/q4/oneatom/cavity200-8/ts/test.coords" #fname = "out.coords" if False: coords = np.array(np.loadtxt(fname)) coords = coords.reshape(-1) boxl = 11.05209 else: natoms = 200 coords = np.random.uniform(-1, 1, natoms * 3) * (natoms)**(1. / 3) / 2 print "max, min coords", coords.max(), coords.min() boxl = 5 natoms = len(coords) / 3 ntypeA = int(natoms * 0.8) rcut = 2.5 print "natoms", natoms, "ntypea", ntypeA blj = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) bljfast = ljpshiftfast.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl) pot = makeBLJNeighborListPot(natoms, ntypeA=ntypeA, rcut=rcut, boxl=boxl) eblj = blj.getEnergy(coords) print "blj energy", eblj epot = pot.getEnergy(coords) print "mcpot energy", epot print "energy difference", (epot - eblj) 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 False: print "quenching" 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 "max, min quenched coords", coords.max(), coords.min() 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) elif True: print "quenching" ret2 = mylbfgs(coords, pot, iprint=-1) print "energy from quench2", ret2.energy print "max, min quenched coords", ret2.coords.max(), ret2.coords.min() print "ret2 evaluated in both potentials", pot.getEnergy( ret2.coords), blj.getEnergy(ret2.coords) print "and in blj fast ", bljfast.getEnergy( ret2.coords)