/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000149.xml diagram  at 2008.03.20 15:09:38
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000149 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        X13X14 = x[17] + x[18];
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_R1 = cytoplasm*k1*x[19]*W;
        rate_R10 = cytoplasm*k10*x[27];
        rate_R11 = cytoplasm*k11*x[0];
        rate_R12 = cytoplasm*V12;
        rate_R13 = _cytoplasm_nucleus_*k13*x[16];
        rate_R14 = _cytoplasm_nucleus_*(k14 + k21*(x[16] + x[18]));
        rate_R15 = cytoplasm*k15*x[1];
        rate_R16 = _cytoplasm_nucleus_*(k_plus16*x[16]*x[17] - k_minus16*x[18]);
        rate_R17 = cytoplasm*(k_plus17*x[25]*x[16] - k_minus17*x[2]);
        rate_R18 = cytoplasm*(Vmax1*x[3]*W/(Km1 + x[3]))*(Ki/(Ki + x[10]));
        rate_R19 = cytoplasm*(Vmax2*x[4]/(Km2 + x[4]));
        rate_R2 = cytoplasm*k2*x[20];
        rate_R20 = cytoplasm*(kcat1*x[4]*x[5]/(Km3 + x[5]));
        rate_R21 = cytoplasm*Vmax3*x[6]/(Km4 + x[6]);
        rate_R22 = cytoplasm*kcat2*x[6]*x[7]/(Km5 + x[7]);
        rate_R23 = cytoplasm*Vmax4*x[8]/(Km6 + x[8]);
        rate_R24 = cytoplasm*kcat3*x[8]*x[9]/(Km7 + x[9]);
        rate_R25 = cytoplasm*Vmax5*x[10]/(Km8 + x[10]);
        rate_R26 = cytoplasm*kcat4*x[10]*x[11]/(Km9 + x[11]);
        rate_R27 = cytoplasm*(k18*x[5]*x[12] - k19*x[11]);
        rate_R28 = cytoplasm*Vmax6*x[13]/(Km10 + x[13]);
        rate_R29 = cytoplasm*(kcat5*Math.pow(x[18], n1)/(Math.pow(Km11, n1) + Math.pow(x[18], n1)));
        rate_R3 = cytoplasm*k3*x[20]*x[22];
        rate_R30 = cytoplasm*k20*x[14];
        rate_R31 = cytoplasm*(kcat6*x[14]*x[5]/(Km12 + x[5]));
        rate_R32 = cytoplasm*kcat7*x[10]*x[23]/(Km13 + x[23]);
        rate_R33 = cytoplasm*Vmax7*x[15]/(Km14 + x[15]);
        rate_R4 = cytoplasm*k4*x[22];
        rate_R5 = cytoplasm*k5*x[21];
        rate_R6 = cytoplasm*(k_plus6*x[23]*x[24] - k_minus6*x[22]);
        rate_R7 = cytoplasm*(k_plus7*x[25]*x[1] - k_minus7*x[24]);
        rate_R8 = cytoplasm*(k_plus8*x[21]*x[16] - k_minus8*x[26]);
        rate_R9 = cytoplasm*k9*x[26];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _cytoplasm_nucleus_ = 1.0; // initial value of $"cytoplasm.nucleus"
        cytoplasm = 1.0; // initial value of $cytoplasm
        Ki = 9.0; // initial value of Ki
        Km1 = 10.0; // initial value of Km1
        Km10 = 12.0; // initial value of Km10
        Km11 = 15.0; // initial value of Km11
        Km12 = 15.0; // initial value of Km12
        Km13 = 15.0; // initial value of Km13
        Km14 = 15.0; // initial value of Km14
        Km2 = 8.0; // initial value of Km2
        Km3 = 15.0; // initial value of Km3
        Km4 = 15.0; // initial value of Km4
        Km5 = 15.0; // initial value of Km5
        Km6 = 15.0; // initial value of Km6
        Km7 = 15.0; // initial value of Km7
        Km8 = 15.0; // initial value of Km8
        Km9 = 9.0; // initial value of Km9
        V12 = 0.423; // initial value of V12
        Vmax1 = 150.0; // initial value of Vmax1
        Vmax2 = 15.0; // initial value of Vmax2
        Vmax3 = 45.0; // initial value of Vmax3
        Vmax4 = 45.0; // initial value of Vmax4
        Vmax5 = 45.0; // initial value of Vmax5
        Vmax6 = 45.0; // initial value of Vmax6
        Vmax7 = 45.0; // initial value of Vmax7
        k1 = 0.182; // initial value of k1
        k10 = 206.0; // initial value of k10
        k11 = 0.417; // initial value of k11
        k13 = 2.57E-4; // initial value of k13
        k14 = 8.22E-5; // initial value of k14
        k15 = 0.167; // initial value of k15
        k18 = 0.15; // initial value of k18
        k19 = 39.0; // initial value of k19
        k2 = 0.0182; // initial value of k2
        k20 = 0.015; // initial value of k20
        k21 = 1.0E-6; // initial value of k21
        k3 = 0.05; // initial value of k3
        k4 = 0.267; // initial value of k4
        k5 = 0.133; // initial value of k5
        k9 = 206.0; // initial value of k9
        k_minus16 = 30.0; // initial value of k_minus16
        k_minus17 = 1200.0; // initial value of k_minus17
        k_minus6 = 0.909; // initial value of k_minus6
        k_minus7 = 50.0; // initial value of k_minus7
        k_minus8 = 120.0; // initial value of k_minus8
        k_plus16 = 1.0; // initial value of k_plus16
        k_plus17 = 1.0; // initial value of k_plus17
        k_plus6 = 0.0909; // initial value of k_plus6
        k_plus7 = 1.0; // initial value of k_plus7
        k_plus8 = 1.0; // initial value of k_plus8
        kcat1 = 1.5; // initial value of kcat1
        kcat2 = 1.5; // initial value of kcat2
        kcat3 = 1.5; // initial value of kcat3
        kcat4 = 1.5; // initial value of kcat4
        kcat5 = 0.6; // initial value of kcat5
        kcat6 = 1.5; // initial value of kcat6
        kcat7 = 1.5; // initial value of kcat7
        n1 = 2.0; // initial value of n1
    }

    /*
     * Model variables initial values
     */
    protected double _cytoplasm_nucleus_;
    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_R21;
    protected double rate_R22;
    protected double rate_R23;
    protected double rate_R24;
    protected double rate_R25;
    protected double rate_R26;
    protected double rate_R27;
    protected double rate_R28;
    protected double rate_R29;
    protected double rate_R3;
    protected double rate_R30;
    protected double rate_R31;
    protected double rate_R32;
    protected double rate_R33;
    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 cytoplasm;
    protected double Ki;
    protected double Km1;
    protected double Km10;
    protected double Km11;
    protected double Km12;
    protected double Km13;
    protected double Km14;
    protected double Km2;
    protected double Km3;
    protected double Km4;
    protected double Km5;
    protected double Km6;
    protected double Km7;
    protected double Km8;
    protected double Km9;
    protected double V12;
    protected double Vmax1;
    protected double Vmax2;
    protected double Vmax3;
    protected double Vmax4;
    protected double Vmax5;
    protected double Vmax6;
    protected double Vmax7;
    protected double W;
    protected double X13X14;
    protected double k1;
    protected double k10;
    protected double k11;
    protected double k13;
    protected double k14;
    protected double k15;
    protected double k18;
    protected double k19;
    protected double k2;
    protected double k20;
    protected double k21;
    protected double k3;
    protected double k4;
    protected double k5;
    protected double k9;
    protected double k_minus16;
    protected double k_minus17;
    protected double k_minus6;
    protected double k_minus7;
    protected double k_minus8;
    protected double k_plus16;
    protected double k_plus17;
    protected double k_plus6;
    protected double k_plus7;
    protected double k_plus8;
    protected double kcat1;
    protected double kcat2;
    protected double kcat3;
    protected double kcat4;
    protected double kcat5;
    protected double kcat6;
    protected double kcat7;
    protected double n1;
    protected double time;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[30];
        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] = x[16];
        y[17] = x[17];
        y[18] = x[18];
        y[19] = x[19];
        y[20] = x[20];
        y[21] = x[21];
        y[22] = x[22];
        y[23] = x[23];
        y[24] = x[24];
        y[25] = x[25];
        y[26] = x[26];
        y[27] = x[27];
        y[28] = W;
        y[29] = X13X14;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[28];
        this.time = 0.0;
        x[0] = 0.9881; // - $"cytoplasm.X10"
        x[1] = 8.0E-4; // - $"cytoplasm.X12"
        x[2] = 3.4392; // - $"cytoplasm.X15"
        x[3] = 200.0; // - $"cytoplasm.X16"
        x[4] = 0.0; // - $"cytoplasm.X17"
        x[5] = 112.5585; // - $"cytoplasm.X18"
        x[6] = 6.486; // - $"cytoplasm.X19"
        x[7] = 296.1137; // - $"cytoplasm.X20"
        x[8] = 3.8863; // - $"cytoplasm.X21"
        x[9] = 297.8897; // - $"cytoplasm.X22"
        x[10] = 2.1103; // - $"cytoplasm.X23"
        x[11] = 180.9595; // - $"cytoplasm.X24"
        x[12] = 418.1788; // - $"cytoplasm.X25"
        x[13] = 0.8619; // - $"cytoplasm.X26"
        x[14] = 10.263; // - $"cytoplasm.X27"
        x[15] = 0.85544; // - $"cytoplasm.X28"
        x[16] = 42.7224; // - $"cytoplasm.nucleus.X11"
        x[17] = 6.1879; // - $"cytoplasm.nucleus.X13"
        x[18] = 8.8121; // - $"cytoplasm.nucleus.X14"
        x[19] = 100.0; // - $cytoplasm.X1
        x[20] = 0.0; // - $cytoplasm.X2
        x[21] = 0.0153; // - $cytoplasm.X3
        x[22] = 0.0076; // - $cytoplasm.X4
        x[23] = 49.1372; // - $cytoplasm.X5
        x[24] = 0.0015; // - $cytoplasm.X6
        x[25] = 96.6019; // - $cytoplasm.X7
        x[26] = 0.0020; // - $cytoplasm.X8
        x[27] = 0.0020; // - $cytoplasm.X9

        __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 'BIOMD0000000149.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_R10-rate_R11;
        result[1] = +rate_R14-rate_R15-rate_R7;
        result[2] = +rate_R17;
        result[3] = -rate_R18+rate_R19;
        result[4] = +rate_R18-rate_R19;
        result[5] = -rate_R20+rate_R21+rate_R26-rate_R27-rate_R31;
        result[6] = +rate_R20-rate_R21+rate_R31;
        result[7] = -rate_R22+rate_R23;
        result[8] = +rate_R22-rate_R23;
        result[9] = -rate_R24+rate_R25;
        result[10] = +rate_R24-rate_R25;
        result[11] = -rate_R26+rate_R27;
        result[12] = -rate_R27+rate_R28;
        result[13] = +rate_R26-rate_R28;
        result[14] = +rate_R29-rate_R30;
        result[15] = +rate_R32-rate_R33;
        result[16] = +rate_R12-rate_R13-rate_R16-rate_R17-rate_R8;
        result[17] = -rate_R16;
        result[18] = +rate_R16;
        result[19] = -rate_R1+rate_R2;
        result[20] = +rate_R1-rate_R2;
        result[21] = +rate_R10+rate_R4-rate_R5-rate_R8;
        result[22] = -rate_R3-rate_R4+rate_R5+rate_R6;
        result[23] = -rate_R32+rate_R33+rate_R3-rate_R6;
        result[24] = +rate_R3-rate_R6+rate_R7;
        result[25] = -rate_R17-rate_R7;
        result[26] = +rate_R8-rate_R9;
        result[27] = -rate_R10+rate_R9;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[28];
        __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[2];
        flags[0] = (time >= 500 && time <= 1000) ? +1 : -1; //event_0000001
        flags[1] = (time > 1000) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041786250, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041786250 == 0) //event_0000001
        {
            W = 1;
        } else if ( __internalVar12060041786250 == 1) //event_0000002
        {
            W = 0;
        }
    }

} // class ...