示例#1
0
void CPengRobinson::SetTDState_hs (su2double h, su2double s ) {

	su2double T, fv, sqrt2=sqrt(2.0), A;
	su2double f, v, atanh;
	su2double x1, x2, xmid, dx, fx1, fx2, fmid, rtb;
	su2double toll = 1e-9, FACTOR=0.2;
	su2double cons_s, cons_h;
	unsigned short countrtb=0, NTRY=10, ITMAX=100;

	A = Gas_Constant / Gamma_Minus_One;
	T = h*Gamma_Minus_One/Gas_Constant/Gamma;
	v = exp(-1/Gamma_Minus_One*log(T) + s/Gas_Constant);


	if (Zed<0.9999) {
		x1 = Zed*v;
		x2 = v;

	} else{
		x1 = 0.2*v;
		x2 = v;
	}


	T = T_v_h(x1, h);
  
  atanh = (log(1.0+( b*sqrt2 / (x1 + b))) - log(1.0-( b*sqrt2 / (x1 + b))))/2.0;
	fv = atanh;
  
	fx1 = A*log(T) + Gas_Constant*log(x1 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
	T = T_v_h(x2, h);
  
  atanh = (log(1.0+( b*sqrt2 / (x2 + b))) - log(1.0-( b*sqrt2 / (x2 + b))))/2.0;
	fv = atanh;
  
	fx2 = A*log(T) + Gas_Constant*log(x2 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;

	// zbrac algorithm NR

	for (int j=1; j<=NTRY; j++) {
		if (fx1*fx2 > 0.0) {
			if (fabs(fx1) < fabs(fx2)) {
				x1 += FACTOR*(x1-x2);
				T = T_v_h(x1, h);
        atanh = (log(1.0+( b*sqrt2/(x1 + b))) - log(1.0-( b*sqrt2/(x1 + b))))/2.0;
				fv = atanh;
				fx1 = A*log(T) + Gas_Constant*log(x1 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
			} else{
				x2 += FACTOR*(x2-x1);
				T = T_v_h(x2, h);
        atanh = (log(1.0+( b*sqrt2/(x2 + b))) - log(1.0-( b*sqrt2/(x2 + b))))/2.0;
        fv = atanh;
				fx2 = A*log(T) + Gas_Constant*log(x2 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
			}
		}
	}

	// rtbis algorithm NR

	f=fx1;
	fmid=fx2;
	if (f*fmid >= 0.0) {
		cout<< "Root must be bracketed for bisection in rtbis"<< endl;
		SetTDState_rhoT(Density, Temperature);
	}
	rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2);
	do{
		xmid=rtb+(dx *= 0.5);
		T = T_v_h(xmid, h);
    atanh = (log(1.0+( b* sqrt2/(xmid + b))) - log(1.0-( b* sqrt2/(xmid + b))))/2.0;
		fv = atanh;
		fmid= A*log(T) + Gas_Constant*log(xmid - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;

		if (fmid <= 0.0) rtb=xmid;
		countrtb++;
	}while(abs(fmid) > toll && countrtb<ITMAX);

	v = xmid;
	if (countrtb==ITMAX) {
		cout <<"Too many bisections in rtbis" << endl;
//			do{
//          atanh = (log(1.0+( b/v* sqrt2/(1 + b/v))) - log(1.0-( b/v* sqrt2/(1 + b/v))))/2.0;
//					fv = atanh;
//					T=T_v_h(v, h);
//					f = A*log(T) + Gas_Constant*log(v - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
//					f1= Gas_Constant/(v-b)+ a*sqrt(alpha2(T)) *k/(sqrt(T*TstarCrit)*(v*v - b*b - 2*v*b));
//					dv= f/f1;
//					v-= dv;
//					countnw++;
//			}while(abs(f/x2) > toll && countnw<ITMAXNW);
//
//		} else{
	}
	if (v!=v) {
		cout <<"not physical solution found, h and s input " << h << " "<< s << endl;
		SetTDState_rhoT(Density, Temperature);
	}

	T=T_v_h(v, h);
	SetTDState_rhoT(1/v, T);

	// consistency check
	cons_h= abs(((StaticEnergy + Pressure/Density) - h)/h);
	cons_s= abs((Entropy-s)/s);

	if (cons_h >1e-4 || cons_s >1e-4) {
		cout<< "TD consistency not verified in hs call"<< endl;
			 //cout <<"Before  "<< h <<" "<< s << endl;
			 //cout <<"After  "<< StaticEnergy + Pressure/Density <<" "<< Entropy << fmid <<" "<< f<< " "<< countrtb<<" "<< countnw<< endl;
			 //getchar();
	}
}
示例#2
0
void CPengRobinson::SetTDState_Ps (su2double P, su2double s){

	su2double T, rho, v, cons_P, cons_s, fv, A, atanh;
	su2double x1,x2, fx1, fx2,f, fmid, rtb, dx, xmid, sqrt2=sqrt(2.0);
	su2double toll = 1e-5, FACTOR=0.2;
	unsigned short count=0, NTRY=10, ITMAX=100;

	A = Gas_Constant / Gamma_Minus_One;
	T   = exp(Gamma_Minus_One/Gamma* (s/Gas_Constant +log(P) -log(Gas_Constant)) );
	v = (T*Gas_Constant)/P;

	if(Zed<0.9999){
		x1 = Zed*v;
		x2 = v;

	}else{
		x1 = 0.2*v;
		x2 = v;
	}
	T = T_P_rho(P,1.0/x1);
  
  atanh = (log(1.0 + ( b*sqrt2 / (x1 + b) )) - log(1.0-( b*sqrt2 / (x1 + b) )))/2.0;
  fv = atanh;
  
	fx1 = A*log(T) + Gas_Constant*log(x1 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
	T = T_P_rho(P,1.0/x2);
  
  atanh = (log(1.0 + ( b*sqrt2 / (x2 + b) )) - log(1.0-( b*sqrt2 / (x2 + b) )))/2.0;
  fv = atanh;

	fx2 = A*log(T) + Gas_Constant*log(x2 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;

	// zbrac algorithm NR

	for (int j=1;j<=NTRY;j++) {
		if (fx1*fx2 > 0.0){
			if (fabs(fx1) < fabs(fx2)){
				x1 += FACTOR*(x1-x2);
				T = T_P_rho(P,1.0/x1);
        
        atanh = (log(1.0 + ( b*sqrt2 / (x1 + b) )) - log(1.0-( b*sqrt2 / (x1 + b) )))/2.0;
        fv = atanh;
        
				fx1 = A*log(T) + Gas_Constant*log(x1 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
			}else{
				T = T_P_rho(P,1.0/x2);
        
        atanh = (log(1.0 + ( b*sqrt2 / (x2 + b) )) - log(1.0-( b*sqrt2 / (x2 + b) )))/2.0;
        fv = atanh;

				fx2 = A*log(T) + Gas_Constant*log(x2 - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
				}
		}
	}


	// rtbis algorithm NR

	f=fx1;
	fmid=fx2;
	if (f*fmid >= 0.0){
		cout<< "Root must be bracketed for bisection in rtbis"<< endl;
		SetTDState_rhoT(Density, Temperature);
	}
	rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2);
	do{
		xmid=rtb+(dx *= 0.5);
		T = T_P_rho(P,1.0/xmid);
    
    atanh = (log(1.0 + ( b*sqrt2 / (xmid + b) )) - log(1.0-( b*sqrt2 / (xmid + b) )))/2.0;
    fv = atanh;
    
		fmid = A*log(T) + Gas_Constant*log(xmid - b) - a*sqrt(alpha2(T)) *k*fv/(b*sqrt2*sqrt(T*TstarCrit)) - s;
		if (fmid <= 0.0) rtb=xmid;
		count++;
		}while(abs(fmid) > toll && count<ITMAX);

		if(count==ITMAX){
			cout <<"Too many bisections in rtbis" << endl;
		}

	rho = 1.0/xmid;
	T = T_P_rho(P, rho);
	SetTDState_rhoT(rho, T);
//	cout << xmid << " "<< T<< " "<< Pressure<< " "<< P << " "<< Entropy << " "<< s <<endl;

	cons_P= abs((Pressure -P)/P);
	cons_s= abs((Entropy-s)/s);

	if(cons_P >1e-3 || cons_s >1e-3){
		cout<< "TD consistency not verified in hs call"<< endl;
	}

}
示例#3
0
void CVanDerWaalsGas::SetTDState_hs (double h, double s ) {

    double v, T, rho, f, fmid, rtb;
    double x1,x2,xmid, dx, fx1, fx2;
    double toll = 1e-5, FACTOR=0.2;
    unsigned short count=0, NTRY=10, ITMAX=100;
    double cons_s, cons_h;

//    cout <<"Before  "<< h <<" "<< s << endl;

    T = 1.0*h*Gamma_Minus_One/Gas_Constant/Gamma;
    v =exp(-1/Gamma_Minus_One*log(T) + s/Gas_Constant);
    if (Zed<0.9999) {
    	x1 = Zed*v;
    	x2 = v;

    } else{
    	x1 = 0.5*v;
    	x2 = v;
    }
    fx1 = log(x1-b) - s/Gas_Constant + log((h+ 2*a/x1)/Gas_Constant/(1/Gamma_Minus_One+ x1/(x1-b)))/Gamma_Minus_One;
    fx2 = log(x2-b) - s/Gas_Constant + log((h+ 2*a/x2)/Gas_Constant/(1/Gamma_Minus_One+ x2/(x2-b)))/Gamma_Minus_One;

    // zbrac algorithm NR

    for (int j=1; j<=NTRY; j++) {
    	if (fx1*fx2 > 0.0) {
    		if (fabs(fx1) < fabs(fx2)) {
    			x1 += FACTOR*(x1-x2);
    			fx1 = log(x1-b) - s/Gas_Constant + log((h+ 2*a/x1)/Gas_Constant/(1/Gamma_Minus_One+ x1/(x1-b)))/Gamma_Minus_One;
    		} else{
    			x2 += FACTOR*(x2-x1);
    			fx2 = log(x2-b) - s/Gas_Constant + log((h+ 2*a/x2)/Gas_Constant/(1/Gamma_Minus_One+ x2/(x2-b)))/Gamma_Minus_One;
    			}
    	}
    }


    // rtbis algorithm NR

	f=fx1;
	fmid=fx2;
	if (f*fmid >= 0.0) {
		cout<< "Root must be bracketed for bisection in rtbis"<< endl;
		SetTDState_rhoT(Density, Temperature);
	}
	rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2);
	do{
		xmid=rtb+(dx *= 0.5);
		fmid= log(xmid-b) - s/Gas_Constant + log((h+ 2*a/xmid)/Gas_Constant/(1/Gamma_Minus_One+ xmid/(xmid-b)))/Gamma_Minus_One;
		if (fmid <= 0.0) rtb=xmid;
		count++;
		}while(abs(fmid) > toll && count<ITMAX);

		v = xmid;
		if (count==ITMAX) {
			cout <<"Too many bisections in rtbis" << endl;
		}


	rho = 1/v;
	T= (h+ 2*a/v)/Gas_Constant/(1/Gamma_Minus_One+ v/(v-b));
	SetTDState_rhoT(rho, T);

	cons_h= abs(((StaticEnergy + Pressure/Density) - h)/h);
	cons_s= abs((Entropy-s)/s);

	if (cons_h >1e-3 or cons_s >1e-3) {
		cout<< "TD consistency not verified in hs call"<< endl;
	}

//	cout <<"After  "<< StaticEnergy + Pressure/Density <<" "<< Entropy<<" "<< fmid <<" "<< f<< " "<< count<< endl;




//	T= (h+ 2*a/v)/Gas_Constant/(1/Gamma_Minus_One+ v/(v-b));
//	do{
//		f=  log(v-b) - s/Gas_Constant + log(T)/Gamma_Minus_One;
//		f1= 1.0/(v-b);
//		dv= f/f1;
//		v-= 1.0*dv;
//		T= (h+ 2*a/v)/Gas_Constant/(1/Gamma_Minus_One+ v/(v-b));
//		count++;
//	}while(abs(dv/v) > toll && count < nmax);





}