15  Forest Succession and Wildfire using setupProject

Author

Eliot McIntire and Ian Eddy

Published

June 18, 2024

See Barebones R script for the code shown in this chapter

1.5 hours – estimated time for 1st time executing

6 GB RAM – estimated peak RAM for executing

We have developed an R version of LANDIS-II’s Biomass Succession Extension, comprised of 2 modules: Biomass_core and Biomass_regeneration. We can combine these with a fire model, such as scfm which is a collaborative effort lead by Steve Cumming at Laval, University (thus the name: Steve Cumming Fire Model).

As with other examples, we also include all the parameterization modules here, with Biomass_borealDataPrep for the Biomass_*** modules and scfmLandcoverInit, scfmRegime and scfmDriver for the scfm family.

This makes for a 10 module project. Using setupProject, we can readily run these from their canonical “i.e., current, up to date, open and available” versions:

Time to run this is between 1 and 2 hours.

Code
repos <- unique(c("predictiveecology.r-universe.dev", getOption("repos")))
options(repos = repos)

# Require::Require(c("SpaDES.project (HEAD)"))
Require::Require(c("PredictiveEcology/SpaDES.project@development (HEAD)")) 
# pkgload::load_all("~/GitHub/SpaDES.project")

out <- SpaDES.project::setupProject(
  updateRprofile = TRUE,
  name = "scfm_example_fresh",
  useGit = FALSE,
  paths = list(projectPath = "~/SpaDES_book"),
  modules = c("PredictiveEcology/Biomass_borealDataPrep@development",
              "PredictiveEcology/Biomass_core@development",
              "PredictiveEcology/Biomass_regeneration@development",
              file.path("PredictiveEcology/scfm@development/modules",
                        c("scfmLandcoverInit", "scfmRegime", "scfmDriver",
                          "scfmIgnition", "scfmEscape", "scfmSpread",
                          "scfmDiagnostics"))
  ),
  params = list(
    .globals = list(.studyAreaName = "scfm_example",
                    dataYear = 2011, #will get kNN 2011 data, and NTEMS 2011 landcover
                    sppEquivCol = "LandR",
                    .plots = "png",
                    .useCache = c(".inputObjects", "init")
                 ),
    scfmDriver = list(targetN = 1000, #default is 4000 - higher targetN adds time + precision
                      # targetN would ideally be minimum 2000 - mean fire size estimates will be bad with 1000
                      .useParallelFireRegimePolys = TRUE) #assumes parallelization is an otpion
    
  ),
  options = list(#spades.allowInitDuringSimInit = TRUE,
                 spades.allowSequentialCaching = TRUE,
                 spades.moduleCodeChecks = FALSE,
                 #                reproducible.shapefileRead = "terra::vect",
                 spades.recoveryMode = 1
  ),
  packages = c('RCurl', 'XML', 'snow'), # need for some downloading; if omitted, a message tells user
  times = list(start = 2011, end = 2061),
  sppEquiv = LandR::sppEquivalencies_CA[KNN %in% c("Popu_Tre", "Betu_Pap",
                                                   "Pice_Gla", "Pice_Mar",
                                                   "Pinu_Con", "Pinu_Ban")],
  studyArea = {
    targetCRS <- paste("+proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0",
                       "+datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0")
    sa <- terra::vect(cbind(-1209980, 7586865), crs = targetCRS)
    sa <- LandR::randomStudyArea(center = sa, size = 10000 * 250 * 30000, seed = 1002)
    sa <- sf::st_as_sf(sa)
  },
  studyAreaLarge = {
    sf::st_buffer(studyArea, 20000)
  },
  rasterToMatchLarge = {
    rtml<- terra::rast(terra::ext(studyAreaLarge), res = c(250, 250))
    terra::crs(rtml) <- terra::crs(studyAreaLarge)
    rtml[] <- 1
    rtml <- terra::mask(rtml, studyAreaLarge)
  },
  rasterToMatch = {
    rtm <- terra::crop(rasterToMatchLarge, studyArea)
    rtm <- terra::mask(rtm, studyArea)
  }
)


#this must be done outside of setupProject (temporarily)
#alternatively
outSim <- do.call(SpaDES.core::simInitAndSpades, out) |>
  Cache()

15.1 Examining things

We can do all sorts of things. We will start with a few simple “accessors”, i.e., “access something from the simList”. completed shows the events that were run. elapsedTime shows how much time each event took.

Code
completed(outSim)
elapsedTime(outSim, units = "minute")

The module metadata can be accessed from the sim or can be read from the module source files or it is usually built into the Rmd file that comes with the module.

Code
mm <- moduleMetadata(outSim)

# a list of all the module metadata
mm$Biomass_borealDataPrep$

15.2 Changing things

Setting .plots = "png" will turn on all known plotting (that uses the Plots function) and save them all as png files in the figures subfolder.

Code
out$params$.globals$.plots <- "png"
outSim <- simInitAndSpades2(out)

Then we can look in the outputs/figures folder to see all the figures that the modules created.

15.3 Barebones R script

Code
repos <- unique(c("predictiveecology.r-universe.dev", getOption("repos")))
options(repos = repos)

# Require::Require(c("SpaDES.project (HEAD)"))
Require::Require(c("PredictiveEcology/SpaDES.project@development (HEAD)")) 
# pkgload::load_all("~/GitHub/SpaDES.project")

out <- SpaDES.project::setupProject(
  updateRprofile = TRUE,
  name = "scfm_example_fresh",
  useGit = FALSE,
  paths = list(projectPath = "~/SpaDES_book"),
  modules = c("PredictiveEcology/Biomass_borealDataPrep@development",
              "PredictiveEcology/Biomass_core@development",
              "PredictiveEcology/Biomass_regeneration@development",
              file.path("PredictiveEcology/scfm@development/modules",
                        c("scfmLandcoverInit", "scfmRegime", "scfmDriver",
                          "scfmIgnition", "scfmEscape", "scfmSpread",
                          "scfmDiagnostics"))
  ),
  params = list(
    .globals = list(.studyAreaName = "scfm_example",
                    dataYear = 2011, #will get kNN 2011 data, and NTEMS 2011 landcover
                    sppEquivCol = "LandR",
                    .plots = "png",
                    .useCache = c(".inputObjects", "init")
                 ),
    scfmDriver = list(targetN = 1000, #default is 4000 - higher targetN adds time + precision
                      # targetN would ideally be minimum 2000 - mean fire size estimates will be bad with 1000
                      .useParallelFireRegimePolys = TRUE) #assumes parallelization is an otpion
    
  ),
  options = list(#spades.allowInitDuringSimInit = TRUE,
                 spades.allowSequentialCaching = TRUE,
                 spades.moduleCodeChecks = FALSE,
                 #                reproducible.shapefileRead = "terra::vect",
                 spades.recoveryMode = 1
  ),
  packages = c('RCurl', 'XML', 'snow'), # need for some downloading; if omitted, a message tells user
  times = list(start = 2011, end = 2061),
  sppEquiv = LandR::sppEquivalencies_CA[KNN %in% c("Popu_Tre", "Betu_Pap",
                                                   "Pice_Gla", "Pice_Mar",
                                                   "Pinu_Con", "Pinu_Ban")],
  studyArea = {
    targetCRS <- paste("+proj=lcc +lat_1=49 +lat_2=77 +lat_0=0 +lon_0=-95 +x_0=0 +y_0=0",
                       "+datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0")
    sa <- terra::vect(cbind(-1209980, 7586865), crs = targetCRS)
    sa <- LandR::randomStudyArea(center = sa, size = 10000 * 250 * 30000, seed = 1002)
    sa <- sf::st_as_sf(sa)
  },
  studyAreaLarge = {
    sf::st_buffer(studyArea, 20000)
  },
  rasterToMatchLarge = {
    rtml<- terra::rast(terra::ext(studyAreaLarge), res = c(250, 250))
    terra::crs(rtml) <- terra::crs(studyAreaLarge)
    rtml[] <- 1
    rtml <- terra::mask(rtml, studyAreaLarge)
  },
  rasterToMatch = {
    rtm <- terra::crop(rasterToMatchLarge, studyArea)
    rtm <- terra::mask(rtm, studyArea)
  }
)


#this must be done outside of setupProject (temporarily)
#alternatively
outSim <- do.call(SpaDES.core::simInitAndSpades, out) |>
  Cache()

completed(outSim)
elapsedTime(outSim, units = "minute")

mm <- moduleMetadata(outSim)

# a list of all the module metadata
mm$Biomass_borealDataPrep$

out$params$.globals$.plots <- "png"
outSim <- simInitAndSpades2(out)