@@ -439,3 +439,64 @@ function cubedspheregrid(referencecell::LobattoCell,
439439
440440 return NodalGrid (cubespherewarp, referencecell, vertices, connectivity, :cubedsphere ; stacksize= stacksize)
441441end
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+ x̂ = (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+ x̂ = (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+ x̂ = (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
0 commit comments