/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000025.xml diagram  at 2008.03.20 15:04:02
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000025 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_rPer = rPer_V*delay(0, time - (rPer_parameter_0000008))*CELL;
        rate_rPerD = rPerD_D*(x[0]/CELL)*CELL;
        rate_rdClk = CELL*rdClk_V*delay(1, time - (rdClk_parameter_0000009));
        rate_rdClkD = rdClkD_D*(x[1]/CELL)*CELL;
        __internalVar12060038425150 = _CELL_dClkF_/CELL/(rPer_K + _CELL_dClkF_/CELL);
        __internalVar12060038425151 = rdClk_K/(rdClk_K + _CELL_dClkF_/CELL);
    }

    public void Init()
    {
        initialValues = new double[2];
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        CELL = 1.0E-15; // initial value of $CELL
        rPerD_D = 0.5; // initial value of rPerD_D
        rPer_K = 0.3; // initial value of rPer_K
        rPer_V = 0.5; // initial value of rPer_V
        rPer_parameter_0000008 = 10.0; // initial value of rPer_parameter_0000008
        rdClkD_D = 0.5; // initial value of rdClkD_D
        rdClk_K = 0.1; // initial value of rdClk_K
        rdClk_V = 0.25; // initial value of rdClk_V
        rdClk_parameter_0000009 = 10.0; // initial value of rdClk_parameter_0000009
    }

    /*
     * Model variables initial values
     */
    protected double _CELL_EmptySet_;
    protected double _CELL_dClkF_;
    protected double rate_rPer;
    protected double rate_rPerD;
    protected double rate_rdClk;
    protected double rate_rdClkD;
    protected double CELL;
    protected double __internalVar12060038425150;
    protected double __internalVar12060038425151;
    protected double rPerD_D;
    protected double rPer_K;
    protected double rPer_V;
    protected double rPer_parameter_0000008;
    protected double rdClkD_D;
    protected double rdClk_K;
    protected double rdClk_V;
    protected double rdClk_parameter_0000009;

/*
 * Returns "historical" variables in one array
 */
    public double[] getHistory(double t, double[] extendedY)
    {
        double[] history = new double[2];
        history[0] = extendedY[3];
        history[1] = extendedY[4];
        return history;
    }

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;


        double[] y = new double[5];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = _CELL_dClkF_;
        y[3] = __internalVar12060038425150;
        y[4] = __internalVar12060038425151;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[2];
        this.time = 0.0;
        x[0] = 5.0E-16; // - $"CELL.Per"
        x[1] = 1.0E-16; // - $"CELL.dClk"

        __internalRateVarInitFunc_0(time, x);

        return x;
    }

    public double[] getInitialValuesOfHistorycal()
    {
        double[] z = new double[2];
        __internalVar12060038425150 = 0.0;
        __internalVar12060038425151 = 1.0;
        z[0] = __internalVar12060038425150;
        z[1] = __internalVar12060038425151;
        return z;
    }
/*
 * code for algebraic rules calculations
 */

/*
 * end of code for algebraic rules calculations
 */

    protected void calculateRates(double time, double[] x)
    {

        __internalRateVarInitFunc_0(time, x);

    }

        /*
         * calculate dy/dt for 'BIOMD0000000025.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_rPer-rate_rPerD;
        result[1] = +rate_rdClk-rate_rdClkD;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[2];
        __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);

        double [] flags = new double[2];
        flags[0] = (x[1] - x[0] < 0.0) ? +1 : -1; //event_1
        flags[1] = (x[1] - x[0] >= 0.0) ? +1 : -1; //event_2
        return flags;
    }

    public void processEvent(int __internalVar12060038425152, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060038425152 == 0) //event_1
        {
            _CELL_dClkF_ = 0.0;
        } else if ( __internalVar12060038425152 == 1) //event_2
        {
            _CELL_dClkF_ = x[1] - x[0];
        }
    }

} // class ...