Skip to content

Commit

Permalink
add binary_mask(var) method
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasloan25 committed Jan 31, 2024
1 parent fae83e1 commit 6b6db62
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion experiments/AMIP/components/ocean/eisenman_seaice_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function ∑tendencies(dY, Y, cache, _)
@. dY.q_sfc = -Y.q_sfc / Δt

# update ice area fraction (binary mask for now)
cache.ice_area_fraction .= Regridder.binary_mask.(Y.h_ice, threshold = eps())
cache.ice_area_fraction .= Regridder.binary_mask.(Y.h_ice)

end

Expand Down
7 changes: 4 additions & 3 deletions experiments/AMIP/components/ocean/prescr_seaice_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ function ice_rhs!(du, u, p, _)
rhs = @. (-F_turb_energy - F_radiative + F_conductive) / (params.h * params.ρ * params.c)

# do not count tendencies that lead to temperatures above freezing, and mask out no-ice areas
area_mask = Regridder.binary_mask.(area_fraction, threshold = eps(FT))
unphysical = @. Regridder.binary_mask.(T_freeze - (Y.T_sfc + FT(rhs) * FT(p.dt)), threshold = FT(0)) .* area_mask
area_mask = Regridder.binary_mask.(area_fraction)
threshold = zero(FT)
unphysical = @. Regridder.binary_mask.(T_freeze - (Y.T_sfc + FT(rhs) * FT(p.dt)), threshold) .* area_mask
parent(dY.T_sfc) .= parent(rhs .* unphysical)

@. p.q_sfc = TD.q_vap_saturation_generic.(p.thermo_params, Y.T_sfc, p.ρ_sfc, TD.Ice())
Expand Down Expand Up @@ -129,7 +130,7 @@ clean_sic(SIC, _info) = swap_space!(zeros(axes(_info.land_fraction)), SIC) ./ fl

# setting that SIC < 0.5 is counted as ocean if binary remapping.
get_ice_fraction(h_ice::FT, mono::Bool, threshold = 0.5) where {FT} =
mono ? h_ice : Regridder.binary_mask(h_ice, threshold = FT(threshold))
mono ? h_ice : Regridder.binary_mask(h_ice, threshold)

# extensions required by Interfacer
get_field(sim::PrescribedIceSimulation, ::Val{:surface_temperature}) = sim.integrator.u.T_sfc
Expand Down
25 changes: 8 additions & 17 deletions src/FieldExchanger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,41 +112,32 @@ function update_sim!(atmos_sim::Interfacer.AtmosModelSimulation, csf, turbulent_
end

"""
update_sim!(sim::SurfaceModelSimulation, csf, area_fraction = nothing)
update_sim!(sim::SurfaceModelSimulation, csf, area_fraction)
Updates the surface component model cache with the current coupler fields of F_turb_energy, F_radiative, F_turb_moisture, P_liq, and ρ_sfc.
# Arguments
- `sim`: [Interfacer.SurfaceModelSimulation] containing a surface model simulation object.
- `csf`: [NamedTuple] containing coupler fields.
"""
function update_sim!(sim::Interfacer.SurfaceModelSimulation, csf, turbulent_fluxes, area_fraction = nothing)
function update_sim!(sim::Interfacer.SurfaceModelSimulation, csf, turbulent_fluxes, area_fraction)
FT = eltype(area_fraction)

# atmospheric surface density
Interfacer.update_field!(sim, Val(:air_density), csf.ρ_sfc)

# turbulent fluxes
mask = Regridder.binary_mask.(area_fraction)

# when PartitionedStateFluxes, turbulent fluxes are updated during the flux calculation
if turbulent_fluxes isa FluxCalculator.CombinedStateFluxes
Interfacer.update_field!(
sim,
Val(:turbulent_energy_flux),
FT.(Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_turb_energy),
)
Interfacer.update_field!(
sim,
Val(:turbulent_moisture_flux),
FT.(Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_turb_moisture),
)
F_turb_energy_masked = FT.(mask .* csf.F_turb_energy)
Interfacer.update_field!(sim, Val(:turbulent_energy_flux), F_turb_energy_masked)
Interfacer.update_field!(sim, Val(:turbulent_moisture_flux), FT.(mask .* csf.F_turb_moisture))
end

# radiative fluxes
Interfacer.update_field!(
sim,
Val(:radiative_energy_flux),
FT.(Regridder.binary_mask.(area_fraction, threshold = eps(FT)) .* csf.F_radiative),
)
Interfacer.update_field!(sim, Val(:radiative_energy_flux), FT.(mask .* csf.F_radiative))

# precipitation
Interfacer.update_field!(sim, Val(:liquid_precipitation), csf.P_liq)
Expand Down
2 changes: 1 addition & 1 deletion src/FluxCalculator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function partitioned_turbulent_fluxes!(
# get area fraction (min = 0, max = 1)
area_fraction = Interfacer.get_field(sim, Val(:area_fraction), colidx)
# get area mask [0, 1], where area_mask = 1 if area_fraction > 0
area_mask = Regridder.binary_mask.(area_fraction, threshold = eps())
area_mask = Regridder.binary_mask.(area_fraction)

if !iszero(parent(area_mask))

Expand Down
23 changes: 17 additions & 6 deletions src/Regridder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ function hdwrite_regridfile_rll_to_cgll(
_, _, row_indices = NCDataset(weightfile, "r") do ds_wt
(Array(ds_wt["S"]), Array(ds_wt["col"]), Array(ds_wt["row"]))
end
@show out_type

target_unique_idxs =
out_type == "cgll" ? collect(Spaces.unique_nodes(space2d_undistributed)) :
Expand Down Expand Up @@ -482,7 +481,7 @@ function land_fraction(
file_dates = JLD2.load(joinpath(REGRID_DIR, outfile_root * "_times.jld2"), "times")
fraction = read_from_hdf5(REGRID_DIR, outfile_root, file_dates[1], varname, comms_ctx)
fraction = swap_space!(zeros(boundary_space), fraction) # needed if we are reading from previous run
return mono ? fraction : binary_mask.(fraction, threshold = threshold)
return mono ? fraction : binary_mask.(fraction, threshold)
end

"""
Expand Down Expand Up @@ -519,15 +518,27 @@ function update_surface_fractions!(cs::CoupledSimulation)
end

"""
binary_mask(var::FT; threshold = 0.5)
binary_mask(var, threshold)
Converts a number `var` to 1, if `var` is greater or equal than a given `threshold` value, or 0 otherwise, keeping the same type.
Converts a number `var` to 1, if `var` is greater or equal than a given `threshold` value,
or 0 otherwise, keeping the same type.
# Arguments
- `var`: [FT] value to be converted.
- `threshold`: [Float] cutoff value for conversions.
- `threshold`: [FT] cutoff value for conversions.
"""
binary_mask(var::FT; threshold = FT(0.5)) where {FT} = var < FT(threshold) ? FT(0) : FT(1)
binary_mask(var, threshold) = var >= threshold ? one(var) : zero(var)

"""
binary_mask(var)
Converts a number `var` to 1, if `var` is greater or equal than `eps(FT)`,
or 0 otherwise, keeping the same type.
# Arguments
- `var`: [FT] value to be converted.
"""
binary_mask(var) = binary_mask(var, eps(eltype(var)))

"""
combine_surfaces!(combined_field::Fields.Field, sims, field_name::Val)
Expand Down

0 comments on commit 6b6db62

Please sign in to comment.