Last active
April 8, 2016 15:56
-
-
Save timcdlucas/e304bc4a0381c42d54062bdea0b04ad8 to your computer and use it in GitHub Desktop.
Diffusion based on direction and velocity raster
This file contains hidden or 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
# Edit: whoops, radians | |
library(raster) | |
direction <- raster(nrows=100, ncols=100, xmn=0, xmx=10, , ymn=0, ymx=10) | |
values(direction) <- seq(0, 2 * pi, length.out = 100^2) + rnorm(100^2) | |
velocity <- raster(nrows=100, ncols=100, xmn=0, xmx=10, , ymn=0, ymx=10) | |
values(velocity) <- runif(100^2, 0, 0.1) | |
plot(direction) | |
plot(velocity) | |
# Number of time steps | |
N <- 10000 | |
# empty lat lon matrix | |
fad <- matrix(NA, nrow = N, ncol = 2) | |
# start at middle | |
fad[1, ] <- c(5, 5) | |
for(hr in 2:N){ | |
dir <- extract(direction, fad[hr - 1, , drop = FALSE]) | |
vel <- extract(velocity, fad[hr - 1, , drop = FALSE]) | |
fad[hr, 1] <- fad[hr - 1, 1] + vel * cos(dir) | |
fad[hr, 2] <- fad[hr - 1, 2] + vel * sin(dir) | |
} | |
plot(direction) | |
lines(fad[, 1] ~ fad[, 2]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment