我正在尝试传递aF变量。但是在调试时,它显示的值为0.任何想法?下面是我正在使用的代码(更新:我包含了整个代码)。
import java.util.ArrayList;
import java.util.List;
public class EOS {
//defining constants, input variables
public static final double GAS_CONSTANT = 8.3144598; //J K-1 mol-1
double criticalTemperature;
double criticalPressure;
double temperature;
double pressure;
double molecularWeight;
public EOS(double criticalTemperature, double criticalPressure, double temperature, double pressure, double molecularWeight) {
this.criticalTemperature = criticalTemperature;
this.criticalPressure = criticalPressure;
this.temperature = temperature;
this.pressure = pressure;
this.molecularWeight = molecularWeight;
}
// calculation of A* and B* (values of "a" and "b" will be provided by subclasses)
public double aStar(double a) {
return a * pressure / (Math.pow(GAS_CONSTANT, 2) * Math.pow(temperature, 2));
}
public double bStar(double b) {
return b * pressure / (GAS_CONSTANT * temperature);
}
//calculation of Z Value. The idea is to form the cubic function of Z as follow:
public List<Double> calculateZ(double aStar, double bStar, double uValue, double wValue) {
List<Double> solution = new ArrayList<>();
double a, b, c, q, r, d;
a = -1 - bStar + uValue * bStar;
b = aStar + wValue * Math.pow(bStar, 2) - uValue * bStar - uValue * Math.pow(bStar, 2);
c = - bStar * aStar - wValue * Math.pow(bStar, 2) - wValue * Math.pow(bStar, 3);
q = (3*b-Math.pow(a, 2))/3;
r = (2*Math.pow(a, 3)-9*a*b+27*c)/27;
d = (Math.pow(q, 3)/27) + (Math.pow(r, 2)/4);
if (d == 0) {
double x1 = 2*Math.pow(-r/2, 1/3) -(a/3);
double x2 = -2*Math.pow(-r/2, 1/3) -(a/3);
double x3 = x2;
double[] temp = {x1, x2, x3};
for (int i = 0; i < temp.length; i++) {
if (temp[i] > 0) {
solution.add(temp[i]);
}
}
} else if (d > 0) {
double x1 = Math.pow((-r/2)+Math.pow(d, 0.5),1/3)+Math.pow((-r/2)+Math.pow(d, 0.5),1/3)-(a/3);
solution.add(x1);
} else {
double theta = Math.acos((3*r/(2*q))*Math.sqrt(-3/q));
double x1 = 2*Math.sqrt(-q/3)*Math.cos(theta/3)-(a/3);
double x2 = 2*Math.sqrt(-q/3)*Math.cos((theta+2*Math.PI)/3)-(a/3);
double x3 = 2*Math.sqrt(-q/3)*Math.cos((theta+4*Math.PI)/3)-(a/3);
double[] temp = {x1, x2, x3};
for (int i = 0; i < temp.length; i++) {
if (temp[i] > 0) {
solution.add(temp[i]);
}
}
}
return solution;
}
}
这里是子类
import java.util.Collections;
public class Soave extends EOS {
public Soave (double aFactor, double criticalTemperature, double criticalPressure, double temperature, double pressure, double molecularWeight) {
super(criticalTemperature, criticalPressure, temperature, pressure, molecularWeight);
this.aF = aFactor;
this.fW = 0.48+1.574*aFactor-0.176*Math.pow(aFactor, 2);
}
double aF;
double uValue = 1;
double wValue = 0;
double fW;
public double reducedTemperature = temperature / criticalTemperature;
public double bValue = 0.08664*GAS_CONSTANT*criticalTemperature/criticalPressure;
public double aValue() {
double term1 = 1 - Math.sqrt(reducedTemperature);
double term2 = 1+fW*term1;
double term3 = Math.pow(term2, 2.0);
double term4 = Math.pow(GAS_CONSTANT, 2)*Math.pow(criticalTemperature, 2.0);
return 0.42748*term3*term4/criticalPressure;
}
public double aStarValue = aStar(aValue());
public double bStarValue = bStar(bValue);
public double gasZValue = Collections.max(calculateZ(aStarValue, bStarValue, uValue, wValue));
public double liquidZValue = Collections.min(calculateZ(aStarValue, bStarValue, uValue, wValue));
public double gasDensity = pressure * molecularWeight / (1000 * gasZValue * GAS_CONSTANT * temperature);
public double liquidDensity = pressure * molecularWeight / (1000 * liquidZValue * GAS_CONSTANT * temperature);
}
所以现在当我们为以下输入创建一个Soave实例时,我们应该为liquidDensity获取值568.77
double p = 500000;
double t = 318.15;
double pC = 3019900;
double tC = 507.9;
double aF = 0.299;
double mW = 86;
Soave soave = new Soave(aF, tC, pC, t, p, mW);
System.out.println(soave.liquidDensity);
答案 0 :(得分:0)
这肯定是通过论证或阅读它的问题。查看传递值的代码段(最有可能传递0,读取时将其设为0非常“难”)。如果您仍然找不到错误,请在此处发布正确的代码。
答案 1 :(得分:0)
在实际设置aF的值之前设置fW变量,因此它使用原始double的默认值为0.
为fW创建一个getter来进行计算,或者为构造函数块中的fW计算更多。
所以要么你喜欢这样:
public class Soave extends EOS {
public double aF;
double uValue = 1;
double wValue = 0;
public double fW;
public Soave (double aFactor, double criticalTemperature, double criticalPressure, double temperature, double pressure, double molecularWeight) {
super(criticalTemperature, criticalPressure, temperature, pressure, molecularWeight);
this.aF = aFactor;
fW = 0.48+1.574*aF-0.176*Math.pow(aF, 2); //This will give you the proper number.
}
或者添加一个getter并直接进行计算(然后不需要类中的fW变量)。
public double getfWValue() {
return 0.48+1.574*aF-0.176*Math.pow(aF, 2);
}
如果是这样,那么直接在你的print语句中使用它。
System.out.println(soave.getfWValue());