public class wa_mix
{
steam st;
ammonia amn;	
// water residual properties	
double[] c_water={0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,6,6,6,6,0,0,0};
double[] d_water={1,1,1,2,2,3,4,1,1,1,2,2,3,4,4,5,7,9,10,11,13,15,1,2,2,2,3,4,4,4,5,6,6,7,9,9,9,9,9,10,10,12,3,4,4,5,14,3,6,6,6,3,3,3};
double[] t_water={-0.5,0.875,1,0.5,0.75,0.375,1,4,6,12,1,5,4,2,13,9,3,4,11,4,13,1,7,1,9,10,10,3,7,10,10,6,10,10,1,2,3,4,8,6,9,8,16,22,23,23,10,50,44,46,50,0,1,4};
double[] n_water={0.1253354793552331021e-1,0.789576347228283101e1,-0.87803203303561e1,0.31802509345418,-0.26145533859358,-0.78199751687981e-2,0.88089493102134e-2,-0.66856572307965,0.20433810950965,-0.66212605039687e-4,-0.19232721156002,-0.25709043003438,0.16074868486251,-0.40092828925807e-1,0.39343422603254e-6,-0.75941377088144e-5,0.56250979351888e-3,-0.15608652257135e-4,0.11537996422951e-8,0.36582165144204e-6,-0.13251180074668e-11,-0.62639586912454e-9,-0.10793600908932,0.17611491008752e-1,0.22132295167546,-0.40247669763528,0.58083399985759,0.49969146990806e-2,-0.31358700712549e-1,-0.74315929710341,0.47807329915480,0.20527940895948e-1,-0.13636435110343,0.14180634400617e-1,0.83326504880713e-2,-0.29052336009585e-1,0.38615085574206e-1,-0.20393486513704e-1,-0.16554050063734e-2,0.19955571979541e-2,0.15870308324157e-3,-0.16388568342530e-4,0.43613615723811e-1,0.34994005463765e-1,-0.76788197844621e-1,0.22446277332006e-1,-0.62689710414685e-4,-0.55711118565645e-9,-0.19905718354408,0.31777497330738,-0.11841182425981,-0.31306260323435e2,0.31546140237781e2,-0.25213154341695e4,-0.14874640856724,0.31806110878444  };
double[] a_water={3.5,3.5};
double[] b_water={0.85,0.95};
double[] B_water={0.2,0.2};
double[] alfa_water={20,20,20};
double[] beta_water={150,150,250};
double[] gamma_water={1.21,1.21,1.25};
double[] eps_water={1,1,1};
double[] C_water={28,32};
double[] D_water={700,800};
double[] A_water={0.32,0.32};
double[] beta2_water={0.3,0.3};
double Tc_water=647.096 ;//K
double roc_water=322; //kg/m3
double R=461.51805 ; // J/kgK
double Tc_amn=405.4; //K
double roc_amn=1/0.00424; //kg/m3
double kv_red=1.2395117;
double kt_red=0.9648407;
double alfa_red=1.125455;
double beta_red=0.8978069;
double gamma_dep=0.5248379;
/////////////////////////////////////////////// idelagas part

double[] alfa_mix_ideal={-7.720435,8.649358,3.00632,0.012436,0.97315,1.27950,0.96956,0.24873,-16.444285,4.036946,-1.0,10.69955,-1.775436,0.82374034};
double[] teta_mix_ideal={0,0,0,1.666,4.578,10.018,11.964,35.600};
double[] t_mix_ideal={0,0,0,0,0,0,0,0,0,0,0,0.333,-1.5,-1.75};

///////////////////////////////////////////////////////////////////////  

double[] a_res={-1.855822e-2,5.258010e-2,3.552874e-10,5.451379e-6,-5.998546e-13,-3.687808e-6,0.2586192,-1.368072e-8,1.226146e-2,-7.181443e-2,9.970849e-2,1.0584086e-3,-0.1963687,-0.7777897};
double[] d_res={4,5,15,12,12,15,4,15,4,5,6,10,6,2};
double[] t_res={1.5,0.5,6.5,1.75,15,6,-1,4,3.5,0,-1,8,7.5,4};
double[] e_res={0,1,1,1,1,2,1,1,1,1,2,2,2,2};

///////////////////////////////////////////////////////////////////////// ammonia 

double[] Nk={(-1.858814),0.04554431,0.7238548,0.0122947,2.141882e-11,-0.0143002,0.3441324,-0.2873571,0.00002352589,-0.03497111,0.001831117,0.02397852,-0.04085375,0.2379275,-0.03548972,-0.1823729,0.02281556,-0.006663444,-0.008847486,0.002272635,-0.0005588655};
double[] tk={1.5,-0.5,0.5,1,3,0,3,4,4,5,5,3,6,8,8,10,10,5,7.5,15,30};
double[] dk={1,2,1,4,15,3,3,1,8,2,8,1,1,2,3,2,4,3,1,2,4};
double[] lk={0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3};
double[] ak={0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

/////////////////////////// ammonia tillner-roth 
double[] ni={0.73022720,-0.11879116e1,-0.68319136,0.40028683e-1,0.90801215e-4,-0.56216175e-1,0.44935601,0.29897121e-1,-0.18181684,-0.98416660e-1,-0.55083744e-1,-0.88983219e-2};


public double[] getvalues(double T,double p) // T in Kelvin p kPa
{
st=new steam();
double[] prop=st.property_SI("tp",T-273,p/100)	;
double ro1=1/prop[2];	

double delta=ro1/roc_water;
double to=Tc_water/T;
double[] depo=new double[2];
depo[0]=delta;//ro
depo[1]=to;//T
return depo;
}

public double dQres_del_water(double T,double p)
{
double[] depo=getvalues(T,p);
double delta=depo[0];
double to=depo[1];
double top1=0.0;
double top2=0.0;
double top3=0.0;
double top4=0.0;
for(int i=0;i<=6;i++)
{
top1+=n_water[i]*Math.pow(delta,d_water[i]-1)*d_water[i]*Math.pow(to,t_water[i]);	
}

for(int ii=7;ii<=50;ii++)
{
top2+=n_water[ii]*Math.exp(-Math.pow(delta,c_water[ii]))*(Math.pow(delta,(d_water[ii]-1))*Math.pow(to,t_water[ii])*(d_water[ii]-c_water[ii]*Math.pow(delta,c_water[ii])));	

}
//System.out.println(Math.exp(-Math.pow(delta,5)));//c_water[49]
for(int i=51;i<=53;i++)
{
top3+=n_water[i]*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i])*Math.exp(-alfa_water[i-51]*(delta-eps_water[i-51])*(delta-eps_water[i-51])-beta_water[i-51]*(to-gamma_water[i-51])*(to-gamma_water[i-51]))*((d_water[i]/delta)-2*alfa_water[i-51]*(delta-eps_water[i-51]));
}

for(int i=54;i<=55;i++)
{
top4+=n_water[i]*(      (Math.pow(((((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))+B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54])),b_water[i-54])*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))+((delta)*(-2*C_water[i-54]*(delta-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))) + (      (b_water[i-54]*Math.pow(((((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))+B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54])),b_water[i-54]-1)*(delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))*delta*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))                            )             );
}


return (top2+top1+top3+top4);
}  
//((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))


public double d2Qres_del_del_water(double T,double p)
{
double[] depo=getvalues(T,p);
double delta=depo[0];
double to=depo[1];
double top1=0.0;
double top2=0.0;
double top3=0.0;
double top4=0.0;	

for(int i=0;i<=6;i++)
{
top1+=n_water[i]*d_water[i]*(d_water[i]-1)*Math.pow(delta,d_water[i]-2)*Math.pow(to,t_water[i]);	
}

for(int i=7;i<=50;i++)
{
top2+=n_water[i]*Math.exp(-Math.pow(delta,c_water[i]))*(           Math.pow(delta,d_water[i]-2)*Math.pow(to,t_water[i])*( (d_water[i]-c_water[i]*Math.pow(delta,c_water[i]))*(d_water[i]-1-c_water[i]*Math.pow(delta,c_water[i]))-(c_water[i]*c_water[i]*Math.pow(delta,c_water[i]))   )   );	
}

for(int i=51;i<=53;i++)
{
top3+=n_water[i]*Math.pow(to,t_water[i])*Math.exp(-alfa_water[i-51]*(delta-eps_water[i-51])*(delta-eps_water[i-51])-beta_water[i-51]*(to-gamma_water[i-51])*(to-gamma_water[i-51]))*( (-2*alfa_water[i-51]*Math.pow(delta,d_water[i]))+(4*alfa_water[i-51]*alfa_water[i-51]*Math.pow(delta,d_water[i])*(delta-eps_water[i-51])*(delta-eps_water[i-51]))-(4*d_water[i]*alfa_water[i-51]*Math.pow(delta,d_water[i]-1)*(delta-eps_water[i-51]))+ (d_water[i]*(d_water[i]-1)*Math.pow(delta,d_water[i]-2)) );	
}

for(int i=54;i<=55;i++)
{
top4+=n_water[i]*( ((Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]))*(2*(-2*C_water[i-54]*(delta-1)*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))+(delta*((2*C_water[i-54]*(delta-1)*(delta-1)-1)*2*C_water[i-54]*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))  + ((2*(b_water[i-54]*Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))))*((Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))+(delta*((-2*C_water[i-54]*(delta-1)*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))) + ((b_water[i-54]*(   (Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)*((((1/(delta-1))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1)))))  + (delta-1)*(delta-1)*((4*B_water[i-54]*a_water[i-54]*(a_water[i-54]-1)*Math.pow((delta-1)*(delta-1),a_water[i-54]-2)) + ( 2*A_water[i-54]*A_water[i-54]*(1/beta2_water[i-54])*(1/beta2_water[i-54])*(Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])-1))*(Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])-1)))  +   (A_water[i-54]*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*(4/beta2_water[i-54])*(1/(2*beta2_water[i-54])-1)*Math.pow((delta-1)*(delta-1),(1/(2*beta2_water[i-54])-2))) )))) + (     (b_water[i-54]-1)* Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-2)*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))  )  ))*delta*((Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))));	
}
return top1+top2+top3+top4;
}

public double dQres_to_water(double T,double p)
{
double[] depo=getvalues(T,p);
double delta=depo[0];
double to=depo[1];
double top1=0.0;
double top2=0.0;
double top3=0.0;
double top4=0.0;	

for(int i=0;i<=6;i++)
{
top1+=n_water[i]*t_water[i]*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i]-1);	
}	
	
for(int i=7;i<=50;i++)
{
top2+=n_water[i]*t_water[i]*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i]-1)*Math.exp(-Math.pow(delta,c_water[i]));	
}

for(int i=51;i<=53;i++)
{
top3+=(n_water[i]*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i])*Math.exp(-alfa_water[i-51]*(delta-eps_water[i-51])*(delta-eps_water[i-51])-beta_water[i-51]*(to-gamma_water[i-51])*(to-gamma_water[i-51])))*((t_water[i]/to)-(2*beta_water[i-51]*(to-gamma_water[i-51])))	;
}

for(int i=54;i<=55;i++)
{
top4+=n_water[i]*delta*( (((-2*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1))))*((Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))))    +  (((Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54])))*((-2*D_water[i-54]*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))))));	
}

return (top1+top2+top3+top4);	
}

public double dQres_to_to_water(double T,double p)
{
double[] depo=getvalues(T,p);
double delta=depo[0];
double to=depo[1];
double top1=0.0;
double top2=0.0;
double top3=0.0;
double top4=0.0;

for(int i=0;i<=6;i++)	
{
top1+=n_water[i]*t_water[i]*(t_water[i]-1)*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i]-2);	
}	
	
for(int i=7;i<=50;i++)	
{
top2+=n_water[i]*t_water[i]*(t_water[i]-1)*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i]-2)*Math.exp(-Math.pow(delta,c_water[i]));	
}	
for(int i=51;i<=53;i++)
{
top3+=n_water[i]*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i])*Math.exp(-alfa_water[i-51]*(delta-eps_water[i-51])*(delta-eps_water[i-51])-beta_water[i-51]*(to-gamma_water[i-51])*(to-gamma_water[i-51]))*( ((t_water[i]/to)-2*beta_water[i-51]*(to-gamma_water[i-51]))*((t_water[i]/to)-2*beta_water[i-51]*(to-gamma_water[i-51]))-(t_water[i]/to/to)-(2*beta_water[i-51]) )	;
}
	
for(int i=54;i<=55;i++)
{
top4+=n_water[i]*delta*(  (((2*b_water[i-54]*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)))+(4*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(b_water[i-54]-1)*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-2)) )))*((Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))  + (2*((-2*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1))))*((-2*D_water[i-54]*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))))) + (((Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54])))*(((2*D_water[i-54]*(to-1)*(to-1)-1)*(2*D_water[i-54]*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))) )	;
}

return 	(top1+top2+top3+top4);
}

public double dQres_delta_to_water(double T,double p) 
{
double[] depo=getvalues(T,p);
double delta=depo[0];
double to=depo[1];
double top1=0.0;
double top2=0.0;
double top3=0.0;
double top4=0.0;	
	
for(int i=0;i<=6;i++)	
{
top1+=n_water[i]*d_water[i]*t_water[i]*Math.pow(delta,d_water[i]-1)*Math.pow(to,t_water[i]-1);	
}	

for(int i=7;i<=50;i++)
{
top2+=n_water[i]*t_water[i]*Math.pow(delta,d_water[i]-1)*Math.pow(to,t_water[i]-1)*Math.exp(-Math.pow(delta,c_water[i]))*(d_water[i]-c_water[i]*Math.pow(delta,c_water[i]));	
}

for(int i=51;i<=53;i++)
{
top3+=(n_water[i]*Math.pow(delta,d_water[i])*Math.pow(to,t_water[i])*Math.exp(-alfa_water[i-51]*(delta-eps_water[i-51])*(delta-eps_water[i-51])-beta_water[i-51]*(to-gamma_water[i-51])*(to-gamma_water[i-51])))*(((d_water[i]/delta)-2*alfa_water[i-51]*(delta-eps_water[i-51])))*((t_water[i]/to)-2*beta_water[i-51]*(to-gamma_water[i-51]))	;
}
	
for(int i=54;i<=55;i++)
{
top4+=n_water[i]*( (((Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54])))*( ((-2*D_water[i-54]*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))))+delta*((4*C_water[i-54]*D_water[i-54]*(delta-1)*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))) + ((delta)*((b_water[i-54]*Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))))*((-2*D_water[i-54]*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))  +   (((-2*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1))))*(((Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))+(delta)*((-2*C_water[i-54]*(delta-1)*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))))   +   ((((-A_water[i-54]*b_water[i-54]*(2/beta2_water[i-54])*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1))*(delta-1)*Math.pow((delta-1)*(delta-1),(1/(2*beta2_water[i-54])-1)))-(2*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(b_water[i-54]-1)*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-2))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1)))) )))*(delta)*((Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))) )	;
}

return 	(top1+top2+top3+top4);
}


////////////////////////////////////////////////////////////////
public double amn_test(double T,double p)
{
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
	
return rowat1;	
	
}



















public double[] ideal_gas_part1(double T,double p,double x)
{
// Tsol in Kelvin
// p in bar for water   
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x))/(((1-x)*0.018015268)+(x*0.01703026));
double Tn=500;//K
double ron=15000;//mol/m3
double to0=Tn/T;	
double delta0=ro/ron;	
double[] aa=new double[2];
aa[0]=to0; //T
aa[1]=delta0; //ro
return aa;
}

public double dQ_ideal(double T,double p,double x)
{
double[] aa=ideal_gas_part1(T,p,x);
double to0=aa[0];
double delta0=aa[1];
double top1=0.0;
double top2=0.0;	

for(int i=3;i<=7;i++)
{
top1+=alfa_mix_ideal[i]*Math.log(1-Math.exp(-teta_mix_ideal[i]*to0));
}

for(int i=11;i<=13;i++)
{
top2+=alfa_mix_ideal[i]*Math.pow(to0,t_mix_ideal[i]);
}

double top=Math.log(delta0)+((1-x)*(alfa_mix_ideal[0]+(alfa_mix_ideal[1]*to0)+(alfa_mix_ideal[2]*Math.log(to0))+Math.log(1-x)+top1))+  (x*(alfa_mix_ideal[8]+(alfa_mix_ideal[9]*to0)+(alfa_mix_ideal[10]*Math.log(to0))+Math.log(x)+top2));
return top;
	
}


public double dQ_ideal_to(double T,double p,double x)
{
double[] aa=ideal_gas_part1(T,p,x);
double to0=aa[0];
double delta0=aa[1];
double top1=0.0;
double top2=0.0;

for(int i=3;i<=7;i++)
{
top1+=(alfa_mix_ideal[i]*teta_mix_ideal[i]*Math.exp(-teta_mix_ideal[i]*to0))/(1-Math.exp(-teta_mix_ideal[i]*to0));	
}

for(int i=11;i<=13;i++)
{
top2+=alfa_mix_ideal[i]*t_mix_ideal[i]*Math.pow(to0,t_mix_ideal[i]-1);
}

double top=((1-x)*(alfa_mix_ideal[1]+(alfa_mix_ideal[2]/to0)+top1))+((x)*(alfa_mix_ideal[9]+(alfa_mix_ideal[10]/to0)+top2));

return top;	
}

public double dQ_ideal_to_to(double T,double p,double x)
{
double[] aa=ideal_gas_part1(T,p,x);
double to0=aa[0];
double delta0=aa[1];
double top1=0.0;
double top2=0.0;	

for(int i=3;i<=7;i++)
{
top1+=(((-alfa_mix_ideal[i]*teta_mix_ideal[i]*teta_mix_ideal[i]*Math.exp(-teta_mix_ideal[i]*to0))*(1-Math.exp(-teta_mix_ideal[i]*to0)))-((alfa_mix_ideal[i]*teta_mix_ideal[i]*Math.exp(-teta_mix_ideal[i]*to0))*(teta_mix_ideal[i]*Math.exp(-teta_mix_ideal[i]*to0))))/((1-Math.exp(-teta_mix_ideal[i]*to0))*(1-Math.exp(-teta_mix_ideal[i]*to0)));
}	
	
for(int i=11;i<=13;i++)
{
top2+=alfa_mix_ideal[i]*t_mix_ideal[i]*(t_mix_ideal[i]-1)*Math.pow(to0,t_mix_ideal[i]-2);
}	

double top=((1-x)*((-alfa_mix_ideal[2]/to0/to0)+top1))+((x)*((-alfa_mix_ideal[10]/to0/to0)+top2));

return top;
}

////////// Ammonia prop ///////////////
/*public double Q_amn(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;
double top1=0.0;
System.out.println("to = "+to);
System.out.println("delta = "+delta);

for(int i=0;i<=20;i++)
{
top1+=(Nk[i]*Math.pow(to,tk[i])*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(-to));
}		
return top1;	
}

public double dQ_amn_dto(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
double top1=0.0;
System.out.println("to = "+to);
System.out.println("delta = "+delta);

for(int i=0;i<=20;i++)
{
top1+=(Nk[i]*tk[i]*Math.pow(to,tk[i]-1)*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(-to))+(-Math.exp(-to)*Nk[i]*Math.pow(to,tk[i])*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,tk[i])));	
}	
return top1;	
}

public double d2Q_amn_d2to(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
double top1=0.0;	
System.out.println("to = "+to);
System.out.println("delta = "+delta);	
for(int i=0;i<=20;i++)
{
top1+=(Nk[i]*tk[i]*(tk[i]-1)*Math.pow(to,tk[i]-2)*Math.pow(delta,dk[i])*Math.exp((-ak[i]*Math.pow(delta,lk[i])))*Math.exp(-to))+(-Math.exp(-to)*Nk[i]*tk[i]*Math.pow(to,tk[i]-1)*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,lk[i])))+(-Math.exp(-to)*Nk[i]*tk[i]*Math.pow(to,tk[i]-1)*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,lk[i])))+( Math.exp(-to)*Nk[i]*Math.pow(to,tk[i])*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,lk[i])));
}	
	
return top1;	
}


public double dQ_amn_ddelta(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
double top1=0.0;	
System.out.println("to = "+to);
System.out.println("delta = "+delta);
for(int i=0;i<=20;i++)
{
top1+=(Nk[i]*Math.pow(to,tk[i])*dk[i]*Math.pow(delta,dk[i]-1)*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(-to))+(  Nk[i]*Math.pow(to,tk[i])*Math.pow(delta,dk[i])*(-ak[i]*lk[i]*Math.pow(delta,lk[i]-1))*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(-to)  );
}		
return top1;	
}

public double d2Q_amn_d2delta(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
double top1=0.0;
System.out.println("to = "+to);
System.out.println("delta = "+delta);
for(int i=0;i<=20;i++)
{
top1+=(Nk[i]*Math.pow(to,tk[i])*dk[i]*(dk[i]-1)*Math.pow(delta,dk[i]-2)*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(-to))+(Nk[i]*Math.pow(to,tk[i])*dk[i]*Math.pow(delta,dk[i]-1)*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*(-ak[i]*lk[i]*Math.pow(delta,lk[i]-1))*Math.exp(-to)) + (Nk[i]*Math.pow(to,tk[i])*dk[i]*Math.pow(delta,dk[i]-1)*(-ak[i]*lk[i]*Math.pow(delta,lk[i]-1))*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(-to)) + (((-ak[i]*lk[i]*(lk[i]-1)*Math.pow(delta,lk[i]-2)*Math.exp(-ak[i]*Math.pow(delta,lk[i]))) + (-ak[i]*lk[i]*Math.pow(delta,lk[i]-1)*(-ak[i]*lk[i]*Math.pow(delta,lk[i]-1))*Math.exp(-ak[i]*Math.pow(delta,lk[i]))))*Nk[i]*Math.pow(to,tk[i])*Math.exp(-to));
}
return top1;
}

public double d2Q_amn_ddeltadto(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
double top1=0.0;
System.out.println("to = "+to);
System.out.println("delta = "+delta);	
for(int i=0;i<=20;i++)
{
top1+=(Nk[i]*tk[i]*Math.pow(to,tk[i]-1)*dk[i]*Math.pow(delta,dk[i]-1)*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(to))+( Nk[i]*tk[i]*Math.pow(to,tk[i]-1)*Math.pow(delta,dk[i])*(-ak[i]*lk[i]*Math.pow(delta,lk[i]-1))*Math.exp(-ak[i]*Math.pow(delta,lk[i]))*Math.exp(to))+(Math.exp(to)*Nk[i]*Math.pow(to,tk[i])*dk[i]*Math.pow(delta,dk[i]-1)*Math.exp(-ak[i]*Math.pow(delta,tk[i])))+( Math.exp(to)*Nk[i]*Math.pow(to,tk[i])*Math.pow(delta,dk[i])*Math.exp(-ak[i]*Math.pow(delta,tk[i]))*(-ak[i]*tk[i]*Math.pow(delta,tk[i]-1)));
}
return top1;	
}
*/


public double Q_amn(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;
System.out.println("to = "+to);
System.out.println("delta = "+delta);
double top= ni[0]*delta*Math.pow(to,0.250) + ni[1]*delta*Math.pow(to,1.250) + ni[2]*delta*Math.pow(to,1.5) + ni[3]*delta*delta*delta*Math.pow(to,0.25) + ni[4]*delta*delta*delta*delta*delta*delta*delta*Math.pow(to,0.875) + ni[5]*delta*Math.pow(to,2.375)*Math.exp(-delta) + ni[6]*delta*delta*Math.pow(to,2.000)*Math.exp(-delta) + ni[7]*delta*delta*delta*delta*delta*Math.pow(to,2.125)*Math.exp(-delta) + ni[8]*delta*Math.pow(to,3.500)*Math.exp(-delta*delta) + ni[9]*delta*Math.pow(to,6.50)*Math.exp(-delta*delta) + ni[10]*delta*delta*delta*delta*Math.pow(to,4.75)*Math.exp(-delta*delta) +ni[11]*delta*delta*Math.pow(to,12.5)*Math.exp(-delta*delta*delta);
return top;
}

public double dQ_amn_dto(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
System.out.println("to = "+to);
System.out.println("delta = "+delta);
double top=(ni[0]*delta*0.25*Math.pow(to,-0.75)) + (ni[1]*delta*1.25*Math.pow(to,0.25)) + (ni[2]*delta*1.5*Math.pow(to,0.5)) +(ni[3]*delta*delta*delta*0.25*Math.pow(to,-0.75))  + (ni[4]*delta*delta*delta*delta*delta*delta*delta*0.875*Math.pow(to,-0.125)) +  (ni[5]*delta*2.375*Math.pow(to,1.375)*Math.exp(-delta)) + (ni[6]*delta*delta*2*to*Math.exp(-delta)) +(ni[7]*delta*delta*delta*delta*delta*2.125*Math.pow(to,1.125)*Math.exp(-delta))+(ni[8]*delta*3.5*Math.pow(to,2.5)*Math.exp(-delta*delta)) + (ni[9]*delta*6.5*Math.pow(to,5.5)*Math.exp(-delta*delta)) + (ni[10]*delta*delta*delta*delta*4.75*Math.pow(to,3.75)*Math.exp(-delta*delta)) +(ni[11]*delta*delta*12.5*Math.pow(to,11.5)*Math.exp(-delta*delta*delta)); 
return top;
}

public double d2Q_amn_d2to(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
System.out.println("to = "+to);
System.out.println("delta = "+delta);
double top=(ni[0]*delta*0.25*-0.75*Math.pow(to,-1.75)) + (ni[1]*delta*1.250*0.25*Math.pow(to,-0.75)) + (ni[2]*delta*1.5*0.5*Math.pow(to,-0.5)) +(ni[3]*delta*delta*delta*0.25*-0.75*Math.pow(to,-1.75))+(ni[4]*delta*delta*delta*delta*delta*delta*delta*0.875*-0.125*Math.pow(to,-1.125))  + (ni[5]*delta*2.375*1.375*Math.pow(to,0.375)*Math.exp(-delta)) + (ni[6]*delta*delta*2*Math.exp(-delta)) + (ni[7]*delta*delta*delta*delta*delta*2.125*1.125*Math.pow(to,0.125)*Math.exp(-delta))  + (ni[8]*delta*3.5*2.5*Math.pow(to,1.5)*Math.exp(-delta*delta))  + (ni[9]*delta*6.5*5.5*Math.pow(to,4.5)*Math.exp(-delta*delta))  +  (ni[10]*delta*delta*delta*delta*4.75*3.75*Math.pow(to,2.75)*Math.exp(-delta*delta)) + (ni[11]*delta*12.5*11.5*Math.pow(to,10.5)*Math.exp(-delta*delta*delta));
return top;
}

public double dQ_amn_ddelta(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	

System.out.println("to = "+to);
System.out.println("delta = "+delta);
double top=(ni[0]*Math.pow(to,0.250))+(ni[1]*Math.pow(to,1.25))+(ni[2]*Math.pow(to,1.5))+(ni[3]*3*delta*delta*Math.pow(to,0.25))+(ni[4]*7*delta*delta*delta*delta*delta*delta*Math.pow(to,0.875)) + ((ni[5]*Math.pow(to,2.375)*Math.exp(-delta))+(ni[5]*delta*Math.pow(to,2.375)*-Math.exp(-delta))) + ( (ni[6]*2*delta*Math.pow(to,2)*Math.exp(-delta))+(ni[6]*delta*delta*Math.pow(to,2)*-Math.exp(-delta))) + ((ni[7]*5*delta*delta*delta*delta*Math.pow(to,2.2125)*Math.exp(-delta))+(ni[7]*delta*delta*delta*delta*delta*Math.pow(to,2.125)*-Math.exp(-delta))) + ((ni[8]*Math.pow(to,3.5)*Math.exp(-delta*delta))+(ni[8]*delta*Math.pow(to,3.5)*-2*delta*Math.exp(-delta*delta)))+((ni[9]*Math.pow(to,6.5)*Math.exp(-delta*delta))+(ni[9]*delta*Math.pow(to,6.5)*-2*delta*Math.exp(-delta*delta))) + ((ni[10]*4*delta*delta*delta*Math.pow(to,4.75)*Math.exp(-delta*delta))+(ni[10]*delta*delta*delta*delta*Math.pow(to,4.75)*-2*delta*Math.exp(-delta*delta))) + ((ni[11]*2*delta*Math.pow(to,12.5)*Math.exp(-delta*delta*delta))+(ni[11]*delta*delta*Math.pow(to,12.5)*-3*delta*delta*Math.exp(-delta*delta*delta)));
return top;
}

public double d2Q_amn_d2delta(double T,double p)
{
amn=new ammonia();
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3
double delta=roamn1/roc_amn;
double to=Tc_amn/T;	
System.out.println("to = "+to);
System.out.println("delta = "+delta);
double top=(ni[3]*6*delta*Math.pow(to,0.25)) + (ni[4]*7*6*delta*delta*delta*delta*delta*Math.pow(to,0.875))+((-ni[5]*Math.pow(to,2.375)*Math.exp(-delta))+(ni[5]*Math.pow(to,2.375)*-Math.exp(-delta))+(ni[5]*delta*Math.pow(to,2.375)*Math.exp(-delta)))+((ni[6]*2*Math.pow(to,2)*Math.exp(-delta))+(ni[6]*2*delta*Math.pow(to,2)*-Math.exp(-delta))+(ni[6]*2*delta*Math.pow(to,2)*-Math.exp(-delta))+(ni[6]*delta*delta*Math.pow(to,2)*Math.exp(-delta)))+((ni[7]*20*delta*delta*delta*Math.pow(to,2.125)*Math.exp(-delta))+(ni[7]*5*delta*delta*delta*delta*Math.pow(to,2.125)*-Math.exp(-delta))+(ni[7]*5*delta*delta*delta*delta*Math.pow(to,2.125)*-Math.exp(-delta))+(ni[7]*delta*delta*delta*delta*delta*Math.pow(to,2.125)*Math.exp(-delta)))+((ni[8]*Math.pow(to,3.5)*-2*delta*Math.exp(-delta*delta))+(ni[8]*Math.pow(to,3.5)*-2*delta*Math.exp(-delta*delta))+(ni[8]*delta*Math.pow(to,3.5)*((4*delta*delta-2)*Math.exp(-delta*delta)))) + (((ni[9]*Math.pow(to,6.5)*-2*delta*Math.exp(-delta*delta)))+(ni[9]*Math.pow(to,6.5)*-2*delta*Math.exp(-delta*delta))+(ni[9]*delta*Math.pow(to,6.5)*((4*delta*delta-2)*Math.exp(-delta*delta))))+( (ni[10]*12*delta*delta*Math.pow(to,4.75)*Math.exp(-delta*delta))+(ni[10]*4*delta*delta*delta*Math.pow(to,4.75)*-2*delta*Math.exp(-delta*delta))+(ni[10]*4*delta*delta*delta*Math.pow(to,4.75)*-2*delta*Math.exp(-delta*delta))+(ni[10]*delta*delta*delta*delta*Math.pow(to,4.75)*((4*delta*delta-2)*Math.exp(-delta*delta))) )  + ((ni[11]*2*Math.pow(to,12.5)*Math.exp(-delta*delta*delta))+(ni[11]*2*delta*Math.pow(to,12.5)*-3*delta*delta*Math.exp(-delta*delta*delta))+(ni[11]*2*delta*Math.pow(to,12.5)*-3*delta*delta*Math.exp(-delta*delta*delta))+(ni[11]*delta*delta*Math.pow(to,12.5)*((9*delta*delta*delta*delta-6*delta)*Math.exp(-delta*delta*delta))));
return top;
}


public double Qr_mix(double T,double p,double x)
{
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);

double top1=0.0;
double top2=0.0;

for(int i=1;i<=5;i++)
{
top1+=a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])*Math.pow(delta,d_res[i]);	
}

for(int i=6;i<=12;i++)
{
top2+=a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])*Math.pow(delta,d_res[i]);	
}

double top=((a_res[0]*Math.pow(to,t_res[0])*Math.pow(delta,d_res[0]))+top1+x*top2+(a_res[13]*x*x*Math.exp(-Math.pow(delta,e_res[13]))*Math.pow(to,t_res[13])*Math.pow(delta,d_res[13])))*(x*(1-Math.pow(x,gamma_dep)));
return top;
}

public double dQr_mix_ddelta(double T,double p,double x)
{
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);
double top1=0.0;
double top2=0.0;

for(int i=1;i<=5;i++)
{
top1+=((a_res[i]*(-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])*Math.pow(delta,d_res[i]))+(d_res[i]*Math.pow(delta,d_res[i]-1)*a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])));	
}

for(int i=6;i<=12;i++)
{
top2+=((a_res[i]*(-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])*Math.pow(delta,d_res[i]))+(d_res[i]*Math.pow(delta,d_res[i]-1)*a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])));	
}

double top=((a_res[0]*Math.pow(to,t_res[0])*d_res[0]*Math.pow(delta,d_res[0]-1))+(top1)+(x*top2)+(a_res[13]*x*x*(-e_res[13]*Math.pow(delta,e_res[13]-1))*Math.exp(-Math.pow(delta,e_res[13]))*Math.pow(to,t_res[13])*Math.pow(delta,d_res[13]))+(d_res[13]*Math.pow(delta,d_res[13]-1)*a_res[13]*x*x*Math.exp(-Math.pow(delta,e_res[13]))*Math.pow(to,t_res[13])))*(x*(1-Math.pow(x,gamma_dep)));
return top;
}


public double dQr_mix_dto(double T,double p,double x)
{
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);
double top1=0.0;
double top2=0.0; 

for(int i=1;i<=5;i++)
{
top1+=(a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*t_res[i]*Math.pow(to,t_res[i]-1)*Math.pow(delta,d_res[i]));
}
	
for(int i=6;i<=12;i++)
{
top2+=(a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*t_res[i]*Math.pow(to,t_res[i]-1)*Math.pow(delta,d_res[i]));
}	
	
double top=((a_res[0]*t_res[0]*Math.pow(to,t_res[0]-1)*Math.pow(delta,d_res[0]))+(top1)+(x*top2)+(a_res[13]*x*x*Math.exp(-Math.pow(delta,e_res[13]))*t_res[13]*Math.pow(to,t_res[13]-1)*Math.pow(delta,d_res[13])))*((x*(1-Math.pow(x,gamma_dep))));	
return top;	
}

public double d2Qr_mix_dto2(double T,double p,double x)
{
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);
double top1=0.0;
double top2=0.0; 
	
	
for(int i=1;i<=5;i++)
{
top1+=(a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*t_res[i]*(t_res[i]-1)*Math.pow(to,t_res[i]-2)*Math.pow(delta,d_res[i]));
}	
	
for(int i=6;i<=12;i++)
{
top2+=(a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*t_res[i]*(t_res[i]-1)*Math.pow(to,t_res[i]-2)*Math.pow(delta,d_res[i]));
}	

double top=(a_res[0]*t_res[0]*(t_res[0]-1)*Math.pow(to,t_res[0]-2)*Math.pow(delta,d_res[0]))+(top1)+(x*top2)+(a_res[13]*x*x*Math.exp(-Math.pow(delta,e_res[13]))*t_res[13]*(t_res[13]-1)*Math.pow(to,t_res[13]-2)*Math.pow(delta,t_res[13]))*((x*(1-Math.pow(x,gamma_dep))));
return top;
}

public double d2Qr_mix_ddelta2(double T,double p,double x)
{
st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);
double top1=0.0;
double top2=0.0; 

for(int i=1;i<=5;i++)
{
top1+=((((-e_res[i]*(e_res[i]-1)*Math.pow(delta,e_res[i]-2)*Math.exp(-Math.pow(delta,e_res[i])))+((-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,-e_res[i]))*(-e_res[i]*Math.pow(delta,e_res[i]-1))))*(a_res[i]*Math.pow(to,t_res[i])*Math.pow(delta,d_res[i])))+(d_res[i]*Math.pow(delta,d_res[i]-1)*Math.pow(to,t_res[i])*a_res[i]*(-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,e_res[i])))+(d_res[i]*(d_res[i]-1)*Math.pow(delta,d_res[i]-2)*a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i]))+( d_res[i]*Math.pow(delta,d_res[i]-1)*a_res[i]*(-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])));
}	

for(int i=6;i<=12;i++)
{
top2+=((((-e_res[i]*(e_res[i]-1)*Math.pow(delta,e_res[i]-2)*Math.exp(-Math.pow(delta,e_res[i])))+((-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,-e_res[i]))*(-e_res[i]*Math.pow(delta,e_res[i]-1))))*(a_res[i]*Math.pow(to,t_res[i])*Math.pow(delta,d_res[i])))+(d_res[i]*Math.pow(delta,d_res[i]-1)*Math.pow(to,t_res[i])*a_res[i]*(-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,e_res[i])))+(d_res[i]*(d_res[i]-1)*Math.pow(delta,d_res[i]-2)*a_res[i]*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i]))+( d_res[i]*Math.pow(delta,d_res[i]-1)*a_res[i]*(-e_res[i]*Math.pow(delta,e_res[i]-1))*Math.exp(-Math.pow(delta,e_res[i]))*Math.pow(to,t_res[i])));
}
double top=((a_res[0]*Math.pow(to,t_res[0])*d_res[0]*(d_res[0]-1)*Math.pow(delta,d_res[0]-2))+(top1)+(x*top2)+(a_res[13]*x*x*(-e_res[13]*Math.pow(delta,e_res[13]-1))*Math.exp(-Math.pow(delta,e_res[13]))*Math.pow(to,t_res[13])*d_res[13]*Math.pow(delta,d_res[13]-1))+((((-e_res[13]*(e_res[13]-1)*Math.pow(delta,e_res[13]-2))*Math.exp(-Math.pow(delta,e_res[13])))+((-e_res[13]*Math.pow(delta,e_res[13]-1))*Math.exp(-Math.pow(delta,e_res[13]))*(-e_res[13]*Math.pow(delta,e_res[13]-1))))*(a_res[13]*x*x*Math.pow(to,t_res[13])*Math.pow(delta,d_res[13]))) + (d_res[13]*(d_res[13]-1)*Math.pow(delta,d_res[13]-2)*a_res[13]*x*x*Math.exp(-Math.pow(delta,e_res[13]))*Math.pow(to,t_res[13]))+(d_res[13]*Math.pow(delta,d_res[13]-1)*a_res[13]*x*x*(-e_res[13]*Math.pow(delta,e_res[13]-1))*Math.exp(-Math.pow(delta,e_res[13]))*Math.pow(to,t_res[13])))*((x*(1-Math.pow(x,gamma_dep))));
return top;
}

public double totaldQdto(double T,double p,double x)
{
return ((1-x)*dQres_to_water(T,p))+(x*(dQ_amn_dto(T,p)))+dQr_mix_dto(T,p,x);	


}

public double totald2Qdtoto(double T,double p,double x)
{
return (dQres_to_to_water(T,p)*(1-x))+(x*d2Q_amn_d2to(T,p))+ d2Qr_mix_dto2(T,p,x);
}

public double totaldQddelta(double T,double p,double x)
{
return (dQres_del_water(T,p)*(1-x))+(x*dQ_amn_ddelta(T,p))+dQr_mix_ddelta(T,p,x);	
}

public double totald2Qddeltadelta(double T,double p,double x)
{
return (d2Qres_del_del_water(T,p)*(1-x))+(x*d2Q_amn_d2delta(T,p))+d2Qr_mix_ddelta2(T,p,x);
}

public double enthalpy(double T,double p,double x)
{
double[] aa=ideal_gas_part1(T,p,x);
double to0=aa[0];
double delta0=aa[1];

st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);	
double Rm=8.314*(((1-x)*0.018015268)+(x*0.01703026));
return  (1+(delta*totaldQddelta(T,p,x))+(to0*dQ_ideal_to(T,p,x))+(to*totaldQdto(T,p,x)))*Rm*T;	
}

public double entropy(double T,double p,double x)
{
double[] aa=ideal_gas_part1(T,p,x);
double to0=aa[0];
double delta0=aa[1];

st=new steam();
amn=new ammonia();

double[] proper=st.property_SI("tp",T-273,p/100);//in  C and bar	
double rowat1=1/proper[2];//kg/m3
double[] amn_prop2=amn.property_SI("tp",T-273,p);
double roamn1=1/amn_prop2[2];//kg/m3	
double ro=((rowat1*(1-x))+(roamn1*x));	
	
double Tc_12=0.5*kt_red*(Tc_water+Tc_amn);
double Vc_12=0.5*kv_red*((1/roc_water)+(1/roc_amn));	
double Tn=((1-x)*(1-x)*Tc_water)+(x*x*Tc_amn)+(2*x*(1-Math.pow(x,alfa_red))*Tc_12);
double Vn=((1-x)*(1-x)*(1/roc_water))+(x*x*(1/roc_amn))+(2*x*(1-Math.pow(x,beta_red))*Vc_12);
	
double to=Tn/T;
double delta=Vn/(1/ro);	
double Rm=8.314*(((1-x)*0.018015268)+(x*0.01703026));// j/kgk

return (to0*dQ_ideal_to(T,p,x))+((to*totaldQdto(T,p,x)))-dQ_ideal(T,p,x);
	
	
}



public static void main(String args[])
{
double T=273-20;//K
double p=100;//kpa
double x=0.8;	
wa_mix wa=new wa_mix();	
double[] val=wa.getvalues(T,p);
double qamn=wa.Q_amn(T,p);
//System.out.println(qamn);	                     

double[] ipart=wa.ideal_gas_part1(T,p,x);
double prp=wa.enthalpy(T,p,x);
         // =wa.ideal_gas_part1( T, p, x)
System.out.println(prp);	

	
}






}







// quake //   (Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))) 

// teta //    ((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))

// triangle //  ((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54])))

// triangle^bi//    (Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]))

// d(quake)/d(delta)//   (-2*C_water[i-54]*(delta-1)*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))

// d(triangle^bi)/d(delta) //   (b_water[i-54]*Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1)))))

// d2(quake)/d(delta)2 //  ((2*C_water[i-54]*(delta-1)*(delta-1)-1)*2*C_water[i-54]*Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))

// d2(triangle^bi)/d(delta)2 // b_water[i-54]*(   (Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)*((((1/(delta-1))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1)))))  + (delta-1)*(delta-1)*((4*B_water[i-54]*a_water[i-54]*(a_water[i-54]-1)*Math.pow((delta-1)*(delta-1),a_water[i-54]-2)) + ( 2*A_water[i-54]*A_water[i-54]*(1/beta2_water[i-54])*(1/beta2_water[i-54])*(Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])-1))*(Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])-1)))  +   (A_water[i-54]*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*(4/beta2_water[i-54])*(1/(2*beta2_water[i-54])-1)*Math.pow((delta-1)*(delta-1),(1/(2*beta2_water[i-54])-2))) )))) + (     (b_water[i-54]-1)* Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-2)*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1))))  )  )

// d2(triangle)/d(delta)2 //    (((1/(delta-1))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1)))))  + (delta-1)*(delta-1)*((4*B_water[i-54]*a_water[i-54]*(a_water[i-54]-1)*Math.pow((delta-1)*(delta-1),a_water[i-54]-2)) + ( 2*A_water[i-54]*A_water[i-54]*(1/beta2_water[i-54])*(1/beta2_water[i-54])*(Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])-1))*(Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])-1)))  +   (A_water[i-54]*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*(4/beta2_water[i-54])*(1/(2*beta2_water[i-54])-1)*Math.pow((delta-1)*(delta-1),(1/(2*beta2_water[i-54])-2))) ))

// d(triangle^bi)/d(to) //    (-2*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)))

// d(quake)/d(to) //    (-2*D_water[i-54]*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1)))) 

// d(triangle^bi)/d(to)2 //  ((2*b_water[i-54]*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1)))+(4*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(b_water[i-54]-1)*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-2)) ))

// d2(quake)/d(to)2 //    ((2*D_water[i-54]*(to-1)*(to-1)-1)*(2*D_water[i]*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))) 

// d2(quake)/(d(delta)*d(to)) //    (4*C_water[i-54]*D_water[i-54]*(delta-1)*(to-1)*(Math.exp(-C_water[i-54]*(delta-1)*(delta-1)-D_water[i-54]*(to-1)*(to-1))))

// d2(triangle^bi)/(d(delta)*d(to)) // ((-A_water[i-54]*b_water[i-54]*(2/beta2_water[i-54])*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-1))*(delta-1)*Math.pow((delta-1)*(delta-1),(1/(2*beta2_water[i-54])-1)))-(2*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*b_water[i-54]*(b_water[i-54]-1)*(Math.pow(((((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54]))))*((1-to)+(A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))))+(B_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]))),b_water[i-54]-2))*((delta-1)*((A_water[i-54]*((1-to)+A_water[i-54]*Math.pow((delta-1)*(delta-1),1/(2*beta2_water[i-54])))*(2/beta2_water[i-54])*Math.pow((delta-1)*(delta-1),(1/(2/beta2_water[i-54])-1)))+(2*B_water[i-54]*a_water[i-54]*Math.pow((delta-1)*(delta-1),a_water[i-54]-1)))) ))