Skip to content

Commit df49a4f

Browse files
committed
get vertices of OC grid not field, rename values_cc
1 parent 077cfb9 commit df49a4f

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

experiments/ClimaEarth/components/ocean/climaocean_helpers.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,24 @@ end
8686

8787
### Helper functions to use ConservativeRemapping.jl with Oceananigans.jl
8888
"""
89-
compute_cell_matrix(field::AbstractField)
89+
compute_cell_matrix(grid::Union{OC.OrthogonalSphericalShellGrid, OC.LatitudeLongitudeGrid})
9090
9191
Get a vector of vector of coordinate tuples, of the format expected by the
9292
ConservativeRemapping.jl regridder.
9393
"""
94-
function compute_cell_matrix(field::AbstractField)
95-
Fx, Fy, _ = size(field)
96-
LX, LY, _ = Oceananigans.Fields.location(field)
97-
ℓx, ℓy = LX(), LY()
94+
function compute_cell_matrix(
95+
grid::Union{OC.OrthogonalSphericalShellGrid, OC.LatitudeLongitudeGrid},
96+
)
97+
Fx, Fy, _ = size(grid)
98+
# TODO is it ok to hardcode Center? Regridder is specifically for Center, Center fields so I think it's ok
99+
ℓx, ℓy = OC.Center(), OC.Center()
98100

99101
if isnothing(ℓx) || isnothing(ℓy)
100102
error(
101103
"cell_matrix can only be computed for fields with non-nothing horizontal location.",
102104
)
103105
end
104106

105-
grid = field.grid
106107
arch = grid.architecture
107108
FT = eltype(grid)
108109

experiments/ClimaEarth/components/ocean/oceananigans.jl

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Oceananigans as OC
22
import ClimaOcean as CO
33
import ClimaCoupler: Checkpointer, FieldExchanger, FluxCalculator, Interfacer, Utilities
4+
import Interfacer: remap, remap!, get_field
45
import ClimaComms
56
import ClimaCore as CC
67
import Thermodynamics as TD
@@ -182,9 +183,7 @@ To regrid from ClimaCore to Oceananigans, use `CR.regrid!(dest_vector, transpose
182183
"""
183184
function construct_remappers(grid_oc, boundary_space)
184185
# Get the vector of polygons for Oceananigans and ClimaCore spaces
185-
# TODO write compute_cell_matrix for OC grid (not field)
186-
field_oc = OC.Field{OC.Center, OC.Center, Nothing}(grid_oc)
187-
vertices_oc = compute_cell_matrix(field_oc)
186+
vertices_oc = compute_cell_matrix(grid_oc)
188187
vertices_cc = CC.Remapping.get_element_vertices(boundary_space)
189188

190189
remapper_oc_to_cc = CR.Regridder(vertices_cc, vertices_oc)
@@ -194,33 +193,51 @@ function construct_remappers(grid_oc, boundary_space)
194193
# Allocate a vector with length equal to the number of elements in the target space
195194
# To be used as a temp field for remapping
196195
# TODO think about name
197-
values_cc = zeros(Float64, CC.Meshes.nelements(boundary_space.grid.topology.mesh))
198-
return (; remapper_oc_to_cc, field_ones_cc, values_cc)
196+
value_per_element_cc =
197+
zeros(Float64, CC.Meshes.nelements(boundary_space.grid.topology.mesh))
198+
return (; remapper_oc_to_cc, field_ones_cc, value_per_element_cc)
199199
end
200200

201201
# Non-allocating ClimaCore -> Oceananigans remap
202-
function remap!(dst_field::OC.Field, src_field::CC.Fields.Field, remapping)
203-
values_cc = CC.Remappping.get_value_per_element(src_field, remapping.field_ones_cc)
204-
CR.regrid!(vec(interior(dst_field)), transpose(remapping.remapper_oc_to_cc), values_cc)
202+
function Interfacer.remap!(dst_field::OC.Field, src_field::CC.Fields.Field, remapping)
203+
value_per_element_cc =
204+
CC.Remappping.get_value_per_element(src_field, remapping.field_ones_cc)
205+
CR.regrid!(
206+
vec(interior(dst_field)),
207+
transpose(remapping.remapper_oc_to_cc),
208+
value_per_element_cc,
209+
)
205210
return nothing
206211
end
207212
# Allocating ClimaCore -> Oceananigans remap
208-
function remap(src_field::CC.Fields.Field, remapping, dst_space::OC.Grid)
209-
dst_field = OC.Field{Center, Center, Nothing}(dst_space)
213+
function Interfacer.remap(
214+
src_field::CC.Fields.Field,
215+
remapping,
216+
dst_space::Union{OC.OrthogonalSphericalShellGrid, OC.LatitudeLongitudeGrid},
217+
)
218+
dst_field = OC.Field{OC.Center, OC.Center, Nothing}(dst_space)
210219
remap!(dst_field, src_field, remapping)
211220
return dst_field
212221
end
213222

214223
# Non-allocating Oceananigans -> ClimaCore remap
215-
function remap!(dst_field::CC.Fields.Field, src_field::OC.Field, remapping)
216-
CR.regrid!(remapping.values_cc, remapping.remapper_oc_to_cc, vec(interior(src_field)))
224+
function Interfacer.remap!(dst_field::CC.Fields.Field, src_field::OC.Field, remapping)
225+
CR.regrid!(
226+
remapping.value_per_element_cc,
227+
remapping.remapper_oc_to_cc,
228+
vec(interior(src_field)),
229+
)
217230

218231
# Convert the vector of remapped values to a ClimaCore Field with one value per element
219-
CC.Remapping.set_value_per_element!(dst_field, remapping.values_cc)
232+
CC.Remapping.set_value_per_element!(dst_field, remapping.value_per_element_cc)
220233
return nothing
221234
end
222235
# Allocating Oceananigans -> ClimaCore remap
223-
function remap(src_field::OC.Field, remapping, dst_space::CC.Spaces.AbstractSpace)
236+
function Interfacer.remap(
237+
src_field::OC.Field,
238+
remapping,
239+
dst_space::CC.Spaces.AbstractSpace,
240+
)
224241
dst_field = CC.Fields.zeros(dst_space)
225242
remap!(dst_field, src_field, remapping)
226243
return dst_field

0 commit comments

Comments
 (0)