Untitled
unknown
julia
2 years ago
5.3 kB
14
Indexable
module fDNLS_Direct using DifferentialEquations, GLMakie, LinearAlgebra, OffsetArrays, NonlinearSolve NonlinearSolve.ForwardDiff.can_dual(::Type{ComplexF64}) = true using SpecialFunctions: zeta, gamma GLMakie.activate!() #= DifferentialEquations - Used for the ODE Solver GLMakie - 2D/3D plotting backend, LinearAlgebra, SpecialFunctions - Used for importing the Riemann zeta function =# #= This function is an equivalent way to defined the Right-Hand-Side of the system. I was using it for testing purposes. function myrhs!(du, u, p, t) #println(t) α = 0.9 #du = zero(u) for n = 1:length(u) out = 0 for m = 1:length(u) if m != n out = out + (u[n]-u[m])/(abs((n-N)-(m-N))^(1+α)) end end du[n] = -im*(out - abs2(u[n])*u[n]) end end =# N = 50 # 2N-1 α = 0.9 ω = 1 γ = 1 #ϵ = 1 h = 0.4 ϵ = ( 2^α * gamma((1+α)/2) ) / (pi^(0.5) * abs(gamma(-α/2)) * h^(α)) p = [ϵ] tspan = (0.0,300) x=-N+1:N-1 function main(N) #= Per the boundary conditions, solve for u = (u_{-N+1}, ..., u_{N-1}) =# # Per u_(-N) = 0 and u_(N) = 0, then we solve the (2N+1) - 2 = 2N-1 total equations RHS(u,p,t) = -im*(p[1]*coefficientMatrix(α,x)*u - γ*abs2.(u).*u) initial_values = Complex.(ground_state(ω,α,x,ϵ)) initial_value_with_exp = Complex.(ground_state(ω,α,x,ϵ)).*exp.(im*x) # Equivalent to Ode45 from Matlab prob = ODEProblem(RHS, initial_values, tspan, p) solution_direct = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8) prob_with_exp = ODEProblem(RHS, initial_value_with_exp, tspan, p) solution_direct_with_exp = solve(prob_with_exp, Tsit5(), reltol=1e-8, abstol=1e-8) # At every point in time, ell2 norm of solution. #print(size(solution_direct.u)) #= for i in 1:size(solution_direct.u)[1] println(norm(solution_direct.u[i]), 2) end =# plot_main(solution_direct, solution_direct_with_exp, x,p,ϵ) # Surface and contour plot of |u|² return solution_direct end function coefficientMatrix(α,x) M = OffsetArray(zeros(length(x), length(x)), x,x) # Offset indices for M[i,j]. For example, M[-N+1, -N+1] corresponds to M[1,1]. for i in firstindex(M,1) : lastindex(M,1) for j in axes(M,2) i == j ? M[i,i] = 1/(abs(i - (-N))^(1+α)) + sum(1/(abs(i - m)^(1+α)) for m in x if m!=i) + 1/(abs(i - N)^(1+α)) : M[i,j] = -1/(abs(i - j)^(1+α)) end end return M.parent end function onSite(ω,α,x) # Assumption: qₙ = q₋ₙ, q₀ >> 1 >> q₁ >> ... Q = OffsetVector(zeros(length(x)),x) Q[0] = sqrt(ω + 2*zeta(1+α)) Q[1] = Q[0]/(2*zeta(1+α) - (2)^(-1-α) + ω) Q[-1] = Q[1] # Obtain q₂, q₃, ... via equation (3.1) for n in 2:maximum(x) #drop first 1 since already initialized #asymptotic_onsite = sum(Q[j]/((n+j)^(1+α)) for j in 1-n:n-1)\ asymptotic_onsite = (Q[0]/(n^(1+α)) + sum((1/abs(n-j)^(1+α) + 1/abs(n+j)^(1+α))*Q[j] for j in 1:n-1))/(2*zeta(1+α) - (2n)^(-1-α) + ω) Q[-n] = asymptotic_onsite Q[n] = asymptotic_onsite end return Float64.(Q.parent) # typeof(Q) = OffsetVector, typeof(Q.parent) = Vector end function ground_state(ω, α, x, ϵ) # 0 = ω*qₙ + ϵ(Lq)ₙ - qₙ³ p = [ϵ, ω] RHS(u,p) = p[2]*u + p[1]*coefficientMatrix(α,x)*u - u.^3 initialValues = onSite(p[1],α,x) # retrieves the asymptoic onsite sequence Q = [q₋ₙ, ..., q₋₂, q₋₁, q₀, q₁, ..., qₙ] probN = NonlinearProblem(RHS, initialValues, p) sol = solve(probN, NewtonRaphson(), reltol = 1e-9) # norm of RHS sol,p # err = norm(RHS(sol, p),2) # println(err) return sol end function plot_main(solution_direct,solution_direct_with_exp,x,p,ϵ) fig = Figure(backgroundcolor=:snow2) y = solution_direct.t z= [abs(solution_direct[i,j])^2 for i in axes(solution_direct,1), j in axes(solution_direct,2)] # solution_direct[i,j] is the ith component at timestep j. y_with_exp = solution_direct_with_exp.t z_with_exp = [abs(solution_direct_with_exp[i,j])^2 for i in axes(solution_direct_with_exp,1), j in axes(solution_direct_with_exp,2)] surface_axis=Axis3(fig[1,1],xlabel="Space", ylabel="Time", zlabel="|u|²", title="Initial condition: q(x)") p1 = surface!(surface_axis, x, y, z) surface_axis_with_exp=Axis3(fig[1:2,2],xlabel="Space", ylabel="Time", zlabel="|u|²", title="Initial condition: q(x)exp(ix)") p1_with_exp = surface!(surface_axis_with_exp, x, y_with_exp, z_with_exp) #p2 = contourf(fig[1,2], x, y, z, axis=(xlabel="Space", ylabel="Time")) axis_initial_condition = Axis(fig[2,1],xlabel="space",title="q(x): ''fsolve'' with onsite initial", subtitle=L"-\omega q_{n} = \epsilon\sum_{m \in \mathbb{Z}, m\neq n}{\frac{q_n - q_m}{|n-m|^{1+\alpha}}} - |q_n|^2 q_n") p2 = scatter!(axis_initial_condition, x, ground_state(ω,α,x,ϵ)) #= norm_of_solution = [norm(solution_direct.u[i],2) for i in 1:size(solution_direct.u)[1]] axis_of_norm = Axis(fig[2,2],xlabel="Time",title="Conservation of 2-norm of solution") p3 = scatter!(axis_of_norm, solution_direct.t, norm_of_solution) =# display(fig) end main(N) end
Editor is loading...