Skip to content

Commit

Permalink
tests pass locally
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Dec 2, 2023
1 parent 1d2d3f8 commit afc5614
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/ConservationChecker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function check_conservation!(

# save atmos
previous = getproperty(ccs, sim_name)
current = sum(Interfacer.get_field(sim, Val(:energy))) # # ∫ J / m^3 dV
current = sum(FT.(Interfacer.get_field(sim, Val(:energy)))) # # ∫ J / m^3 dV

push!(previous, current)
total += current + radiation_sources_accum
Expand All @@ -122,7 +122,7 @@ function check_conservation!(
current = FT(0)
else
previous = getproperty(ccs, sim_name)
current = sum(Interfacer.get_field(sim, Val(:energy)) .* area_fraction) # # ∫ J / m^3 dV
current = sum(FT.(Interfacer.get_field(sim, Val(:energy)) .* area_fraction)) # # ∫ J / m^3 dV
end
push!(previous, current)
total += current
Expand Down
9 changes: 4 additions & 5 deletions src/FieldExchanger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,31 @@ Updates the surface component model cache with the current coupler fields of F_t
"""
function update_sim!(sim::Interfacer.SurfaceModelSimulation, csf, turbulent_fluxes, area_fraction = nothing)

FT = eltype(area_fraction)
FT = eltype(csf.F_turb_energy)

# atmospheric surface density
Interfacer.update_field!(sim, Val(:air_density), csf.ρ_sfc)

# turbulent fluxes
# when PartitionedStateFluxes, turbulent fluxes are updated during the flux calculation
if turbulent_fluxes isa FluxCalculator.CombinedStateFluxes

Interfacer.update_field!(
sim,
Val(:turbulent_energy_flux),
Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_turb_energy,
FT.(Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_turb_energy),
)
Interfacer.update_field!(
sim,
Val(:turbulent_moisture_flux),
Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_turb_moisture,
FT.(Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_turb_moisture),
)
end

# radiative fluxes
Interfacer.update_field!(
sim,
Val(:radiative_energy_flux),
Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_radiative,
FT.(Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_radiative),
)

# precipitation
Expand Down
30 changes: 17 additions & 13 deletions test/conservation_checker_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,40 @@ struct TestAtmos{I} <: Interfacer.AtmosModelSimulation
i::I
end
name(s::TestAtmos) = "TestAtmos"
get_field(s::TestAtmos, ::Val{:F_radiative_TOA}) = ones(s.i.space) .* 200
get_field(s::TestAtmos, ::Val{:energy}) = ones(s.i.space) .* 1e6
get_field(s::TestAtmos, ::Val{:water}) = ones(s.i.space) .* 1

struct TestOcean{I} <: Interfacer.SurfaceModelSimulation
i::I
end
name(s::TestOcean) = "TestOcean"
get_field(s::TestOcean, ::Val{:energy}) = ones(s.i.space) .* 1e6
get_field(s::TestOcean, ::Val{:water}) = ones(s.i.space) .* 0
get_field(s::TestOcean, ::Val{:area_fraction}) = ones(s.i.space) .* 0.25

struct TestLand{I} <: Interfacer.SurfaceModelSimulation
i::I
end
name(s::TestLand) = "TestLand"
get_field(s::TestLand, ::Val{:energy}) = ones(s.i.space) .* 0
get_field(s::TestLand, ::Val{:water}) = ones(s.i.space) .* 0
get_field(s::TestLand, ::Val{:area_fraction}) = ones(s.i.space) .* 0.25



for FT in (Float32, Float64)
global get_field(s::TestAtmos, ::Val{:F_radiative_TOA}) = ones(s.i.space) .* FT.(200)
global get_field(s::TestAtmos, ::Val{:energy}) = ones(s.i.space) .* FT.(1e6)
global get_field(s::TestAtmos, ::Val{:water}) = ones(s.i.space) .* FT.(1)

global get_field(s::TestOcean, ::Val{:energy}) = ones(s.i.space) .* FT.(1e6)
global get_field(s::TestOcean, ::Val{:water}) = ones(s.i.space) .* FT.(0)
global get_field(s::TestOcean, ::Val{:area_fraction}) = ones(s.i.space) .* FT.(0.25)

global get_field(s::TestLand, ::Val{:energy}) = ones(s.i.space) .* FT.(0)
global get_field(s::TestLand, ::Val{:water}) = ones(s.i.space) .* FT.(0)
global get_field(s::TestLand, ::Val{:area_fraction}) = ones(s.i.space) .* FT.(0.25)

@testset "test check_conservation for conservation for FT=$FT" begin
space = TestHelper.create_space(FT)

# set up model simulations
atmos = TestAtmos((; space = space))
land = TestOcean((; space = space))
ocean = TestLand((; space = space))
ice = SurfaceStub((; area_fraction = Fields.ones(space) .* 0.5))
ice = SurfaceStub((; area_fraction = Fields.ones(space) .* FT(0.5)))
model_sims = (; atmos_sim = atmos, land_sim = land, ocean_sim = ocean, ice_sim = ice)

# conservation checkers
Expand Down Expand Up @@ -96,8 +100,8 @@ for FT in (Float32, Float64)
Δt = cs.Δt_cpl

# analytical solution
tot_energy_an = sum(F_r .* 3Δt .+ 1e6 .* 1.25)
tot_water_an = sum(.-P .* 3Δt .* 0.5 .+ Fields.ones(space))
tot_energy_an = sum(FT.(F_r .* 3Δt .+ 1e6 .* 1.25))
tot_water_an = sum(FT.(.-P .* 3Δt .* 0.5 .+ Fields.ones(space)))

# run check_conservation!
check_conservation!(cs, runtime_check = true)
Expand Down Expand Up @@ -132,7 +136,7 @@ for FT in (Float32, Float64)
atmos = TestAtmos((; space = space))
land = TestOcean((; space = space))
ocean = TestLand((; space = space))
ice = SurfaceStub((; area_fraction = Fields.ones(space) .* 0.5))
ice = SurfaceStub((; area_fraction = Fields.ones(space) .* FT(0.5)))
model_sims = (; atmos_sim = atmos, land_sim = land, ocean_sim = ocean, ice_sim = ice)

# conservation checkers
Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import ClimaCoupler.Diagnostics:


for FT in (Float32, Float64)
get_var(cs::Interfacer.CoupledSimulation, ::Val{:x}) = FT(1)
global get_var(cs::Interfacer.CoupledSimulation, ::Val{:x}) = FT(1)

@testset "init_diagnostics for FT=$FT" begin
names = (:x, :y)
Expand Down Expand Up @@ -58,7 +58,7 @@ for FT in (Float32, Float64)
accumulate_diagnostics!(cs)
@test cs.diagnostics[1].field_vector[1] == expected_results[c_i]

@test get_var(cs, Val(:z)) == nothing
@test isnothing(get_var(cs, Val(:z)))
end
end

Expand Down
99 changes: 52 additions & 47 deletions test/field_exchanger_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,58 @@ function calculate_surface_air_density(atmos_sim::DummySimulation, T_S::Fields.F
return T_S .* FT(0.0) .+ FT(1.0)
end


# surface field exchange tests
struct TestSurfaceSimulation1{C} <: SurfaceModelSimulation
cache_field::C
end
struct TestSurfaceSimulation2{C} <: SurfaceModelSimulation
cache_field::C
end

get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:surface_temperature}) =
sim.cache_field .* eltype(sim.cache_field)(1.0)
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:albedo}) =
sim.cache_field .* eltype(sim.cache_field)(1.0)
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:roughness_momentum}) =
sim.cache_field .* eltype(sim.cache_field)(1.0)
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:roughness_buoyancy}) =
sim.cache_field .* eltype(sim.cache_field)(1.0)
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:beta}) =
sim.cache_field .* eltype(sim.cache_field)(1.0)

get_field(sim::TestSurfaceSimulation1, ::Val{:area_fraction}) = sim.cache_field .* eltype(sim.cache_field)(0)
get_field(sim::TestSurfaceSimulation2, ::Val{:area_fraction}) = sim.cache_field .* eltype(sim.cache_field)(0.5)

get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:surface_humidity}) =
sim.cache_field .* eltype(sim.cache_field)(0)
get_field(sim::Union{TestSurfaceSimulation2, TestSurfaceSimulation2}, ::Val{:surface_humidity}) =
sim.cache_field .* eltype(sim.cache_field)(0)


# atmos sim
struct TestAtmosSimulation{C} <: AtmosModelSimulation
cache::C
end
function update_field!(sim::TestAtmosSimulation, ::Val{:albedo}, field)
parent(sim.cache.albedo) .= parent(field)
end
function update_field!(sim::TestAtmosSimulation, ::Val{:roughness_momentum}, field)
parent(sim.cache.roughness_momentum) .= parent(field)
end

#surface sim
struct TestSurfaceSimulationLand{C} <: SurfaceModelSimulation
cache::C
end
get_field(::TestSurfaceSimulationLand, ::Val{:area_fraction}) = 0.5
function update_field!(sim::TestSurfaceSimulationLand, ::Val{:turbulent_energy_flux}, field)
parent(sim.cache.turbulent_energy_flux) .= parent(field)
end
function update_field!(sim::TestSurfaceSimulationLand, ::Val{:turbulent_moisture_flux}, field)
parent(sim.cache.turbulent_moisture_flux) .= parent(field)
end

for FT in (Float32, Float64)
@testset "import_atmos_fields! for FT=$FT" begin
boundary_space = TestHelper.create_space(FT)
Expand Down Expand Up @@ -58,31 +110,6 @@ for FT in (Float32, Float64)
end
end

# surface field exchange tests
struct TestSurfaceSimulation1{C} <: SurfaceModelSimulation
cache_field::C
end
struct TestSurfaceSimulation2{C} <: SurfaceModelSimulation
cache_field::C
end

get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:surface_temperature}) =
sim.cache_field .* 1.0
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:albedo}) = sim.cache_field .* 1.0
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:roughness_momentum}) =
sim.cache_field .* 1.0
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:roughness_buoyancy}) =
sim.cache_field .* 1.0
get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:beta}) = sim.cache_field .* 1.0

get_field(sim::TestSurfaceSimulation1, ::Val{:area_fraction}) = sim.cache_field .* 0.0
get_field(sim::TestSurfaceSimulation2, ::Val{:area_fraction}) = sim.cache_field .* 0.5

get_field(sim::Union{TestSurfaceSimulation1, TestSurfaceSimulation2}, ::Val{:surface_humidity}) =
sim.cache_field .* 0.0
get_field(sim::Union{TestSurfaceSimulation2, TestSurfaceSimulation2}, ::Val{:surface_humidity}) =
sim.cache_field .* 0.0


@testset "import_combined_surface_fields! for FT=$FT" begin
# coupler cache setup
Expand All @@ -109,28 +136,6 @@ for FT in (Float32, Float64)
end
end

# atmos sim
struct TestAtmosSimulation{C} <: AtmosModelSimulation
cache::C
end
function update_field!(sim::TestAtmosSimulation, ::Val{:albedo}, field)
parent(sim.cache.albedo) .= parent(field)
end
function update_field!(sim::TestAtmosSimulation, ::Val{:roughness_momentum}, field)
parent(sim.cache.roughness_momentum) .= parent(field)
end

#surface sim
struct TestSurfaceSimulationLand{C} <: SurfaceModelSimulation
cache::C
end
get_field(::TestSurfaceSimulationLand, ::Val{:area_fraction}) = 0.5
function update_field!(sim::TestSurfaceSimulationLand, ::Val{:turbulent_energy_flux}, field)
parent(sim.cache.turbulent_energy_flux) .= parent(field)
end
function update_field!(sim::TestSurfaceSimulationLand, ::Val{:turbulent_moisture_flux}, field)
parent(sim.cache.turbulent_moisture_flux) .= parent(field)
end
@testset "update_model_sims! for FT=$FT" begin
# coupler cache setup
boundary_space = TestHelper.create_space(FT)
Expand Down
6 changes: 3 additions & 3 deletions test/interfacer_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ struct DummySimulation3 <: LandModelSimulation end

for FT in (Float32, Float64)
boundary_space = TestHelper.create_space(FT)
get_field(::SurfaceModelSimulation, ::Val{:var}) = ones(boundary_space)
get_field(::SurfaceModelSimulation, ::Val{:var_float}) = FT(2)
get_field(::SurfaceModelSimulation, ::Val{:surface_temperature}) = ones(boundary_space) .* FT(300)
global get_field(::SurfaceModelSimulation, ::Val{:var}) = ones(boundary_space)
global get_field(::SurfaceModelSimulation, ::Val{:var_float}) = 2.0
global get_field(::SurfaceModelSimulation, ::Val{:surface_temperature}) = ones(boundary_space) .* 300.0

@testset "test CoupledSim construction, float_type for FT=$FT" begin
cs = CoupledSimulation{FT}(
Expand Down

0 comments on commit afc5614

Please sign in to comment.