/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000120.xml diagram  at 2008.03.20 15:08:57
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000120 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        _compartment_lck_total_ = x[1] + x[0];
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_v1 = r_l;
        rate_v10 = d2*x[2];
        rate_v2 = n1*x[0]*x[2];
        rate_v3 = k1*x[1];
        rate_v4 = m1*Math.pow(x[0], n)*x[1];
        rate_v5 = d0*x[1];
        rate_v6 = d1*x[0];
        rate_v7 = k2*x[3];
        rate_v8 = m2*x[0]*x[3];
        rate_v9 = n2*x[2];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        d0 = 0.15; // initial value of d0
        d1 = 0.15; // initial value of d1
        k1 = 0.01; // initial value of k1
        k2 = 0.01; // initial value of k2
        m1 = 1.0; // initial value of m1
        m2 = 1.0; // initial value of m2
        n = 1.95; // initial value of n
        n1 = 1.0; // initial value of n1
        n2 = 0.02; // initial value of n2
    }

    /*
     * Model variables initial values
     */
    protected double _compartment_lck_total_;
    protected double rate_v1;
    protected double rate_v10;
    protected double rate_v2;
    protected double rate_v3;
    protected double rate_v4;
    protected double rate_v5;
    protected double rate_v6;
    protected double rate_v7;
    protected double rate_v8;
    protected double rate_v9;
    protected double d0;
    protected double d1;
    protected double d2;
    protected double k1;
    protected double k2;
    protected double m1;
    protected double m2;
    protected double n;
    protected double n1;
    protected double n2;
    protected double r_l;
    protected double time;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[6];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = _compartment_lck_total_;
        y[3] = x[2];
        y[4] = x[3];
        y[5] = r_l;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[4];
        this.time = 0.0;
        x[0] = 0.0; // - $"compartment.lck_active"
        x[1] = 0.0; // - $"compartment.lck_inactive"
        x[2] = 0.6; // - $"compartment.phosphatase_active"
        x[3] = 0.6; // - $"compartment.phosphatase_inactive"

        __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 'BIOMD0000000120.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_v2+rate_v3+rate_v4-rate_v6;
        result[1] = +rate_v1+rate_v2-rate_v3-rate_v4-rate_v5;
        result[2] = -rate_v10+rate_v7+rate_v8-rate_v9;
        result[3] = -rate_v7-rate_v8+rate_v9;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[4];
        __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 >= 10) ? +1 : -1; //event_0000001
        flags[1] = (time >= 24) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041375150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041375150 == 0) //event_0000001
        {
            r_l = 1;
        } else if ( __internalVar12060041375150 == 1) //event_0000002
        {
            r_l = 0;
        }
    }

} // class ...