Automatic Differentiation & Relaxations
Implementation:
- Forward Mode:
Dualinpackages/runtime/src/wasm/autodiff/dual.ts - Reverse Mode:
AdTapeinpackages/runtime/src/wasm/autodiff/tape.ts - Sparse Jacobian Coloring:
CCSMatrix&ColoringEngineinpackages/runtime/src/wasm/autodiff/coloring.ts - McCormick Envelopes:
McCormickTupleinpackages/runtime/src/wasm/autodiff/mccormick.ts - Affine Arithmetic:
AffineForminpackages/runtime/src/wasm/autodiff/affine.ts - Hybrid Adjoint Sensitivity & Saltation Matrices:
HybridAdjointSolverinpackages/simulate/src/core/hybrid-adjoint.ts - Aitken Dynamic
-Relaxation & Multi-Rate Sub-Cycling: VectorAitkenRelaxation&MultiRateSubCycleSchedulerinpackages/simulate/src/core/fsi-relaxation.ts
Academic Citations
Forward-Mode Dual Numbers
- Clifford, W. K. (1873). "Preliminary sketch of biquaternions."
Proceedings of the London Mathematical Society, 4, pp. 381–395.
DOI: 10.1112/plms/s1-4.1.381 - Study, E. (1903). Geometrie der Dynamen: Die Zusammensetzung von Kräften und verwandte Gegenstände der Geometrie. B.G. Teubner.
- Rall, L. B. (1981). Automatic Differentiation: Techniques and Applications.
Lecture Notes in Computer Science, 120, Springer.
DOI: 10.1007/3-540-10861-0
Reverse-Mode Adjoint Accumulation (Wengert List)
- Wengert, R. E. (1964). "A simple automatic derivative evaluation program."
Communications of the ACM, 7(8), pp. 463–464.
DOI: 10.1145/355586.364791 - Linnainmaa, S. (1976). "Taylor expansion of the accumulated rounding error."
BIT Numerical Mathematics, 16(2), pp. 146–160.
DOI: 10.1007/BF01931367 - Griewank, A., & Walther, A. (2008). Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation (2nd ed.). SIAM.
ISBN: 978-0-898716-59-7
Distance-2 Graph Coloring for Sparse Jacobians
- Curtis, A. R., Powell, M. J. D., & Reid, J. K. (1974). "On the estimation of sparse Jacobian matrices."
Journal of the Institute of Mathematics and Its Applications, 13(1), pp. 117–119.
DOI: 10.1093/imamat/13.1.117 (CPR Algorithm) - Coleman, T. F., & Moré, J. J. (1983). "Estimation of sparse Jacobian matrices and graph coloring problems."
SIAM Journal on Numerical Analysis, 20(1), pp. 187–209.
DOI: 10.1137/0720013 - Gebremedhin, A. H., Manne, F., & Pothen, A. (2005). "What color is your Jacobian? Graph coloring for computing derivatives."
SIAM Review, 47(4), pp. 629–705.
DOI: 10.1137/S0036144504446096
McCormick Relaxations & Affine Arithmetic
- McCormick, G. P. (1976). "Computability of global solutions to factorable nonconvex programs: Part I—Convex underestimating problems."
Mathematical Programming, 10(1), pp. 147–175.
DOI: 10.1007/BF01580665 - Mitsos, A., Chachuat, B., & Barton, P. I. (2009). "McCormick-based relaxations of algorithms."
SIAM Journal on Optimization, 20(2), pp. 573–601.
DOI: 10.1137/080717341 - Tsoukalas, A., & Mitsos, A. (2014). "Multivariate McCormick relaxations."
Journal of Global Optimization, 59(2), pp. 633–662.
DOI: 10.1007/s10898-014-0169-z - de Figueiredo, L. H., & Stolfi, J. (2004). "Affine arithmetic: concepts and applications."
Numerical Algorithms, 37(1), pp. 147–158.
DOI: 10.1023/B:NUMA.0000049462.70970.b6
Hybrid Adjoint Sensitivity & Saltation Matrices
- Barton, P. I., & Lee, C. K. (2002). "Modeling, simulation, sensitivity analysis, and optimization of hybrid systems."
ACM Transactions on Modeling and Computer Simulation, 12(4), pp. 256–289.
DOI: 10.1145/643120.643122 - Filippov, A. F. (1988). Differential Equations with Discontinuous Righthand Sides.
Mathematics and Its Applications, 18, Springer.
DOI: 10.1007/978-94-015-7793-9 - Corner, S., Sandu, A., & Sandu, C. (2019). "Adjoint sensitivity analysis of hybrid multibody dynamical systems."
Multibody System Dynamics, 49, pp. 395–420.
DOI: 10.1007/s11044-019-09725-2
Aitken Dynamic -Relaxation for Partitioned Co-Simulation
- Küttler, U., & Wall, W. A. (2008). "Fixed-point fluid–structure interaction solvers with dynamic relaxation."
Computational Mechanics, 43(1), pp. 61–72.
DOI: 10.1007/s00466-008-0255-5 - Irons, B. M., & Tuck, R. C. (1969). "A version of the Aitken accelerator for computer iteration."
International Journal for Numerical Methods in Engineering, 1(3), pp. 275–277.
DOI: 10.1002/nme.1620010306
ModelScript Architectural Rationale
Numerical finite-difference approximations (
ModelScript implements an integrated suite of exact differentiation and continuous bounding engines inside WebAssembly linear memory:
- Forward-Mode Dual Numbers: Computes exact machine-precision directional derivatives
concurrently with primal function evaluation for Newton steps in algebraic loops and stiff ODE/DAE integrators without allocating expression trees. - Reverse-Mode Computational Tape: Evaluates scalar objective gradients
with cost proportional to evaluations of , enabling gradient-based parameter estimation and surrogate neural network training. - CPR Distance-2 Graph Coloring: For large-scale DAE systems (
equations), evaluating the full Jacobian naively requires forward passes. CPR distance-2 coloring partitions structural columns into non-interfering groups (typically for sparse physical networks), reducing Jacobian assembly time by orders of magnitude. - McCormick Envelopes & Affine Arithmetic: Classical interval arithmetic suffers from the dependency problem: evaluating
for yields instead of , causing rapid exponential bounding blowup. Affine arithmetic tracks first-order noise symbols ( ) across state equations, while McCormick relaxations compute rigorous convex lower bounds ( ) and concave upper bounds ( ) for non-convex global optimization. - Hybrid Adjoint Sensitivity with Saltation Matrices: Standard continuous adjoint solvers break down across Modelica
when/reinitevents, impact mechanics, or switching surfaces because Dirac delta impulses arise at event-crossing times. ModelScript propagates reverse-mode adjoints across non-smooth mode transitions using exact saltation matrix jump conditions alongside optional sigmoidal equation smoothing ( DAEEventRelaxer). - Aitken Dynamic
-Relaxation: Partitioned co-simulation of strongly coupled multi-physics domains (such as lightweight aerodynamic structures coupled to dense fluids or algebraic FMU loops) diverges under explicit Gauss-Seidel macro-steps due to the artificial added-mass effect. Dynamic vector Aitken acceleration stabilizes fixed-point iterations without requiring monolithic cross-FMU Jacobians.
ModelScript Modifications & WASM Architecture
1. Zero-GC Dual Number Arithmetic
Represented as a 16-byte unmanaged struct (@unmanaged) in WASM memory:
@unmanaged
export class Dual {
val: f64; // Primal evaluation f(x)
dot: f64; // Directional derivative f'(x) * v
}All elementary operators (
2. Linear Memory Computational Tape
The reverse-mode tape records operations into a flat 32-byte stride unmanaged array:
During the reverse pass, adjoints are accumulated in topological order directly in linear memory:
3. CPR Distance-2 Coloring Algorithm
Two columns
The graph coloring algorithm:
- Constructs the bipartite graph
where edge . - Colors columns
such that any pair of columns connected by a path of length 2 receives different colors. - Groups columns of color
into a perturbation vector . - Computes the entire compressed Jacobian in exactly
directional forward-mode passes:
4. McCormick Relaxations
For factorable bilinear products
ModelScript evaluates McCormick tuples
5. Hybrid Adjoint Sensitivity with Saltation Matrices (hybrid-adjoint.ts)
When a hybrid DAE trajectory crosses a discrete event surface when clauses, reinit(x, expr) impulses, or mechanical contact restitution), the forward sensitivity transition matrix undergoes a discontinuous rank-1 update governed by the saltation matrix:
HybridAdjointSolver (solveHybridAdjoint in packages/simulate/src/core/hybrid-adjoint.ts) implements a three-phase hybrid sensitivity pipeline:
- Forward Integration & Event Localization: Integrates the primal DAE arena while streaming cubic Hermite segments into
DenseHermiteCheckpointTape. Whenever an event indicatorchanges sign across a step, a 20-iteration bisection root-finder isolates the exact crossing time , records pre-event limits , applies the discrete reset map , and re-evaluates post-event derivatives . - Adjoint Jump across Event Surfaces: During backward Heun integration of the co-state
, crossing in reverse applies the transposed saltation jump condition to and accumulates the discrete event timing contribution into the parameter gradient : - Optional Sigmoidal Event Relaxation: For models with dense chattering or non-smooth algebraic switches (
if,abs,min,max),HybridAdjointSolveroptionally invokesDAEEventRelaxerto smooth C0 discontinuities intosigmoidal transitions prior to adjoint integration.
6. Aitken Dynamic -Relaxation for Coupled Co-Simulation (fsi-relaxation.ts)
In partitioned Fluid-Structure Interaction (FSI) and multi-FMU co-simulation macro-steps, exchanging boundary loads and displacements via unrelaxed fixed-point iteration
VectorAitkenRelaxation (packages/simulate/src/core/fsi-relaxation.ts) accelerates and stabilizes convergence over scalar variables, multi-DOF nodal displacement vectors, or 3D boundary traction maps (relaxBoundaryLoads):
- Fixed-Point Residual: At macro-step sub-iteration
, computes the interface residual and residual difference . - Secant Aitken Factor Update: Updates the dynamic relaxation factor
via the multidimensional Aitken projection: clamped to a safe stability interval (default ). - Relaxed State Update: Advances the interface state via under-relaxation:
terminating when the relative residual norm . - Multi-Rate Sub-Cycling (
MultiRateSubCycleScheduler): Synchronizes fast acoustic CFD steps (), intermediate elastodynamic FEA steps ( ), and macro 1D DAE steps ( ) using linear sub-step boundary interpolation .
Upstream & Downstream Integration
| Pipeline Component | Upstream Dependencies | Downstream Consumers |
|---|---|---|
| Dual Numbers | DAEBuilder AST, Variable seed vector | Algebraic loop tearing, Newton solvers, sensitivity analysis |
| AdTape | DAEBuilder objective expressions | L-BFGS-B optimizer, surrogate loss backprop |
| CPR Coloring | Bipartite incidence matrix from BltEngine | Sparse Jacobian generation, SUNDIALS/TR-BDF2 linear solves |
| McCormick & Affine | Bound intervals from QueryEngine | HC4 contractor, Nelson-Oppen theory coordinator, SOS bounds |
| HybridAdjointSolver | DAEBuilder, DenseHermiteCheckpointTape, Events | Hybrid parameter calibration, optimal control across mode switches |
| VectorAitkenRelaxation | Partitioned CFD/FEA boundary loads & FMU ports | Coupled aeroelastic FSI solver, SSP co-simulation master |

