Skip to content

Commit 210f3dd

Browse files
committed
Refactor meshbuilder and gridbuilder functions to accept AbstractVector instead of AbstractRange, enhancing flexibility and usability
1 parent ecabb5a commit 210f3dd

1 file changed

Lines changed: 36 additions & 36 deletions

File tree

src/utils.jl

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function get_polygon(points::AbstractMatrix; ratio::Real=0.1, allow_holes::Bool=
4545
end
4646

4747
"""
48-
meshbuilder(x::AbstractRange, y::AbstractRange)
48+
meshbuilder(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, step::Real)
4949
5050
Description:
5151
---
@@ -54,13 +54,17 @@ Generate structured mesh in 2D space.
5454
Example:
5555
---
5656
```julia
57-
x = 0:0.1:1
58-
y = 0:0.1:1
59-
mesh = meshbuilder(x, y) # returns a 2xN array of points
57+
x = [0, 1]
58+
y = [0, 1]
59+
mesh = meshbuilder(x, y, 0.1) # returns a 2xN array of points
6060
```
6161
"""
62-
function meshbuilder(x::AbstractRange, y::AbstractRange)::Array{Float64, 2}
62+
function meshbuilder(xr::AbstractVector{<:Real}, yr::AbstractVector{<:Real}, step::Real)::Array{Float64, 2}
63+
x = range(xr[1], xr[end], step=step)
64+
y = range(yr[1], yr[end], step=step)
6365
nx, ny = length(x), length(y)
66+
nx 1 && error("Mesh must have at least 2 points in x-direction")
67+
ny 1 && error("Mesh must have at least 2 points in y-direction")
6468
result = Array{Float64, 2}(undef, nx * ny, 2)
6569
idx = 1
6670
@inbounds for i = 1:nx
@@ -75,7 +79,7 @@ function meshbuilder(x::AbstractRange, y::AbstractRange)::Array{Float64, 2}
7579
end
7680

7781
"""
78-
meshbuilder(x::AbstractRange, y::AbstractRange, z::AbstractRange)
82+
meshbuilder(x::AbstractVector{<:Real}, y::AbstractVector{<:Real}, z::AbstractVector{<:Real}, step::Real)
7983
8084
Description:
8185
---
@@ -84,14 +88,20 @@ Generate structured mesh in 3D space.
8488
Example:
8589
---
8690
```julia
87-
x = 0:0.1:1
88-
y = 0:0.1:1
89-
z = 0:0.1:1
90-
mesh = meshbuilder(x, y, z) # returns a 3xN array of points
91+
x = [0, 1]
92+
y = [0, 1]
93+
z = [0, 1]
94+
mesh = meshbuilder(x, y, z, 0.1) # returns a 3xN array of points
9195
```
9296
"""
93-
function meshbuilder(x::AbstractRange, y::AbstractRange, z::AbstractRange)::Array{Float64, 2}
97+
function meshbuilder(xr::AbstractVector{<:Real}, yr::AbstractVector{<:Real}, zr::AbstractVector{<:Real}, step::Real)::Array{Float64, 2}
98+
x = range(xr[1], xr[end], step=step)
99+
y = range(yr[1], yr[end], step=step)
100+
z = range(zr[1], zr[end], step=step)
94101
nx, ny, nz = length(x), length(y), length(z)
102+
nx 1 && error("Mesh must have at least 2 points in x-direction")
103+
ny 1 && error("Mesh must have at least 2 points in y-direction")
104+
nz 1 && error("Mesh must have at least 2 points in z-direction")
95105
result = Array{Float64, 2}(undef, nx * ny * nz, 3)
96106
idx = 1
97107
@inbounds for k = 1:nz
@@ -109,20 +119,16 @@ function meshbuilder(x::AbstractRange, y::AbstractRange, z::AbstractRange)::Arra
109119
return result
110120
end
111121

112-
function gridbuilder(xr::AbstractRange, yr::AbstractRange)
122+
function gridbuilder(xr::AbstractVector{<:Real}, yr::AbstractVector{<:Real}, step::Real)
113123
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")
124+
x, y, step = convert.(T2, xr), convert.(T2, yr), T2(step)
125+
ξ = meshbuilder(x, y, step)
126+
nx = ξ[:, 1] |> unique |> length
127+
ny = ξ[:, 2] |> unique |> length
117128
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)
129+
ni == size(ξ, 1) || error("Inconsistent grid points generated")
130+
h = step
124131
inv_h = T2(1 / h)
125-
ξ = meshbuilder(xr, yr)
126132
vmin = minimum(ξ, dims=1)
127133
vmax = maximum(ξ, dims=1)
128134
x1 = T2(vmin[1])
@@ -132,23 +138,17 @@ function gridbuilder(xr::AbstractRange, yr::AbstractRange)
132138
return (nx=nx, ny=ny, ni=ni, nc=nc, h=h, x1=x1, x2=x2, y1=y1, y2=y2, inv_h=inv_h, ξ=ξ)
133139
end
134140

135-
function gridbuilder(xr::AbstractRange, yr::AbstractRange, zr::AbstractRange)
141+
function gridbuilder(xr::AbstractVector{<:Real}, yr::AbstractVector{<:Real}, zr::AbstractVector{<:Real}, step::Real)
136142
T2 = Float64
137-
x, y, z = convert.(Float64, xr), convert.(Float64, yr), convert.(Float64, zr)
138-
nx = length(x); nx 1 && error("Grid must have at least 2 points in x-direction")
139-
ny = length(y); ny 1 && error("Grid must have at least 2 points in y-direction")
140-
nz = length(z); nz 1 && error("Grid must have at least 2 points in z-direction")
143+
x, y, z, step = convert.(Float64, xr), convert.(Float64, yr), convert.(Float64, zr), T2(step)
144+
ξ = meshbuilder(x, y, z, step)
145+
nx = ξ[:, 1] |> unique |> length
146+
ny = ξ[:, 2] |> unique |> length
147+
nz = ξ[:, 3] |> unique |> length
141148
ni = nx * ny * nz; nc = (nx - 1) * (ny - 1) * (nz - 1)
142-
_tmp_diff_vec_ = diff(x); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
143-
h1 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in x direction must be equal")
144-
_tmp_diff_vec_ = diff(y); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
145-
h2 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in y direction must be equal")
146-
_tmp_diff_vec_ = diff(z); all_equal = all((_tmp_diff_vec_[1]), _tmp_diff_vec_)
147-
h3 = all_equal ? _tmp_diff_vec_[1] : error("grid spacing in z direction must be equal")
148-
h1 h2 h3 || error("Grid spacing in x, y and z directions must be equal")
149-
h = T2(h1)
149+
ni == size(ξ, 1) || error("Inconsistent grid points generated")
150+
h = step
150151
inv_h = T2(1 / h)
151-
ξ = meshbuilder(xr, yr, zr)
152152
vmin = minimum(ξ, dims=1)
153153
vmax = maximum(ξ, dims=1)
154154
x1 = T2(vmin[1])

0 commit comments

Comments
 (0)