/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000121.xml diagram  at 2008.03.20 15:08:58
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000121 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        vk = R*Temp/F*Math.log(ko/ki);
        bb = 0.0029357*Math.exp(-0.02158*v);
        ai = 0.439*Math.exp(-0.02352*(v + 25))*4.5/ko;
        bi = 0.656*Math.exp(9.42E-4*v)*Math.pow((4.5/ko), 0.3);
        u = ai*bb*aa/(aa*bi);
        cell_ik = Gk*x[1]*(v - vk);
        Gk = 0.0135*Math.pow(ko, 0.59);
        a = 0.0555*Math.exp(0.05547153*(v - 12));
        b = 0.002357*Math.exp(-0.036588*v);
        aa = 0.0655*Math.exp(0.05547153*(v - 36));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0000001 = (a*x[4] - b*x[3])*cell;
        rate_reaction_0000002 = (ain*x[3] - bin*x[2])*cell;
        rate_reaction_0000003 = (x[2]*aa - bb*x[1])*cell;
        rate_reaction_0000004 = (x[1]*bi - ai*x[0])*cell;
        rate_reaction_0000005 = (aa*x[2] - u*x[0])*cell;
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        cell = 1.0; // initial value of $cell
        cell_ik = 0.1; // initial value of $cell.ik
        F = 96.485; // initial value of F
        R = 8.314; // initial value of R
        Temp = 310.0; // initial value of Temp
        ain = 2.172; // initial value of ain
        bin = 1.077; // initial value of bin
        ki = 140.0; // initial value of ki
        ko = 5.4; // initial value of ko
        v = -40.0; // initial value of v
        vhold = -40.0; // initial value of vhold
    }

    /*
     * 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 cell;
    protected double cell_ik;
    protected double F;
    protected double Gk;
    protected double R;
    protected double Temp;
    protected double a;
    protected double aa;
    protected double ai;
    protected double ain;
    protected double b;
    protected double bb;
    protected double bi;
    protected double bin;
    protected double ki;
    protected double ko;
    protected double time;
    protected double u;
    protected double v;
    protected double vhold;
    protected double vk;
    protected double vtest;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[16];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = x[4];
        y[5] = cell_ik;
        y[6] = Gk;
        y[7] = a;
        y[8] = aa;
        y[9] = ai;
        y[10] = b;
        y[11] = bb;
        y[12] = bi;
        y[13] = u;
        y[14] = v;
        y[15] = vk;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[5];
        this.time = 0.0;
        x[0] = 0.0; // - $"cell.i"
        x[1] = 0.06; // - $"cell.o"
        x[2] = 0.0; // - $cell.c1
        x[3] = 0.0; // - $cell.c2
        x[4] = 1.0; // - $cell.c3

        __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 'BIOMD0000000121.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_reaction_0000004+rate_reaction_0000005;
        result[1] = +rate_reaction_0000003-rate_reaction_0000004;
        result[2] = +rate_reaction_0000002-rate_reaction_0000003-rate_reaction_0000005;
        result[3] = +rate_reaction_0000001-rate_reaction_0000002;
        result[4] = -rate_reaction_0000001;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[5];
        __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[2];
        flags[0] = (time > 50 && time <= 300) ? +1 : -1; //event_0000001
        flags[1] = (time > 300) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041385150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041385150 == 0) //event_0000001
        {
            v = vtest;
        } else if ( __internalVar12060041385150 == 1) //event_0000002
        {
            v = vhold;
        }
    }

} // class ...