# -*- 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
x = np.linspace(-50, 50, 10001)
z = np.linspace(-50, 50, 10001)



d=81.8

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


h =AHH_Coil.get_coil_height()
w = AHH_Coil.get_coil_width()

vert_surf = h * 46.875*1e-3 *2 *np.pi 
hor_surf = np.pi*(AHH_Coil.get_R_outer()**2-AHH_Coil.get_R_inner()**2)

tot = 2*vert_surf + 2*hor_surf
print(f"Surface area = {tot}")

print(AHH_Coil.get_coil_height())
print(AHH_Coil.get_coil_width())

I = 10
AHH_Coil.print_info()
R = AHH_Coil.resistance(30)
print(f"R = {R} ")
#B = AHH_Coil.B_multiple_3d(10, x,z,raster=2)
AHH_Coil.cooling(I,30)

B_z,B_x = AHH_Coil.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)

B_z_grad = BC.BCoil.grad(B_z, z)
B_x_grad = BC.BCoil.grad(B_x, x)

lim = 7000
B_0 = B_z_grad[5000] 
print((B_0- B_z_grad[6700]))
print((B_0- B_z_grad[6700])/B_0)


plt.subplot(2,1,1)

plt.plot(z,B_z,linestyle = "solid", label = f"$Bz$, d = {d} mm")
#plt.plot(z,B_tot_z, label = "B_tot_z")
plt.plot(x,B_x, label = f"$B_x$, d = {d} mm")
#plt.plot(z,B_tot_x, label = "B_tot_x")
#plt.xlim(-0.01,0.01)
plt.title("B-field" )
#plt.ylim(-0.5,0.4)
plt.ylabel(r"$B$ [G]")
plt.xlabel("z-axis / x-axis [mm]")
plt.legend()

plt.subplot(2,1,2)
plt.plot(z,B_z_grad,linestyle = "solid", label = r"$\nabla_z Bz$")
plt.plot(x,B_x_grad,linestyle = "solid", label = r"$\nabla_x B_x$")

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.savefig("output/AHH_field.pdf")
plt.show()


#AHH_Coil.plot_3d(I, 80, 80)
#print(B_z_grad[1500])
#print(2*B_x_grad[1500])
"""
print(" ")
print(f"B_grad_z(0) = {B_z_grad[1500]} G/cm")
print(f"B_grad_z(10 mm) = {B_z_grad[1800]} G/cm")
print(f"Diff B_grad z 10mm - 0 mm, {-(B_z_grad[1800]-B_z_grad[1500])} G/cm, relative: {(B_z_grad[1800]-B_z_grad[1500])/-B_z_grad[1500]}")
print(" ")
print(f"B_grad_x(0) = {B_x_grad[1500]} G/cm")
print(f"B_grad_x(10 mm) = {B_x_grad[1800]} G/cm")
print(f"Diff B_grad x 10mm - 0 mm, {B_x_grad[1800]-B_x_grad[1500]} G/cm, relative: {(B_x_grad[1800]-B_x_grad[1500])/-B_x_grad[1500]}")
"""
