/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000058.xml diagram  at 2008.03.20 15:04:43
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000058 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        Phi1_c1 = r2*x[0]/(R1 + x[0]);
        Phi_minus1_c2 = k1/(R3 + x[1]);
        Phi2_c2 = (k2 + r4*x[1])/(R3 + x[1]);
        Phi3_c2 = k3/(R5 + x[1]);
        Phi_minus1_c1 = k1/(R3 + x[0]);
        Phi2_c1 = (k2 + r4*x[0])/(R3 + x[0]);
        Phi3_c1 = k3/(R5 + x[0]);
        Phi1_c2 = r2*x[1]/(R1 + x[1]);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_Inactivated_to_S_Cell1 = compartment*Phi3_c1*(1 - x[2]);
        rate_Inactivated_to_S_Cell2 = compartment*Phi3_c2*(1 - x[3]);
        rate_Jleak_Cell1 = compartment*Jleak_Cell1_Jleak;
        rate_Jleak_Cell2 = compartment*Jleak_Cell2_Jleak;
        rate_Jpump_Cell1 = compartment*(Jpump_Cell1_Vp*Math.pow(x[0], 2)/(Math.pow(Jpump_Cell1_Kp, 2) + Math.pow(x[0], 2)));
        rate_Jpump_Cell2 = compartment*Jpump_Cell2_Vp*Math.pow(x[1], 2)/(Math.pow(Jpump_Cell2_Kp, 2) + Math.pow(x[1], 2));
        rate_Jreceptor_Cell1 = compartment*Jreceptor_Cell1_kf*Math.pow((Jreceptor_Cell1_p*x[2]*Phi1_c1/(Phi1_c1*Jreceptor_Cell1_p + Phi_minus1_c1)), 4);
        rate_Jreceptor_Cell2 = compartment*Jreceptor_Cell2_kf*Math.pow((Jreceptor_Cell2_p*x[3]*Phi1_c2/(Phi1_c2*Jreceptor_Cell2_p + Phi_minus1_c2)), 4);
        rate_Open_to_Inactivated_Cell1 = compartment*Phi1_c1*Phi2_c1*x[2]*Open_to_Inactivated_Cell1_p/(Phi1_c1*Open_to_Inactivated_Cell1_p + Phi_minus1_c1);
        rate_Open_to_Inactivated_Cell2 = compartment*Phi1_c2*Phi2_c2*x[3]*Open_to_Inactivated_Cell2_p/(Phi1_c2*Open_to_Inactivated_Cell2_p + Phi_minus1_c2);
        rate_diffusion = compartment*diffusion_D*(x[1] - x[0]);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        compartment = 1.0; // initial value of $compartment
        Jleak_Cell1_Jleak = 0.2; // initial value of Jleak_Cell1_Jleak
        Jleak_Cell2_Jleak = 0.2; // initial value of Jleak_Cell2_Jleak
        Jpump_Cell1_Kp = 0.18; // initial value of Jpump_Cell1_Kp
        Jpump_Cell1_Vp = 1.2; // initial value of Jpump_Cell1_Vp
        Jpump_Cell2_Kp = 0.18; // initial value of Jpump_Cell2_Kp
        Jpump_Cell2_Vp = 1.2; // initial value of Jpump_Cell2_Vp
        Jreceptor_Cell1_kf = 28.0; // initial value of Jreceptor_Cell1_kf
        Jreceptor_Cell1_p = 0.2778; // initial value of Jreceptor_Cell1_p
        Jreceptor_Cell2_kf = 28.0; // initial value of Jreceptor_Cell2_kf
        Jreceptor_Cell2_p = 0.2778; // initial value of Jreceptor_Cell2_p
        Open_to_Inactivated_Cell1_p = 0.2778; // initial value of Open_to_Inactivated_Cell1_p
        Open_to_Inactivated_Cell2_p = 0.2778; // initial value of Open_to_Inactivated_Cell2_p
        R1 = 6.0; // initial value of R1
        R3 = 50.0; // initial value of R3
        R5 = 1.6; // initial value of R5
        diffusion_D = 0.01; // initial value of diffusion_D
        k1 = 44.0; // initial value of k1
        k2 = 26.5; // initial value of k2
        k3 = 1.6; // initial value of k3
        r2 = 100.0; // initial value of r2
        r4 = 20.0; // initial value of r4
    }

    /*
     * Model variables initial values
     */
    protected double rate_Inactivated_to_S_Cell1;
    protected double rate_Inactivated_to_S_Cell2;
    protected double rate_Jleak_Cell1;
    protected double rate_Jleak_Cell2;
    protected double rate_Jpump_Cell1;
    protected double rate_Jpump_Cell2;
    protected double rate_Jreceptor_Cell1;
    protected double rate_Jreceptor_Cell2;
    protected double rate_Open_to_Inactivated_Cell1;
    protected double rate_Open_to_Inactivated_Cell2;
    protected double rate_diffusion;
    protected double compartment;
    protected double Jleak_Cell1_Jleak;
    protected double Jleak_Cell2_Jleak;
    protected double Jpump_Cell1_Kp;
    protected double Jpump_Cell1_Vp;
    protected double Jpump_Cell2_Kp;
    protected double Jpump_Cell2_Vp;
    protected double Jreceptor_Cell1_kf;
    protected double Jreceptor_Cell1_p;
    protected double Jreceptor_Cell2_kf;
    protected double Jreceptor_Cell2_p;
    protected double Open_to_Inactivated_Cell1_p;
    protected double Open_to_Inactivated_Cell2_p;
    protected double Phi1_c1;
    protected double Phi1_c2;
    protected double Phi2_c1;
    protected double Phi2_c2;
    protected double Phi3_c1;
    protected double Phi3_c2;
    protected double Phi_minus1_c1;
    protected double Phi_minus1_c2;
    protected double R1;
    protected double R3;
    protected double R5;
    protected double diffusion_D;
    protected double k1;
    protected double k2;
    protected double k3;
    protected double r2;
    protected double r4;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[12];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = x[2];
        y[3] = x[3];
        y[4] = Phi1_c1;
        y[5] = Phi1_c2;
        y[6] = Phi2_c1;
        y[7] = Phi2_c2;
        y[8] = Phi3_c1;
        y[9] = Phi3_c2;
        y[10] = Phi_minus1_c1;
        y[11] = Phi_minus1_c2;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[4];
        this.time = 0.0;
        x[0] = 0.3; // - $compartment.c1
        x[1] = 0.1; // - $compartment.c2
        x[2] = 0.8; // - $compartment.h1
        x[3] = 0.1; // - $compartment.h2

        __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 'BIOMD0000000058.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_Jleak_Cell1-rate_Jpump_Cell1+rate_Jreceptor_Cell1+rate_diffusion;
        result[1] = +rate_Jleak_Cell2-rate_Jpump_Cell2+rate_Jreceptor_Cell2-rate_diffusion;
        result[2] = +rate_Inactivated_to_S_Cell1-rate_Open_to_Inactivated_Cell1;
        result[3] = +rate_Inactivated_to_S_Cell2-rate_Open_to_Inactivated_Cell2;
    }
    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 ...