def gb_vs_iters_disp(sigma_0,iters,strength):
	"""
	Calculate Gilbert Damping versus number of iterations for displacement regime.
	"""
	with open('result_disp_'+str(strength)+'_'+str(iters)+'.txt', 'w') as f:					
		system = topological_insulator.make_topo_displacement(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,strength)
		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,sys)
		for num in range(2,iters+2):
			system = topological_insulator.make_topo_displacement(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,strength)
			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,sys)) / 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_disp_'+str(strength)+'_'+str(iters)+'.png')
		plt.close(fig)		
		return gb
def test_topo_soc_cond_2():	
	"""
	Test conductance with spin-orbit coupling included - version 2.
	"""
	sigma_0 = 1.e-4+1.e-4j
	N = 10
	f, ((ax1, ax2, ax3), (ax4, ax5, ax6), (ax7,ax8,ax9)) = plt.subplots(3, 3, sharex='col', sharey='row')
	ax_dict = {1: ax1, 2: ax2, 3: ax3, 4: ax4, 5: ax5, 6: ax6, 7: ax7, 8: ax8, 9: ax9}
	i = 1	
	topo = make_topo(50.+0.j, 3.645+0.j, -68.6+0.j, -0.09+0.j, -51.2+0.j, -0.01+0.j, -0.09+0.j, 1.+0.j,sigma_0,0.5,500,N,1.)	
	topo_sys = topo[0]
	t_x = topo[1]
	hamil = topo_sys.hamil
	for energy in range(-15,-6, 1):		
		x_list = []
		y_list = []
		phi_rad = 0
		#energy = energy / 100
		topo_sys = topo[0]
		topo_sys.update_energy( energy / 100)
		for theta in range(0,180,18):
			theta_rad = np.radians(theta)		
			hamil = calc.make_hamil_layers(topo_sys.hamil, 50, topo[1],N)
			theta_rad = theta * (np.pi / 180)	
			topo_sys.update_hamil_2(calc.build_fm_island(hamil,theta_rad,phi_rad,0.02+0.j,N,50),N,50)
			if np.array_equal(topo_sys.hamil, hamil):
				print("unchanged")
			x_list.append(theta_rad)
			y_list.append(-topo_sys.cond)
		ax_dict[i].plot(x_list,y_list)
		i += 1
	plt.show()
def inf_rashba_integrand(k,energy,layers=20,so_strength=0.0, theta=0.0, randomize=False,rando_strength=0.0, collector=[]):
	"""
	Integrand function for infinite Rashba plane k-integration.
	"""
	sigma_0 = 1.e-4+1.e-4j
	sites = 1
	mag = 100
	ener = energy
	delt = 0.1
	on_site = -0.067+0.j
	rashba_sys = build_rashba(on_site,1,1,so_strength,sigma_0, 500, 0.5, sites, 1,ener+0.j)
	ras = rashba_sys[0]
	if randomize:
		ras = gd.randomize_hamil(ras,rando_strength,orbitals=1)
	t_x = rashba_sys[1]
	hamil_new = ras.hamil + ras.t_y * np.exp((k)*1.j) + calc.hermitian(ras.t_y) * np.exp((-k)*1.j)
	hamil_se = sparse.kron(on_site,np.identity(2)) + ras.t_y * np.exp((k)*1.j) + calc.hermitian(ras.t_y) * np.exp((-k)*1.j)
	hamil_new = calc.build_fm_island(calc.make_hamil_layers(hamil_new, layers, t_x,sites),theta,0,delt+0.j,sites,layers,padding=False,topo=False)
	off_diag = np.diag(hamil_new,1)
	print(off_diag)
	ras.update_hamil_2(hamil_new, sites, layers,no_mag_leads = True, hamil_sub = hamil_se)
	gb = gd.find_gb_local(theta,0,ras,fm_width=layers,n_orbitals=1,topo=False,delta=delt)
	gil = gb[0]
	s = 0
	for i in range(gil.shape[0]):
		s+=gil[i]
	print("k = " + str(k))
	print("s = " + str(s))
	if randomize:
		collector.append(s)	
	return s	
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_rashba_local():
	"""
	Compute and plot the local Gilbert Damping and LDOS of finite Rashba plane.
	"""	
	process = psutil.Process(os.getpid())
	sigma_0 = 1.e-4+1.e-4j
	layers = 100
	sites = 20
	so_strength = 0.01
	mag = 100
	ener = -0.2
	delt = 0.1
	rashba_sys = build_rashba(-0.067+0.j,1,1,so_strength,sigma_0, 500, 0.5, sites, 1,ener+0.j)
	ras = rashba_sys[0]
	t_x = rashba_sys[1]
	ras.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(ras.hamil, layers, t_x,sites),0,0,delt+0.j,sites,layers,padding=False,topo=False),sites,layers)
	ras.greens_ret = gd.mod_greens(ras,1e-2,magmom=mag,delta=delt)
	gb = gd.find_gb_local(0,0,ras,fm_width=layers,n_orbitals=1,topo=False,delta=delt)
	
	maj = gb[1]
	print(maj.size)
	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)
	fig.savefig('rashba_majority'+str(sites)+'x'+str(layers)+'SO'+str(so_strength)+'magmom'+str(mag)+'E'+str(ener)+'.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)
	fig.savefig('rashba_minority'+str(sites)+'x'+str(layers)+'SO'+str(so_strength)+'magmom'+str(mag)+'E'+str(ener)+'.png')
	plt.close(fig)
	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)
	fig.savefig('rashba_gilbert'+str(sites)+'x'+str(layers)+'SO'+str(so_strength)+'magmom'+str(mag)+'E'+str(ener)+'.png')
	plt.close(fig)
	print("gilbert = " + str(s))
	print(process.memory_info().rss)
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 inf_rashba_integrand_cond(k, energy, second=False,layers=20):
	"""
	Integrand for calculating conductance of infinite Rashba Plane
	"""	
	sigma_0 = 1.e-4+1.e-4j
	sites = 1
	so_strength = 0.00
	mag = 100
	delt = 0.1
	on_site = -0.067+0.j
	rashba_sys = build_rashba(on_site,1,1,so_strength,sigma_0, 500, 0.5, sites, 1,energy+0.j)
	ras = rashba_sys[0]
	t_x = rashba_sys[1]	
	hamil_new = ras.hamil + ras.t_y * np.exp((k)*1.j) + calc.hermitian(ras.t_y) * np.exp((-k)*1.j)
	hamil_se = sparse.kron(np.identity(2),on_site) + ras.t_y * np.exp((k)*1.j) + calc.hermitian(ras.t_y) * np.exp((-k)*1.j)
	hamil_new = calc.build_fm_island(calc.make_hamil_layers(hamil_new, layers, t_x,sites),np.radians(45),0,delt+0.j,sites,layers,padding=False,topo=False)
	ras.update_hamil_2(hamil_new, sites, layers,no_mag_leads = True, hamil_sub = hamil_se)
	cond = -ras.cond
	return cond
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 build_fm_topo():
	"""
	Build a topological insulator with a ferromagnetic island.
	"""	
	sigma_0 = 1.e-4+1.e-4j
	topo = 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)
	topo_sys = topo[0]
	topo_sys.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(topo_sys.hamil, 20, topo[1],20),np.radians(0),0,0.02+0.j,20,20),20,20)
	x_list = []
	y_list = []	
	for theta in range(0,180,18):
		theta_rad = np.radians(theta)	
		gb = calc.find_gb(theta_rad,0,topo_sys)
		x_list.append(theta)
		y_list.append(gb)
		
	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 self_energy_vs_location():
	"""
	Plot the self-energy of the system versus site location.
	"""
	sigma_0 = 1.e-4+1.e-4j
	sites = 5
	so_strength = 0.01
	mag = 100
	delt = 0.1
	on_site = -0.067+0.j
	layers = 20
	rashba_sys = build_rashba(on_site,1,1,so_strength,sigma_0, 500, 0.5, sites, 1,energy+0.j)
	ras = rashba_sys[0]
	t_x = rashba_sys[1]	
	hamil_new = ras.hamil + ras.t_y * np.exp((k)*1.j) + calc.hermitian(ras.t_y) * np.exp((-k)*1.j)
	hamil_se = sparse.kron(np.identity(2),on_site) + ras.t_y * np.exp((k)*1.j) + calc.hermitian(ras.t_y) * np.exp((-k)*1.j)
	hamil_new = calc.build_fm_island(calc.make_hamil_layers(hamil_new, layers, t_x,sites),np.radians(45),0,delt+0.j,sites,layers,padding=False,topo=False)
	ras.update_hamil_2(hamil_new, sites, layers,no_mag_leads = True, hamil_sub = hamil_se)
	greens = ras.greens_ret
	self_e = self_energy_integrand(greens, 0, 0, delta)
	print(self_e)
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_rashba_cond():
	"""
	Test conductance of Rashba plane over range of energies.
	"""
	sigma_0 = 1.e-4+1.e-4j
	layers = 30
	sites = 20
	so_strength = 0.01
	mag = 100
	xs = []
	ys = []
	for energy in range(-600,600, 5):
		rashba_sys = build_rashba(-0.067+0.j,1,1,so_strength,sigma_0, 500, 0.5, sites, 1,energy/1000)
		ras = rashba_sys[0]
		t_x = rashba_sys[1]
		ras.update_hamil_2(calc.build_fm_island(calc.make_hamil_layers(ras.hamil, layers, t_x,sites),0,0,0.01+0.j,sites,layers,padding=False,topo=False),sites,layers)
		xs.append(energy/1000)
		ys.append(-ras.cond)
	fig = plt.figure()
	ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
	ax.plot(xs,ys,color='g')
	fig.savefig('rashba_cond'+str(sites)+'x'+str(layers)+'SO'+str(so_strength)+'magmom'+str(mag)+'.png')
	plt.close(fig)
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)