/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000097.xml diagram  at 2008.03.20 15:08:33
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000097 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_R1 = _cytoplasm_nucleus_*ld*q1*x[12];
        rate_R10 = _cytoplasm_nucleus_*m4*x[13]/(k4 + x[13]);
        rate_R11 = cytoplasm*p2*x[13];
        rate_R12 = cytoplasm*r3*x[3];
        rate_R13 = _cytoplasm_nucleus_*r4*x[14];
        rate_R14 = cytoplasm*((lmax - ld)*m5 + m6)*(x[3]/(k5 + x[3]));
        rate_R15 = _cytoplasm_nucleus_*((lmax - ld)*m7 + m8)*(x[14]/(k6 + x[14]));
        rate_R16 = _cytoplasm_nucleus_*n3*Math.pow(x[14], d)/(Math.pow(g4, d) + Math.pow(x[14], d));
        rate_R17 = _cytoplasm_nucleus_*m9*x[15]/(k7 + x[15]);
        rate_R18 = cytoplasm*p3*x[15];
        rate_R19 = cytoplasm*r5*x[4];
        rate_R2 = _cytoplasm_nucleus_*(n1*Math.pow(x[16], a)/(Math.pow(g1, a) + Math.pow(x[16], a)))*(Math.pow(g7, h)/(Math.pow(g7, h) + Math.pow(x[9], h)))*(Math.pow(g8, i)/(Math.pow(g8, i) + Math.pow(x[11], i)));
        rate_R20 = _cytoplasm_nucleus_*r6*x[16];
        rate_R21 = cytoplasm*m10*x[4]/(k8 + x[4]);
        rate_R22 = _cytoplasm_nucleus_*m11*x[16]/(k9 + x[16]);
        rate_R23 = _cytoplasm_nucleus_*((ld*n4 + n5)*Math.pow(g5, e)/(Math.pow(g5, e) + Math.pow(x[14], e)))*(Math.pow(g6, f)/(Math.pow(g6, f) + Math.pow(x[7], f)));
        rate_R24 = _cytoplasm_nucleus_*m12*x[17]/(k10 + x[17]);
        rate_R25 = cytoplasm*p4*x[17];
        rate_R26 = cytoplasm*r7*x[5];
        rate_R27 = _cytoplasm_nucleus_*r8*x[18];
        rate_R28 = cytoplasm*m13*x[5]/(k11 + x[5]);
        rate_R29 = _cytoplasm_nucleus_*m14*x[18]/(k12 + x[18]);
        rate_R3 = _cytoplasm_nucleus_*m1*x[6]/(k1 + x[6]);
        rate_R30 = _cytoplasm_nucleus_*(lmax - ld)*p5;
        rate_R31a = _cytoplasm_nucleus_*m15*x[12]/(k13 + x[12]);
        rate_R31b = _cytoplasm_nucleus_*q3*ld*x[12];
        rate_R32 = _cytoplasm_nucleus_*n6*Math.pow(x[7], j)/(Math.pow(g9, j) + Math.pow(x[7], j));
        rate_R33 = _cytoplasm_nucleus_*m16*x[8]/(k14 + x[8]);
        rate_R34 = cytoplasm*p6*x[8];
        rate_R35 = cytoplasm*r9*x[1];
        rate_R36 = _cytoplasm_nucleus_*r10*x[9];
        rate_R37 = cytoplasm*m17*x[1]/(k15 + x[1]);
        rate_R38 = _cytoplasm_nucleus_*m18*x[9]/(k16 + x[9]);
        rate_R39 = _cytoplasm_nucleus_*((ld*q4*x[12] + n7*ld + n8)*Math.pow(x[7], k)/(Math.pow(g10, k) + Math.pow(x[7], k)));
        rate_R4 = cytoplasm*p1*x[6];
        rate_R40 = _cytoplasm_nucleus_*m19*x[10]/(k17 + x[10]);
        rate_R41 = cytoplasm*p7*x[10];
        rate_R42 = cytoplasm*r11*x[2];
        rate_R43 = _cytoplasm_nucleus_*r12*x[11];
        rate_R44 = cytoplasm*m20*x[2]/(k18 + x[2]);
    }
    public void __internalRateVarInitFunc_1(double time, double[] x)
    {
        rate_R45 = _cytoplasm_nucleus_*m21*x[11]/(k19 + x[11]);
        rate_R5 = cytoplasm*r1*x[0];
        rate_R6 = _cytoplasm_nucleus_*r2*x[7];
        rate_R7 = cytoplasm*m2*x[0]/(k2 + x[0]);
        rate_R8 = _cytoplasm_nucleus_*m3*x[7]/(k3 + x[7]);
        rate_R9 = _cytoplasm_nucleus_*(n2*Math.pow(x[18], b)/(Math.pow(g2, b) + Math.pow(x[18], b)))*(Math.pow(g3, c)/(Math.pow(g3, c) + Math.pow(x[7], c)));
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _cytoplasm_nucleus_ = 1.0; // initial value of $"cytoplasm.nucleus"
        cytoplasm = 1.0; // initial value of $cytoplasm
        Day_in_hours = 24.0; // initial value of Day_in_hours
        a = 1.2497; // initial value of a
        b = 4.2126; // initial value of b
        c = 1.4509; // initial value of c
        d = 1.3058; // initial value of d
        e = 2.4146; // initial value of e
        f = 2.1349; // initial value of f
        g1 = 9.041; // initial value of g1
        g10 = 5.8418; // initial value of g10
        g2 = 16.6598; // initial value of g2
        g3 = 13.4112; // initial value of g3
        g4 = 20.5277; // initial value of g4
        g5 = 1.5987; // initial value of g5
        g6 = 16.489; // initial value of g6
        g7 = 0.2778; // initial value of g7
        g8 = 0.9187; // initial value of g8
        g9 = 20.3795; // initial value of g9
        h = 1.4176; // initial value of h
        i = 2.0074; // initial value of i
        j = 1.7615; // initial value of j
        k = 3.8877; // initial value of k
        k1 = 13.0594; // initial value of k1
        k10 = 11.5688; // initial value of k10
        k11 = 26.9638; // initial value of k11
        k12 = 21.8348; // initial value of k12
        k13 = 1.2; // initial value of k13
        k14 = 51.261; // initial value of k14
        k15 = 32.939; // initial value of k15
        k16 = 24.451; // initial value of k16
        k17 = 9.8065; // initial value of k17
        k18 = 25.9739; // initial value of k18
        k19 = 21.6441; // initial value of k19
        k2 = 30.5639; // initial value of k2
        k3 = 33.514; // initial value of k3
        k4 = 1.3722; // initial value of k4
        k5 = 34.2078; // initial value of k5
        k6 = 56.7596; // initial value of k6
        k7 = 14.9114; // initial value of k7
        k8 = 12.528; // initial value of k8
        k9 = 15.0626; // initial value of k9
        ld = 1.0; // initial value of ld
        lmax = 1.0; // initial value of lmax
        m1 = 6.8248; // initial value of m1
        m10 = 8.5523; // initial value of m10
        m11 = 23.5996; // initial value of m11
        m12 = 5.9504; // initial value of m12
        m13 = 7.5959; // initial value of m13
        m14 = 8.1796; // initial value of m14
        m15 = 1.2; // initial value of m15
        m16 = 9.3186; // initial value of m16
        m17 = 3.6143; // initial value of m17
        m18 = 6.7455; // initial value of m18
        m19 = 1.9234; // initial value of m19
        m2 = 9.4099; // initial value of m2
        m20 = 3.7484; // initial value of m20
        m21 = 0.0193; // initial value of m21
        m3 = 13.7795; // initial value of m3
        m4 = 12.1232; // initial value of m4
        m5 = 7.2129; // initial value of m5
        m6 = 9.5754; // initial value of m6
        m7 = 1.1032; // initial value of m7
        m8 = 2.2006; // initial value of m8
        m9 = 4.2193; // initial value of m9
        n1 = 3.2016; // initial value of n1
        n2 = 11.6086; // initial value of n2
        n3 = 2.4751; // initial value of n3
        n4 = 1.7832; // initial value of n4
        n5 = 7.4615; // initial value of n5
        n6 = 11.0924; // initial value of n6
        n7 = 0.1031; // initial value of n7
        n8 = 3.5262; // initial value of n8
        p1 = 0.6926; // initial value of p1
        p2 = 0.5403; // initial value of p2
        p3 = 6.9124; // initial value of p3
        p4 = 6.0042; // initial value of p4
        p5 = 0.5; // initial value of p5
        p6 = 9.8416; // initial value of p6
        p7 = 1.5323; // initial value of p7
        q1 = 13.4334; // initial value of q1
        q3 = 1.0; // initial value of q3
        q4 = 6.274; // initial value of q4
        r1 = 25.6818; // initial value of r1
        r10 = 0.5024; // initial value of r10
        r11 = 25.7542; // initial value of r11
        r12 = 27.2451; // initial value of r12
        r2 = 3.9781; // initial value of r2
        r3 = 51.1965; // initial value of r3
        r4 = 8.9147; // initial value of r4
        r5 = 29.4607; // initial value of r5
        r6 = 4.5034; // initial value of r6
        r7 = 35.7842; // initial value of r7
        r8 = 27.9229; // initial value of r8
        r9 = 24.5689; // initial value of r9
    }

    /*
     * 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_R31a;
    protected double rate_R31b;
    protected double rate_R32;
    protected double rate_R33;
    protected double rate_R34;
    protected double rate_R35;
    protected double rate_R36;
    protected double rate_R37;
    protected double rate_R38;
    protected double rate_R39;
    protected double rate_R4;
    protected double rate_R40;
    protected double rate_R41;
    protected double rate_R42;
    protected double rate_R43;
    protected double rate_R44;
    protected double rate_R45;
    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 Day_in_hours;
    protected double a;
    protected double b;
    protected double c;
    protected double d;
    protected double e;
    protected double f;
    protected double g1;
    protected double g10;
    protected double g2;
    protected double g3;
    protected double g4;
    protected double g5;
    protected double g6;
    protected double g7;
    protected double g8;
    protected double g9;
    protected double h;
    protected double i;
    protected double j;
    protected double k;
    protected double k1;
    protected double k10;
    protected double k11;
    protected double k12;
    protected double k13;
    protected double k14;
    protected double k15;
    protected double k16;
    protected double k17;
    protected double k18;
    protected double k19;
    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 ld;
    protected double lmax;
    protected double m1;
    protected double m10;
    protected double m11;
    protected double m12;
    protected double m13;
    protected double m14;
    protected double m15;
    protected double m16;
    protected double m17;
    protected double m18;
    protected double m19;
    protected double m2;
    protected double m20;
    protected double m21;
    protected double m3;
    protected double m4;
    protected double m5;
    protected double m6;
    protected double m7;
    protected double m8;
    protected double m9;
    protected double n1;
    protected double n2;
    protected double n3;
    protected double n4;
    protected double n5;
    protected double n6;
    protected double n7;
    protected double n8;
    protected double p1;
    protected double p2;
    protected double p3;
    protected double p4;
    protected double p5;
    protected double p6;
    protected double p7;
    protected double q1;
    protected double q3;
    protected double q4;
    protected double r1;
    protected double r10;
    protected double r11;
    protected double r12;
    protected double r2;
    protected double r3;
    protected double r4;
    protected double r5;
    protected double r6;
    protected double r7;
    protected double r8;
    protected double r9;
    protected double time;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;


        double[] y = new double[21];
        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] = Day_in_hours;
        y[20] = ld;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[19];
        this.time = 0.0;
        x[0] = 0.0659; // - $"cytoplasm.cLc"
        x[1] = 0.0861; // - $"cytoplasm.cP7c"
        x[2] = 0.0077; // - $"cytoplasm.cP9c"
        x[3] = 0.7852; // - $"cytoplasm.cTc"
        x[4] = 2.5615; // - $"cytoplasm.cXc"
        x[5] = 7.6795; // - $"cytoplasm.cYc"
        x[6] = 0.1951; // - $"cytoplasm.nucleus.cLm"
        x[7] = 0.3952; // - $"cytoplasm.nucleus.cLn"
        x[8] = 0.0772; // - $"cytoplasm.nucleus.cP7m"
        x[9] = 2.7182; // - $"cytoplasm.nucleus.cP7n"
        x[10] = 0.0011; // - $"cytoplasm.nucleus.cP9m"
        x[11] = 0.0073; // - $"cytoplasm.nucleus.cP9n"
        x[12] = 0.0; // - $"cytoplasm.nucleus.cPn"
        x[13] = 0.1234; // - $"cytoplasm.nucleus.cTm"
        x[14] = 4.5209; // - $"cytoplasm.nucleus.cTn"
        x[15] = 1.6104; // - $"cytoplasm.nucleus.cXm"
        x[16] = 14.5474; // - $"cytoplasm.nucleus.cXn"
        x[17] = 1.1149; // - $"cytoplasm.nucleus.cYm"
        x[18] = 9.7016; // - $"cytoplasm.nucleus.cYn"

        __internalRateVarInitFunc_0(time, x);
        __internalRateVarInitFunc_1(time, x);

        return x;
    }

/*
 * code for algebraic rules calculations
 */

/*
 * end of code for algebraic rules calculations
 */

    protected void calculateRates(double time, double[] x)
    {

        __internalRateVarInitFunc_0(time, x);
        __internalRateVarInitFunc_1(time, x);

    }

        /*
         * calculate dy/dt for 'BIOMD0000000097.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_R4-rate_R5+rate_R6-rate_R7;
        result[1] = +rate_R34-rate_R35+rate_R36-rate_R37;
        result[2] = +rate_R41-rate_R42+rate_R43-rate_R44;
        result[3] = +rate_R11-rate_R12+rate_R13-rate_R14;
        result[4] = +rate_R18-rate_R19+rate_R20-rate_R21;
        result[5] = +rate_R25-rate_R26+rate_R27-rate_R28;
        result[6] = +rate_R1+rate_R2-rate_R3;
        result[7] = +rate_R5-rate_R6-rate_R8;
        result[8] = +rate_R32-rate_R33;
        result[9] = +rate_R35-rate_R36-rate_R38;
        result[10] = +rate_R39-rate_R40;
        result[11] = +rate_R42-rate_R43-rate_R45;
        result[12] = +rate_R30-rate_R31a-rate_R31b;
        result[13] = -rate_R10+rate_R9;
        result[14] = +rate_R12-rate_R13-rate_R15;
        result[15] = +rate_R16-rate_R17;
        result[16] = +rate_R19-rate_R20-rate_R22;
        result[17] = +rate_R23-rate_R24;
        result[18] = +rate_R26-rate_R27-rate_R29;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[19];
        __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;

        __internalRateVarInitFunc_0(time, x);
        __internalRateVarInitFunc_1(time, x);

        double [] flags = new double[2];
        flags[0] = (Day_in_hours - time <= 0) ? +1 : -1; //event_0000001
        flags[1] = (Day_in_hours - time <= 16 && Day_in_hours - time > 0) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041135150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041135150 == 0) //event_0000001
        {
            Day_in_hours = Day_in_hours + 24;
            ld = 1;
        } else if ( __internalVar12060041135150 == 1) //event_0000002
        {
            ld = 0;
        }
    }

} // class ...