def lattice(data, funcG, funcW, n): #the only option - we need Gkw and Wqnu for self energy in the next iteration #data.get_Gkw(funcG) #gets Gkw from G0 and Sigma def func(var, data): mu = var[0] dt = data[0] #print "func call! mu: ", mu, " n: ",dt.ns['up'] n= data[1] dt.mus['up'] = mu dt.mus['down'] = dt.mus['up'] dt.get_Gkw_direct(funcG) #gets Gkw from w, mu, epsilon and Sigma and X dt.get_G_loc() #gets G_loc from Gkw dt.get_n_from_G_loc() #print "funcvalue: ",-abs(n - dt.ns['up']) return 1.0-abs(n - dt.ns['up']) if not MASTER_SLAVE_ARCHITECTURE: mpi.barrier() varbest, funcvalue, iterations = amoeba(var=[data.mus['up']], scale=[0.01], func=func, data = [data, n], itmax=30, ftolerance=1e-2, xtolerance=1e-2) if mpi.is_master_node(): print "mu best: ", varbest print "-abs(diff n - data.n): ", funcvalue print "iterations used: ", iterations data.get_Gtildekw() #gets Gkw-G_loc data.get_Wqnu_from_func(funcW) #gets Wqnu from P and J data.get_W_loc() #gets W_loc from Wqnu data.get_Wtildeqnu() #gets Wqnu-W_loc,
def search_for_mu(get_mu, set_mu, get_n, n, ph_symmetry, accepted_mu_range=[-20.0,20.0]): print "getters: search_for_mu: n: ",n,", ph_symmetry",ph_symmetry, "accepted mu_range: ",accepted_mu_range if (n is None) or ((n==0.5) and ph_symmetry): print "no mu search to be performed! it is your duty to set the chemical potential to U/2. mu =",get_mu() print 'n on the lattice : ', get_n() else: def func(var, data=None): mu = var[0] set_mu(mu) actual_n = get_n() val = 1.0-abs(n - actual_n) print "amoeba func call: mu: %.2f desired n: %.2f actual n: %.2f val = "%(mu,n,actual_n),val if val != val: return -1e+6 else: return val print "about to do mu search:" guesses = [get_mu(), 0.0, -0.1, -0.3, -0.4, -0.5, -0.7, 0.3, 0.5, 0.7] found = False for l in range(len(guesses)): varbest, funcvalue, iterations = amoeba(var=[guesses[l]], scale=[0.01], func=func, data = None, itmax=30, ftolerance=1e-2, xtolerance=1e-2, known_max = 1.0, known_max_accr = 5e-5) if (varbest[0]>accepted_mu_range[0] and varbest[0]<accepted_mu_range[1]) and (abs(funcvalue-1.0)<1e-2): #change the bounds for large doping found = True func(varbest) break if l+1 == len(guesses): print "mu search FAILED: doing a scan..." mu_grid = numpy.linspace(-1.0,0.3,50) func_values = [func(var=[mu]) for mu in mu_grid] print "func_values: " for i in range(len(mu_grid)): print "mu: ",mu_grid[i], " 1-abs(n-n): ", func_values[i] mui_max = numpy.argmax(func_values) print "using mu: ", mu_grid[mui_max] set_mu(mu_grid[mui_max]) get_n() if found: print "guesses tried: ", l print "mu best: ", varbest print "1-abs(diff n - data.n): ", funcvalue print "iterations used: ", iterations
f.write("%endblock PAO.Basis\n") f.close() atoms = ListOfAtoms([ Atom('Ga', (0.0, 0.0, 0.0), magmom=0), Atom('Ga', (2.0, 0.0, 0.0), magmom=0) ]) a = Siesta(executable="/Users/ag/bin/siesta-xlf") a.SetOption("%include", "base.fdf") energy = a.run(atoms) print rcs, rcp, energy return -energy URLbase = "http://fisica.ehu.es/ag/siesta-psffiles/" # create a work subdirectory orig_dir = os.getcwd() dir = "simplex_work" if os.path.isdir(dir): # does dir exist? shutil.rmtree(dir) # yes, remove old directory os.mkdir(dir) # make dir directory os.chdir(dir) # move to dir urllib.urlretrieve(URLbase + "Ga.lda.psf", "Ga.psf") print amoeba.amoeba([4.25, 4.25], [2.5, 2.5], afunc, xtolerance=5.0e-2)
def main(): # global variables global screen, pd, master, w, h, dcBright ## Graphics Initialization w = 1200 h = 250 screen = pygame.display.set_mode((w, h)) pygame.display.set_caption("Amoebas") pd = pygame.draw drawmode = 'stick' dcBright = randColorBright() master = cake.c_master(friction=.92, gravity_glob=[0., 200.], slip=.8) amoebas = [] amoeba0 = amoeba.amoeba(master, [100, h-80]) amoeba0.circle_res = 14 amoeba0.circle_radius = 50. amoeba0.node_mass = 1.0 amoeba0.treading = 3 amoeba0.treadk = 100. amoeba0.tread_damp = .5 amoeba0.musclek = 100 amoeba0.muscle_period = .2*2*pi amoeba0.muscle_amp = 50. amoeba0.muscle_damp = .5 amoeba1 = amoeba.amoeba(master, [100, h-80]) amoeba1.circle_res = 20 amoeba1.circle_radius = 50. amoeba1.node_mass = 1.0 amoeba1.treading = 2 amoeba1.treadk = 200. amoeba1.tread_damp = .5 amoeba1.musclek = 400 amoeba1.muscle_period = 1.2 amoeba1.muscle_amp = 60. amoeba1.muscle_damp = .9 amoeba2 = amoeba.amoeba(master, [150, h-70]) amoeba2.circle_res = 14 amoeba2.circle_radius = 20. amoeba2.node_mass = 1. amoeba2.treading = 2 amoeba2.treadk = 100. amoeba2.tread_damp = .9 amoeba2.musclek = 400. amoeba2.muscle_period = .7 amoeba2.muscle_amp = 30. amoeba2.muscle_damp = amoeba2.tread_damp amoeba3 = amoeba.amoeba(master, [100, h-80]) amoeba3.circle_res = 8 amoeba3.circle_radius = 50. amoeba3.node_mass = 1.0 amoeba3.treading = 2 amoeba3.treadk = 200. amoeba3.tread_damp = .5 amoeba3.musclek = 400 amoeba3.muscle_period = .2*2*pi amoeba3.muscle_amp = 60. amoeba3.muscle_damp = .9 amoeba4 = amoeba.amoeba(master, [100, h-80]) amoeba4.circle_res = 10 amoeba4.circle_radius = 50. amoeba4.node_mass = 1.0 amoeba4.treading = 2 amoeba4.treadk = 200. amoeba4.tread_damp = .5 amoeba4.musclek = 400 amoeba4.muscle_period = .18*2*pi amoeba4.muscle_amp = 90. amoeba4.muscle_damp = .9 amoeba5 = amoeba.amoeba(master, [100, h-180]) amoeba5.circle_res = 16 amoeba5.circle_radius = 30. amoeba5.node_mass = 1.0 amoeba5.treading = 2 amoeba5.treadk = 600. amoeba5.tread_damp = .5 amoeba5.musclek = 40 amoeba5.muscle_period = .18*2*pi amoeba5.muscle_amp = 20. amoeba5.muscle_damp = .2 amoebas.append(amoeba5) amoebas.append(amoeba0) amoebas.append(amoeba1) amoebas.append(amoeba2) amoebas.append(amoeba3) amoebas.append(amoeba4) activeID = 0 amoebas[activeID].assemble() # terrain ground_wall = master.make_wall([(-200, h-20), (w+200, h-10)]) slope_wall = master.make_wall([(200, h-5), (400, h-50)]) master.make_wall([(400, h-50), (500, h)]) timescale = 1 timestep = time.time() time_last = time.time() time_start = time.time() last_render = 0. lagged = False frame = -1 while True: frame += 1 ## Event handler for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit(); elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE or event.unicode == 'q': pygame.quit(); sys.exit(); elif event.key == pygame.K_v: dcBright = randColorBright() if drawmode == 'stick': drawmode = 'solid' elif drawmode == 'solid': drawmode = 'stick' elif event.unicode in ['1', '2', '3', '4', '5', '6', '7', '8', '9']: if int(event.unicode) in range(1,len(amoebas)+1): dcBright = randColorBright() amoebas[activeID].destroy() activeID = int(event.unicode)-1 amoebas[activeID].assemble() print "amoeba ID:\t", activeID elif event.key == pygame.K_RIGHT: amoebas[activeID].control.right = True elif event.key == pygame.K_LEFT: amoebas[activeID].control.left = True elif event.key == pygame.K_DOWN: amoebas[activeID].control.down = True elif event.key == pygame.K_UP: amoebas[activeID].control.up = True elif event.key == pygame.K_SPACE: amoebas[activeID].control.poof = True elif event.key == pygame.K_a: amoebas[activeID].control.stick = True # elif event.key == pygame.K_s: # master.slip = .99 elif event.key == pygame.K_d: amoebas[activeID].control.fat = True elif event.type == pygame.KEYUP: if event.key == pygame.K_RIGHT: amoebas[activeID].control.right = False elif event.key == pygame.K_LEFT: amoebas[activeID].control.left = False elif event.key == pygame.K_DOWN: amoebas[activeID].control.down = False elif event.key == pygame.K_UP: amoebas[activeID].control.up = False elif event.key == pygame.K_SPACE: amoebas[activeID].control.poof = False elif event.key == pygame.K_a: amoebas[activeID].control.stick = False # elif event.key == pygame.K_s: # master.slip = .8 elif event.key == pygame.K_d: amoebas[activeID].control.fat = False elif event.type == pygame.MOUSEBUTTONDOWN: amoebas[activeID].destroy() activeID = (activeID + 1) % len(amoebas) amoebas[activeID].assemble() dcBright = randColorBright() print "amoeba ID:\t", activeID ## Time Steppage timestep = time.time() - time_last time_last = time.time() if master.update((time.time()-time_start)*timescale, max=.033) == False: lagged = True # print "simtime\t" + str(master.simtime) # print "time offset\t" + str((time.time() - time_start) - master.simtime) # print "frame\t" + str(frame) # print "step\t" + str(timestep) ## Update Amoeba amoebas[activeID].update(master.timestep) # Win Test if amoebas[activeID].circle[0].pos[0] >= w-15: amoebas[activeID].destroy() activeID = (activeID + 1) % len(amoebas) amoebas[activeID].assemble() dcBright = randColorBright() print "amoeba ID:\t", activeID ## Render # fps frames per second since_render = time.time() - last_render fps = 60. dectime = time.time() - int(time.time()) if since_render >= 1/fps: screen.fill((255, 255, 255)) render(drawmode, lagged) pygame.display.flip() lagged = False last_render = time.time()
outname = outdir + '/' + sourceName + '-' + order fp = open(outname + '-gtmap3.txt', 'w') for iT in range(len(tList)): t = tList[iT] for iG in range(len(gList)): g = gList[iG] #p0 = [0.00010, 0.869, 5.0, 35.0, xStar[0], median(xStar), xStar[-1]] p0 = [0.00010, 0.5, 0.0, 25.0, xStar[0], median(xStar), xStar[-1]] scl = [ 0.2 * p0[0], 0.1 * p0[1], 15.0, 25.0, p0[0] * 2.0, p0[0] * 2.0, p0[0] * 2.0 ] ymodel = protoModels[:, iT, iG] aresult = amoeba.amoeba(p0, scl, negchisq, data=(xmodel, ymodel)) p0 = aresult[0] scl = list(array(scl) / 2.0) aresult = amoeba.amoeba(p0, scl, negchisq, data=(xmodel, ymodel)) p0 = aresult[0] print -aresult[1], print t, g, for thing in aresult[0]: print thing, print '' if t >= 8.5 and t <= 28. and g >= 3.5 and g <= 5.5: print >> fp, -aresult[1], print >> fp, t, g, for thing in aresult[0]: print >> fp, thing,
def lattice(data, frozen_boson, n, ph_symmetry, accepted_mu_range=[-2.0,2.0]): def get_n(dt): dt.get_Gkw_direct() #gets Gkw from w, mu, epsilon and Sigma and X dt.get_Fkw_direct() #gets Fkw from w, mu, epsilon and Sigma and X dt.get_G_loc() #gets G_loc from Gkw dt.get_n_from_G_loc() if mpi.is_master_node(): print "supercond_hubbard: lattice" if (n is None) or ((n==0.5) and ph_symmetry): if n==0.5: #otherwise - nothing to be done data.mus['up'] = 0 if 'down' in data.fermionic_struct.keys(): data.mus['down'] = data.mus['up'] get_n(data) else: def func(var, data): mu = var[0] dt = data[0] #print "func call! mu: ", mu, " n: ",dt.ns['up'] n= data[1] dt.mus['up'] = mu if 'down' in dt.fermionic_struct.keys(): dt.mus['down'] = dt.mus['up'] get_n(dt) #print "funcvalue: ",-abs(n - dt.ns['up']) val = 1.0-abs(n - dt.ns['up']) if mpi.is_master_node(): print "amoeba func call: val = ",val if val != val: return -1e+6 else: return val if not MASTER_SLAVE_ARCHITECTURE: mpi.barrier() if mpi.is_master_node(): print "about to do mu search:" guesses = [data.mus['up'], 0.0, -0.1, -0.3, -0.4, -0.5, -0.7, 0.3, 0.5, 0.7] found = False for l in range(len(guesses)): varbest, funcvalue, iterations = amoeba(var=[guesses[l]], scale=[0.01], func=func, data = [data, n], itmax=30, ftolerance=1e-2, xtolerance=1e-2, known_max = 1.0, known_max_accr = 5e-5) if (varbest[0]>accepted_mu_range[0] and varbest[0]<accepted_mu_range[1]) and (abs(funcvalue-1.0)<1e-2): #change the bounds for large doping found = True func(varbest, [data, n]) break if l+1 == len(guesses): if mpi.is_master_node(): print "mu search FAILED: doing a scan..." mu_grid = numpy.linspace(-1.0,0.3,50) func_values = [func(var=[mu], data=[data,n]) for mu in mu_grid] if mpi.is_master_node(): print "func_values: " for i in range(len(mu_grid)): print "mu: ",mu_grid[i], " 1-abs(n-n): ", func_values[i] mui_max = numpy.argmax(func_values) if mpi.is_master_node(): print "using mu: ", mu_grid[mui_max] data.mus['up'] = mu_grid[mui_max] if 'down' in data.fermionic_struct.keys(): data.mus['down'] = data.mus['up'] get_n(data) if mpi.is_master_node() and found: print "guesses tried: ", l print "mu best: ", varbest print "1-abs(diff n - data.n): ", funcvalue print "iterations used: ", iterations data.get_Gtildekw() #gets Gkw-G_loc if not frozen_boson: data.get_Wqnu_from_func(func = dict.fromkeys(data.bosonic_struct.keys(), dyson.scalar.W_from_P_and_J)) #gets Wqnu from P and J data.get_W_loc() #gets W_loc from Wqnu, used in local bubbles data.get_Wtildeqnu()
f.write("1 1\n") f.write("%10.5f\n1.0\n" % (rcp,) ) f.write("%endblock PAO.Basis\n") f.close() atoms = ListOfAtoms([Atom('Ga', (0.0,0.0,0.0),magmom=0), Atom('Ga', (2.0,0.0,0.0),magmom=0)]) a = Siesta(executable="/Users/ag/bin/siesta-xlf") a.SetOption("%include","base.fdf") energy = a.run(atoms) print rcs, rcp, energy return -energy URLbase = "http://fisica.ehu.es/ag/siesta-psffiles/" # create a work subdirectory orig_dir = os.getcwd() dir = "simplex_work" if os.path.isdir(dir): # does dir exist? shutil.rmtree(dir) # yes, remove old directory os.mkdir(dir) # make dir directory os.chdir(dir) # move to dir urllib.urlretrieve(URLbase + "Ga.lda.psf", "Ga.psf") print amoeba.amoeba([4.25,4.25],[2.5,2.5],afunc,xtolerance=5.0e-2)
def optimize( runpath, resultspath, inputtemplatepath, vararr, runcommand, ftolerance=1.0e-4, xtolerance=1.0e-4, itmax=500 ): amoebalog = open(resultspath + "/amoeba.log", "w+") optimizelog = open(resultspath + "/optimize.log", "w+") optimizelog.write("runpath " + runpath + "\n") optimizelog.write("resultspath " + resultspath + "\n") optimizelog.write("inputtemplatepath " + inputtemplatepath + "\n") optimizelog.write("Variables:\n") inputtree = etree.parse(inputtemplatepath) inputdscr = "Input file generated by script optimize.py \n" inputdscr = inputdscr + "runpath: %s\n" % (runpath) inputdscr = inputdscr + "resultspath: %s\n" % (resultspath) inputdscr = inputdscr + "input template from path: %s\n" % (inputtemplatepath) inputdscr = inputdscr + "optimization varibales:\n" for var in vararr: optimizelog.write("xpath: " + var[0] + "\n") optimizelog.write("guess: " + str(var[1]) + "\n") optimizelog.write("scale: " + str(var[2]) + "\n") inputdscr = inputdscr + "xpath: " + str(var[0]) + "\n" inputdscr = inputdscr + "guess: " + str(var[1]) + "\n" inputdscr = inputdscr + "scale: " + str(var[2]) + "\n" optimizelog.flush() setinputxml.setByXpath(inputtree, "/input/keywords", inputdscr) inputtree.write(runpath + "/input.xml", pretty_print="true") guess = [] scale = [] data = [] data.append(runpath) data.append(runcommand) data.append([]) for var in vararr: data[-1].append(var[0]) # build xpath array guess.append(var[1]) # build array of initial guess values for the variables scale.append(var[2]) simplex, fvalue, iteration = amoeba.amoeba(guess, scale, energy, ftolerance, xtolerance, itmax, data, amoebalog) amoebalog.write("%d\t" % (iteration)) amoebalog.write("%1.10f\t" % (fvalue)) for value in simplex: amoebalog.write("%1.10f\t" % (value)) amoebalog.write("\n") amoebalog.flush() optimizelog.write("\nOptimization converged after %d iterations of amoeba.py\n" % (iteration)) optimizelog.write("\nOptimized variables found:\n") for i, var in enumerate(vararr): optimizelog.write("xpath: " + var[0] + "\n") optimizelog.write("optimized value: %1.0f\n\n" % (simplex[i])) optimizelog.flush() optimizelog.close() amoebalog.close()