This is built with spread2() and is still experimental.
This one differs from other attempts in that it treats the advection and
dispersal as mathematical vectors that are added together.
They are "rounded" to pixel centres.
spread3(
start,
rasQuality,
rasAbundance,
advectionDir,
advectionMag,
meanDist,
dispersalKernel = "exponential",
sdDist = 1,
plot.it = 2,
minNumAgents = 50,
verbose = getOption("LandR.verbose", 0),
saveStack = NULL,
skipChecks = FALSE
)Raster indices from which to initiate dispersal
A raster with habitat quality. Currently, must be scaled from 0 to 1, i.e., a probability of "settling"
A raster where each pixel represents the number of "agents" or pseudo-agents contained. This number of agents, will be spread horizontally, and distributed from each pixel that contains a non-zero non NA value.
A single number or RasterLayer in degrees from North = 0
(though it will use radians if all values are abs(advectionDir) > 2 * pi).
This indicates the direction of advective forcing (i.e., wind).
A single number or RasterLayer in distance units of the
rasQuality, e.g., meters, indicating the relative forcing that will
occur. It is imposed on the total event, i.e., if the meanDist is
10000, and advectionMag is 5000, then the expected
distance (i.e., 63% of agents) will have settled by 15000 map units.
A single number indicating the mean distance parameter in map units
(not pixels), for a negative exponential distribution
dispersal kernel (e.g., dexp). This will mean that 63% of agents will have
settled at this meanDist (still experimental).
One of either "exponential" or "weibull".
A single number indicating the sd parameter of a two-parameter
dispersalKernel.
Defaults to 1, which is the same as the exponential distribution.
Numeric. With increasing numbers above 0, there will be plots produced during iterations. Currently, only 0, 1, or 2+ are distinct.
Single numeric indicating the minimum number of agents to consider all dispersing finished. Default is 50.
Numeric. With increasing numbers above 0, there will be more messages produced. Currently, only 0, 1, or 2+ are distinct.
If provided as a character string, it will save each iteration
as part of a rasterStack to disk upon exit.
Logical. If TRUE, assertions will be skipped (faster, but could miss
problems)
A data.table with all information used during the spreading
## these tests are fairly heavy, so don't run during automated tests
#########################################################
# Simple case, no variation in rasQuality, numeric advectionDir and advectionMag
#########################################################
# \donttest{
library(terra)
origDTThreads <- data.table::setDTthreads(2L)
origNcpus <- options(Ncpus = 2L)
maxDim <- 10000
ras <- terra::rast(terra::ext(c(0, maxDim, 0, maxDim)), res = 100, vals = 0)
rasQuality <- terra::rast(ras)
rasQuality[] <- 1
rasAbundance <- terra::rast(rasQuality)
rasAbundance[] <- 0
# startPixel <- middlePixel(rasAbundance)
startPixel <- sample(seq(terra::ncell(rasAbundance)), 30)
rasAbundance[startPixel] <- 1000
advectionDir <- 70
advectionMag <- 4 * res(rasAbundance)[1]
meanDist <- 2600
# Test the dispersal kernel -- create a function
plotDispersalKernel <- function(out, meanAdvectionMag) {
out[, disGroup := round(distance / 100) * 100]
freqs <- out[, .N, by = "disGroup"]
freqs[, `:=`(cumSum = cumsum(N), N = N)]
plot(freqs$disGroup, freqs$cumSum, # addTo = "CumulativeNumberSettled",
main = "Cumulative Number Settled") # can plot the distance X number
abline(v = meanAdvectionMag + meanDist)
newTitle <- "Number Settled By Distance"
plot(freqs$disGroup, freqs$N, # addTo = gsub(" ", "", newTitle),
main = newTitle) # can plot the distance X number
abline(v = meanAdvectionMag + meanDist)
# should be 0.63:
freqs[disGroup == meanAdvectionMag + meanDist, cumSum] / tail(freqs, 1)[, cumSum]
mtext(side = 3, paste("Average habitat quality: ",
round(mean(rasQuality[], na.rm = TRUE), 2)),
outer = TRUE, line = -2, cex = 2)
}
out <- spread3(rasAbundance = rasAbundance,
rasQuality = rasQuality,
advectionDir = advectionDir,
advectionMag = advectionMag,
meanDist = meanDist, verbose = 2,
plot.it = interactive())
#> assuming that advectionDir is in geographic degrees(i.e., North is 0)
#> Iteration 1
#> Number still dispersing 29942.6974241127
#> Iteration 2
#> Number still dispersing 29845.2283154888
#> Iteration 3
#> Number still dispersing 29684.7959410341
#> Iteration 4
#> Number still dispersing 29471.599029421
#> Iteration 5
#> Number still dispersing 29158.6271121521
#> Iteration 6
#> Number still dispersing 28721.3624223276
#> Iteration 7
#> Number still dispersing 28356.312261047
#> Iteration 8
#> Number still dispersing 27944.1789644038
#> Iteration 9
#> Number still dispersing 27472.4559373704
#> Iteration 10
#> Number still dispersing 26955.9834618051
#> Iteration 11
#> Number still dispersing 26402.5413351275
#> Iteration 12
#> Number still dispersing 25788.7671182813
#> Iteration 13
#> Number still dispersing 24683.7365078135
#> Iteration 14
#> Number still dispersing 23959.6153383102
#> Iteration 15
#> Number still dispersing 23172.8367733528
#> Iteration 16
#> Number still dispersing 21540.4639608792
#> Iteration 17
#> Number still dispersing 20652.9238036112
#> Iteration 18
#> Number still dispersing 19558.149808691
#> Iteration 19
#> Number still dispersing 18468.4974394741
#> Iteration 20
#> Number still dispersing 17540.1893851047
#> Iteration 21
#> Number still dispersing 16559.6334290857
#> Iteration 22
#> Number still dispersing 15614.4540391122
#> Iteration 23
#> Number still dispersing 14557.4945396908
#> Iteration 24
#> Number still dispersing 13639.0590108462
#> Iteration 25
#> Number still dispersing 12757.7408552931
#> Iteration 26
#> Number still dispersing 12020.0088966356
#> Iteration 27
#> Number still dispersing 11387.7198721067
#> Iteration 28
#> Number still dispersing 10500.2799232803
#> Iteration 29
#> Number still dispersing 9777.83223661686
#> Iteration 30
#> Number still dispersing 9075.35468747974
#> Iteration 31
#> Number still dispersing 8321.95881246421
#> Iteration 32
#> Number still dispersing 7627.66500204817
#> Iteration 33
#> Number still dispersing 7029.82400405333
#> Iteration 34
#> Number still dispersing 6440.22832760077
#> Iteration 35
#> Number still dispersing 5967.84917526771
#> Iteration 36
#> Number still dispersing 5465.87770138271
#> Iteration 37
#> Number still dispersing 5052.12128095253
#> Iteration 38
#> Number still dispersing 4620.12281295926
#> Iteration 39
#> Number still dispersing 4257.26204831949
#> Iteration 40
#> Number still dispersing 3915.79558202362
#> Iteration 41
#> Number still dispersing 3569.3125777356
#> Iteration 42
#> Number still dispersing 3222.55243387645
#> Iteration 43
#> Number still dispersing 2923.03809921265
#> Iteration 44
#> Number still dispersing 2620.74565128232
#> Iteration 45
#> Number still dispersing 2358.92331983854
#> Iteration 46
#> Number still dispersing 2157.29849950781
#> Iteration 47
#> Number still dispersing 1931.94434904993
#> Iteration 48
#> Number still dispersing 1717.73625814676
#> Iteration 49
#> Number still dispersing 1480.25529780422
#> Iteration 50
#> Number still dispersing 1312.40079831681
#> Iteration 51
#> Number still dispersing 1147.17279580882
#> Iteration 52
#> Number still dispersing 983.266197311499
#> Iteration 53
#> Number still dispersing 868.799854201858
#> Iteration 54
#> Number still dispersing 706.120908417786
#> Iteration 55
#> Number still dispersing 591.211179453619
#> Iteration 56
#> Number still dispersing 500.100668805833
#> Iteration 57
#> Number still dispersing 435.1939715952
#> Iteration 58
#> Number still dispersing 370.628788238076
#> Iteration 59
#> Number still dispersing 276.250649924435
#> Iteration 60
#> Number still dispersing 167.862606295719
#> Iteration 61
#> Number still dispersing 102.249930007771
#> Iteration 62
#> Number still dispersing 2.05275484378793
plotDispersalKernel(out, advectionMag)
# The next examples are potentially time consuming; avoid on automated testing
if (interactive()) {
#########################################################
### The case of variable quality raster
#########################################################
rasQuality <- terra::rast(system.file("extdata", "rasQuality.tif",
package = "SpaDES.tools"))
terra::crs(rasQuality) <- system.file("extdata", "targetCRS.rds", package = "SpaDES.tools") |>
readRDS() |>
slot("projargs")
mask <- rasQuality < 5
rasQuality[mask[] %in% TRUE] <- 0
# rescale so min is 0.75 and max is 1
rasQuality[] <- rasQuality[] / (reproducible::maxFn(rasQuality) * 4) + 1 / 4
rasAbundance <- terra::rast(rasQuality)
rasAbundance[] <- 0
startPixel <- sample(seq(ncell(rasAbundance)), 300)
rasAbundance[startPixel] <- 1000
advectionDir <- 75
advectionMag <- 4 * res(rasAbundance)[1]
meanDist <- 2600
out <- spread3(rasAbundance = rasAbundance,
rasQuality = rasQuality,
advectionDir = advectionDir,
advectionMag = advectionMag,
meanDist = meanDist, verbose = 2,
plot.it = interactive())
if (interactive()) {
plotDispersalKernel(out, advectionMag)
}
###############################################################################
### The case of variable quality raster, raster for advectionDir & advectionMag
###############################################################################
maxDim <- 10000
ras <- terra::rast(terra::ext(c(0, maxDim, 0, maxDim)), res = 100, vals = 0)
rasQuality <- terra::rast(ras)
rasQuality[] <- 1
rasAbundance <- terra::rast(rasQuality)
rasAbundance[] <- NA
# startPixel <- middlePixel(rasAbundance)
startPixel <- sample(seq(ncell(rasAbundance)), 25)
rasAbundance[startPixel] <- 1000
# raster for advectionDir
advectionDir <- terra::rast(system.file("extdata", "advectionDir.tif",
package = "SpaDES.tools"))
crs(advectionDir) <- crs(rasQuality)
# rescale so min is 0.75 and max is 1
advectionDir[] <- advectionDir[] / (reproducible::maxFn(advectionDir)) * 180
# raster for advectionMag
advectionMag <- terra::rast(system.file("extdata", "advectionMag.tif",
package = "SpaDES.tools"))
crs(advectionMag) <- crs(rasQuality)
# rescale so min is 0.75 and max is 1
advectionMag[] <- advectionMag[] / (reproducible::maxFn(advectionMag)) * 600
out <- spread3(rasAbundance = rasAbundance,
rasQuality = rasQuality,
advectionDir = advectionDir,
advectionMag = advectionMag,
meanDist = meanDist, verbose = 2,
plot.it = interactive())
if (interactive()) {
names(advectionDir) <- "Wind direction"
names(advectionMag) <- "Wind speed"
names(rasAbundance) <- "Initial abundances"
terra::plot(c(advectionDir, advectionMag, rasAbundance))
plotDispersalKernel(out, mean(advectionMag[]))
}
#########################################
# save iterations to a stack to make animated GIF
########################################
tmpStack <- tempfile(pattern = "stackToAnimate", fileext = ".tif")
out <- spread3(rasAbundance = rasAbundance,
rasQuality = rasQuality,
advectionDir = advectionDir,
advectionMag = advectionMag,
meanDist = 2600, verbose = 2,
plot.it = interactive(), saveStack = tmpStack)
## This animates the series of images into an animated GIF
if (require(animation, quietly = TRUE)) {
out2 <- terra::rast(tmpStack)
gifName <- file.path(tempdir(), "animation.gif")
# Only works on some systems; may need to configure
# Works on Windows without system adjustments
if (identical(.Platform$OS.type, "windows"))
saveGIF(interval = 0.1, movie.name = gifName, expr = {
for (i in seq(length(names(out2)))) terra::plot(out2[[i]])
})
}
}
# clean up
data.table::setDTthreads(origDTThreads)
options(Ncpus = origNcpus)
# }