/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000077.xml diagram  at 2008.03.20 15:07:58
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000077 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        _compartment_0_species_8_ = 0.0010*parameter_1*x[6]*(0.3 + 0.3*parameter_2*time*Math.exp(1 - parameter_2*time))/(1 + 0.0010*parameter_1*x[6]);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0 = compartment_0*(reaction_0_k1*x[0]*x[2] - reaction_0_k2*x[1]);
        rate_reaction_1 = compartment_0*(reaction_1_k1*Math.pow(x[1], 2.0) - reaction_1_k2*x[3]);
        rate_reaction_2 = compartment_0*(reaction_2_k1*x[3]*x[5] - reaction_2_k2*x[4]);
        rate_reaction_3 = compartment_0*reaction_3_k1*x[6];
        rate_reaction_4 = compartment_0*reaction_4_k*x[4];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        compartment_0 = 1.0; // initial value of $compartment_0
        parameter_1 = 2.0; // initial value of parameter_1
        parameter_2 = 4.0; // initial value of parameter_2
        reaction_0_k1 = 2.5; // initial value of reaction_0_k1
        reaction_0_k2 = 5.0; // initial value of reaction_0_k2
        reaction_1_k1 = 2500.0; // initial value of reaction_1_k1
        reaction_1_k2 = 5.0; // initial value of reaction_1_k2
        reaction_2_k1 = 4000.0; // initial value of reaction_2_k1
        reaction_2_k2 = 200.0; // initial value of reaction_2_k2
        reaction_3_k1 = 10.0; // initial value of reaction_3_k1
        reaction_4_k = 2.0E7; // initial value of reaction_4_k
    }

    /*
     * Model variables initial values
     */
    protected double _compartment_0_species_8_;
    protected double rate_reaction_0;
    protected double rate_reaction_1;
    protected double rate_reaction_2;
    protected double rate_reaction_3;
    protected double rate_reaction_4;
    protected double compartment_0;
    protected double parameter_1;
    protected double parameter_2;
    protected double reaction_0_k1;
    protected double reaction_0_k2;
    protected double reaction_1_k1;
    protected double reaction_1_k2;
    protected double reaction_2_k1;
    protected double reaction_2_k2;
    protected double reaction_3_k1;
    protected double reaction_4_k;
    protected double time;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[8];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = x[4];
        y[5] = x[5];
        y[6] = x[6];
        y[7] = _compartment_0_species_8_;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[7];
        this.time = 0.0;
        x[0] = 1.0; // - $"compartment_0.species_0"
        x[1] = 0.0; // - $"compartment_0.species_1"
        x[2] = 0.01; // - $"compartment_0.species_2"
        x[3] = 0.0; // - $"compartment_0.species_3"
        x[4] = 0.0; // - $"compartment_0.species_4"
        x[5] = 0.1; // - $"compartment_0.species_5"
        x[6] = 0.0; // - $"compartment_0.species_6"

        __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 'BIOMD0000000077.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*2;
        result[2] = -rate_reaction_0;
        result[3] = +rate_reaction_1-rate_reaction_2;
        result[4] = +rate_reaction_2;
        result[5] = -rate_reaction_2;
        result[6] = -rate_reaction_3+rate_reaction_4;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[7];
        __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[1];
        flags[0] = (time > 5) ? +1 : -1; //RemovalH
        return flags;
    }

    public void processEvent(int __internalVar12060040785150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060040785150 == 0) //RemovalH
        {
            x[0] = 0;
        }
    }

} // class ...