Skip to content

raw rho coarse dump indexes the wrong component #6648

@WeiqunZhang

Description

@WeiqunZhang

Summary

FlushFormatPlotfile::WriteAllRawFields unconditionally calls WriteCoarseScalar("rho", ..., /*icomp=*/1) when dumping raw coarse rho data (Diagnostics/FlushFormats/FlushFormatPlotfile.cpp:688). That hard-coded component index only exists when rho_cp stores both old and new values (i.e. rho_ncomps >= 2). In the default electromagnetic configuration (do_dive_cleaning = 0 and PSATD JRhom disabled) rho_cp has a single component, so trying to alias component 1 throws in AMReX (0 <= comp && comp < ncomp). Any simulation with AMR, diagnostics.<diag>.plot_raw_fields = 1, and the standard rho layout hits this abort as soon as the diagnostic writes level > 0.

Impact

Raw-field plotfiles cannot be produced for multi-level runs unless dive cleaning/JRhom are enabled. The code aborts with “invalid component index” before any data is written, so the diagnostic is useless for the default configuration and automated workflows get stuck.

Fix

Pick the newest rho component dynamically instead of assuming it lives at slot 1. The fine-patch path already aliases the last WarpX::ncomps components to get the “new” rho. Reuse the same strategy for coarse data: compute const int rho_new_comp = F_cp->nComp() - WarpX::ncomps; (clamped at zero) and pass that into WriteCoarseScalar. Also assert that icomp < F_cp->nComp() inside WriteCoarseScalar so future mistakes fail loudly.

Patch

@@ Diagnostics/FlushFormats/FlushFormatPlotfile.cpp:532-559 @@ WriteCoarseScalar(
-    if (lev == 0) {
+    if (lev == 0) {
         WriteZeroRawMF( *F_fp, dm, filename, level_prefix, field_name+"_cp", lev, ng );
     } else {
-        // Create an alias to the component `icomp` of F_cp
-        const MultiFab F_comp(*F_cp, amrex::make_alias, icomp, 1);
+        const int ncomp_cp = F_cp->nComp();
+        WARPX_ALWAYS_ASSERT_WITH_MESSAGE(icomp >= 0 && icomp < ncomp_cp,
+            "WriteCoarseScalar: requested component out of range");
+        const MultiFab F_comp(*F_cp, amrex::make_alias, icomp, 1);
@@
-            WriteCoarseScalar("rho", warpx.m_fields.get(FieldType::rho_cp, lev), warpx.m_fields.get(FieldType::rho_fp, lev),
-                dm, raw_pltname, default_level_prefix, lev, plot_raw_fields_guards, 1);
+            auto const* rho_cp = warpx.m_fields.get(FieldType::rho_cp, lev);
+            int const rho_new_comp = amrex::max(0, rho_cp->nComp() - WarpX::ncomps);
+            WriteCoarseScalar("rho", rho_cp, warpx.m_fields.get(FieldType::rho_fp, lev),
+                dm, raw_pltname, default_level_prefix, lev, plot_raw_fields_guards, rho_new_comp);
         }

Prepared by Codex

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions