|
1 | 1 | package com.rae.formicapi.thermal_utilities; |
2 | 2 |
|
| 3 | +import com.rae.formicapi.FormicAPI; |
3 | 4 | import net.createmod.catnip.data.Couple; |
4 | | -import oshi.util.tuples.Pair; |
5 | 5 |
|
6 | 6 | import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
7 | 8 | import java.util.List; |
8 | 9 |
|
9 | 10 | public abstract class CubicEOS implements EquationOfState{ |
| 11 | + |
| 12 | + public static final double T_REF = 298.15; |
| 13 | + public static final double P_REF = 101325.0; |
| 14 | + protected final double M; |
| 15 | + protected final double Tc; // Critical temperature [K] |
| 16 | + protected final double Pc; // Critical pressure [Pa] |
| 17 | + |
| 18 | + |
| 19 | + protected CubicEOS(double m, double Tc, double Pc) { |
| 20 | + this.M = m; |
| 21 | + this.Tc = Tc; |
| 22 | + this.Pc = Pc; |
| 23 | + } |
| 24 | + |
10 | 25 | @Override |
11 | 26 | public abstract double pressure(double temperature, double volumeMolar); |
12 | 27 |
|
13 | 28 |
|
14 | | - abstract Couple<Double> findSpinodalPoints(double T); |
| 29 | + public abstract List<Double> findSpinodalPoints(double T); |
| 30 | + public List<Double> findSpinodalPoints(double T, double vMin, double vMax) { |
| 31 | + List<Double> spinodals = new ArrayList<>(); |
| 32 | + |
| 33 | + double previousV = vMin; |
| 34 | + double previousP = pressure(T, previousV); |
| 35 | + double previousSlope = Double.NaN; |
| 36 | + |
| 37 | + for (double V = vMin; V <= vMax; V *= 1.05) { |
| 38 | + double P = pressure(T, V); |
| 39 | + double slope = (P - previousP) / (V - previousV); |
| 40 | + |
| 41 | + if (!Double.isNaN(previousSlope) && previousSlope * slope <= 0) { |
| 42 | + // slope changed sign → inflection/spinodal |
| 43 | + spinodals.add(V); |
| 44 | + } |
| 45 | + |
| 46 | + previousV = V; |
| 47 | + previousP = P; |
| 48 | + previousSlope = slope; |
| 49 | + } |
| 50 | + |
| 51 | + return spinodals; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public double volumeMolar(double T, double P, double vaporFraction) { |
| 56 | + double[] roots = getZFactors(T, P); |
| 57 | + |
| 58 | + if (roots.length == 0) { |
| 59 | + throw new IllegalStateException("No valid real roots for Z at T = "+ T+ " P = "+ P); |
| 60 | + } |
| 61 | + |
| 62 | + double Z; |
| 63 | + if (vaporFraction <= 0.0) { |
| 64 | + Z = roots[0]; // Liquid-like root |
| 65 | + } else if (vaporFraction >= 1.0) { |
| 66 | + Z = roots[roots.length - 1]; // Vapor-like root |
| 67 | + } else if (roots.length >= 2) { |
| 68 | + double saturationPressure = saturationPressure(T); |
| 69 | + // Interpolate molar volume, not Z |
| 70 | + double Zl = roots[0]; |
| 71 | + double Zv = roots[roots.length - 1]; |
| 72 | + double Vl = Zl * R * T / saturationPressure; |
| 73 | + double Vv = Zv * R * T / saturationPressure; |
| 74 | + return (1 - vaporFraction) * Vl + vaporFraction * Vv; |
| 75 | + } else { |
| 76 | + // Fallback for single phase |
| 77 | + Z = roots[0]; |
| 78 | + } |
| 79 | + |
| 80 | + return Z * R * T / P; |
| 81 | + } |
| 82 | + |
15 | 83 | abstract double[] getZFactors(double T, double P); |
| 84 | + protected double idealGasEntropy(double T, double P) { |
| 85 | + if (T < 0) T = 0.1f; |
| 86 | + if (P < 0) P = 0.1f; |
| 87 | + |
| 88 | + double Cp = 3.5 * R; |
| 89 | + |
| 90 | + return (Cp * Math.log(T) - R * Math.log(P))/M; |
| 91 | + } |
| 92 | + abstract double saturationPressure(double T); |
| 93 | + |
| 94 | + /** |
| 95 | + * @param T temperature |
| 96 | + * @param Vm molar volume -> TODO go toward a by mass volume |
| 97 | + * @return specific entropy |
| 98 | + */ |
| 99 | + public final double totalEntropy(double T, double Vm) { |
| 100 | + double SRef = idealGasEntropy(T_REF, P_REF) + residualEntropy(T_REF, P_REF, getZFactors(T_REF, P_REF)[0]); |
| 101 | + |
| 102 | + // Check two-phase region |
| 103 | + if (T >= Tc ) {//we can't compute the saturation pressure past the critical temperature by definition |
| 104 | + // Not in 2-phase region, just compute entropy at T, P, Z |
| 105 | + double P = pressure(T, Vm); // From PR EOS |
| 106 | + double Z = Vm * P / (T * R);//only one roots |
| 107 | + return residualEntropy(T, P, Z) + idealGasEntropy(T,P) - SRef; |
| 108 | + } |
| 109 | + double PSat = saturationPressure(T);//if we are 2 phased then we need to use the saturation pressure |
| 110 | + |
| 111 | + // Two-phase region |
| 112 | + double[] Zs = getZFactors(T, PSat); |
| 113 | + if (Zs.length == 0) {//if there is no solution we are a liquid that is too low in temperature. |
| 114 | + double P = pressure(T, Vm); // From PR EOS |
| 115 | + double Z = Vm * P / (T * R);//only one roots |
| 116 | + return residualEntropy(T, P, Z) + idealGasEntropy(T,P)- SRef; |
| 117 | + } |
| 118 | + double Z_l = Zs[0]; |
| 119 | + double Z_v = Zs[Zs.length - 1]; |
| 120 | + |
| 121 | + // Compute molar volumes of saturated phases |
| 122 | + double v_l = Z_l * R * T / PSat; |
| 123 | + double v_v = Z_v * R * T / PSat; |
| 124 | + |
| 125 | + if (Vm < v_l || Vm > v_v){//check if 2 phases or not |
| 126 | + double P = pressure(T, Vm); // From PR EOS |
| 127 | + double Z = Vm * P / (T * R);//only one roots |
| 128 | + return residualEntropy(T, P, Z) + idealGasEntropy(T,P)- SRef; |
| 129 | + } |
| 130 | + |
| 131 | + // Compute vapor quality x |
| 132 | + double x = (Vm - v_l) / (v_v - v_l); |
| 133 | + x = Math.max(0.0, Math.min(1.0, x)); // Clamp between 0 and 1 |
| 134 | + |
| 135 | + // Residual entropy for each phase |
| 136 | + double S_l = residualEntropy(T, pressure(T, v_l), Z_l); //+ idealGasEntropy(T, PSat); |
| 137 | + double S_v = residualEntropy(T, pressure(T, v_v), Z_v);// + idealGasEntropy(T, PSat); |
| 138 | + // Mix and convert to specific entropy |
| 139 | + return (1 - x) * S_l + x * S_v - SRef + idealGasEntropy(T, PSat); // [J/kg·K] |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Warning only use this if you know that the fluid is in the LV phase. |
| 144 | + * @param T temperature |
| 145 | + * @return return the couple Vl, Vv or the intermediate V if it's super critical. |
| 146 | + */ |
| 147 | + public Couple<Double> getSaturationVolumes(double T) { |
| 148 | + try { |
| 149 | + if (T < Tc ) { |
| 150 | + double PSat = saturationPressure(T); // try normal coexistence |
| 151 | + double[] roots = getZFactors(T, PSat); |
| 152 | + |
| 153 | + if (roots.length <= 2) { |
| 154 | + throw new IllegalStateException("The Saturation Pressure is not in a 2 phase region"); |
| 155 | + } |
| 156 | + double Zl = roots[0]; |
| 157 | + double Zv = roots[roots.length - 1]; |
| 158 | + double Vl = Zl * R * T / PSat; |
| 159 | + double Vv = Zv * R * T / PSat; |
| 160 | + return Couple.create(Vl, Vv); |
| 161 | + } else { |
| 162 | + double[] critRoot = getZFactors(Tc, Pc); |
| 163 | + |
| 164 | + // Start guess: approximate ideal gas volume |
| 165 | + double Z = Arrays.stream(critRoot).max().getAsDouble(); |
| 166 | + //we need to find a better interpolation function. |
| 167 | + double VmInflection = Z * EquationOfState.R * Tc / Pc; |
| 168 | + return Couple.create(VmInflection, VmInflection); |
| 169 | + } |
| 170 | + } catch (RuntimeException e) { |
| 171 | + FormicAPI.LOGGER.debug("Unexpectedly found outside LV phase {}", T); |
| 172 | + FormicAPI.LOGGER.debug(e); |
| 173 | + // Fall back to inflection point |
| 174 | + return Couple.create(Double.NaN, Double.NaN); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + protected abstract double residualEntropy(double T, double P, double Z); |
| 179 | + |
| 180 | + |
| 181 | + /** |
| 182 | + * |
| 183 | + * @param T temperature |
| 184 | + * @return specific enthalpy |
| 185 | + */ |
| 186 | + protected double idealGasEnthalpy(double T) { |
| 187 | + double Cp = 75.0; // J/mol·K |
| 188 | + return Cp * (T)/M; |
| 189 | + } |
| 190 | + |
| 191 | + protected abstract double residualEnthalpy(double T, double P, double Z); |
| 192 | + |
| 193 | + public double totalEnthalpy(double T, double Vm) { |
| 194 | + double HRef = idealGasEnthalpy(T_REF) + residualEnthalpy(T_REF, P_REF, getZFactors(T_REF, P_REF)[0]); |
| 195 | + double P = pressure(T, Vm); // From PR EOS |
| 196 | + |
| 197 | + // Check two-phase region |
| 198 | + if (T >= Tc ) {//we can't compute the saturation pressure past the critical temperature by definition |
| 199 | + // Not in 2-phase region, just compute entropy at T, P, Z |
| 200 | + double Z = Vm * P / (T * R);//only one roots |
| 201 | + return residualEnthalpy(T, P, Z) + idealGasEnthalpy(T) - HRef; |
| 202 | + } |
| 203 | + |
| 204 | + double PSat = saturationPressure(T);//if we are 2 phased then we need to use the saturation pressure |
| 205 | + |
| 206 | + // Two-phase region |
| 207 | + double[] Zs = getZFactors(T, PSat); |
| 208 | + |
| 209 | + if (Zs.length == 0){//check if 2 phases or not |
| 210 | + System.out.println("weird T "+T); |
| 211 | + double Z = Vm * P / (T * R);//only one roots |
| 212 | + return residualEnthalpy(T, P, Z) + idealGasEnthalpy(T) - HRef; |
| 213 | + } |
| 214 | + |
| 215 | + double Z_l = Zs[0]; |
| 216 | + double Z_v = Zs[Zs.length - 1]; |
| 217 | + |
| 218 | + // Compute molar volumes of saturated phases |
| 219 | + double v_l = Z_l * R * T / PSat; |
| 220 | + double v_v = Z_v * R * T / PSat; |
| 221 | + |
| 222 | + if (Vm < v_l || Vm > v_v){//check if 2 phases or not |
| 223 | + double Z = Vm * P / (T * R);//only one roots |
| 224 | + return residualEnthalpy(T, P, Z) + idealGasEnthalpy(T) - HRef; |
| 225 | + } |
| 226 | + |
| 227 | + |
| 228 | + // Compute vapor quality x |
| 229 | + double x = (Vm - v_l) / (v_v - v_l); |
| 230 | + x = Math.max(0.0, Math.min(1.0, x)); // Clamp between 0 and 1 |
| 231 | + |
| 232 | + // Residual entropy for each phase |
| 233 | + double H_l = residualEnthalpy(T, PSat, Z_l) + idealGasEnthalpy(T); |
| 234 | + double H_v = residualEnthalpy(T, PSat, Z_v) + idealGasEnthalpy(T); |
| 235 | + // Mix and convert to specific entropy |
| 236 | + return (1 - x) * H_l + x * H_v - HRef; // [J/kg·K] |
| 237 | + } |
| 238 | + |
| 239 | + public boolean is2phases(double T, double P) { |
| 240 | + double saturationPressure = saturationPressure(T); |
| 241 | + return P >= saturationPressure * 0.99f || P <= saturationPressure * 1.01f;// do this to avoid problem with numerical error |
| 242 | + } |
| 243 | + |
| 244 | + public double getEntropy(float T, float P, float x) { |
| 245 | + double[] roots1 = getZFactors(T, P); |
| 246 | + if (roots1.length == 0) |
| 247 | + return 0;//this is bad -> do it differently |
| 248 | + if (roots1.length == 1) {// this is when it's monophasique or past critical |
| 249 | + double Z1 = roots1[0]; |
| 250 | + double Vm1 = Z1 * R * T / P; |
| 251 | + return totalEntropy(T, Vm1); |
| 252 | + } |
| 253 | + else { |
| 254 | + if (T < Tc ){ |
| 255 | + double saturationPressure = saturationPressure(T); |
| 256 | + if (saturationPressure * 1.01 < P) { |
| 257 | + return totalEntropy(T,roots1[0]* R * T / P); |
| 258 | + } |
| 259 | + if (saturationPressure * 0.99 > P) { |
| 260 | + return totalEntropy(T,roots1[roots1.length-1]* R * T / P); |
| 261 | + } |
| 262 | + } |
| 263 | + Couple<Double> Vms = getSaturationVolumes(T); |
| 264 | + return (totalEntropy(T,Vms.getFirst()) * (1- x) +totalEntropy(T, Vms.getSecond())* x); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + public double getEnthalpy(float T, float P, float x){ |
| 269 | + final double R = EquationOfState.R; |
| 270 | + if (Float.isNaN(T)) T = 100; |
| 271 | + if (Float.isNaN(P)) P = 1; |
16 | 272 |
|
| 273 | + if (T < 100) T = 100; |
| 274 | + if (P < 1) P = 1; |
| 275 | + return totalEnthalpy(T, volumeMolar(T, P, x)); |
| 276 | + } |
17 | 277 |
|
| 278 | + protected abstract double minZ(float finalT, float finalP); |
18 | 279 | } |
0 commit comments