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 minima_from_ts(pot, xt, n=None, quench=None, stepmin=0.01, **kwargs): """ step off either side of a transition state and quench to find the minima Parameters ---------- pot : potential object xt : array transition state coords n : array direction to step off quench : callable routine to use to do the quenching kwargs : dict parameters to pass to determine_pushoff """ if n is None: # if no direction is given, choose random direction n = vec_random_ndim(xt.size) if quench is None: quench = lambda coords: mylbfgs(coords, pot) x1 = determine_pushoff(pot, xt, n, stepmin=stepmin, **kwargs) x2 = determine_pushoff(pot, xt, -n, stepmin=stepmin, **kwargs) minimum1 = quench(x1) minimum2 = quench(x2) return minimum1, minimum2
def test2(): # pragma: no cover import numpy as np from pele.potentials import LJ from pele.utils.frozen_atoms import FrozenPotWrapper from pele.optimize import mylbfgs natoms = 4 pot = LJ() reference_coords = np.random.uniform(-1, 1, [3 * natoms]) print reference_coords # freeze the first two atoms (6 degrees of freedom) frozen_dof = range(6) fpot = FrozenPotWrapper(pot, reference_coords, frozen_dof) reduced_coords = fpot.coords_converter.get_reduced_coords(reference_coords) print "the energy in the full representation:" print pot.getEnergy(reference_coords) print "is the same as the energy in the reduced representation:" print fpot.getEnergy(reduced_coords) ret = mylbfgs(reduced_coords, fpot) print "after a minimization the energy is ", ret.energy, "and the rms gradient is", ret.rms print "the coordinates of the frozen degrees of freedom are unchanged" print "starting coords:", reference_coords print "minimized coords:", fpot.coords_converter.get_full_coords( ret.coords)
def test2(): import numpy as np from pele.potentials import LJ from pele.utils.frozen_atoms import FrozenPotWrapper from pele.optimize import mylbfgs natoms = 4 pot = LJ() reference_coords = np.random.uniform(-1, 1, [3 * natoms]) print reference_coords # freeze the first two atoms (6 degrees of freedom) frozen_dof = range(6) fpot = FrozenPotWrapper(pot, reference_coords, frozen_dof) reduced_coords = fpot.coords_converter.get_reduced_coords(reference_coords) print "the energy in the full representation:" print pot.getEnergy(reference_coords) print "is the same as the energy in the reduced representation:" print fpot.getEnergy(reduced_coords) ret = mylbfgs(reduced_coords, fpot) print "after a minimization the energy is ", ret.energy, "and the rms gradient is", ret.rms print "the coordinates of the frozen degrees of freedom are unchanged" print "starting coords:", reference_coords print "minimized coords:", fpot.coords_converter.get_full_coords(ret.coords)
def main(): natoms = 4 pot = LJ() reference_coords = np.random.uniform(-1, 1, [3 * natoms]) print reference_coords # freeze the first two atoms (6 degrees of freedom) frozen_dof = range(6) fpot = FrozenPotWrapper(pot, reference_coords, frozen_dof) reduced_coords = fpot.coords_converter.get_reduced_coords(reference_coords) print "the energy in the full representation:" print pot.getEnergy(reference_coords) print "is the same as the energy in the reduced representation:" print fpot.getEnergy(reduced_coords) ret = mylbfgs(reduced_coords, fpot) print "after a minimization the energy is ", ret.energy, "and the rms gradient is", ret.rms print "the coordinates of the frozen degrees of freedom are unchanged" print "starting coords:", reference_coords print "minimized coords:", fpot.coords_converter.get_full_coords( ret.coords)
def minima_from_ts(pot, xt, n=None, quench=None, stepmin=0.01, **kwargs): """ step off either side of a transition state and quench to find the minima Parameters ---------- pot : potential object xt : array transition state coords n : array direction to step off quench : callable routine to use to do the quenching kwargs : dict parameters to pass to determine_pushoff """ if n is None: # if no direction is given, choose random direction n = vec_random_ndim(xt.size) if quench: quenchRoutine = lambda coords: quench(coords, pot=pot, **kwargs) else: quenchRoutine = lambda coords: mylbfgs(coords, pot=pot, **kwargs) x1 = determine_pushoff(pot, xt, n, stepmin=stepmin, **kwargs) x2 = determine_pushoff(pot, xt, -n, stepmin=stepmin, **kwargs) minimum1 = quench(x1) minimum2 = quench(x2) return minimum1, minimum2
def test(): pi = np.pi nspins = 6 #phases = np.zeros(nspins) pot = XYModel(nspins, phi = np.pi/8.) #, phases=phases) angles = np.random.uniform(-pi/4, pi/4, nspins) print angles e = pot.getEnergy(angles) print e print "numerical gradient" ret = pot.getEnergyGradientNumerical(angles) print ret[1] print "analytical gradient" ret2 = pot.getEnergyGradient(angles) print ret2[1] print ret[0] print ret2[0] #try a quench from pele.optimize import mylbfgs ret = mylbfgs(angles, pot) print "quenched e = ", ret.energy print ret.coords test_basin_hopping(pot, angles)
def setUp(self): self.natoms = 18 self.pot = _lj.LJ() self.pot_comp = LJ() x = np.random.uniform(-1, 1, 3 * self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 18 self.pot = _lj.LJ() self.pot_comp = LJ() x = np.random.uniform(-1,1, 3*self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): from pele.optimize import mylbfgs self.natoms = 10 self.coords = np.random.uniform(-1,1.,3*self.natoms) * self.natoms**(-1./3) self.pot = LJ() ret = mylbfgs(self.coords, self.pot, tol=2.) self.coords = ret.coords self.E = self.pot.getEnergy(self.coords) self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)
def setUp(self): self.natoms = 18 ntypeA = 5 self.pot = _lj.BLJCut(self.natoms, ntypeA) self.pot_comp = LJpshift(self.natoms, ntypeA) x = np.random.uniform(-1, 1, 3 * self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def testBLJ(self): X1 = np.copy(self.X1) X2 = np.random.uniform(-1,1,[self.natoms*3])*(float(self.natoms))**(1./3)/2 #run a quench so the structure is not crazy ret = mylbfgs(X2, self.pot) X2 = ret.coords self.runtest(X1, X2, MinPermDistCluster(measure=MeasureAtomicCluster(permlist=self.permlist)))
def setUp(self): self.natoms = 18 ntypeA = 5 self.pot = _lj.BLJCut(self.natoms, ntypeA) self.pot_comp = LJpshift(self.natoms, ntypeA) x = np.random.uniform(-1, 1, 3 * self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.0) self.x = ret.coords
def setUp(self): self.natoms = 200 rcut = 2.5 self.pot = _lj.LJCut(rcut=rcut) self.pot_comp = LJpshift(self.natoms, self.natoms, rcut=rcut) x = np.random.uniform(-rcut*2,rcut*2, 3*self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 200 rcut = 2.5 self.pot = _lj.LJCut(rcut=rcut) self.pot_comp = LJpshift(self.natoms, self.natoms, rcut=rcut) x = np.random.uniform(-rcut * 2, rcut * 2, 3 * self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def testBLJ(self): X1 = np.copy(self.X1) X2 = old_div(np.random.uniform(-1,1,[self.natoms*3])*(float(self.natoms))**(1./3),2) #run a quench so the structure is not crazy ret = mylbfgs(X2, self.pot) X2 = ret.coords self.runtest(X1, X2, MinPermDistCluster(measure=MeasureAtomicCluster(permlist=self.permlist)))
def setUp(self): from pele.optimize import mylbfgs self.natoms = 10 self.coords = np.random.uniform(-1, 1., 3 * self.natoms) * self.natoms ** (-1. / 3) self.pot = LJ() ret = mylbfgs(self.coords, self.pot, tol=2.) self.coords = ret.coords self.E = self.pot.getEnergy(self.coords) self.Egrad, self.grad = self.pot.getEnergyGradient(self.coords)
def setUp(self): self.natoms = 200 boxl = 7. self.boxvec = [boxl] * 3 self.pot = _lj.LJ(boxvec=self.boxvec) self.pot_comp = LJ(boxl=boxl) x = np.random.uniform(-1,1, 3*self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 200 boxl = 7. self.boxvec = [boxl] * 3 self.pot = _lj.LJ(boxvec=self.boxvec) self.pot_comp = LJ(boxl=boxl) x = np.random.uniform(-1, 1, 3 * self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def __init__(self, coords, potential, takeStep, storage=None, event_after_step=None, acceptTest=None, temperature=1.0, quench=None, confCheck=None, outstream=sys.stdout, insert_rejected=False): ######################################################################### # initialize MonteCarlo base class ######################################################################### MonteCarlo.__init__(self, coords, potential, takeStep, storage=storage, event_after_step=event_after_step, acceptTest=acceptTest, temperature=temperature, confCheck=confCheck, outstream=outstream, store_initial=False) if quench is None: quench = lambda coords: mylbfgs(coords, self.potential) self.quench = quench ######################################################################### # do initial quench ######################################################################### self.markovE_old = self.markovE res = self.quench(self.coords) self.result.nfev += res.nfev self.coords = res.coords self.markovE = res.energy self.rms = res.rms self.funcalls = res.nfev self.insert_rejected = insert_rejected if self.storage: self.storage(self.markovE, self.coords) # print the initial quench self.acceptstep = True self.trial_energy = self.markovE self.printStep() self.result.energy = self.markovE self.result.coords = self.coords.copy()
def setUp(self): self.natoms = 18 self.ilist = np.array([(i,j) for i in xrange(self.natoms) for j in xrange(i+1,self.natoms)]).reshape(-1) assert self.ilist.size == self.natoms*(self.natoms-1) # print self.ilist self.pot = _lj.LJInteractionList(self.ilist) self.pot_comp = LJ() x = np.random.uniform(-1,1, 3*self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 200 rcut = 2.5 boxl = 7. self.boxvec = [boxl] * 3 self.pot = _lj.LJCut(rcut=rcut, boxvec=self.boxvec) self.pot_comp = LJpshift(self.natoms, self.natoms, rcut=rcut, boxl=boxl) x = np.random.uniform(-rcut*2,rcut*2, 3*self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 18 self.ilist = np.array([(i,j) for i in range(self.natoms) for j in range(i+1,self.natoms)]).reshape(-1) assert self.ilist.size == self.natoms*(self.natoms-1) # print self.ilist self.pot = _lj.LJInteractionList(self.ilist) self.pot_comp = LJ() x = np.random.uniform(-1,1, 3*self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 18 frozen_atoms = np.array([1, 2, 5, 9], dtype=int) reference_coords = np.random.uniform(-1, 1, 3 * self.natoms) self.pot = _lj.LJFrozen(reference_coords, frozen_atoms) system = LJClusterFrozen(self.natoms, frozen_atoms, reference_coords) self.pot_comp = system.get_potential() x = system.get_random_configuration() ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def setUp(self): self.natoms = 18 frozen_atoms = np.array([1, 2, 5, 9], dtype=int) reference_coords = np.random.uniform(-1,1, 3*self.natoms) self.pot = _lj.LJFrozen(reference_coords, frozen_atoms) system = LJClusterFrozen(self.natoms, frozen_atoms, reference_coords) self.pot_comp = system.get_potential() x = system.get_random_configuration() ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def calculate_spointE(pot, db, nsteps=1000): min_energies = [] ts_energies = [] ts_id = [] for m1 in db.minima(): #pot = ase_ff(atoms.set_positions(m1.coords.reshape((7, -1)))) res1 = mylbfgs(m1.coords, pot, nsteps=nsteps) E1 = res1.energy min_energies.append(E1) for m2 in db.minima(): if m1 == m2 or m1.id() > m2.id(): continue try: ts_coords = db.getTransitionState(m1, m2).coords res1 = mylbfgs(ts_coords, pot, nsteps=0) E1 = res1.energy ts_energies.append(E1) ts_id.append([m1.id(), m2.id()]) except: pass return np.array(min_energies), np.array(ts_energies), np.array(ts_id)
def setUp(self): from pele.potentials.ljpshiftfast import LJpshift as BLJ self.natoms = 25 self.ntypeA = int(self.natoms * .8) self.pot = BLJ(self.natoms, self.ntypeA) self.permlist = [list(range(self.ntypeA)), list(range(self.ntypeA, self.natoms))] self.X1 = old_div(np.random.uniform(-1,1,[self.natoms*3])*(float(self.natoms))**(1./3),2) #run a quench so the structure is not crazy ret = mylbfgs(self.X1, self.pot) self.X1 = ret.coords
def setUp(self): from pele.potentials.ljpshiftfast import LJpshift as BLJ self.natoms = 25 self.ntypeA = int(self.natoms * .8) self.pot = BLJ(self.natoms, self.ntypeA) self.permlist = [list(range(self.ntypeA)), list(range(self.ntypeA, self.natoms))] self.X1 = np.random.uniform(-1,1,[self.natoms*3])*(float(self.natoms))**(1./3)/2 #run a quench so the structure is not crazy ret = mylbfgs(self.X1, self.pot) self.X1 = ret.coords
def setUp(self): self.natoms = 200 rcut = 2.5 boxl = 7. self.boxvec = [boxl] * 3 self.pot = _lj.LJCut(rcut=rcut, boxvec=self.boxvec) self.pot_comp = LJpshift(self.natoms, self.natoms, rcut=rcut, boxl=boxl) x = np.random.uniform(-rcut * 2, rcut * 2, 3 * self.natoms) ret = mylbfgs(x, self.pot_comp, tol=10.) self.x = ret.coords
def minimise(self, cluster): '''Minimise a cluster parameters: cluster- a Cluster object from bcga.cluster Using this method will overwrite the coordinates and energy of the supplied Cluster object.''' # Set up pele minimiser coords = cluster._coords.flatten() quench = lambda coords: mylbfgs(coords, self.potential) # Minimise res = quench(coords) # Get cluster properties back from pele cluster.energy = res.energy cluster._coords = np.reshape(res.coords, (-1, 3)) cluster.quenched = True return cluster
def minima_from_ts(pot, xt, n=None, quench=None, **kwargs): """ step off either side of a transition state and quench to find the minima Parameters ---------- pot : potential object xt : array transition state coords stepmin : float initial guess for size of pushoff when stepping off the transition state stepmax : float maximum size of pushoff when stepping off the transition state gdiff : float criterion for choosing a step size. Try to choose a stepsize so that:: (gradpar - gradpar_ts) / gradpar_ts >= gdiff where gradpar and gradpar_ts is the parallel component of the gradient after the step and at the top of the transition state quenchRoutine : callable routine to use to do the quenching quenchParams : dict parameters to pass to quenchRoutine verbose : bool """ # if no direction is given, choose random direction if n==None: # TODO: replace by better algorithm with uniform sampling n = np.random.random(xt.shape)-0.5 if quench is None: quench = lambda coords : mylbfgs(coords, pot) #x1 = xt - displace*n x1 = determinePushoff(pot, xt, n, **kwargs) x2 = determinePushoff(pot, xt, -n, **kwargs) #x2 = xt + displace*n #e1,g1 = getEnergyGradient(x1) #e2,g2 = getEnergyGradient(x2) #print np.dot(g1,g2) minimum1 = quench(x1) minimum2 = quench(x2) return minimum1, minimum2
def __init__(self, coords, potential, takeStep, storage=None, event_after_step=[], acceptTest=None, temperature=1.0, quench=None, confCheck=[], outstream=sys.stdout, insert_rejected=False ): ######################################################################### # initialize MonteCarlo base class ######################################################################### MonteCarlo.__init__(self, coords, potential, takeStep, storage=storage, event_after_step=event_after_step, acceptTest=acceptTest, temperature=temperature, confCheck=confCheck, outstream=outstream, store_initial=False) if quench is None: quench = lambda coords : mylbfgs(coords, self.potential) self.quench = quench ######################################################################### # do initial quench ######################################################################### self.markovE_old = self.markovE res = self.quench(self.coords) self.coords = res.coords self.markovE = res.energy self.rms = res.rms self.funcalls = res.nfev self.insert_rejected = insert_rejected if(self.storage): self.storage(self.markovE, self.coords) # print the initial quench self.acceptstep = True self.trial_energy = self.markovE self.printStep() self.result.energy = self.markovE self.result.coords = self.coords.copy()
def main(): natoms = 4 pot = LJ() reference_coords = np.random.uniform(-1, 1, [3 * natoms]) print reference_coords # freeze the first two atoms (6 degrees of freedom) frozen_dof = range(6) fpot = FrozenPotentialWrapper(pot, reference_coords, frozen_dof) reduced_coords = fpot.get_reduced_coords(reference_coords) print "the energy in the full representation:" print pot.getEnergy(reference_coords) print "is the same as the energy in the reduced representation:" print fpot.getEnergy(reduced_coords) ret = mylbfgs(reduced_coords, fpot) print "after a minimization the energy is ", ret.energy, "and the rms gradient is", ret.rms print "the coordinates of the frozen degrees of freedom are unchanged" print "starting coords:", reference_coords print "minimized coords:", fpot.get_full_coords(ret.coords)
pot = XYModel( dim = [L,L], phi = np.pi) angles = np.random.uniform(-pi, pi, nspins) print angles e = pot.getEnergy(angles) print "energy ", e # try a quench if False: from pele.optimize import mylbfgs ret = mylbfgs(angles, pot) print ret # set up and run basin hopping from pele.basinhopping import BasinHopping from pele.takestep.displace import RandomDisplacement from pele.takestep.adaptive import AdaptiveStepsize from pele.storage import savenlowest # should probably use a different take step routine which takes into account # the cyclical periodicity of angles takestep = RandomDisplacement(stepsize = np.pi/4) takestepa = AdaptiveStepsize(takestep, frequency = 20)
coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i, :] = make2dVector(vec) coords = np.reshape(coords, [nspins * 2]) coordsinit = np.copy(coords) print coords e = pot.getEnergy(coords) print "energy ", e print "try a quench" from pele.optimize import mylbfgs ret = mylbfgs(coords, pot) print "quenched e = ", ret.energy, "funcalls", ret.nfev print ret.coords m = getm(ret[0]) print "magnetization after quench", m # do basin hopping from pele.basinhopping import BasinHopping from pele.takestep.displace import RandomDisplacement from pele.takestep.adaptive import AdaptiveStepsize from pele.storage import savenlowest takestep = RandomDisplacement(stepsize=np.pi / 4) takestepa = AdaptiveStepsize(takestep, frequency=10)
coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i, :] = make2dVector(vec) coords = np.reshape(coords, [nspins * 2]) coordsinit = np.copy(coords) print coords e = pot.getEnergy(coords) print "energy ", e print "try a quench" from pele.optimize import mylbfgs ret = mylbfgs(coords, pot) print "quenched e = ", ret.energy, "funcalls", ret.nfev print ret.coords m = getm(ret[0]) print "magnetization after quench", m # do basin hopping from pele.basinhopping import BasinHopping from pele.takestep.displace import RandomDisplacement from pele.takestep.adaptive import AdaptiveStepsize from pele.storage import savenlowest takestep = RandomDisplacement(stepsize=np.pi / 4)
ntypeA = int(natoms*0.8) rcut = 2.5 print "natoms", natoms, "ntypea", ntypeA bljslow = ljpshift.LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl, sigBB=1.7) blj = LJpshift(natoms, ntypeA, rcut=rcut, boxl=boxl, sigBB=1.7) 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) print "blj energy ", eblj blj.test_potential(coords, eps=1e-6) print "" print "quenching coords" ret1 = mylbfgs(coords, blj, iprint=-11) coords = ret1.coords ebljslow = bljslow.getEnergy(coords) print "blj energy slow", ebljslow
natoms = int(sys.argv[1]) print "benchmarking lennard jones potential, %d atoms, %d calls" % (natoms, N) pot_old = LJ() pot = _lj.LJ() t0 = time.time() for i in xrange(N): x = 1. * (np.random.random(3 * natoms) - 0.5) clbfgs = _lbfgs.LBFGS_CPP(pot, x, tol=1e-4) ret = clbfgs.run() t1 = time.time() for i in xrange(N): x = 1. * (np.random.random(3 * natoms) - 0.5) ret = mylbfgs(x, pot_old, tol=1e-4) t2 = time.time() for i in xrange(N): x = 1. * (np.random.random(3 * natoms) - 0.5) clbfgs = _lbfgs.LBFGS_CPP(_lj_cython.LJ_cython(), x, tol=1e-4) ret = clbfgs.run() t3 = time.time() for i in xrange(N): x = 1. * (np.random.random(3 * natoms) - 0.5) clbfgs = _lbfgs.LBFGS_CPP(pot_old, x, tol=1e-4) ret = clbfgs.run()
L = 24 nspins = L**2 pot = XYModel(dim=[L, L], phi=np.pi) angles = np.random.uniform(-pi, pi, nspins) print(angles) e = pot.getEnergy(angles) print("energy ", e) # try a quench if False: from pele.optimize import mylbfgs ret = mylbfgs(angles, pot) print(ret) # set up and run basin hopping from pele.basinhopping import BasinHopping from pele.takestep.displace import RandomDisplacement from pele.takestep.adaptive import AdaptiveStepsize from pele.storage import savenlowest # should probably use a different take step routine which takes into account # the cyclical periodicity of angles takestep = RandomDisplacement(stepsize=old_div(np.pi, 4)) takestepa = AdaptiveStepsize(takestep, frequency=20) storage = savenlowest.SaveN(500)
import sys import _lj import _lbfgs from pele.optimize import mylbfgs N=100 natoms=[10, 13, 20, 30, 31, 38, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150] print "benchmarking lennard jones potential, %d atoms, %d calls", natoms, N pot_old = LJ() pot = _lj.LJ() clbfgs = _lbfgs.LBFGS(pot) res = open("results.txt", "w") for na in natoms: t0 = time.time() for i in xrange(N): x = np.random.random(3*na) - 0.5 ret = clbfgs.run(x) t1 = time.time() for i in xrange(N): x = np.random.random(3*na) - 0.5 ret = mylbfgs(x, pot_old, tol=1e-5) res.write("%d %f %f\n"%(na, t1-t0, time.time()-t1)) print "%d %f %f\n"%(na, t1-t0, time.time()-t1) res.close()
def test(): pi = np.pi L = 8 nspins = L**2 #phases = np.zeros(nspins) pot = HeisenbergModel( dim = [L,L], field_disorder = 1.) #, phases=phases) coords = np.zeros([nspins, 2]) for i in range(nspins): vec = rotations.vec_random() coords[i,:] = make2dVector(vec) coords = np.reshape(coords, [nspins*2]) if False: normfields = np.copy(pot.fields) for i in range(nspins): normfields[i,:] /= np.linalg.norm(normfields[i,:]) coords = coords3ToCoords2( np.reshape(normfields, [nspins*3] ) ) coords = np.reshape(coords, nspins*2) #print np.shape(coords) coordsinit = np.copy(coords) #print "fields", pot.fields print coords if False: coords3 = coords2ToCoords3(coords) coords2 = coords3ToCoords2(coords3) print np.reshape(coords, [nspins,2]) print coords2 coords3new = coords2ToCoords3(coords2) print coords3 print coords3new e = pot.getEnergy(coords) print "energy ", e if False: print "numerical gradient" ret = pot.getEnergyGradientNumerical(coords) print ret[1] if True: print "analytical gradient" ret2 = pot.getEnergyGradient(coords) print ret2[1] print ret[0] print ret2[0] print "ratio" print ret2[1] / ret[1] print "inverse sin" print 1./sin(coords) print cos(coords) print "try a quench" from pele.optimize import mylbfgs ret = mylbfgs(coords, pot, iprint=1) print "quenched e = ", ret.energy, "funcalls", ret.nfev print ret.coords with open("out.spins", "w") as fout: s = coords2ToCoords3( ret.coords ) h = pot.fields c = coords2ToCoords3( coordsinit ) for node in pot.G.nodes(): i = pot.indices[node] fout.write( "%g %g %g %g %g %g %g %g %g %g %g\n" % (node[0], node[1], \ s[i,0], s[i,1], s[i,2], h[i,0], h[i,1], h[i,2], c[i,0], c[i,1], c[i,2] ) ) coords3 = coords2ToCoords3( ret.coords ) m = np.linalg.norm( coords3.sum(0) ) / nspins print "magnetization after quench", m test_basin_hopping(pot, coords)
def test2(): import pele.potentials.ljpshiftfast as ljpshiftfast import pele.potentials.ljpshift as ljpshift from pele.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.0 / 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)