|
| 1 | +include("runtests_setup.jl") |
| 2 | + |
| 3 | +using NumericalEarth.OSPapa |
| 4 | +using NumericalEarth.Atmospheres: PrescribedAtmosphere |
| 5 | +using Oceananigans.BoundaryConditions: BoundaryCondition, Flux |
| 6 | +using CUDA: @allowscalar |
| 7 | + |
| 8 | +const OSPAPA_TEST_START = DateTime(2012, 10, 1) |
| 9 | +const OSPAPA_TEST_END = DateTime(2012, 10, 3) |
| 10 | + |
| 11 | +@testset "OS Papa Prescribed Atmosphere" begin |
| 12 | + for arch in test_architectures |
| 13 | + A = typeof(arch) |
| 14 | + @info "Testing OSPapaPrescribedAtmosphere on $A..." |
| 15 | + |
| 16 | + atmosphere = OSPapaPrescribedAtmosphere(arch; |
| 17 | + start_date = OSPAPA_TEST_START, |
| 18 | + end_date = OSPAPA_TEST_END) |
| 19 | + |
| 20 | + @test atmosphere isa PrescribedAtmosphere |
| 21 | + |
| 22 | + # All expected fields are present |
| 23 | + @test haskey(atmosphere.velocities, :u) |
| 24 | + @test haskey(atmosphere.velocities, :v) |
| 25 | + @test haskey(atmosphere.tracers, :T) |
| 26 | + @test haskey(atmosphere.tracers, :q) |
| 27 | + @test !isnothing(atmosphere.pressure) |
| 28 | + @test !isnothing(atmosphere.downwelling_radiation) |
| 29 | + @test haskey(atmosphere.freshwater_flux, :rain) |
| 30 | + |
| 31 | + # Radiation sanity checks |
| 32 | + ℐꜜˢʷ = atmosphere.downwelling_radiation.shortwave |
| 33 | + ℐꜜˡʷ = atmosphere.downwelling_radiation.longwave |
| 34 | + |
| 35 | + @allowscalar begin |
| 36 | + sw_data = interior(ℐꜜˢʷ) |
| 37 | + lw_data = interior(ℐꜜˡʷ) |
| 38 | + |
| 39 | + @test all(sw_data .>= 0) # downwelling SW is non-negative |
| 40 | + @test all(lw_data .>= 0) # downwelling LW is non-negative |
| 41 | + @test maximum(sw_data) < 1500 # below solar constant |
| 42 | + @test maximum(lw_data) < 600 # reasonable atmospheric LW |
| 43 | + @test maximum(lw_data) > 50 # not all zero |
| 44 | + |
| 45 | + # Air temperature in physical range (K) |
| 46 | + T_data = interior(atmosphere.tracers.T) |
| 47 | + @test all(T_data .>= 240) |
| 48 | + @test all(T_data .<= 320) |
| 49 | + end |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +@testset "OS Papa Prescribed Fluxes" begin |
| 54 | + @info "Testing OSPapaPrescribedFluxes on CPU..." |
| 55 | + |
| 56 | + fluxes = OSPapaPrescribedFluxes(; start_date = OSPAPA_TEST_START, |
| 57 | + end_date = OSPAPA_TEST_END) |
| 58 | + |
| 59 | + # NamedTuple structure |
| 60 | + @test haskey(fluxes, :Qnet) |
| 61 | + @test haskey(fluxes, :τx) |
| 62 | + @test haskey(fluxes, :τy) |
| 63 | + @test haskey(fluxes, :EMP) |
| 64 | + @test haskey(fluxes, :times) |
| 65 | + @test haskey(fluxes, :start_date) |
| 66 | + |
| 67 | + # Times are monotonically increasing |
| 68 | + @test issorted(fluxes.times) |
| 69 | + @test length(fluxes.times) > 1 |
| 70 | + |
| 71 | + # No NaN after gap filling (short window with no large gaps) |
| 72 | + @test all(isfinite, fluxes.Qnet) |
| 73 | + @test all(isfinite, fluxes.τx) |
| 74 | + @test all(isfinite, fluxes.τy) |
| 75 | + @test all(isfinite, fluxes.EMP) |
| 76 | +end |
| 77 | + |
| 78 | +@testset "OS Papa Prescribed Flux BCs" begin |
| 79 | + for arch in test_architectures |
| 80 | + A = typeof(arch) |
| 81 | + @info "Testing OSPapaPrescribedFluxBoundaryConditions on $A..." |
| 82 | + |
| 83 | + fluxes = OSPapaPrescribedFluxes(; start_date = OSPAPA_TEST_START, |
| 84 | + end_date = OSPAPA_TEST_END) |
| 85 | + |
| 86 | + bcs = OSPapaPrescribedFluxBoundaryConditions(fluxes, arch) |
| 87 | + |
| 88 | + # Returns BCs for all four fields |
| 89 | + @test haskey(bcs, :u) |
| 90 | + @test haskey(bcs, :v) |
| 91 | + @test haskey(bcs, :T) |
| 92 | + @test haskey(bcs, :S) |
| 93 | + |
| 94 | + # Each has a FluxBoundaryCondition at the top |
| 95 | + @test bcs.u.top isa BoundaryCondition{<:Flux} |
| 96 | + @test bcs.v.top isa BoundaryCondition{<:Flux} |
| 97 | + @test bcs.T.top isa BoundaryCondition{<:Flux} |
| 98 | + @test bcs.S.top isa BoundaryCondition{<:Flux} |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +@testset "OS Papa ocean profile set!" begin |
| 103 | + for arch in test_architectures |
| 104 | + A = typeof(arch) |
| 105 | + @info "Testing OSPapaMetadatum set! on $A..." |
| 106 | + |
| 107 | + grid = RectilinearGrid(arch; |
| 108 | + size = 20, |
| 109 | + x = -144.9, y = 50.1, |
| 110 | + z = (-200, 0), |
| 111 | + topology = (Flat, Flat, Bounded)) |
| 112 | + |
| 113 | + T_field = CenterField(grid) |
| 114 | + S_field = CenterField(grid) |
| 115 | + |
| 116 | + @test begin |
| 117 | + set!(T_field, Metadatum(:temperature, dataset=OSPapaHourly(), date=OSPAPA_TEST_START)) |
| 118 | + set!(S_field, Metadatum(:salinity, dataset=OSPapaHourly(), date=OSPAPA_TEST_START)) |
| 119 | + true |
| 120 | + end |
| 121 | + |
| 122 | + # Values should be finite and physically reasonable |
| 123 | + @allowscalar begin |
| 124 | + T_interior = Array(interior(T_field, 1, 1, :)) |
| 125 | + S_interior = Array(interior(S_field, 1, 1, :)) |
| 126 | + @test all(isfinite, T_interior) |
| 127 | + @test all(isfinite, S_interior) |
| 128 | + @test all(T_interior .> -2) # above freezing |
| 129 | + @test all(T_interior .< 35) # below boiling |
| 130 | + @test all(S_interior .> 20) # saline ocean |
| 131 | + @test all(S_interior .< 45) # not hypersaline |
| 132 | + end |
| 133 | + end |
| 134 | +end |
| 135 | + |
| 136 | +@testset "OS Papa flux BC simulation" begin |
| 137 | + for arch in test_architectures |
| 138 | + A = typeof(arch) |
| 139 | + @info "Testing short simulation with OSPapaPrescribedFluxBoundaryConditions on $A..." |
| 140 | + |
| 141 | + fluxes = OSPapaPrescribedFluxes(; start_date = OSPAPA_TEST_START, |
| 142 | + end_date = OSPAPA_TEST_END) |
| 143 | + |
| 144 | + bcs = OSPapaPrescribedFluxBoundaryConditions(fluxes, arch) |
| 145 | + |
| 146 | + grid = RectilinearGrid(arch; |
| 147 | + size = 10, |
| 148 | + x = -144.9, y = 50.1, |
| 149 | + z = (-200, 0), |
| 150 | + topology = (Flat, Flat, Bounded)) |
| 151 | + |
| 152 | + ocean = ocean_simulation(grid; Δt = 10minutes, |
| 153 | + coriolis = FPlane(latitude=50.1), |
| 154 | + boundary_conditions = bcs) |
| 155 | + |
| 156 | + set!(ocean.model, |
| 157 | + T = Metadatum(:temperature, dataset=OSPapaHourly(), date=OSPAPA_TEST_START), |
| 158 | + S = Metadatum(:salinity, dataset=OSPapaHourly(), date=OSPAPA_TEST_START)) |
| 159 | + |
| 160 | + ocean.stop_iteration = 2 |
| 161 | + |
| 162 | + @test begin |
| 163 | + run!(ocean) |
| 164 | + true |
| 165 | + end |
| 166 | + end |
| 167 | +end |
| 168 | + |
| 169 | +@testset "OS Papa prescribed atmosphere simulation" begin |
| 170 | + for arch in test_architectures |
| 171 | + A = typeof(arch) |
| 172 | + @info "Testing OceanOnlyModel with OSPapaPrescribedAtmosphere on $A..." |
| 173 | + |
| 174 | + grid = RectilinearGrid(arch; |
| 175 | + size = 10, |
| 176 | + x = -144.9, y = 50.1, |
| 177 | + z = (-200, 0), |
| 178 | + topology = (Flat, Flat, Bounded)) |
| 179 | + |
| 180 | + ocean = ocean_simulation(grid; Δt = 10minutes, |
| 181 | + coriolis = FPlane(latitude=50.1)) |
| 182 | + |
| 183 | + set!(ocean.model, |
| 184 | + T = Metadatum(:temperature, dataset=OSPapaHourly(), date=OSPAPA_TEST_START), |
| 185 | + S = Metadatum(:salinity, dataset=OSPapaHourly(), date=OSPAPA_TEST_START)) |
| 186 | + |
| 187 | + atmosphere = OSPapaPrescribedAtmosphere(arch; |
| 188 | + start_date = OSPAPA_TEST_START, |
| 189 | + end_date = OSPAPA_TEST_END) |
| 190 | + |
| 191 | + coupled_model = OceanOnlyModel(ocean; atmosphere, radiation=Radiation(arch)) |
| 192 | + simulation = Simulation(coupled_model; Δt=ocean.Δt, stop_iteration=2) |
| 193 | + |
| 194 | + @test begin |
| 195 | + run!(simulation) |
| 196 | + true |
| 197 | + end |
| 198 | + end |
| 199 | +end |
0 commit comments