Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GPU indexing flat fields #4090

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
10 changes: 8 additions & 2 deletions docs/src/fields.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Fields basics

`Field`s and its relatives are core Oceananigans data structures.
`Field`s are more or less arrays of `data` located on a `grid`, whose entries
`Field`s are more or less arrays of _three dimensional_ `data` located on a `grid`, whose entries
correspond to the average value of some quantity over some finite-sized volume.
`Field`s also may contain `boundary_conditions`, may be computed from an `operand`
or expression involving other fields, and may cover only a portion of the total
Expand Down Expand Up @@ -326,7 +326,8 @@ heatmap(view(c, :, :, 1))
For `Field`s on three-dimensional grids, `set!` functions must have arguments `x, y, z` for
`RectilinearGrid`, or `λ, φ, z` for `LatitudeLongitudeGrid` and `OrthogonalSphericalShellGrid`.
But for `Field`s on one- and two-dimensional grids, only the arguments that correspond to the
non-`Flat` directions must be included. For example, to `set!` on a one-dimensional grid we write
non-`Flat` directions must be included.
For example, to `set!` on a one-dimensional grid we write

```jldoctest fields
# Make a field on a one-dimensional grid
Expand All @@ -346,6 +347,11 @@ set!(one_d_c, still_pretty_fun)
└── max=19.5, min=1.5, mean=10.5
```

!!! note
Despite we `set` a `Field` with reduced arguments in the `Flat` directions, a `Field` remains
intrinsically a three-dimensional object as it maps to a three-dimensional data structure.
For this reason a `Field` must be always indexed by providing all three indices `i, j, k`.

### A bit more about setting with functions

Let's return to the three-dimensional `fun_stuff` case to investigate in more detail how `set!` works with functions.
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/abstract_field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ interior(f::AbstractField) = f
##### Coordinates of fields
#####

@propagate_inbounds node(i, j, k, ψ::AbstractField) = node(i, j, k, ψ.grid, instantiated_location(ψ)...)
@propagate_inbounds node(i, j, k, ψ::AbstractField) = node(i, j, k, ψ.grid, instantiated_location(ψ)...)
@propagate_inbounds xnode(i, j, k, ψ::AbstractField) = xnode(i, j, k, ψ.grid, instantiated_location(ψ)...)
@propagate_inbounds ynode(i, j, k, ψ::AbstractField) = ynode(i, j, k, ψ.grid, instantiated_location(ψ)...)
@propagate_inbounds znode(i, j, k, ψ::AbstractField) = znode(i, j, k, ψ.grid, instantiated_location(ψ)...)
Expand Down
10 changes: 5 additions & 5 deletions test/test_field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Statistics
using Oceananigans.Fields: ReducedField, has_velocities
using Oceananigans.Fields: VelocityFields, TracerFields, interpolate, interpolate!
using Oceananigans.Fields: reduced_location
using Oceananigans.Fields: FractionalIndices, interpolator
using Oceananigans.Fields: fractional_indices, interpolator
using Oceananigans.Fields: convert_to_0_360, convert_to_λ₀_λ₀_plus360
using Oceananigans.Grids: ξnode, ηnode, rnode
using Oceananigans.Grids: total_length
Expand Down Expand Up @@ -492,11 +492,11 @@ end

for X in Xs
(x, y, z) = X
fi = @allowscalar FractionalIndices(X, grid, loc, loc, loc)
fi, fj, fk = @allowscalar fractional_indices(X, grid, loc, loc, loc)

i⁻, i⁺, _ = interpolator(fi.i)
j⁻, j⁺, _ = interpolator(fi.j)
k⁻, k⁺, _ = interpolator(fi.k)
i⁻, i⁺, _ = interpolator(fi)
j⁻, j⁺, _ = interpolator(fj)
k⁻, k⁺, _ = interpolator(fk)

x⁻ = @allowscalar ξnode(i⁻, j⁻, k⁻, grid, loc, loc, loc)
y⁻ = @allowscalar ηnode(i⁻, j⁻, k⁻, grid, loc, loc, loc)
Expand Down