Skip to content

Commit 6bc6eb3

Browse files
committed
use 0 instead of zero(field) in broadcasts
1 parent 7df0754 commit 6bc6eb3

File tree

6 files changed

+23
-31
lines changed

6 files changed

+23
-31
lines changed

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, 0, csf.scalar_temp1)
611+
@. csf.scalar_temp2 = ifelse(area_fraction == 0, 0, 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, 0, 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, 0, 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, 0, 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, 0, 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, 0, 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, 0, 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/prescr_seaice.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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, 0, 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function slab_ocean_rhs!(dY, Y, cache, t)
262262
rhs = @. (-F_turb_energy + (1 - α) * SW_d + ϵ * (LW_d - σ * Y.T_sfc^4)) / (h * ρ * c)
263263

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

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

experiments/ClimaEarth/test/fluxes_test.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ include(joinpath("..", "setup_run.jl"))
4646
p = land_sim.integrator.p
4747
land_fraction = Interfacer.get_field(land_sim, Val(:area_fraction))
4848
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)
49+
@. land_flux = ifelse(land_fraction 0, 0, land_flux)
5050

5151
err_land = @. atmos_flux - land_flux
52-
@. err_land = ifelse(land_fraction 0, zero(err_land), err_land)
52+
@. err_land = ifelse(land_fraction 0, 0, err_land)
5353
@show "Bucket flux error: $(maximum(abs.(err_land)))"
5454
@test maximum(abs.(err_land)) < 5
5555

@@ -65,7 +65,7 @@ include(joinpath("..", "setup_run.jl"))
6565
ice_rad_flux =
6666
(1 .- α) .* p.SW_d .+
6767
ϵ .* (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)
68+
@. ice_rad_flux = ifelse(p.area_fraction 0, 0, ice_rad_flux)
6969
ice_fraction = Interfacer.get_field(ice_sim, Val(:area_fraction))
7070

7171
# Prescribed ocean: SST is prescribed, but for this test we can still compute

src/FieldExchanger.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function combine_surfaces!(combined_field, sims, field_name, scalar_temp)
342342
scalar_temp .*
343343
ifelse.(
344344
scalar_temp .≈ 0,
345-
zero(combined_field),
345+
0,
346346
Interfacer.get_field(sim, field_name, boundary_space),
347347
)
348348
end
@@ -373,7 +373,7 @@ function combine_surfaces!(csf, sims, field_name::Val{:surface_temperature})
373373
area_fraction .*
374374
ifelse.(
375375
area_fraction .≈ 0,
376-
zero(T_sfc),
376+
0,
377377
emissivity_sim .*
378378
Interfacer.get_field(sim, field_name, boundary_space) .^ FT(4),
379379
)

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, 0, F_turb_ρτxz)
336+
@. F_turb_ρτyz = ifelse(area_fraction 0, 0, F_turb_ρτyz)
337+
@. F_sh = ifelse(area_fraction 0, 0, F_sh)
338+
@. F_lh = ifelse(area_fraction 0, 0, F_lh)
339+
@. F_turb_moisture = ifelse(area_fraction 0, 0, F_turb_moisture)
340+
@. L_MO = ifelse(area_fraction 0, 0, L_MO)
341+
@. ustar = ifelse(area_fraction 0, 0, ustar)
342+
@. buoyancy_flux = ifelse(area_fraction 0, 0, 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)