# -*- coding: utf-8 -*-
"""
Created on Mon Aug 23 17:40:37 2021

@author: Joschka
"""


import matplotlib.pyplot as plt
import numpy as np
#from src import B_field_calculation as bf

from src import coil_class as BC

from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'qt')
#get_ipython().run_line_magic('matplotlib', 'inline')

#set up axis

#set up axis
lim = 10001
zr = lim//2
x = np.linspace(-50, 50, lim)
z = np.linspace(-50, 50, lim)



d_opt=81.8
d_comp = 69.4

AHH_opt = BC.BCoil(HH = -1, distance = d_opt ,radius =  46.875 ,layers = 4, windings = 4 , wire_width= 1, wire_height= 2 ,layers_spacing = 0.25, windings_spacing= 0.25)
AHH_opt = BC.BCoil(HH = -1, distance = d_opt ,radius =  46.875 ,layers = 8, windings = 2 , wire_width= 1, wire_height= 2 ,layers_spacing = 0.25, windings_spacing= 0.25)
AHH_opt.set_R_outer(49.25)


AHH_comp = BC.BCoil(HH = -1, distance = d_comp ,radius =  46.875 ,layers = 4, windings = 4 , wire_width= 1, wire_height= 2 ,layers_spacing = 0.25, windings_spacing= 0.25)


I = 10
print("Optimum configuration:")
AHH_opt.print_info()

print("Not cutting optical axis:")
AHH_comp.print_info()


Bz_opt, Bx_opt = AHH_opt.B_field(I, x, z)
Bz_comp, Bx_comp = AHH_comp.B_field(I, x, z)


#Bz = B[:,150,1]
#B_x = B[150,:,0]
#B_tot_z, B_tot_x = AHH_Coil.B_tot_along_axis(I, x, z)

Bz_grad_opt = BC.BCoil.grad(Bz_opt, z)
Bx_grad_opt = BC.BCoil.grad(Bx_opt, x)

Bz_grad_comp = BC.BCoil.grad(Bz_comp, z)
Bx_grad_comp = BC.BCoil.grad(Bx_comp, x)

Bz_rel_opt = (Bz_grad_opt[zr]- Bz_grad_opt)/Bz_grad_opt[zr]*100
Bx_rel_opt = (Bx_grad_opt[zr]- Bx_grad_opt)/Bx_grad_opt[zr]*100

Bz_rel_comp = (Bz_grad_comp[zr]- Bz_grad_comp)/Bz_grad_comp[zr]*100
Bx_rel_comp = (Bx_grad_comp[zr]- Bx_grad_comp)/Bx_grad_comp[zr]*100

plt.figure(figsize = (10,30))
plt.tight_layout()
plt.subplot(3,1,1)

plt.plot(z,Bz_opt,linestyle = "solid", color = "orange", label = f"$B_z$, d = {d_opt} mm")
plt.plot(z,Bz_comp,linestyle = "solid",color = "blue", label = f"$B_z$, d = {d_comp} mm")

plt.plot(x,Bx_opt, linestyle = "dashed", color = "orange", label = f"$B_x$, d = {d_opt} mm")
plt.plot(x,Bx_comp,linestyle = "dashed", color = "blue", label = f"$B_x$, d = {d_comp} mm")
#plt.plot(z,B_tot_x, label = "B_tot_x")
#plt.xlim(-0.01,0.01)
plt.title("B-field" )

plt.ylabel(r"$B$ [G]")
plt.xlabel("z-axis / x-axis [mm]")
plt.legend()



plt.subplot(3,1,2)
plt.plot(z,Bz_grad_opt,linestyle = "solid", color = "orange",label = f"$B_z$, d = {d_opt} mm")
plt.plot(z,Bz_grad_comp,linestyle = "solid",color = "blue", label = f"$B_z$, d = {d_comp} mm")

plt.plot(x,Bx_grad_opt,linestyle = "dashed",color = "orange", label = f"$B_x$, d = {d_opt} mm")
plt.plot(x,Bx_grad_comp, linestyle = "dashed",color = "blue", label = f"$B_x$, d = {d_comp} mm")

plt.ylabel(r"$\nabla_i B_i [G/cm]$")
plt.xlabel("z-axis /x-axis [mm]")#plt.xlim(-10,10)
#plt.title("Gradient of B-field")
plt.legend()

plt.subplot(3,1,3)
plt.plot(z,Bz_rel_opt,linestyle = "solid", color = "orange",label = f"$B_z$, d = {d_opt} mm")
plt.plot(z,Bz_rel_comp,linestyle = "solid",color = "blue", label = f"$B_z$, d = {d_comp} mm")

plt.plot(x,Bx_rel_opt,linestyle = "dashed",color = "orange", label = f"$B_x$, d = {d_opt} mm")
plt.plot(x,Bx_rel_comp, linestyle = "dashed",color = "blue", label = f"$B_x$, d = {d_comp} mm")

plt.ylabel(r"rel. Deviation from grad to center [%]$")
plt.xlabel("z-axis /x-axis [mm]")#plt.xlim(-10,10)
#plt.title(r"$\nabla_i B_i")
#plt.ylim(-0.05,0.05)
plt.xlim(-10,10)
plt.legend()

plt.savefig("output/AHH_field.pdf")

plt.show()
print("")
print(" 10 μm")
print(f"Optimum: Dev. of gradient (z) +- 10μm to center: {Bz_rel_opt[zr+1]:.8f} %")
print(f"Not cutting opt. axis: Dev. of gradient (z) +- 10μm to center: {Bz_rel_comp[zr+1]:.8f} %")
print("")
print(" 1mm ")
print(f"Optimum: Dev. of gradient (z) +- 1 mm to center: {Bz_rel_opt[zr+100]:.6f} %")
print(f"Not cutting opt. axis: Dev. of gradient (z) +- 1 mm to center: {Bz_rel_comp[zr+100]:.3f} %")

print("")
print(" 10mm ")
print(f"Optimum: Dev. of gradient (z) +- to center: {Bz_rel_opt[zr+1000]:.2f} %")
print(f"Not cutting opt. axis: Dev. of gradient (z) to center: {Bz_rel_comp[zr+1000]:.2f} %")

print("")
print(" 17mm ")
print(f"Optimum: Dev. of gradient (z) +- 1 mm to center: {Bz_rel_opt[zr+1700]:.2f} %")
print(f"Not cutting opt. axis: Dev. of gradient (z) +- 1 mm to center: {Bz_rel_comp[zr+1700]:.2f} %")
