/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000007.xml diagram  at 2008.03.20 15:03:18
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000007 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        _Cell_Cdc25B_ = 1 + -1.0*x[0];
        _Cell_Rum1Total_ = x[2] + x[4] + x[6] + x[7];
        _Cell_Cdc13Total_ = x[3] + x[4] + x[5] + x[6];
        _Cell_Cig2Total_ = x[1] + x[2];
        k2 = x[8]*V2 + (1 + -1.0*x[8])*V2prime;
        k6 = x[9]*V6 + (1 + -1.0*x[9])*V6prime;
        kwee = Vwprime*(1 + -1.0*x[10]) + Vw*x[10];
        k25 = x[0]*V25 + (1 + -1.0*x[0])*V25prime;
        _Cell_MPF_ = x[3] + beta*x[5];
        _Cell_SPF_ = Cig1 + alpha*x[1] + _Cell_MPF_;
        _Cell_IEB_ = 1 + -1.0*x[11];
        _Cell_UbEB_ = 1 + -1.0*x[8];
        _Cell_UbE2B_ = 1 + -1.0*x[9];
        _Cell_Wee1B_ = 1 + -1.0*x[10];
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_Cdc25_Reaction = _Cell_Cdc25B_*kc*_Cell_MPF_*Math.pow((_Cell_Cdc25B_ + Kmc), -1.0) + -1.0*x[0]*kcr*Math.pow((x[0] + Kmcr), -1.0);
        rate_Cdc2Phos = x[3]*kwee + -1.0*k25*x[5];
        rate_G1K_Creation = k5;
        rate_G1K_Dissociation = x[1]*k6;
        rate_G1R_Binding = x[1]*k8*x[7] + -1.0*x[2]*k8r;
        rate_G1R_Dissociation = x[2]*k6prime;
        rate_G2K_Creation = k1;
        rate_G2K_dissoc = x[3]*k2;
        rate_G2R_Creation = x[3]*k7*x[7] + -1.0*x[4]*k7r;
        rate_G2R_Dissociation = x[4]*k2prime;
        rate_G2R_Dissociation_UbE = x[4]*k2;
        rate_IE_Reaction = _Cell_IEB_*ki*_Cell_MPF_*Math.pow((_Cell_IEB_ + Kmi), -1.0) + -1.0*x[11]*kir*Math.pow((x[11] + Kmir), -1.0);
        rate_PG2R_Creation = k7*x[5]*x[7] + -1.0*k7r*x[6];
        rate_PG2R_Dissociation = k2prime*x[6];
        rate_PG2R_Dissociation_UbE = k2*x[6];
        rate_PG2_dissoc = k2*x[5];
        rate_Rum1Deg = k4*x[7];
        rate_Rum1DegInG2R = x[4]*k4;
        rate_Rum1DegInPG2R = k4*x[6];
        rate_Rum1_Deg_SPF = kp*x[12]*x[7]*_Cell_SPF_*Math.pow((Kmp + x[7]), -1.0);
        rate_Rum1_Production = k3;
        rate_RumDegInG1R = x[2]*k4;
        rate_UbE2_Reaction = ku2*_Cell_MPF_*_Cell_UbE2B_*Math.pow((Kmu2 + x[9]), -1.0) + -1.0*kur2*x[9]*Math.pow((Kmur2 + x[9]), -1.0);
        rate_UbE_Reaction = x[11]*ku*_Cell_UbEB_*Math.pow((Kmu + _Cell_UbEB_), -1.0) + -1.0*kur*x[8]*Math.pow((Kmur + x[8]), -1.0);
        rate_Wee1_Reaction = kwr*_Cell_Wee1B_*Math.pow((Kmwr + _Cell_Wee1B_), -1.0) + -1.0*kw*_Cell_MPF_*x[10]*Math.pow((Kmw + x[10]), -1.0);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        Kmc = 0.1; // initial value of Kmc
        Kmcr = 0.1; // initial value of Kmcr
        Kmi = 0.01; // initial value of Kmi
        Kmir = 0.01; // initial value of Kmir
        Kmp = 0.0010; // initial value of Kmp
        Kmu = 0.01; // initial value of Kmu
        Kmu2 = 0.05; // initial value of Kmu2
        Kmur = 0.01; // initial value of Kmur
        Kmur2 = 0.05; // initial value of Kmur2
        Kmw = 0.1; // initial value of Kmw
        Kmwr = 0.1; // initial value of Kmwr
        V2 = 0.25; // initial value of V2
        V25 = 0.5; // initial value of V25
        V25prime = 0.025; // initial value of V25prime
        V2prime = 0.0075; // initial value of V2prime
        V6 = 7.5; // initial value of V6
        V6prime = 0.0375; // initial value of V6prime
        Vw = 0.35; // initial value of Vw
        Vwprime = 0.035; // initial value of Vwprime
        alpha = 0.25; // initial value of alpha
        beta = 0.05; // initial value of beta
        k1 = 0.015; // initial value of k1
        k2prime = 0.05; // initial value of k2prime
        k3 = 0.09375; // initial value of k3
        k4 = 0.1875; // initial value of k4
        k5 = 0.00175; // initial value of k5
        k7 = 100.0; // initial value of k7
        k7r = 0.1; // initial value of k7r
        k8 = 10.0; // initial value of k8
        k8r = 0.1; // initial value of k8r
        kc = 1.0; // initial value of kc
        kcr = 0.25; // initial value of kcr
        ki = 0.4; // initial value of ki
        kir = 0.1; // initial value of kir
        kp = 3.25; // initial value of kp
        ku = 0.2; // initial value of ku
        ku2 = 1.0; // initial value of ku2
        kur = 0.1; // initial value of kur
        kur2 = 0.3; // initial value of kur2
        kw = 1.0; // initial value of kw
        kwr = 0.25; // initial value of kwr
        mu = 0.00495; // initial value of mu
    }

    /*
     * Model variables initial values
     */
    protected double _Cell_Cdc13Total_;
    protected double _Cell_Cdc25B_;
    protected double _Cell_Cig2Total_;
    protected double _Cell_IEB_;
    protected double _Cell_MPF_;
    protected double _Cell_Rum1Total_;
    protected double _Cell_SPF_;
    protected double _Cell_UbE2B_;
    protected double _Cell_UbEB_;
    protected double _Cell_Wee1B_;
    protected double rate_Cdc25_Reaction;
    protected double rate_Cdc2Phos;
    protected double rate_G1K_Creation;
    protected double rate_G1K_Dissociation;
    protected double rate_G1R_Binding;
    protected double rate_G1R_Dissociation;
    protected double rate_G2K_Creation;
    protected double rate_G2K_dissoc;
    protected double rate_G2R_Creation;
    protected double rate_G2R_Dissociation;
    protected double rate_G2R_Dissociation_UbE;
    protected double rate_IE_Reaction;
    protected double rate_PG2R_Creation;
    protected double rate_PG2R_Dissociation;
    protected double rate_PG2R_Dissociation_UbE;
    protected double rate_PG2_dissoc;
    protected double rate_Rum1Deg;
    protected double rate_Rum1DegInG2R;
    protected double rate_Rum1DegInPG2R;
    protected double rate_Rum1_Deg_SPF;
    protected double rate_Rum1_Production;
    protected double rate_RumDegInG1R;
    protected double rate_UbE2_Reaction;
    protected double rate_UbE_Reaction;
    protected double rate_Wee1_Reaction;
    protected double Cig1;
    protected double Kmc;
    protected double Kmcr;
    protected double Kmi;
    protected double Kmir;
    protected double Kmp;
    protected double Kmu;
    protected double Kmu2;
    protected double Kmur;
    protected double Kmur2;
    protected double Kmw;
    protected double Kmwr;
    protected double V2;
    protected double V25;
    protected double V25prime;
    protected double V2prime;
    protected double V6;
    protected double V6prime;
    protected double Vw;
    protected double Vwprime;
    protected double alpha;
    protected double beta;
    protected double k1;
    protected double k2;
    protected double k25;
    protected double k2prime;
    protected double k3;
    protected double k4;
    protected double k5;
    protected double k6;
    protected double k6prime;
    protected double k7;
    protected double k7r;
    protected double k8;
    protected double k8r;
    protected double kc;
    protected double kcr;
    protected double ki;
    protected double kir;
    protected double kp;
    protected double ku;
    protected double ku2;
    protected double kur;
    protected double kur2;
    protected double kw;
    protected double kwee;
    protected double kwr;
    protected double mu;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[28];
        y[0] = _Cell_Cdc13Total_;
        y[1] = x[0];
        y[2] = _Cell_Cdc25B_;
        y[3] = _Cell_Cig2Total_;
        y[4] = x[1];
        y[5] = x[2];
        y[6] = x[3];
        y[7] = x[4];
        y[8] = _Cell_IEB_;
        y[9] = _Cell_MPF_;
        y[10] = x[5];
        y[11] = x[6];
        y[12] = x[7];
        y[13] = _Cell_Rum1Total_;
        y[14] = _Cell_SPF_;
        y[15] = x[8];
        y[16] = x[9];
        y[17] = _Cell_UbE2B_;
        y[18] = _Cell_UbEB_;
        y[19] = x[10];
        y[20] = _Cell_Wee1B_;
        y[21] = x[11];
        y[22] = x[12];
        y[23] = k2;
        y[24] = k25;
        y[25] = k6;
        y[26] = kp;
        y[27] = kwee;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[13];
        this.time = 0.0;
        x[0] = 0.0; // - $"Cell.Cdc25"
        x[1] = 0.0; // - $"Cell.G1K"
        x[2] = 0.0; // - $"Cell.G1R"
        x[3] = 0.0; // - $"Cell.G2K"
        x[4] = 0.0; // - $"Cell.G2R"
        x[5] = 0.0; // - $"Cell.PG2"
        x[6] = 0.0; // - $"Cell.PG2R"
        x[7] = 0.4; // - $"Cell.R"
        x[8] = 1.0; // - $"Cell.UbE"
        x[9] = 0.0; // - $"Cell.UbE2"
        x[10] = 0.0; // - $"Cell.Wee1"
        x[11] = 0.0; // - $Cell.IE
        x[12] = 1.0; // - Mass

        __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 'BIOMD0000000007.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_Cdc25_Reaction;
        result[1] = +rate_G1K_Creation-rate_G1K_Dissociation-rate_G1R_Binding+rate_RumDegInG1R;
        result[2] = +rate_G1R_Binding-rate_G1R_Dissociation-rate_RumDegInG1R;
        result[3] = -rate_Cdc2Phos+rate_G2K_Creation-rate_G2K_dissoc-rate_G2R_Creation+rate_Rum1DegInG2R;
        result[4] = +rate_G2R_Creation-rate_G2R_Dissociation-rate_G2R_Dissociation_UbE-rate_Rum1DegInG2R;
        result[5] = +rate_Cdc2Phos-rate_PG2R_Creation-rate_PG2_dissoc+rate_Rum1DegInPG2R;
        result[6] = +rate_PG2R_Creation-rate_PG2R_Dissociation-rate_PG2R_Dissociation_UbE-rate_Rum1DegInPG2R;
        result[7] = -rate_G1R_Binding+rate_G1R_Dissociation-rate_G2R_Creation+rate_G2R_Dissociation+rate_G2R_Dissociation_UbE-rate_PG2R_Creation+rate_PG2R_Dissociation+rate_PG2R_Dissociation_UbE-rate_Rum1Deg-rate_Rum1_Deg_SPF+rate_Rum1_Production;
        result[8] = +rate_UbE_Reaction;
        result[9] = +rate_UbE2_Reaction;
        result[10] = +rate_Wee1_Reaction;
        result[11] = +rate_IE_Reaction;
        result[12] = +x[12]*mu;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[13];
        __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] = (x[8] <= 0.1) ? +1 : -1; //Division
        flags[1] = (_Cell_SPF_ >= 0.1) ? +1 : -1; //Start
        return flags;
    }

    public void processEvent(int __internalVar12060037985150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060037985150 == 0) //Division
        {
            kp = 2*kp;
            x[12] = x[12]*0.5;
        } else if ( __internalVar12060037985150 == 1) //Start
        {
            kp = kp*0.5;
        }
    }

} // class ...