Skip to content

Conversation

@hsinhaoHHuang
Copy link
Contributor

@hsinhaoHHuang hsinhaoHHuang commented Jun 23, 2025

Issue

The functions Aux_FindExtrema() and Aux_FindWeightedAverageCenter() can produce inconsistent results when called in the middle of a global simulation step. This occurs because of how they access field data:

  • Intrinsic Fields (fluid, potential): These fields are accessed directly using amr->patch[ amr->FluSg[lv] ][lv][PID]->fluid or amr->patch[ amr->PotSg[lv] ][lv][PID]->pot.
  • Derived Fields (e.g., TOTAL_DENS): These fields are obtained via Prepare_PatchData().

The inconsistency arises because Prepare_PatchData() previously used Time[lv] as its PrepTime. While this works correctly when these Aux_ functions are called after a complete global step (e.g., in Aux_Record_Center()), it fails when they are called mid-step (e.g., between the fluid and Poisson solvers).

In a mid-step scenario, the intrinsic fluid fields (amr->...->fluid) might have already been updated by the fluid solver, but Time[lv] (and thus the data used by Prepare_PatchData() for derived fields) has not yet been updated to reflect the current state. This leads to a mismatch where intrinsic fields represent the current state within the step, while derived fields still represent the state from the beginning of the step.

Changes

Replace all the Time[lv] with amr->FluSgTime[lv][ amr->FluSg[lv] ] inside both Aux_FindExtrema() and Aux_FindWeightedAverageCenter().
This change ensures consistency because amr->FluSgTime[lv][ amr->FluSg[lv] ] is updated together with amr->patch[ amr->FluSg[lv] ][lv][PID]->fluid in the EvolveLevel.cpp.

Verification

One can temporarily add if ( lv == MAX_LEVEL ) Aux_Record_Center(); in somewhere like this between the fluid and Poisson solvers. The real OPT__RECORD_CENTER should be turned off to avoid confusion.
And then run ELBDM/HaloMerger with HaloMerger_Halo_Num 0 and HaloMerger_Soliton_Num 1 in Input__TestProb. It is compiled with PARTICLE but there is no particle in the simulation.

Record__Center before the changes:

#               Time        Step         MaxDens       MaxDens_x       MaxDens_y       MaxDens_z   ...    MaxTotalDens  MaxTotalDens_x  MaxTotalDens_y  MaxTotalDens_z   ...
3.12211591304518e-04           0   1.2022824e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2021755e+05   1.2935895e-01   6.4666840e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2023956e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2022824e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2025088e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2023956e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2026148e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2025088e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2027082e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2026148e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2027878e+05   1.2935895e-01   6.4717380e-02   1.2935895e-01   ...   1.2027082e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2028538e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...   1.2027878e+05   1.2935895e-01   6.4717380e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2029072e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...   1.2028538e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2029492e+05   1.2940949e-01   6.4717380e-02   1.2940949e-01   ...   1.2029072e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2029800e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...   1.2029492e+05   1.2940949e-01   6.4717380e-02   1.2940949e-01   ...

-> The MaxDens and MaxTotalDens are different in the same row even though there is no particle. The MaxTotalDens matches the MaxDens in the previous row.

Record__Center after the changes:

#               Time        Step         MaxDens       MaxDens_x       MaxDens_y       MaxDens_z   ...    MaxTotalDens  MaxTotalDens_x  MaxTotalDens_y  MaxTotalDens_z   ...
3.12211591304518e-04           0   1.2022824e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2022824e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2023956e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2023956e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2025088e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2025088e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2026148e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2026148e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2027082e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...   1.2027082e+05   1.2935895e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2027878e+05   1.2935895e-01   6.4717380e-02   1.2935895e-01   ...   1.2027878e+05   1.2935895e-01   6.4717380e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2028538e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...   1.2028538e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2029072e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...   1.2029072e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...
3.12211591304518e-04           0   1.2029492e+05   1.2940949e-01   6.4717380e-02   1.2940949e-01   ...   1.2029492e+05   1.2940949e-01   6.4717380e-02   1.2940949e-01   ...
3.12211591304518e-04           0   1.2029800e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...   1.2029800e+05   1.2940949e-01   6.4717380e-02   1.2935895e-01   ...

-> The MaxDens and MaxTotalDens are the same in the same row as expected now.

@hyschive hyschive merged commit 9ba22ff into gamer-project:main Jun 23, 2025
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.

2 participants