Skip to content

Commit

Permalink
tests use local FTs
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Dec 8, 2023
1 parent cad1606 commit c4efb06
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
49 changes: 36 additions & 13 deletions test/conservation_checker_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,55 @@ struct TestAtmos{I} <: Interfacer.AtmosModelSimulation
i::I
end
name(s::TestAtmos) = "TestAtmos"
function get_field(s::TestAtmos, ::Val{:F_radiative_TOA})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(200)
end
function get_field(s::TestAtmos, ::Val{:energy})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(1e6)
end
function get_field(s::TestAtmos, ::Val{:water})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(1)
end

struct TestOcean{I} <: Interfacer.SurfaceModelSimulation
i::I
end
name(s::TestOcean) = "TestOcean"
function get_field(s::TestOcean, ::Val{:energy})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(1e6)
end
function get_field(s::TestOcean, ::Val{:water})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(0)
end
function get_field(s::TestOcean, ::Val{:area_fraction})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(0.25)
end

struct TestLand{I} <: Interfacer.SurfaceModelSimulation
i::I
end
name(s::TestLand) = "TestLand"

function get_field(s::TestLand, ::Val{:energy})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(0)
end
function get_field(s::TestLand, ::Val{:water})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(0)
end
function get_field(s::TestLand, ::Val{:area_fraction})
FT = Domains.float_type(Meshes.domain(s.i.space.grid.topology.mesh))
ones(s.i.space) .* FT.(0.25)
end


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)

Expand Down
3 changes: 1 addition & 2 deletions test/diagnostics_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import ClimaCoupler.Diagnostics:
post_save,
save_time_format

get_var(cs::Interfacer.CoupledSimulation, ::Val{:x}) = float_type(cs)(1)

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

@testset "init_diagnostics for FT=$FT" begin
names = (:x, :y)
space = create_space(FT)
Expand Down
17 changes: 12 additions & 5 deletions test/flux_calculator_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ function get_thermo_params(sim::TestAtmos)
TP.ThermodynamicsParameters{FT}(; pairs...)
end

function get_surface_params(sim::TestAtmos)
FT = sim.params.FT
toml_dict = CP.create_toml_dict(FT; dict_type = "alias")
sf_params = create_parameters(toml_dict, UF.BusingerType())
return sf_params
end


# ocean sim object and extensions
struct TestOcean{M, Y, D, I} <: Interfacer.SurfaceModelSimulation
model::M
Expand Down Expand Up @@ -208,10 +216,6 @@ for FT in (Float32, Float64)
@test_throws ErrorException calculate_surface_air_density(sim2, coupler_fields.T_sfc)
end

toml_dict = CP.create_toml_dict(FT; dict_type = "alias")
sf_params = create_parameters(toml_dict, UF.BusingerType())
global get_surface_params(::TestAtmos) = sf_params

@testset "calculate correct fluxes: dry for FT=$FT" begin
surface_scheme_list = (MoninObukhovScheme(), BulkScheme())
for scheme in surface_scheme_list
Expand Down Expand Up @@ -321,7 +325,10 @@ for FT in (Float32, Float64)
end

@testset "get_surface_params for FT=$FT" begin
@test get_surface_params(TestAtmos([], [], [], [])) == sf_params
toml_dict = CP.create_toml_dict(FT; dict_type = "alias")
sf_params = create_parameters(toml_dict, UF.BusingerType())

@test get_surface_params(TestAtmos((; FT = FT), [], [], [])) == sf_params
sim = DummySimulation([], [])
@test_throws ErrorException(
"get_surface_params is required to be dispatched on" * Interfacer.name(sim) * ", but no method defined",
Expand Down
31 changes: 20 additions & 11 deletions test/interfacer_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@ import ClimaCoupler.Interfacer:
update_field!

# test for a simple generic surface model
struct DummySimulation <: SeaIceModelSimulation end
struct DummySimulation2 <: OceanModelSimulation end
struct DummySimulation3 <: LandModelSimulation end
struct DummySimulation{S} <: SeaIceModelSimulation
space::S
end
struct DummySimulation2{S} <: OceanModelSimulation
space::S
end
struct DummySimulation3{S} <: LandModelSimulation
space::S
end

for FT in (Float32, Float64)
boundary_space = TestHelper.create_space(FT)
global get_field(::SurfaceModelSimulation, ::Val{:var}) = ones(boundary_space)
global get_field(::SurfaceModelSimulation, ::Val{:var_float}) = FT(2)
global get_field(::SurfaceModelSimulation, ::Val{:surface_temperature}) = ones(boundary_space) .* FT(300)
get_field(sim::SurfaceModelSimulation, ::Val{:var}) = ones(sim.space)
get_field(sim::SurfaceModelSimulation, ::Val{:surface_temperature}) = ones(sim.space) .* FT(300)
function get_field(sim::SurfaceModelSimulation, ::Val{:var_float})
FT = Domains.float_type(Meshes.domain(sim.space.grid.topology.mesh))
FT(2)
end

for FT in (Float32, Float64)
@testset "test CoupledSim construction, float_type for FT=$FT" begin
cs = CoupledSimulation{FT}(
nothing, # comms_ctx
Expand All @@ -50,7 +58,8 @@ for FT in (Float32, Float64)
end

@testset "get_field indexing" begin
for sim in (DummySimulation(), DummySimulation2(), DummySimulation3())
space = TestHelper.create_space(FT)
for sim in (DummySimulation(space), DummySimulation2(space), DummySimulation3(space))
# field
colidx = Fields.ColumnIndex{2}((1, 1), 73)
@test parent(get_field(sim, Val(:var), colidx))[1] == FT(1)
Expand All @@ -60,14 +69,14 @@ for FT in (Float32, Float64)
end

@testset "undefined get_field" begin
sim = DummySimulation()
space = TestHelper.create_space(FT)
sim = DummySimulation(space)
val = Val(:v)
@test_throws ErrorException("undefined field $val for " * name(sim)) get_field(sim, val)
end

# test for a simple generic surface model
@testset "get_field for a SurfaceStub" begin

toml_dict = CP.create_toml_dict(FT; dict_type = "alias")
aliases = string.(fieldnames(TDP.ThermodynamicsParameters))
param_pairs = CP.get_parameter_values!(toml_dict, aliases, "Thermodynamics")
Expand Down

0 comments on commit c4efb06

Please sign in to comment.