Skip to content

Commit

Permalink
Merge pull request #23 from fgasdia/domainfix
Browse files Browse the repository at this point in the history
Fix rectangulardomain
  • Loading branch information
fgasdia authored Dec 23, 2020
2 parents e684692 + 7234c9d commit 9096822
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
65 changes: 30 additions & 35 deletions src/coordinate_domains.jl
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
"""
rectangulardomain(Zb, Ze, Δr)
rectangulardomain(zbl, ztr, Δr)
Generate initial mesh node coordinates for a rectangular domain ∈ {`Zb`, `Ze`} with
initial mesh step `Δr`.
`Ze` should be greater than `Zb`.
Generate initial mesh node coordinates for a rectangular domain extending from the complex
bottom left corner `zbl` to the top right corner `ztr` with initial mesh step `Δr`.
"""
function rectangulardomain(Zb::Complex, Ze::Complex, Δr)
rZb, iZb = reim(Zb)
rZe, iZe = reim(Ze)
function rectangulardomain(zbl::Complex, ztr::Complex, Δr)
rzbl, izbl = reim(zbl)
rztr, iztr = reim(ztr)

rZb > rZe && throw(ArgumentError("real part of Zb greater than Ze"))
iZb > iZe && throw(ArgumentError("imag part of Zb greater than Ze"))
X = rztr - rzbl
Y = iztr - izbl

X = rZe - rZb
Y = iZe - iZb
n = ceil(Int, Y/Δr)
dy = Y/n

n = ceil(Int, Y/Δr + 1)
dy = Y/(n-1)
half_dy = dy/2
## dx = sqrt(Δr² - (dy/2)²), solved for equilateral triangle
m = ceil(Int, X/sqrt(Δr^2 - dy^2/4))
dx = X/m
half_dx = dx/2

m = ceil(Int, X/sqrt(Δr^2 - dy^2/4) + 1)
dx = X/(m-1)
T = promote_type(ComplexF64, typeof(zbl), typeof(ztr), typeof(Δr))
v = Vector{T}()
sizehint!(v, (m+1)*(n+1))

# precalculate
mn = m*n
shift = false # we will displace every other line by dx/2
for j = 0:n
y = izbl + dy*j

vlength = mn
T = promote_type(typeof(Zb), typeof(Ze), typeof(Δr), Float64)
v = Vector{T}()
sizehint!(v, vlength)
for i = 0:m
x = rzbl + dx*i

on = false
@inbounds for j = 0:m-1, i = 0:n-1
x = rZb + dx*j
y = iZb + dy*i
if shift && i == 0
continue # otherwise, we shift out of left bound
elseif shift
x -= half_dx
end

if (i+1) == n
on = !on
end
if on
y += half_dy
end
if i == m
shift = !shift
end

if y < iZe || (abs(y - iZe) < sqrt(eps(real(T))))
# Can't just check y <= iZe because of floating point limitations
push!(v, complex(x, y))
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/DefaultFunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ xb = -2 # real part begin
xe = 2 # real part end
yb = -2 # imag part begin
ye = 2 # imag part end
r = 0.2 # initial mesh step
r = 0.1999 # initial mesh step

origcoords = rectangulardomain(complex(xb, yb), complex(xe, ye), r)

Expand Down Expand Up @@ -64,11 +64,11 @@ params = GRPFParams(100, maxnodes, 3, maxnodes-1, 1e-9, false)
@test_logs (:warn,"GRPFParams.maxnodes reached") grpf(defaultfcn, origcoords, PlotData(), params)

# Test with big origcoords
xb = big"-2" # real part begin
xe = big"2" # real part end
yb = big"-2" # imag part begin
ye = big"2" # imag part end
r = big"0.2" # initial mesh step
xb = big(xb) # real part begin
xe = big(xe) # real part end
yb = big(yb) # imag part begin
ye = big(ye) # imag part end
r = big(r) # initial mesh step

origcoords = rectangulardomain(complex(xb, yb), complex(xe, ye), r)

Expand Down

0 comments on commit 9096822

Please sign in to comment.