Flipping the Coins: Training Physical Simulators through Hard Switches
Organizations: School of Computing and Data Science, The University of Hong Kong · Department of Artificial Intelligence, School of Engineering, Westlake University · Shanghai Innovation Institute, Shanghai, China · Department of Earth System Science, Tsinghua University
Abstract
Hybrid simulators embed learnable components such as neural networks in physical solvers and train them on reference trajectories. Many scientific solvers feature hard switches, such as a tripping power line, an igniting grid cell, or depolarizing brain tissue, where the loss exhibits discrete jumps across switch flips, leaving standard automatic differentiation to capture only its smooth part. Existing training paradigms face a fundamentally challenging trade-off, as gradient-based relaxations smooth the switches and introduce bias across jumps, whereas derivative-free methods treat the solver as a black box and discard path gradients of the continuous dynamics. Here we introduce EventFlip, which flips a near-threshold switch from a stored state, replays the original solver, and measures the loss difference between the two runs, in effect a finite difference across the switch. By perturbing every threshold with small Gaussian noise, we cast each switch as a stochastic binary decision and derive an unbiased gradient estimator that combines the continuous path gradient with a jump term constructed from these counterfactual rollouts. EventFlip-Rand optimizes this noise-averaged objective, whereas EventFlip-Hard operates on the deterministic hard-switch simulator used at deployment and provably targets the complete discontinuous loss. Under an identical forward evaluation budget, we benchmark EventFlip against twelve baselines across a power-grid cascade, a detonation wave, and cortical spreading depolarization, achieving the lowest error on every task and reducing the strongest baseline's error by factors of 1.5, 1.4, and 1.4, respectively.
Figures & tables
| Scenario | Power-grid cascade | Detonation wave | Cortical spreading depolarization |
|---|---|---|---|
| Metric | Rollout loss | Wave-speed error (%) | Activation-time error (%) |
| EventFlip-Hard | |||
| EventFlip-Rand | |||
| Direct ( Um et al., 2020 ; Feng et al., 2022 ) | |||
| Sigmoid ( Seo, 2026 ) | |||
| STE ( Bengio et al., 2013 ) |
| Power-grid cascade | Detonation wave | Cortical spreading depolarization | |
| Direct (path gradient only) | 0.181 | 0.641 | 1.068 |
| EventFlip-Hard , jump term only | 4.432 | 0.883 | |
| EventFlip-Rand , jump term only | 4.322 | 0.858 | |
| EventFlip-Hard | 0.067 | 0.104 | 0.793 |
| EventFlip-Rand | 0.072 | 0.120 | 0.759 |
| EventFlip-Rand , independent noise | 0.072 | 0.132 | 1.101 |
Appendix figures & tables14 assets
Supplementary material from the paper’s appendix.
Appendix
| Symbol | Meaning |
|---|---|
| , | Model parameters and fixed window context. |
| , , , , , , | State, reference state, switch values , number of cells or components, quantities per cell, state update, and per-step loss. |
| , , , , | Window length, event count, event index, and its time step and cell or component. |
| , , , | Pre-event margin, switch value, complete history, and earlier decisions. |
| , , | State before the event's stage, pre-event history and state, and complete base record. |
| , , , | Positive width, standard normal threshold perturbation, remaining perturbations, and complete threshold-noise field. |
| Power-grid cascade | Detonation wave | Cortical spreading depolarization | |
|---|---|---|---|
| Closure changes the outcome with every switch held fixed | No, since the topology fixes flows and shed load | Yes, through pressure, temperature, and density | Yes, since K + rises continuously toward the threshold |
| Physical intermediate state of a switch | None, since a line is either in service or open | Partial burning | None, since a cell is either resting or activated and activation is never undone |
| Switches near their thresholds per training window | 1.0–1.3 | 3.4–8.0 | 347–374 |
| Method | Setting | Power-grid cascade | Detonation wave | Cortical spreading depolarization |
| EventFlip-Rand | Width | 0.02 | 0.02 | 0.02 |
| Flip branches | 4 | 1 | 8 | |
| Windows per update | 32 | 8 | 16 | |
| EventFlip-Hard | Width | 0.02 | 0.02 | 0.04 |
| Flip branches | 4 | 1 | 8 | |
| Windows per update | 32 | 8 | 16 |
| Method | Rollout loss | Shed load (%) | Trip error (lines) | AUC |
|---|---|---|---|---|
| EventFlip-Hard | ||||
| EventFlip-Rand | ||||
| Direct | ||||
| Sigmoid | ||||
| STE | ||||
| AoBG |
| Method | Wave-speed error (%) | Peak pressure (%) |
|---|---|---|
| EventFlip-Hard | ||
| EventFlip-Rand | ||
| Direct | ||
| Sigmoid | ||
| STE | – | |
| AoBG |
| Method | Act.-time error (%) | Mismatch (%) | K + error (ratio) | Penumbra arrival (%) | Penumbra act. time (%) |
|---|---|---|---|---|---|
| EventFlip-Hard | |||||
| EventFlip-Rand | |||||
| Direct | |||||
| Sigmoid | |||||
| STE | |||||
| AoBG |
| Method | No cascade (504) | Cascade, not severe (29) | Severe (67) |
|---|---|---|---|
| EventFlip-Hard | 0.005 | 0.167 | 0.487 |
| EventFlip-Rand | 0.003 | 0.154 | 0.551 |
| Direct | 0.017 | 0.204 | 1.403 |
| Sigmoid | 0.002 | 0.133 | 1.211 |
| STE | 0.007 | 0.221 | 1.452 |
| AoBG | 0.009 | 0.173 | 1.138 |
| Method | Loss | Missed | Wrong generation | Correct | Exact history |
|---|---|---|---|---|---|
| Direct | 0.181 | 1.612 | 0.042 | 0.107 | 83.5% |
| EventFlip-Hard | 0.067 | 0.505 | 0.254 | 1.001 | 90.0% |
| EventFlip-Rand | 0.072 | 0.579 | 0.268 | 0.913 | 89.8% |
| Method | All pairs | Peak loading |
|---|---|---|
| EventFlip-Hard | 0.046 / 0.051 / 0.048 | 0.022 / 0.028 / 0.028 |
| EventFlip-Rand | 0.046 / 0.051 / 0.043 | 0.033 / 0.031 / 0.035 |
| Sigmoid | 0.106 / 0.073 / 0.167 | 0.128 / 0.091 / 0.221 |
| CMA-ES | 0.088 / 0.093 / 0.122 | 0.080 / 0.063 / 0.087 |
| Untrained closure | 0.186 | 0.211 |
| Simulator | Closure | Jump-term var. ratio | Shared noise | Independent noise |
|---|---|---|---|---|
| Power grid | Untrained | 1.00 | 0.072 | 0.072 |
| Trained | 0.94 | |||
| Detonation wave (%) | Untrained | 1.11 | 0.120 | 0.132 |
| Trained | 1.06 | |||
| Cortical spreading depolarization (%) | Untrained | 22.15 | 0.759 | 1.101 |
| Trained | 9.06 |
| Simulator | Untrained closure | Trained closure |
|---|---|---|
| Power grid | 14.1 | 68.4 |
| Detonation wave | 48.0 | 70.7 |
| Cortical spreading depolarization | 4,681 | 30.5 |
| Returned derivative / measured derivative | |||
| Simulator | Gradient of | Untrained closure | Trained closure |
| Power grid | EventFlip-Rand | 1.1–1.2 | 0.34–1.1 |
| Direct | 0 | 0 | |
| Sigmoid | 0.54–2.2 | – | |
| STE | 12–34 | 0.009–0.28 | |
| Detonation wave | EventFlip-Rand | 0.94–1.0 | 0.87–1.0 |
| Simulator | Method | Proposal | Seed 1 | Seed 2 | Seed 3 | Mean |
|---|---|---|---|---|---|---|
| Power grid | EventFlip-Hard | Kernel weight, 5% uniform | 0.062 | 0.064 | 0.075 | 0.067 |
| Uniform | 0.067 | 0.062 | 0.063 | 0.064 | ||
| EventFlip-Rand | Kernel weight, 5% uniform | 0.071 | 0.059 | 0.085 | 0.072 | |
| Uniform | 0.068 | 0.070 | 0.063 | 0.067 | ||
| Detonation wave (%) | EventFlip-Hard | Kernel weight, 5% uniform | 0.109 | 0.092 | 0.112 | 0.104 |
| Uniform | 0.123 | 0.136 | 0.117 | 0.126 |
Differentiable Physics
Differentiable physics makes physical simulators usable for gradient-based learning, parameter estimation, design, and control.
See all 77 papers