Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit c24e079

Browse files
committed
Add function to find cube sphere reference cell
This add a function that given point it finds the associated reference cell indices and reference coordinates for that point.
1 parent a88d309 commit c24e079

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/gridgenerators.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,64 @@ function cubedspheregrid(referencecell::LobattoCell,
439439

440440
return NodalGrid(cubespherewarp, referencecell, vertices, connectivity, :cubedsphere; stacksize=stacksize)
441441
end
442+
443+
function cubedspherereference(x, vert_coord, ncells_h_panel)
444+
# Find the radial index
445+
ρ = norm(x)
446+
k = clamp(sum.> vert_coord), 1, length(vert_coord) - 1)
447+
= (2ρ - vert_coord[k] - vert_coord[k+1]) / (vert_coord[k+1] - vert_coord[k])
448+
449+
# Find the indices on the faces
450+
T = eltype(x)
451+
r = range(T(-1), length=(ncells_h_panel+1), stop=T(1))
452+
s = range(T(1), length=(ncells_h_panel+1), stop=T(-1))
453+
454+
hinv = ncells_h_panel / T(2)
455+
ξ = Bennu.cubesphereunwarp(x) / ρ
456+
457+
# Find which face the point is on
458+
fdim = argmax(abs.(ξ))
459+
nsb = !signbit(ξ[fdim])
460+
f = 2 * fdim - 1 + nsb
461+
462+
sn = sign(ξ[fdim])
463+
if fdim == 1
464+
i = clamp(ceil(Int, (sn*ξ[2]+1)*hinv), 1, ncells_h_panel)
465+
j = clamp(ceil(Int, (ξ[3]+1)*hinv), 1, ncells_h_panel)
466+
467+
ax = nsb ? r[i] : s[i]
468+
bx = nsb ? r[i+1] : s[i+1]
469+
= (2ξ[2] - ax - bx) / (bx - ax)
470+
471+
ay = r[j]
472+
by = r[j+1]
473+
= (2ξ[3] - ay - by) / (by - ay)
474+
elseif fdim == 2
475+
i = clamp(ceil(Int, (-sn*ξ[1]+1)*hinv), 1, ncells_h_panel)
476+
j = clamp(ceil(Int, (ξ[3]+1)*hinv), 1, ncells_h_panel)
477+
478+
ax = nsb ? s[i] : r[i]
479+
bx = nsb ? s[i+1] : r[i+1]
480+
= (2ξ[1] - ax - bx) / (bx - ax)
481+
482+
ay = r[j]
483+
by = r[j+1]
484+
= (2ξ[3] - ay - by) / (by - ay)
485+
elseif fdim == 3
486+
i = clamp(ceil(Int, (ξ[1]+1)*hinv), 1, ncells_h_panel)
487+
j = clamp(ceil(Int, (sn*ξ[2]+1)*hinv), 1, ncells_h_panel)
488+
489+
ax = r[i]
490+
bx = r[i+1]
491+
= (2ξ[1] - ax - bx) / (bx - ax)
492+
493+
ay = nsb ? r[j] : s[j]
494+
by = nsb ? r[j+1] : s[j+1]
495+
= (2ξ[2] - ay - by) / (by - ay)
496+
end
497+
498+
cellcoordinate = (x̂, ŷ, ẑ)
499+
cellindex = (k, i, j, f)
500+
501+
return (cellcoordinate, cellindex)
502+
end

test/gridgenerators.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,25 @@ end
253253
= Bennu.cubespherewarp(r)
254254
@test p
255255
end
256-
end
256+
257+
let
258+
FT = Float64
259+
A = Array
260+
261+
cell = LobattoCell{FT,A}(3, 3, 4)
262+
vert_coord = [one(FT), 2one(FT), 4one(FT), (11//2)*one(FT)]
263+
ncells_h_panel = 3
264+
265+
grid = cubedspheregrid(cell, vert_coord, ncells_h_panel)
266+
267+
x = points(grid)
268+
ξ = Bennu.cubesphereunwarp.(x)
269+
ps = reshape(points(grid), length(cell), size(grid)...)
270+
271+
for p in ps
272+
(x̂, cell_coords) = Bennu.cubedspherereference(p, vert_coord, ncells_h_panel)
273+
V = kron(reverse(spectralinterpolation.(cell.points_1d, x̂))...)
274+
@test first(V * ps[:, cell_coords...]) p
275+
end
276+
end
277+
end

0 commit comments

Comments
 (0)