Last active
August 29, 2015 14:11
-
-
Save eschnett/e7a7cb4555143cdd220b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function init(u) | |
n = length(u) | |
dx = 1.0 / (n-1) | |
@fastmath @inbounds @simd for i in 1:n | |
u[i] = sin(2pi*dx*i) | |
end | |
end | |
function deriv(u, du) | |
n = length(u) | |
dx = 1.0 / (n-1) | |
@fastmath @inbounds du[1] = (u[2] - u[1]) / dx | |
@fastmath @inbounds @simd for i in 2:n-1 | |
du[i] = (u[i+1] - u[i-1]) / (2*dx) | |
end | |
@fastmath @inbounds du[n] = (u[n] - u[n-1]) / dx | |
end | |
function norm(u) | |
n = length(u) | |
T = eltype(u) | |
s = zero(T) | |
@fastmath @inbounds @simd for i in 1:n | |
s += u[i]^2 | |
end | |
@fastmath @inbounds return sqrt(s/n) | |
end | |
function main() | |
n = 2000 | |
u = Array(Float64, n) | |
init(u) | |
du = similar(u) | |
deriv(u, du) | |
nu = norm(du) | |
@time for i in 1:10^6 | |
deriv(u, du) | |
nu = norm(du) | |
end | |
println(nu) | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment