Skip to content

Commit ce3f197

Browse files
committed
replace interpolate with remap [skip ci]
1 parent df49a4f commit ce3f197

File tree

1 file changed

+36
-58
lines changed

1 file changed

+36
-58
lines changed

experiments/ClimaEarth/components/ocean/oceananigans.jl

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,24 @@ function construct_remappers(grid_oc, boundary_space)
188188

189189
remapper_oc_to_cc = CR.Regridder(vertices_cc, vertices_oc)
190190

191+
# Create a field of ones on the boundary space so we can compute element areas
191192
field_ones_cc = CC.Fields.ones(boundary_space)
192193

193194
# Allocate a vector with length equal to the number of elements in the target space
194195
# To be used as a temp field for remapping
195-
# TODO think about name
196196
value_per_element_cc =
197197
zeros(Float64, CC.Meshes.nelements(boundary_space.grid.topology.mesh))
198-
return (; remapper_oc_to_cc, field_ones_cc, value_per_element_cc)
198+
199+
# Construct two 2D Oceananigans Center/Center fields to use as scratch space while remapping
200+
scratch_field_oc1 = OC.Field{OC.Center, OC.Center, Nothing}(grid_oc)
201+
scratch_field_oc2 = OC.Field{OC.Center, OC.Center, Nothing}(grid_oc)
202+
return (;
203+
remapper_oc_to_cc,
204+
field_ones_cc,
205+
value_per_element_cc,
206+
scratch_field_oc1,
207+
scratch_field_oc2,
208+
)
199209
end
200210

201211
# Non-allocating ClimaCore -> Oceananigans remap
@@ -348,30 +358,17 @@ function FluxCalculator.update_turbulent_fluxes!(sim::OceananigansSimulation, fi
348358
grid = sim.ocean.model.grid
349359
ice_concentration = sim.ice_concentration
350360

351-
# Remap momentum fluxes onto reduced 2D Center, Center fields using scratch arrays and fields
352-
# TODO replace these with remap! calls
353-
CC.Remapping.interpolate!(
354-
sim.remapping.scratch_arr1,
355-
sim.remapping.remapper_cc,
356-
F_turb_ρτxz,
357-
)
358-
OC.set!(sim.remapping.scratch_cc1, sim.remapping.scratch_arr1) # zonal momentum flux
359-
CC.Remapping.interpolate!(
360-
sim.remapping.scratch_arr2,
361-
sim.remapping.remapper_cc,
362-
F_turb_ρτyz,
363-
)
364-
OC.set!(sim.remapping.scratch_cc2, sim.remapping.scratch_arr2) # meridional momentum flux
361+
# Remap momentum fluxes onto scratch 2D Center, Center fields
362+
Interfacer.remap!(sim.remapping.scratch_field_oc1, F_turb_ρτxz, sim.remapping) # zonal momentum flux
363+
Interfacer.remap!(sim.remapping.scratch_field_oc2, F_turb_ρτyz, sim.remapping) # meridional momentum flux
365364

366365
# Rename for clarity; these are now Center, Center Oceananigans fields
367-
F_turb_ρτxz_cc = sim.remapping.scratch_cc1
368-
F_turb_ρτyz_cc = sim.remapping.scratch_cc2
366+
F_turb_ρτxz_oc = OC.interior(sim.remapping.scratch_field_oc1, :, :, 1)
367+
F_turb_ρτyz_oc = OC.interior(sim.remapping.scratch_field_oc2, :, :, 1)
369368

370369
# Weight by (1 - sea ice concentration)
371-
OC.interior(F_turb_ρτxz_cc, :, :, 1) .=
372-
OC.interior(F_turb_ρτxz_cc, :, :, 1) .* (1.0 .- ice_concentration)
373-
OC.interior(F_turb_ρτyz_cc, :, :, 1) .=
374-
OC.interior(F_turb_ρτyz_cc, :, :, 1) .* (1.0 .- ice_concentration)
370+
OC.interior(F_turb_ρτxz_oc, :, :, 1) .= F_turb_ρτxz_oc .* (1.0 .- ice_concentration)
371+
OC.interior(F_turb_ρτyz_oc, :, :, 1) .= F_turb_ρτyz_oc .* (1.0 .- ice_concentration)
375372

376373
# Set the momentum flux BCs at the correct locations using the remapped scratch fields
377374
oc_flux_u = surface_flux(sim.ocean.model.velocities.u)
@@ -386,12 +383,11 @@ function FluxCalculator.update_turbulent_fluxes!(sim::OceananigansSimulation, fi
386383
(; reference_density, heat_capacity, fresh_water_density) = sim.ocean_properties
387384

388385
# Remap the latent and sensible heat fluxes using scratch arrays
389-
CC.Remapping.interpolate!(sim.remapping.scratch_arr1, sim.remapping.remapper_cc, F_lh) # latent heat flux
390-
CC.Remapping.interpolate!(sim.remapping.scratch_arr2, sim.remapping.remapper_cc, F_sh) # sensible heat flux
386+
Interfacer.remap!(sim.remapping.scratch_field_oc1, F_lh, sim.remapping) # latent heat flux
387+
Interfacer.remap!(sim.remapping.scratch_field_oc2, F_sh, sim.remapping) # sensible heat flux
391388

392-
# Rename for clarity; recall F_turb_energy = F_lh + F_sh
393-
remapped_F_lh = sim.remapping.scratch_arr1
394-
remapped_F_sh = sim.remapping.scratch_arr2
389+
remapped_F_lh = OC.interior(sim.remapping.scratch_field_oc1, :, :, 1)
390+
remapped_F_sh = OC.interior(sim.remapping.scratch_field_oc2, :, :, 1)
395391

396392
# TODO: Note, SW radiation penetrates the surface. Right now, we just put
397393
# everything on the surface, but later we will need to account for this.
@@ -404,12 +400,9 @@ function FluxCalculator.update_turbulent_fluxes!(sim::OceananigansSimulation, fi
404400

405401
# Add the part of the salinity flux that comes from the moisture flux, we also need to
406402
# add the component due to precipitation (that was done with the radiative fluxes)
407-
CC.Remapping.interpolate!(
408-
sim.remapping.scratch_arr1,
409-
sim.remapping.remapper_cc,
410-
F_turb_moisture,
411-
)
412-
moisture_fresh_water_flux = sim.remapping.scratch_arr1 ./ fresh_water_density
403+
Interfacer.remap!(sim.remapping.scratch_field_oc1, F_turb_moisture, sim.remapping) # moisture flux
404+
moisture_fresh_water_flux =
405+
OC.interior(sim.remapping.scratch_field_oc1, :, :, 1) ./ fresh_water_density
413406
oc_flux_S = surface_flux(sim.ocean.model.tracers.S)
414407
surface_salinity = OC.interior(sim.ocean.model.tracers.S, :, :, 1)
415408
OC.interior(oc_flux_S, :, :, 1) .=
@@ -446,20 +439,12 @@ function FieldExchanger.update_sim!(sim::OceananigansSimulation, csf)
446439
(; reference_density, heat_capacity, fresh_water_density) = sim.ocean_properties
447440
ice_concentration = sim.ice_concentration
448441

449-
# Remap radiative flux onto scratch array; rename for clarity
450-
CC.Remapping.interpolate!(
451-
sim.remapping.scratch_arr1,
452-
sim.remapping.remapper_cc,
453-
csf.SW_d,
454-
)
455-
remapped_SW_d = sim.remapping.scratch_arr1
442+
# Remap radiative flux onto scratch fields; rename for clarity
443+
Interfacer.remap!(sim.remapping.scratch_field_oc1, csf.SW_d, sim.remapping) # shortwave radiation
444+
remapped_SW_d = OC.interior(sim.remapping.scratch_field_oc1, :, :, 1)
456445

457-
CC.Remapping.interpolate!(
458-
sim.remapping.scratch_arr2,
459-
sim.remapping.remapper_cc,
460-
csf.LW_d,
461-
)
462-
remapped_LW_d = sim.remapping.scratch_arr2
446+
Interfacer.remap!(sim.remapping.scratch_field_oc2, csf.LW_d, sim.remapping) # longwave radiation
447+
remapped_LW_d = OC.interior(sim.remapping.scratch_field_oc2, :, :, 1)
463448

464449
# Update only the part due to radiative fluxes. For the full update, the component due
465450
# to latent and sensible heat is missing and will be updated in update_turbulent_fluxes.
@@ -477,18 +462,11 @@ function FieldExchanger.update_sim!(sim::OceananigansSimulation, csf)
477462
) ./ (reference_density * heat_capacity)
478463

479464
# Remap precipitation fields onto scratch arrays; rename for clarity
480-
CC.Remapping.interpolate!(
481-
sim.remapping.scratch_arr1,
482-
sim.remapping.remapper_cc,
483-
csf.P_liq,
484-
)
485-
CC.Remapping.interpolate!(
486-
sim.remapping.scratch_arr2,
487-
sim.remapping.remapper_cc,
488-
csf.P_snow,
489-
)
490-
remapped_P_liq = sim.remapping.scratch_arr1
491-
remapped_P_snow = sim.remapping.scratch_arr2
465+
Interfacer.remap!(sim.remapping.scratch_field_oc1, csf.P_liq, sim.remapping) # liquid precipitation
466+
remapped_P_liq = OC.interior(sim.remapping.scratch_field_oc1, :, :, 1)
467+
468+
Interfacer.remap!(sim.remapping.scratch_field_oc2, csf.P_snow, sim.remapping) # snow precipitation
469+
remapped_P_snow = OC.interior(sim.remapping.scratch_field_oc2, :, :, 1)
492470

493471
# Virtual salt flux
494472
oc_flux_S = surface_flux(sim.ocean.model.tracers.S)

0 commit comments

Comments
 (0)