# -*- coding: utf-8 -*-
"""
Created on Tue Sep  7 13:18:18 2021

@author: Joschka
"""

import matplotlib.pyplot as plt
import numpy as np

from src import coil_class as BC

from IPython import get_ipython

# get_ipython().run_line_magic('matplotlib', 'qt')

I = 1.25

HH_Coil = BC.BCoil(HH=1, distance=54, radius=48, layers=8, windings=8, wire_height=0.5, wire_width=0.5,
                   windings_spacing=0.25, layers_spacing=0.25)
HH_Coil.set_R_outer(49.3)
HH_Coil.set_d_min(49.8)
HH_Coil.print_info()
# todo: asdkjflö

# Fast_Coil = BC.BCoil(HH = 1, distance = 54 ,radius = 48 , layers = 8, windings = 8, wire_height =0.5, wire_width = 0.5,windings_spacing=0, layers_spacing = 0)
# Fast_Coil.set_R_outer(49.3)
# Fast_Coil.set_d_min(49.8)


# AHH_Coil = BC.BCoil(-1, 82 , 47.3 , 4, 6, wire_width= 1, wire_height= 1.5 ,layers_spacing = 0.25, windings_spacing= 0.25)

def I_t(Coil, I_0, t):
    L = Coil.induct_perry()

    # L = Coil.inductivity()
    R = Coil.resistance(22.5)
    print(f"L={L}")
    print(f" R= {R}")
    tau = L / R
    print(f" τ = {tau}")
    I = I_0 * (1 - np.exp(-R / L * t))
    return I


def I_t_2(Coil, I_0, t):
    L = Coil.induct_perry()

    # L = Coil.inductivity()
    R = 2 * Coil.resistance(22.5)
    print(f"L={L}")
    print(f" R= {R}")
    tau = L / R
    print(f" τ = {tau}")
    I = I_0 * (1 - np.exp(-R / L * t))
    return I


def U_t(t, U_0, t_f):
    if t < t_f:
        U = 2 * U_0 - U_0 / t_f * t
    else:
        U = U_0
    return U


test = np.vectorize(U_t)


def I_t_3(Coil, I_0, t_f, t):
    L = Coil.induct_perry()

    # L = Coil.inductivity()
    R = Coil.resistance(22.5)
    # print(f"L={L}")
    # print(f" R= {R}")
    # tau = L/R
    # print(f" τ = {tau}")
    # print(R*I_0)
    I = test(t, R * I_0, t_f * 1e-3) / R * (1 - np.exp(-R / L * t))
    return I


def I_current_exp(I_0, R, L, t):
    print("")
    print(L / R)
    I = I_0 * (1 - np.exp(-R / L * t))
    return I


def main():
    # execute some code here
    t = np.linspace(0, 0.002, 1000)

    # set up color
    color = iter(plt.cm.rainbow(np.linspace(0, 1, 5)))

    plt.figure(1)
    plt.subplot(2, 1, 1)
    plt.title("time response")
    plt.plot(t * 1e3, I_t(HH_Coil, I, t), label="R = R_coil, U = const. =  10 V ")
    plt.plot(t * 1e3, I_t_2(HH_Coil, I, t), label="R = 2 * R_coil, U = const. = 20 V")

    for t_f in np.arange(0.2, 1.2, 0.3):
        print(t_f)
        plt.plot(t * 1e3, I_t_3(HH_Coil, I, t_f, t), c=next(color), label=f"U overshoot, t_f = {t_f:.1f} ms")

    plt.xlabel("time [ms]")
    plt.ylabel("current I [A]")
    plt.legend()
    plt.show()

    color = iter(plt.cm.rainbow(np.linspace(0, 1, 5)))

    plt.subplot(2, 1, 2)
    for t_f in np.arange(0.2, 1.2, 0.3):
        plt.plot(t * 1e3, test(t, 10, t_f * 1e-3), c=next(color), label=f"U overshoot, t_f = {t_f:.1f} ms")

    plt.xlabel("time [ms]")
    plt.ylabel("voltage U [V]")
    plt.legend()

    plt.show()


if __name__ == "__main__":
    print("g")
    main()
