using PyPlot using NtToolBox using Interpolations n = 128 V = perform_blurring(randn(n,n,2),[40], "per"); myplot = V-> plot_vf(V[1:6:n,1:6:n, :]) figure(figsize = (7,7)) myplot(V) Normalize = V -> V ./ repeat( max(1e-9,sqrt.(sum(V.^2, 3))) , outer=(1,1,2)); figure(figsize = (7,7)) myplot(Normalize(V)) Y, X = meshgrid(0:n-1,0:n-1) mu = sin.(X*pi/n).^2 mu = -4*(mu + mu') mu[1,1] = 1; A = V -> real.(ifft(fft(Div(V[:,:,1], V[:,:,2], "per"))./mu)) ProjI = V -> V - Grad(A(V), "per") U = ProjI(V) figure(figsize=(7,7)) myplot(U) figure(figsize=(7,7)) myplot(V-U) f = load_image("NtToolBox/src/data/lena.png", 2*n) f = f[n-Base.div(n,2):n+Base.div(n,2)-1, n-Base.div(n,2):n+Base.div(n,2)-1]; U = Normalize(ProjI(V)); periodic = P -> cat(3, mod(P[:,:,1]-1,n)+1, mod(P[:,:,2]-1,n)+1 ); extend1 = f -> [f f[:,1]] extend = f -> extend1(extend1(f)')'; f1 = extend(f) itp = interpolate((1:size(f1,1), 1:size(f1,2)), f1, Gridded(Linear())) size(itp) function myinterp(P1,f1,Pi) itp = interpolate((1:size(f1,1), 1:size(f1,2)), f1, Gridded(Linear())) itpd = zeros(size(Pi)[1:2]) for i in 1:size(Pi,1) for j in 1:size(Pi,2) itpd[i,j] = itp[Pi[i,j,1], Pi[i,j,2]] end end return itpd end; (Y,X) = meshgrid(1:n,1:n) P = cat(3, X,Y) (Y1,X1) = meshgrid(1:n+1,1:n+1) P1 = cat(3, X1,Y1); W = (f, U) -> myinterp(P1, extend(f), periodic(P - U)); rho = 2 figure(figsize = (5,5)) imageplot(W(f, rho*U)) include("NtSolutions/graphics_5_fluids/exo1.jl") ## Insert your code here. include("NtSolutions/graphics_5_fluids/exo2.jl") ## Insert your code here. nu = 1/10 mu = 2*nu Wt = (V,U) -> cat(3, W(V[:,:,1],U), W(V[:,:,2],U) ); tau = .5; V = Normalize(ProjI(V)); g = copy(f); g = W(g, tau*U) V = Wt(V, tau*U); s1 = [2:n; 1]; s2 = [n; 1:n-1]; function Delta(g) delta = 1/4.*(g[s1,:,:] + g[s2,:,:] + g[:,s1,:] + g[:,s2,:]) - g if size(delta,3)==1 delta = delta[:,:,1] end return delta end; V = V + tau*nu*Delta(V) g = g + tau*mu*Delta(g); V = ProjI(V); include("NtSolutions/graphics_5_fluids/exo3.jl") ## Insert your code here.