Class DifferentiableFlash

java.lang.Object
neqsim.thermo.util.derivatives.DifferentiableFlash
All Implemented Interfaces:
Serializable

public class DifferentiableFlash extends Object implements 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 Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • logger

      private static final org.apache.logging.log4j.Logger logger
    • system

      private final SystemInterface system
      The thermodynamic system at equilibrium.
    • cachedFlashGradients

      private FlashGradients cachedFlashGradients
      Cached flash gradients.
    • cachedFugacityJacobians

      private FugacityJacobian[] cachedFugacityJacobians
      Cached fugacity Jacobians for each phase.
    • gradientsComputed

      private boolean gradientsComputed
      Whether gradients have been computed.
    • EPSILON

      private static final double EPSILON
      Small perturbation for numerical derivatives.
      See Also:
  • Constructor Details

    • DifferentiableFlash

      public DifferentiableFlash(SystemInterface system)
      Constructor for DifferentiableFlash.
      Parameters:
      system - thermodynamic system (must be at equilibrium, i.e., flash already performed)
  • Method Details

    • extractFugacityJacobian

      public FugacityJacobian extractFugacityJacobian(int phaseIndex)
      Extract fugacity Jacobian from a phase using existing NeqSim derivatives.
      Parameters:
      phaseIndex - phase index
      Returns:
      FugacityJacobian for the phase
    • computeFlashGradients

      public FlashGradients 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

      public PropertyGradient computePropertyGradient(String propertyName)
      Compute gradient of a thermodynamic property.
      Parameters:
      propertyName - name of the property ("density", "enthalpy", "entropy", etc.)
      Returns:
      PropertyGradient containing derivatives
    • getFugacityJacobians

      public FugacityJacobian[] 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

      private double getPropertyValue(String propertyName)
    • getPropertyUnit

      private String getPropertyUnit(String propertyName)
    • computeNumericalDerivativeT

      private double computeNumericalDerivativeT(String propertyName)
    • computeNumericalDerivativeP

      private double computeNumericalDerivativeP(String propertyName)
    • computeNumericalDerivativeZ

      private double[] computeNumericalDerivativeZ(String propertyName)
    • 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)