Repository: yt-project/yt

Issue:
BUG: yt with WarpX: field data can become 0 if particle data has already been read
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

In some cases, the field output of a WarpX simulation read from yt is exactly 0 if the particle data has been read before. I've only observed this issue in 2D simulations for which the lower boundary of the simulation domain in the second direction is <= -1 and the higher boundary is >= 1. This issue has already been posted in the WarpX github repo: https://github.com/ECP-WarpX/WarpX/issues/3136 

**Code for reproduction**

For completeness, I attach here a full reproducer, which includes a bash script containing all the commands needed to reproduce the issue (including the compilation of WarpX). [yt_bug_reproducer.zip](https://github.com/yt-project/yt/files/9021455/yt_bug_reproducer.zip)

I show below a typical failing python script

```python
import sys
import numpy as np
import yt

fn = sys.argv[1]
ds = yt.load(fn)

## If I comment the two lines below then the issue disappears
ad_particles = ds.all_data()
px = ad_particles['electrons', 'particle_momentum_x'].to_ndarray()

ad_field = ds.covering_grid(level=0, left_edge=ds.domain_left_edge, dims=ds.domain_dimensions)
Ex = ad_field[('boxlib','Ex')].squeeze().v
print("Max of Ex field after reading particle data")
print(np.amax(np.abs(Ex)))
```

**Actual outcome**

When the issue is present, the printed output is exactly 0.

**Expected outcome**

The printed output should be strictly greater than 0 (and is strictly greater than 0 if the 2 middle lines in the code snippet are commented out).

**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: Linux (Debian)
  * Python Version: 3.8.8
  * yt version: 3.6.1 (but I've also observed the issue with version 4.0.1)
  * Other Libraries (if applicable): I'm using the current development version of WarpX (commit 542ef3c09b467974b5bd9d1e3b25a208e3296562)

yt and python have been installed from conda with the conda-forge channel.

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->



Discussion:
Hi, and welcome to yt!  Thanks for opening your first issue.  We have an issue template that helps us to gather relevant information to help diagnosing and fixing the issue.

Hi @NeilZaim -- I'm looking at this now, but I also want to say, this is the most pleasant bug reproducer I've ever seen, and *thank you* so much for taking the time to make it.  It really, really helps, and I know it's probably kind of annoying to do -- so thank you!
Hi @NeilZaim , I'm currently running on `4339e160f46eb6a4695b7a8b0a45bf9fe6298f30`, which is not part of a release yet, but whose most recent release was 4.0.4, along with a few changes that have occurred on `main` since then.  I get these results, which don't show the behavior:

```
END REGION WarpX::Evolve()
AMReX (22.06-44-g027f2ff77fed) finalized
yt : [INFO     ] 2022-06-30 14:10:14,563 Parameters: current_time              = 2.9475805649829545e-09
yt : [INFO     ] 2022-06-30 14:10:14,563 Parameters: domain_dimensions         = [128 128   1]
yt : [INFO     ] 2022-06-30 14:10:14,564 Parameters: domain_left_edge          = [-1. -1.  0.]
yt : [INFO     ] 2022-06-30 14:10:14,564 Parameters: domain_right_edge         = [1.    0.999 1.   ]
Max of Ex field before reading particle data
2597.771546328757
yt : [INFO     ] 2022-06-30 14:10:20,028 Parameters: current_time              = 2.9475805649829545e-09
yt : [INFO     ] 2022-06-30 14:10:20,028 Parameters: domain_dimensions         = [128 128   1]
yt : [INFO     ] 2022-06-30 14:10:20,029 Parameters: domain_left_edge          = [-1. -1.  0.]
yt : [INFO     ] 2022-06-30 14:10:20,029 Parameters: domain_right_edge         = [1.    0.999 1.   ]
Max of Ex field after reading particle data
2597.771546328757
```

Before I dig in, can you verify that this is indeed *not* showing the bug?
Hi @matthewturk, and thanks for looking into the issue so quickly!

I'm sorry, the reproducer was not really clear. Actually it starts 2 simulations. A first one with a simulation domain of [-1. 1.] in the second direction, which shows the bug, and a second one with a simulation domain of [-1. 0.999] in the second direction, which no longer shows the bug. The one you see at the very end of the output is the second one, so the result you got is expected.

If you look slightly higher in the output though, you should see something like that:
```
yt : [INFO     ] 2022-07-01 11:14:04,323 Parameters: current_time              = 2.948317920937106e-09
yt : [INFO     ] 2022-07-01 11:14:04,324 Parameters: domain_dimensions         = [128 128   1]
yt : [INFO     ] 2022-07-01 11:14:04,324 Parameters: domain_left_edge          = [-1. -1.  0.]
yt : [INFO     ] 2022-07-01 11:14:04,324 Parameters: domain_right_edge         = [1. 1. 1.]
Max of Ex field before reading particle data
2597.4956601784093
yt : [INFO     ] 2022-07-01 11:14:05,960 Parameters: current_time              = 2.948317920937106e-09
yt : [INFO     ] 2022-07-01 11:14:05,961 Parameters: domain_dimensions         = [128 128   1]
yt : [INFO     ] 2022-07-01 11:14:05,961 Parameters: domain_left_edge          = [-1. -1.  0.]
yt : [INFO     ] 2022-07-01 11:14:05,961 Parameters: domain_right_edge         = [1. 1. 1.]
Max of Ex field after reading particle data
0.0
```

Resolving patch:
diff --git a/yt/frontends/boxlib/data_structures.py b/yt/frontends/boxlib/data_structures.py
--- a/yt/frontends/boxlib/data_structures.py
+++ b/yt/frontends/boxlib/data_structures.py
@@ -372,7 +372,7 @@ def _parse_index(self):
             if self.dimensionality < 2:
                 dx[i].append(DRE[1] - DLE[1])
             if self.dimensionality < 3:
-                dx[i].append(DRE[2] - DLE[1])
+                dx[i].append(DRE[2] - DLE[2])
         self.level_dds = np.array(dx, dtype="float64")
         next(header_file)
         if self.ds.geometry == "cartesian":


Test patch:
diff --git a/tests/tests.yaml b/tests/tests.yaml
--- a/tests/tests.yaml
+++ b/tests/tests.yaml
@@ -142,7 +142,7 @@ answer_tests:
     - yt/frontends/boxlib/tests/test_outputs.py:test_units_override
     - yt/frontends/boxlib/tests/test_outputs.py:test_raw_fields
 
-  local_boxlib_particles_009:
+  local_boxlib_particles_010:
     - yt/frontends/boxlib/tests/test_outputs.py:test_LyA
     - yt/frontends/boxlib/tests/test_outputs.py:test_nyx_particle_io
     - yt/frontends/boxlib/tests/test_outputs.py:test_castro_particle_io