Skip to content

Commit 476c5a9

Browse files
committed
Fix field2array error msg, level func bounds check
Bump UnrolledUtilities compat Fix inconsistent tensor indexing behavior Fix field2array error message Add tests for level function on Fields Also add `issubspace` for `FiniteDifferenceSpaces` The test currently fails because an error is not thrown for face spaces when out of bounds indexing. remove issubspace Remove `@inbounds` from `level` function `level` on a field on cell faces does not throw errors when attempting to access a level outside the space. This fixes that, but there may be a performance penalty. Simplify AxisTensor idxin function The previous idxin method for a 1d axis tensor behaved different that non singlular dimension axistensors. This commit unifies all the different methods by using unrolled_findfirst Bump UnrolledUtilities compat Simplify level bounds tests Bump UnrolledUtilities again
1 parent 2bc09fb commit 476c5a9

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Statistics = "1"
7878
StatsBase = "0.34"
7979
TerminalLoggers = "0.1"
8080
Test = "1"
81-
UnrolledUtilities = "0.1.6"
81+
UnrolledUtilities = "0.1.9"
8282
julia = "1.10"
8383

8484
[extras]

src/Fields/Fields.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ Base.@propagate_inbounds function level(
517517
v::PlusHalf,
518518
)
519519
hspace = level(axes(field), v)
520-
@inbounds data = level(field_values(field), v.i + 1)
520+
data = level(field_values(field), v.i + 1)
521521
Field(data, hspace)
522522
end
523523

@@ -664,7 +664,7 @@ element type of the array is the same as the struct type of `field`.
664664
"""
665665
function field2array(field::Field)
666666
if sizeof(eltype(field)) != sizeof(eltype(parent(field)))
667-
f_axis_size = sizeof(eltype(parent(field))) ÷ sizeof(eltype(field))
667+
f_axis_size = sizeof(eltype(field)) ÷ sizeof(eltype(parent(field)))
668668
error("unable to use field2array because each Field element is \
669669
represented by $f_axis_size array elements (must be 1)")
670670
end

src/Geometry/Geometry.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Geometry
22

33
using ..RecursiveApply
44
import LinearAlgebra
5+
import UnrolledUtilities: unrolled_findfirst
56

67
using StaticArrays
78

src/Geometry/axistensors.jl

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,8 @@ coordinate_axis(::Type{<:LatLongPoint}) = (1, 2)
6161

6262
coordinate_axis(coord::AbstractPoint) = coordinate_axis(typeof(coord))
6363

64-
@inline idxin(I::Tuple{Int}, i::Int) = 1
65-
66-
@inline function idxin(I::Tuple{Int, Int}, i::Int)
67-
@inbounds begin
68-
if I[1] == i
69-
return 1
70-
elseif I[2] == i
71-
return 2
72-
else
73-
return nothing
74-
end
75-
end
76-
end
77-
78-
@inline function idxin(I::Tuple{Int, Int, Int}, i::Int)
79-
@inbounds begin
80-
if I[1] == i
81-
return 1
82-
elseif I[2] == i
83-
return 2
84-
elseif I[3] == i
85-
return 3
86-
else
87-
return nothing
88-
end
89-
end
90-
end
64+
@inline idxin(I::NTuple{N, Int}, i::Int) where {N} =
65+
unrolled_findfirst(isequal(i), I)
9166

9267
struct PropertyError <: Exception
9368
ax::Any

test/Fields/unit_field.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,43 @@ end
11961196
bf_new = @. bf # test copy()
11971197
end
11981198

1199+
@testset "Level function boundschecking" begin
1200+
FT = Float32
1201+
extruded_center_space = ExtrudedCubedSphereSpace(
1202+
FT;
1203+
z_elem = 10,
1204+
z_min = 0,
1205+
z_max = 1,
1206+
radius = 10,
1207+
h_elem = 10,
1208+
n_quad_points = 4,
1209+
staggering = Grids.CellCenter(),
1210+
)
1211+
fd_center_space = ColumnSpace(
1212+
FT;
1213+
z_elem = 10,
1214+
z_min = 0,
1215+
z_max = 10,
1216+
staggering = CellCenter(),
1217+
)
1218+
for center_space in (extruded_center_space, fd_center_space)
1219+
face_space = Spaces.face_space(center_space)
1220+
center_field = fill(FT(1), center_space)
1221+
face_field = fill(FT(2), face_space)
1222+
if Base.JLOptions().check_bounds == 1
1223+
for level in (-1, 11, 0)
1224+
@test_throws BoundsError Fields.level(center_field, level)
1225+
level != 0 && @test_throws BoundsError Fields.level(
1226+
face_field,
1227+
PlusHalf(level),
1228+
)
1229+
end
1230+
else
1231+
@warn "Bounds check on level(::Field) not verified."
1232+
end
1233+
end
1234+
end
1235+
11991236
include("unit_field_multi_broadcast_fusion.jl")
12001237

12011238
nothing

0 commit comments

Comments
 (0)