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

Adds compute! method for collections #3491

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Fields/field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ Computes `field.data` from `field.operand`.
"""
compute!(field, time=nothing) = field # fallback

compute!(collection::Union{Tuple, NamedTuple}) = map(compute!, collection)

"""
@compute(exprs...)

Expand Down
24 changes: 24 additions & 0 deletions test/test_computed_field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ function computations_with_computed_fields(model)
return all(interior(tke, 2:3, 2:3, 2:3) .== 9/2)
end

function compute_tuples_and_namedtuples(model)
c = CenterField(model.grid)
set!(c, 1)

one_c = Field(1 * c)
two_c = tuple(Field(2 * c))
six_c = (; field = Field(6 * c))
ten_c = (; field = Field(10 * c))

compute!(one_c)
compute!(two_c)
compute!(six_c)

at_ijk(i, j, k, grid, nt::NamedTuple) = nt.field[i,j,k]
ten_c_op = KernelFunctionOperation{Center, Center, Center}(at_ijk, model.grid, ten_c)
ten_c_field = Field(ten_c_op)
compute!(ten_c_field)

return all(interior(one_c) .== 1) & all(interior(two_c[1]) .== 2) & all(interior(six_c.field) .== 6) & all(interior(ten_c.field) .== 10)
end

for arch in archs
A = typeof(arch)
@testset "Computed Fields [$A]" begin
Expand Down Expand Up @@ -588,6 +609,9 @@ for arch in archs
@testset "Computations with Fields [$A, $G]" begin
@info " Testing computations with Field [$A, $G]..."
@test computations_with_computed_fields(model)

@info " Testing computations of Tuples and NamedTuples"
@test compute_tuples_and_namedtuples(model)
end

@testset "Conditional computation of Field and BuoyancyField [$A, $G]" begin
Expand Down