# -*- coding: utf-8 -*-
"""
Created on Mon Sep 20 18:01:04 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 src import physical_constants as cs

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

AHH_opt = BC.BCoil(HH = 1, distance = 70, radius = 60, layers = 2, windings = 10, wire_height = 1, wire_width = np.pi/4)
I = 5
AHH_opt.cooling(I, 36)
print(f"res = {AHH_opt.resistance(36)/2:.2f} Ohm")
h = AHH_opt.get_coil_height()
w = AHH_opt.get_coil_width()

vert_surf = h * AHH_opt.radius * 2 *np.pi 
hor_surf = np.pi*(AHH_opt.get_R_outer()**2-AHH_opt.get_R_inner()**2)

S_coil = 2*vert_surf + 2*hor_surf #+5e-3
#S_coil = S_coil/2
print(f"Surface area = {S_coil}")

def power_bal(T,h_air):
    T_0 = 22.5
    
    f = h_air * S_coil *(T-T_0)  - 0.5*AHH_opt.power(I, T)
    return f

#print(e_cu * S_coil * cs.sigma_B**4 * (50**4 - 22.5**4))
T = np.linspace(20,120,500)
T_calc = np.linspace(20,2200,1000)

for h_air in [2.5,10,25]:
    pos_min = np.argmin(np.abs(power_bal(T_calc,h_air)))
    T_SS = T_calc[pos_min]
    print(f"T_ss = {T_SS} °C")
    plt.plot(T,power_bal(T,h_air),label = f"$h_{{air}} = {h_air} \; W/m^2 K$ , $T_{{SS}}$ = {T_SS:.2f}°C")    
plt.ylabel("Power balance [W]")
plt.xlabel("temparature [°C]")
plt.title(f"Power balance, free convection, Weidemüller Coil, I = {I} A, windings: 2 x 10")
plt.legend()
plt.show()

print(AHH_opt.power(I, 940)/2)
