You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add section in the docs for reduced operations (#4235)
* add section for reduced operations
* Apply suggestions from code review
Co-authored-by: Gregory L. Wagner <[email protected]>
* apply review suggestions
* Apply suggestions from code review
Co-authored-by: Gregory L. Wagner <[email protected]>
* Update docs/src/operations.md
Co-authored-by: Gregory L. Wagner <[email protected]>
* Update docs/src/operations.md
Co-authored-by: Gregory L. Wagner <[email protected]>
* Apply suggestions from code review
* few tweaks
* no need for CPU() arg in grid
* some space
* few tweaks
* add a sentence about reductions
* fix latex formatting in docstrings
we might as well, since we are at it..
* fix Reduction ref
* Some typo fixes and added note about face fields
* Apply suggestions from code review
* showcase that condition can get an array of bools
* slight rephrase
* move the comment up
* remove duplicate broadcasting
---------
Co-authored-by: Gregory L. Wagner <[email protected]>
Co-authored-by: Taimoor Sohail <[email protected]>
Copy file name to clipboardExpand all lines: docs/src/operations.md
+115-1Lines changed: 115 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ BinaryOperation at (Center, Center, Center)
67
67
└── 1
68
68
```
69
69
70
-
Like `Field`s, `AbstractOperations` have a location and a grid.
70
+
Like `Field`s, `AbstractOperations` have a location and a grid.
71
71
In addition to `BinaryOperation`s like the kind above, `UnaryOperation`s and `MultiaryOperation`s are also supported,
72
72
73
73
```jldoctest operations
@@ -211,3 +211,117 @@ BinaryOperation at (Center, Center, Center)
211
211
│ └── 4×1×4 Field{Center, Center, Center} on RectilinearGrid on CPU
212
212
└── 2
213
213
```
214
+
215
+
## Averages and integrals
216
+
217
+
An operation that reduces one or more dimensions of a field is called a [`Reduction`](@ref); some reductions include [`Average`](@ref), [`Integral`](@ref), and [`CumulativeIntegral`](@ref).
218
+
219
+
Let's demonstrate now how we can compute the average or the integral of a field over the grid or over some part of the grid.
220
+
We start by creating a latitude-longitude grid that only goes up to 30 degrees latitude.
221
+
Conveniently, with this latitude extent that grid covers half the total area of the sphere, i.e., `2π * grid.radius^2`.
222
+
223
+
Let's try to estimate this area using `Integral` operation.
224
+
We create a Field, we fill it with ones and we integrate it over the whole grid.
225
+
We use a `CenterField` for the example below, but all location combinations work similarly.
226
+
227
+
228
+
```jldoctest operations_avg_int
229
+
using Oceananigans
230
+
231
+
grid = LatitudeLongitudeGrid(size=(60, 10, 5),
232
+
latitude = (-30, 30),
233
+
longitude = (0, 360),
234
+
z = (-1000, 0))
235
+
236
+
c = CenterField(grid)
237
+
set!(c, 1)
238
+
239
+
∫c = Field(Integral(c, dims=(1, 2)))
240
+
241
+
# output
242
+
1×1×5 Field{Nothing, Nothing, Center} reduced over dims = (1, 2) on LatitudeLongitudeGrid on CPU
├── grid: 60×10×5 LatitudeLongitudeGrid{Float64, Periodic, Bounded, Bounded} on CPU with 3×3×3 halo and with precomputed metrics
245
+
├── operand: Integral of BinaryOperation at (Center, Center, Center) over dims (1, 2)
246
+
├── status: time=0.0
247
+
└── data: 1×1×11 OffsetArray(::Array{Float64, 3}, 1:1, 1:1, -2:8) with eltype Float64 with indices 1:1×1:1×-2:8
248
+
└── max=0.0, min=0.0, mean=0.0
249
+
```
250
+
251
+
A few remarks: note that the `∫c` has locations `Nothing, Nothing, Center`; this is because we have integrated in the first two dimensions and thus it's `reduced over dims = (1, 2)`.
252
+
Further note that `∫c` is full of zeros; its max, min, and mean values are all 0.
253
+
No computation has been done yet.
254
+
To compute `∫c`, we call [`compute!`](@ref),
255
+
256
+
```jldoctest operations_avg_int
257
+
compute!(∫c)
258
+
259
+
# output
260
+
1×1×5 Field{Nothing, Nothing, Center} reduced over dims = (1, 2) on LatitudeLongitudeGrid on CPU
├── grid: 60×10×5 LatitudeLongitudeGrid{Float64, Periodic, Bounded, Bounded} on CPU with 3×3×3 halo and with precomputed metrics
296
+
├── operand: Integral of ConditionalOperation of BinaryOperation at (Center, Center, Center) with condition cond (generic function with 1 method) over dims (1, 2)
297
+
├── status: time=0.0
298
+
└── data: 1×1×11 OffsetArray(::Array{Float64, 3}, 1:1, 1:1, -2:8) with eltype Float64 with indices 1:1×1:1×-2:8
299
+
└── max=0.0, min=0.0, mean=0.0
300
+
```
301
+
302
+
Above we have attached a condition to the operand.
303
+
Now the operand is applied only when the condition is true.
304
+
Let's compute and see if we get 1/4 of the area of the sphere
305
+
306
+
```jldoctest operations_avg_int
307
+
compute!(conditional_∫c)
308
+
conditional_∫c[1, 1, 1] ≈ π * grid.radius^2 # area of spherical zone with 0ᵒ ≤ φ ≤ 30ᵒ
309
+
310
+
# output
311
+
true
312
+
```
313
+
314
+
Another way to do the above is to provide the `condition` keyword argument with an array of booleans.
315
+
316
+
```jldoctest operations_avg_int
317
+
cond_array = trues(size(grid))
318
+
cond_array[:, 1:5, :] .= false # set the first half of the latitude range to false
0 commit comments