Skip to content

dermgvort: EB one-sided vorticity stencil reads covered cells two away in thin gaps #179

Closed

Description

@WeiqunZhang

Severity: low · Category: correctness · Fix order: 21 of 21 — fix this last.

Filenames are numbered in reverse fix order: 001 = fix last, 021 = fix first. This file is 001.

Location: Source/NS_derive.cpp:136

Based on commit 9bf664bf (line numbers refer to that tree).

The defect

What is wrong — The EB one-sided vorticity stencils in dermgvort read velocity at distance-2 cells (e.g. dat_arr(i-2,j,k,1)) without checking those cells are uncovered; EBCellFlag::isConnected only certifies the immediate neighbor, and the chosen branch never verifies the opposite-side neighbors it reads.

Why it matters

EB build with a fluid gap <=2 cells wide (thin channel between EB surfaces, common in under-resolved geometry): cell i has i+1 covered and i-2 covered, so the left-biased stencil reads covered-cell garbage (state in covered cells holds ~1e40-scale values). mag_vort in that cut cell is garbage; with the ns.vorticity_greater refinement criterion (NS_error.cpp) this spuriously tags/refines those regions, and plotfiles show junk vorticity.

Suggested fix

Guard every point the stencil touches, not just the immediate neighbour: EBCellFlag's connection bits span only the 3^D neighbourhood (AMReX_EBCellFlag.H:135), so a false isConnected(1,0,0) says nothing about i-2. AMReX guards its own wider stencils exactly this way — amrex_calc_slopes_eb_given_A_grown tests !flag(i+ii,j+jj,0).isCovered() on every cell of the grown footprint, and amrex_overwrite_with_regular_slopes takes the 4th-order form only when vfrac==1 at i±2. So use the quadratic one-sided form only when both offset cells are uncovered, drop to a two-point first-order difference otherwise, and zero that derivative when both sides fail (one-cell gap). The added flag lookups are in bounds: the derive requests two grow cells (NS_setup.cpp:449) and the flag fab is asserted to contain the fab box (AMReX_EBFArrayBox.cpp:24).

Maintainer calls: plain isCovered tests versus a connectivity chain (which also rejects fluid across a thin wall), and fixing x/y/z in 2D and 3D in one patch. The pre-port DERIVE_ND.F90 dermgvort had no EB branch at all — its one-sided forms were for EXT_DIR/HOEXTRAP domain faces — so nothing here is inherited Fortran behaviour. Since mag_vort drives ns.vorticity_greater tagging, EB regression grids may shift.

--- a/Source/NS_derive.cpp
+++ b/Source/NS_derive.cpp
@@ -131,25 +131,51 @@
         // Need to check if there are covered cells in neighbours --
         // -- if so, use one-sided difference computation (but still quadratic)
         if (!flag_fab(i,j,k).isConnected( 1,0,0)) {
-          vx = - (c0 * dat_arr(i  ,j,k,1)
-              + c1 * dat_arr(i-1,j,k,1)
-              + c2 * dat_arr(i-2,j,k,1)) * idx;
+          if (flag_fab(i,j,k).isConnected(-1,0,0)) {
+            if (flag_fab(i-1,j,k).isConnected(-1,0,0)) {
+              vx = - (c0 * dat_arr(i  ,j,k,1)
+                  + c1 * dat_arr(i-1,j,k,1)
+                  + c2 * dat_arr(i-2,j,k,1)) * idx;
+            } else {
+              // Only one uncovered cell that way, drop to linear
+              vx = (dat_arr(i  ,j,k,1) - dat_arr(i-1,j,k,1)) * idx;
+            }
+          }
+          // Covered on both sides, leave the derivative at zero
         } else if (!flag_fab(i,j,k).isConnected(-1,0,0)) {
-          vx = (c0 * dat_arr(i  ,j,k,1)
-            + c1 * dat_arr(i+1,j,k,1)
-            + c2 * dat_arr(i+2,j,k,1)) * idx;
+          if (flag_fab(i+1,j,k).isConnected( 1,0,0)) {
+            vx = (c0 * dat_arr(i  ,j,k,1)
+              + c1 * dat_arr(i+1,j,k,1)
+              + c2 * dat_arr(i+2,j,k,1)) * idx;
+          } else {
+            // Only one uncovered cell that way, drop to linear
+            vx = (dat_arr(i+1,j,k,1) - dat_arr(i  ,j,k,1)) * idx;
+          }
         } else {
           vx = 0.5 * (dat_arr(i+1,j,k,1) - dat_arr(i-1,j,k,1)) * idx;
         }
         // Do the same in y-direction
         if (!flag_fab(i,j,k).isConnected( 0,1,0)) {
-          uy = - (c0 * dat_arr(i,j  ,k,0)
-              + c1 * dat_arr(i,j-1,k,0)
-              + c2 * dat_arr(i,j-2,k,0)) * idy;
+          if (flag_fab(i,j,k).isConnected(0,-1,0)) {
+            if (flag_fab(i,j-1,k).isConnected(0,-1,0)) {
+              uy = - (c0 * dat_arr(i,j  ,k,0)
+                  + c1 * dat_arr(i,j-1,k,0)
+                  + c2 * dat_arr(i,j-2,k,0)) * idy;
+            } else {
+              // Only one uncovered cell that way, drop to linear
+              uy = (dat_arr(i,j  ,k,0) - dat_arr(i,j-1,k,0)) * idy;
+            }
+          }
+          // Covered on both sides, leave the derivative at zero
         } else if (!flag_fab(i,j,k).isConnected(0,-1,0)) {
-          uy = (c0 * dat_arr(i,j  ,k,0)
-            + c1 * dat_arr(i,j+1,k,0)
-            + c2 * dat_arr(i,j+2,k,0)) * idy;
+          if (flag_fab(i,j+1,k).isConnected( 0,1,0)) {
+            uy = (c0 * dat_arr(i,j  ,k,0)
+              + c1 * dat_arr(i,j+1,k,0)
+              + c2 * dat_arr(i,j+2,k,0)) * idy;
+          } else {
+            // Only one uncovered cell that way, drop to linear
+            uy = (dat_arr(i,j+1,k,0) - dat_arr(i,j  ,k,0)) * idy;
+          }
         } else {
           uy = 0.5 * (dat_arr(i,j+1,k,0) - dat_arr(i,j-1,k,0)) * idy;
         }
@@ -167,20 +193,35 @@
         // -- if so, use one-sided difference computation (but still quadratic)
         if (!flag_fab(i,j,k).isConnected( 1,0,0)) {
           // Covered cell to the right, go fish left
-          vx = - (c0 * dat_arr(i  ,j,k,1)
-              + c1 * dat_arr(i-1,j,k,1)
-              + c2 * dat_arr(i-2,j,k,1)) * idx;
-          wx = - (c0 * dat_arr(i  ,j,k,2)
-              + c1 * dat_arr(i-1,j,k,2)
-              + c2 * dat_arr(i-2,j,k,2)) * idx;
+          if (flag_fab(i,j,k).isConnected(-1,0,0)) {
+            if (flag_fab(i-1,j,k).isConnected(-1,0,0)) {
+              vx = - (c0 * dat_arr(i  ,j,k,1)
+                  + c1 * dat_arr(i-1,j,k,1)
+                  + c2 * dat_arr(i-2,j,k,1)) * idx;
+              wx = - (c0 * dat_arr(i  ,j,k,2)
+                  + c1 * dat_arr(i-1,j,k,2)
+                  + c2 * dat_arr(i-2,j,k,2)) * idx;
+            } else {
+              // Only one uncovered cell that way, drop to linear
+              vx = (dat_arr(i  ,j,k,1) - dat_arr(i-1,j,k,1)) * idx;
+              wx = (dat_arr(i  ,j,k,2) - dat_arr(i-1,j,k,2)) * idx;
+            }
+          }
+          // Covered on both sides, leave the derivative at zero
         } else if (!flag_fab(i,j,k).isConnected(-1,0,0)) {
           // Covered cell to the left, go fish right
-          vx = (c0 * dat_arr(i  ,j,k,1)
-            + c1 * dat_arr(i+1,j,k,1)
-            + c2 * dat_arr(i+2,j,k,1)) * idx;
-          wx = (c0 * dat_arr(i  ,j,k,2)
-            + c1 * dat_arr(i+1,j,k,2)
-            + c2 * dat_arr(i+2,j,k,2)) * idx;
+          if (flag_fab(i+1,j,k).isConnected( 1,0,0)) {
+            vx = (c0 * dat_arr(i  ,j,k,1)
+              + c1 * dat_arr(i+1,j,k,1)
+              + c2 * dat_arr(i+2,j,k,1)) * idx;
+            wx = (c0 * dat_arr(i  ,j,k,2)
+              + c1 * dat_arr(i+1,j,k,2)
+              + c2 * dat_arr(i+2,j,k,2)) * idx;
+          } else {
+            // Only one uncovered cell that way, drop to linear
+            vx = (dat_arr(i+1,j,k,1) - dat_arr(i  ,j,k,1)) * idx;
+            wx = (dat_arr(i+1,j,k,2) - dat_arr(i  ,j,k,2)) * idx;
+          }
         } else {
           // No covered cells right or left, use standard stencil
           vx = 0.5 * (dat_arr(i+1,j,k,1) - dat_arr(i-1,j,k,1)) * idx;
@@ -188,38 +229,68 @@
         }
         // Do the same in y-direction
         if (!flag_fab(i,j,k).isConnected(0, 1,0)) {
-          uy = - (c0 * dat_arr(i,j  ,k,0)
-              + c1 * dat_arr(i,j-1,k,0)
-              + c2 * dat_arr(i,j-2,k,0)) * idy;
-          wy = - (c0 * dat_arr(i,j  ,k,2)
-              + c1 * dat_arr(i,j-1,k,2)
-              + c2 * dat_arr(i,j-2,k,2)) * idy;
+          if (flag_fab(i,j,k).isConnected(0,-1,0)) {
+            if (flag_fab(i,j-1,k).isConnected(0,-1,0)) {
+              uy = - (c0 * dat_arr(i,j  ,k,0)
+                  + c1 * dat_arr(i,j-1,k,0)
+                  + c2 * dat_arr(i,j-2,k,0)) * idy;
+              wy = - (c0 * dat_arr(i,j  ,k,2)
+                  + c1 * dat_arr(i,j-1,k,2)
+                  + c2 * dat_arr(i,j-2,k,2)) * idy;
+            } else {
+              // Only one uncovered cell that way, drop to linear
+              uy = (dat_arr(i,j  ,k,0) - dat_arr(i,j-1,k,0)) * idy;
+              wy = (dat_arr(i,j  ,k,2) - dat_arr(i,j-1,k,2)) * idy;
+            }
+          }
+          // Covered on both sides, leave the derivative at zero
         } else if (!flag_fab(i,j,k).isConnected(0,-1,0)) {
-          uy = (c0 * dat_arr(i,j  ,k,0)
-            + c1 * dat_arr(i,j+1,k,0)
-            + c2 * dat_arr(i,j+2,k,0)) * idy;
-          wy = (c0 * dat_arr(i,j  ,k,2)
-            + c1 * dat_arr(i,j+1,k,2)
-            + c2 * dat_arr(i,j+2,k,2)) * idy;
+          if (flag_fab(i,j+1,k).isConnected(0, 1,0)) {
+            uy = (c0 * dat_arr(i,j  ,k,0)
+              + c1 * dat_arr(i,j+1,k,0)
+              + c2 * dat_arr(i,j+2,k,0)) * idy;
+            wy = (c0 * dat_arr(i,j  ,k,2)
+              + c1 * dat_arr(i,j+1,k,2)
+              + c2 * dat_arr(i,j+2,k,2)) * idy;
+          } else {
+            // Only one uncovered cell that way, drop to linear
+            uy = (dat_arr(i,j+1,k,0) - dat_arr(i,j  ,k,0)) * idy;
+            wy = (dat_arr(i,j+1,k,2) - dat_arr(i,j  ,k,2)) * idy;
+          }
         } else {
           uy = 0.5 * (dat_arr(i,j+1,k,0) - dat_arr(i,j-1,k,0)) * idy;
           wy = 0.5 * (dat_arr(i,j+1,k,2) - dat_arr(i,j-1,k,2)) * idy;
         }
         // Do the same in z-direction
         if (!flag_fab(i,j,k).isConnected(0,0, 1)) {
-          uz = - (c0 * dat_arr(i,j,k  ,0)
-              + c1 * dat_arr(i,j,k-1,0)
-              + c2 * dat_arr(i,j,k-2,0)) * idz;
-          vz = - (c0 * dat_arr(i,j,k  ,1)
-              + c1 * dat_arr(i,j,k-1,1)
-              + c2 * dat_arr(i,j,k-2,1)) * idz;
+          if (flag_fab(i,j,k).isConnected(0,0,-1)) {
+            if (flag_fab(i,j,k-1).isConnected(0,0,-1)) {
+              uz = - (c0 * dat_arr(i,j,k  ,0)
+                  + c1 * dat_arr(i,j,k-1,0)
+                  + c2 * dat_arr(i,j,k-2,0)) * idz;
+              vz = - (c0 * dat_arr(i,j,k  ,1)
+                  + c1 * dat_arr(i,j,k-1,1)
+                  + c2 * dat_arr(i,j,k-2,1)) * idz;
+            } else {
+              // Only one uncovered cell that way, drop to linear
+              uz = (dat_arr(i,j,k  ,0) - dat_arr(i,j,k-1,0)) * idz;
+              vz = (dat_arr(i,j,k  ,1) - dat_arr(i,j,k-1,1)) * idz;
+            }
+          }
+          // Covered on both sides, leave the derivative at zero
         } else if (!flag_fab(i,j,k).isConnected(0,0,-1)) {
-          uz = (c0 * dat_arr(i,j,k  ,0)
-            + c1 * dat_arr(i,j,k+1,0)
-            + c2 * dat_arr(i,j,k+2,0)) * idz;
-          vz = (c0 * dat_arr(i,j,k  ,1)
-            + c1 * dat_arr(i,j,k+1,1)
-            + c2 * dat_arr(i,j,k+2,1)) * idz;
+          if (flag_fab(i,j,k+1).isConnected(0,0, 1)) {
+            uz = (c0 * dat_arr(i,j,k  ,0)
+              + c1 * dat_arr(i,j,k+1,0)
+              + c2 * dat_arr(i,j,k+2,0)) * idz;
+            vz = (c0 * dat_arr(i,j,k  ,1)
+              + c1 * dat_arr(i,j,k+1,1)
+              + c2 * dat_arr(i,j,k+2,1)) * idz;
+          } else {
+            // Only one uncovered cell that way, drop to linear
+            uz = (dat_arr(i,j,k+1,0) - dat_arr(i,j,k  ,0)) * idz;
+            vz = (dat_arr(i,j,k+1,1) - dat_arr(i,j,k  ,1)) * idz;
+          }
         } else {
           uz = 0.5 * (dat_arr(i,j,k+1,0) - dat_arr(i,j,k-1,0)) * idz;
           vz = 0.5 * (dat_arr(i,j,k+1,1) - dat_arr(i,j,k-1,1)) * idz;

Every distance-2 read is now certified via amrex EBCellFlag::isConnected on the intermediate cell (flags encode only the 3^D neighborhood), falling back to the two-point difference, or leaving the pre-initialized 0 when both sides are disconnected. Flag reads at i+-1 stay in range because mag_vort registers grow_box_by_two (NS_setup.cpp:449).

Diff(s) are against 9bf664bf, written from the current source and verified only with git apply --check — never compiled, never run, never applied to the tree. Treat them as precise intent, not tested patches.

Verification evidence

F074 — confirmed (one verifier lens)

Lens 1 (refutation attempt): 'if (!flag_fab(i,j,k).isConnected( 1,0,0)) { vx = -(c0dat_arr(i,j,k,1) + c1dat_arr(i-1,j,k,1) + c2*dat_arr(i-2,j,k,1))*idx;' — no check that i-1/i-2 are uncovered; EBCellFlag encodes only the 3^D neighborhood. IAMR sets covered state to COVERED_VAL=1.0e40 (NavierStokes.cpp:439), and mag_vort feeds ns.vorticity_greater tagging (NS_error.cpp:69). A 1-2 cell gap makes the stencil read covered garbage.


Based on commit 9bf664bf, which is also the tree the audit verified against. From an automated audit of Source/, Tutorials/ and Util/. Audit finding id: F074. Reviewer unit(s): theme:eb-smallcell. Nothing here was compiled or run — the failure scenarios are code reasoning, so the reaching configuration above is the cheapest way to confirm or refute it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions