Skip to content

Commit e02fc73

Browse files
committed
use zero(FT) instead of zero(field) in broadcasts
1 parent a3c91e9 commit e02fc73

File tree

10 files changed

+42
-47
lines changed

10 files changed

+42
-47
lines changed

experiments/ClimaEarth/components/land/climaland_bucket.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ function BucketSimulation(
152152
Y.bucket.T .= orog_adjusted_T
153153
end
154154

155-
Y.bucket.W .= 0.15
156-
Y.bucket.Ws .= 0.0
157-
Y.bucket.σS .= 0.0
155+
Y.bucket.W .= FT(0.15)
156+
Y.bucket.Ws .= zero(FT)
157+
Y.bucket.σS .= zero(FT)
158158

159159
# Overwrite initial conditions with interpolated values from a netcdf file using
160160
# the `SpaceVaryingInputs` tool. We expect the file to contain the following variables:

experiments/ClimaEarth/components/land/climaland_integrated.jl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,8 @@ function FluxCalculator.compute_surface_fluxes!(
607607
)
608608

609609
# Zero out the fluxes where the area fraction is zero
610-
@. csf.scalar_temp1 =
611-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
612-
@. csf.scalar_temp2 =
613-
ifelse(area_fraction == 0, zero(csf.scalar_temp2), csf.scalar_temp2)
610+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
611+
@. csf.scalar_temp2 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp2)
614612

615613
# Update the coupler field in-place
616614
@. csf.F_lh += csf.scalar_temp1 * area_fraction
@@ -628,8 +626,7 @@ function FluxCalculator.compute_surface_fluxes!(
628626
p.snow.snow_cover_fraction .* snow_dest.vapor_flux
629627
) .* ρ_liq,
630628
)
631-
@. csf.scalar_temp1 =
632-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
629+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
633630
@. csf.F_turb_moisture += csf.scalar_temp1 * area_fraction
634631

635632
# Combine turbulent momentum fluxes from each component of the land model
@@ -640,17 +637,15 @@ function FluxCalculator.compute_surface_fluxes!(
640637
soil_dest.ρτxz .* (1 .- p.snow.snow_cover_fraction) .+
641638
p.snow.snow_cover_fraction .* snow_dest.ρτxz,
642639
)
643-
@. csf.scalar_temp1 =
644-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
640+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
645641
@. csf.F_turb_ρτxz += csf.scalar_temp1 * area_fraction
646642

647643
Interfacer.remap!(
648644
csf.scalar_temp1,
649645
soil_dest.ρτyz .* (1 .- p.snow.snow_cover_fraction) .+
650646
p.snow.snow_cover_fraction .* snow_dest.ρτyz,
651647
)
652-
@. csf.scalar_temp1 =
653-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
648+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
654649
@. csf.F_turb_ρτyz += csf.scalar_temp1 * area_fraction
655650

656651
# Combine the buoyancy flux from each component of the land model
@@ -661,15 +656,13 @@ function FluxCalculator.compute_surface_fluxes!(
661656
soil_dest.buoy_flux .* (1 .- p.snow.snow_cover_fraction) .+
662657
p.snow.snow_cover_fraction .* snow_dest.buoy_flux,
663658
)
664-
@. csf.scalar_temp1 =
665-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
659+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
666660
@. csf.buoyancy_flux += csf.scalar_temp1 * area_fraction
667661

668662
# Compute ustar from the momentum fluxes and surface air density
669663
# ustar = sqrt(ρτ / ρ)
670664
@. csf.scalar_temp1 = sqrt(sqrt(csf.F_turb_ρτxz^2 + csf.F_turb_ρτyz^2) / csf.ρ_atmos)
671-
@. csf.scalar_temp1 =
672-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
665+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
673666
# If ustar is zero, set it to eps to avoid division by zero in the atmosphere
674667
@. csf.ustar += max(csf.scalar_temp1 * area_fraction, eps(FT))
675668

@@ -683,8 +676,7 @@ function FluxCalculator.compute_surface_fluxes!(
683676
surface_params = LP.surface_fluxes_parameters(sim.model.soil.parameters.earth_param_set)
684677
@. csf.scalar_temp1 =
685678
-csf.ustar^3 / SFP.von_karman_const(surface_params) / non_zero(csf.buoyancy_flux)
686-
@. csf.scalar_temp1 =
687-
ifelse(area_fraction == 0, zero(csf.scalar_temp1), csf.scalar_temp1)
679+
@. csf.scalar_temp1 = ifelse(area_fraction == 0, zero(FT), csf.scalar_temp1)
688680
# When L_MO is infinite, avoid multiplication by zero to prevent NaN
689681
@. csf.L_MO +=
690682
ifelse(isinf(csf.scalar_temp1), csf.scalar_temp1, csf.scalar_temp1 * area_fraction)

experiments/ClimaEarth/components/ocean/oceananigans.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ function FieldExchanger.resolve_area_fractions!(
238238
polar_mask .= abs.(lat) .>= FT(80)
239239

240240
# Set land fraction to 1 and ice/ocean fraction to 0 where polar_mask is 1
241-
@. land_fraction = ifelse.(polar_mask == FT(1), FT(1), land_fraction)
242-
@. ice_fraction = ifelse.(polar_mask == FT(1), FT(0), ice_fraction)
243-
@. ocean_fraction = ifelse.(polar_mask == FT(1), FT(0), ocean_fraction)
241+
@. land_fraction = ifelse(polar_mask == FT(1), one(FT), land_fraction)
242+
@. ice_fraction = ifelse(polar_mask == FT(1), zero(FT), ice_fraction)
243+
@. ocean_fraction = ifelse(polar_mask == FT(1), zero(FT), ocean_fraction)
244244
end
245245

246246
# Update the ice concentration field in the ocean simulation

experiments/ClimaEarth/components/ocean/prescr_seaice.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function PrescribedIceSimulation(
177177
evaluate!(ice_fraction, SIC_timevaryinginput, tspan[1])
178178

179179
# Make ice fraction binary rather than fractional
180-
ice_fraction = ifelse.(ice_fraction .> FT(0.5), FT(1), FT(0))
180+
@. ice_fraction = ifelse(ice_fraction > FT(0.5), one(FT), zero(FT))
181181

182182
params = IceSlabParameters{FT}(coupled_param_dict)
183183

@@ -322,7 +322,7 @@ function ice_rhs!(dY, Y, p, t)
322322

323323
# Update the cached area fraction with the current SIC
324324
evaluate!(p.area_fraction, p.SIC_timevaryinginput, t)
325-
@. p.area_fraction = ifelse(p.area_fraction > FT(0.5), FT(1), FT(0))
325+
@. p.area_fraction = ifelse(p.area_fraction > FT(0.5), one(FT), zero(FT))
326326

327327
# Overwrite ice fraction with the static land area fraction anywhere we have nonzero land area
328328
# max needed to avoid Float32 errors (see issue #271; Heisenbug on HPC)
@@ -337,7 +337,7 @@ function ice_rhs!(dY, Y, p, t)
337337
F_conductive
338338
) / (h * ρ * c)
339339
# Zero out tendencies where there is no ice, so that ice temperature remains constant there
340-
@. rhs = ifelse(p.area_fraction 0, zero(rhs), rhs)
340+
@. rhs = ifelse(p.area_fraction 0, zero(FT), rhs)
341341

342342
# If tendencies lead to temperature above freezing, set temperature to freezing
343343
@. dY.T_bulk = min(rhs, (T_freeze - Y.T_bulk) / float(p.dt))

experiments/ClimaEarth/components/ocean/slab_ocean.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,11 @@ end
259259
function slab_ocean_rhs!(dY, Y, cache, t)
260260
params, F_turb_energy, SW_d, LW_d = cache
261261
(; α, ϵ, σ, h, ρ, c) = params
262+
FT = eltype(Y)
262263
rhs = @. (-F_turb_energy + (1 - α) * SW_d + ϵ * (LW_d - σ * Y.T_sfc^4)) / (h * ρ * c)
263264

264265
# Zero out tendencies where there is no ocean, so that temperature remains constant there
265-
@. rhs = ifelse(cache.area_fraction 0, zero(rhs), rhs)
266+
@. rhs = ifelse(cache.area_fraction 0, zero(FT), rhs)
266267

267268
# Note that the area fraction has already been applied to the fluxes,
268269
# so we don't need to multiply by it here.

experiments/ClimaEarth/setup_run.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function CoupledSimulation(config_dict::AbstractDict)
269269
land_mask_data =
270270
joinpath(@clima_artifact("landsea_mask_60arcseconds", comms_ctx), "landsea_mask.nc")
271271
land_fraction = SpaceVaryingInput(land_mask_data, "landsea", boundary_space)
272-
land_fraction = ifelse.(land_fraction .> eps(FT), FT(1), FT(0))
272+
@. land_fraction = ifelse(land_fraction > eps(FT), one(FT), zero(FT))
273273

274274
#=
275275
### Surface Models: AMIP and SlabPlanet Modes

experiments/ClimaEarth/test/compare.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function _error(arr1::AbstractArray, arr2::AbstractArray; ABS_TOL = 100eps(eltyp
2626
arr2 = Array(arr2) .* isfinite.(Array(arr2))
2727
diff = abs.(arr1 .- arr2)
2828
denominator = abs.(arr1)
29-
error = ifelse.(denominator .> ABS_TOL, diff ./ denominator, diff)
29+
error = @. ifelse(denominator > ABS_TOL, diff ./ denominator, diff)
3030
return error
3131
end
3232

experiments/ClimaEarth/test/fluxes_test.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ include(joinpath("..", "setup_run.jl"))
2929
cs = CoupledSimulation(config_dict)
3030
step!(cs)
3131
boundary_space = Interfacer.boundary_space(cs)
32+
FT = CC.Spaces.undertype(boundary_space)
3233

3334
# Unpack component models
3435
(; atmos_sim, land_sim, ocean_sim, ice_sim) = cs.model_sims
@@ -46,10 +47,10 @@ include(joinpath("..", "setup_run.jl"))
4647
p = land_sim.integrator.p
4748
land_fraction = Interfacer.get_field(land_sim, Val(:area_fraction))
4849
land_flux = Interfacer.remap(land_sim.integrator.p.bucket.R_n, boundary_space)
49-
@. land_flux = ifelse(land_fraction 0, zero(land_flux), land_flux)
50+
@. land_flux = ifelse(land_fraction 0, zero(FT), land_flux)
5051

5152
err_land = @. atmos_flux - land_flux
52-
@. err_land = ifelse(land_fraction 0, zero(err_land), err_land)
53+
@. err_land = ifelse(land_fraction 0, zero(FT), err_land)
5354
@show "Bucket flux error: $(maximum(abs.(err_land)))"
5455
@test maximum(abs.(err_land)) < 5
5556

@@ -65,7 +66,7 @@ include(joinpath("..", "setup_run.jl"))
6566
ice_rad_flux =
6667
(1 .- α) .* p.SW_d .+
6768
ϵ .* (p.LW_d .- σ .* Interfacer.get_field(ice_sim, Val(:surface_temperature)) .^ 4)
68-
@. ice_rad_flux = ifelse(p.area_fraction 0, zero(ice_rad_flux), ice_rad_flux)
69+
@. ice_rad_flux = ifelse(p.area_fraction 0, zero(FT), ice_rad_flux)
6970
ice_fraction = Interfacer.get_field(ice_sim, Val(:area_fraction))
7071

7172
# Prescribed ocean: SST is prescribed, but for this test we can still compute
@@ -79,7 +80,7 @@ include(joinpath("..", "setup_run.jl"))
7980
σ .* Interfacer.get_field(ocean_sim, Val(:surface_temperature)) .^ 4
8081
)
8182
ocean_fraction = Interfacer.get_field(ocean_sim, Val(:area_fraction))
82-
@. ocean_rad_flux = ifelse(ocean_fraction 0, zero(ocean_rad_flux), ocean_rad_flux)
83+
@. ocean_rad_flux = ifelse(ocean_fraction 0, zero(FT), ocean_rad_flux)
8384

8485
# Combine component fluxes by area-weighted sum (incl. bucket sign convention):
8586
combined_fluxes =

src/FieldExchanger.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function update_surface_fractions!(cs::Interfacer.CoupledSimulation)
3838
if haskey(cs.model_sims, :land_sim)
3939
land_fraction = Interfacer.get_field(cs.model_sims.land_sim, Val(:area_fraction))
4040
else
41-
cs.fields.scalar_temp1 .= 0
41+
cs.fields.scalar_temp1 .= zero(FT)
4242
land_fraction = cs.fields.scalar_temp1
4343
end
4444

@@ -56,7 +56,7 @@ function update_surface_fractions!(cs::Interfacer.CoupledSimulation)
5656
)
5757
ice_fraction = Interfacer.get_field(ice_sim, Val(:area_fraction))
5858
else
59-
cs.fields.scalar_temp1 .= 0
59+
cs.fields.scalar_temp1 .= zero(FT)
6060
ice_fraction = cs.fields.scalar_temp1
6161
end
6262

@@ -74,7 +74,7 @@ function update_surface_fractions!(cs::Interfacer.CoupledSimulation)
7474
resolve_area_fractions!(ocean_sim, cs.model_sims.ice_sim, land_fraction)
7575
end
7676
else
77-
cs.fields.scalar_temp1 .= 0
77+
cs.fields.scalar_temp1 .= zero(FT)
7878
ocean_fraction = cs.fields.scalar_temp1
7979
end
8080

@@ -331,7 +331,8 @@ is computed from the combined upward longwave radiation.
331331
"""
332332
function combine_surfaces!(combined_field, sims, field_name, scalar_temp)
333333
boundary_space = axes(combined_field)
334-
combined_field .= 0
334+
FT = CC.Spaces.undertype(boundary_space)
335+
combined_field .= zero(FT)
335336
for sim in sims
336337
if sim isa Interfacer.SurfaceModelSimulation
337338
# Store the area fraction of this simulation in `scalar_temp`
@@ -342,7 +343,7 @@ function combine_surfaces!(combined_field, sims, field_name, scalar_temp)
342343
scalar_temp .*
343344
ifelse.(
344345
scalar_temp .≈ 0,
345-
zero(combined_field),
346+
zero(FT),
346347
Interfacer.get_field(sim, field_name, boundary_space),
347348
)
348349
end
@@ -357,7 +358,7 @@ function combine_surfaces!(csf, sims, field_name::Val{:surface_temperature})
357358
boundary_space = axes(T_sfc)
358359
FT = CC.Spaces.undertype(boundary_space)
359360

360-
T_sfc .= FT(0)
361+
T_sfc .= zero(FT)
361362
for sim in sims
362363
if sim isa Interfacer.SurfaceModelSimulation
363364
# Store the area fraction and emissivity of this simulation in temp fields
@@ -373,7 +374,7 @@ function combine_surfaces!(csf, sims, field_name::Val{:surface_temperature})
373374
area_fraction .*
374375
ifelse.(
375376
area_fraction .≈ 0,
376-
zero(T_sfc),
377+
zero(FT),
377378
emissivity_sim .*
378379
Interfacer.get_field(sim, field_name, boundary_space) .^ FT(4),
379380
)

src/FluxCalculator.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,14 @@ function compute_surface_fluxes!(
332332
# Zero out fluxes where the area fraction is zero
333333
# Multiplying by `area_fraction` is not sufficient because the fluxes may
334334
# be NaN where the area fraction is zero.
335-
@. F_turb_ρτxz = ifelse(area_fraction 0, zero(F_turb_ρτxz), F_turb_ρτxz)
336-
@. F_turb_ρτyz = ifelse(area_fraction 0, zero(F_turb_ρτyz), F_turb_ρτyz)
337-
@. F_sh = ifelse(area_fraction 0, zero(F_sh), F_sh)
338-
@. F_lh = ifelse(area_fraction 0, zero(F_lh), F_lh)
339-
@. F_turb_moisture = ifelse(area_fraction 0, zero(F_turb_moisture), F_turb_moisture)
340-
@. L_MO = ifelse(area_fraction 0, zero(L_MO), L_MO)
341-
@. ustar = ifelse(area_fraction 0, zero(ustar), ustar)
342-
@. buoyancy_flux = ifelse(area_fraction 0, zero(buoyancy_flux), buoyancy_flux)
335+
@. F_turb_ρτxz = ifelse(area_fraction 0, zero(FT), F_turb_ρτxz)
336+
@. F_turb_ρτyz = ifelse(area_fraction 0, zero(FT), F_turb_ρτyz)
337+
@. F_sh = ifelse(area_fraction 0, zero(FT), F_sh)
338+
@. F_lh = ifelse(area_fraction 0, zero(FT), F_lh)
339+
@. F_turb_moisture = ifelse(area_fraction 0, zero(FT), F_turb_moisture)
340+
@. L_MO = ifelse(area_fraction 0, zero(FT), L_MO)
341+
@. ustar = ifelse(area_fraction 0, zero(FT), ustar)
342+
@. buoyancy_flux = ifelse(area_fraction 0, zero(FT), buoyancy_flux)
343343

344344
# update the fluxes, which are now area-weighted, of this surface model
345345
fields = (; F_turb_ρτxz, F_turb_ρτyz, F_lh, F_sh, F_turb_moisture)

0 commit comments

Comments
 (0)