-
Notifications
You must be signed in to change notification settings - Fork 0
/
EX3_quiver_gradient.jl
92 lines (77 loc) · 2.16 KB
/
EX3_quiver_gradient.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Include packages
if !(@isdefined peaks)
include("peaks.jl")
end
using Printf: @printf, @sprintf
####################
## function
####################
function grad2d(dataorg::Array{Float64,2})
ny, nx = size(dataorg)
Gx = zeros(ny,nx)
Gy = zeros(ny,nx)
diffx = diff(dataorg, dims=2)
Gx[:,1], Gx[:,end] = diffx[:,1], diffx[:,end]
Gx[:,2:nx-1] = [(dataorg[i,j+1]-dataorg[i,j-1])/2 for i=1:ny, j=2:nx-1]
diffy = diff(dataorg, dims=1)
Gy[1,:], Gy[end,:] = diffy[1,:], diffy[end,:]
Gy[2:ny-1,:] = [(dataorg[i+1,j]-dataorg[i-1,j])/2 for i=2:ny-1, j=1:nx]
return(Gx, Gy)
end
####################
####################
## main
####################
# Parameters
const N=31
qs = Int64(2)
L=0.25
# create mesh data
xmat, ymat, ϕ = peaks(N)
xvec = vec(xmat[1,:])
yvec = vec(ymat[:,1])
# gradient
ϕx,ϕy = grad2d(ϕ)
ϕxx,_ = grad2d(ϕx)
_,ϕyy = grad2d(ϕy)
####################
####################
## plot
####################
# directory where figures are printed
figdir="./fig"
if !isdir(figdir); mkdir(figdir); end
using Plots
pyplot()
#gr()
########
## Figure 1
# Two-dimensional non-filled contour
clibrary(:misc)
contour(xvec, yvec, ϕ, c=(:rainbow), fill=false, tickfont=12,
xlims=(-3.,3.), ylims=(-3.,3.), clims=(-6.,8.),
xlabel="X", ylabel="Y", axis_ratio=:equal, size=(800,600),
)
# arrows
quiver!(vec(xmat[1:qs:end,1:qs:end]), vec(ymat[1:qs:end,1:qs:end]),
quiver=(-L*vec(ϕx[1:qs:end,1:qs:end]), -L*vec(ϕy[1:qs:end,1:qs:end])),
color=:black, arrow=arrow(:closed,:head,0.3,0.3))
# save
savefig(joinpath(figdir,"contour_grad2d.png"))
########
########
## Figure 2
# filled contour
clibrary(:misc)
contour(xvec, yvec, ϕ, c=(:rainbow), fill=true, tickfont=12,
xlims=(-3.,3.), ylims=(-3.,3.), clims=(-6.,8.),
xlabel="X", ylabel="Y", axis_ratio=:equal, size=(800,600),
)
# arrows
quiver!(vec(xmat[1:qs:end,1:qs:end]), vec(ymat[1:qs:end,1:qs:end]),
quiver=(L*vec(ϕxx[1:qs:end,1:qs:end]), L*vec(ϕyy[1:qs:end,1:qs:end])),
color=:black, arrow=arrow(:closed,:head,0.3,0.3))
# save
savefig(joinpath(figdir,"contour-filled_grad2d.png"))
########
####################