Skip to content

Commit 7bca257

Browse files
committed
Merge branch 'main' of github.com:j-fu/GridVisualize.jl
2 parents 64618c7 + 4f7a851 commit 7bca257

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/common.jl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ streamlines.
153153
The code is 3D ready.
154154
"""
155155
function vectorsample(grid::ExtendableGrid{Tv, Ti}, v; offset = :default,
156-
spacing = :default, reltol = 1.0e-10) where {Tv, Ti}
156+
spacing = :default, reltol = 1.0e-10, xlimits = (1,-1), ylimits = (1,-1), zlimits = (1,-1)) where {Tv, Ti}
157157
coord = grid[Coordinates]
158158
cn = grid[CellNodes]
159159
ncells::Int = num_cells(grid)
@@ -172,6 +172,15 @@ function vectorsample(grid::ExtendableGrid{Tv, Ti}, v; offset = :default,
172172

173173
# coordinate window
174174
cminmax = extrema(coord; dims = (2,))
175+
if xlimits[1] < xlimits[2]
176+
cminmax[1] = xlimits[:]
177+
end
178+
if ylimits[1] < ylimits[2]
179+
cminmax[2] = ylimits[:]
180+
end
181+
if zlimits[1] < zlimits[2]
182+
cminmax[3] = zlimits[:]
183+
end
175184

176185
if offset == :default
177186
offset = [cminmax[i][1] for i = 1:dim]
@@ -255,8 +264,17 @@ function vectorsample(grid::ExtendableGrid{Tv, Ti}, v; offset = :default,
255264
# If so, obtain P1 interpolated raster data and
256265
# assign them to rasterflux
257266
for I[1] Imin[1]:Imax[1] # 0 alloc
267+
if I[1] <= 0 || I[1] > length(rastercoord[1])
268+
continue
269+
end
258270
for I[2] Imin[2]:Imax[2]
271+
if I[2] <= 0 || I[2] > length(rastercoord[2])
272+
continue
273+
end
259274
for I[3] Imin[3]:Imax[3]
275+
if dim == 3 && (I[3] <= 0 || I[3] > length(rastercoord[3]))
276+
continue
277+
end
260278

261279
# Fill raster point to be tested
262280
for idim = 1:dim
@@ -315,7 +333,7 @@ is scaled by vscale
315333
Result data are meant to be ready for being passed to calls to `quiver`.
316334
317335
"""
318-
function quiverdata(rastercoord, rasterflux; vscale = 1.0, vnormalize = true)
336+
function quiverdata(rastercoord, rasterflux; vscale = 1.0, vnormalize = true, vconstant = false)
319337
dim = length(rastercoord)
320338

321339
imax = length(rastercoord[1])
@@ -355,7 +373,11 @@ function quiverdata(rastercoord, rasterflux; vscale = 1.0, vnormalize = true)
355373
qv = reshape(reinterpret(Float32, qvvalues), (2, length(qvvalues)))
356374

357375
# Normalize vectors to raster point spacing
358-
if vnormalize
376+
if vconstant
377+
for j = 1 : size(qv,2)
378+
view(qv,:,j) ./= norm(view(qv,:,j))
379+
end
380+
elseif vnormalize
359381
@views vmax = maximum(norm, (qv[:, i] for i = 1:length(qvvalues)))
360382
vscale = vscale * min(spacing...) / vmax
361383
end

src/pyplot.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
function initialize!(p, ::Type{PyPlotType})
22
PyPlot = p.context[:Plotter]
33
PyPlot.PyObject(PyPlot.axes3D)# see https://github.com/JuliaPy/PyPlot.jl/issues/351
4+
PyPlot.rc("font", size = p.context[:fontsize])
45
if !haskey(p.context, :figure)
56
res = p.context[:size]
67
if !isdefined(Main, :PlutoRunner)
7-
p.context[:figure] = PyPlot.figure(p.context[:fignumber]; dpi = 50)
8+
p.context[:figure] = PyPlot.figure(p.context[:fignumber]; dpi = 50, figsize=res./50)
89
else
9-
p.context[:figure] = PyPlot.figure(p.context[:fignumber]; dpi = 100)
10+
p.context[:figure] = PyPlot.figure(p.context[:fignumber]; dpi = 100, figsize=res./100)
1011
end
11-
p.context[:figure].set_size_inches(res[1] / 100, res[2] / 100; forward = true)
12+
#p.context[:figure].set_size_inches(res[1] / 100, res[2] / 100, forward = true)
1213
for ctx in p.subplots
1314
ctx[:figure] = p.context[:figure]
1415
end
@@ -43,6 +44,7 @@ function reveal(ctx::SubVisualizer, TP::Type{PyPlotType})
4344
if ctx[:show] || ctx[:reveal]
4445
reveal(ctx[:GridVisualizer], TP)
4546
end
47+
ctx[:GridVisualizer].Plotter.tight_layout()
4648
end
4749

4850
#translate Julia attribute symbols to pyplot-speak
@@ -552,6 +554,7 @@ function scalarplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}}, grids, parentgri
552554
crange = (crange[1] - eps, crange[1] + eps)
553555
colorlevels = collect(crange[1]:((crange[2]-crange[1])/(1)):crange[2])
554556
else
557+
crange = (crange[1] - 1e-16, crange[2] + 1e-16) # avoids rare clipping of last color level
555558
colorlevels =
556559
collect(crange[1]:((crange[2]-crange[1])/(ctx[:colorlevels]-1)):crange[2])
557560
end
@@ -716,10 +719,10 @@ function vectorplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}}, grid, func)
716719
ax.set_aspect(ctx[:aspect])
717720
ax.set_title(ctx[:title])
718721

719-
rc, rv = vectorsample(grid, func; spacing = ctx[:spacing], offset = ctx[:offset])
720-
qc, qv = quiverdata(rc, rv; vscale = ctx[:vscale], vnormalize = ctx[:vnormalize])
722+
rc, rv = vectorsample(grid, func; spacing = ctx[:spacing], offset = ctx[:offset], xlimits = ctx[:xlimits], ylimits = ctx[:ylimits])
723+
qc, qv = quiverdata(rc, rv; vscale = ctx[:vscale], vnormalize = ctx[:vnormalize], vconstant = ctx[:vconstant])
721724

722-
ax.quiver(qc[1, :], qc[2, :], qv[1, :], qv[2, :])
725+
ax.quiver(qc[1, :], qc[2, :], qv[1, :], qv[2, :]; color = ctx[:color])
723726
ax.set_xlabel(ctx[:xlabel])
724727
ax.set_ylabel(ctx[:ylabel])
725728

@@ -769,11 +772,11 @@ function streamplot!(ctx, TP::Type{PyPlotType}, ::Type{Val{2}}, grid, func)
769772
xout, yout
770773
end
771774

772-
rc, rv = vectorsample(grid, func; spacing = ctx[:spacing], offset = ctx[:offset])
775+
rc, rv = vectorsample(grid, func; spacing = ctx[:spacing], offset = ctx[:offset], xlimits = ctx[:xlimits], ylimits = ctx[:ylimits])
773776

774777
X, Y = meshgrid(rc)
775778

776-
ax.streamplot(X, Y, rv[1, :, :, 1], rv[2, :, :, 1]; color = ctx[:color])
779+
ax.streamplot(X, Y, rv[1, :, :, 1]', rv[2, :, :, 1]'; color = ctx[:color], density = ctx[:density])
777780
ax.set_xlabel(ctx[:xlabel])
778781
ax.set_ylabel(ctx[:ylabel])
779782

0 commit comments

Comments
 (0)