/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000119.xml diagram  at 2008.03.20 15:08:56
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000119 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        Minfs = GAMMAF(x[1], thetam, sigmam);
        INa = gNa*Math.pow(Minfs, pms)*x[4]*(x[1] - VNa);
        INaP = gNaP*Pinfs*(x[1] - VNa);
        IKdr = gKdr*Math.pow(x[5], pns)*(x[1] - VK);
        mKCa = ZFUNC(ac, x[0], pwrc);
        ICa = gCa*Math.pow(x[7], 2)*(x[1] - VCa);
        IKC = gKCa*mKCa*x[3]*(x[1] - VK);
        Pinfs = GAMMAF(x[1], thetap, sigmap);
        IAHP = gKAHP*x[6]*(x[1] - VK);
        Ainfs = GAMMAF(x[1], thetaa, sigmaa);
        IA = gA*Math.pow(Ainfs, 3)*x[2]*(x[1] - VK);
        Iz = gZ*x[8]*(x[1] - VK);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0000001 = compartment_0000001*(-uuCa*ICa - x[0])/tauCa;
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        compartment_0000001 = 1.0; // initial value of $compartment_0000001
        Cm = 1.0; // initial value of Cm
        Iapp = 1.0; // initial value of Iapp
        VCa = 120.0; // initial value of VCa
        VK = -90.0; // initial value of VK
        VL = -70.0; // initial value of VL
        VNa = 55.0; // initial value of VNa
        ac = 6.0; // initial value of ac
        aq = 2.0; // initial value of aq
        gA = 1.4; // initial value of gA
        gCa = 0.08; // initial value of gCa
        gKAHP = 5.0; // initial value of gKAHP
        gKCa = 10.0; // initial value of gKCa
        gKdr = 6.0; // initial value of gKdr
        gL = 0.05; // initial value of gL
        gNa = 35.0; // initial value of gNa
        gNaP = 0.2; // 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
        pwrc = 1.0; // initial value of pwrc
        pwrq = 4.0; // initial value of pwrq
        sigmaa = 20.0; // initial value of sigmaa
        sigmab = -6.0; // initial value of sigmab
        sigmac = 7.0; // initial value of sigmac
        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
        sigmar = 10.0; // initial value of sigmar
        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
        tauCa = 13.0; // initial value of tauCa
        tauKc = 2.0; // initial value of tauKc
        tauRs = 1.0; // initial value of tauRs
        tauZs = 75.0; // initial value of tauZs
        tauq = 450.0; // initial value of tauq
        thetaa = -50.0; // initial value of thetaa
        thetab = -80.0; // initial value of thetab
        thetac = -30.0; // initial value of thetac
        thetah = -45.0; // initial value of thetah
        thetam = -30.0; // initial value of thetam
        thetan = -35.0; // initial value of thetan
        thetap = -41.0; // initial value of thetap
        thetar = -20.0; // initial value of thetar
        thetaz = -39.0; // initial value of thetaz
        uuCa = 0.13; // initial value of uuCa
    }

    /*
     * Model variables initial values
     */
    protected double rate_reaction_0000001;
    protected double compartment_0000001;
    protected double Ainfs;
    protected double Cm;
    protected double IA;
    protected double IAHP;
    protected double ICa;
    protected double IKC;
    protected double IKdr;
    protected double INa;
    protected double INaP;
    protected double Iapp;
    protected double Iz;
    protected double Minfs;
    protected double Pinfs;
    protected double VCa;
    protected double VK;
    protected double VL;
    protected double VNa;
    protected double ac;
    protected double aq;
    protected double gA;
    protected double gCa;
    protected double gKAHP;
    protected double gKCa;
    protected double gKdr;
    protected double gL;
    protected double gNa;
    protected double gNaP;
    protected double gZ;
    protected double mKCa;
    protected double phi;
    protected double pms;
    protected double pns;
    protected double pwrc;
    protected double pwrq;
    protected double sigmaa;
    protected double sigmab;
    protected double sigmac;
    protected double sigmah;
    protected double sigmam;
    protected double sigman;
    protected double sigmap;
    protected double sigmar;
    protected double sigmaz;
    protected double t_tauh;
    protected double t_taun;
    protected double tauBs;
    protected double tauCa;
    protected double tauKc;
    protected double tauRs;
    protected double tauZs;
    protected double tauq;
    protected double thetaa;
    protected double thetab;
    protected double thetac;
    protected double thetah;
    protected double thetam;
    protected double thetan;
    protected double thetap;
    protected double thetar;
    protected double thetaz;
    protected double uuCa;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[21];
        y[0] = x[0];
        y[1] = Ainfs;
        y[2] = IA;
        y[3] = IAHP;
        y[4] = ICa;
        y[5] = IKC;
        y[6] = IKdr;
        y[7] = INa;
        y[8] = INaP;
        y[9] = Iz;
        y[10] = Minfs;
        y[11] = Pinfs;
        y[12] = x[1];
        y[13] = x[2];
        y[14] = x[3];
        y[15] = x[4];
        y[16] = mKCa;
        y[17] = x[5];
        y[18] = x[6];
        y[19] = x[7];
        y[20] = x[8];
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[9];
        this.time = 0.0;
        x[0] = 7.87E-4; // - $compartment_0000001.Ca
        x[1] = -71.962; // - V
        x[2] = 0.207565; // - bbs
        x[3] = 0.002486; // - ccs
        x[4] = 0.979199; // - hhs
        x[5] = 0.0242166; // - nns
        x[6] = 0.0; // - qqs
        x[7] = 0.005507; // - rrs
        x[8] = 0.0013689; // - zzs

        __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 'BIOMD0000000119.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_reaction_0000001;
        result[1] = +(-gL*(x[1] - VL) - INa - INaP - IKdr - IA - Iz - ICa - IKC - IAHP + Iapp)/Cm;
        result[2] = +(GAMMAF(x[1], thetab, sigmab) - x[2])/tauBs;
        result[3] = +(GAMMAF(x[1], thetac, sigmac) - x[3])/tauKc;
        result[4] = +phi*(GAMMAF(x[1], thetah, sigmah) - x[4])/(1 + 7.5*GAMMAF(x[1], t_tauh, -6.0));
        result[5] = +phi*(GAMMAF(x[1], thetan, sigman) - x[5])/(1.0 + 5.0*GAMMAF(x[1], t_taun, -15.0));
        result[6] = +(ZFUNC(aq, x[0], pwrq) - x[6])/tauq;
        result[7] = +(GAMMAF(x[1], thetar, sigmar) - x[7])/tauRs;
        result[8] = +(GAMMAF(x[1], thetaz, sigmaz) - x[8])/tauZs;
    }
    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 );
    }

    protected double GAMMAF(double VV, double theta, double sigma)
    {
         return 1.0/(1.0 + Math.exp(-(VV - theta)/sigma));
    }
    protected double ZFUNC(double AA, double CA, double zz)
    {
         return 1/(1 + Math.pow(AA, zz)/Math.pow(CA, zz));
    }
} // class ...