# Is the run long enough?

Channel: release; package: 2.2.0; source: 084b5032b7d014f12b65b4b947cf61a840970bf4

Page: https://remilab.ai/rfx/check/simulation-time/
Source: https://github.com/bk-squared/rfx/blob/084b5032b7d014f12b65b4b947cf61a840970bf4/docs/public/check/simulation-time.mdx

Every frequency-domain result rfx gives you is a Fourier transform of a time
signal of finite length: S-parameters, DFT planes, flux, far fields and
resonances. If the run ends while the structure is still ringing, each of them
misses the part of the signal beyond the end, and nothing in the curve says so.
A resonator with quality factor Q at frequency f decays over roughly
Q / (π f) seconds, so a high-Q structure needs a long run.

This page shows how to check that the simulation time was long enough for the
frequencies you read. Run it on every result you intend to report, after the
[check of the model on the grid](https://remilab.ai/rfx/check/model-on-grid/) and before the
[mesh convergence study](https://remilab.ai/rfx/check/mesh-convergence/).

## Set the simulation time

Set the simulation time with one of three `run()` arguments:

| Argument | Meaning |
|---|---|
| `n_steps=N` | exactly N time steps |
| `num_periods=P` | P periods at `freq_max` (default 20, used when nothing else is given) |
| `until_decay=1e-3` | step until the field energy left in the domain outside the absorbers after the source has ended falls to that fraction of its largest value since then, up to `decay_max_steps` (see [The decay stop](#the-decay-stop)) |

## Read the settling check

After the run, read `result.settling_witness["status"]`. The model below is a
lossy dielectric slab between a 50 Ω lumped port and a probe, on a coarse 2 mm
mesh so it runs in seconds. It is run for three simulation times:

```python
import numpy as np
from rfx import Box, GaussianPulse, Simulation

def build():
    sim = Simulation(freq_max=4e9, domain=(0.06, 0.05, 0.04), dx=2e-3,
                     boundary="cpml", cpml_layers=8)
    sim.add_material("slab", eps_r=4.0, sigma=0.01)
    sim.add(Box((0.024, 0.019, 0.014), (0.036, 0.031, 0.026)), material="slab")
    sim.add_port((0.018, 0.025, 0.020), "ez", impedance=50.0,
                 waveform=GaussianPulse(f0=3e9, bandwidth=0.8))
    sim.add_probe((0.044, 0.025, 0.020), "ez")
    return sim

for n in (200, 300, 400):
    result = build().run(n_steps=n, s_param_freqs=np.linspace(2e9, 4e9, 11),
                         skip_preflight=True)
    check = result.settling_witness
    print(n, check["status"], f"{result.settling_db:.1f} dB")
```

```text
200 undetermined nan dB
300 pass -103.4 dB
400 pass -96.1 dB
```

At 200 steps the source pulse has not ended inside the run, so the check
cannot be formed and says `undetermined` (its `reason` says the source end is
unavailable, once for each signal judged). From 300 steps on it passes. Both
values are far below −40 dB. The 300-step value changes with the CPU: it read
−103.4 dB on one and −71.8 dB on another. The dB figure does not measure
convergence. Read the status, not the number.

The four states:

| `status` | Meaning | What to do |
|---|---|---|
| `pass` | At every frequency the result reads, the estimated part of the value beyond the end of the run is at most 1 % of the value (−40 dB). | Nothing for the simulation time. Check the mesh next. |
| `fail` | At some frequency that share is above 1 %. `worst_freq_hz` names it. | Run longer, or use [tail extrapolation](#tail-extrapolation-by-matrix-pencil). |
| `undetermined` | The check could not be formed: the source was still on at the end of the run, the field had not yet passed a far-field box, DFT plane, flux monitor or current-moment monitor (`coverage` names it, and `run()` warns once), the part of the run after the source is too short to fit, no decaying term was found, the two fits disagree on whether a frequency passes, a growing term was found, or the fit would exceed its memory bound. `reason` says which. | Not a pass. Run longer; if the reason is a growing term, look for an instability first. |
| `absent` | The run reads nothing at a frequency (no S-parameters, DFT planes, flux or far field; only probe time series), or a far-field box or DFT plane is read with no probe recording the fields it comes from. | Not a pass. With probe time series alone, judge their decay yourself, as in [Quick Start](https://remilab.ai/rfx/start/first-simulation/). For a far-field box or DFT plane, add a probe away from every source and port, somewhere the field is live. |

`result.settling_db` is the worst share in dB; it is NaN or `None` when the
check was not formed. One run's status is a statement about that run. When the simulation
time matters to a result you report, read the result itself over at least three
simulation times and say how it moves, as the
[terms for agreement with a reference](https://remilab.ai/rfx/check/agreement/) ask.

## The decay stop

`run(until_decay=1e-3)` steps in chunks. Once the source has ended, it reads
the field energy stored in every cell outside the absorbers every
`decay_check_interval` steps, and stops when that energy is below the given
fraction of the largest value it has had since the source ended, at two checks
in a row (`decay_energy_consecutive`, at least 2), or at `decay_max_steps`. The
source's own near field does not set the reference, so a resonant structure
runs as long as the threshold asks. Each run records what ended it in
`result.diagnostics` (`until_decay.stop`): the threshold, `decay_max_steps`,
or the float32 floor. When the energy stops falling at about 10⁻¹² of the
source's peak energy (single-precision rounding), the run stops there and
warns.

In a lossy dielectric the run can last longer than the field rings: charge
moved onto the dielectric's surfaces during the drive relaxes with the time
constant ε/σ afterwards. That field is quasi-static, outside the band, and the
record stays correct; the stop record says what ended the run.

Closed PEC boxes on the uniform mesh use the same after-drive energy rule.
A lossy box can reach the threshold; a lossless box can reach `decay_max_steps`
with energy still inside. The cap message reports the after-drive reference
as a fraction of the drive's peak energy. The public graded-mesh `run()` still
refuses closed-box `until_decay`; use fixed `n_steps` there and in convergence
studies. Read the settling check after a decay-stopped run, as after any other.

On absorbing boundaries, `run(until_decay=..., radiated_flux_box=((x0, y0, z0),
(x1, y1, z1)))` stops on the power flowing out through a box around the
structure instead of the energy inside. Like the energy stop it starts reading
only after the drive has ended. Field that does not radiate does not hold it
back. `radiated_flux_box` needs a positive `until_decay` and an absorbing
boundary (`cpml` or `upml`); otherwise `run()` raises `ValueError`. The default pulse leaves no static charge at a current source,
so its field does not hold back either stop.

## Tail extrapolation by matrix pencil

When a run was cut while a resonance still rings, rfx can add the missing tail
instead of running longer. After the source is off, every port voltage and
current is a sum of damped oscillations. rfx fits their poles to the late part
of the time signal by the matrix pencil method and adds each spectrum's part
beyond the end of the run in closed form. rfx calls this ring-down completion;
it is the same idea as Prony or matrix-pencil extrapolation of a time signal,
or the AR filter of the CST transient solver.

It applies to wire ports (`add_port(..., extent=...)`) and runs a fixed
simulation time:

```text
from rfx.ringdown import RingdownSpec

result = sim.run(n_steps=n, ringdown=RingdownSpec())
s_completed = result.ringdown.s_params      # same frequencies as result.s_params
print(result.ringdown.report.summary())     # fitted poles and the checks
```

`report.ok` tells you whether the completion is usable. `s_params` is `None`
when no completion was produced, and the plain result is returned with the
failed check named in the report and a warning. The options you are most
likely to change, all on `RingdownSpec`:

| Option | Default | Meaning |
|---|---|---|
| `window_start` | 0.5 | The fit uses the last part of the run, from `window_start × T` to the end. The source must be off there. |
| `witness_tol` | 1e-3 | The largest difference in \|S\| between the two fits (below) that still counts as agreement. |
| `identification_probes` | none | Extra field probes, `((x, y, z), "ez")` pairs, used only to find the poles. Add one inside a weakly coupled high-Q resonator, which the port alone may not see. |

`run(..., ringdown=RingdownSpec(), until_identified=True)` treats `n_steps` as
the longest run. It stops at the first simulation time where the source is off
over the fit windows, the two fits (below) agree within `witness_tol` at
two checks in a row, the run is long enough for the slowest pole that can
still move S, and the completed S is passive with no growing pole. `result.ringdown.stop` says where
it stopped, or which condition failed when it reached `n_steps`.

## How the checks work

**The settling check** (`settling_witness`). For each frequency the result reads,
rfx fits the poles still present in the time signal after the source ends, on
the second half of the run (or from the source's end if that is later), and
computes the part of the value that lies beyond the end of the run. It does the
same fit on a second window and requires the two to agree on which frequencies
pass. For a plane wave, the source's end for each record is the end of the
waveform plus the travel time from the source plane to each corner of the
plane-wave box and from there to that record, so an echo from the target is not read as ringing. A term that does not change with time (a static field left by the source)
is exempt, because it does not ring. A growing term above 1 % of a frequency's
value makes the result `undetermined`. A pole whose decay or growth per time
step is too small for the fit to resolve (below ln(1 + 10⁻⁶) divided by the
fit's decimation factor) is judged the same way as a growing term: it does not
lengthen the time the check looks ahead, which is the longer of the run and the
time the slowest resolved decaying pole that still moves a value needs to
fall to 1 %, and its tail over twice that time must stay within 1 % of the
frequency's value. When that time is too long for the fit to resolve growth
over it, the result is `undetermined` as well. The frequencies read are those every
spectral output of the result stores: S-parameter frequencies, DFT planes, flux
monitors and the far-field box.

The signals judged are the port voltages and currents behind `run()`'s
S-parameters, the waveguide port signals, and your probes. A probe on a source
or port cell measures how the source turns off, not how the structure rings, so
rfx uses the other probes when there are any and marks the result
`source_dominated` when there are not. The microstrip, waveguide and coaxial S-parameter calculators run the
same check on their own port signals, per drive, and warn when a drive does not
pass.

**Tail extrapolation** (`result.ringdown.report`). Before completing, rfx
rebuilds the port voltages and currents from the run's field samples and checks
that they reproduce the run's own frequency-domain accumulators. After
completing, it fits the poles a second time on a window that starts twice as
early and completes the same run with them; the largest difference of the two
answers is the check, judged against `witness_tol`. When the source is still
on in that earlier window, a shorter second window is compared instead. The
report also checks that no completed |S| exceeds 1 by more than
`passivity_tol` and that no growing pole was kept. A pole too slow for the fit
to resolve, judged with `RingdownSpec.unit_tol` in place of 10⁻⁶, is left out
of the completion unless it is static: its tail is judged as in the settling
check, not added to the spectrum. None of these checks is an
error bound.

## Inputs rfx refuses

| Input | Solver path | What happens | Instead |
|---|---|---|---|
| `until_decay` | multi-device (`devices=`), subgridded | refused before the first step: these paths run exactly `n_steps` | set `n_steps` or `num_periods` |
| `radiated_flux_box` without a positive `until_decay`, or on a closed boundary | all | `ValueError`: the box only selects the radiated-flux stop | a positive `until_decay` with `cpml` or `upml`; for a fixed-length measurement, `add_flux_monitor()` |
| `until_decay` with a plane-wave source driven by a `CustomWaveform` | all | refused: the waveform has no time at which it is off | set `n_steps` |
| `until_decay` | graded mesh with closed boundaries | refused by public `run()`; the low-level graded energy loop is available | set `n_steps`, or use absorbing boundaries |
| a flux monitor with `dft_window="hann"` or `"tukey"`, with `until_decay` | uniform and graded mesh | refused: a window needs the run's length in advance | the default `dft_window="rect"`, or `n_steps` |
| `decay_energy_consecutive` below 2 | uniform and graded mesh | refused | 2 or more |
| `ringdown=` with `until_decay` | all | refused: the completion needs a run of fixed length | pass `n_steps` or `num_periods` |
| `ringdown=` with `devices=` | multi-device | refused | run on one device |
| `ringdown=` | subgridded, periodic axes, a plane-wave source, a Kerr (χ³) material, `mode` other than `"3d"`, `stencil_order=4` | refused, with the reason | run without `ringdown=` and lengthen the run |
| `ringdown=` with a microstrip, waveguide, coaxial or Floquet port, or a one-cell lumped port (`add_port` without `extent=`) | all | refused: only wire ports are completed | declare a wire port, or lengthen the run |
| `ringdown=` with `reference_plane_cells=` on a port | all | refused | drop `reference_plane_cells` |
| `ringdown=` with no driven wire port | all | `ValueError` | declare and drive a wire port |
| `ringdown=` with more than one wire port | uniform mesh | refused: the uniform path builds the S-matrix from separate per-port runs | use a graded mesh, where one run fills the driven column |
| `ringdown=` with `compute_s_params=False` | all | refused: there is nothing to complete | drop one of the two |
| `ringdown=` when a source is on in the fit window | all | refused before the run | delay `window_start`, or run longer |
| `until_identified=True` without `ringdown=` | all | `ValueError` | pass `ringdown=RingdownSpec(...)` |
| `until_identified=True` with `until_decay=` | all | `ValueError`: two rules for where the record ends | pass one; with `until_identified`, `n_steps` is the longest record |
| `until_identified=True` with `snapshot=`, a DFT plane or a flux monitor | all | refused: their accumulators are laid out for `n_steps` before the run | `run(n_steps=..., ringdown=...)` with a fixed record |
| `until_identified=True` with an NTFF box or a current-moment monitor | all | refused: the stop completes only the wire-port S, so the far field would come from a record cut while the structure still rings | `run(n_steps=..., ringdown=...)` with a record long enough for the far field |

## Where it is tested

- The −40 dB limit is `_SETTLING_WITNESS_DB` in `rfx/measurement/sparam_advisories.py`, pinned
  by `tests/unit/sparams/test_settling_witness.py`; the per-frequency check is
  `rfx/sparams/_tail_witness.py`.
- On signals built from known damped oscillations, the completion reproduces
  the DFT of the infinitely long signal from a run that stops while they are
  still large, and finds the poles they were made of
  (`tests/unit/sparams/test_ringdown_oracles.py`).

## See also

- [When does a result agree with a reference?](https://remilab.ai/rfx/check/agreement/): judging
  a result over three simulation times.
- [Gradients](https://remilab.ai/rfx/check/gradients/): a gradient needs its own check of the
  simulation time.
- [Simulation time for gradients](https://remilab.ai/rfx/check/gradient-simulation-time/): at
  a resonance that dominates the value, a 1 % gradient needs 1.44 times the
  simulation time of a 1 % value.
- [Simulation API](https://remilab.ai/rfx/api/simulation/): `run()` arguments.
- [rfx terms](https://remilab.ai/rfx/start/terms/): settling check, ring-down completion.
