/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000117.xml diagram  at 2008.03.20 15:08:54
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000117 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        beta = flag*0.96*Math.exp(-0.2*(time - tstim));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0000001 = v1*beta*Cytosol;
        rate_reaction_0000002 = v0*Cytosol;
        rate_reaction_0000003 = kf*x[1]*Cytosol;
        rate_reaction_0000004 = intracellular_Ca_storepool*VM2*Math.pow(x[0], n)/(Math.pow(K2, n) + Math.pow(x[0], n));
        rate_reaction_0000005 = Cytosol*VM3*(Math.pow(x[1], m)/(Math.pow(KR, m) + Math.pow(x[1], m)))*(Math.pow(x[0], p)/(Math.pow(KA, p) + Math.pow(x[0], p)));
        rate_reaction_0000006 = k*x[0]*extracellular;
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        Cytosol = 1.0; // initial value of $Cytosol
        extracellular = 1.0; // initial value of $extracellular
        intracellular_Ca_storepool = 1.0; // initial value of $intracellular_Ca_storepool
        K2 = 1.0; // initial value of K2
        KA = 0.9; // initial value of KA
        KR = 2.0; // initial value of KR
        VM2 = 65.0; // initial value of VM2
        VM3 = 500.0; // initial value of VM3
        k = 10.0; // initial value of k
        kf = 1.0; // initial value of kf
        m = 2.0; // initial value of m
        n = 2.0; // initial value of n
        p = 4.0; // initial value of p
        tstim = 4.0; // initial value of tstim
        v0 = 1.0; // initial value of v0
        v1 = 7.3; // initial value of v1
    }

    /*
     * Model variables initial values
     */
    protected double rate_reaction_0000001;
    protected double rate_reaction_0000002;
    protected double rate_reaction_0000003;
    protected double rate_reaction_0000004;
    protected double rate_reaction_0000005;
    protected double rate_reaction_0000006;
    protected double Cytosol;
    protected double extracellular;
    protected double intracellular_Ca_storepool;
    protected double K2;
    protected double KA;
    protected double KR;
    protected double VM2;
    protected double VM3;
    protected double beta;
    protected double flag;
    protected double k;
    protected double kf;
    protected double m;
    protected double n;
    protected double p;
    protected double time;
    protected double tstim;
    protected double v0;
    protected double v1;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[4];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = beta;
        y[3] = flag;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[2];
        this.time = 0.0;
        x[0] = 0.1; // - $"Cytosol.z"
        x[1] = 1.4; // - $"intracellular_Ca_storepool.y"

        __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 'BIOMD0000000117.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_reaction_0000001+rate_reaction_0000002+rate_reaction_0000003-rate_reaction_0000004+rate_reaction_0000005-rate_reaction_0000006;
        result[1] = -rate_reaction_0000003+rate_reaction_0000004-rate_reaction_0000005;
    }
    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;

        __internalVarInitFunc_0(time, x);
        __internalRateVarInitFunc_0(time, x);

        double [] flags = new double[1];
        flags[0] = (time > tstim) ? +1 : -1; //event_0000001
        return flags;
    }

    public void processEvent(int __internalVar12060041345150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041345150 == 0) //event_0000001
        {
            flag = 1;
        }
    }

} // class ...