Flipping the Coins: Training Physical Simulators through Hard Switches

Tianrun Gao, Ruiqi Feng, Haomin Wen, Tailin Wu, Liangqiong Qu, Xiaomeng Huang, Ruiqi ShuarXiv 2026

TL;DR

Nature is full of tipping points—power lines trip, fuel ignites, and brain cells abruptly fire—yet standard deep learning breaks down when trying to train hybrid physical simulators across these sudden, discontinuous jumps. EventFlip solves this by pausing the simulation right at a near-threshold switch, flipping it to play out a counterfactual "what-if" branch, and combining the measured difference with standard smooth gradients. It gives us the best of both worlds: exact physics without throwing away gradient-based learning.

What Problem Does This Paper Address?

In scientific machine learning, researchers love embedding neural networks inside traditional numerical physics solvers to correct coarse-grid errors or learn unknown physical thresholds. To train these hybrid simulators end-to-end, you normally differentiate right through the solver.

Here is the catch: many critical physical systems rely on hard switches (step functions). Once a state crosses a critical value, the rules abruptly change. Because a step function has zero slope everywhere except at the jump (where the derivative doesn't exist), standard automatic differentiation (AD) is completely blind to switch flips. It captures smooth evolution, but misses the massive downstream consequences of a line tripping or a cell igniting.

Existing workarounds force an ugly compromise:

  • Gradient-based relaxations (like replacing the hard switch with a smooth sigmoid or using straight-through estimators) distort the physics and introduce severe gradient bias.
  • Derivative-free methods (like evolution strategies or black-box reinforcement learning) preserve the hard switches, but treat the entire solver as a black box, throwing away rich, continuous path gradients.

Methods

Instead of guessing what happens at a discontinuity or smoothing it away, EventFlip uses the native solver to measure it directly via interventional differencing—essentially taking a finite difference across a discrete switch.

Overview of EventFlip showing smooth path gradients combined with counterfactual flip-and-replay branches

First, the authors add tiny Gaussian noise to every threshold, turning deterministic switches into stochastic coin flips. Mathematically, the gradient of this noise-averaged loss cleanly splits into two pieces:

  1. Path gradient: Standard AD through the continuous physics with all switches held fixed.
  2. Jump term: A policy-gradient-style term weighted by the advantage—how much worse the final loss gets if a specific switch is ON versus OFF.

To estimate that jump term without running a counterfactual branch for every single switch in space and time, EventFlip only samples $M$ switches whose states are hovering inside a narrow "threshold band" near zero. From a stored state along the base trajectory, it flips one selected switch, replays the native solver to the end of the window, and measures the downstream loss difference. Crucially, the counterfactual branch shares the exact same random noise for all subsequent switches so the two runs diverge only because of that single flip.

The authors introduce two variants: EventFlip-Rand (trains with threshold noise, yielding an unbiased gradient estimator of the smoothed objective) and EventFlip-Hard (sets noise to zero, operating directly on the deterministic simulator used at deployment while still provably targeting the complete discontinuous loss).

Datasets

The authors evaluate EventFlip on simulated reference trajectories across three distinct physical benchmarks:

  • Power-grid cascade (IEEE 118-bus network): Neural network predicts latent maximum line ratings. When power flow exceeds a rating, the line permanently trips. Because post-trip flow depends purely on topology, the continuous path gradient is identically zero!
  • 1D Detonation wave: Simulates combustion through unburnt gas where cells ignite past a critical temperature. A neural closure corrects coarse-grid fluxes to match a fine-grid reference.
  • Cortical spreading depolarization: Models post-stroke wave propagation of extracellular $\text{K}^+$ across a $64 \times 64$ grid of brain tissue (featuring an infarct core and vulnerable penumbra), where cells irreversibly activate past an excitation threshold.

Key Insights and Ideas

  • Existing baselines cannot generalize across switching regimes: A baseline that shines on one physics problem completely collapses on another (e.g., Sigmoid relaxation is the strongest baseline on detonation waves, yet fails to learn at all on brain depolarization, overestimating directional derivatives by $10^4\text{--}10^7\times$). EventFlip is the only method that succeeds across all regimes.
  • Unbiasedness means nothing without credit assignment: If you run counterfactual branches without sharing future threshold noise—or try to estimate switch advantages without flip branches (REINFORCE-style)—gradient variance explodes (up to $4,681\times$ higher) when hundreds of switches sit near their thresholds simultaneously. Isolating one switch at a time is the secret sauce that lets EventFlip scale.
  • Recovering invisible physics: On the power grid, where standard AD gets literally zero gradient signal, EventFlip's jump term alone accurately reverse-engineers hidden physical line ratings just from observing downstream blackouts.

Key Results

Under an identical forward evaluation budget (750k–800k solver steps), both EventFlip variants beat all 12 gradient-based and derivative-free baselines on every task:

  • Error reduction: EventFlip reduces test error over the strongest baseline on each task by factors of 1.5× (power-grid rollout loss: 0.067 vs. CMA-ES's 0.101), 1.4× (detonation wave-speed error: 0.104% vs. Sigmoid's 0.150%), and 1.4× (cortical activation-time error: 0.759% vs. Closure-RL's 1.050%).
  • Sample efficiency: EventFlip beats the full-budget score of every power-grid baseline within just the first 100k steps (under 15% of the budget), and matches Sigmoid's final detonation accuracy in 1/4 of the budget.
Qualitative results comparing EventFlip to baselines on power-grid cascades and cortical spreading depolarization

Limitations and Future Work

  • Compute overhead of full replays: Each flip branch currently re-runs the entire spatial domain to the end of the training window. Because physical perturbations often travel locally at finite speeds, future work could exploit spatiotemporal locality to reuse unaffected grid computations from the base trajectory.
  • Theoretical guarantees: While EventFlip-Hard is proven consistent with the discontinuous loss along parameter paths, establishing formal optimization convergence guarantees for coupling deterministic jump differences with hard path gradients remains an open theoretical question.
  • Scaling up: The authors aim to test EventFlip on larger, more complex solvers and higher-capacity neural closures.

Summary updated

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.
BibTeX
@misc{gao2026flipping,
  author = {Tianrun Gao and Ruiqi Feng and Haomin Wen and Tailin Wu and Liangqiong Qu and Xiaomeng Huang and Ruiqi Shu},
  title = {{Flipping the Coins: Training Physical Simulators through Hard Switches}},
  year = {2026},
  eprint = {2611.33716},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.33716},
}
Figure