|
| 1 | +using ClimaSeaIce |
| 2 | +using ClimaSeaIce.SeaIceDynamics |
| 3 | +using ClimaSeaIce.SeaIceThermodynamics |
| 4 | +using Test |
| 5 | + |
| 6 | +using Oceananigans.Fields: @allowscalar |
| 7 | +using Oceananigans: prognostic_fields |
| 8 | + |
| 9 | +# # Same test as in Oceananigans |
| 10 | +function test_model_equality(test_model, true_model) |
| 11 | + @allowscalar begin |
| 12 | + test_model_fields = prognostic_fields(test_model) |
| 13 | + true_model_fields = prognostic_fields(true_model) |
| 14 | + field_names = keys(test_model_fields) |
| 15 | + |
| 16 | + for name in field_names |
| 17 | + @test all(test_model_fields[name].data .≈ true_model_fields[name].data) |
| 18 | + end |
| 19 | + end |
| 20 | + |
| 21 | + return nothing |
| 22 | +end |
| 23 | + |
| 24 | +# Test copied from Oceananigans.jl, code credit: |
| 25 | +# https://github.com/CliMA/Oceananigans.jl/blob/main/test/test_checkpointer.jl#L94-L174 |
| 26 | +function run_checkpointer_tests(true_model, test_model, Δt) |
| 27 | + true_simulation = Simulation(true_model, Δt=Δt, stop_iteration=5) |
| 28 | + |
| 29 | + checkpointer = Checkpointer(true_model, schedule=IterationInterval(5), overwrite_existing=true) |
| 30 | + push!(true_simulation.output_writers, checkpointer) |
| 31 | + |
| 32 | + run!(true_simulation) # for 5 iterations |
| 33 | + |
| 34 | + checkpointed_model = deepcopy(true_simulation.model) |
| 35 | + |
| 36 | + true_simulation.stop_iteration = 9 |
| 37 | + run!(true_simulation) # for 4 more iterations |
| 38 | + |
| 39 | + ##### |
| 40 | + ##### Test `set!(model, checkpoint_file)` |
| 41 | + ##### |
| 42 | + |
| 43 | + set!(test_model, "checkpoint_iteration5.jld2") |
| 44 | + |
| 45 | + @test test_model.clock.iteration == checkpointed_model.clock.iteration |
| 46 | + @test test_model.clock.time == checkpointed_model.clock.time |
| 47 | + test_model_equality(test_model, checkpointed_model) |
| 48 | + |
| 49 | + # This only applies to QuasiAdamsBashforthTimeStepper: |
| 50 | + @test test_model.clock.last_Δt == checkpointed_model.clock.last_Δt |
| 51 | + |
| 52 | + ##### |
| 53 | + ##### Test pickup from explicit checkpoint path |
| 54 | + ##### |
| 55 | + |
| 56 | + test_simulation = Simulation(test_model, Δt=Δt, stop_iteration=9) |
| 57 | + |
| 58 | + # Pickup from explicit checkpoint path |
| 59 | + run!(test_simulation, pickup="checkpoint_iteration0.jld2") |
| 60 | + |
| 61 | + @info "Testing model equality when running with pickup=checkpoint_iteration0.jld2." |
| 62 | + @test test_simulation.model.clock.iteration == true_simulation.model.clock.iteration |
| 63 | + @test test_simulation.model.clock.time == true_simulation.model.clock.time |
| 64 | + test_model_equality(test_model, true_model) |
| 65 | + |
| 66 | + run!(test_simulation, pickup="checkpoint_iteration5.jld2") |
| 67 | + @info "Testing model equality when running with pickup=checkpoint_iteration5.jld2." |
| 68 | + |
| 69 | + @test test_simulation.model.clock.iteration == true_simulation.model.clock.iteration |
| 70 | + @test test_simulation.model.clock.time == true_simulation.model.clock.time |
| 71 | + test_model_equality(test_model, true_model) |
| 72 | + |
| 73 | + ##### |
| 74 | + ##### Test `run!(sim, pickup=true) |
| 75 | + ##### |
| 76 | + |
| 77 | + # Pickup using existing checkpointer |
| 78 | + test_simulation.output_writers[:checkpointer] = |
| 79 | + Checkpointer(test_model, schedule=IterationInterval(5), overwrite_existing=true) |
| 80 | + |
| 81 | + run!(test_simulation, pickup=true) |
| 82 | + @info " Testing model equality when running with pickup=true." |
| 83 | + |
| 84 | + @test test_simulation.model.clock.iteration == true_simulation.model.clock.iteration |
| 85 | + @test test_simulation.model.clock.time == true_simulation.model.clock.time |
| 86 | + test_model_equality(test_model, true_model) |
| 87 | + |
| 88 | + run!(test_simulation, pickup=0) |
| 89 | + @info " Testing model equality when running with pickup=0." |
| 90 | + |
| 91 | + @test test_simulation.model.clock.iteration == true_simulation.model.clock.iteration |
| 92 | + @test test_simulation.model.clock.time == true_simulation.model.clock.time |
| 93 | + test_model_equality(test_model, true_model) |
| 94 | + |
| 95 | + run!(test_simulation, pickup=5) |
| 96 | + @info " Testing model equality when running with pickup=5." |
| 97 | + |
| 98 | + @test test_simulation.model.clock.iteration == true_simulation.model.clock.iteration |
| 99 | + @test test_simulation.model.clock.time == true_simulation.model.clock.time |
| 100 | + test_model_equality(test_model, true_model) |
| 101 | + |
| 102 | + rm("checkpoint_iteration0.jld2", force=true) |
| 103 | + rm("checkpoint_iteration5.jld2", force=true) |
| 104 | + |
| 105 | + return nothing |
| 106 | +end |
| 107 | + |
| 108 | +function test_sea_ice_checkpointer_output(arch) |
| 109 | + # Create and run "true model" |
| 110 | + Nx, Ny = 16, 16 |
| 111 | + Lx, Ly = 100, 100 |
| 112 | + Δt = 1 |
| 113 | + |
| 114 | + grid = RectilinearGrid(arch, size=(Nx, Ny), x=(0, Lx), y=(0, Ly), topology=(Bounded, Bounded, Flat)) |
| 115 | + for ice_thermodynamics in (nothing, SlabSeaIceThermodynamics(grid)) |
| 116 | + for dynamics in (nothing, SeaIceMomentumEquation(grid)) |
| 117 | + |
| 118 | + true_model = SeaIceModel(grid; dynamics, ice_thermodynamics) |
| 119 | + test_model = deepcopy(true_model) |
| 120 | + |
| 121 | + for field in merge(true_model.velocities, |
| 122 | + (; h = true_model.ice_concentration, |
| 123 | + ℵ = true_model.ice_thickness)) |
| 124 | + |
| 125 | + set!(field, (x, y) -> rand() * 1e-5) |
| 126 | + end |
| 127 | + |
| 128 | + run_checkpointer_tests(true_model, test_model, Δt) |
| 129 | + end |
| 130 | + end |
| 131 | +end |
| 132 | + |
| 133 | +@testset "Checkpointing Tests" begin |
| 134 | + test_sea_ice_checkpointer_output(CPU()) |
| 135 | +end |
0 commit comments