/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000126.xml diagram  at 2008.03.20 15:09:03
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000126 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        a11 = 3.802/(0.1027*Math.exp(-v/17.0) + 0.2*Math.exp(-v/150));
        b3 = 0.0084 + 2.0E-5*v;
        a4 = a2/100;
        b4 = a3;
        a5 = a2/95000.0;
        b5 = a3/50;
        Ena = Rk*Temp/Fara*Math.log(nao/nai);
        Ina = Gna*(x[4]/(x[1] + x[0] + x[8] + x[2] + x[3] + x[7] + x[6] + x[5] + x[4]))*(v - Ena)/scale;
        b11 = 0.1917*Math.exp(-v/20.3);
        b12 = 0.2*Math.exp(-(v - 5)/20.3);
        b13 = 0.22*Math.exp(-(v - 10)/20.3);
        a12 = 3.802/(0.1027*Math.exp(-v/15) + 0.23*Math.exp(-v/150));
        b2 = a13*a2*a3/(b13*b3);
        a13 = 3.802/(0.1027*Math.exp(-v/12) + 0.25*Math.exp(-v/150));
        a2 = 9.178*Math.exp(v/29.68);
        a3 = 3.7933E-7*Math.exp(-v/7.7);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0000001 = cell*(x[1]*a11 - x[0]*b11);
        rate_reaction_0000002 = cell*(x[0]*a12 - x[8]*b12);
        rate_reaction_0000003 = cell*(x[8]*a4 - x[2]*b4);
        rate_reaction_0000004 = cell*(x[2]*a5 - x[3]*b5);
        rate_reaction_0000005 = cell*(x[7]*b3 - x[1]*a3);
        rate_reaction_0000006 = cell*(x[6]*b11 - x[7]*a11);
        rate_reaction_0000007 = cell*(x[6]*b3 - x[0]*a3);
        rate_reaction_0000008 = cell*(x[5]*b12 - x[6]*a12);
        rate_reaction_0000009 = cell*(x[5]*b3 - x[8]*a3);
        rate_reaction_0000010 = cell*(x[8]*b2 - a2*x[4]);
        rate_reaction_0000011 = cell*(-x[5]*a13 + x[4]*b13);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        cell = 1.0; // initial value of $cell
        Fara = 96485.0; // initial value of Fara
        Gna = 23.5; // initial value of Gna
        Rk = 8314.0; // initial value of Rk
        Temp = 310.0; // initial value of Temp
        nai = 15.0; // initial value of nai
        nao = 140.0; // initial value of nao
        scale = 1000.0; // initial value of scale
        v = -80.0; // initial value of v
    }

    /*
     * 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 rate_reaction_0000007;
    protected double rate_reaction_0000008;
    protected double rate_reaction_0000009;
    protected double rate_reaction_0000010;
    protected double rate_reaction_0000011;
    protected double cell;
    protected double Ena;
    protected double Fara;
    protected double Gna;
    protected double Ina;
    protected double Rk;
    protected double Temp;
    protected double a11;
    protected double a12;
    protected double a13;
    protected double a2;
    protected double a3;
    protected double a4;
    protected double a5;
    protected double b11;
    protected double b12;
    protected double b13;
    protected double b2;
    protected double b3;
    protected double b4;
    protected double b5;
    protected double nai;
    protected double nao;
    protected double scale;
    protected double time;
    protected double v;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[26];
        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] = x[7];
        y[8] = x[8];
        y[9] = Ena;
        y[10] = Ina;
        y[11] = a11;
        y[12] = a12;
        y[13] = a13;
        y[14] = a2;
        y[15] = a3;
        y[16] = a4;
        y[17] = a5;
        y[18] = b11;
        y[19] = b12;
        y[20] = b13;
        y[21] = b2;
        y[22] = b3;
        y[23] = b4;
        y[24] = b5;
        y[25] = v;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[9];
        this.time = 0.0;
        x[0] = 0.0; // - $"cell.IC2"
        x[1] = 0.0; // - $"cell.IC3"
        x[2] = 0.0; // - $"cell.IM1"
        x[3] = 0.0; // - $"cell.IM2"
        x[4] = 0.0; // - $"cell.O"
        x[5] = 0.0; // - $cell.C1
        x[6] = 0.0; // - $cell.C2
        x[7] = 1.0; // - $cell.C3
        x[8] = 0.0; // - $cell.IF

        __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 'BIOMD0000000126.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_reaction_0000001-rate_reaction_0000002+rate_reaction_0000007;
        result[1] = -rate_reaction_0000001+rate_reaction_0000005;
        result[2] = +rate_reaction_0000003-rate_reaction_0000004;
        result[3] = +rate_reaction_0000004;
        result[4] = +rate_reaction_0000010-rate_reaction_0000011;
        result[5] = -rate_reaction_0000008-rate_reaction_0000009+rate_reaction_0000011;
        result[6] = -rate_reaction_0000006-rate_reaction_0000007+rate_reaction_0000008;
        result[7] = -rate_reaction_0000005+rate_reaction_0000006;
        result[8] = +rate_reaction_0000002-rate_reaction_0000003+rate_reaction_0000009-rate_reaction_0000010;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[9];
        __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 >= 5 && time <= 20) ? +1 : -1; //event_0000001
        flags[1] = (time > 20) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060041435150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041435150 == 0) //event_0000001
        {
            v = -30.0;
        } else if ( __internalVar12060041435150 == 1) //event_0000002
        {
            v = -80.0;
        }
    }

} // class ...