/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000081.xml diagram  at 2008.03.20 15:08:02
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000081 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        MgSat20 = 1.0/(1.0 + 20.0*(1.0/_EX_M_Cytoplasm_Mg2_C_));
        Gactive = x[10] + x[12] + x[6];
        ATPSat300 = 1.0/(1.0 + 300.0*(1.0/(0.5 + x[0])));
        I_KCNQ = 1.33*Math.pow((0.025*x[17]), nHill);
        _EX_M_Cytoplasm_GTP_C_ = 100.0 + (-(50.0*Math.exp(-(0.00833333333333333*(290.0 + time)))));
        ATPSat1000 = 1.0/(1.0 + 1000.0*(1.0/(0.5 + x[0])));
        MgSat10 = 0.2 + 0.8*(1/(1 + KMgGTPase*(1/_EX_M_Cytoplasm_Mg2_C_)));
        OxoSat = 1/(1 + Koxom*(1/(1.0E-5 + _EX_oxoM_EX_)));
        fGactive = (x[10] + x[12] + x[6])/200;
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_AlF4on = AlF4on_kAlF4on*x[1]*x[8] + (-(AlF4on_kAlF4off*x[7]));
        rate_GDPbSon = GDPbSon_kGDPbSon*x[14]*x[3] + (-(GDPbSon_kGGDPbSoff*(1.0 + 20.0*OxoSat)*x[9]));
        rate_GDPdissoc = GDPdissoc_kGDPoff*x[8]*(OxoSat + GDPdissoc_TonicAct) + (-(GDPdissoc_kGDPon*x[2]*x[14]));
        rate_GGTPase = GGTPase_kGGTPase*MgSat10*x[10];
        rate_GTPgSon = GTPgSon_kGTPgSon*x[14]*x[4] + (-(GTPgSon_kGTPgSoff*x[13]));
        rate_GTPon = GTPon_kGTPon*x[14]*_EX_M_Cytoplasm_GTP_C_ + (-(GTPon_kGTPoff*x[11]));
        rate_IP3Phosphatase = x[5]*IP3Phosphatase_kIP3ase;
        rate_MgGTPon = MgGTPon_kMg2onGTP*x[11]*_EX_M_Cytoplasm_Mg2_C_;
        rate_MgonGDPAlF4 = MgonGDPAlF4_kMgonAlF4*x[7]*_EX_M_Cytoplasm_Mg2_C_;
        rate_MgonGGTPgS = MgonGGTPgS_kMg2onGTPgS*x[13]*_EX_M_Cytoplasm_Mg2_C_;
        rate_PI4Kinase = (0.2 + 0.8*MgSat20)*PI4Kinase_kPI4Kinase*x[19]*ATPSat1000;
        rate_PIP2_5Pase = MgSat20*PIP2_5Pase_kPIP2Pase*x[16];
        rate_PIP2binding = PIP2binding_kPIP2on*x[15]*x[16] + (-(PIP2binding_kPIP2off*x[17]));
        rate_PIP5kinase = (0.2 + 0.8*MgSat20)*PIP5kinase_kPIP5Kinase*x[18]*ATPSat300;
        rate_PLC = PLC_kPLC*(fGactive + PLC_PLCspont)*x[16];
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _EX_M_Cytoplasm_Mg2_C_ = 2100.0; // initial value of $"EX.M.Cytoplasm.Mg2_C"
        AlF4on_kAlF4off = 0.0050; // initial value of AlF4on_kAlF4off
        AlF4on_kAlF4on = 7.0E-6; // initial value of AlF4on_kAlF4on
        GDPbSon_kGDPbSon = 0.28; // initial value of GDPbSon_kGDPbSon
        GDPbSon_kGGDPbSoff = 0.1; // initial value of GDPbSon_kGGDPbSoff
        GDPdissoc_TonicAct = 0.0020; // initial value of GDPdissoc_TonicAct
        GDPdissoc_kGDPoff = 0.5; // initial value of GDPdissoc_kGDPoff
        GDPdissoc_kGDPon = 0.0030; // initial value of GDPdissoc_kGDPon
        GGTPase_kGGTPase = 1.8; // initial value of GGTPase_kGGTPase
        GTPgSon_kGTPgSoff = 0.0050; // initial value of GTPgSon_kGTPgSoff
        GTPgSon_kGTPgSon = 0.0060; // initial value of GTPgSon_kGTPgSon
        GTPon_kGTPoff = 0.08; // initial value of GTPon_kGTPoff
        GTPon_kGTPon = 0.45; // initial value of GTPon_kGTPon
        IP3Phosphatase_kIP3ase = 0.3; // initial value of IP3Phosphatase_kIP3ase
        KMgGTPase = 10.0; // initial value of KMgGTPase
        Koxom = 8.0; // initial value of Koxom
        MgGTPon_kMg2onGTP = 0.0030; // initial value of MgGTPon_kMg2onGTP
        MgonGDPAlF4_kMgonAlF4 = 0.0020; // initial value of MgonGDPAlF4_kMgonAlF4
        MgonGGTPgS_kMg2onGTPgS = 0.0020; // initial value of MgonGGTPgS_kMg2onGTPgS
        PI4Kinase_kPI4Kinase = 1.0E-4; // initial value of PI4Kinase_kPI4Kinase
        PIP2_5Pase_kPIP2Pase = 0.0050; // initial value of PIP2_5Pase_kPIP2Pase
        PIP2binding_kPIP2off = 0.25; // initial value of PIP2binding_kPIP2off
        PIP2binding_kPIP2on = 2.5E-4; // initial value of PIP2binding_kPIP2on
        PIP5kinase_kPIP5Kinase = 0.06; // initial value of PIP5kinase_kPIP5Kinase
        PLC_PLCspont = 7.5E-4; // initial value of PLC_PLCspont
        PLC_kPLC = 4.8; // initial value of PLC_kPLC
        nHill = 1.8; // initial value of nHill
    }

    /*
     * Model variables initial values
     */
    protected double _EX_M_Cytoplasm_GTP_C_;
    protected double _EX_M_Cytoplasm_Mg2_C_;
    protected double _EX_oxoM_EX_;
    protected double rate_AlF4on;
    protected double rate_GDPbSon;
    protected double rate_GDPdissoc;
    protected double rate_GGTPase;
    protected double rate_GTPgSon;
    protected double rate_GTPon;
    protected double rate_IP3Phosphatase;
    protected double rate_MgGTPon;
    protected double rate_MgonGDPAlF4;
    protected double rate_MgonGGTPgS;
    protected double rate_PI4Kinase;
    protected double rate_PIP2_5Pase;
    protected double rate_PIP2binding;
    protected double rate_PIP5kinase;
    protected double rate_PLC;
    protected double ATPSat1000;
    protected double ATPSat300;
    protected double AlF4on_kAlF4off;
    protected double AlF4on_kAlF4on;
    protected double GDPbSon_kGDPbSon;
    protected double GDPbSon_kGGDPbSoff;
    protected double GDPdissoc_TonicAct;
    protected double GDPdissoc_kGDPoff;
    protected double GDPdissoc_kGDPon;
    protected double GGTPase_kGGTPase;
    protected double GTPgSon_kGTPgSoff;
    protected double GTPgSon_kGTPgSon;
    protected double GTPon_kGTPoff;
    protected double GTPon_kGTPon;
    protected double Gactive;
    protected double IP3Phosphatase_kIP3ase;
    protected double I_KCNQ;
    protected double KMgGTPase;
    protected double Koxom;
    protected double MgGTPon_kMg2onGTP;
    protected double MgSat10;
    protected double MgSat20;
    protected double MgonGDPAlF4_kMgonAlF4;
    protected double MgonGGTPgS_kMg2onGTPgS;
    protected double OxoSat;
    protected double PI4Kinase_kPI4Kinase;
    protected double PIP2_5Pase_kPIP2Pase;
    protected double PIP2binding_kPIP2off;
    protected double PIP2binding_kPIP2on;
    protected double PIP5kinase_kPIP5Kinase;
    protected double PLC_PLCspont;
    protected double PLC_kPLC;
    protected double fGactive;
    protected double nHill;
    protected double time;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[30];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = _EX_M_Cytoplasm_GTP_C_;
        y[5] = x[4];
        y[6] = x[5];
        y[7] = x[6];
        y[8] = x[7];
        y[9] = x[8];
        y[10] = x[9];
        y[11] = x[10];
        y[12] = x[11];
        y[13] = x[12];
        y[14] = x[13];
        y[15] = x[14];
        y[16] = x[15];
        y[17] = x[16];
        y[18] = x[17];
        y[19] = x[18];
        y[20] = x[19];
        y[21] = _EX_oxoM_EX_;
        y[22] = ATPSat1000;
        y[23] = ATPSat300;
        y[24] = Gactive;
        y[25] = I_KCNQ;
        y[26] = MgSat10;
        y[27] = MgSat20;
        y[28] = OxoSat;
        y[29] = fGactive;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[20];
        this.time = 0.0;
        x[0] = 3000.0; // - $"EX.M.Cytoplasm.ATP_C"
        x[1] = 0.0; // - $"EX.M.Cytoplasm.AlF4_C"
        x[2] = 0.0; // - $"EX.M.Cytoplasm.GDP_C"
        x[3] = 0.0; // - $"EX.M.Cytoplasm.GDPbS_C"
        x[4] = 0.0; // - $"EX.M.Cytoplasm.GTPgS_C"
        x[5] = 0.48; // - $"EX.M.Cytoplasm.ip3_C"
        x[6] = 0.0; // - $"EX.M.GGDPAlF4Mg_M"
        x[7] = 0.0; // - $"EX.M.GGDPAlF4_M"
        x[8] = 200.0; // - $"EX.M.GGDP_M"
        x[9] = 0.0; // - $"EX.M.GGDPbS_M"
        x[10] = 0.1; // - $"EX.M.GGTPMg_M"
        x[11] = 0.048; // - $"EX.M.GGTP_M"
        x[12] = 0.0; // - $"EX.M.GGTPgSMg_M"
        x[13] = 0.0; // - $"EX.M.GGTPgS_M"
        x[14] = 0.065; // - $"EX.M.G_M"
        x[15] = 6.7; // - $"EX.M.KCNQsites_M"
        x[16] = 5000.0; // - $"EX.M.PIP2_M"
        x[17] = 33.3; // - $"EX.M.PIP2xKCNQ_M"
        x[18] = 1150.0; // - $"EX.M.PIP_M"
        x[19] = 200000.0; // - $"EX.M.PI_M"

        __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 'BIOMD0000000081.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_PI4Kinase*9.967E-4-rate_PIP5kinase*9.967E-4;
        result[1] = -rate_AlF4on*9.967E-4;
        result[2] = +rate_GDPdissoc*9.967E-4;
        result[3] = -rate_GDPbSon*9.967E-4;
        result[4] = -rate_GTPgSon*9.967E-4;
        result[5] = -rate_IP3Phosphatase+rate_PLC*9.967E-4;
        result[6] = +rate_MgonGDPAlF4;
        result[7] = +rate_AlF4on-rate_MgonGDPAlF4;
        result[8] = -rate_AlF4on-rate_GDPdissoc+rate_GGTPase;
        result[9] = +rate_GDPbSon;
        result[10] = -rate_GGTPase+rate_MgGTPon;
        result[11] = +rate_GTPon-rate_MgGTPon;
        result[12] = +rate_MgonGGTPgS;
        result[13] = +rate_GTPgSon-rate_MgonGGTPgS;
        result[14] = -rate_GDPbSon+rate_GDPdissoc-rate_GTPgSon-rate_GTPon;
        result[15] = -rate_PIP2binding;
        result[16] = -rate_PIP2_5Pase-rate_PIP2binding+rate_PIP5kinase-rate_PLC;
        result[17] = +rate_PIP2binding;
        result[18] = +rate_PI4Kinase+rate_PIP2_5Pase-rate_PIP5kinase;
        result[19] = -rate_PI4Kinase;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[20];
        __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[2];
        flags[0] = (time >= 3.0) ? +1 : -1; //event_0000001
        flags[1] = (time >= 8.0) ? +1 : -1; //event_0000002
        return flags;
    }

    public void processEvent(int __internalVar12060040825150, double time, double[] x)
    {
        this.time = time;
        if ( __internalVar12060040825150 == 0) //event_0000001
        {
            _EX_oxoM_EX_ = 10;
        } else if ( __internalVar12060040825150 == 1) //event_0000002
        {
            _EX_oxoM_EX_ = 0;
        }
    }

} // class ...