/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000111.xml diagram  at 2008.03.20 15:08:47
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000111 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        sigma = x[2] + x[4] + Kdiss;
        k25 = k25_prime + (k25_double_prime - k25_prime)*(2*Va25*_cell_MPF_*Ji25/(Vi25 - Va25*_cell_MPF_ + Vi25*Ja25 + Va25*_cell_MPF_*Ji25 + Math.pow(Math.pow((Vi25 - Va25*_cell_MPF_ + Vi25*Ja25 + Va25*_cell_MPF_*Ji25), 2) - 4*(Vi25 - Va25*_cell_MPF_)*Va25*_cell_MPF_*Ji25, 1.0/( (double)(2)))));
        Trimer = 2*x[2]*x[4]/(sigma + Math.pow((Math.pow(sigma, 2) - 4*x[2]*x[4]), 0.5));
        _cell_MPF_ = (x[2] - x[3])*(x[2] - Trimer)/x[2];
        TF = 2*k15*x[1]*J16/(k16_prime + k16_double_prime*_cell_MPF_ - k15*x[1] + (k16_prime + k16_double_prime*_cell_MPF_)*J15 + k15*x[1]*J16 + Math.pow(Math.pow((k16_prime + k16_double_prime*_cell_MPF_ - k15*x[1] + (k16_prime + k16_double_prime*_cell_MPF_)*J15 + k15*x[1]*J16), 2) - 4*(k16_prime + k16_double_prime*_cell_MPF_ - k15*x[1])*k15*x[1]*J16, 1.0/( (double)(2))));
        kwee = kwee_prime + (kwee_double_prime - kwee_prime)*(2*Vawee*Jiwee/(Viwee*_cell_MPF_ - Vawee + Viwee*_cell_MPF_*Jawee + Vawee*Jiwee + Math.pow(Math.pow((Viwee*_cell_MPF_ - Vawee + Viwee*_cell_MPF_*Jawee + Vawee*Jiwee), 2) - 4*(Viwee*_cell_MPF_ - Vawee)*Vawee*Jiwee, 1.0/( (double)(2)))));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_R1 = k1*x[1];
        rate_R10 = k7*x[0]*(x[6] - x[5])/(J7 + x[6] - x[5]);
        rate_R11 = k8*x[5]/(J8 + x[5]);
        rate_R12 = k6*x[5];
        rate_R13 = k9*_cell_MPF_*(1 - x[0])/(J9 + 1 - x[0]);
        rate_R14 = k10*x[0]/(J10 + x[0]);
        rate_R15 = k11;
        rate_R16 = (k12 + k12_prime*x[8] + k12_double_prime*_cell_MPF_)*x[4];
        rate_R17 = k13*TF;
        rate_R18 = k14*x[8];
        rate_R19 = mu*x[1];
        rate_R2 = (k2_prime + k2_double_prime*x[7] + k2_triple_prime*x[5])*x[2];
        rate_R3 = kwee*(x[2] - x[3]);
        rate_R4 = k25*x[3];
        rate_R5 = (k2_prime + k2_double_prime*x[7] + k2_triple_prime*x[5])*x[3];
        rate_R6 = (k3_prime + k3_double_prime*x[5])*(1 - x[7])/(J3 + 1 - x[7]);
        rate_R7 = (k4_prime*x[8] + k4*_cell_MPF_)*x[7]/(J4 + x[7]);
        rate_R8 = k5_prime + k5_double_prime*Math.pow(_cell_MPF_, 4)/(Math.pow(J5, 4) + Math.pow(_cell_MPF_, 4));
        rate_R9 = k6*x[6];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        J10 = 0.01; // initial value of J10
        J15 = 0.01; // initial value of J15
        J16 = 0.01; // initial value of J16
        J3 = 0.01; // initial value of J3
        J4 = 0.01; // initial value of J4
        J5 = 0.3; // initial value of J5
        J7 = 0.0010; // initial value of J7
        J8 = 0.0010; // initial value of J8
        J9 = 0.01; // initial value of J9
        Ja25 = 0.01; // initial value of Ja25
        Jawee = 0.01; // initial value of Jawee
        Ji25 = 0.01; // initial value of Ji25
        Jiwee = 0.01; // initial value of Jiwee
        Kdiss = 0.0010; // initial value of Kdiss
        Va25 = 1.0; // initial value of Va25
        Vawee = 0.25; // initial value of Vawee
        Vi25 = 0.25; // initial value of Vi25
        Viwee = 1.0; // initial value of Viwee
        k1 = 0.03; // initial value of k1
        k10 = 0.04; // initial value of k10
        k11 = 0.1; // initial value of k11
        k12 = 0.01; // initial value of k12
        k12_double_prime = 3.0; // initial value of k12_double_prime
        k12_prime = 1.0; // initial value of k12_prime
        k13 = 0.1; // initial value of k13
        k14 = 0.1; // initial value of k14
        k15 = 1.5; // initial value of k15
        k16_double_prime = 2.0; // initial value of k16_double_prime
        k16_prime = 1.0; // initial value of k16_prime
        k25_double_prime = 5.0; // initial value of k25_double_prime
        k25_prime = 0.05; // initial value of k25_prime
        k2_double_prime = 1.0; // initial value of k2_double_prime
        k2_prime = 0.03; // initial value of k2_prime
        k2_triple_prime = 0.1; // initial value of k2_triple_prime
        k3_double_prime = 10.0; // initial value of k3_double_prime
        k3_prime = 1.0; // initial value of k3_prime
        k4 = 35.0; // initial value of k4
        k4_prime = 2.0; // initial value of k4_prime
        k5_double_prime = 0.3; // initial value of k5_double_prime
        k5_prime = 0.0050; // initial value of k5_prime
        k6 = 0.1; // initial value of k6
        k7 = 1.0; // initial value of k7
        k8 = 0.25; // initial value of k8
        k9 = 0.1; // initial value of k9
        kwee_double_prime = 1.3; // initial value of kwee_double_prime
        kwee_prime = 0.15; // initial value of kwee_prime
        mu = 0.0050; // initial value of mu
    }

    /*
     * Model variables initial values
     */
    protected double _cell_MPF_;
    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_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 J10;
    protected double J15;
    protected double J16;
    protected double J3;
    protected double J4;
    protected double J5;
    protected double J7;
    protected double J8;
    protected double J9;
    protected double Ja25;
    protected double Jawee;
    protected double Ji25;
    protected double Jiwee;
    protected double Kdiss;
    protected double TF;
    protected double Trimer;
    protected double Va25;
    protected double Vawee;
    protected double Vi25;
    protected double Viwee;
    protected double flag_MPF;
    protected double k1;
    protected double k10;
    protected double k11;
    protected double k12;
    protected double k12_double_prime;
    protected double k12_prime;
    protected double k13;
    protected double k14;
    protected double k15;
    protected double k16_double_prime;
    protected double k16_prime;
    protected double k25;
    protected double k25_double_prime;
    protected double k25_prime;
    protected double k2_double_prime;
    protected double k2_prime;
    protected double k2_triple_prime;
    protected double k3_double_prime;
    protected double k3_prime;
    protected double k4;
    protected double k4_prime;
    protected double k5_double_prime;
    protected double k5_prime;
    protected double k6;
    protected double k7;
    protected double k8;
    protected double k9;
    protected double kwee;
    protected double kwee_double_prime;
    protected double kwee_prime;
    protected double mu;
    protected double sigma;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[16];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = _cell_MPF_;
        y[3] = x[2];
        y[4] = x[3];
        y[5] = x[4];
        y[6] = x[5];
        y[7] = x[6];
        y[8] = x[7];
        y[9] = x[8];
        y[10] = TF;
        y[11] = Trimer;
        y[12] = flag_MPF;
        y[13] = k25;
        y[14] = kwee;
        y[15] = sigma;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[9];
        this.time = 0.0;
        x[0] = 0.0; // - $"cell.IEP"
        x[1] = 1.0; // - $"cell.M"
        x[2] = 0.2; // - $"cell.cdc13T"
        x[3] = 0.0; // - $"cell.preMPF"
        x[4] = 0.0; // - $"cell.rum1T"
        x[5] = 2.2; // - $"cell.slp1"
        x[6] = 0.0; // - $"cell.slp1T"
        x[7] = 1.0; // - $"cell.ste9"
        x[8] = 0.0; // - $cell.SK

        __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 'BIOMD0000000111.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_R13-rate_R14;
        result[1] = +rate_R19;
        result[2] = +rate_R1-rate_R2;
        result[3] = +rate_R3-rate_R4-rate_R5;
        result[4] = +rate_R15-rate_R16;
        result[5] = +rate_R10-rate_R11-rate_R12;
        result[6] = +rate_R8-rate_R9;
        result[7] = +rate_R6-rate_R7;
        result[8] = +rate_R17-rate_R18;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[9];
        __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] = (_cell_MPF_ <= 0.1 && flag_MPF == 1) ? +1 : -1; //event_0000001
        flags[1] = (_cell_MPF_ > 0.1) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041275150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041275150 == 0) //event_0000001
        {
            x[1] = x[1]/2;
            flag_MPF = 0;
        } else if ( __internalVar12060041275150 == 1) //event_0000002
        {
            flag_MPF = 1;
        }
    }

} // class ...