import matplotlib.pyplot as plt
import numpy as np
import matplotlib
#matplotlib.use('Qt5Agg')
from src import coil_class as BC

scale = 1000
lim = 1
nr_points = (2 * lim) * scale + 1
x = np.linspace(-lim,lim,nr_points)
z = np.linspace(-lim,lim,nr_points)

def mu_it(x_pos):
    it = nr_points//2 + x_pos
    return it


Wires = [[0.45, 0.514],[0.475, 0.543],[0.5, 0.568]]

Wire_1 = Wires[2]

#I_current = 0.94
HH_Coil = BC.BCoil(HH = 1, distance = 54, radius = 48, layers = 8, windings = 8, wire_height = Wire_1[0],
                   wire_width = Wire_1[0], insulation_thickness=(Wire_1[1] - Wire_1[0]) / 2, is_round = True,
                   winding_scheme= 2)


print(f"HH N = {HH_Coil.get_N()}")
I = 64 / HH_Coil.get_N() * 1.25

# set radius plus distance
HH_Coil.set_R_outer(50.5 - HH_Coil.get_tot_wire_width()*1e3)
HH_Coil.set_d_min(47.15)

# HH_Coil.B_quick_plot(I)
# HH_Coil.B_curv_quick_plot(I)
# HH_Coil.plot_raster()
HH_Coil.print_info()

D_max = 2 * (HH_Coil.get_R_inner()*1e3 - 1) * np.tan(np.radians(41.11))
print(D_max)

AHH_Coil = BC.BCoil(HH = -1, distance = 54, radius = 48, layers = HH_Coil.get_layers, windings=2 * HH_Coil.get_windings,
                    wire_height = Wire_1[0], wire_width=Wire_1[0], insulation_thickness=(Wire_1[1] - Wire_1[0]) / 2,
                    is_round = True, winding_scheme= 2)

AHH_Coil.set_R_inner(HH_Coil.get_R_inner() * 1e3)
AHH_Coil.set_d_max(D_max)

AHH_Coil.print_info()
print(f"AHH N = {AHH_Coil.get_N()}")
I_grad = I - 0.128 # 8 x 9#128 / AHH_Coil.get_N() * I

I_grad = I - 0.15 # 8 x 8

print(f"current @ 6G/cm: {I_grad} A")
AHH_Coil.B_grad_quick_plot(I_grad)
#Bz, Bx = AHH_Coil.B_field(I)
AHH_Coil.cooling(I_grad, 22.5)

#Bz, Bx = AHH_Coil.B_field(I_grad, x, z, raster = 7)
# Bz_grad = BC.BCoil.grad(Bz,z)
# Bx_grad = BC.BCoil.grad(Bx,x)

#AHH_Coil.B_quick_plot(I_grad)
#AHH_Coil.B_grad_quick_plot(I_grad)
AHH_Coil.plot_raster(raster_value= 11)
AHH_Coil.plot_3d(I,50,50)

# zero = mu_it(0)
# print(f"Bz_grad({z[zero]}) = {Bz_grad[zero]} G/cm")
# mu = mu_it(1)
# mm = mu_it(1000)
# mid = mu_it(5000)
# outer = mu_it(15000)
# # Bz0 = Bz_grad[zero]
#
#
#
# print(f"Bz_grad({z[mu]} mm) - Bz_grad (0) = {Bz0 - Bz_grad[mu]}, relative = {(Bz0 - Bz_grad[mu])/Bz0}")
# print(f"Bz_grad({z[mm]} mm) - Bz_grad (0) = {Bz0 - Bz_grad[mm]}, relative = {(Bz0 - Bz_grad[mm])/Bz0}")
# print(f"Bz_grad({z[mid]} mm) - Bz_grad (0) = {Bz0 - Bz_grad[mid]}, relative = {(Bz0 - Bz_grad[mid])/Bz0}")
# print(f"Bz_grad({z[outer]} mm) - Bz_grad (0) = {Bz0 - Bz_grad[outer]}, relative = {(Bz0 - Bz_grad[outer])/Bz0}")
