/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000148.xml diagram  at 2008.03.20 15:09:37
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000148 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        gamma = g12*g21 - (1 - g11)*(1 - g22);
        _compartment_x1_bar_ = Math.pow((beta1/alpha1), ((1 - g22)/gamma))*Math.pow((beta2/alpha2), (g21/gamma));
        _compartment_x2_bar_ = Math.pow((beta1/alpha1), (g12/gamma))*Math.pow((beta2/alpha2), ((1 - g11)/gamma));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_R1 = alpha1*Math.pow(x[1], g11)*Math.pow(x[2], g21);
        rate_R2 = beta1*x[1];
        rate_R3 = alpha2*Math.pow(x[1], g12)*Math.pow(x[2], g22);
        rate_R4 = beta2*x[2];
        rate_R5 = flag_resorption*k1*(x[1] - _compartment_x1_bar_);
        rate_R6 = flag_formation*k2*(x[2] - _compartment_x2_bar_);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        alpha1 = 3.0; // initial value of alpha1
        alpha2 = 4.0; // initial value of alpha2
        beta1 = 0.2; // initial value of beta1
        beta2 = 0.02; // initial value of beta2
        g11 = 0.5; // initial value of g11
        g12 = 1.0; // initial value of g12
        g21 = -0.5; // initial value of g21
        k1 = 0.24; // initial value of k1
        k2 = 0.0017; // initial value of k2
    }

    /*
     * Model variables initial values
     */
    protected double _compartment_x1_bar_;
    protected double _compartment_x2_bar_;
    protected double rate_R1;
    protected double rate_R2;
    protected double rate_R3;
    protected double rate_R4;
    protected double rate_R5;
    protected double rate_R6;
    protected double alpha1;
    protected double alpha2;
    protected double beta1;
    protected double beta2;
    protected double flag_formation;
    protected double flag_resorption;
    protected double g11;
    protected double g12;
    protected double g21;
    protected double g22;
    protected double gamma;
    protected double k1;
    protected double k2;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[8];
        y[0] = _compartment_x1_bar_;
        y[1] = _compartment_x2_bar_;
        y[2] = x[0];
        y[3] = x[1];
        y[4] = x[2];
        y[5] = flag_formation;
        y[6] = flag_resorption;
        y[7] = gamma;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[3];
        this.time = 0.0;
        x[0] = 100.0; // - $"compartment.z"
        x[1] = 11.0; // - $compartment.x1
        x[2] = 212.0; // - $compartment.x2

        __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 'BIOMD0000000148.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_R5+rate_R6;
        result[1] = +rate_R1-rate_R2;
        result[2] = +rate_R3-rate_R4;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[3];
        __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[4];
        flags[0] = (x[1] > _compartment_x1_bar_) ? +1 : -1; //event_0000001
        flags[1] = (x[2] > _compartment_x2_bar_) ? +1 : -1; //event_0000002
        flags[2] = (x[1] <= _compartment_x1_bar_) ? +1 : -1; //event_0000003
        flags[3] = (x[2] <= _compartment_x2_bar_) ? +1 : -1; //event_0000004
        return flags;
    }

    public void processEvent(int __internalVar12060041776250, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041776250 == 0) //event_0000001
        {
            flag_resorption = 1;
        } else if ( __internalVar12060041776250 == 1) //event_0000002
        {
            flag_formation = 1;
        } else if ( __internalVar12060041776250 == 2) //event_0000003
        {
            flag_resorption = 0;
        } else if ( __internalVar12060041776250 == 3) //event_0000004
        {
            flag_formation = 0;
        }
    }

} // class ...