/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000104.xml diagram  at 2008.03.20 15:08:40
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000104 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_reaction_0 = compartment_0*x[0]*_compartment_0_species_2_*reaction_0_k1;
        rate_reaction_1 = compartment_0*reaction_1_k2*x[1]*_compartment_0_species_3_;
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _compartment_0_species_2_ = 1.0; // initial value of $"compartment_0.species_2"
        _compartment_0_species_5_ = 1.0; // initial value of $"compartment_0.species_5"
        compartment_0 = 1.0; // initial value of $compartment_0
        reaction_0_k1 = 1.0; // initial value of reaction_0_k1
        reaction_1_k2 = 1.0; // initial value of reaction_1_k2
    }

    /*
     * Model variables initial values
     */
    protected double _compartment_0_species_2_;
    protected double _compartment_0_species_3_;
    protected double _compartment_0_species_5_;
    protected double rate_reaction_0;
    protected double rate_reaction_1;
    protected double compartment_0;
    protected double reaction_0_k1;
    protected double reaction_1_k2;
    protected double time;

    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] = _compartment_0_species_2_;
        y[3] = _compartment_0_species_3_;
        y[4] = x[2];
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[3];
        this.time = 0.0;
        x[0] = 1.0; // - $"compartment_0.species_0"
        x[1] = 0.0; // - $"compartment_0.species_1"
        x[2] = 0.0; // - $"compartment_0.species_4"

        __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)
    {

        __internalRateVarInitFunc_0(time, x);

    }

        /*
         * calculate dy/dt for 'BIOMD0000000104.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_reaction_0;
        result[1] = +rate_reaction_0-rate_reaction_1;
        result[2] = +rate_reaction_1;
    }
    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;

        __internalRateVarInitFunc_0(time, x);

        double [] flags = new double[1];
        flags[0] = (time > 1) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041205150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041205150 == 0) //event_0000002
        {
            _compartment_0_species_2_ = 0.4;
            _compartment_0_species_3_ = _compartment_0_species_5_ - _compartment_0_species_2_;
        }
    }

} // class ...