Skip to content

Commit

Permalink
Prevent NaN arrays in gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
cphyc committed Jan 23, 2020
1 parent 25b9cdc commit 5d2c4e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions yt/fields/fluid_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,19 @@ def grad_func(axi, ax):
slice_3dl = slice_3d[:axi] + (sl_left,) + slice_3d[axi+1:]
slice_3dr = slice_3d[:axi] + (sl_right,) + slice_3d[axi+1:]
def func(field, data):
ds = div_fac * data[ftype, "d%s" % ax]
f = data[grad_field][slice_3dr]/ds[slice_3d]
f -= data[grad_field][slice_3dl]/ds[slice_3d]
ds = data[ftype, "d%s" % ax]
vl = data[grad_field][slice_3dl]
vc = data[grad_field][slice_3d]
vr = data[grad_field][slice_3dr]

okl = np.isfinite(vl)
okr = np.isfinite(vr)

f = np.where(okl & okr, (vr - vl) / 2,
np.where(okl, vc - vl, vr - vc)) / ds[slice_3d]

new_field = np.zeros_like(data[grad_field], dtype=np.float64)
new_field = data.ds.arr(new_field, f.units)
new_field = data.ds.arr(new_field, vr.units / ds.units)
new_field[slice_3d] = f
return new_field
return func
Expand Down
1 change: 1 addition & 0 deletions yt/fields/vector_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def create_vector_fields(registry, basename, field_units,
if slice_info is None:
sl_left = slice(None, -2, None)
sl_right = slice(2, None, None)
sl_centre = slice(1, -1, None)
div_fac = 2.0
else:
sl_left, sl_right, div_fac = slice_info
Expand Down

0 comments on commit 5d2c4e7

Please sign in to comment.