def test(): pi = np.pi L = 3 nspins = L**2 #phases = np.zeros(nspins) pot = XYModel( dim = [L,L], phi = np.pi) #, phases=phases) angles = np.random.uniform(-pi, pi, nspins) print angles e = pot.getEnergy(angles) print "energy ", 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 pygmin.optimize.quench import mylbfgs ret = mylbfgs(angles, pot.getEnergyGradient) print "quenched e = ", ret[1] print ret[0] test_basin_hopping(pot, angles)
def test(): pi = np.pi L = 4 nspins = L**2 #phases = np.zeros(nspins) pot = HeisenbergModelRA( dim = [L,L], field_disorder = 2. ) #, 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 True: 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 pygmin.optimize.quench import mylbfgs ret = mylbfgs(coords, pot.getEnergyGradient) print "quenched e = ", ret[1], "funcalls", ret[3] print ret[0] with open("out.spins", "w") as fout: s = coords2ToCoords3( ret[0] ) 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[0] ) m = np.linalg.norm( coords3.sum(0) ) / nspins print "magnetization after quench", m test_basin_hopping(pot, coords)
help="outfile for results") (options, args) = parser.parse_args() configurations = pickle.load(open("quench_benchmark.dat")) GMIN.initialize() pot = GMINPotential(GMIN) #fl = open("quenched.xyz", "w") res = open("results.txt", "w") t0 = time.time() print "# energy nsteps rms" res.write("# energy nsteps rms\n") for coords in configurations[0:5]: ret = mylbfgs(coords, pot.getEnergyGradient, tol=options.tol, M=options.M, maxstep=options.maxstep, maxErise=options.maxErise) energy = ret[1] fcalls = ret[3] #oxdna.export_xyz(fl, ret[0]) print energy, fcalls, ret[2] res.write("%f %d %g\n" % (energy, fcalls, ret[2])) print "total runtime: ", time.time() - t0 #fl.close() res.close()
parser.add_option("--maxErise", dest="maxErise", type=float, default=1e-4, help="maximum energy increase for lbfgs") parser.add_option("--maxstep", dest="maxstep", type=float, default=0.1, help="maximum stepsize for minimizer") parser.add_option("-o", dest="outfile", default="results.txt", help="outfile for results") (options, args) = parser.parse_args() configurations = pickle.load(open("quench_benchmark.dat")) GMIN.initialize() pot = GMINPotential(GMIN) #fl = open("quenched.xyz", "w") res = open("results.txt", "w") t0 = time.time() print "# energy nsteps rms" res.write("# energy nsteps rms\n") for coords in configurations[0:5]: ret = mylbfgs(coords, pot.getEnergyGradient, tol=options.tol, M=options.M, maxstep=options.maxstep, maxErise=options.maxErise) energy = ret[1] fcalls = ret[3] #oxdna.export_xyz(fl, ret[0]) print energy, fcalls, ret[2] res.write("%f %d %g\n"%(energy, fcalls, ret[2])) print "total runtime: ", time.time()-t0 #fl.close() res.close()
vec = rotations.vec_random() coords[i,:] = make2dVector(vec) coords = np.reshape(coords, [nspins*2]) #print np.shape(coords) coordsinit = np.copy(coords) #print "fields", pot.fields print coords e = pot.getEnergy(coords) print "energy ", e print "try a quench" from pygmin.optimize.quench import mylbfgs ret = mylbfgs(coords, pot.getEnergyGradient) print "quenched e = ", ret[1], "funcalls", ret[3] print ret[0] m = getm( ret[0] ) print "magnetization after quench", m #do basin hopping from pygmin.basinhopping import BasinHopping from pygmin.takestep.displace import RandomDisplacement from pygmin.takestep.adaptive import AdaptiveStepsize from pygmin.storage import savenlowest
#phases = np.zeros(nspins) pot = XYModel( dim = [L,L], phi = np.pi) #, phases=phases) angles = np.random.uniform(-pi, pi, nspins) print angles e = pot.getEnergy(angles) print "energy ", e #try a quench if False: from pygmin.optimize.quench import mylbfgs ret = mylbfgs(angles, pot.getEnergyGradient) print "quenched e = ", ret[1] print ret[0] #set up and run basin hopping from pygmin.basinhopping import BasinHopping from pygmin.takestep.displace import RandomDisplacement from pygmin.takestep.adaptive import AdaptiveStepsize from pygmin.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)