/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000021.xml diagram  at 2008.03.20 15:03:58
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000021 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        Pt = x[1] + x[0] + x[4] + x[5] + x[6];
        Tt = x[1] + x[0] + x[7] + x[8] + x[9];
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_Mp_degradation = Cell*Mp_degradation_k_d*x[2] + Cell*Mp_degradation_V_mP*x[2]/(Mp_degradation_K_mP + x[2]);
        rate_Mp_production = Cell*Mp_production_v_sP*Math.pow(Mp_production_K_IP, Mp_production_n)/(Math.pow(Mp_production_K_IP, Mp_production_n) + Math.pow(x[0], Mp_production_n));
        rate_Mt_degradation = Cell*Mt_degradation_k_d*x[3] + Cell*V_mT*x[3]/(Mt_degradation_K_mT + x[3]);
        rate_Mt_production = Cell*Mt_production_V_sT*Math.pow(Mt_production_K_IT, Mt_production_n)/(Math.pow(Mt_production_K_IT, Mt_production_n) + Math.pow(x[0], Mt_production_n));
        rate_P0_degradation = Cell*P0_degradation_k_d*x[4];
        rate_P0_production = Cell*P0_production_k_sP*x[2];
        rate_P0_to_P1 = Cell*P0_to_P1_V_1P*x[4]/(P0_to_P1_K1_P + x[4]);
        rate_P1_degradation = Cell*P1_degradation_k_d*x[5];
        rate_P1_to_P0 = Cell*P1_to_P0_V_2P*x[5]/(P1_to_P0_K_2P + x[5]);
        rate_P1_to_P2 = Cell*P1_to_P2_V_3P*x[5]/(P1_to_P2_K_3P + x[5]);
        rate_P2_degradation = Cell*P2_degradation_k_d*x[6] + Cell*P2_degradation_V_dP*x[6]/(P2_degradation_K_dP + x[6]);
        rate_P2_to_P1 = Cell*P2_to_P1_V_4P*x[6]/(P2_to_P1_K_4P + x[6]);
        rate_PT_complex_degradation = Cell*PT_complex_degradation_k_dC*x[1];
        rate_PT_complex_formation = Cell*PT_complex_formation_k3*x[6]*x[9] - Cell*PT_complex_formation_k4*x[1];
        rate_PT_complex_nucleation = Cell*PT_complex_nucleation_k1*x[1] - _Cell_compartment_0000002_*PT_complex_nucleation_k2*x[0];
        rate_PTnucl_complex_degradation = _Cell_compartment_0000002_*PTnucl_complex_degradation_k_dN*x[0];
        rate_T0_degradation = Cell*T0_degradation_k_d*x[7];
        rate_T0_production = Cell*T0_production_k_sT*x[3];
        rate_T0_to_T1 = Cell*T0_to_T1_V_1T*x[7]/(T0_to_T1_K_1T + x[7]);
        rate_T1_degradation = Cell*T1_degradation_k_d*x[8];
        rate_T1_to_T0 = Cell*T1_to_T0_V_2T*x[8]/(T1_to_T0_K_2T + x[8]);
        rate_T1_to_T2 = Cell*T1_to_T2_V_3T*x[8]/(T1_to_T2_K_3T + x[8]);
        rate_T2_degradation = Cell*T2_degradation_k_d*x[9] + Cell*V_dT*x[9]/(T2_degradation_K_dT + x[9]);
        rate_T2_to_T1 = Cell*T2_to_T1_V_4T*x[9]/(T2_to_T1_K_4T + x[9]);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        _Cell_compartment_0000002_ = 1.0; // initial value of $"Cell.compartment_0000002"
        Cell = 1.0; // initial value of $Cell
        Mp_degradation_K_mP = 0.2; // initial value of Mp_degradation_K_mP
        Mp_degradation_V_mP = 0.7; // initial value of Mp_degradation_V_mP
        Mp_degradation_k_d = 0.01; // initial value of Mp_degradation_k_d
        Mp_production_K_IP = 1.0; // initial value of Mp_production_K_IP
        Mp_production_n = 4.0; // initial value of Mp_production_n
        Mp_production_v_sP = 1.0; // initial value of Mp_production_v_sP
        Mt_degradation_K_mT = 0.2; // initial value of Mt_degradation_K_mT
        Mt_degradation_k_d = 0.01; // initial value of Mt_degradation_k_d
        Mt_production_K_IT = 1.0; // initial value of Mt_production_K_IT
        Mt_production_V_sT = 1.0; // initial value of Mt_production_V_sT
        Mt_production_n = 4.0; // initial value of Mt_production_n
        P0_degradation_k_d = 0.01; // initial value of P0_degradation_k_d
        P0_production_k_sP = 0.9; // initial value of P0_production_k_sP
        P0_to_P1_K1_P = 2.0; // initial value of P0_to_P1_K1_P
        P0_to_P1_V_1P = 8.0; // initial value of P0_to_P1_V_1P
        P1_degradation_k_d = 0.01; // initial value of P1_degradation_k_d
        P1_to_P0_K_2P = 2.0; // initial value of P1_to_P0_K_2P
        P1_to_P0_V_2P = 1.0; // initial value of P1_to_P0_V_2P
        P1_to_P2_K_3P = 2.0; // initial value of P1_to_P2_K_3P
        P1_to_P2_V_3P = 8.0; // initial value of P1_to_P2_V_3P
        P2_degradation_K_dP = 0.2; // initial value of P2_degradation_K_dP
        P2_degradation_V_dP = 2.0; // initial value of P2_degradation_V_dP
        P2_degradation_k_d = 0.01; // initial value of P2_degradation_k_d
        P2_to_P1_K_4P = 2.0; // initial value of P2_to_P1_K_4P
        P2_to_P1_V_4P = 1.0; // initial value of P2_to_P1_V_4P
        PT_complex_degradation_k_dC = 0.01; // initial value of PT_complex_degradation_k_dC
        PT_complex_formation_k3 = 1.2; // initial value of PT_complex_formation_k3
        PT_complex_formation_k4 = 0.6; // initial value of PT_complex_formation_k4
        PT_complex_nucleation_k1 = 0.6; // initial value of PT_complex_nucleation_k1
        PT_complex_nucleation_k2 = 0.2; // initial value of PT_complex_nucleation_k2
        PTnucl_complex_degradation_k_dN = 0.01; // initial value of PTnucl_complex_degradation_k_dN
        T0_degradation_k_d = 0.01; // initial value of T0_degradation_k_d
        T0_production_k_sT = 0.9; // initial value of T0_production_k_sT
        T0_to_T1_K_1T = 2.0; // initial value of T0_to_T1_K_1T
        T0_to_T1_V_1T = 8.0; // initial value of T0_to_T1_V_1T
        T1_degradation_k_d = 0.01; // initial value of T1_degradation_k_d
        T1_to_T0_K_2T = 2.0; // initial value of T1_to_T0_K_2T
        T1_to_T0_V_2T = 1.0; // initial value of T1_to_T0_V_2T
        T1_to_T2_K_3T = 2.0; // initial value of T1_to_T2_K_3T
        T1_to_T2_V_3T = 8.0; // initial value of T1_to_T2_V_3T
        T2_degradation_K_dT = 0.2; // initial value of T2_degradation_K_dT
        T2_degradation_k_d = 0.01; // initial value of T2_degradation_k_d
        T2_to_T1_K_4T = 2.0; // initial value of T2_to_T1_K_4T
        T2_to_T1_V_4T = 1.0; // initial value of T2_to_T1_V_4T
        V_dT = 2.0; // initial value of V_dT
        V_mT = 0.7; // initial value of V_mT
    }

    /*
     * Model variables initial values
     */
    protected double _Cell_compartment_0000002_;
    protected double rate_Mp_degradation;
    protected double rate_Mp_production;
    protected double rate_Mt_degradation;
    protected double rate_Mt_production;
    protected double rate_P0_degradation;
    protected double rate_P0_production;
    protected double rate_P0_to_P1;
    protected double rate_P1_degradation;
    protected double rate_P1_to_P0;
    protected double rate_P1_to_P2;
    protected double rate_P2_degradation;
    protected double rate_P2_to_P1;
    protected double rate_PT_complex_degradation;
    protected double rate_PT_complex_formation;
    protected double rate_PT_complex_nucleation;
    protected double rate_PTnucl_complex_degradation;
    protected double rate_T0_degradation;
    protected double rate_T0_production;
    protected double rate_T0_to_T1;
    protected double rate_T1_degradation;
    protected double rate_T1_to_T0;
    protected double rate_T1_to_T2;
    protected double rate_T2_degradation;
    protected double rate_T2_to_T1;
    protected double Cell;
    protected double Mp_degradation_K_mP;
    protected double Mp_degradation_V_mP;
    protected double Mp_degradation_k_d;
    protected double Mp_production_K_IP;
    protected double Mp_production_n;
    protected double Mp_production_v_sP;
    protected double Mt_degradation_K_mT;
    protected double Mt_degradation_k_d;
    protected double Mt_production_K_IT;
    protected double Mt_production_V_sT;
    protected double Mt_production_n;
    protected double P0_degradation_k_d;
    protected double P0_production_k_sP;
    protected double P0_to_P1_K1_P;
    protected double P0_to_P1_V_1P;
    protected double P1_degradation_k_d;
    protected double P1_to_P0_K_2P;
    protected double P1_to_P0_V_2P;
    protected double P1_to_P2_K_3P;
    protected double P1_to_P2_V_3P;
    protected double P2_degradation_K_dP;
    protected double P2_degradation_V_dP;
    protected double P2_degradation_k_d;
    protected double P2_to_P1_K_4P;
    protected double P2_to_P1_V_4P;
    protected double PT_complex_degradation_k_dC;
    protected double PT_complex_formation_k3;
    protected double PT_complex_formation_k4;
    protected double PT_complex_nucleation_k1;
    protected double PT_complex_nucleation_k2;
    protected double PTnucl_complex_degradation_k_dN;
    protected double Pt;
    protected double T0_degradation_k_d;
    protected double T0_production_k_sT;
    protected double T0_to_T1_K_1T;
    protected double T0_to_T1_V_1T;
    protected double T1_degradation_k_d;
    protected double T1_to_T0_K_2T;
    protected double T1_to_T0_V_2T;
    protected double T1_to_T2_K_3T;
    protected double T1_to_T2_V_3T;
    protected double T2_degradation_K_dT;
    protected double T2_degradation_k_d;
    protected double T2_to_T1_K_4T;
    protected double T2_to_T1_V_4T;
    protected double Tt;
    protected double V_dT;
    protected double V_mT;

    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] = x[4];
        y[5] = x[5];
        y[6] = x[6];
        y[7] = x[7];
        y[8] = x[8];
        y[9] = x[9];
        y[10] = Pt;
        y[11] = Tt;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[10];
        this.time = 0.0;
        x[0] = 0.0; // - $"Cell.compartment_0000002.Cn"
        x[1] = 0.0; // - $Cell.CC
        x[2] = 0.0; // - $Cell.Mp
        x[3] = 0.0; // - $Cell.Mt
        x[4] = 0.0; // - $Cell.P0
        x[5] = 0.0; // - $Cell.P1
        x[6] = 0.0; // - $Cell.P2
        x[7] = 0.0; // - $Cell.T0
        x[8] = 0.0; // - $Cell.T1
        x[9] = 0.0; // - $Cell.T2

        __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 'BIOMD0000000021.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = +rate_PT_complex_nucleation-rate_PTnucl_complex_degradation;
        result[1] = -rate_PT_complex_degradation+rate_PT_complex_formation-rate_PT_complex_nucleation;
        result[2] = -rate_Mp_degradation+rate_Mp_production;
        result[3] = -rate_Mt_degradation+rate_Mt_production;
        result[4] = -rate_P0_degradation+rate_P0_production-rate_P0_to_P1+rate_P1_to_P0;
        result[5] = +rate_P0_to_P1-rate_P1_degradation-rate_P1_to_P0-rate_P1_to_P2+rate_P2_to_P1;
        result[6] = +rate_P1_to_P2-rate_P2_degradation-rate_P2_to_P1-rate_PT_complex_formation;
        result[7] = -rate_T0_degradation+rate_T0_production-rate_T0_to_T1+rate_T1_to_T0;
        result[8] = +rate_T0_to_T1-rate_T1_degradation-rate_T1_to_T0-rate_T1_to_T2+rate_T2_to_T1;
        result[9] = -rate_PT_complex_formation+rate_T1_to_T2-rate_T2_degradation-rate_T2_to_T1;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[10];
        __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 ...