public class otto
{
Gas g;
double TT1[],ss1[];
double TT2[],ss2[];
double W12,W34,Wnet,Q23,Q41,eta,T1,P1,T2,P2,T3,P3,T4,P4;
public otto(double m,double T1,double P1, double compressionratio, double T3,double etacompression,double etaexpansion)
{ TT1=new double[24];ss1=new double[24];
  TT2=new double[3];ss2=new double[3];
g=new Gas("air");
g.mole=false;
// 1 
double a1[]=g.property(T1,P1);
double h1=a1[3];
double u1=a1[4];
double s1=a1[5];
double v1=a1[2];
double vr1=a1[18];
System.out.println("p1="+P1+"t1="+T1+"h1="+h1+"u1="+u1+"s1="+s1+"vr1="+vr1);
double vr2=vr1/compressionratio;
double v2=v1/compressionratio;
double T2i=g.Tvi(vr2,v2);
//double P2=P1*T2i/T1*v1/v2;
double P2=g.P(T2i,v2);
double a2[]=g.property(T2i,P2);
double h2i=a2[3];
double u2i=a2[4];
double s2i=a2[5];
double Cp2i=a2[9];
System.out.println("p2="+P2+"t2="+T2i+"h2="+h2i+"u2i="+u2i+"s2="+s2i+"vr2="+vr2);

double pr1=a1[17];
double h2=h1+(h2i-h1)/etacompression;
double T2=T2i+(h2-h2i)/Cp2i;
a2=g.property(T2,P2);
double s2=a2[5];
double u2=a2[4];
double P3=P2*T3/T2;
TT1[0]=T1;ss1[0]=s1;
TT1[1]=T2;ss1[1]=s2;
System.out.println("p2="+P2+"t2="+T2+"h2="+h2+"u2="+u2+"s2="+s2+"vr2="+vr2);

double a3[]=g.property(T3,P3);
double vr3=a3[18];
double s3=a3[5];
double h3=a3[3];
double v3=a3[2];
double u3=a3[4];
System.out.println("p3="+P3+"t3="+T3+"h3="+h3+"u3="+u3+"s3="+s3+"vr2="+vr3);
double vr4=vr3*compressionratio;
double T4i=g.Tvi(vr4,v1);

double P4i=P1*T4i/T1;
double a4[]=g.property(T4i,P4i);
double v4i=a4[2];
double s4i=a4[5];
double h4i=a4[3];
double u4i=a4[4];
double Cp4i=a4[9];
System.out.println("p4i="+P4i+"t4i="+T4i+"h4i="+h4i+"u4i="+u4i+"s4i="+s4i);

double h4=h3-(h3-h4i)*etaexpansion;
double T4=T4i+(h4-h4i)/Cp4i;
double P4=P1*T4/T1;
a4=g.property(T4,P4);
double v4=a4[2];
double s4=a4[5];
double u4=a4[4];
System.out.println("p4="+P4+"t4="+T4+"h4="+h4+"u4="+u4+"s4="+s4+"vr4="+vr4);

double PP=0.0;
for(int i=0;i<10;i++)
{TT1[i+2]=T2+i*(T3-T2)/10;
 PP=P2*TT1[i+2]/T2;
 a3=g.property(TT1[i+2],PP);
 ss1[i+2]=a3[5];
}
TT1[12]=T3;ss1[12]=s3;
TT1[13]=T4;ss1[13]=s4;

TT2[0]=T1;ss2[0]=s1;
TT2[1]=T2i;ss2[1]=s2i;
TT2[2]=T2;ss2[2]=s2;

for(int i=0;i<10;i++)
{TT1[i+14]=T4-i*(T4-T1)/10;
 PP=P4*TT1[i+14]/T4;
 a3=g.property(TT1[i+14],PP);
 ss1[i+14]=a3[5];
}
TT1[23]=T1;
ss1[23]=s1;
W12=m*(u2-u1);
W34=m*(u3-u4);
Wnet=W34-W12;
Q23=m*(u3-u2);
Q41=m*(u4-u1);
Wnet=W34-W12;
eta=Wnet/Q23;
this.T1=T1;
this.P1=P1;
this.T2=T2;
this.P2=P2;
this.T3=T3;
this.P3=P3;
this.T4=T4;
this.P4=P4;
double aa[][]={ss1,TT1};
Text.printT(aa);
Plot pp=new Plot(ss1,TT1);
//pp.addData(ss2,TT2);
pp.plot();
}
public String toString()
{
String s=" Otto cycle \n";
s+="W12 = "+W12+" kJ \n";
s+="W34 = "+W34+" kJ\n";	
s+="Wnet = "+Wnet+" kW\n";
s+="Q23 = "+Q23+" kJ\n";
s+="Q41 = "+Q41+" kJ \n";
s+="Cycle efficiency eta  = "+eta+"\n";
return s;
}

}