Class ProcessDerivativeCalculator
java.lang.Object
neqsim.process.mpc.ProcessDerivativeCalculator
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDerivative calculation methods.static classResult container for derivative calculations with error estimates.static classSpecification for an input or output variable.static enumVariable types for optimal step size calculation. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhether base case is valid.private double[]Cached base case input values.private double[]Cached base case output values.List of input variables.Derivative calculation method.private doubleMinimum absolute step size.private intNumber of threads for parallel computation.List of output variables.private booleanWhether to use parallel computation.private ProcessSystemThe process system to calculate derivatives for.private doubleRelative step size for finite differences.private ProcessVariableAccessorVariable accessor for reading/writing process variables. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddInputVariable(String path, String unit) Add an input variable for derivative calculation.addInputVariable(String path, String unit, double stepSize) Add an input variable with custom step size.addOutputVariable(String path, String unit) Add an output variable for derivative calculation.private voidCache the current base case values.private double[]calculateCentralDifference(ProcessDerivativeCalculator.VariableSpec inputSpec, double baseValue, double step) Central difference calculation (more accurate).private double[]calculateCentralDifferenceSecondOrder(ProcessDerivativeCalculator.VariableSpec inputSpec, double baseValue, double step) Second-order central difference with error estimation.private double[]calculateForwardDifference(ProcessDerivativeCalculator.VariableSpec inputSpec, double baseValue, double step) Forward difference calculation.private double[]calculateGradientForInput(int inputIndex) Calculate gradient of all outputs with respect to one input.double[][]calculateHessian(String outputPath) Calculate Hessian matrix for a single output (second derivatives).double[][]Calculate the full Jacobian matrix ∂outputs/∂inputs.private voidcalculateJacobianParallel(double[][] jacobian) Calculate Jacobian in parallel.private voidcalculateJacobianSequential(double[][] jacobian) Calculate Jacobian sequentially.private doublecalculateStepSize(ProcessDerivativeCalculator.VariableSpec spec, double value) Calculate optimal step size based on variable type and value.Clear all input variables.Clear all output variables.private voidEnsure base case is calculated and cached.private double[]Evaluate all output variables at current process state.voidexportJacobianToCSV(String filename) Export Jacobian to CSV format.Export Jacobian to JSON format for AI/ML applications.double[]Get the cached base case input values.double[]Get the cached base case output values.doublegetDerivative(String outputPath, String inputPath) Calculate a single derivative ∂output/∂input.double[]getGradient(String outputPath) Calculate gradient of a single output with respect to all inputs.Get list of input variable names.Get list of output variable names.private voidInvalidate the cached base case.private double[]perturbAndEvaluate(ProcessDerivativeCalculator.VariableSpec inputSpec, double value) Helper to perturb input and evaluate outputs.Set the derivative calculation method.setParallel(boolean enabled, int numThreads) Enable parallel computation of derivatives.setRelativeStepSize(double relativeStep) Set the relative step size for finite differences.
-
Field Details
-
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[] baseOutputValuesCached base case output values. -
baseInputValues
private double[] baseInputValuesCached base case input values. -
baseCaseValid
private boolean baseCaseValidWhether base case is valid. -
relativeStepSize
private double relativeStepSizeRelative step size for finite differences. -
minAbsoluteStep
private double minAbsoluteStepMinimum absolute step size. -
parallelEnabled
private boolean parallelEnabledWhether to use parallel computation. -
numThreads
private int numThreadsNumber of threads for parallel computation. -
variableAccessor
Variable accessor for reading/writing process variables.
-
-
Constructor Details
-
ProcessDerivativeCalculator
Constructor.- Parameters:
process- the process system to calculate derivatives for
-
-
Method Details
-
addInputVariable
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
Add an input variable with custom step size.- Parameters:
path- variable pathunit- unit of measurementstepSize- custom absolute step size- Returns:
- this calculator for chaining
-
addOutputVariable
Add an output variable for derivative calculation.- Parameters:
path- variable pathunit- unit of measurement- Returns:
- this calculator for chaining
-
clearInputVariables
Clear all input variables.- Returns:
- this calculator for chaining
-
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
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
Enable parallel computation of derivatives.- Parameters:
enabled- whether to enable parallel computationnumThreads- 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
-
getGradient
Calculate gradient of a single output with respect to all inputs.- Parameters:
outputPath- output variable path- Returns:
- gradient vector
-
calculateHessian
Calculate Hessian matrix for a single output (second derivatives).- Parameters:
outputPath- output variable path- Returns:
- Hessian matrix [numInputs x numInputs]
-
calculateStepSize
Calculate optimal step size based on variable type and value.- Parameters:
spec- the variable specificationvalue- 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
-
getOutputVariableNames
-
exportJacobianToCSV
Export Jacobian to CSV format.- Parameters:
filename- output filename
-
exportJacobianToJSON
Export Jacobian to JSON format for AI/ML applications.- Returns:
- JSON string representation
-