Skip to content

Commit f5c917d

Browse files
committed
try out XESMF regridder
1 parent e16f311 commit f5c917d

File tree

3 files changed

+138
-7
lines changed

3 files changed

+138
-7
lines changed

experiments/ClimaEarth/Manifest-v1.11.toml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,12 @@ git-tree-sha1 = "d9d26935a0bcffc87d2613ce14c527c99fc543fd"
689689
uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
690690
version = "2.5.0"
691691

692+
[[deps.CondaPkg]]
693+
deps = ["JSON3", "Markdown", "MicroMamba", "Pidfile", "Pkg", "Preferences", "Scratch", "TOML", "pixi_jll"]
694+
git-tree-sha1 = "bd491d55b97a036caae1d78729bdb70bf7dababc"
695+
uuid = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
696+
version = "0.2.33"
697+
692698
[[deps.ConstructionBase]]
693699
git-tree-sha1 = "b4b092499347b18a015186eae3042f72267106cb"
694700
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
@@ -2088,6 +2094,12 @@ version = "1.5.0"
20882094
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
20892095
SimpleWeightedGraphs = "47aef6b3-ad0c-573a-a1e2-d07658019622"
20902096

2097+
[[deps.MicroMamba]]
2098+
deps = ["Pkg", "Scratch", "micromamba_jll"]
2099+
git-tree-sha1 = "011cab361eae7bcd7d278f0a7a00ff9c69000c51"
2100+
uuid = "0b3b1443-0f03-428d-bdfb-f27f9c1191ea"
2101+
version = "0.1.14"
2102+
20912103
[[deps.MicrosoftMPI_jll]]
20922104
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
20932105
git-tree-sha1 = "bc95bf4149bf535c09602e3acdf950d9b4376227"
@@ -2413,6 +2425,12 @@ git-tree-sha1 = "7d2f8f21da5db6a806faf7b9b292296da42b2810"
24132425
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
24142426
version = "2.8.3"
24152427

2428+
[[deps.Pidfile]]
2429+
deps = ["FileWatching", "Test"]
2430+
git-tree-sha1 = "2d8aaf8ee10df53d0dfb9b8ee44ae7c04ced2b03"
2431+
uuid = "fa939f87-e72e-5be4-a000-7fc836dbe307"
2432+
version = "1.3.0"
2433+
24162434
[[deps.Pixman_jll]]
24172435
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "LLVMOpenMP_jll", "Libdl"]
24182436
git-tree-sha1 = "db76b1ecd5e9715f3d043cec13b2ec93ce015d53"
@@ -2535,6 +2553,20 @@ git-tree-sha1 = "1d36ef11a9aaf1e8b74dacc6a731dd1de8fd493d"
25352553
uuid = "43287f4e-b6f4-7ad1-bb20-aadabca52c3d"
25362554
version = "1.3.0"
25372555

2556+
[[deps.PythonCall]]
2557+
deps = ["CondaPkg", "Dates", "Libdl", "MacroTools", "Markdown", "Pkg", "Serialization", "Tables", "UnsafePointers"]
2558+
git-tree-sha1 = "34510e11cabd7964291f32f14d28b367e9960e6e"
2559+
uuid = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
2560+
version = "0.9.28"
2561+
2562+
[deps.PythonCall.extensions]
2563+
CategoricalArraysExt = "CategoricalArrays"
2564+
PyCallExt = "PyCall"
2565+
2566+
[deps.PythonCall.weakdeps]
2567+
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
2568+
PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
2569+
25382570
[[deps.QOI]]
25392571
deps = ["ColorTypes", "FileIO", "FixedPointNumbers"]
25402572
git-tree-sha1 = "8b3fc30bc0390abdce15f8822c889f669baed73d"
@@ -3316,6 +3348,11 @@ weakdeps = ["LLVM"]
33163348
[deps.UnsafeAtomics.extensions]
33173349
UnsafeAtomicsLLVM = ["LLVM"]
33183350

3351+
[[deps.UnsafePointers]]
3352+
git-tree-sha1 = "c81331b3b2e60a982be57c046ec91f599ede674a"
3353+
uuid = "e17b2a0c-0bdf-430a-bd0c-3a23cae4ff39"
3354+
version = "1.0.0"
3355+
33193356
[[deps.VectorInterface]]
33203357
deps = ["LinearAlgebra"]
33213358
git-tree-sha1 = "cea8abaa6e43f72f97a09cf95b80c9eb53ff75cf"
@@ -3340,6 +3377,12 @@ git-tree-sha1 = "c1a7aa6219628fcd757dede0ca95e245c5cd9511"
33403377
uuid = "efce3f68-66dc-5838-9240-27a6d6f5f9b6"
33413378
version = "1.0.0"
33423379

3380+
[[deps.XESMF]]
3381+
deps = ["CondaPkg", "LinearAlgebra", "PythonCall", "SparseArrays"]
3382+
git-tree-sha1 = "f7c53612764f77438c23743d20c926169cb57b45"
3383+
uuid = "2e0b0046-e7a1-486f-88de-807ee8ffabe5"
3384+
version = "0.1.5"
3385+
33433386
[[deps.XML2_jll]]
33443387
deps = ["Artifacts", "JLLWrappers", "Libdl", "Libiconv_jll", "Zlib_jll"]
33453388
git-tree-sha1 = "80d3930c6347cfce7ccf96bd3bafdf079d9c0390"
@@ -3494,6 +3537,12 @@ git-tree-sha1 = "86addc139bca85fdf9e7741e10977c45785727b7"
34943537
uuid = "337d8026-41b4-5cde-a456-74a10e5b31d1"
34953538
version = "1.11.3+0"
34963539

3540+
[[deps.micromamba_jll]]
3541+
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl"]
3542+
git-tree-sha1 = "2ca2ac0b23a8e6b76752453e08428b3b4de28095"
3543+
uuid = "f8abcde7-e9b7-5caa-b8af-a437887ae8e4"
3544+
version = "1.5.12+0"
3545+
34973546
[[deps.nghttp2_jll]]
34983547
deps = ["Artifacts", "Libdl"]
34993548
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
@@ -3510,6 +3559,12 @@ deps = ["Artifacts", "Libdl"]
35103559
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
35113560
version = "17.4.0+2"
35123561

3562+
[[deps.pixi_jll]]
3563+
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl"]
3564+
git-tree-sha1 = "f349584316617063160a947a82638f7611a8ef0f"
3565+
uuid = "4d7b5844-a134-5dcd-ac86-c8f19cd51bed"
3566+
version = "0.41.3+0"
3567+
35133568
[[deps.x264_jll]]
35143569
deps = ["Artifacts", "JLLWrappers", "Libdl"]
35153570
git-tree-sha1 = "14cc7083fc6dff3cc44f2bc435ee96d06ed79aa7"

experiments/ClimaEarth/Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
3232
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
3333
SurfaceFluxes = "49b00bb7-8bd4-4f2b-b78c-51cd0450215f"
3434
Thermodynamics = "b60c26fb-14c3-4610-9d3e-2d17fe7ff00c"
35+
XESMF = "2e0b0046-e7a1-486f-88de-807ee8ffabe5"
3536
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
3637

3738
[compat]
3839
ArgParse = "1.1"
40+
CUDA = "5"
3941
ClimaAnalysis = "0.5.10"
4042
ClimaAtmos = "0.27, 0.28, 0.29, 0.30, 0.31"
4143
ClimaCalibrate = "0.1"
@@ -46,7 +48,6 @@ ClimaParams = "1.0"
4648
ClimaSeaIce = "0.3"
4749
ClimaTimeSteppers = "0.7, 0.8"
4850
ClimaUtilities = "0.1"
49-
CUDA = "5"
5051
EnsembleKalmanProcesses = "2"
5152
Insolation = "0.10.2"
5253
Interpolations = "0.14, 0.15"

experiments/ClimaEarth/components/ocean/oceananigans.jl

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Oceananigans as OC
22
import ClimaOcean as CO
3+
import ClimaAtmos as CA
34
import ClimaCoupler: Checkpointer, FieldExchanger, FluxCalculator, Interfacer, Utilities
45
import ClimaComms
56
import ClimaCore as CC
@@ -46,6 +47,9 @@ function OceananigansSimulation(
4647
output_dir,
4748
comms_ctx = ClimaComms.context(),
4849
)
50+
# using Dates
51+
# start_date = Dates.DateTime(2008)
52+
# stop_date = Dates.DateTime(2008, 1, 2)
4953
arch = comms_ctx.device isa ClimaComms.CUDADevice ? OC.GPU() : OC.CPU()
5054

5155
# Retrieve EN4 data (monthly)
@@ -102,13 +106,84 @@ function OceananigansSimulation(
102106
OC.set!(ocean.model, T = en4_temperature[1], S = en4_salinity[1])
103107

104108
# Construct a remapper from the exchange grid to `Center, Center` fields
105-
long_cc = OC.λnodes(grid, OC.Center(), OC.Center(), OC.Center())
106-
lat_cc = OC.φnodes(grid, OC.Center(), OC.Center(), OC.Center())
109+
110+
# Tripolar to cubed sphere (fails currently)
111+
# long_oc = OC.λnodes(grid, OC.Center(), OC.Center(), OC.Center())
112+
# lat_oc = OC.φnodes(grid, OC.Center(), OC.Center(), OC.Center())
113+
114+
T = OC.CenterField(grid) # field on tripolar grid
115+
OC.set!(T, en4_temperature[1])
116+
coords_tripolar, _ = XESMF.extract_xesmf_coordinates_structure(T, T)
107117

108118
# Create a 2D matrix containing each lat/long combination as a LatLongPoint
109119
# Note this must be done on CPU since the CC.Remapper module is not GPU-compatible
110-
target_points_cc = Array(CC.Geometry.LatLongPoint.(lat_cc, long_cc))
120+
# target_points_oc = Array(CC.Geometry.LatLongPoint.(lat_oc, long_oc))
121+
122+
# TODO double check these coords are on centers, get lat/lon bounds for each element
123+
# boundary_space = axes(area_fraction)
124+
boundary_space = CC.CommonSpaces.CubedSphereSpace(
125+
Float32;
126+
radius = 1.0,
127+
n_quad_points = 2,
128+
h_elem = 10,
129+
)
130+
boundary_coords = CC.Fields.coordinate_field(boundary_space)
131+
boundary_lat_arr = CC.Fields.field2array(boundary_coords.lat)
132+
boundary_long_arr = CC.Fields.field2array(boundary_coords.long)
133+
coords_cubedsphere = Dict("lat" => boundary_lat_arr, "lon" => boundary_long_arr)
134+
135+
regridder_tripolar_to_cubedsphere =
136+
XESMF.Regridder(coords_cubedsphere, coords_tripolar; method = "conservative")
137+
regridder_cubedsphere_to_tripolar =
138+
XESMF.Regridder(coords_tripolar, coords_cubedsphere; method = "conservative")
139+
140+
# Tripolar to LatLon
141+
T = OC.CenterField(grid) # field on tripolar grid
142+
OC.set!(T, en4_temperature[1])
143+
144+
grid_latlon = OC.LatitudeLongitudeGrid(
145+
arch;
146+
size = (Nx, Ny, Nz),
147+
longitude = (0, 360),
148+
latitude = (-81, 90),
149+
z,
150+
)
151+
field_latlon = OC.CenterField(grid_latlon)
152+
remapper_tripolar_to_latlon = XESMF.Regridder(T, field_latlon; method = "conservative")
153+
remapper_tripolar_to_latlon(
154+
vec(OC.interior(field_latlon, :, :, Nz)),
155+
vec(OC.interior(T, :, :, Nz)),
156+
)
157+
OC.fill_halo_regions!(field_latlon)
158+
# heatmap(field_latlon) # using GeoMakie, using CairoMakie
159+
160+
# LatLon to cubed sphere
161+
boundary_space = CC.CommonSpaces.CubedSphereSpace(
162+
Float32;
163+
radius = 1.0,
164+
n_quad_points = 2,
165+
h_elem = 10,
166+
)
167+
field_cubedsphere = Interfacer.remap(field_latlon, boundary_space)
168+
# fieldheatmap(field_cubedsphere) # using ClimaCoreMakie
169+
170+
# Cubed sphere to LatLon
171+
long_oc = OC.λnodes(grid_latlon, OC.Center(), OC.Center(), OC.Center())
172+
lat_oc = OC.φnodes(grid_latlon, OC.Center(), OC.Center(), OC.Center())
173+
long_oc = reshape(long_oc, length(long_oc), 1)
174+
lat_oc = reshape(lat_oc, 1, length(lat_oc))
175+
target_points_oc = @. CC.Geometry.LatLongPoint(lat_oc, long_oc)
176+
177+
remapper_cubedsphere_to_latlon = CC.Remapping.Remapper(boundary_space, target_points_oc)
178+
field_cubedsphere = CC.Fields.zeros(boundary_space)
179+
CC.Remapping.interpolate!(
180+
field_cubedsphere,
181+
remapper_cubedsphere_to_latlon,
182+
field_latlon,
183+
)
184+
111185

186+
# Previous remapper for LatLon
112187
if pkgversion(CC) >= v"0.14.34"
113188
remapper_cc = CC.Remapping.Remapper(axes(area_fraction), target_points_cc)
114189
else
@@ -194,14 +269,14 @@ Interfacer.step!(sim::OceananigansSimulation, t) =
194269

195270
# We always want the surface, so we always set zero(pt.lat) for z
196271
"""
197-
to_node(pt::CA.ClimaCore.Geometry.LatLongPoint)
272+
to_node(pt::CCGeometry.LatLongPoint)
198273
199274
Transform `LatLongPoint` into a tuple (long, lat, 0), where the 0 is needed because we only
200275
care about the surface.
201276
"""
202-
@inline to_node(pt::CA.ClimaCore.Geometry.LatLongPoint) = pt.long, pt.lat, zero(pt.lat)
277+
@inline to_node(pt::CC.Geometry.LatLongPoint) = pt.long, pt.lat, zero(pt.lat)
203278
# This next one is needed if we have "LevelGrid"
204-
@inline to_node(pt::CA.ClimaCore.Geometry.LatLongZPoint) = pt.long, pt.lat, zero(pt.lat)
279+
@inline to_node(pt::CC.Geometry.LatLongZPoint) = pt.long, pt.lat, zero(pt.lat)
205280

206281
"""
207282
map_interpolate(points, oc_field::OC.Field)

0 commit comments

Comments
 (0)