-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathgeneric.py
More file actions
470 lines (425 loc) · 16.7 KB
/
generic.py
File metadata and controls
470 lines (425 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import math
import numpy as np
import uxarray as ux
import xarray as xr
__all__ = ["Nx", "datasets"]
T = 13
Nx = 20
vmax = 1.0
delta = 0.1
TIME = xr.date_range("2000", "2001", T)
def _stommel_gyre_delaunay():
"""
Stommel gyre on a Delaunay grid. the naming convention of the dataset and grid is consistent with what is
provided by UXArray when reading in FESOM2 datasets.
This dataset is a single vertical layer of a barotropic ocean gyre on a square domain with closed boundaries.
The velocity field provides a slow moving interior circulation and a western boundary current. All fields are placed
on the vertices of the grid and at the element vertical faces.
"""
lon, lat = np.meshgrid(np.linspace(0, 60.0, Nx, dtype=np.float32), np.linspace(0, 60.0, Nx, dtype=np.float32))
lon_flat = lon.ravel()
lat_flat = lat.ravel()
zf = np.linspace(0.0, 1000.0, 2, endpoint=True, dtype=np.float32) # Vertical element faces
zc = 0.5 * (zf[:-1] + zf[1:]) # Vertical element centers
# mask any point on one of the boundaries
mask = (
np.isclose(lon_flat, 0.0) | np.isclose(lon_flat, 60.0) | np.isclose(lat_flat, 0.0) | np.isclose(lat_flat, 60.0)
)
boundary_points = np.flatnonzero(mask)
uxgrid = ux.Grid.from_points(
(lon_flat, lat_flat),
method="regional_delaunay",
boundary_points=boundary_points,
)
uxgrid.attrs["Conventions"] = "UGRID-1.0"
# Define arrays U (zonal), V (meridional) and P (sea surface height)
U = np.zeros((1, zc.size, uxgrid.n_face), dtype=np.float64)
V = np.zeros((1, zc.size, uxgrid.n_face), dtype=np.float64)
W = np.zeros((1, zf.size, lat.size), dtype=np.float64)
P = np.zeros((1, zc.size, uxgrid.n_face), dtype=np.float64)
for i, (x, y) in enumerate(zip(uxgrid.face_lon, uxgrid.face_lat, strict=False)):
xi = x / 60.0
yi = y / 60.0
P[0, 0, i] = -vmax * delta * (1 - xi) * (math.exp(-xi / delta) - 1) * np.sin(math.pi * yi)
U[0, 0, i] = -vmax * (1 - math.exp(-xi / delta) - xi) * np.cos(math.pi * yi)
V[0, 0, i] = vmax * ((2.0 - xi) * math.exp(-xi / delta) - 1) * np.sin(math.pi * yi)
u = ux.UxDataArray(
data=U,
name="U",
uxgrid=uxgrid,
dims=["time", "zc", "n_face"],
coords=dict(
time=(["time"], [TIME[0]]),
zc=(["zc"], zc),
),
attrs=dict(
description="zonal velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
),
)
v = ux.UxDataArray(
data=V,
name="V",
uxgrid=uxgrid,
dims=["time", "zc", "n_face"],
coords=dict(
time=(["time"], [TIME[0]]),
zc=(["zc"], zc),
),
attrs=dict(
description="meridional velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
),
)
w = ux.UxDataArray(
data=W,
name="W",
uxgrid=uxgrid,
dims=["time", "zf", "n_node"],
coords=dict(
time=(["time"], [TIME[0]]),
zf=(["zf"], zf),
),
attrs=dict(
description="meridional velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
),
)
p = ux.UxDataArray(
data=P,
name="p",
uxgrid=uxgrid,
dims=["time", "zc", "n_face"],
coords=dict(
time=(["time"], [TIME[0]]),
zc=(["zc"], zc),
),
attrs=dict(description="pressure", units="N/m^2", location="node", mesh="delaunay", Conventions="UGRID-1.0"),
)
return ux.UxDataset({"U": u, "V": v, "W": w, "p": p}, uxgrid=uxgrid)
def _fesom2_square_delaunay_uniform_z_coordinate():
"""
Delaunay grid with uniform z-coordinate, mimicking a FESOM2 dataset.
This dataset consists of a square domain with closed boundaries, where the grid is generated using Delaunay triangulation.
The bottom topography is flat and uniform, and the vertical grid spacing is constant with 10 layers spanning [0,1000.0]
The lateral velocity field components are non-zero constant, and the vertical velocity component is zero.
The pressure field is constant.
All fields are placed on location consistent with FESOM2 variable placement conventions
"""
lon, lat = np.meshgrid(np.linspace(0, 60.0, Nx, dtype=np.float32), np.linspace(0, 60.0, Nx, dtype=np.float32))
lon_flat = lon.ravel()
lat_flat = lat.ravel()
zf = np.linspace(0.0, 1000.0, 10, endpoint=True, dtype=np.float32) # Vertical element faces
zc = 0.5 * (zf[:-1] + zf[1:]) # Vertical element centers
# mask any point on one of the boundaries
mask = (
np.isclose(lon_flat, 0.0) | np.isclose(lon_flat, 60.0) | np.isclose(lat_flat, 0.0) | np.isclose(lat_flat, 60.0)
)
boundary_points = np.flatnonzero(mask)
uxgrid = ux.Grid.from_points(
(lon_flat, lat_flat),
method="regional_delaunay",
boundary_points=boundary_points,
)
uxgrid.attrs["Conventions"] = "UGRID-1.0"
# Define arrays U (zonal), V (meridional) and P (sea surface height)
U = np.ones(
(T, zc.size, uxgrid.n_face), dtype=np.float64
) # Lateral velocity is on the element centers and face centers
V = np.ones(
(T, zc.size, uxgrid.n_face), dtype=np.float64
) # Lateral velocity is on the element centers and face centers
W = np.zeros(
(T, zf.size, uxgrid.n_node), dtype=np.float64
) # Vertical velocity is on the element faces and face vertices
P = np.ones((T, zc.size, uxgrid.n_node), dtype=np.float64) # Pressure is on the element centers and face vertices
u = ux.UxDataArray(
data=U,
name="U",
uxgrid=uxgrid,
dims=["time", "nz1", "n_face"],
coords=dict(
time=(["time"], TIME),
nz1=(["nz1"], zc),
),
attrs=dict(
description="zonal velocity", units="m/s", location="face", mesh="delaunay", Conventions="UGRID-1.0"
),
)
v = ux.UxDataArray(
data=V,
name="V",
uxgrid=uxgrid,
dims=["time", "nz1", "n_face"],
coords=dict(
time=(["time"], TIME),
nz1=(["nz1"], zc),
),
attrs=dict(
description="meridional velocity", units="m/s", location="face", mesh="delaunay", Conventions="UGRID-1.0"
),
)
w = ux.UxDataArray(
data=W,
name="w",
uxgrid=uxgrid,
dims=["time", "nz", "n_node"],
coords=dict(
time=(["time"], TIME),
nz=(["nz"], zf),
),
attrs=dict(
description="vertical velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
),
)
p = ux.UxDataArray(
data=P,
name="p",
uxgrid=uxgrid,
dims=["time", "nz1", "n_node"],
coords=dict(
time=(["time"], TIME),
nz1=(["nz1"], zc),
),
attrs=dict(description="pressure", units="N/m^2", location="node", mesh="delaunay", Conventions="UGRID-1.0"),
)
return ux.UxDataset({"U": u, "V": v, "W": w, "p": p}, uxgrid=uxgrid)
def _fesom2_square_delaunay_antimeridian():
"""
Delaunay grid that crosses the antimeridian with uniform z-coordinate, mimicking a FESOM2 dataset.
This dataset consists of a square domain with closed boundaries, where the grid is generated using Delaunay triangulation.
The bottom topography is flat and uniform, and the vertical grid spacing is constant with 10 layers spanning [0,1000.0]
The lateral velocity field components are non-zero constant, and the vertical velocity component is zero.
The pressure field is constant.
All fields are placed on location consistent with FESOM2 variable placement conventions
"""
lon, lat = np.meshgrid(
np.linspace(-210.0, -150.0, Nx, dtype=np.float32), np.linspace(-40.0, 40.0, Nx, dtype=np.float32)
)
# wrap longitude from [-180,180]
lon_flat = lon.ravel()
lat_flat = lat.ravel()
zf = np.linspace(0.0, 1000.0, 10, endpoint=True, dtype=np.float32) # Vertical element faces
zc = 0.5 * (zf[:-1] + zf[1:]) # Vertical element centers
# mask any point on one of the boundaries
mask = (
np.isclose(lon_flat, -210.0)
| np.isclose(lon_flat, -150.0)
| np.isclose(lat_flat, -40.0)
| np.isclose(lat_flat, 40.0)
)
boundary_points = np.flatnonzero(mask)
uxgrid = ux.Grid.from_points(
(lon_flat, lat_flat),
method="regional_delaunay",
boundary_points=boundary_points,
)
uxgrid.attrs["Conventions"] = "UGRID-1.0"
# Define arrays U (zonal), V (meridional) and P (sea surface height)
U = np.ones(
(T, zc.size, uxgrid.n_face), dtype=np.float64
) # Lateral velocity is on the element centers and face centers
V = np.ones(
(T, zc.size, uxgrid.n_face), dtype=np.float64
) # Lateral velocity is on the element centers and face centers
W = np.zeros(
(T, zf.size, uxgrid.n_node), dtype=np.float64
) # Vertical velocity is on the element faces and face vertices
P = np.ones((T, zc.size, uxgrid.n_node), dtype=np.float64) # Pressure is on the element centers and face vertices
u = ux.UxDataArray(
data=U,
name="U",
uxgrid=uxgrid,
dims=["time", "nz1", "n_face"],
coords=dict(
time=(["time"], TIME),
nz1=(["nz1"], zc),
),
attrs=dict(
description="zonal velocity", units="m/s", location="face", mesh="delaunay", Conventions="UGRID-1.0"
),
)
v = ux.UxDataArray(
data=V,
name="V",
uxgrid=uxgrid,
dims=["time", "nz1", "n_face"],
coords=dict(
time=(["time"], TIME),
nz1=(["nz1"], zc),
),
attrs=dict(
description="meridional velocity", units="m/s", location="face", mesh="delaunay", Conventions="UGRID-1.0"
),
)
w = ux.UxDataArray(
data=W,
name="w",
uxgrid=uxgrid,
dims=["time", "nz", "n_node"],
coords=dict(
time=(["time"], TIME),
nz=(["nz"], zf),
),
attrs=dict(
description="vertical velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
),
)
p = ux.UxDataArray(
data=P,
name="p",
uxgrid=uxgrid,
dims=["time", "nz1", "n_node"],
coords=dict(
time=(["time"], TIME),
nz1=(["nz1"], zc),
),
attrs=dict(description="pressure", units="N/m^2", location="node", mesh="delaunay", Conventions="UGRID-1.0"),
)
return ux.UxDataset({"U": u, "V": v, "W": w, "p": p}, uxgrid=uxgrid)
def _icon_square_delaunay_uniform_z_coordinate():
"""
Delaunay grid with uniform z-coordinate, mimicking an ICON dataset.
This dataset consists of a square domain with closed boundaries, where the grid is generated using Delaunay triangulation.
The bottom topography is flat and uniform, and the vertical grid spacing is constant with 10 layers spanning [0,1000.0]
The lateral velocity field components are non-zero constant, and the vertical velocity component is zero.
The pressure field is constant.
All fields are face registered and at vertical layer centers, except for the vertical velocity component, which is
at vertical layer interfaces.
"""
lon, lat = np.meshgrid(np.linspace(0, 60.0, Nx, dtype=np.float64), np.linspace(0, 60.0, Nx, dtype=np.float64))
lon_flat = lon.ravel()
lat_flat = lat.ravel()
zf = np.linspace(0.0, 1000.0, 10, endpoint=True, dtype=np.float64) # Vertical element faces
zc = 0.5 * (zf[:-1] + zf[1:]) # Vertical element centers
# mask any point on one of the boundaries
mask = (
np.isclose(lon_flat, 0.0) | np.isclose(lon_flat, 60.0) | np.isclose(lat_flat, 0.0) | np.isclose(lat_flat, 60.0)
)
boundary_points = np.flatnonzero(mask)
uxgrid = ux.Grid.from_points(
(lon_flat, lat_flat),
method="regional_delaunay",
boundary_points=boundary_points,
)
# Define arrays U (zonal), V (meridional) and P (sea surface height)
U = np.ones(
(T, zc.size, uxgrid.n_face), dtype=np.float64
) # Lateral velocity is on the element centers and face centers
V = np.ones(
(T, zc.size, uxgrid.n_face), dtype=np.float64
) # Lateral velocity is on the element centers and face centers
W = np.zeros(
(T, zf.size, uxgrid.n_face), dtype=np.float64
) # Vertical velocity is on the element faces and face vertices
P = np.ones((T, zc.size, uxgrid.n_node), dtype=np.float64) # Pressure is on the element centers and face vertices
U[0, :, :] = zc[:, None] * uxgrid.face_lon.values[None, :]
V[0, :, :] = zc[:, None] * uxgrid.face_lat.values[None, :]
W[0, :, :] = zf[:, None] * uxgrid.face_lon.values[None, :] * uxgrid.face_lat.values[None, :]
P[0, :, :] = zc[:, None] * (uxgrid.node_lon.values[None, :] + uxgrid.node_lat.values[None, :])
u = ux.UxDataArray(
data=U,
name="U",
uxgrid=uxgrid,
dims=["time", "depth", "n_face"],
coords=dict(
time=(["time"], TIME),
depth=(["depth"], zc),
),
attrs=dict(
description="zonal velocity", units="m/s", location="face", mesh="delaunay", Conventions="UGRID-1.0"
),
)
v = ux.UxDataArray(
data=V,
name="V",
uxgrid=uxgrid,
dims=["time", "depth", "n_face"],
coords=dict(
time=(["time"], TIME),
depth=(["depth"], zc),
),
attrs=dict(
description="meridional velocity", units="m/s", location="face", mesh="delaunay", Conventions="UGRID-1.0"
),
)
w = ux.UxDataArray(
data=W,
name="W",
uxgrid=uxgrid,
dims=["time", "depth_2", "n_face"],
coords=dict(
time=(["time"], TIME),
depth_2=(["depth_2"], zf),
),
attrs=dict(
description="vertical velocity", units="m/s", location="node", mesh="delaunay", Conventions="UGRID-1.0"
),
)
p = ux.UxDataArray(
data=P,
name="p",
uxgrid=uxgrid,
dims=["time", "depth", "n_node"],
coords=dict(
time=(["time"], TIME),
depth=(["depth"], zc),
),
attrs=dict(description="pressure", units="N/m^2", location="node", mesh="delaunay", Conventions="UGRID-1.0"),
)
return ux.UxDataset({"U": u, "V": v, "W": w, "p": p}, uxgrid=uxgrid)
def _ux_constant_flow_face_centered_2D():
NX = 10
NT = 2
lon, lat = np.meshgrid(
np.linspace(0, 20, NX, dtype=np.float64),
np.linspace(0, 20, NX, dtype=np.float64),
)
lon_flat, lat_flat = lon.ravel(), lat.ravel()
mask = np.isclose(lon_flat, 0) | np.isclose(lon_flat, 20) | np.isclose(lat_flat, 0) | np.isclose(lat_flat, 20)
uxgrid = ux.Grid.from_points(
(lon_flat, lat_flat),
method="regional_delaunay",
boundary_points=np.flatnonzero(mask),
)
uxgrid.attrs["Conventions"] = "UGRID-1.0"
# --- Uniform velocity field on face centers ---
U0 = 0.001 # degrees/s
V0 = 0.0
TIME = xr.date_range("2000-01-01", periods=NT, freq="1h")
zf = np.array([0.0, 1.0])
zc = np.array([0.5])
U = np.full((NT, 1, uxgrid.n_face), U0)
V = np.full((NT, 1, uxgrid.n_face), V0)
W = np.zeros((NT, 2, uxgrid.n_node))
ds = ux.UxDataset(
{
"U": ux.UxDataArray(
U,
uxgrid=uxgrid,
dims=["time", "zc", "n_face"],
coords=dict(time=(["time"], TIME), zc=(["zc"], zc)),
attrs=dict(location="face", mesh="delaunay", Conventions="UGRID-1.0"),
),
"V": ux.UxDataArray(
V,
uxgrid=uxgrid,
dims=["time", "zc", "n_face"],
coords=dict(time=(["time"], TIME), zc=(["zc"], zc)),
attrs=dict(location="face", mesh="delaunay", Conventions="UGRID-1.0"),
),
"W": ux.UxDataArray(
W,
uxgrid=uxgrid,
dims=["time", "zf", "n_node"],
coords=dict(time=(["time"], TIME), nz=(["zf"], zf)),
attrs=dict(location="node", mesh="delaunay", Conventions="UGRID-1.0"),
),
},
uxgrid=uxgrid,
)
return ds
datasets = {
"stommel_gyre_delaunay": _stommel_gyre_delaunay(),
"fesom2_square_delaunay_uniform_z_coordinate": _fesom2_square_delaunay_uniform_z_coordinate(),
"fesom2_square_delaunay_antimeridian": _fesom2_square_delaunay_antimeridian(),
"icon_square_delaunay_uniform_z_coordinate": _icon_square_delaunay_uniform_z_coordinate(),
"ux_constant_flow_face_centered_2D": _ux_constant_flow_face_centered_2D(),
}