Skip to content

Commit d909389

Browse files
committed
🧔🏽‍♂️ Refactor gridbuilder functions to improve type handling and error checking for grid dimensions.
1 parent 3cfb136 commit d909389

1 file changed

Lines changed: 38 additions & 18 deletions

File tree

src/utils.jl

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,52 @@ function meshbuilder(x::AbstractRange, y::AbstractRange, z::AbstractRange)::Arra
109109
return result
110110
end
111111

112-
function gridbuilder(xr::AbstractRange, yr::AbstractRange, zr::AbstractRange; ϵ::Symbol=:double)
113-
T1 = ϵ == :single ? Int32 : Int64
114-
T2 = ϵ == :single ? Float32 : Float64
112+
function gridbuilder(xr::AbstractRange, yr::AbstractRange)
113+
T2 = Float64
114+
x, y = convert.(T2, xr), convert.(T2, yr)
115+
nx = length(x); nx 1 && error("Grid must have at least 2 points in x-direction")
116+
ny = length(y); ny 1 && error("Grid must have at least 2 points in y-direction")
117+
ni = nx * ny; nc = (nx - 1) * (ny - 1)
118+
_tmp_diff_vec_ = diff(x); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
119+
h1 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in x direction must be equal")
120+
_tmp_diff_vec_ = diff(y); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
121+
h2 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in y direction must be equal")
122+
h1 h2 || error("Grid spacing in x and y directions must be equal")
123+
h = T2(h1)
124+
inv_h = T2(1 / h)
125+
x1 = T2(x[1])
126+
x2 = T2(x[end])
127+
y1 = T2(y[1])
128+
y2 = T2(y[end])
129+
ξ = meshbuilder(x1:h:x2, y1:h:y2)
130+
return (nx=nx, ny=ny, ni=ni, nc=nc, h=h, x1=x[1], x2=x[end], y1=y[1], y2=y[end], inv_h=inv_h, ξ=ξ)
131+
end
132+
133+
function gridbuilder(xr::AbstractRange, yr::AbstractRange, zr::AbstractRange)
134+
T2 = Float64
115135
x, y, z = convert.(Float64, xr), convert.(Float64, yr), convert.(Float64, zr)
116-
nx = T1(length(x)); nx 1 && error("Grid must have at least 2 points in x-direction")
117-
ny = T1(length(y)); ny 1 && error("Grid must have at least 2 points in y-direction")
118-
nz = T1(length(z)); nz 1 && error("Grid must have at least 2 points in z-direction")
119-
ni = T1(nx * ny * nz)
136+
nx = length(x); nx 1 && error("Grid must have at least 2 points in x-direction")
137+
ny = length(y); ny 1 && error("Grid must have at least 2 points in y-direction")
138+
nz = length(z); nz 1 && error("Grid must have at least 2 points in z-direction")
139+
ni = nx * ny * nz; nc = (nx - 1) * (ny - 1) * (nz - 1)
120140
_tmp_diff_vec_ = diff(x); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
121141
h1 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in x direction must be equal")
122142
_tmp_diff_vec_ = diff(y); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
123143
h2 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in y direction must be equal")
124144
_tmp_diff_vec_ = diff(z); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
125145
h3 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in z direction must be equal")
126146
h1 h2 h3 || error("Grid spacing in x, y and z directions must be equal")
127-
h = T2(h1)
128-
invh = T2(1 / h)
129-
x1 = T2(x[1])
130-
x2 = T2(x[end])
131-
y1 = T2(y[1])
132-
y2 = T2(y[end])
133-
z1 = T2(z[1])
134-
z2 = T2(z[end])
135-
ξ = meshbuilder(x1:h:x2, y1:h:y2, z1:h:z2)
136-
return (x1=x1, x2=x2, y1=y1, y2=y2, z1=z1, z2=z2, h=h, invh=invh, ni=ni, nx=nx, ny=ny,
137-
nz=nz, ξ=ξ)
147+
h = T2(h1)
148+
inv_h = T2(1 / h)
149+
x1 = T2(x[1])
150+
x2 = T2(x[end])
151+
y1 = T2(y[1])
152+
y2 = T2(y[end])
153+
z1 = T2(z[1])
154+
z2 = T2(z[end])
155+
ξ = meshbuilder(x1:h:x2, y1:h:y2, z1:h:z2)
156+
return (nx=nx, ny=ny, nz=nz, ni=ni, nc=nc, h=h, x1=x1, x2=x2, y1=y1, y2=y2, z1=z1,
157+
z2=z2, inv_h=inv_h, ξ=ξ)
138158
end
139159

140160
"""

0 commit comments

Comments
 (0)