from rates import *
from scipy.constants import pi, e, k, m_e
from toolbox_functions import uB, v_th, mass
import numpy as np
from objects_generator import Reaction


def differentials(t, y, ydot, case):
    """This function has been generated by parser.py at 2019-02-20 18:08:05.304430."""

    global thruster, params
    [thruster, params, chem] = case
    R = chem.R

    ng = + y[1]
    d = thruster.volume()/thruster.total_area()

    h = thruster.h(y[0], ng, y[3], y[4], params.B0, mass('Xe'), R['Xe']['e^-']['ELASTIC'].K(y[3]))

    # Species e^-
    ydot[0] = + (+1) * R['Xe']['e^-']['IONIZATION'].K(y[3]) * y[1] * y[0] \
        - (+ h * y[2] * uB(y[3], "Xe") ) * d**(-1)

    # Species Xe
    ydot[1] = + (-1) * R['Xe']['e^-']['IONIZATION'].K(y[3]) * y[1] * y[0] \
        + (params.Q0 / thruster.volume()) \
        - 0.25 * y[1] * v_th(y[4], "Xe") * thruster.open_area_neutrals()/thruster.volume()\
        + h * y[2] * uB(y[3], "Xe") * (thruster.total_area() - thruster.open_area_ions())/thruster.volume()

    # Species Xe^+
    ydot[2] = + (+1) * R['Xe']['e^-']['IONIZATION'].K(y[3]) * y[1] * y[0] \
        \
        - h * y[2] * uB(y[3], "Xe") * d**(-1) 

    # Electronic temperature
    Ploss =\
        + R['Xe']['e^-']['IONIZATION'].K(y[3]) * e * R['Xe']['e^-']['IONIZATION'].energy_loss(y[3]-y[4])* y[1] * y[0] \
        + R['Xe']['e^-']['EXCITATION'].K(y[3]) * e * R['Xe']['e^-']['EXCITATION'].energy_loss(y[3]-y[4])* y[1] * y[0] \
        + R['Xe']['e^-']['EXCITATION'].K(y[3]) * e * R['Xe']['e^-']['EXCITATION'].energy_loss(y[3]-y[4])* y[1] * y[0] \
        + R['Xe']['e^-']['EXCITATION'].K(y[3]) * e * R['Xe']['e^-']['EXCITATION'].energy_loss(y[3]-y[4])* y[1] * y[0] \
        + R['Xe']['e^-']['ELASTIC'].K(y[3]) * e * R['Xe']['e^-']['ELASTIC'].energy_loss(y[3]-y[4])* y[1] * y[0] \
        + 2 * e * y[3] * h * y[0] * uB(y[3], "Xe") * d**(-1)\
        + 0.5 * e * y[3] * h * y[0] * uB(y[3], "Xe") * d**(-1)\
        + 0.5 * np.log(mass("Xe^+") / (2 * pi * m_e)) * e * y[3] * h * y[0] * uB(y[3], "Xe^+") * d**(-1)

    ydot[3] = (y[0] * e)**(-1) * ((2/3)*(params.Pabs - Ploss) - e * y[3] * ydot[0])

    # Gas temperature
    ydot[4] = 0
