Skip to content

Use the solver's iteration count for plot_source_wave time windows - #875

Merged
agianno merged 2 commits into
gprMax:masterfrom
SajalDevX:fix/plot-source-wave-iterations
Oct 7, 2026
Merged

agianno merged 2 commits into
gprMax:masterfrom
SajalDevX:fix/plot-source-wave-iterations

Conversation

@SajalDevX

Copy link
Copy Markdown
Contributor

Fixes #874

plot_source_wave.check_timewindow worked out the number of iterations for a time window in seconds with round_value(timewindow / dt) + 1. The solver's #time_window has used int(np.ceil(time / dt)) + 1 since b2c3e54, so the tool could use one iteration fewer than the model. When that happens the plotted waveform stops before the requested window and the logged iteration count is wrong. The README example (6 ns, dt = 1.926 ps) plots 3116 samples ending at 5.9995 ns, while gprMax runs 3117.

Changes:

  • check_timewindow now uses the same ceil formula as TimeWindow.build. The round_value import was only used there, so I removed it.
  • Added test_plot_source_wave_time_window_matches_solver to tests/toolboxes/test_plotting_outputs.py. It builds a TimeWindow on a stub model and checks that check_timewindow gives the same iteration count, and that the last sample reaches the requested time.

Testing:

  • pytest tests/toolboxes/test_plotting_outputs.py: 5 passed. Before the fix, the new test fails for both cases (3116 == 3117, 2728 == 2729).
  • pytest tests/waveforms/test_gauspulse_workflow.py -k standard_plotting tests/utilities/test_toolbox_namespace.py: passed.
  • I ran black and isort (line length 100) on both changed files. The isort/black pass also removed an extra blank line in the imports and a trailing blank line at the end of the test file, which were already there.

@agianno

agianno commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks—the change to ceil correctly matches the solver’s time-window-to-iteration calculation.

Further checking found two pre-existing issues, not regressions introduced by this PR, which would be useful to address here:

  1. Generate the time axis using integer-step multiplication:
    time = np.arange(iterations, dtype=float) * dt
    With the current linspace, iterations=234 and dt=1e-12 produce a second timestamp slightly below dt. The impulse evaluator consequently returns two nonzero samples instead of one. Please add a regression test checking the actual plotted impulse.
  2. Correct the FFT fallback range for odd sample counts. When the requested range covers all nonnegative frequencies, len(freqs) // 2 omits the last positive-frequency bin for odd lengths. For example, nine samples have five nonnegative bins, but only four are plotted. Please derive the fallback endpoint from the nonnegative-frequency indices and test both odd and even lengths.
    Please also clarify that this function previews the waveform formula on whole timesteps; it does not read simulation outputs or reproduce every source’s sampling and gating. Output lengths legitimately differ: finite-resistance voltage ports have N−1 aligned samples, whereas receiver histories have N. Those output conventions should remain unchanged. In FDTD not all fields are sampled at the same time instance and not all sources are evaluated at the same time instance. Their time instance that a source is obtained depends on the physics of the source model. For previewing purposes of a waveform the given approach and corrections are however OK.

With these additions and tests, this would cover both the iteration-count discrepancy and the related preview edge cases.

@SajalDevX

Copy link
Copy Markdown
Contributor Author

Thanks for checking this so carefully. I've pushed a follow-up commit covering all three points:

  1. Time axis. It's now np.arange(iterations, dtype=float) * dt instead of linspace. I added a test that calls mpl_plot headless with an impulse, iterations=234 and dt=1e-12, and checks the plotted line: exactly one nonzero sample (at t=0), and the time values equal n * dt. Before the change the test failed with two nonzero samples.

  2. FFT fallback range. When the requested range covers all nonnegative frequencies, the end point now comes from the nonnegative-frequency indices (positive[-1] + 1) instead of len(freqs) // 2. The new test is parametrized over 9 and 10 samples and checks that the plotted frequencies are all the nonnegative bins. Before the change, 9 samples plotted 4 of 5 bins and 10 samples were already correct, so the result for even lengths doesn't change.

  3. Scope. The mpl_plot docstring and the plotting README now say that the tool previews the waveform formula on whole timesteps. They also say it doesn't read simulation outputs or reproduce each source's sampling and gating, and that output files keep their own conventions, e.g. finite-resistance voltage ports have N-1 aligned samples and receiver histories have N. I haven't changed any output conventions.

tests/toolboxes/test_plotting_outputs.py passes (8 tests), and so do the related plotting, utilities and waveform tests. black and isort are clean at line length 100.

@agianno

agianno commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks—all three requested changes look correct. I independently verified the regression tests against the old and updated code, and the related local tests pass.
Since #876 has now merged, please update your branch against master and resolve the conflict in tests/toolboxes/test_plotting_outputs.py, preserving both your waveform tests and #876’s CLI gathered-filename test. Once the updated branch passes CI, this is ready to merge.

check_timewindow rounded timewindow/dt to the nearest integer, but #time_window has used ceil since b2c3e54 so the simulation always reaches the requested time. With the README example (6 ns, dt 1.926 ps) the tool plotted 3116 samples ending at 5.9995 ns while gprMax runs 3117. Use the same ceil formula so the plotted window matches the model.
Build the time axis as np.arange(iterations, dtype=float) * dt instead of
np.linspace. With iterations=234 and dt=1e-12 linspace put the second
sample at 9.999999999999998e-13, just below dt, so the impulse evaluator
returned 1 at both of the first two samples.

When the requested FFT range covers every nonnegative frequency, end the
plotted range after the last nonnegative-frequency index rather than at
len(freqs) // 2, which dropped the last positive bin for odd lengths
(e.g. nine samples have five nonnegative bins but only four were shown).

Document that the tool previews the waveform formula on whole timesteps
and does not read simulation outputs or reproduce each source's sampling
and gating, so output files keep their own lengths and alignment.

Add tests for the plotted impulse and for the FFT range with odd and
even sample counts.
@SajalDevX
SajalDevX force-pushed the fix/plot-source-wave-iterations branch from 27d531e to 4438ea0 Compare October 7, 2026 00:37
@SajalDevX

Copy link
Copy Markdown
Contributor Author

Thanks for verifying them. I've rebased the branch onto master and resolved the conflict in tests/toolboxes/test_plotting_outputs.py, keeping both the waveform tests from this PR and the gathered-filename CLI test from #876. All 9 tests in that file pass locally, and black/isort are clean.

@agianno
agianno merged commit 5d9085a into gprMax:master Oct 7, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plot_source_wave uses a different iteration count from #time_window, so the plot stops short of the requested time window

2 participants