Skip to content

Commit

Permalink
tracer advection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milankl committed Dec 18, 2024
1 parent 167cdb3 commit 722715d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/dynamics/tracers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ end
add!(vars::AbstractVariables, tracer_dict::TRACER_DICT) = add!(vars, values(tracer_dict)...)

export activate!
activate!(simulation::AbstractSimulation, tracers::Tracer...) = activate!(simulation.model, tracers...)
activate!(model::AbstractModel, tracers::Tracer...) = activate!(model.tracers, tracers...)
activate!(dict::TRACER_DICT, tracers::Tracer...) = _activate!(dict, tracers, value=true)
activate!(dict::TRACER_DICT, tracers::Tracer...) = _activate!(dict, tracers..., value=true)
function _activate!(dict::TRACER_DICT, tracers::Tracer...; value::Bool=true)
for tracer in tracers
dict[tracer.name].active = value
end
end

export deactivate!
deactivate!(simulation::AbstractSimulation, tracers::Tracer...) = deactivate!(simulation.model, tracers...)
deactivate!(model::AbstractModel, tracers::Tracer...) = deactivate!(model.tracers, tracers...)
deactivate!(dict::TRACER_DICT, tracers::Tracer...) = _activate!(dict, tracers, value=false)
deactivate!(dict::TRACER_DICT, tracers::Tracer...) = _activate!(dict, tracers..., value=false)

Base.delete!(model::AbstractModel, tracer::Tracer) = delete!(model.tracers, tracer.name)
function Base.delete!(simulation::AbstractSimulation, tracer::Tracer)
Expand Down
91 changes: 91 additions & 0 deletions test/tracer_advection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
@testset "Tracers: add!, delete!" begin
spectral_grid = SpectralGrid(nlayers=1)
model = BarotropicModel(spectral_grid)

add!(model, Tracer(:abc))
@test length(model.tracers) == 1

add!(model, Tracer(:t1), Tracer(:t2))
@test length(model.tracers) == 3

delete!(model, Tracer(:t1))
delete!(model, Tracer(:t2))
@test length(model.tracers) == 1

simulation = initialize!(model)

add!(simulation, Tracer(:co2))
@test length(model.tracers) == 2
@test length(simulation.prognostic_variables.tracers) == 2

delete!(simulation, Tracer(:abc))
@test length(model.tracers) == 1
@test length(simulation.prognostic_variables.tracers) == 1
@test length(simulation.diagnostic_variables.grid.tracers_grid) == 1
@test length(simulation.diagnostic_variables.grid.tracers_grid_prev) == 1
@test length(simulation.diagnostic_variables.tendencies.tracers_tend) == 1
@test length(simulation.diagnostic_variables.tendencies.tracers_tend_grid) == 1
end

@testset "Tracers: activate!, deactivate!" begin
spectral_grid = SpectralGrid(nlayers=1)
model = BarotropicModel(spectral_grid)

add!(model, Tracer(:abc))
deactivate!(model, Tracer(:abc))
@test model.tracers[:abc].active == false

activate!(model, Tracer(:abc))
@test model.tracers[:abc].active

simulation = initialize!(model)
deactivate!(simulation, Tracer(:abc))
@test model.tracers[:abc].active == false

activate!(simulation, Tracer(:abc))
@test model.tracers[:abc].active

set!(simulation, abc = (λ, φ, σ) -> exp(--180)^2/10^2))
run!(simulation, period=Day(0))

# initial conditions
abc0 = simulation.diagnostic_variables.grid.tracers_grid[:abc][:, 1]

# check that everything is different after 10 days
run!(simulation, period=Day(10))
abc1 = simulation.diagnostic_variables.grid.tracers_grid[:abc][:, 1]

for ij in eachindex(abc0, abc1)
@test abc0[ij] != abc1[ij]
end

# check that everything is the same if tracer deactivated
deactivate!(simulation, Tracer(:abc))
run!(simulation, period=Day(10))
abc2 = simulation.diagnostic_variables.grid.tracers_grid[:abc][:, 1]

for ij in eachindex(abc1, abc2)
@test abc1[ij] == abc2[ij]
end
end

@testset "Tracers primitive equation models" begin
@testset for Model in (PrimitiveDryModel, PrimitiveWetModel)
spectral_grid = SpectralGrid(nlayers=8)
model = Model(spectral_grid)

add!(model, Tracer(:abc))
simulation = initialize!(model)
set!(simulation, abc = (λ, φ, σ) -> σ*exp(--180)^2/10^2))
run!(simulation, period=Day(0))
abc0 = simulation.diagnostic_variables.grid.tracers_grid[:abc][:, :]

# check that everything is different after simulation
run!(simulation, period=Day(1))
abc1 = simulation.diagnostic_variables.grid.tracers_grid[:abc][:, :]

for ij in eachindex(abc0, abc1)
@test abc0[ij] != abc1[ij]
end
end
end

0 comments on commit 722715d

Please sign in to comment.