/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000059.xml diagram  at 2008.03.20 15:04:44
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000059 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        _compartment_ADP_cyt_ = 4000 - x[0];
        pvca = 1/(1 + Math.exp((Vcah - x[5])/Kcah));
        f_CRAN = -gm_CRAN/(1 + Math.exp((x[2] - Kcar)/3));
        p_CRAN = x[5] - V_CRAN;
        pna = 1/(1 + Math.exp((104 + x[5])/8));
        f_5 = f5_ast*Math.exp(x[5]/(2*TV));
        b_5 = b5_ast*Math.exp(-x[5]/(2*TV));
        F1 = f_1*Math.pow(x[4], 3);
        F4 = f_4*Math.pow(K_ec, 2);
        F5 = f_5*x[0];
        B2 = b_2*_compartment_ADP_cyt_;
        Vca = TV/2*Math.log(Ca_ec/x[1]);
        B3 = b_3*Math.pow(Na_ec, 3);
        B4 = b_4*P;
        B6 = b_6*Math.pow(Ksup, 2);
        D = f_2*f_3*F4*F5*f_6 + b_1*f_3*F4*F5*f_6 + b_1*B2*F4*F5*f_6 + b_1*B2*B3*F5*f_6 + b_1*B2*B3*B4*f_6 + b_1*B2*B3*B4*b_5;
        f_Ca = Math.pow(x[1], 4)/(Math.pow(x[1], 4) + Math.pow(Kkca, 4));
        Okatp = (0.08*(1 + 0.33*(_compartment_ADP_cyt_/Kdd)) + 0.89*Math.pow((0.165*(_compartment_ADP_cyt_/Kdd)), 2))/(Math.pow((1 + 0.165*(_compartment_ADP_cyt_/Kdd)), 2)*(1 + 0.135*(_compartment_ADP_cyt_/Ktd) + 0.05*(x[0]/Ktt)));
        n_infinity = 1/(1 + Math.exp((Vn - x[5])/Sn));
        tau_n = c/(Math.exp((x[5] - Vtau)/a) + Math.exp((Vtau - x[5])/b));
        I_Vca = gmvca*pvca*(x[5] - Vca);
        I_CaPump = Pmcap*Math.pow(x[1], 2)/(Math.pow(Kcap, 2) + Math.pow(x[1], 2));
        Vk = TV*Math.log(K_ec/K_cyt);
        I_NaCa = gnaca*Math.pow(x[1], 5)*(x[5] - Vnaca)/(Math.pow(x[1], 5) + Math.pow(Knaca, 5));
        I_CRAN = f_CRAN*p_CRAN*(x[5] - Vna);
        I_Na = gmna*pna*(x[5] - Vna);
        I_NaK = Pnak*(F1*f_2*f_3*F4*F5*f_6 - b_1*B2*B3*B4*b_5*B6)/D;
        I_KDr = gmKDr*x[6]*(x[5] - Vk);
        I_KCa = gmkca*f_Ca*(x[5] - Vk);
        I_KATP = gmkatp*Okatp*(x[5] - Vk);
        Jerp = Pcaer*Math.pow(x[1], 2)/(Math.pow(x[1], 2) + Math.pow(Kcarp, 2));
        O_infinity = x[1]*Math.pow(x[3], 3)/((x[1] + Krca)*(Math.pow(x[3], 3) + Math.pow(Kip3, 3)));
        Jout = (Pleak + Pip3*O_infinity)*(x[2] - x[1]);
        Vna = TV*Math.log(Na_ec/x[4]);
        Vnaca = TV*(3*Math.log(Na_ec/x[4]) - Math.log(Ca_ec/x[1]));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_ATP_Ca_dependent_consumption = compartment*ATP_Ca_dependent_consumption_katpca*x[1]*x[0];
        rate_ATP_Capump = compartment*I_CaPump/(ATP_Capump_Vi*ATP_Capump_F);
        rate_ATP_Jerp = compartment*Jerp/2;
        rate_ATP_NaKATPase = compartment*I_NaK/(ATP_NaKATPase_Vi*ATP_NaKATPase_F);
        rate_ATP_consumption = compartment*ATP_consumption_katp*x[0];
        rate_ATP_production = compartment*ATP_production_kadp*_compartment_ADP_cyt_;
        rate_Calcium_cyt_Icapump = compartment*Calcium_cyt_Icapump_fi*2*I_CaPump/(2*Calcium_cyt_Icapump_F*Calcium_cyt_Icapump_Vi);
        rate_Calcium_cyt_Inaca = compartment*Calcium_cyt_Inaca_fi*2*I_NaCa/(2*Calcium_cyt_Inaca_F*Calcium_cyt_Inaca_Vi);
        rate_Calcium_cyt_Ivca = compartment*Calcium_cyt_Ivca_fi*(-I_Vca)/(2*Calcium_cyt_Ivca_F*Calcium_cyt_Ivca_Vi);
        rate_Calcium_cyt_Jerp = compartment*Calcium_cyt_Jerp_fi*Jerp;
        rate_Calcium_cyt_Jout = compartment*Calcium_cyt_Jout_fi*Jout/Calcium_cyt_Jout_Vi;
        rate_Calcium_cyt_sequestration = compartment*Calcium_cyt_sequestration_ksg*x[1];
        rate_Calcium_er_Jerp = compartment*Calcium_er_Jerp_fer*Jerp*Calcium_er_Jerp_Vi/Calcium_er_Jerp_Ver;
        rate_Calcium_er_Jout = compartment*Calcium_er_Jout_fer*Jout/Calcium_er_Jout_Ver;
        rate_IP3_degradation = compartment*IP3_degradation_kdip*x[3];
        rate_IP3_synthesis = compartment*IP3_synthesis_kip*Math.pow(x[1], 2)/(Math.pow(x[1], 2) + Math.pow(IP3_synthesis_Kipca, 2));
        rate_Na_Icran = compartment*(-I_CRAN)/(Na_Icran_Vi*Na_Icran_F);
        rate_Na_Ina = compartment*(-I_Na)/(Na_Ina_Vi*Na_Ina_F);
        rate_Na_Inaca = compartment*3*I_NaCa/(Na_Inaca_Vi*Na_Inaca_F);
        rate_Na_Inak = compartment*3*I_NaK/(Na_Inak_Vi*Na_Inak_F);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _compartment_ADP_cyt_ = 3067.9; // initial value of $"compartment.ADP_cyt"
        compartment = 1.0; // initial value of $compartment
        ATP_Ca_dependent_consumption_katpca = 5.0E-5; // initial value of ATP_Ca_dependent_consumption_katpca
        ATP_Capump_F = 9.6485E16; // initial value of ATP_Capump_F
        ATP_Capump_Vi = 7.64E-13; // initial value of ATP_Capump_Vi
        ATP_NaKATPase_F = 9.6485E16; // initial value of ATP_NaKATPase_F
        ATP_NaKATPase_Vi = 7.64E-13; // initial value of ATP_NaKATPase_Vi
        ATP_consumption_katp = 5.0E-5; // initial value of ATP_consumption_katp
        ATP_production_kadp = 3.7E-4; // initial value of ATP_production_kadp
        Ca_ec = 2600.0; // initial value of Ca_ec
        Calcium_cyt_Icapump_F = 9.6485E16; // initial value of Calcium_cyt_Icapump_F
        Calcium_cyt_Icapump_Vi = 7.64E-13; // initial value of Calcium_cyt_Icapump_Vi
        Calcium_cyt_Icapump_fi = 0.01; // initial value of Calcium_cyt_Icapump_fi
        Calcium_cyt_Inaca_F = 9.6485E16; // initial value of Calcium_cyt_Inaca_F
        Calcium_cyt_Inaca_Vi = 7.64E-13; // initial value of Calcium_cyt_Inaca_Vi
        Calcium_cyt_Inaca_fi = 0.01; // initial value of Calcium_cyt_Inaca_fi
        Calcium_cyt_Ivca_F = 9.6485E16; // initial value of Calcium_cyt_Ivca_F
        Calcium_cyt_Ivca_Vi = 7.64E-13; // initial value of Calcium_cyt_Ivca_Vi
        Calcium_cyt_Ivca_fi = 0.01; // initial value of Calcium_cyt_Ivca_fi
        Calcium_cyt_Jerp_fi = 0.01; // initial value of Calcium_cyt_Jerp_fi
        Calcium_cyt_Jout_Vi = 7.64E-13; // initial value of Calcium_cyt_Jout_Vi
        Calcium_cyt_Jout_fi = 0.01; // initial value of Calcium_cyt_Jout_fi
        Calcium_cyt_sequestration_ksg = 1.0E-4; // initial value of Calcium_cyt_sequestration_ksg
        Calcium_er_Jerp_Ver = 2.8E-13; // initial value of Calcium_er_Jerp_Ver
        Calcium_er_Jerp_Vi = 7.64E-13; // initial value of Calcium_er_Jerp_Vi
        Calcium_er_Jerp_fer = 0.03; // initial value of Calcium_er_Jerp_fer
        Calcium_er_Jout_Ver = 2.8E-13; // initial value of Calcium_er_Jout_Ver
        Calcium_er_Jout_fer = 0.03; // initial value of Calcium_er_Jout_fer
        Cm = 6158.0; // initial value of Cm
        IP3_degradation_kdip = 4.0E-5; // initial value of IP3_degradation_kdip
        IP3_synthesis_Kipca = 0.4; // initial value of IP3_synthesis_Kipca
        IP3_synthesis_kip = 3.0E-4; // initial value of IP3_synthesis_kip
        K_cyt = 132400.0; // initial value of K_cyt
        K_ec = 8000.0; // initial value of K_ec
        Kcah = 9.5; // initial value of Kcah
        Kcap = 0.1; // initial value of Kcap
        Kcar = 200.0; // initial value of Kcar
        Kcarp = 0.5; // initial value of Kcarp
        Kdd = 17.0; // initial value of Kdd
        Kip3 = 3.2; // initial value of Kip3
        Kkca = 0.1; // initial value of Kkca
        Knaca = 0.75; // initial value of Knaca
        Krca = 0.077; // initial value of Krca
        Ksup = 150400.0; // initial value of Ksup
        Ktd = 26.0; // initial value of Ktd
        Ktt = 1.0; // initial value of Ktt
        Na_Icran_F = 9.6485E16; // initial value of Na_Icran_F
        Na_Icran_Vi = 7.64E-13; // initial value of Na_Icran_Vi
        Na_Ina_F = 9.6485E16; // initial value of Na_Ina_F
        Na_Ina_Vi = 7.64E-13; // initial value of Na_Ina_Vi
        Na_Inaca_F = 9.6485E16; // initial value of Na_Inaca_F
        Na_Inaca_Vi = 7.64E-13; // initial value of Na_Inaca_Vi
        Na_Inak_F = 9.6485E16; // initial value of Na_Inak_F
        Na_Inak_Vi = 7.64E-13; // initial value of Na_Inak_Vi
        Na_ec = 140000.0; // initial value of Na_ec
        P = 4950.0; // initial value of P
        Pcaer = 0.105; // initial value of Pcaer
        Pip3 = 1.2E-15; // initial value of Pip3
        Pleak = 1.0E-16; // initial value of Pleak
        Pmcap = 2000.0; // initial value of Pmcap
        Pnak = 600.0; // initial value of Pnak
        Sn = 7.0; // initial value of Sn
        TV = 26.73; // initial value of TV
        Vcah = -19.0; // initial value of Vcah
        Vn = -14.0; // initial value of Vn
        Vtau = -75.0; // initial value of Vtau
        a = 65.0; // initial value of a
        b = 20.0; // initial value of b
        b5_ast = 0.03; // initial value of b5_ast
        b_1 = 100.0; // initial value of b_1
        b_2 = 1.0E-4; // initial value of b_2
        b_3 = 1.72E-17; // initial value of b_3
        b_4 = 2.0E-4; // initial value of b_4
        b_6 = 6.0E-7; // initial value of b_6
        c = 20.0; // initial value of c
        f5_ast = 0.0020; // initial value of f5_ast
        f_1 = 2.5E-10; // initial value of f_1
        f_2 = 10.0; // initial value of f_2
        f_3 = 0.172; // initial value of f_3
        f_4 = 1.5E-8; // initial value of f_4
        f_6 = 11.5; // initial value of f_6
        gmKDr = 3000.0; // initial value of gmKDr
        gm_CRAN = 0.7; // initial value of gm_CRAN
        gmkatp = 24000.0; // initial value of gmkatp
        gmkca = 130.0; // initial value of gmkca
        gmna = 1200.0; // initial value of gmna
        gmvca = 770.0; // initial value of gmvca
        gnaca = 271.0; // initial value of gnaca
    }

    /*
     * Model variables initial values
     */
    protected double _compartment_ADP_cyt_;
    protected double rate_ATP_Ca_dependent_consumption;
    protected double rate_ATP_Capump;
    protected double rate_ATP_Jerp;
    protected double rate_ATP_NaKATPase;
    protected double rate_ATP_consumption;
    protected double rate_ATP_production;
    protected double rate_Calcium_cyt_Icapump;
    protected double rate_Calcium_cyt_Inaca;
    protected double rate_Calcium_cyt_Ivca;
    protected double rate_Calcium_cyt_Jerp;
    protected double rate_Calcium_cyt_Jout;
    protected double rate_Calcium_cyt_sequestration;
    protected double rate_Calcium_er_Jerp;
    protected double rate_Calcium_er_Jout;
    protected double rate_IP3_degradation;
    protected double rate_IP3_synthesis;
    protected double rate_Na_Icran;
    protected double rate_Na_Ina;
    protected double rate_Na_Inaca;
    protected double rate_Na_Inak;
    protected double compartment;
    protected double ATP_Ca_dependent_consumption_katpca;
    protected double ATP_Capump_F;
    protected double ATP_Capump_Vi;
    protected double ATP_NaKATPase_F;
    protected double ATP_NaKATPase_Vi;
    protected double ATP_consumption_katp;
    protected double ATP_production_kadp;
    protected double B2;
    protected double B3;
    protected double B4;
    protected double B6;
    protected double Ca_ec;
    protected double Calcium_cyt_Icapump_F;
    protected double Calcium_cyt_Icapump_Vi;
    protected double Calcium_cyt_Icapump_fi;
    protected double Calcium_cyt_Inaca_F;
    protected double Calcium_cyt_Inaca_Vi;
    protected double Calcium_cyt_Inaca_fi;
    protected double Calcium_cyt_Ivca_F;
    protected double Calcium_cyt_Ivca_Vi;
    protected double Calcium_cyt_Ivca_fi;
    protected double Calcium_cyt_Jerp_fi;
    protected double Calcium_cyt_Jout_Vi;
    protected double Calcium_cyt_Jout_fi;
    protected double Calcium_cyt_sequestration_ksg;
    protected double Calcium_er_Jerp_Ver;
    protected double Calcium_er_Jerp_Vi;
    protected double Calcium_er_Jerp_fer;
    protected double Calcium_er_Jout_Ver;
    protected double Calcium_er_Jout_fer;
    protected double Cm;
    protected double D;
    protected double F1;
    protected double F4;
    protected double F5;
    protected double IP3_degradation_kdip;
    protected double IP3_synthesis_Kipca;
    protected double IP3_synthesis_kip;
    protected double I_CRAN;
    protected double I_CaPump;
    protected double I_KATP;
    protected double I_KCa;
    protected double I_KDr;
    protected double I_Na;
    protected double I_NaCa;
    protected double I_NaK;
    protected double I_Vca;
    protected double Jerp;
    protected double Jout;
    protected double K_cyt;
    protected double K_ec;
    protected double Kcah;
    protected double Kcap;
    protected double Kcar;
    protected double Kcarp;
    protected double Kdd;
    protected double Kip3;
    protected double Kkca;
    protected double Knaca;
    protected double Krca;
    protected double Ksup;
    protected double Ktd;
    protected double Ktt;
    protected double Na_Icran_F;
    protected double Na_Icran_Vi;
    protected double Na_Ina_F;
    protected double Na_Ina_Vi;
    protected double Na_Inaca_F;
    protected double Na_Inaca_Vi;
    protected double Na_Inak_F;
    protected double Na_Inak_Vi;
    protected double Na_ec;
    protected double O_infinity;
    protected double Okatp;
    protected double P;
    protected double Pcaer;
    protected double Pip3;
    protected double Pleak;
    protected double Pmcap;
    protected double Pnak;
    protected double Sn;
    protected double TV;
    protected double V_CRAN;
    protected double Vca;
    protected double Vcah;
    protected double Vk;
    protected double Vn;
    protected double Vna;
    protected double Vnaca;
    protected double Vtau;
    protected double a;
    protected double b;
    protected double b5_ast;
    protected double b_1;
    protected double b_2;
    protected double b_3;
    protected double b_4;
    protected double b_5;
    protected double b_6;
    protected double c;
    protected double f5_ast;
    protected double f_1;
    protected double f_2;
    protected double f_3;
    protected double f_4;
    protected double f_5;
    protected double f_6;
    protected double f_CRAN;
    protected double f_Ca;
    protected double gmKDr;
    protected double gm_CRAN;
    protected double gmkatp;
    protected double gmkca;
    protected double gmna;
    protected double gmvca;
    protected double gnaca;
    protected double n_infinity;
    protected double p_CRAN;
    protected double pna;
    protected double pvca;
    protected double tau_n;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[42];
        y[0] = _compartment_ADP_cyt_;
        y[1] = x[0];
        y[2] = x[1];
        y[3] = x[2];
        y[4] = x[3];
        y[5] = x[4];
        y[6] = B2;
        y[7] = B3;
        y[8] = B4;
        y[9] = B6;
        y[10] = D;
        y[11] = F1;
        y[12] = F4;
        y[13] = F5;
        y[14] = I_CRAN;
        y[15] = I_CaPump;
        y[16] = I_KATP;
        y[17] = I_KCa;
        y[18] = I_KDr;
        y[19] = I_Na;
        y[20] = I_NaCa;
        y[21] = I_NaK;
        y[22] = I_Vca;
        y[23] = Jerp;
        y[24] = Jout;
        y[25] = O_infinity;
        y[26] = Okatp;
        y[27] = x[5];
        y[28] = Vca;
        y[29] = Vk;
        y[30] = Vna;
        y[31] = Vnaca;
        y[32] = b_5;
        y[33] = f_5;
        y[34] = f_CRAN;
        y[35] = f_Ca;
        y[36] = x[6];
        y[37] = n_infinity;
        y[38] = p_CRAN;
        y[39] = pna;
        y[40] = pvca;
        y[41] = tau_n;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[7];
        this.time = 0.0;
        x[0] = 932.1; // - $"compartment.ATP_cyt"
        x[1] = 0.085; // - $"compartment.Ca_cyt"
        x[2] = 22.8; // - $"compartment.Ca_er"
        x[3] = 0.33; // - $"compartment.IP3_cyt"
        x[4] = 9858.0; // - $"compartment.Na_cyt"
        x[5] = -60.9; // - V
        x[6] = 0.00123; // - n

        __internalVarInitFunc_0(time, x);
        __internalRateVarInitFunc_0(time, x);

        return x;
    }

/*
 * code for algebraic rules calculations
 */

/*
 * end of code for algebraic rules calculations
 */

    protected void calculateRates(double time, double[] x)
    {

        __internalVarInitFunc_0(time, x);
        __internalRateVarInitFunc_0(time, x);

    }

        /*
         * calculate dy/dt for 'BIOMD0000000059.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_ATP_Ca_dependent_consumption-rate_ATP_Capump-rate_ATP_Jerp-rate_ATP_NaKATPase-rate_ATP_consumption+rate_ATP_production;
        result[1] = -rate_Calcium_cyt_Icapump+rate_Calcium_cyt_Inaca+rate_Calcium_cyt_Ivca-rate_Calcium_cyt_Jerp+rate_Calcium_cyt_Jout-rate_Calcium_cyt_sequestration;
        result[2] = +rate_Calcium_er_Jerp-rate_Calcium_er_Jout;
        result[3] = -rate_IP3_degradation+rate_IP3_synthesis;
        result[4] = +rate_Na_Icran+rate_Na_Ina-rate_Na_Inaca-rate_Na_Inak;
        result[5] = +(0 - (I_Vca + I_CaPump + I_NaCa + I_CRAN + I_Na + I_NaK + I_KDr + I_KCa + I_KATP))/Cm;
        result[6] = +(n_infinity - x[6])/tau_n;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[7];
        __internalDyDt_0(time, x, result);
        return result;
    }
    public double[] dy_dt(double time, double[] x)
    {
        this.time = time;
        calculateRates( time,x );

        return calculateResult( time,x );
    }

} // class ...