/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000145.xml diagram  at 2008.03.20 15:09:26
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000145 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        Cytosol_DG = x[3];
        Rip3 = Math.pow(x[3], 3)/(Math.pow(Ks, 3) + Math.pow(x[3], 3));
        Rcyt1 = x[1]/(Kc1 + x[1]);
        Rcyt2 = x[1]/(Kc2 + x[1]);
        Rer = Math.pow(x[4], w)/(Math.pow(Ker, w) + Math.pow(x[4], w));
        _Cytosol_PLC_ = Cplc_total - x[0];
        Raplc = x[0]/(Kp + x[0]);
        Rpkc = Cytosol_DG/(Kd + Cytosol_DG)*x[1]/(Kr + x[1]);
        Rgalpha_gtp = Math.pow(x[2], n)/(Math.pow(Kg, n) + Math.pow(x[2], n));
        Rdg = Math.pow(Cytosol_DG, m)/(Math.pow(Kd, m) + Math.pow(Cytosol_DG, m));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_R1 = Cytosol*k0;
        rate_R10 = Cytosol*k10*Rcyt2;
        rate_R11 = Cytosol*k11;
        rate_R2 = Cytosol*k1*x[2];
        rate_R3 = Cytosol*k2*Raplc*x[2];
        rate_R4 = Cytosol*k3*Rpkc*x[2];
        rate_R5 = Cytosol*k4*Rgalpha_gtp*Rdg*_Cytosol_PLC_;
        rate_R6 = Cytosol*k5*x[0];
        rate_R7 = Cytosol*k6*x[0];
        rate_R8 = Cytosol*k7*x[3];
        rate_R9 = ER*(k8*Rip3*Rer - k9*Rcyt1);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _Cytosol_PLC_ = 1.0; // initial value of $"Cytosol.PLC"
        Cytosol = 1.0; // initial value of $Cytosol
        Cytosol_DG = 1.0; // initial value of $Cytosol.DG
        ER = 1.0; // initial value of $ER
        Cplc_total = 10.0; // initial value of Cplc_total
        Kc1 = 1000.0; // initial value of Kc1
        Kc2 = 2000.0; // initial value of Kc2
        Kd = 10.0; // initial value of Kd
        Ker = 75.0; // initial value of Ker
        Kg = 25.0; // initial value of Kg
        Kp = 4.0; // initial value of Kp
        Kr = 200.0; // initial value of Kr
        Ks = 25.0; // initial value of Ks
        k0 = 0.1; // initial value of k0
        k1 = 3.4; // initial value of k1
        k10 = 3000.0; // initial value of k10
        k11 = 260.0; // initial value of k11
        k2 = 4.0; // initial value of k2
        k3 = 4.5; // initial value of k3
        k4 = 1.2; // initial value of k4
        k5 = 0.12; // initial value of k5
        k6 = 14.0; // initial value of k6
        k7 = 2.0; // initial value of k7
        k8 = 10500.0; // initial value of k8
        k9 = 600.0; // initial value of k9
        m = 2.0; // initial value of m
        n = 4.0; // initial value of n
        w = 3.0; // initial value of w
    }

    /*
     * Model variables initial values
     */
    protected double _Cytosol_PLC_;
    protected double rate_R1;
    protected double rate_R10;
    protected double rate_R11;
    protected double rate_R2;
    protected double rate_R3;
    protected double rate_R4;
    protected double rate_R5;
    protected double rate_R6;
    protected double rate_R7;
    protected double rate_R8;
    protected double rate_R9;
    protected double Cytosol;
    protected double Cytosol_DG;
    protected double ER;
    protected double Cplc_total;
    protected double Kc1;
    protected double Kc2;
    protected double Kd;
    protected double Ker;
    protected double Kg;
    protected double Kp;
    protected double Kr;
    protected double Ks;
    protected double Raplc;
    protected double Rcyt1;
    protected double Rcyt2;
    protected double Rdg;
    protected double Rer;
    protected double Rgalpha_gtp;
    protected double Rip3;
    protected double Rpkc;
    protected double k0;
    protected double k1;
    protected double k10;
    protected double k11;
    protected double k2;
    protected double k3;
    protected double k4;
    protected double k5;
    protected double k6;
    protected double k7;
    protected double k8;
    protected double k9;
    protected double m;
    protected double n;
    protected double w;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[15];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = _Cytosol_PLC_;
        y[5] = x[4];
        y[6] = Cytosol_DG;
        y[7] = Raplc;
        y[8] = Rcyt1;
        y[9] = Rcyt2;
        y[10] = Rdg;
        y[11] = Rer;
        y[12] = Rgalpha_gtp;
        y[13] = Rip3;
        y[14] = Rpkc;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[5];
        this.time = 0.0;
        x[0] = 9.0; // - $"Cytosol.APLC"
        x[1] = 200.0; // - $"Cytosol.Ca_Cyt"
        x[2] = 1.0; // - $"Cytosol.Galpha_GTP"
        x[3] = 1.0; // - $"Cytosol.IP3"
        x[4] = 1000.0; // - $"ER.Ca_ER"

        __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 'BIOMD0000000145.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_R5-rate_R6;
        result[1] = -rate_R10*0.05+rate_R11*0.05+rate_R9*0.01;
        result[2] = +rate_R1+rate_R2-rate_R3-rate_R4;
        result[3] = +rate_R7-rate_R8;
        result[4] = -rate_R9*0.0010;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[5];
        __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 ...