Skip to content

Commit

Permalink
Update MultiFab Test: Component Access
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed Mar 25, 2022
1 parent 62b8631 commit 4ac3dde
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions tests/test_multifab.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
@pytest.mark.parametrize("nghost", [0, 1])
def test_mfab_loop(mfab, nghost):
ngv = mfab.nGrowVect
print(ngv)
print(f"\n mfab={mfab}, mfab.nGrowVect={ngv}")

for mfi in mfab:
print(mfi)
bx = mfi.tilebox().grow(ngv)
marr = mfab.array(mfi)

Expand All @@ -21,7 +20,11 @@ def test_mfab_loop(mfab, nghost):
#print(marr.size)
#print(marr.nComp)

# slow, index by index assignment
# index by index assignment
# notes:
# - this is AMReX Array4, F-order indices
# - even though we iterate by fastest varying index,
# such loops are naturally very slow in Python
three_comps = mfab.num_comp == 3
if three_comps:
for i, j, k in bx:
Expand All @@ -34,19 +37,30 @@ def test_mfab_loop(mfab, nghost):
#print(i,j,k)
marr[i, j, k] = 10.0 * i

# fast, range based assignment
# challenge: offset from index space
#bx_zeroshift = bx - bx.small_end - mfab.nGrowVect
# note: offset from index space in numpy
# in numpy, we start indices from zero, not small_end

# numpy assignment: including guard/ghost region
# numpy representation: non-copying view, including the
# guard/ghost region
# note: in numpy, indices are in C-order!
marr_np = np.array(marr, copy=False)
print(marr_np.shape)

# check the values at start/end are the same: first component
assert(marr_np[0, 0, 0, 0] == marr[bx.small_end])
# assert(marr_np[-1, -1, -1, -1] == marr[bx.big_end]) # FIXME

#marr_np[24:200, :, :, :] = 42. # this should fail
#marr_np[:, :, :] = 42.
assert(marr_np[0, -1, -1, -1] == marr[bx.big_end])
# same check, but for all components
for n in range(mfab.num_comp):
small_end_comp = list(bx.small_end) + [n]
big_end_comp = list(bx.big_end) + [n]
assert(marr_np[n, 0, 0, 0] == marr[small_end_comp])
assert(marr_np[n, -1, -1, -1] == marr[big_end_comp])

# now we do some faster assignments, using range based access
# this should fail as out-of-bounds, but does not
# does Numpy not check array access for non-owned views?
#marr_np[24:200, :, :, :] = 42.

# all components and all indices set at once to 42
marr_np[:, :, :, :] = 42.
assert(marr_np[0, 0, 0, 0] == marr[bx.small_end])
assert(marr_np[-1, -1, -1, -1] == marr[bx.big_end])
Expand Down

0 comments on commit 4ac3dde

Please sign in to comment.