|
| 1 | +using Distributed |
| 2 | +import ClimaCalibrate as CAL |
| 3 | +using ClimaCalibrate |
| 4 | +using ClimaAnalysis |
| 5 | +import ClimaAnalysis: SimDir, get, slice, average_xy |
| 6 | +import ClimaComms |
| 7 | +import EnsembleKalmanProcesses: I, ParameterDistributions.constrained_gaussian |
| 8 | + |
| 9 | +# Ensure ClimaComms doesn't use MPI |
| 10 | +ENV["CLIMACOMMS_CONTEXT"] = "SINGLETON" |
| 11 | +ClimaComms.@import_required_backends |
| 12 | + |
| 13 | +single_member_dims = (1,) |
| 14 | +function CAL.observation_map(iteration) |
| 15 | + G_ensemble = Array{Float64}(undef, single_member_dims..., ensemble_size) |
| 16 | + |
| 17 | + for m in 1:ensemble_size |
| 18 | + member_path = CAL.path_to_ensemble_member(output_dir, iteration, m) |
| 19 | + simdir_path = joinpath(member_path, "model_config/output_active/clima_atmos") |
| 20 | + if isdir(simdir_path) |
| 21 | + simdir = SimDir(simdir_path) |
| 22 | + G_ensemble[:, m] .= process_member_data(simdir) |
| 23 | + else |
| 24 | + @info "No data found for member $m." |
| 25 | + G_ensemble[:, m] .= NaN |
| 26 | + end |
| 27 | + end |
| 28 | + return G_ensemble |
| 29 | +end |
| 30 | + |
| 31 | +function process_member_data(simdir::SimDir) |
| 32 | + output = zeros(single_member_dims...) |
| 33 | + days = 86_400 |
| 34 | + isempty(simdir) && return NaN |
| 35 | + |
| 36 | + rsut = get(simdir; short_name = "rsut", reduction = "average", period = "30d") |
| 37 | + rsut_slice = slice(average_lon(average_lat(rsut)); time = 30days).data |
| 38 | + return rsut_slice |
| 39 | +end |
| 40 | + |
| 41 | +addprocs(CAL.SlurmManager()) |
| 42 | +# Make variables and the forward model available on the worker sessions |
| 43 | +@everywhere begin |
| 44 | + import ClimaComms, CUDA |
| 45 | + ENV["CLIMACOMMS_DEVICE"] = "CUDA" |
| 46 | + ENV["CLIMACOMMS_CONTEXT"] = "SINGLETON" |
| 47 | + import ClimaCalibrate as CAL |
| 48 | + import JLD2 |
| 49 | + |
| 50 | + experiment_dir = CAL.project_dir() |
| 51 | + include(joinpath(experiment_dir, "model_interface.jl")) |
| 52 | + output_dir = joinpath(experiment_dir, "output") |
| 53 | + obs_path = joinpath(experiment_dir, "observations.jld2") |
| 54 | +end |
| 55 | + |
| 56 | +# Experiment Configuration |
| 57 | +ensemble_size = 10 |
| 58 | +n_iterations = 5 |
| 59 | +noise = 0.1 * I |
| 60 | +prior = constrained_gaussian("total_solar_irradiance", 1000, 500, 250, 2000) |
| 61 | + |
| 62 | +# Generate observations if needed |
| 63 | +if !isfile(obs_path) |
| 64 | + import JLD2 |
| 65 | + @info "Generating observations" |
| 66 | + obs_output_dir = CAL.path_to_ensemble_member(output_dir, 0, 0) |
| 67 | + mkpath(obs_output_dir) |
| 68 | + touch(joinpath(obs_output_dir, "parameters.toml")) |
| 69 | + CAL.forward_model(0, 0) |
| 70 | + observations = Vector{Float64}(undef, 1) |
| 71 | + observations .= process_member_data(SimDir(joinpath(obs_output_dir, "amip_config/output_active/clima_atmos"))) |
| 72 | + JLD2.save_object(obs_path, observations) |
| 73 | +end |
| 74 | + |
| 75 | +# Initialize experiment data |
| 76 | +@everywhere observations = JLD2.load_object(obs_path) |
| 77 | + |
| 78 | +eki = CAL.calibrate(CAL.WorkerBackend, ensemble_size, n_iterations, observations, noise, prior, output_dir) |
| 79 | + |
| 80 | +# Postprocessing |
| 81 | +import EnsembleKalmanProcesses as EKP |
| 82 | +import Statistics: var, mean |
| 83 | +using Test |
| 84 | +import CairoMakie |
| 85 | + |
| 86 | +function scatter_plot(eki::EKP.EnsembleKalmanProcess) |
| 87 | + f = CairoMakie.Figure(resolution = (800, 600)) |
| 88 | + ax = CairoMakie.Axis(f[1, 1], ylabel = "Parameter Value", xlabel = "Top of atmosphere radiative SW flux") |
| 89 | + |
| 90 | + g = vec.(EKP.get_g(eki; return_array = true)) |
| 91 | + params = vec.((EKP.get_ϕ(prior, eki))) |
| 92 | + |
| 93 | + for (gg, uu) in zip(g, params) |
| 94 | + CairoMakie.scatter!(ax, gg, uu) |
| 95 | + end |
| 96 | + |
| 97 | + CairoMakie.vlines!(ax, observations, linestyle = :dash) |
| 98 | + |
| 99 | + output = joinpath(output_dir, "scatter.png") |
| 100 | + CairoMakie.save(output, f) |
| 101 | + return output |
| 102 | +end |
| 103 | + |
| 104 | +function param_versus_iter_plot(eki::EKP.EnsembleKalmanProcess) |
| 105 | + f = CairoMakie.Figure(resolution = (800, 600)) |
| 106 | + ax = CairoMakie.Axis(f[1, 1], ylabel = "Parameter Value", xlabel = "Iteration") |
| 107 | + params = EKP.get_ϕ(prior, eki) |
| 108 | + for (i, param) in enumerate(params) |
| 109 | + CairoMakie.scatter!(ax, fill(i, length(param)), vec(param)) |
| 110 | + end |
| 111 | + |
| 112 | + output = joinpath(output_dir, "param_vs_iter.png") |
| 113 | + CairoMakie.save(output, f) |
| 114 | + return output |
| 115 | +end |
| 116 | + |
| 117 | +scatter_plot(eki) |
| 118 | +param_versus_iter_plot(eki) |
| 119 | + |
| 120 | +params = EKP.get_ϕ(prior, eki) |
| 121 | +spread = map(var, params) |
| 122 | + |
| 123 | +# Spread should be heavily decreased as particles have converged |
| 124 | +@test last(spread) / first(spread) < 0.1 |
0 commit comments