|
| 1 | + |
| 2 | +# get the paths to the necessary data files: land-sea mask, sst map, sea ice concentration |
| 3 | +include(joinpath(pkgdir(ClimaCoupler), "artifacts", "artifact_funcs.jl")) |
| 4 | +sst_data = joinpath(sst_dataset_path(), "sst.nc") |
| 5 | +sic_data = joinpath(sic_dataset_path(), "sic.nc") |
| 6 | +co2_data = joinpath(co2_dataset_path(), "mauna_loa_co2.nc") |
| 7 | +land_mask_data = joinpath(mask_dataset_path(), "seamask.nc") |
| 8 | + |
| 9 | +atmos_sim = atmos_init(FT, config_dict_atmos); |
| 10 | +thermo_params = get_thermo_params(atmos_sim) # TODO: this should be shared by all models |
| 11 | + |
| 12 | +#= |
| 13 | +We use a common `Space` for all global surfaces. This enables the MPI processes to operate on the same columns in both |
| 14 | +the atmospheric and surface components, so exchanges are parallelized. Note this is only possible when the |
| 15 | +atmosphere and surface are of the same horizontal resolution. |
| 16 | +=# |
| 17 | +## init a 2D boundary space at the surface |
| 18 | +boundary_space = Spaces.horizontal_space(atmos_sim.domain.face_space) |
| 19 | + |
| 20 | +# init land-sea fraction |
| 21 | +land_fraction = |
| 22 | + FT.( |
| 23 | + Regridder.land_fraction( |
| 24 | + FT, |
| 25 | + REGRID_DIR, |
| 26 | + comms_ctx, |
| 27 | + land_mask_data, |
| 28 | + "LSMASK", |
| 29 | + boundary_space, |
| 30 | + mono = mono_surface, |
| 31 | + ) |
| 32 | + ) |
| 33 | + |
| 34 | +@info mode_name |
| 35 | +if mode_name == "amip" |
| 36 | + @info "AMIP boundary conditions - do not expect energy conservation" |
| 37 | + |
| 38 | + ## land |
| 39 | + land_sim = bucket_init( |
| 40 | + FT, |
| 41 | + tspan, |
| 42 | + config_dict["land_domain_type"], |
| 43 | + config_dict["land_albedo_type"], |
| 44 | + config_dict["land_temperature_anomaly"], |
| 45 | + comms_ctx, |
| 46 | + REGRID_DIR; |
| 47 | + dt = Δt_cpl, |
| 48 | + space = boundary_space, |
| 49 | + saveat = saveat, |
| 50 | + area_fraction = land_fraction, |
| 51 | + date_ref = date0, |
| 52 | + t_start = t_start, |
| 53 | + ) |
| 54 | + |
| 55 | + ## ocean |
| 56 | + SST_info = bcfile_info_init( |
| 57 | + FT, |
| 58 | + REGRID_DIR, |
| 59 | + sst_data, |
| 60 | + "SST", |
| 61 | + boundary_space, |
| 62 | + comms_ctx, |
| 63 | + interpolate_daily = true, |
| 64 | + scaling_function = clean_sst, ## convert to Kelvin |
| 65 | + land_fraction = land_fraction, |
| 66 | + date0 = date0, |
| 67 | + mono = mono_surface, |
| 68 | + ) |
| 69 | + |
| 70 | + update_midmonth_data!(date0, SST_info) |
| 71 | + SST_init = interpolate_midmonth_to_daily(date0, SST_info) |
| 72 | + ocean_sim = SurfaceStub((; |
| 73 | + T_sfc = SST_init, |
| 74 | + ρ_sfc = ClimaCore.Fields.zeros(boundary_space), |
| 75 | + z0m = FT(1e-3), |
| 76 | + z0b = FT(1e-3), |
| 77 | + beta = FT(1), |
| 78 | + α = FT(0.06), |
| 79 | + area_fraction = (FT(1) .- land_fraction), |
| 80 | + phase = TD.Liquid(), |
| 81 | + thermo_params = thermo_params, |
| 82 | + )) |
| 83 | + |
| 84 | + ## sea ice |
| 85 | + SIC_info = bcfile_info_init( |
| 86 | + FT, |
| 87 | + REGRID_DIR, |
| 88 | + sic_data, |
| 89 | + "SEAICE", |
| 90 | + boundary_space, |
| 91 | + comms_ctx, |
| 92 | + interpolate_daily = true, |
| 93 | + scaling_function = clean_sic, ## convert to fraction |
| 94 | + land_fraction = land_fraction, |
| 95 | + date0 = date0, |
| 96 | + mono = mono_surface, |
| 97 | + ) |
| 98 | + update_midmonth_data!(date0, SIC_info) |
| 99 | + SIC_init = interpolate_midmonth_to_daily(date0, SIC_info) |
| 100 | + ice_fraction = get_ice_fraction.(SIC_init, mono_surface) |
| 101 | + ice_sim = ice_init( |
| 102 | + FT; |
| 103 | + tspan = tspan, |
| 104 | + dt = Δt_cpl, |
| 105 | + space = boundary_space, |
| 106 | + saveat = saveat, |
| 107 | + area_fraction = ice_fraction, |
| 108 | + thermo_params = thermo_params, |
| 109 | + ) |
| 110 | + |
| 111 | + ## CO2 concentration |
| 112 | + CO2_info = bcfile_info_init( |
| 113 | + FT, |
| 114 | + REGRID_DIR, |
| 115 | + co2_data, |
| 116 | + "co2", |
| 117 | + boundary_space, |
| 118 | + comms_ctx, |
| 119 | + interpolate_daily = true, |
| 120 | + land_fraction = ones(boundary_space), |
| 121 | + date0 = date0, |
| 122 | + mono = mono_surface, |
| 123 | + ) |
| 124 | + |
| 125 | + update_midmonth_data!(date0, CO2_info) |
| 126 | + CO2_init = interpolate_midmonth_to_daily(date0, CO2_info) |
| 127 | + update_field!(atmos_sim, Val(:co2_gm), CO2_init) |
| 128 | + |
| 129 | + mode_specifics = (; name = mode_name, SST_info = SST_info, SIC_info = SIC_info, CO2_info = CO2_info) |
| 130 | + |
| 131 | +elseif mode_name in ("slabplanet", "slabplanet_aqua", "slabplanet_terra") |
| 132 | + |
| 133 | + land_fraction = mode_name == "slabplanet_aqua" ? land_fraction .* 0 : land_fraction |
| 134 | + land_fraction = mode_name == "slabplanet_terra" ? land_fraction .* 0 .+ 1 : land_fraction |
| 135 | + |
| 136 | + ## land |
| 137 | + land_sim = bucket_init( |
| 138 | + FT, |
| 139 | + tspan, |
| 140 | + config_dict["land_domain_type"], |
| 141 | + config_dict["land_albedo_type"], |
| 142 | + config_dict["land_temperature_anomaly"], |
| 143 | + comms_ctx, |
| 144 | + REGRID_DIR; |
| 145 | + dt = Δt_cpl, |
| 146 | + space = boundary_space, |
| 147 | + saveat = saveat, |
| 148 | + area_fraction = land_fraction, |
| 149 | + date_ref = date0, |
| 150 | + t_start = t_start, |
| 151 | + ) |
| 152 | + |
| 153 | + ## ocean |
| 154 | + ocean_sim = ocean_init( |
| 155 | + FT; |
| 156 | + tspan = tspan, |
| 157 | + dt = Δt_cpl, |
| 158 | + space = boundary_space, |
| 159 | + saveat = saveat, |
| 160 | + area_fraction = (FT(1) .- land_fraction), ## NB: this ocean fraction includes areas covered by sea ice (unlike the one contained in the cs) |
| 161 | + thermo_params = thermo_params, |
| 162 | + evolving = evolving_ocean, |
| 163 | + ) |
| 164 | + |
| 165 | + ## sea ice (here set to zero area coverage) |
| 166 | + ice_sim = SurfaceStub((; |
| 167 | + T_sfc = ClimaCore.Fields.ones(boundary_space), |
| 168 | + ρ_sfc = ClimaCore.Fields.zeros(boundary_space), |
| 169 | + z0m = FT(0), |
| 170 | + z0b = FT(0), |
| 171 | + beta = FT(1), |
| 172 | + α = FT(1), |
| 173 | + area_fraction = ClimaCore.Fields.zeros(boundary_space), |
| 174 | + phase = TD.Ice(), |
| 175 | + thermo_params = thermo_params, |
| 176 | + )) |
| 177 | + |
| 178 | + mode_specifics = (; name = mode_name, SST_info = nothing, SIC_info = nothing) |
| 179 | +end |
| 180 | + |
| 181 | +#= |
| 182 | +## Coupler Initialization |
| 183 | +The coupler needs to contain exchange information, manage the calendar and be able to access all component models. It can also optionally |
| 184 | +save online diagnostics. These are all initialized here and saved in a global `CouplerSimulation` struct, `cs`. |
| 185 | +=# |
| 186 | + |
| 187 | +## coupler exchange fields |
| 188 | +coupler_field_names = ( |
| 189 | + :T_S, |
| 190 | + :z0m_S, |
| 191 | + :z0b_S, |
| 192 | + :ρ_sfc, |
| 193 | + :q_sfc, |
| 194 | + :albedo, |
| 195 | + :beta, |
| 196 | + :F_turb_energy, |
| 197 | + :F_turb_moisture, |
| 198 | + :F_turb_ρτxz, |
| 199 | + :F_turb_ρτyz, |
| 200 | + :F_radiative, |
| 201 | + :P_liq, |
| 202 | + :P_snow, |
| 203 | + :F_radiative_TOA, |
| 204 | + :P_net, |
| 205 | +) |
| 206 | +coupler_fields = |
| 207 | + NamedTuple{coupler_field_names}(ntuple(i -> ClimaCore.Fields.zeros(boundary_space), length(coupler_field_names))) |
| 208 | + |
| 209 | +## model simulations |
| 210 | +model_sims = (atmos_sim = atmos_sim, ice_sim = ice_sim, land_sim = land_sim, ocean_sim = ocean_sim); |
| 211 | + |
| 212 | +## dates |
| 213 | +dates = (; date = [date], date0 = [date0], date1 = [Dates.firstdayofmonth(date0)], new_month = [false]) |
| 214 | + |
| 215 | +#= |
| 216 | +### Online Diagnostics |
| 217 | +User can write custom diagnostics in the `user_diagnostics.jl`. |
| 218 | +=# |
| 219 | +monthly_3d_diags = init_diagnostics( |
| 220 | + (:T, :u, :q_tot, :q_liq_ice), |
| 221 | + atmos_sim.domain.center_space; |
| 222 | + save = Monthly(), |
| 223 | + operations = (; accumulate = TimeMean([Int(0)])), |
| 224 | + output_dir = COUPLER_OUTPUT_DIR, |
| 225 | + name_tag = "monthly_mean_3d_", |
| 226 | +) |
| 227 | + |
| 228 | +monthly_2d_diags = init_diagnostics( |
| 229 | + (:precipitation_rate, :toa_fluxes, :T_sfc, :tubulent_energy_fluxes), |
| 230 | + boundary_space; |
| 231 | + save = Monthly(), |
| 232 | + operations = (; accumulate = TimeMean([Int(0)])), |
| 233 | + output_dir = COUPLER_OUTPUT_DIR, |
| 234 | + name_tag = "monthly_mean_2d_", |
| 235 | +) |
| 236 | + |
| 237 | +diagnostics = (monthly_3d_diags, monthly_2d_diags) |
| 238 | + |
| 239 | +#= |
| 240 | +## Initialize Conservation Checks |
| 241 | +=# |
| 242 | +## init conservation info collector |
| 243 | +conservation_checks = nothing |
| 244 | +if energy_check |
| 245 | + @assert( |
| 246 | + mode_name[1:10] == "slabplanet" && !CA.is_distributed(ClimaComms.context(boundary_space)), |
| 247 | + "Only non-distributed slabplanet allowable for energy_check" |
| 248 | + ) |
| 249 | + conservation_checks = (; energy = EnergyConservationCheck(model_sims), water = WaterConservationCheck(model_sims)) |
| 250 | +end |
| 251 | + |
| 252 | +dir_paths = (; output = COUPLER_OUTPUT_DIR, artifacts = COUPLER_ARTIFACTS_DIR) |
| 253 | +checkpoint_cb = |
| 254 | + HourlyCallback(dt = FT(480), func = checkpoint_sims, ref_date = [dates.date[1]], active = hourly_checkpoint) # 20 days |
| 255 | +update_firstdayofmonth!_cb = |
| 256 | + MonthlyCallback(dt = FT(1), func = update_firstdayofmonth!, ref_date = [dates.date1[1]], active = true) # for BCReader |
| 257 | +callbacks = (; checkpoint = checkpoint_cb, update_firstdayofmonth! = update_firstdayofmonth!_cb) |
0 commit comments