-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinterpolate_array.jl
367 lines (305 loc) · 10 KB
/
interpolate_array.jl
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
"""
interpolate_slab!(output_array, field, slab_indices, weights)
Interpolate horizontal field on the given `slab_indices` using the given interpolation
`weights`.
`interpolate_slab!` interpolates several values at a fixed `z` coordinate. For this reason,
it requires several slab indices and weights.
"""
interpolate_slab!(output_array, field::Fields.Field, slab_indices, weights) =
interpolate_slab!(
output_array,
field::Fields.Field,
slab_indices,
weights,
ClimaComms.device(field),
)
# CPU kernel for 3D configurations
function interpolate_slab!(
output_array,
field::Fields.Field,
slab_indices,
weights::AbstractArray{Tuple{A, A}},
device::ClimaComms.AbstractCPUDevice,
) where {A}
space = axes(field)
FT = Spaces.undertype(space)
@inbounds for index in 1:length(output_array)
(I1, I2) = weights[index]
Nq1, Nq2 = length(I1), length(I2)
output_array[index] = zero(FT)
for j in 1:Nq2, i in 1:Nq1
ij = CartesianIndex((i, j))
output_array[index] +=
I1[i] *
I2[j] *
Operators.get_node(space, field, ij, slab_indices[index])
end
end
end
# CPU kernel for 2D configurations
function interpolate_slab!(
output_array,
field::Fields.Field,
slab_indices,
weights::AbstractArray{Tuple{A}},
device::ClimaComms.AbstractCPUDevice,
) where {A}
space = axes(field)
FT = Spaces.undertype(space)
@inbounds for index in 1:length(output_array)
(I1,) = weights[index]
Nq = length(I1)
output_array[index] = zero(FT)
for i in 1:Nq
ij = CartesianIndex((i,))
output_array[index] +=
I1[i] *
Operators.get_node(space, field, ij, slab_indices[index])
end
end
end
"""
vertical_indices_ref_coordinate(space, zcoord)
Return the vertical indices of the elements below and above `zcoord`.
Return also the correct reference coordinate `zcoord` for vertical interpolation.
"""
function vertical_indices_ref_coordinate end
function vertical_indices_ref_coordinate(
space::Spaces.FaceExtrudedFiniteDifferenceSpace,
zcoord,
)
vert_topology = Spaces.vertical_topology(space)
vert_mesh = vert_topology.mesh
velem = Meshes.containing_element(vert_mesh, zcoord)
ξ3, = Meshes.reference_coordinates(vert_mesh, velem, zcoord)
v_lo, v_hi = velem - half, velem + half
return v_lo, v_hi, ξ3
end
function vertical_indices_ref_coordinate(
space::Spaces.CenterExtrudedFiniteDifferenceSpace,
zcoord,
)
vert_topology = Spaces.vertical_topology(space)
vert_mesh = vert_topology.mesh
Nz = Spaces.nlevels(space)
velem = Meshes.containing_element(vert_mesh, zcoord)
ξ3, = Meshes.reference_coordinates(vert_mesh, velem, zcoord)
if ξ3 < 0
if Topologies.isperiodic(Spaces.vertical_topology(space))
v_lo = mod1(velem - 1, Nz)
else
v_lo = max(velem - 1, 1)
end
v_hi = velem
ξ3 = ξ3 + 1
else
v_lo = velem
if Topologies.isperiodic(Spaces.vertical_topology(space))
v_hi = mod1(velem + 1, Nz)
else
v_hi = min(velem + 1, Nz)
end
ξ3 = ξ3 - 1
end
return v_lo, v_hi, ξ3
end
"""
interpolate_slab_level(
field::Fields.Field,
h::Integer,
Is::Tuple,
zpts;
fill_value = eltype(field)(NaN)
)
Vertically interpolate the given `field` on `zpts`.
`interpolate_slab_level!` interpolates several values at a fixed horizontal coordinate.
The field is linearly interpolated across two neighboring vertical elements.
For centered-valued fields, if `zcoord` is in the top (bottom) half of a top (bottom)
element in a column, no interpolation is performed and the value at the cell center is
returned. Effectively, this means that the interpolation is first-order accurate across the
column, but zeroth-order accurate close to the boundaries.
Return `fill_value` when the vertical coordinate is negative.
"""
function interpolate_slab_level!(
output_array,
field::Fields.Field,
h::Integer,
Is::Tuple,
vertical_indices_ref_coordinates,
)
device = ClimaComms.device(field)
interpolate_slab_level!(
output_array,
field,
vertical_indices_ref_coordinates,
h,
Is,
device,
)
end
# CPU kernel for 3D configurations
function interpolate_slab_level!(
output_array,
field::Fields.Field,
vidx_ref_coordinates,
h::Integer,
(I1, I2)::Tuple{<:AbstractArray, <:AbstractArray},
device::ClimaComms.AbstractCPUDevice,
)
space = axes(field)
FT = Spaces.undertype(space)
Nq1, Nq2 = length(I1), length(I2)
@inbounds for index in 1:length(vidx_ref_coordinates)
v_lo, v_hi, ξ3 = vidx_ref_coordinates[index]
f_lo = zero(FT)
f_hi = zero(FT)
for j in 1:Nq2, i in 1:Nq1
ij = CartesianIndex((i, j))
f_lo +=
I1[i] *
I2[j] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_lo, h))
f_hi +=
I1[i] *
I2[j] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_hi, h))
end
output_array[index] = ((1 - ξ3) * f_lo + (1 + ξ3) * f_hi) / 2
end
end
# CPU kernel for 2D configurations
function interpolate_slab_level!(
output_array,
field::Fields.Field,
vidx_ref_coordinates,
h::Integer,
(I1,)::Tuple{<:AbstractArray},
device::ClimaComms.AbstractCPUDevice,
)
space = axes(field)
FT = Spaces.undertype(space)
Nq = length(I1)
@inbounds for index in 1:length(vidx_ref_coordinates)
v_lo, v_hi, ξ3 = vidx_ref_coordinates[index]
f_lo = zero(FT)
f_hi = zero(FT)
for i in 1:Nq
ij = CartesianIndex((i,))
f_lo +=
I1[i] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_lo, h))
f_hi +=
I1[i] *
Operators.get_node(space, field, ij, Fields.SlabIndex(v_hi, h))
end
output_array[index] = ((1 - ξ3) * f_lo + (1 + ξ3) * f_hi) / 2
end
end
"""
interpolate_array(field, xpts, ypts)
interpolate_array(field, xpts, ypts, zpts)
Interpolate a field to a regular array using pointwise interpolation.
This is primarily used for plotting and diagnostics.
# Examples
```julia
longpts = range(Geometry.LongPoint(-180.0), Geometry.LongPoint(180.0), length = 21)
latpts = range(Geometry.LatPoint(-80.0), Geometry.LatPoint(80.0), length = 21)
zpts = range(Geometry.ZPoint(0.0), Geometry.ZPoint(1000.0), length = 21)
interpolate_array(field, longpts, latpts, zpts)
```
!!! note
Hypsography is not currently handled correctly.
"""
function interpolate_array end
function interpolate_array(
field::Fields.ExtrudedFiniteDifferenceField,
xpts,
zpts,
)
space = axes(field)
@assert ClimaComms.context(space) isa ClimaComms.SingletonCommsContext
horz_topology = Spaces.topology(space)
horz_mesh = horz_topology.mesh
T = eltype(field)
array = zeros(T, length(xpts), length(zpts))
FT = Spaces.undertype(space)
vertical_indices_ref_coordinates =
[vertical_indices_ref_coordinate(space, zcoord) for zcoord in zpts]
@inbounds for (ix, xcoord) in enumerate(xpts)
hcoord = xcoord
helem = Meshes.containing_element(horz_mesh, hcoord)
quad = Spaces.quadrature_style(space)
quad_points, _ = Quadratures.quadrature_points(FT, quad)
weights = interpolation_weights(horz_mesh, hcoord, quad_points)
h = helem
interpolate_slab_level!(
view(array, ix, :),
field,
h,
weights,
vertical_indices_ref_coordinates,
)
end
return array
end
function interpolate_array(
field::Fields.ExtrudedFiniteDifferenceField,
xpts,
ypts,
zpts,
)
space = axes(field)
@assert ClimaComms.context(space) isa ClimaComms.SingletonCommsContext
horz_topology = Spaces.topology(space)
horz_mesh = horz_topology.mesh
T = eltype(field)
array = zeros(T, length(xpts), length(ypts), length(zpts))
FT = Spaces.undertype(space)
vertical_indices_ref_coordinates =
[vertical_indices_ref_coordinate(space, zcoord) for zcoord in zpts]
@inbounds for (iy, ycoord) in enumerate(ypts),
(ix, xcoord) in enumerate(xpts)
hcoord = Geometry.product_coordinates(xcoord, ycoord)
helem = Meshes.containing_element(horz_mesh, hcoord)
quad = Spaces.quadrature_style(space)
quad_points, _ = Quadratures.quadrature_points(FT, quad)
weights = interpolation_weights(horz_mesh, hcoord, quad_points)
gidx = horz_topology.orderindex[helem]
h = gidx
interpolate_slab_level!(
view(array, ix, iy, :),
field,
h,
weights,
vertical_indices_ref_coordinates,
)
end
return array
end
"""
interpolation_weights(horz_mesh, hcoord, quad_points)
Return the weights (tuple of arrays) to interpolate fields onto `hcoord` on the
given mesh and quadrature points.
"""
function interpolation_weights end
function interpolation_weights(
horz_mesh::Meshes.AbstractMesh2D,
hcoord,
quad_points,
)
helem = Meshes.containing_element(horz_mesh, hcoord)
ξ1, ξ2 = Meshes.reference_coordinates(horz_mesh, helem, hcoord)
WI1 = Quadratures.interpolation_matrix(SVector(ξ1), quad_points)
WI2 = Quadratures.interpolation_matrix(SVector(ξ2), quad_points)
return (WI1, WI2)
end
function interpolation_weights(
horz_mesh::Meshes.AbstractMesh1D,
hcoord,
quad_points,
)
helem = Meshes.containing_element(horz_mesh, hcoord)
ξ1, = Meshes.reference_coordinates(horz_mesh, helem, hcoord)
WI1 = Quadratures.interpolation_matrix(SVector(ξ1), quad_points)
return (WI1,)
end