/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000138.xml diagram  at 2008.03.20 15:09:16
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000138 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        phik = 1/(1 + Math.exp((vn - x[1])/sn));
        ica = gcal*phical*(x[1] - vca);
        cinf = Math.pow(x[0], 2)/(Math.pow(x[0], 2) + Math.pow(ks, 2));
        isk = gsk*cinf*(x[1] - vk);
        ibk = gf*phif*(x[1] - vk);
        ikdr = gk*x[3]*(x[1] - vk);
        ia = ga*phia*x[2]*(x[1] - vk);
        ik = isk + ibk + ikdr + ia;
        phia = 1/(1 + Math.exp((va - x[1])/sa));
        phih = 1/(1 + Math.exp((x[1] - vh)/sh));
        phif = 1/(1 + Math.exp((vf - x[1])/sf));
        phical = 1/(1 + Math.exp((vm - x[1])/sm));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0000004 = -ff*(alpha*ica + kc*x[0])*cell;
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        cell = 1.0; // initial value of $cell
        Cm = 10.0; // initial value of Cm
        alpha = 0.0015; // initial value of alpha
        ff = 0.01; // initial value of ff
        gcal = 2.0; // initial value of gcal
        gk = 4.0; // initial value of gk
        gsk = 1.7; // initial value of gsk
        kc = 0.16; // initial value of kc
        ks = 0.5; // initial value of ks
        lambda = 0.7; // initial value of lambda
        sa = 10.0; // initial value of sa
        sf = 5.6; // initial value of sf
        sh = 5.0; // initial value of sh
        sm = 12.0; // initial value of sm
        sn = 10.0; // initial value of sn
        tauh = 20.0; // initial value of tauh
        taun = 30.0; // initial value of taun
        va = -20.0; // initial value of va
        vca = 50.0; // initial value of vca
        vf = -20.0; // initial value of vf
        vh = -60.0; // initial value of vh
        vk = -75.0; // initial value of vk
        vm = -20.0; // initial value of vm
        vn = -5.0; // initial value of vn
    }

    /*
     * Model variables initial values
     */
    protected double rate_reaction_0000004;
    protected double cell;
    protected double Cm;
    protected double alpha;
    protected double cinf;
    protected double ff;
    protected double ga;
    protected double gcal;
    protected double gf;
    protected double gk;
    protected double gsk;
    protected double ia;
    protected double ibk;
    protected double ica;
    protected double ik;
    protected double ikdr;
    protected double isk;
    protected double kc;
    protected double ks;
    protected double lambda;
    protected double phia;
    protected double phical;
    protected double phif;
    protected double phih;
    protected double phik;
    protected double sa;
    protected double sf;
    protected double sh;
    protected double sm;
    protected double sn;
    protected double tauh;
    protected double taun;
    protected double va;
    protected double vca;
    protected double vf;
    protected double vh;
    protected double vk;
    protected double vm;
    protected double vn;

    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] = cinf;
        y[3] = x[2];
        y[4] = ia;
        y[5] = ibk;
        y[6] = ica;
        y[7] = ik;
        y[8] = ikdr;
        y[9] = isk;
        y[10] = x[3];
        y[11] = phia;
        y[12] = phical;
        y[13] = phif;
        y[14] = phih;
        y[15] = phik;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[4];
        this.time = 0.0;
        x[0] = 0.3; // - $"cell.c"
        x[1] = -60.0; // - V
        x[2] = 0.1; // - h
        x[3] = 0.1; // - n

        __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 'BIOMD0000000138.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_reaction_0000004;
        result[1] = -(ica + ik)/Cm;
        result[2] = +(phih - x[2])/tauh;
        result[3] = +lambda*(phik - x[3])/taun;
    }
    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 ...