/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000140.xml diagram  at 2008.03.20 15:09:18
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000140 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        Total_IkBbeta = x[9] + x[10] + x[3] + x[4];
        Total_IkBeps = x[11] + x[12] + x[5] + x[6];
        Total_IkBalpha = x[7] + x[8] + x[1] + x[2];
        Total_NFkBn = x[18] + x[23];
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_v1 = cytoplasm*(a4*x[7]*x[13] - d4*x[8]);
        rate_v10 = cytoplasm*deg4*x[8];
        rate_v11 = cytoplasm*deg4*x[10];
        rate_v12 = cytoplasm*deg4*x[12];
        rate_v13 = cytoplasm*k1*x[13] - _cytoplasm_nucleus_*k01*x[23];
        rate_v14 = _cytoplasm_nucleus_*(a4*x[14]*x[23] - d4*x[15]);
        rate_v15 = _cytoplasm_nucleus_*(a5*x[17]*x[23] - d5*x[18]);
        rate_v16 = _cytoplasm_nucleus_*(a6*x[20]*x[23] - d6*x[21]);
        rate_v17 = _cytoplasm_nucleus_*tr2a;
        rate_v18 = _cytoplasm_nucleus_*tr2*Math.pow(x[23], 2);
        rate_v19 = _cytoplasm_nucleus_*tr3*x[16];
        rate_v2 = cytoplasm*(a5*x[9]*x[13] - d5*x[10]);
        rate_v20 = _cytoplasm_nucleus_*tr2b;
        rate_v21 = _cytoplasm_nucleus_*tr3*x[19];
        rate_v22 = _cytoplasm_nucleus_*tr2e;
        rate_v23 = _cytoplasm_nucleus_*tr3*x[22];
        rate_v24 = cytoplasm*(a1*x[7]*x[0] - d1*x[1]);
        rate_v25 = _cytoplasm_nucleus_*tr1*x[16];
        rate_v26 = cytoplasm*deg1*x[7];
        rate_v27 = cytoplasm*tp1*x[7] - _cytoplasm_nucleus_*tp2*x[14];
        rate_v28 = cytoplasm*(a2*x[9]*x[0] - d2*x[3]);
        rate_v29 = _cytoplasm_nucleus_*tr1*x[19];
        rate_v3 = cytoplasm*(a6*x[11]*x[13] - d6*x[12]);
        rate_v30 = cytoplasm*deg1*x[9];
        rate_v31 = cytoplasm*0.5*tp1*x[9] - _cytoplasm_nucleus_*0.5*tp2*x[17];
        rate_v32 = cytoplasm*(a3*x[11]*x[0] - d3*x[5]);
        rate_v33 = _cytoplasm_nucleus_*tr1*x[22];
        rate_v34 = cytoplasm*deg1*x[11];
        rate_v35 = cytoplasm*0.5*tp1*x[11] - _cytoplasm_nucleus_*0.5*tp2*x[20];
        rate_v36 = cytoplasm*(a7*x[0]*x[8] - d1*x[2]);
        rate_v37 = _cytoplasm_nucleus_*k2*x[15];
        rate_v38 = cytoplasm*(a8*x[0]*x[10] - d2*x[4]);
        rate_v39 = _cytoplasm_nucleus_*v39_k2_IkBbeta_nuc_NFkB_nuc*(x[24] + flag_for_after_trigger)*x[18];
        rate_v4 = cytoplasm*(a4*x[1]*x[13] - d4*x[2]);
        rate_v40 = cytoplasm*(a9*x[0]*x[12] - d3*x[6]);
        rate_v41 = _cytoplasm_nucleus_*0.5*k2_eps*x[21];
        rate_v42 = cytoplasm*r1*x[1];
        rate_v43 = cytoplasm*r2*x[3];
        rate_v44 = cytoplasm*r3*x[5];
        rate_v45 = cytoplasm*k02*x[0];
    }
    public void __internalRateVarInitFunc_1(double time, double[] x)
    {
        rate_v5 = cytoplasm*r4*x[2];
        rate_v6 = cytoplasm*(a5*x[3]*x[13] - d5*x[4]);
        rate_v7 = cytoplasm*r5*x[4];
        rate_v8 = cytoplasm*(a6*x[5]*x[13] - d6*x[6]);
        rate_v9 = cytoplasm*r6*x[6];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _cytoplasm_nucleus_ = 1.0; // initial value of $"cytoplasm.nucleus"
        cytoplasm = 1.0; // initial value of $cytoplasm
        a1 = 1.35; // initial value of a1
        a2 = 0.36; // initial value of a2
        a3 = 0.54; // initial value of a3
        a4 = 30.0; // initial value of a4
        a5 = 30.0; // initial value of a5
        a6 = 30.0; // initial value of a6
        a7 = 11.1; // initial value of a7
        a8 = 2.88; // initial value of a8
        a9 = 4.2; // initial value of a9
        d1 = 0.075; // initial value of d1
        d2 = 0.105; // initial value of d2
        d3 = 0.105; // initial value of d3
        d4 = 0.03; // initial value of d4
        d5 = 0.03; // initial value of d5
        d6 = 0.03; // initial value of d6
        deg1 = 0.00678; // initial value of deg1
        deg4 = 0.00135; // initial value of deg4
        flag_for_after_trigger = 0.5; // initial value of flag_for_after_trigger
        k01 = 0.0048; // initial value of k01
        k02 = 0.0072; // initial value of k02
        k1 = 5.4; // initial value of k1
        k2 = 0.828; // initial value of k2
        k2_eps = 0.624; // initial value of k2_eps
        r1 = 0.2442; // initial value of r1
        r2 = 0.09; // initial value of r2
        r3 = 0.132; // initial value of r3
        r4 = 1.224; // initial value of r4
        r5 = 0.45; // initial value of r5
        r6 = 0.66; // initial value of r6
        tp1 = 0.018; // initial value of tp1
        tp2 = 0.012; // initial value of tp2
        tr1 = 0.2448; // initial value of tr1
        tr2 = 0.99; // initial value of tr2
        tr2a = 9.25E-5; // initial value of tr2a
        tr2b = 1.068E-5; // initial value of tr2b
        tr2e = 7.62E-6; // initial value of tr2e
        tr3 = 0.0168; // initial value of tr3
        v39_k2_IkBbeta_nuc_NFkB_nuc = 0.0069; // initial value of v39_k2_IkBbeta_nuc_NFkB_nuc
    }

    /*
     * Model variables initial values
     */
    protected double _cytoplasm_nucleus_;
    protected double rate_v1;
    protected double rate_v10;
    protected double rate_v11;
    protected double rate_v12;
    protected double rate_v13;
    protected double rate_v14;
    protected double rate_v15;
    protected double rate_v16;
    protected double rate_v17;
    protected double rate_v18;
    protected double rate_v19;
    protected double rate_v2;
    protected double rate_v20;
    protected double rate_v21;
    protected double rate_v22;
    protected double rate_v23;
    protected double rate_v24;
    protected double rate_v25;
    protected double rate_v26;
    protected double rate_v27;
    protected double rate_v28;
    protected double rate_v29;
    protected double rate_v3;
    protected double rate_v30;
    protected double rate_v31;
    protected double rate_v32;
    protected double rate_v33;
    protected double rate_v34;
    protected double rate_v35;
    protected double rate_v36;
    protected double rate_v37;
    protected double rate_v38;
    protected double rate_v39;
    protected double rate_v4;
    protected double rate_v40;
    protected double rate_v41;
    protected double rate_v42;
    protected double rate_v43;
    protected double rate_v44;
    protected double rate_v45;
    protected double rate_v5;
    protected double rate_v6;
    protected double rate_v7;
    protected double rate_v8;
    protected double rate_v9;
    protected double cytoplasm;
    protected double Total_IkBalpha;
    protected double Total_IkBbeta;
    protected double Total_IkBeps;
    protected double Total_NFkBn;
    protected double a1;
    protected double a2;
    protected double a3;
    protected double a4;
    protected double a5;
    protected double a6;
    protected double a7;
    protected double a8;
    protected double a9;
    protected double d1;
    protected double d2;
    protected double d3;
    protected double d4;
    protected double d5;
    protected double d6;
    protected double deg1;
    protected double deg4;
    protected double flag_for_after_trigger;
    protected double k01;
    protected double k02;
    protected double k1;
    protected double k2;
    protected double k2_eps;
    protected double r1;
    protected double r2;
    protected double r3;
    protected double r4;
    protected double r5;
    protected double r6;
    protected double time;
    protected double tp1;
    protected double tp2;
    protected double tr1;
    protected double tr2;
    protected double tr2a;
    protected double tr2b;
    protected double tr2e;
    protected double tr3;
    protected double trigger_value;
    protected double v39_k2_IkBbeta_nuc_NFkB_nuc;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[31];
        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] = Total_IkBalpha;
        y[25] = Total_IkBbeta;
        y[26] = Total_IkBeps;
        y[27] = Total_NFkBn;
        y[28] = flag_for_after_trigger;
        y[29] = x[24];
        y[30] = trigger_value;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[25];
        this.time = 0.0;
        x[0] = 0.0; // - $"cytoplasm.IKK"
        x[1] = 0.0; // - $"cytoplasm.IKK_IkBalpha"
        x[2] = 0.0; // - $"cytoplasm.IKK_IkBalpha_NFkB"
        x[3] = 0.0; // - $"cytoplasm.IKK_IkBbeta"
        x[4] = 0.0; // - $"cytoplasm.IKK_IkBbeta_NFkB"
        x[5] = 0.0; // - $"cytoplasm.IKK_IkBeps"
        x[6] = 0.0; // - $"cytoplasm.IKK_IkBeps_NFkB"
        x[7] = 0.1; // - $"cytoplasm.IkBalpha"
        x[8] = 0.0; // - $"cytoplasm.IkBalpha_NFkB"
        x[9] = 0.0; // - $"cytoplasm.IkBbeta"
        x[10] = 0.0; // - $"cytoplasm.IkBbeta_NFkB"
        x[11] = 0.0; // - $"cytoplasm.IkBeps"
        x[12] = 0.0; // - $"cytoplasm.IkBeps_NFkB"
        x[13] = 0.1; // - $"cytoplasm.NFkB"
        x[14] = 0.0; // - $"cytoplasm.nucleus.IkBalpha_nuc"
        x[15] = 0.0; // - $"cytoplasm.nucleus.IkBalpha_nuc_NFkB_nuc"
        x[16] = 0.0; // - $"cytoplasm.nucleus.IkBalpha_transcript"
        x[17] = 0.0; // - $"cytoplasm.nucleus.IkBbeta_nuc"
        x[18] = 0.0; // - $"cytoplasm.nucleus.IkBbeta_nuc_NFkB_nuc"
        x[19] = 0.0; // - $"cytoplasm.nucleus.IkBbeta_transcript"
        x[20] = 0.0; // - $"cytoplasm.nucleus.IkBeps_nuc"
        x[21] = 0.0; // - $"cytoplasm.nucleus.IkBeps_nuc_NFkB_nuc"
        x[22] = 0.0; // - $"cytoplasm.nucleus.IkBeps_transcript"
        x[23] = 0.0010; // - $"cytoplasm.nucleus.NFkB_nuc"
        x[24] = 0.5; // - fr_after_trigger

        __internalVarInitFunc_0(time, x);
        __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)
    {

        __internalVarInitFunc_0(time, x);
        __internalRateVarInitFunc_0(time, x);
        __internalRateVarInitFunc_1(time, x);

    }

        /*
         * calculate dy/dt for 'BIOMD0000000140.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_v24-rate_v28-rate_v32-rate_v36-rate_v38-rate_v40+rate_v42+rate_v43+rate_v44-rate_v45+rate_v5+rate_v7+rate_v9;
        result[1] = +rate_v24-rate_v42-rate_v4;
        result[2] = +rate_v36+rate_v4-rate_v5;
        result[3] = +rate_v28-rate_v43-rate_v6;
        result[4] = +rate_v38+rate_v6-rate_v7;
        result[5] = +rate_v32-rate_v44-rate_v8;
        result[6] = +rate_v40+rate_v8-rate_v9;
        result[7] = -rate_v1-rate_v24+rate_v25-rate_v26-rate_v27;
        result[8] = -rate_v10+rate_v1-rate_v36+rate_v37;
        result[9] = -rate_v28+rate_v29-rate_v2-rate_v30-rate_v31;
        result[10] = -rate_v11+rate_v2-rate_v38+rate_v39;
        result[11] = -rate_v32+rate_v33-rate_v34-rate_v35-rate_v3;
        result[12] = -rate_v12+rate_v3-rate_v40+rate_v41;
        result[13] = +rate_v10+rate_v11+rate_v12-rate_v13-rate_v1-rate_v2-rate_v3-rate_v4+rate_v5-rate_v6+rate_v7-rate_v8+rate_v9;
        result[14] = -rate_v14+rate_v27;
        result[15] = +rate_v14-rate_v37;
        result[16] = +rate_v17+rate_v18-rate_v19;
        result[17] = -rate_v15+rate_v31;
        result[18] = +rate_v15-rate_v39;
        result[19] = +rate_v20-rate_v21;
        result[20] = -rate_v16+rate_v35;
        result[21] = +rate_v16-rate_v41;
        result[22] = +rate_v22-rate_v23;
        result[23] = +rate_v13-rate_v14-rate_v15-rate_v16;
        result[24] = +trigger_value*(-0.5/Math.pow((1 + (time - 2000)), 2));
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[25];
        __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);
        __internalRateVarInitFunc_1(time, x);

        double [] flags = new double[1];
        flags[0] = (time >= 2000 && trigger_value == 0) ? +1 : -1; //event_0000001
        return flags;
    }

    public void processEvent(int __internalVar12060041586250, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041586250 == 0) //event_0000001
        {
            x[0] = 0.1;
            trigger_value = 1;
            flag_for_after_trigger = 0;
        }
    }

} // class ...