Skip to content

Commit

Permalink
add update_field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Feb 29, 2024
1 parent 9111287 commit c6645e4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Interfacer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,18 @@ simulation. This should be extended by component models. If it isn't extended,
the field won't be updated and a warning will be raised.
"""
update_field!(sim::ComponentModelSimulation, val::Val{:air_density}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:surface_albedo}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:area_fraction}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:co2}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:liquid_precipitation}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:radiative_energy_flux}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:snow_precipitation}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:surface_albedo}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:surface_temperature}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:turbulent_energy_flux}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:turbulent_fluxes}, _) = update_field_warning(sim, val)
update_field!(sim::ComponentModelSimulation, val::Val{:turbulent_moisture_flux}, _) = update_field_warning(sim, val)
update_field_warning(sim, val) =
@warn("`update_field!` is not extended for the $val field of " * name(sim) * ": skipping update.", maxlog = 1)
update_field_warning(sim, val::Val{X}) where {X} =
@warn("`update_field!` is not extended for the `$X` field of " * name(sim) * ": skipping update.", maxlog = 1)

"""
update_field!(sim::SurfaceStub, ::Val{:area_fraction}, field::Fields.Field)
Expand Down
57 changes: 57 additions & 0 deletions test/interfacer_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ end
struct DummySimulation3{S} <: LandModelSimulation
space::S
end
name(::DummySimulation3) = "DummySimulation3"

get_field(sim::SurfaceModelSimulation, ::Val{:var}) = ones(sim.space)
get_field(sim::SurfaceModelSimulation, ::Val{:surface_temperature}) = ones(sim.space) .* 300
Expand Down Expand Up @@ -116,3 +117,59 @@ for FT in (Float32, Float64)
@test parent(get_field(stub, Val(:surface_temperature)))[1] == FT(2)
end
end

@testset "update_field! warnings" begin
FT = Float64
space = TestHelper.create_space(FT)
dummy_field = Fields.ones(space)
sim = DummySimulation3(space)

# Test that update_field! gives correct warnings for unextended fields
@test_logs (
:warn,
"`update_field!` is not extended for the `air_density` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:air_density), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `area_fraction` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:area_fraction), dummy_field)
@test_logs (:warn, "`update_field!` is not extended for the `co2` field of " * name(sim) * ": skipping update.") update_field!(
sim,
Val(:co2),
dummy_field,
)
@test_logs (
:warn,
"`update_field!` is not extended for the `liquid_precipitation` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:liquid_precipitation), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `radiative_energy_flux` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:radiative_energy_flux), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `snow_precipitation` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:snow_precipitation), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `surface_albedo` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:surface_albedo), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `surface_temperature` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:surface_temperature), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `turbulent_energy_flux` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:turbulent_energy_flux), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `turbulent_fluxes` field of " * name(sim) * ": skipping update.",
) update_field!(sim, Val(:turbulent_fluxes), dummy_field)
@test_logs (
:warn,
"`update_field!` is not extended for the `turbulent_moisture_flux` field of " *
name(sim) *
": skipping update.",
) update_field!(sim, Val(:turbulent_moisture_flux), dummy_field)
end

0 comments on commit c6645e4

Please sign in to comment.