/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000124.xml diagram  at 2008.03.20 15:09:01
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000124 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        alphaIRn = 0.09/(1 + Math.exp(0.11*(x[2] + 100)));
        betaIRr = 0.15/(1 + Math.exp(-0.05*(x[2] + 120)));
        rIRinf = 1/(1 + betaIRr/alphaIRr);
        tauIRr = 1/(alphaIRr + betaIRr);
        minf = 1/(1 + Math.exp((vm - x[2])/sm));
        ik = gk*x[4]*(x[2] - vk);
        w = Math.pow(x[0], 5)/(Math.pow(x[0], 5) + Math.pow(kd, 5));
        ikatp = gkatp*x[3]*(x[2] - vk);
        ica = gca*minf*(x[2] - vca);
        ninf = 1/(1 + Math.exp((vn - x[2])/sn));
        ainf = 1.0/(1.0 + Math.exp((r - x[0])/sa));
        betaIRn = 3.5E-4*Math.exp(0.07*(x[2] + 25));
        ikca = gkca*w*(x[2] - vk);
        jmem = -(alpha*ica + kpmca*x[0]);
        jserca = kserca*x[0];
        jleak = pleak*(x[1] - x[0]);
        oinf = x[0]/(dact + x[0])*(ip3/(dip3 + ip3))*(dinact/(dinact + x[0]));
        jip3 = oinf*(x[1] - x[0]);
        jer = jleak + jip3 - jserca;
        iir = gir*x[5]*x[6]*(x[2] - vir);
        nIRinf = 1/(1 + betaIRn/alphaIRn);
        tauIRn = 1/(alphaIRn + betaIRn);
        alphaIRr = 30/(1 + Math.exp(0.04*(x[2] + 230)));
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_reaction_0000005 = cell*fcyt*(jmem + jer);
        rate_reaction_0000006 = -fer*vcytver*jer*cell;
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        cell = 1.0; // initial value of $cell
        alpha = 4.5E-6; // initial value of alpha
        cm = 5300.0; // initial value of cm
        dact = 0.35; // initial value of dact
        dinact = 0.4; // initial value of dinact
        dip3 = 0.5; // initial value of dip3
        fcyt = 0.01; // initial value of fcyt
        fer = 0.01; // initial value of fer
        gca = 1000.0; // initial value of gca
        gir = 5.0; // initial value of gir
        gk = 1400.0; // initial value of gk
        gkatp = 500.0; // initial value of gkatp
        gkca = 900.0; // initial value of gkca
        kd = 0.3; // initial value of kd
        kpmca = 0.2; // initial value of kpmca
        kserca = 0.4; // initial value of kserca
        pleak = 5.0E-4; // initial value of pleak
        r = 0.14; // initial value of r
        sa = 0.1; // initial value of sa
        sm = 12.0; // initial value of sm
        sn = 5.0; // initial value of sn
        taua = 300000.0; // initial value of taua
        taun = 16.0; // initial value of taun
        vca = 50.0; // initial value of vca
        vcytver = 5.0; // initial value of vcytver
        vir = -75.0; // initial value of vir
        vk = -75.0; // initial value of vk
        vm = -20.0; // initial value of vm
        vn = -16.0; // initial value of vn
    }

    /*
     * Model variables initial values
     */
    protected double rate_reaction_0000005;
    protected double rate_reaction_0000006;
    protected double cell;
    protected double ainf;
    protected double alpha;
    protected double alphaIRn;
    protected double alphaIRr;
    protected double betaIRn;
    protected double betaIRr;
    protected double cm;
    protected double dact;
    protected double dinact;
    protected double dip3;
    protected double fcyt;
    protected double fer;
    protected double gca;
    protected double gir;
    protected double gk;
    protected double gkatp;
    protected double gkca;
    protected double ica;
    protected double iir;
    protected double ik;
    protected double ikatp;
    protected double ikca;
    protected double ip3;
    protected double jer;
    protected double jip3;
    protected double jleak;
    protected double jmem;
    protected double jserca;
    protected double kd;
    protected double kpmca;
    protected double kserca;
    protected double minf;
    protected double nIRinf;
    protected double ninf;
    protected double oinf;
    protected double pleak;
    protected double r;
    protected double rIRinf;
    protected double sa;
    protected double sm;
    protected double sn;
    protected double tauIRn;
    protected double tauIRr;
    protected double taua;
    protected double taun;
    protected double time;
    protected double vca;
    protected double vcytver;
    protected double vir;
    protected double vk;
    protected double vm;
    protected double vn;
    protected double w;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[31];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = ainf;
        y[5] = alphaIRn;
        y[6] = alphaIRr;
        y[7] = betaIRn;
        y[8] = betaIRr;
        y[9] = gkatp;
        y[10] = ica;
        y[11] = iir;
        y[12] = ik;
        y[13] = ikatp;
        y[14] = ikca;
        y[15] = jer;
        y[16] = jip3;
        y[17] = jleak;
        y[18] = jmem;
        y[19] = jserca;
        y[20] = minf;
        y[21] = x[4];
        y[22] = x[5];
        y[23] = nIRinf;
        y[24] = ninf;
        y[25] = oinf;
        y[26] = x[6];
        y[27] = rIRinf;
        y[28] = tauIRn;
        y[29] = tauIRr;
        y[30] = w;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[7];
        this.time = 0.0;
        x[0] = 0.1; // - $"cell.c"
        x[1] = 100.0; // - $"cell.cer"
        x[2] = -60.0; // - V
        x[3] = 0.46; // - a
        x[4] = 0.01; // - n
        x[5] = 0.0080; // - nIR
        x[6] = 0.282; // - rIR

        __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 'BIOMD0000000124.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_reaction_0000005;
        result[1] = +rate_reaction_0000006;
        result[2] = -(ica + ik + ikca + ikatp + iir)/cm;
        result[3] = +(ainf - x[3])/taua;
        result[4] = +(ninf - x[4])/taun;
        result[5] = +(nIRinf - x[5])/tauIRn;
        result[6] = +(rIRinf - x[6])/tauIRr;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[7];
        __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[1];
        flags[0] = (time > 30000.0) ? +1 : -1; //event_0000001
        return flags;
    }

    public void processEvent(int __internalVar12060041415150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060041415150 == 0) //event_0000001
        {
            gkatp = 530;
        }
    }

} // class ...