Skip to content

Commit 3c9afd4

Browse files
authored
Improve and test error handling for vertical level specification in grid construction (#4461)
* Elevate too many interfaces from warning to error * Test that too few/many interfaces raises error for RectilinearGrid * Test that too few/many interfaces raises error for LatitudeLongitudeGrid * Validate number of z-faces for tripolar grid * Test that too few/many interfaces raises error for TripolarGrid
1 parent 779b2e3 commit 3c9afd4

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/Grids/input_validation.jl

+2-4
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,13 @@ function validate_dimension_specification(T, ξ::AbstractVector, dir, N, FT)
135135

136136
ξ[end] ξ[1] || throw(ArgumentError("$dir= should have increasing values."))
137137

138-
# Validate the length of ξ: error is ξ is too short, warn if ξ is too long.
138+
# Validate the length of ξ: error is ξ is too short, error if ξ is too long.
139139
= length(ξ)
140140
N⁺¹ = N + 1
141141
if< N⁺¹
142142
throw(ArgumentError("length($dir) = $Nξ has too few interfaces for the dimension size $(N)!"))
143143
elseif> N⁺¹
144-
msg = "length($dir) = $Nξ is greater than $N+1, where $N was passed to `size`.\n" *
145-
"$dir cell interfaces will be constructed from $dir[1:$N⁺¹]."
146-
@warn msg
144+
throw(ArgumentError("length($dir) = $Nξ has too many interfaces for the dimension size $(N)!"))
147145
end
148146

149147
return ξ

src/OrthogonalSphericalShellGrids/tripolar_grid.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Oceananigans.BoundaryConditions: ZipperBoundaryCondition
22
using Oceananigans.Grids: architecture, cpu_face_constructor_z
33

4-
import Oceananigans.Grids: with_halo
4+
import Oceananigans.Grids: with_halo, validate_dimension_specification
55

66
"""
77
struct Tripolar{N, F, S}
@@ -92,6 +92,8 @@ function TripolarGrid(arch = CPU(), FT::DataType = Float64;
9292
# but for the φ coordinate we need to remove one point at the north
9393
# because the the north pole is a `Center`point, not on `Face` point...
9494
topology = (Periodic, RightConnected, Bounded)
95+
TZ = topology[3]
96+
z = validate_dimension_specification(TZ, z, :z, Nz, FT)
9597

9698
Lx, λᶠᵃᵃ, λᶜᵃᵃ, Δλᶠᵃᵃ, Δλᶜᵃᵃ = generate_coordinate(FT, topology, size, halo, longitude, :longitude, 1, CPU())
9799
Lz, z = generate_coordinate(FT, topology, size, halo, z, :z, 3, CPU())
@@ -391,4 +393,4 @@ function with_halo(new_halo, old_grid::TripolarGrid)
391393
southernmost_latitude)
392394

393395
return new_grid
394-
end
396+
end

test/test_grids.jl

+10
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ function test_regular_rectilinear_constructor_errors(FT)
281281
@test_throws ArgumentError RectilinearGrid(CPU(), FT, topology=(Flat, Flat, Periodic), size=(16, 16), extent=1)
282282

283283
@test_throws ArgumentError RectilinearGrid(CPU(), FT, topology=(Flat, Flat, Flat), size=16, extent=1)
284+
285+
@test_throws ArgumentError RectilinearGrid(CPU(), FT, size=(4, 4, 4), x=(0, 1), y=(0, 1), z=[-50.0, -30.0, -20.0, 0.0]) # too few z-faces
286+
@test_throws ArgumentError RectilinearGrid(CPU(), FT, size=(4, 4, 4), x=(0, 1), y=(0, 1), z=[-2000.0, -1000.0, -50.0, -30.0, -20.0, 0.0]) # too many z-faces
284287

285288
return nothing
286289
end
@@ -912,6 +915,13 @@ end
912915
end
913916

914917
@testset "Latitude-longitude grid" begin
918+
@info " Testing latitude-longitude grid construction errors..."
919+
920+
for FT in float_types
921+
@test_throws ArgumentError LatitudeLongitudeGrid(CPU(), FT, size=(10, 10, 4), longitude=(-180, 180), latitude=(-80, 80), z=[-50.0, -30.0, -20.0, 0.0]) # too few z-faces
922+
@test_throws ArgumentError LatitudeLongitudeGrid(CPU(), FT, size=(10, 10, 4), longitude=(-180, 180), latitude=(-80, 80), z=[-2000.0, -1000.0, -50.0, -30.0, -20.0, 0.0]) # too many z-faces
923+
end
924+
915925
@info " Testing general latitude-longitude grid..."
916926

917927
for FT in float_types

test/test_tripolar_grid.jl

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ end
106106
end
107107
end
108108

109+
@testset "Grid construction error tests..." begin
110+
for FT in float_types
111+
@test_throws ArgumentError TripolarGrid(CPU(), FT, size=(10, 10, 4), z=[-50.0, -30.0, -20.0, 0.0]) # too few z-faces
112+
@test_throws ArgumentError TripolarGrid(CPU(), FT, size=(10, 10, 4), z=[-2000.0, -1000.0, -50.0, -30.0, -20.0, 0.0]) # too many z-faces
113+
end
114+
end
115+
109116
@testset "Orthogonality of family of ellipses and hyperbolae..." begin
110117
for arch in archs
111118
# Test the orthogonality of a tripolar grid based on the orthogonality of a

0 commit comments

Comments
 (0)