Class DifferentiableFlash
java.lang.Object
neqsim.thermo.util.derivatives.DifferentiableFlash
- All Implemented Interfaces:
Serializable
Computes gradients of flash calculation results using the implicit function theorem.
This class provides exact derivatives of phase equilibrium results without differentiating through the iterative flash solver. At equilibrium, the residual equations F(y; θ) = 0 are satisfied. The implicit function theorem gives:
dy/dθ = -(∂F/∂y)^(-1) * (∂F/∂θ)
where y = (K_1, ..., K_n, β) are the solution variables and θ = (T, P, z) are parameters.
Usage Example:
// Run flash calculation
SystemInterface system = new SystemSrkEos(300, 50);
system.addComponent("methane", 1.0);
system.addComponent("ethane", 0.5);
ThermodynamicOperations ops = new ThermodynamicOperations(system);
ops.TPflash();
// Compute gradients
DifferentiableFlash diffFlash = new DifferentiableFlash(system);
FlashGradients grads = diffFlash.computeFlashGradients();
// Use gradients
double dBeta_dT = grads.getDBetadT();
double[] dK_dP = grads.getDKdP();
// Or compute property gradients directly
PropertyGradient densityGrad = diffFlash.computePropertyGradient("density");
Supported Properties:
- density - mixture density [kg/m³]
- enthalpy - mixture enthalpy [J/mol]
- entropy - mixture entropy [J/mol/K]
- Cp - heat capacity at constant pressure [J/mol/K]
- compressibility - Z-factor [-]
- molarvolume - molar volume [m³/mol]
- Since:
- 3.0
- Author:
- ESOL
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate FlashGradientsCached flash gradients.private FugacityJacobian[]Cached fugacity Jacobians for each phase.private static final doubleSmall perturbation for numerical derivatives.private booleanWhether gradients have been computed.private static final org.apache.logging.log4j.Loggerprivate static final longprivate final SystemInterfaceThe thermodynamic system at equilibrium. -
Constructor Summary
ConstructorsConstructorDescriptionDifferentiableFlash(SystemInterface system) Constructor for DifferentiableFlash. -
Method Summary
Modifier and TypeMethodDescriptionprivate doublecomputeDxDBeta(int i, double[] z, double[] K, double beta) private doublecomputeDxDK(int j, double[] z, double[] K, double beta) private doublecomputeDyDBeta(int i, double[] z, double[] K, double beta) private doublecomputeDyDK(int j, double[] z, double[] K, double beta) Compute gradients of flash results using implicit function theorem.private doublecomputeNumericalDerivativeP(String propertyName) private doublecomputeNumericalDerivativeT(String propertyName) private double[]computeNumericalDerivativeZ(String propertyName) computePropertyGradient(String propertyName) Compute gradient of a thermodynamic property.extractFugacityJacobian(int phaseIndex) Extract fugacity Jacobian from a phase using existing NeqSim derivatives.private double[]getColumn(double[][] matrix, int col) Get cached fugacity Jacobians.private StringgetPropertyUnit(String propertyName) private doublegetPropertyValue(String propertyName) private double[][]invertMatrix(double[][] matrix) private double[][]matrixMultiply(double[][] a, double[][] b) private double[]matrixVectorMultiply(double[][] matrix, double[] vector) private voidnegate(double[] vector) private voidnegateMatrix(double[][] matrix)
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
logger
private static final org.apache.logging.log4j.Logger logger -
system
The thermodynamic system at equilibrium. -
cachedFlashGradients
Cached flash gradients. -
cachedFugacityJacobians
Cached fugacity Jacobians for each phase. -
gradientsComputed
private boolean gradientsComputedWhether gradients have been computed. -
EPSILON
private static final double EPSILONSmall perturbation for numerical derivatives.- See Also:
-
-
Constructor Details
-
DifferentiableFlash
Constructor for DifferentiableFlash.- Parameters:
system- thermodynamic system (must be at equilibrium, i.e., flash already performed)
-
-
Method Details
-
extractFugacityJacobian
Extract fugacity Jacobian from a phase using existing NeqSim derivatives.- Parameters:
phaseIndex- phase index- Returns:
- FugacityJacobian for the phase
-
computeFlashGradients
Compute gradients of flash results using implicit function theorem.The equilibrium conditions are:
- F_i = ln(K_i) - ln(φ_i^L) + ln(φ_i^V) = 0 for i = 1...nc
- F_{nc+1} = Σ z_i*(K_i - 1)/(1 + β*(K_i - 1)) = 0 (Rachford-Rice)
- Returns:
- FlashGradients containing all derivatives
-
computePropertyGradient
Compute gradient of a thermodynamic property.- Parameters:
propertyName- name of the property ("density", "enthalpy", "entropy", etc.)- Returns:
- PropertyGradient containing derivatives
-
getFugacityJacobians
Get cached fugacity Jacobians.- Returns:
- array of FugacityJacobian (index 0 = liquid, 1 = vapor), or null if not computed
-
computeDxDK
private double computeDxDK(int j, double[] z, double[] K, double beta) -
computeDyDK
private double computeDyDK(int j, double[] z, double[] K, double beta) -
computeDxDBeta
private double computeDxDBeta(int i, double[] z, double[] K, double beta) -
computeDyDBeta
private double computeDyDBeta(int i, double[] z, double[] K, double beta) -
getPropertyValue
-
getPropertyUnit
-
computeNumericalDerivativeT
-
computeNumericalDerivativeP
-
computeNumericalDerivativeZ
-
invertMatrix
private double[][] invertMatrix(double[][] matrix) -
matrixVectorMultiply
private double[] matrixVectorMultiply(double[][] matrix, double[] vector) -
matrixMultiply
private double[][] matrixMultiply(double[][] a, double[][] b) -
getColumn
private double[] getColumn(double[][] matrix, int col) -
negate
private void negate(double[] vector) -
negateMatrix
private void negateMatrix(double[][] matrix)
-