/*
 * This code is generated by BioUML FrameWork 
 * for BIOMD0000000062.xml diagram  at 2008.03.20 15:04:47
 */
import biouml.plugins.simulation.ae.NewtonSolver;
import biouml.plugins.simulation.java.JavaBaseModel;
import ru.biosoft.math.MathRoutines;

public class BIOMD0000000062 extends JavaBaseModel
{

/*
 * Write rules to calculate equation parameters
 */
    private void __internalVarInitFunc_0(double time, double[] x)
    {
        compartment_Tt = (-f_val*e_val - f_val*Tex + Tex*x[1] + Math.pow((Math.pow((f_val*(e_val + Tex) - Tex*x[1]), 2) - 4*f_val*Tex*(-e_val*x[1] - Tex*Tomax - Tex*x[1])), 0.5))/(2*Tex);
    }


/*
 * Write rules to calculate equation parameters excluding internal variables.
 */
    public void __internalRateVarInitFunc_0(double time, double[] x)
    {
        rate_Enzyme_dilution = compartment*Enzyme_dilution_mu*x[0];
        rate_Enzyme_synthesis = compartment*Enzyme_synthesis_k1*Math.pow(Enzyme_synthesis_ki1, Enzyme_synthesis_nH)*Enzyme_synthesis_Ot/(Math.pow(Enzyme_synthesis_ki1, Enzyme_synthesis_nH) + Math.pow(compartment_Tt, Enzyme_synthesis_nH));
        rate_tryptophan_consumption = compartment*tryptophan_consumption_g*x[1]/(tryptophan_consumption_Kg + x[1]);
        rate_tryptophan_dilution = compartment*tryptophan_dilution_mu*x[1];
        rate_tryptophan_synthesis = compartment*tryptophan_synthesis_k2*x[0]*tryptophan_synthesis_Ki2/(tryptophan_synthesis_Ki2 + compartment_Tt);
    }

    public void Init()
    {
        initialValues = getInitialValues();
/*
 * Initialize variables
 */
        compartment = 1.0; // initial value of $compartment
        Enzyme_dilution_mu = 0.01; // initial value of Enzyme_dilution_mu
        Enzyme_synthesis_Ot = 0.0033; // initial value of Enzyme_synthesis_Ot
        Enzyme_synthesis_k1 = 65.0; // initial value of Enzyme_synthesis_k1
        Enzyme_synthesis_ki1 = 3.53; // initial value of Enzyme_synthesis_ki1
        Enzyme_synthesis_nH = 1.92; // initial value of Enzyme_synthesis_nH
        Tex = 0.14; // initial value of Tex
        Tomax = 100.0; // initial value of Tomax
        e_val = 0.9; // initial value of e_val
        f_val = 380.0; // initial value of f_val
        tryptophan_consumption_Kg = 0.2; // initial value of tryptophan_consumption_Kg
        tryptophan_consumption_g = 25.0; // initial value of tryptophan_consumption_g
        tryptophan_dilution_mu = 0.01; // initial value of tryptophan_dilution_mu
        tryptophan_synthesis_Ki2 = 810.0; // initial value of tryptophan_synthesis_Ki2
        tryptophan_synthesis_k2 = 25.0; // initial value of tryptophan_synthesis_k2
    }

    /*
     * Model variables initial values
     */
    protected double rate_Enzyme_dilution;
    protected double rate_Enzyme_synthesis;
    protected double rate_tryptophan_consumption;
    protected double rate_tryptophan_dilution;
    protected double rate_tryptophan_synthesis;
    protected double compartment;
    protected double compartment_Tt;
    protected double Enzyme_dilution_mu;
    protected double Enzyme_synthesis_Ot;
    protected double Enzyme_synthesis_k1;
    protected double Enzyme_synthesis_ki1;
    protected double Enzyme_synthesis_nH;
    protected double Tex;
    protected double Tomax;
    protected double e_val;
    protected double f_val;
    protected double tryptophan_consumption_Kg;
    protected double tryptophan_consumption_g;
    protected double tryptophan_dilution_mu;
    protected double tryptophan_synthesis_Ki2;
    protected double tryptophan_synthesis_k2;

    public double[] extendResult(double time,double [] x)
    {
        this.time = time;

        __internalVarInitFunc_0(time, x);

        double[] y = new double[3];
        y[0] = x[0];
        y[1] = x[1];
        y[2] = compartment_Tt;
        return y;
    }
    public double[] getInitialValues()
    {
        double [] x = new double[2];
        this.time = 0.0;
        x[0] = 0.0; // - $"compartment.Enz"
        x[1] = 0.0; // - $compartment.Ts

        __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 'BIOMD0000000062.xml' model
         */
    public void __internalDyDt_0(double time, double [] x, double[] result)
    {
        result[0] = -rate_Enzyme_dilution+rate_Enzyme_synthesis;
        result[1] = -rate_tryptophan_consumption-rate_tryptophan_dilution+rate_tryptophan_synthesis;
    }
    protected double [] calculateResult(double time, double[] x)
    {
        double[] result = new double[2];
        __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 ...