def gb_vs_iters(sigma_0, iters,strength):
	"""
	Plot Gilbert Damping as a function of iterations to test convergence.
	"""	
	with open('result_'+str(strength)+'_'+str(iters)+'.txt', 'w') as f:					
		system = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.05+0.j, -0.067+0.j, 1.+0.j,sigma_0,0.5,500,20,1)
		sys = system[0]
		sys.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(sys.hamil, 20, system[1],20),np.radians(0),0,0.02+0.j,20,20),20,20)		
		x_list = []
		y_list = []
		gb = calc.find_gb(0,0,randomize_hamil(sys,strength))
		for num in range(2,iters+2):
			system = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.05+0.j, -0.067+0.j, 1.+0.j,sigma_0,0.5,500,20,1)
			sys = system[0]
			sys.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(sys.hamil, 20, system[1],20),np.radians(0),0,0.02+0.j,20,20),20,20)			
			x_list.append(num)
			gb = (gb*(num-1) + calc.find_gb(0,0,randomize_hamil(sys,strength))) / num
			y_list.append(gb)
			f.write(str(gb) + '\n')
		fig = plt.figure()
		ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
		ax.plot(x_list,y_list,color='g')
		fig.savefig('output_'+str(strength)+'_'+str(iters)+'.png')
		plt.close(fig)		
		return gb
def test_gb_greens():
	sigma_0 = 1.e-4+1.e-4j	
	topo = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.05+0.j, -0.067+0.j, 1.+0.j,sigma_0,0.5,500,20,1)
	system = topo[0]
	system.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(system.hamil, 20, topo[1],20),0,0,0.02+0.j,20,20,padding=False),20,20)
	gb_1 = find_gb_theta_phi(0,0,system)
	system.greens_ret = mod_greens(system,1e-6)
	gb_2 = find_gb_theta_phi(0,0,system)
	return gb
def test_gb():
	energy_low = -0.11
	energy_high = -0.07
	efermi = -0.09
	sigma_0 = 1.e-4+1.e-4j
	temp = 0.01
	topo = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.01+0.j, 2.+0.j, 1.+0.j,sigma_0,0.5,500,20,1)
	system = topo[0]
	system.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(system.hamil, 20, topo[1],20),0,0,0.02+0.j,20,20),20,20)
	x_list = []
	y_list = []	
	for omega in [1e-2]:
		x_list.append(omega)
		y_list.append(find_gb_omega(omega,energy_low,energy_high,system,efermi,temp, 0.02))
	fig = plt.figure()
	ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
	ax.plot(x_list,y_list,color='g')
	plt.show()
def test_gb_greens_local_2():
	sigma_0 = 1.e-4+1.e-4j	
	topo = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.05+0.j, -0.067+0.j, 1.+0.j,sigma_0,0.5,500,20,1)
	system = topo[0]
	system.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(system.hamil, 20, topo[1],20),0,0,0.02+0.j,20,20,padding=False),20,20)
	system.greens_ret = mod_greens(system,1e-3)
	gb = find_gb_local(0,0,system)

	xs = []
	ys_1 = []
	ys_2 = []
	s = 0
	for i in range(gb[0].shape[0]):
		xs.append(i)
		ys_1.append(gb[0][i])
		ys_2.append(gb[2][i])
		s+=gb[0][i]
	plt.plot(xs,ys_1)
	plt.show()
def gb_versus_temp():
	sigma_0 = 1.e-4+1.e-4j
	for energy in range(-80,-50):
		with open('gbvstemp_result_'+str(energy/1000)+'.txt', 'w') as f:		
			xs = []
			ys = []
			energy = energy / 1000
			for temp in range(10,410,20):
				xs.append(temp)
				topo = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.05+0.j, energy, 1.+0.j,sigma_0,0.5,500,20,1)
				system = topo[0]
				system.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(system.hamil, 20, topo[1],20),0,0,0.01+0.j,20,20,padding=False),20,20)
				system.greens_ret = mod_greens(system,5e-3,magmom=100,boson_dist=boson_dist(0.01,temp))
				gb = find_gb_local(0,0,system)
				s = 0
				for i in range(gb[0].shape[0]):
					s+=gb[0][i]
				ys.append(s)
				f.write(str(energy)+' '+str(temp)+' '+str(s))
			plt.plot(xs,ys)
			plt.savefig('gb_vs_temp_' + str(energy)+'.png')
			plt.close()
def test_gb_greens_local():
	sigma_0 = 1.e-4+1.e-4j
	layers = 50
	sites = 20	
	topo = topological_insulator.make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.1+0.j, -51.2+0.j, -0.05+0.j, -0.067+0.j, 1.+0.j,sigma_0,0.5,500,sites,1)
	system = topo[0]
	system.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(system.hamil, layers, topo[1],sites),0,0,0.01+0.j,sites,layers,padding=False),sites,layers)
	system.greens_ret = mod_greens(system,1e-2,magmom=100)
	gb = find_gb_local(0,0,system,fm_width=layers)
	maj = gb[1]
	grid = maj.reshape((layers,sites))
	fig, ax = plt.subplots()
	cax = ax.imshow(grid, extent=(0, sites, 0, layers), interpolation='spline16', cmap=cm.OrRd)
	cbar = fig.colorbar(cax)
	#cbar.ax.set_yticklabels(['< -1', '0', '> 1'])
	fig.savefig('majority'+str(sites)+'x'+str(layers)+'.png')
	plt.close(fig)
	mino = gb[2]
	grid = mino.reshape((layers,sites))
	fig, ax = plt.subplots()
	cax = ax.imshow(grid, extent=(0, sites, 0, layers), interpolation='spline16', cmap=cm.OrRd)
	cbar = fig.colorbar(cax)
	#cbar.ax.set_yticklabels(['< -1', '0', '> 1'])
	fig.savefig('minority'+str(sites)+'x'+str(layers)+'.png')
	plt.close(fig)
	#return gb
	gil = gb[0]
	s = 0
	for i in range(gil.shape[0]):
		s+=gil[i]
	grid = gil.reshape((layers,sites))
	fig, ax = plt.subplots()
	cax = ax.imshow(grid, extent=(0, sites, 0, layers), interpolation='spline16', cmap=cm.OrRd)
	cbar = fig.colorbar(cax)
	#cbar.ax.set_yticklabels(['< -1', '0', '> 1'])
	fig.savefig('gilbert'+str(sites)+'x'+str(layers)+'.png')
	plt.close(fig)