/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000020.xml diagram  at 2008.03.20 15:03:57
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000020 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        E_Na = E_R + 115.0;
        alpha_h = 0.07*Math.exp(x[0]/20.0);
        beta_h = 1.0/(Math.exp(0.1*(x[0] + 30.0)) + 1.0);
        alpha_n = 0.01*(x[0] + 10.0)/(Math.exp(0.1*(x[0] + 10.0)) - 1.0);
        beta_n = 0.125*Math.exp(x[0]/80.0);
        i_Na = g_Na*Math.pow(x[2], 3.0)*x[1]*(x[0] - E_Na);
        i_K = g_K*Math.pow(x[3], 4.0)*(x[0] - E_K);
        i_L = g_L*(x[0] - E_L);
        E_K = E_R - 12.0;
        E_L = E_R + 10.613;
        alpha_m = 0.1*(x[0] + 25.0)/(Math.exp(0.1*(x[0] + 25.0)) - 1.0);
        beta_m = 4.0*Math.exp(x[0]/18.0);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        Cm = 1.0; // initial value of Cm
        E_R = -75.0; // initial value of E_R
        g_K = 36.0; // initial value of g_K
        g_L = 0.3; // initial value of g_L
        g_Na = 120.0; // initial value of g_Na
    }

    /*
     * Model variables initial values
     */
    protected double Cm;
    protected double E_K;
    protected double E_L;
    protected double E_Na;
    protected double E_R;
    protected double I;
    protected double alpha_h;
    protected double alpha_m;
    protected double alpha_n;
    protected double beta_h;
    protected double beta_m;
    protected double beta_n;
    protected double g_K;
    protected double g_L;
    protected double g_Na;
    protected double i_K;
    protected double i_L;
    protected double i_Na;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[16];
        y[0] = E_K;
        y[1] = E_L;
        y[2] = E_Na;
        y[3] = x[0];
        y[4] = alpha_h;
        y[5] = alpha_m;
        y[6] = alpha_n;
        y[7] = beta_h;
        y[8] = beta_m;
        y[9] = beta_n;
        y[10] = x[1];
        y[11] = i_K;
        y[12] = i_L;
        y[13] = i_Na;
        y[14] = x[2];
        y[15] = x[3];
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[4];
        this.time = 0.0;
        x[0] = -75.0; // - V
        x[1] = 0.6; // - h
        x[2] = 0.05; // - m
        x[3] = 0.325; // - n

        __internalVarInitFunc_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);

    }

        /*
         * calculate dy/dt for 'BIOMD0000000020.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +(I - (i_Na + i_K + i_L))/Cm;
        result[1] = +alpha_h*(1.0 - x[1]) - beta_h*x[1];
        result[2] = +alpha_m*(1.0 - x[2]) - beta_m*x[2];
        result[3] = +alpha_n*(1.0 - x[3]) - beta_n*x[3];
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[4];
        __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 );
    }

} // class ...