Skip to content

Commit e5b5756

Browse files
authored
Update 'RegularGrid' implementation (#1053)
1 parent b362495 commit e5b5756

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

src/domains/meshes/rectilineargrid.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function RectilinearGrid{M,C}(xyz::NTuple{N,AbstractVector}, topology::GridTopol
4343
end
4444

4545
xyz′ = ntuple(i -> numconvert.(T, withunit.(xyz[i], us[i])), nc)
46+
4647
RectilinearGrid{M,C,N,typeof(xyz′)}(xyz′, topology)
4748
end
4849

src/domains/meshes/regulargrid.jl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ RegularGrid((10, 20, 30), Point(Cylindrical(0.0, 0.0, 0.0)), (3.0, 2.0, 1.0))
2424
2525
See also [`CartesianGrid`](@ref).
2626
"""
27-
struct RegularGrid{M<:Manifold,C<:CRS,S<:Tuple,N} <: Grid{M,C,N}
27+
struct RegularGrid{M<:Manifold,C<:CRS,N,S<:NTuple{N,Quantity}} <: Grid{M,C,N}
2828
origin::Point{M,C}
2929
spacing::S
3030
offset::Dims{N}
3131
topology::GridTopology{N}
3232

33-
function RegularGrid{M,C,S,N}(origin, spacing, offset, topology) where {M<:Manifold,C<:CRS,S<:Tuple,N}
33+
function RegularGrid{M,C,N,S}(origin, spacing, offset, topology) where {M<:Manifold,C<:CRS,N,S<:NTuple{N,Quantity}}
3434
if !all(s -> s > zero(s), spacing)
3535
throw(ArgumentError("spacing must be positive"))
3636
end
@@ -40,18 +40,17 @@ end
4040

4141
function RegularGrid(
4242
origin::Point{M,C},
43-
spacing::Tuple,
43+
spacing::NTuple{N,Number},
4444
offset::Dims{N},
4545
topology::GridTopology{N}
4646
) where {M<:Manifold,C<:CRS,N}
47-
if manifold(origin) <: 🌐 && !(crs(origin) <: LatLon)
47+
if M <: 🌐 && !(C <: LatLon)
4848
throw(ArgumentError("regular spacing on `🌐` requires `LatLon` coordinates"))
4949
end
5050

5151
T = CoordRefSystems.mactype(C)
5252
nc = CoordRefSystems.ncoords(C)
5353
us = CoordRefSystems.units(C)
54-
ns = length(spacing)
5554

5655
if N nc
5756
throw(ArgumentError("""
@@ -60,19 +59,17 @@ function RegularGrid(
6059
"""))
6160
end
6261

63-
if ns nc
64-
throw(ArgumentError("""
65-
A $N-dimensional regular grid requires $N spacing values.
66-
The provided spacing has $ns values.
67-
"""))
68-
end
69-
7062
sp = ntuple(i -> numconvert(T, withunit(spacing[i], us[i])), nc)
7163

72-
RegularGrid{M,C,typeof(sp),N}(origin, sp, offset, topology)
64+
RegularGrid{M,C,N,typeof(sp)}(origin, sp, offset, topology)
7365
end
7466

75-
function RegularGrid(dims::Dims{N}, origin::Point, spacing::Tuple, offset::Dims{N}=ntuple(i -> 1, N)) where {N}
67+
function RegularGrid(
68+
dims::Dims{N},
69+
origin::Point,
70+
spacing::NTuple{N,Number},
71+
offset::Dims{N}=ntuple(i -> 1, N)
72+
) where {N}
7673
if !all(>(0), dims)
7774
throw(ArgumentError("dimensions must be positive"))
7875
end
@@ -90,7 +87,7 @@ function vertex(g::RegularGrid, ijk::Dims)
9087
Point(ctor(vals...))
9188
end
9289

93-
@generated function xyz(g::RegularGrid{M,C,S,N}) where {M,C,S,N}
90+
@generated function xyz(g::RegularGrid{M,C,N}) where {M,C,N}
9491
exprs = ntuple(N) do i
9592
:(range(start=orig[$i], step=spac[$i], length=(dims[$i] + 1)))
9693
end

src/domains/meshes/structuredgrid.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function StructuredGrid{M,C}(XYZ::NTuple{N,AbstractArray}, topology::GridTopolog
4343
end
4444

4545
XYZ′ = ntuple(i -> numconvert.(T, withunit.(XYZ[i], us[i])), nc)
46+
4647
StructuredGrid{M,C,N,typeof(XYZ′)}(XYZ′, topology)
4748
end
4849

test/meshes.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@
114114
p = latlon(0, 0) |> Proj(Cartesian)
115115
@test_throws ArgumentError RegularGrid((10, 10), p, T.((1, 1)))
116116
# error: the number of dimensions must be equal to the number of coordinates
117-
@test_throws ArgumentError RegularGrid((10, 10, 10), latlon(0, 0), T.((1, 1)))
118-
# error: the number of spacings must be equal to the number of coordinates
119-
@test_throws ArgumentError RegularGrid((10, 10), latlon(0, 0), T.((1, 1, 1)))
117+
@test_throws ArgumentError RegularGrid((10, 10, 10), latlon(0, 0), T.((1, 1, 1)))
120118

121119
grid = RegularGrid((10, 10), latlon(0, 0), T.((1, 1)))
122120
if T == Float32

0 commit comments

Comments
 (0)