Class ProcessDerivativeCalculator

java.lang.Object
neqsim.process.mpc.ProcessDerivativeCalculator

public class ProcessDerivativeCalculator extends Object
Efficient derivative calculator for NeqSim process simulations.

Calculates numerical derivatives (gradients, Jacobians, Hessians) of process outputs with respect to process inputs. Designed for use in MPC, optimization, and AI/ML applications.

Efficiency features:

  • Central differences for accuracy with adaptive step sizing
  • Parallel computation for independent perturbations
  • Caching of base case to avoid redundant calculations
  • Sparse Jacobian support for large systems
  • Batch gradient computation

Example usage:


ProcessDerivativeCalculator calc = new ProcessDerivativeCalculator(process);

// Define variables
calc.addInputVariable("Feed.flowRate", "kg/hr");
calc.addInputVariable("Feed.pressure", "bara");
calc.addOutputVariable("Separator.gasOutStream.flowRate", "kg/hr");
calc.addOutputVariable("Separator.liquidLevel", "fraction");

// Calculate Jacobian
double[][] jacobian = calc.calculateJacobian();

// Or get single derivative
double dGasFlow_dFeedFlow =
    calc.getDerivative("Separator.gasOutStream.flowRate", "Feed.flowRate");

Version:
1.0
Author:
NeqSim Development Team
  • Field Details

    • process

      private ProcessSystem process
      The process system to calculate derivatives for.
    • method

      Derivative calculation method.
    • inputVariables

      List of input variables.
    • outputVariables

      List of output variables.
    • baseOutputValues

      private double[] baseOutputValues
      Cached base case output values.
    • baseInputValues

      private double[] baseInputValues
      Cached base case input values.
    • baseCaseValid

      private boolean baseCaseValid
      Whether base case is valid.
    • relativeStepSize

      private double relativeStepSize
      Relative step size for finite differences.
    • minAbsoluteStep

      private double minAbsoluteStep
      Minimum absolute step size.
    • parallelEnabled

      private boolean parallelEnabled
      Whether to use parallel computation.
    • numThreads

      private int numThreads
      Number of threads for parallel computation.
    • variableAccessor

      private ProcessVariableAccessor variableAccessor
      Variable accessor for reading/writing process variables.
  • Constructor Details

    • ProcessDerivativeCalculator

      public ProcessDerivativeCalculator(ProcessSystem process)
      Constructor.
      Parameters:
      process - the process system to calculate derivatives for
  • Method Details

    • addInputVariable

      public ProcessDerivativeCalculator addInputVariable(String path, String unit)
      Add an input variable for derivative calculation.
      Parameters:
      path - variable path (e.g., "Feed.flowRate" or "unit:Feed,property:flowRate")
      unit - unit of measurement
      Returns:
      this calculator for chaining
    • addInputVariable

      public ProcessDerivativeCalculator addInputVariable(String path, String unit, double stepSize)
      Add an input variable with custom step size.
      Parameters:
      path - variable path
      unit - unit of measurement
      stepSize - custom absolute step size
      Returns:
      this calculator for chaining
    • addOutputVariable

      public ProcessDerivativeCalculator addOutputVariable(String path, String unit)
      Add an output variable for derivative calculation.
      Parameters:
      path - variable path
      unit - unit of measurement
      Returns:
      this calculator for chaining
    • clearInputVariables

      public ProcessDerivativeCalculator clearInputVariables()
      Clear all input variables.
      Returns:
      this calculator for chaining
    • clearOutputVariables

      public ProcessDerivativeCalculator clearOutputVariables()
      Clear all output variables.
      Returns:
      this calculator for chaining
    • setMethod

      Set the derivative calculation method.
      Parameters:
      method - the method to use
      Returns:
      this calculator for chaining
    • setRelativeStepSize

      public ProcessDerivativeCalculator setRelativeStepSize(double relativeStep)
      Set the relative step size for finite differences.
      Parameters:
      relativeStep - relative step (e.g., 1e-4 for 0.01%)
      Returns:
      this calculator for chaining
    • setParallel

      public ProcessDerivativeCalculator setParallel(boolean enabled, int numThreads)
      Enable parallel computation of derivatives.
      Parameters:
      enabled - whether to enable parallel computation
      numThreads - number of threads to use
      Returns:
      this calculator for chaining
    • calculateJacobian

      public double[][] calculateJacobian()
      Calculate the full Jacobian matrix ∂outputs/∂inputs.

      The Jacobian matrix J[i][j] contains the derivative of output i with respect to input j.

      Returns:
      Jacobian matrix [numOutputs x numInputs]
    • calculateJacobianSequential

      private void calculateJacobianSequential(double[][] jacobian)
      Calculate Jacobian sequentially.
    • calculateJacobianParallel

      private void calculateJacobianParallel(double[][] jacobian)
      Calculate Jacobian in parallel.
    • calculateGradientForInput

      private double[] calculateGradientForInput(int inputIndex)
      Calculate gradient of all outputs with respect to one input.
      Parameters:
      inputIndex - index of the input variable
      Returns:
      gradient vector
    • calculateForwardDifference

      private double[] calculateForwardDifference(ProcessDerivativeCalculator.VariableSpec inputSpec, double baseValue, double step)
      Forward difference calculation.
    • calculateCentralDifference

      private double[] calculateCentralDifference(ProcessDerivativeCalculator.VariableSpec inputSpec, double baseValue, double step)
      Central difference calculation (more accurate).
    • calculateCentralDifferenceSecondOrder

      private double[] calculateCentralDifferenceSecondOrder(ProcessDerivativeCalculator.VariableSpec inputSpec, double baseValue, double step)
      Second-order central difference with error estimation.
    • perturbAndEvaluate

      private double[] perturbAndEvaluate(ProcessDerivativeCalculator.VariableSpec inputSpec, double value)
      Helper to perturb input and evaluate outputs.
    • getDerivative

      public double getDerivative(String outputPath, String inputPath)
      Calculate a single derivative ∂output/∂input.
      Parameters:
      outputPath - output variable path
      inputPath - input variable path
      Returns:
      derivative value
    • getGradient

      public double[] getGradient(String outputPath)
      Calculate gradient of a single output with respect to all inputs.
      Parameters:
      outputPath - output variable path
      Returns:
      gradient vector
    • calculateHessian

      public double[][] calculateHessian(String outputPath)
      Calculate Hessian matrix for a single output (second derivatives).
      Parameters:
      outputPath - output variable path
      Returns:
      Hessian matrix [numInputs x numInputs]
    • calculateStepSize

      private double calculateStepSize(ProcessDerivativeCalculator.VariableSpec spec, double value)
      Calculate optimal step size based on variable type and value.
      Parameters:
      spec - the variable specification
      value - the current value of the variable
      Returns:
      the calculated optimal step size
    • ensureBaseCase

      private void ensureBaseCase()
      Ensure base case is calculated and cached.
    • cacheBaseCase

      private void cacheBaseCase()
      Cache the current base case values.
    • invalidateBaseCase

      private void invalidateBaseCase()
      Invalidate the cached base case.
    • evaluateOutputs

      private double[] evaluateOutputs()
      Evaluate all output variables at current process state.
      Returns:
      array of output variable values
    • getBaseOutputValues

      public double[] getBaseOutputValues()
      Get the cached base case output values.
      Returns:
      base case outputs
    • getBaseInputValues

      public double[] getBaseInputValues()
      Get the cached base case input values.
      Returns:
      base case inputs
    • getInputVariableNames

      public List<String> getInputVariableNames()
      Get list of input variable names.
      Returns:
      input variable names
    • getOutputVariableNames

      public List<String> getOutputVariableNames()
      Get list of output variable names.
      Returns:
      output variable names
    • exportJacobianToCSV

      public void exportJacobianToCSV(String filename)
      Export Jacobian to CSV format.
      Parameters:
      filename - output filename
    • exportJacobianToJSON

      public String exportJacobianToJSON()
      Export Jacobian to JSON format for AI/ML applications.
      Returns:
      JSON string representation