Skip to content

Commit

Permalink
Merge #1339
Browse files Browse the repository at this point in the history
1339: define fill on Fields, remove TU.FieldFromNamedTuple r=simonbyrne a=simonbyrne

It probably makes sense to define this.

- [x] Code follows the [style guidelines](https://clima.github.io/ClimateMachine.jl/latest/DevDocs/CodeStyle/) OR N/A.
- [x] Unit tests are included OR N/A.
- [x] Code is exercised in an integration test OR N/A.
- [x] Documentation has been added/updated OR N/A.


Co-authored-by: Simon Byrne <[email protected]>
  • Loading branch information
bors[bot] and simonbyrne authored Jun 20, 2023
2 parents eb6f155 + 4abbd8c commit 3bfe783
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 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 @@ -435,5 +435,3 @@ function Base.Broadcast.copyto!(field::Field, nt::NamedTuple)
),
)
end

Base.fill!(field::Fields.Field, val) = field .= val
24 changes: 12 additions & 12 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,7 +321,7 @@ 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
Expand All @@ -337,7 +337,7 @@ end
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.x).z, TU.fc_index(1, space))
ᶜx_surf = Spaces.level(Y.x, TU.fc_index(1, space))
Expand Down Expand Up @@ -499,7 +499,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 @@ -640,7 +640,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 @@ -683,7 +683,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
10 changes: 5 additions & 5 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 Down Expand Up @@ -60,7 +60,7 @@ end
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 3bfe783

Please sign in to comment.