Skip to content

Conversation

@ax3l
Copy link
Member

@ax3l ax3l commented Nov 16, 2025

The math of a variable-window and variable-start/stop moving window is duplicated partly over the code base. This caused bugs. Refactoring to avoid bugs in the future.

ax3l added 2 commits November 16, 2025 16:31
After restarts, all diagnostics lo/hi were not properly
restored. This causes new checkpoints and diagnostics to
become corrupted, as the wrong spatial data gets filtered.

This fixes it.
The math of a variable-window and variable-start/stop
moving window is duplicated partly over the code base.
This caused bugs. Refactoring to avoid bugs in the future.
@ax3l ax3l added cleaning Clean code, improve readability component: core Core WarpX functionality component: checkpoint/restart Checkpointing & restarts component: boosted frame boosted frame components & logic labels Nov 16, 2025
Cell-aligned shifts are used in a few moving window
places and hard-to-read code.
Comment on lines +541 to +546
const amrex::Real total_shift = WarpX::CalculateMovingWindowShift(0, last_replayed_step, warpx.getdt(0));

if (total_shift > 0.0_rt) {
// Use the unified helper function to calculate physical shift aligned to cell boundaries
const amrex::Real physical_shift = WarpX::CalculateMovingWindowAlignedShift(
total_shift, warpx.Geom(0).CellSize(moving_dir));
Copy link
Member Author

@ax3l ax3l Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we refactor this logic... Does restart (in development) even work with m_galilean_shift being active?

I would like to share even more logic that resides in FullDiagnostics::MovingWindowAndGalileanDomainShift here...

if (WarpX::do_moving_window) {
// current moving_window location. Account for start_moving_window_step and
// end_moving_window_step to only shift based on steps where the window was active.
if (WarpX::do_moving_window && warpx.getistep(0) > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was warpx.getistep(0) > 0 added here?
Do we expect negative step number in WarpX?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added for only catching restarts, this PR is still wip as an idea to refactor. Forgot the title

m_hi[moving_dir] += shift_num_base * warpx.Geom(0).CellSize(moving_dir);
const int current_step = warpx.getistep(0);

// Note: current_step is the checkpoint step, but the replay loop goes from 0 to current_step - 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "replay loop" mean here? Do you mean "restart"?

const amrex::Real total_shift = WarpX::CalculateMovingWindowShift(0, last_replayed_step, warpx.getdt(0));

if (total_shift > 0.0_rt) {
// Use the unified helper function to calculate physical shift aligned to cell boundaries
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Use the unified helper function to calculate physical shift aligned to cell boundaries
// Calculate physical shift aligned to cell boundaries

const amrex::Real* geom_dx = m_geom_output[0][0].CellSize();
const auto num_shift_base = static_cast<int>((moving_window_x - cur_lo[moving_dir])
/ geom_dx[moving_dir]);
// Use the unified helper function to calculate physical shift aligned to cell boundaries
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Use the unified helper function to calculate physical shift aligned to cell boundaries
// Calculate physical shift aligned to cell boundaries

amrex::Real
WarpX::CalculateMovingWindowShiftPerStep (amrex::Real dt)
{
// Calculate the shift per step (same formula as used in MoveWindow)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Calculate the shift per step (same formula as used in MoveWindow)
// Calculate the shift per step

The formula does not exist anymore in MoveWindow, so this comment is confusing.
(Instead we now call CalculateMovingWindowShiftPerStep in MoveWindow ; but I don't think comments should point out where the function is called - or if they should, then we also need to say that this function is called in MovingWindowAndGalileanDomainShift

/** Calculate the shift per step for the moving window
*
* This function calculates how much the moving window shifts in one timestep.
* This is the same calculation used in MoveWindow.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used in MoveWindow, so it is confusing to say that this is the "same" calculation used in MoveWindow.

@ax3l ax3l changed the title Deduplicate Moving Window Logic [WIP] Deduplicate Moving Window Logic Nov 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleaning Clean code, improve readability component: boosted frame boosted frame components & logic component: checkpoint/restart Checkpointing & restarts component: core Core WarpX functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants