/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000137.xml diagram  at 2008.03.20 15:09:14
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000137 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        k9 = (k9_stimulated + k9_basal)*(x[2]/PI3K) + k9_basal;
        x4x5 = x[18] + x[19];
        k11 = 0.1*kminus11*(x[3] - 0.31)/2.79;
        k12 = 0.1*kminus12*(x[3] - 0.31)/2.79;
        Effect = (0.2*x[7] + 0.8*x[9])/APequil;
        k13prime = 0.625*kminus13*Effect;
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_R1 = CellSurface*(k1*CellSurface_x1*x[16] - kminus1*x[17]);
        rate_R10 = _CellSurface_Intracellular_*k6*PTP*x[12];
        rate_R11 = _CellSurface_Intracellular_*k6*PTP*x[13];
        rate_R12 = _CellSurface_Intracellular_*(k7*x[14]*(x[18] + x[19])/IRp - kminus7*PTP*x[0]);
        rate_R13 = _CellSurface_Intracellular_*(k8*x[0]*x[1] - kminus8*x[2]);
        rate_R14 = _CellSurface_Intracellular_*(k9*x[4] - kminus9*PTEN*x[3]);
        rate_R15 = _CellSurface_Intracellular_*(k10*x[5] - kminus10*SHIP*x[3]);
        rate_R16 = _CellSurface_Intracellular_*(k11*x[6] - kminus11*x[7]);
        rate_R17 = _CellSurface_Intracellular_*(k12*x[8] - kminus12*x[9]);
        rate_R18 = _CellSurface_Intracellular_*k14;
        rate_R19 = _CellSurface_Intracellular_*kminus14*x[10];
        rate_R2 = CellSurface*k3*x[17];
        rate_R20 = _CellSurface_Intracellular_*((k13 + k13prime)*x[10] - kminus13*x[15]);
        rate_R3 = CellSurface*k2*CellSurface_x1*x[19] - kminus2*x[18];
        rate_R4 = CellSurface*kminus3*PTP*x[19];
        rate_R5 = CellSurface*(k4*x[16] - kminus4*x[11]);
        rate_R6 = CellSurface*(k4prime*x[18] - kminus4prime*x[12]);
        rate_R7 = CellSurface*(k4prime*x[19] - kminus4prime*x[13]);
        rate_R8 = _CellSurface_Intracellular_*k5;
        rate_R9 = _CellSurface_Intracellular_*kminus5*x[11];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _CellSurface_Intracellular_ = 1.0; // initial value of $"CellSurface.Intracellular"
        CellSurface = 1.0; // initial value of $CellSurface
        CellSurface_x1 = 1.0E8; // initial value of $CellSurface.x1
        APequil = 9.09091; // initial value of APequil
        IRp = 897.0; // initial value of IRp
        PI3K = 5.0; // initial value of PI3K
        PTEN = 1.0; // initial value of PTEN
        PTP = 1.0; // initial value of PTP
        SHIP = 1.0; // initial value of SHIP
        k1 = 6.0E-8; // initial value of k1
        k10 = 2.961; // initial value of k10
        k13 = 0.00696; // initial value of k13
        k14 = 0.11088; // initial value of k14
        k2 = 6.0E-8; // initial value of k2
        k3 = 2500.0; // initial value of k3
        k4 = 3.3333334E-4; // initial value of k4
        k4prime = 0.0021; // initial value of k4prime
        k6 = 0.461; // initial value of k6
        k7 = 4.16; // initial value of k7
        k8 = 7.06E-4; // initial value of k8
        k9_basal = 0.13145; // initial value of k9_basal
        k9_stimulated = 1.39; // initial value of k9_stimulated
        kminus1 = 0.2; // initial value of kminus1
        kminus10 = 2.77; // initial value of kminus10
        kminus11 = 6.9315; // initial value of kminus11
        kminus12 = 6.9315; // initial value of kminus12
        kminus13 = 0.167; // initial value of kminus13
        kminus14 = 0.001155; // initial value of kminus14
        kminus2 = 20.0; // initial value of kminus2
        kminus3 = 0.2; // initial value of kminus3
        kminus4 = 0.0030; // initial value of kminus4
        kminus4prime = 2.1E-4; // initial value of kminus4prime
        kminus5 = 1.67E-18; // initial value of kminus5
        kminus7 = 1.396; // initial value of kminus7
        kminus8 = 10.0; // initial value of kminus8
        kminus9 = 42.15; // initial value of kminus9
    }

    /*
     * Model variables initial values
     */
    protected double _CellSurface_Intracellular_;
    protected double rate_R1;
    protected double rate_R10;
    protected double rate_R11;
    protected double rate_R12;
    protected double rate_R13;
    protected double rate_R14;
    protected double rate_R15;
    protected double rate_R16;
    protected double rate_R17;
    protected double rate_R18;
    protected double rate_R19;
    protected double rate_R2;
    protected double rate_R20;
    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 CellSurface;
    protected double CellSurface_x1;
    protected double APequil;
    protected double Effect;
    protected double IRp;
    protected double PI3K;
    protected double PTEN;
    protected double PTP;
    protected double SHIP;
    protected double k1;
    protected double k10;
    protected double k11;
    protected double k12;
    protected double k13;
    protected double k13prime;
    protected double k14;
    protected double k2;
    protected double k3;
    protected double k4;
    protected double k4prime;
    protected double k5;
    protected double k6;
    protected double k7;
    protected double k8;
    protected double k9;
    protected double k9_basal;
    protected double k9_stimulated;
    protected double kminus1;
    protected double kminus10;
    protected double kminus11;
    protected double kminus12;
    protected double kminus13;
    protected double kminus14;
    protected double kminus2;
    protected double kminus3;
    protected double kminus4;
    protected double kminus4prime;
    protected double kminus5;
    protected double kminus7;
    protected double kminus8;
    protected double kminus9;
    protected double time;
    protected double x4x5;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[28];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = x[4];
        y[5] = x[5];
        y[6] = x[6];
        y[7] = x[7];
        y[8] = x[8];
        y[9] = x[9];
        y[10] = x[10];
        y[11] = x[11];
        y[12] = x[12];
        y[13] = x[13];
        y[14] = x[14];
        y[15] = x[15];
        y[16] = CellSurface_x1;
        y[17] = x[16];
        y[18] = x[17];
        y[19] = x[18];
        y[20] = x[19];
        y[21] = Effect;
        y[22] = k11;
        y[23] = k12;
        y[24] = k13prime;
        y[25] = k5;
        y[26] = k9;
        y[27] = x4x5;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[20];
        this.time = 0.0;
        x[0] = 0.0; // - $"CellSurface.Intracellular.x10"
        x[1] = 100.0; // - $"CellSurface.Intracellular.x11"
        x[2] = 0.0; // - $"CellSurface.Intracellular.x12"
        x[3] = 0.31; // - $"CellSurface.Intracellular.x13"
        x[4] = 99.4; // - $"CellSurface.Intracellular.x14"
        x[5] = 0.29; // - $"CellSurface.Intracellular.x15"
        x[6] = 100.0; // - $"CellSurface.Intracellular.x16"
        x[7] = 0.0; // - $"CellSurface.Intracellular.x17"
        x[8] = 100.0; // - $"CellSurface.Intracellular.x18"
        x[9] = 0.0; // - $"CellSurface.Intracellular.x19"
        x[10] = 96.0; // - $"CellSurface.Intracellular.x20"
        x[11] = 100.0; // - $"CellSurface.Intracellular.x6"
        x[12] = 0.0; // - $"CellSurface.Intracellular.x7"
        x[13] = 0.0; // - $"CellSurface.Intracellular.x8"
        x[14] = 1000.0; // - $"CellSurface.Intracellular.x9"
        x[15] = 4.0; // - $"CellSurface.x21"
        x[16] = 900.0; // - $CellSurface.x2
        x[17] = 0.0; // - $CellSurface.x3
        x[18] = 0.0; // - $CellSurface.x4
        x[19] = 0.0; // - $CellSurface.x5

        __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 'BIOMD0000000137.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_R12-rate_R13;
        result[1] = -rate_R13;
        result[2] = +rate_R13;
        result[3] = +rate_R14+rate_R15;
        result[4] = -rate_R14;
        result[5] = -rate_R15;
        result[6] = -rate_R16;
        result[7] = +rate_R16;
        result[8] = -rate_R17;
        result[9] = +rate_R17;
        result[10] = +rate_R18-rate_R19-rate_R20;
        result[11] = +rate_R10+rate_R11+rate_R5+rate_R8-rate_R9;
        result[12] = -rate_R10+rate_R6;
        result[13] = -rate_R11+rate_R7;
        result[14] = -rate_R12;
        result[15] = +rate_R20;
        result[16] = -rate_R1+rate_R4-rate_R5;
        result[17] = +rate_R1-rate_R2;
        result[18] = +rate_R3-rate_R6;
        result[19] = +rate_R2-rate_R3-rate_R4-rate_R7;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[20];
        __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 );
    }

    public double[] checkEvent(double time, double[] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);
        __internalRateVarInitFunc_0(time, x);

        double [] flags = new double[3];
        flags[0] = (time >= 15) ? +1 : -1; //event_0000001
        flags[1] = (x[11] + x[12] + x[13] > 100) ? +1 : -1; //event_0000002
        flags[2] = (x[11] + x[12] + x[13] <= 100) ? +1 : -1; //event_0000003
        return flags;
    }

    public void processEvent(int __internalVar12060041545150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041545150 == 0) //event_0000001
        {
            CellSurface_x1 = 0;
        } else if ( __internalVar12060041545150 == 1) //event_0000002
        {
            k5 = 0.0167;
        } else if ( __internalVar12060041545150 == 2) //event_0000003
        {
            k5 = 0.1002;
        }
    }

} // class ...