Skip to content

Commit

Permalink
Try #1339:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] committed Jun 22, 2023
2 parents 320c4ac + dab5a6e commit 5ce4ccd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
18 changes: 18 additions & 0 deletions src/Fields/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ function Base.copyto!(dest::Field{V, M}, src::Field{V, M}) where {V, M}
return dest
end

"""
fill!(field::Field, value)
Fill `field` with `value`.
"""
function Base.fill!(field::Field, value)
fill!(field_values(field), value)
return field
end
"""
fill(value, space::AbstractSpace)
Create a new `Field` on `space` and fill it with `value`.
"""
function Base.fill(value::FT, space::AbstractSpace) where {FT}
field = Field(FT, space)
return fill!(field, value)
end

"""
zeros(space::AbstractSpace)
Expand Down
2 changes: 0 additions & 2 deletions src/Fields/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,3 @@ function Base.Broadcast.copyto!(field::Field, nt::NamedTuple)
),
)
end

Base.fill!(field::Fields.Field, val) = field .= val
28 changes: 14 additions & 14 deletions test/Fields/field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ end
@testset "Constructing & broadcasting over empty fields" begin
FT = Float32
for space in TU.all_spaces(FT)
f = TU.FieldFromNamedTuple(space, (;))
f = fill((;), space)
@. f += f
end

Expand All @@ -112,7 +112,7 @@ end
@test_broken true
end
end
empty_field(space) = TU.FieldFromNamedTuple(space, (;))
empty_field(space) = fill((;), space)

# Broadcasting over the wrong size should error
test_broken_throws(empty_field(TU.PointSpace(FT)))
Expand Down Expand Up @@ -294,7 +294,7 @@ end
FT = Float64
nt =
(; x = FT(0), y = FT(0), tup = ntuple(i -> (; a = FT(1), b = FT(1)), 2))
Y = TU.FieldFromNamedTuple(space, nt)
Y = fill(nt, space)

prop_chains = Fields.property_chains(Y)
@test prop_chains[1] == (:x,)
Expand All @@ -321,23 +321,23 @@ end
ClimaCore.Fields.truncate_printing_field_types() = true
@testset "Truncated printing" begin
nt = (; x = Float64(0), y = Float64(0))
Y = TU.FieldFromNamedTuple(spectral_space_2D(), nt)
Y = fill(nt, spectral_space_2D())
@test sprint(show, typeof(Y); context = IOContext(stdout)) ==
"Field{(:x, :y)} (trunc disp)"
end
ClimaCore.Fields.truncate_printing_field_types() = false

@testset "Standard printing" begin
nt = (; x = Float64(0), y = Float64(0))
Y = TU.FieldFromNamedTuple(spectral_space_2D(), nt)
Y = fill(nt, spectral_space_2D())
s = sprint(show, typeof(Y)) # just make sure this doesn't break
end

@testset "Set!" begin
space = spectral_space_2D()
FT = Float64
nt = (; x = FT(0), y = FT(0))
Y = TU.FieldFromNamedTuple(space, nt)
Y = fill(nt, space)
foo(local_geom) =
sin(local_geom.coordinates.x * local_geom.coordinates.y) + 3
Fields.set!(foo, Y.x)
Expand Down Expand Up @@ -373,7 +373,7 @@ end
FT = Float64
for space in TU.all_spaces(FT)
TU.levelable(space) || continue
Y = TU.FieldFromNamedTuple(space, (; x = FT(2)))
Y = fill((; x = FT(2)), space)
lg_space = Spaces.level(space, TU.fc_index(1, space))
lg_field_space = axes(Fields.level(Y, TU.fc_index(1, space)))
@test all(
Expand All @@ -388,14 +388,14 @@ end
FT = Float64
for space in TU.all_spaces(FT)
if space isa Spaces.SpectralElementSpace1D
Y = TU.FieldFromNamedTuple(space, (; x = FT(1)))
Y = fill((; x = FT(1)), space)
point_space_from_field = axes(Fields.column(Y.x, 1, 1))
point_space = Spaces.column(space, 1, 1)
@test Fields.ones(point_space) ==
Fields.ones(point_space_from_field)
end
if space isa Spaces.SpectralElementSpace2D
Y = TU.FieldFromNamedTuple(space, (; x = FT(1)))
Y = fill((; x = FT(1)), space)
point_space_from_field = axes(Fields.column(Y.x, 1, 1, 1))
point_space = Spaces.column(space, 1, 1, 1)
@test Fields.ones(point_space) ==
Expand Down Expand Up @@ -424,7 +424,7 @@ end
for space in TU.all_spaces(FT)
# Filter out spaces without z coordinates:
TU.has_z_coordinates(space) || continue
Y = TU.FieldFromNamedTuple(space, (; x = FT(1)))
Y = fill((; x = FT(1)), space)
ᶜz_surf =
Spaces.level(Fields.coordinate_field(Y).z, TU.fc_index(1, space))
ᶜx_surf = copy(Spaces.level(Y.x, TU.fc_index(1, space)))
Expand All @@ -434,7 +434,7 @@ end

# Skip spaces incompatible with Fields.bycolumn:
TU.bycolumnable(space) || continue
Yc = TU.FieldFromNamedTuple(space, (; x = FT(1)))
Yc = fill((; x = FT(1)), space)
column_surface_bc!(Yc.x, ᶜz_surf, ᶜx_surf)
@test Y.x == Yc.x
nothing
Expand Down Expand Up @@ -491,7 +491,7 @@ Base.broadcastable(x::InferenceFoo) = Ref(x)
foo = InferenceFoo(2.0)

for space in TU.all_spaces(FT)
Y = TU.FieldFromNamedTuple(space, (; a = FT(0), b = FT(1)))
Y = fill((; a = FT(0), b = FT(1)), space)
@test_throws ErrorException("type InferenceFoo has no field bingo") FieldFromNamedTupleBroken(
space,
ics_foo,
Expand Down Expand Up @@ -632,7 +632,7 @@ convergence_rate(err, Δh) =
# Skip spaces incompatible with Fields.bycolumn:
TU.bycolumnable(space) || continue

Y = TU.FieldFromNamedTuple(space, (; y = FT(1)))
Y = fill((; y = FT(1)), space)
zcf = Fields.coordinate_field(Y.y).z
Δz = Fields.Δz_field(axes(zcf))
Δz_col = Δz[Fields.ColumnIndex((1, 1), 1)]
Expand Down Expand Up @@ -675,7 +675,7 @@ ClimaCore.enable_threading() = false # launching threads allocates
for space in TU.all_spaces(FT)
# Filter out spaces without z coordinates:
TU.has_z_coordinates(space) || continue
Y = TU.FieldFromNamedTuple(space, (; y = FT(1)))
Y = fill((; y = FT(1)), space)
zcf = Fields.coordinate_field(Y.y).z
∫y = Spaces.level(similar(Y.y), TU.fc_index(1, space))
∫y .= 0
Expand Down
12 changes: 6 additions & 6 deletions test/Fields/field_opt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import .TestUtilities as TU
end
for space in TU.all_spaces(FT)
TU.bycolumnable(space) || continue
Y = TU.FieldFromNamedTuple(space, (; x = FT(2)))
Y = fill((; x = FT(2)), space)

# Plain broadcast
Yx = Y.x
Expand All @@ -56,11 +56,11 @@ end
nothing
end
function callfill!(Y)
fill!(Y, ((; x = 2.0),))
fill!(Y, (; x = 2.0))
nothing
end
for space in TU.all_spaces(FT)
Y = TU.FieldFromNamedTuple(space, (; x = FT(2)))
Y = fill((; x = FT(2)), space)
allocs_test!(Y)
p = @allocated allocs_test!(Y)
@test p == 0
Expand Down Expand Up @@ -127,7 +127,7 @@ end
@testset "Allocations StencilCoefs broadcasting" begin
FT = Float64
for space in TU.all_spaces(FT)
Y = TU.FieldFromNamedTuple(space, (; x = sc(FT)))
Y = fill((; x = sc(FT)), space)
allocs_test1!(Y)
p = @allocated allocs_test1!(Y)
@test p == 0
Expand Down Expand Up @@ -206,8 +206,8 @@ end
FT = Float64
for space in TU.all_spaces(FT)
Y = Fields.FieldVector(;
c = TU.FieldFromNamedTuple(space, (; x = FT(0))),
f = TU.FieldFromNamedTuple(space, (; x = FT(0))),
c = fill((; x = FT(0)), space),
f = fill((; x = FT(0)), space),
)

Y .= 0 # compile first
Expand Down
9 changes: 2 additions & 7 deletions test/Operators/spectralelement/benchmark_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,11 @@ function setup_kernel_args(ARGS::Vector{String} = ARGS)
end,
)

function FieldFromNamedTuple(space, nt::NamedTuple)
cmv(z) = nt
return cmv.(Fields.coordinate_field(space))
end

ϕψ = combine.(ϕ, ψ)
nt_ϕψ = combine_nt.(ϕ, ψ)
nt_ϕψ_ft = combine_nt_ft.(ϕ)
f_comp = FieldFromNamedTuple(space, complicated_field(FT))
f_comp2 = FieldFromNamedTuple(space, complicated_field2(FT))
f_comp = fill(complicated_field(FT), space)
f_comp2 = fill(complicated_field2(FT), space)
u = initial_velocity(space)
du = initial_velocity(space)
ϕ_buffer = Spaces.create_dss_buffer(ϕ)
Expand Down
5 changes: 0 additions & 5 deletions test/TestUtilities/TestUtilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,4 @@ fc_index(

has_z_coordinates(space) = :z in propertynames(Spaces.coordinates_data(space))

function FieldFromNamedTuple(space, nt::NamedTuple)
cmv(z) = nt
return cmv.(Fields.coordinate_field(space))
end

end

0 comments on commit 5ce4ccd

Please sign in to comment.