/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000118.xml diagram  at 2008.03.20 15:08:55
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000118 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        Minfs = GAMMAF(x[0], thetam, sigmam);
        INa = gNa*Math.pow(Minfs, pms)*x[2]*(x[0] - VNa);
        INaP = gNaP*Pinfs*(x[0] - VNa);
        IKdr = gKdr*Math.pow(x[3], pns)*(x[0] - VK);
        Pinfs = GAMMAF(x[0], thetap, sigmap);
        Ainfs = GAMMAF(x[0], thetaa, sigmaa);
        IA = gA*Math.pow(Ainfs, 3)*x[1]*(x[0] - VK);
        Iz = gZ*x[4]*(x[0] - VK);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        Cm = 1.0; // initial value of Cm
        Iapp = 0.661914; // initial value of Iapp
        VK = -90.0; // initial value of VK
        VL = -70.0; // initial value of VL
        VNa = 55.0; // initial value of VNa
        gA = 1.4; // initial value of gA
        gKdr = 6.0; // initial value of gKdr
        gL = 0.05; // initial value of gL
        gNa = 35.0; // initial value of gNa
        gNaP = 0.3; // initial value of gNaP
        gZ = 1.0; // initial value of gZ
        phi = 10.0; // initial value of phi
        pms = 3.0; // initial value of pms
        pns = 4.0; // initial value of pns
        sigmaa = 20.0; // initial value of sigmaa
        sigmab = -6.0; // initial value of sigmab
        sigmah = -7.0; // initial value of sigmah
        sigmam = 9.5; // initial value of sigmam
        sigman = 10.0; // initial value of sigman
        sigmap = 3.0; // initial value of sigmap
        sigmaz = 5.0; // initial value of sigmaz
        t_tauh = -40.5; // initial value of t_tauh
        t_taun = -27.0; // initial value of t_taun
        tauBs = 15.0; // initial value of tauBs
        tauZs = 75.0; // initial value of tauZs
        thetaa = -50.0; // initial value of thetaa
        thetab = -80.0; // initial value of thetab
        thetah = -45.0; // initial value of thetah
        thetam = -30.0; // initial value of thetam
        thetan = -35.0; // initial value of thetan
        thetap = -47.0; // initial value of thetap
        thetaz = -39.0; // initial value of thetaz
    }

    /*
     * Model variables initial values
     */
    protected double Ainfs;
    protected double Cm;
    protected double IA;
    protected double IKdr;
    protected double INa;
    protected double INaP;
    protected double Iapp;
    protected double Iz;
    protected double Minfs;
    protected double Pinfs;
    protected double VK;
    protected double VL;
    protected double VNa;
    protected double gA;
    protected double gKdr;
    protected double gL;
    protected double gNa;
    protected double gNaP;
    protected double gZ;
    protected double phi;
    protected double pms;
    protected double pns;
    protected double sigmaa;
    protected double sigmab;
    protected double sigmah;
    protected double sigmam;
    protected double sigman;
    protected double sigmap;
    protected double sigmaz;
    protected double t_tauh;
    protected double t_taun;
    protected double tauBs;
    protected double tauZs;
    protected double thetaa;
    protected double thetab;
    protected double thetah;
    protected double thetam;
    protected double thetan;
    protected double thetap;
    protected double thetaz;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[13];
        y[0] = Ainfs;
        y[1] = IA;
        y[2] = IKdr;
        y[3] = INa;
        y[4] = INaP;
        y[5] = Iz;
        y[6] = Minfs;
        y[7] = Pinfs;
        y[8] = x[0];
        y[9] = x[1];
        y[10] = x[2];
        y[11] = x[3];
        y[12] = x[4];
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[5];
        this.time = 0.0;
        x[0] = -71.81327; // - V
        x[1] = 0.203517; // - bbs
        x[2] = 0.98786; // - hhs
        x[3] = 0.02457; // - nns
        x[4] = 0.00141; // - zzs

        __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 'BIOMD0000000118.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +(-gL*(x[0] - VL) - INa - INaP - IKdr - IA - Iz + Iapp)/Cm;
        result[1] = +(GAMMAF(x[0], thetab, sigmab) - x[1])/tauBs;
        result[2] = +phi*(GAMMAF(x[0], thetah, sigmah) - x[2])/(1 + 7.5*GAMMAF(x[0], t_tauh, -6.0));
        result[3] = +phi*(GAMMAF(x[0], thetan, sigman) - x[3])/(1.0 + 5.0*GAMMAF(x[0], t_taun, -15.0));
        result[4] = +(GAMMAF(x[0], thetaz, sigmaz) - x[4])/tauZs;
    }
    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 );
    }

    protected double GAMMAF(double VV, double theta, double sigma)
    {
         return 1.0/(1.0 + Math.exp(-(VV - theta)/sigma));
    }
} // class ...