tests/testthat/test-exploitablefuelwoodvolume.R

test_that("exploitablefuelwoodvolume", {

  # Data loading
  data(Paracou6_2016)
  data(MainTrails)
  data(HarvestablePolygons)


  MainTrail <- sf::st_linestring(matrix(c(286400, 583130,
                                          286400, 583250,
                                          286655, 583250,
                                          286655, 583130,
                                          286400, 583130) # the return
                                        ,ncol=2, byrow=TRUE))

  pol1 <- list(matrix(c(286503, 583134,
                        286503, 583240,
                        286507, 583240,
                        286507, 583134,
                        286503, 583134) # the return
                      ,ncol=2, byrow=TRUE))
  pol2 <- list(matrix(c(286650, 583134,
                        286650, 583240,
                        286654, 583240,
                        286654, 583134,
                        286650, 583134) # the return
                      ,ncol=2, byrow=TRUE))

  PolList = list(pol1,pol2) #list of lists of numeric matrices
  ScndTrail <- sf::st_multipolygon(PolList)


  inventory <- addtreedim(inventorycheckformat(Paracou6_2016),
                          volumeparameters = ForestZoneVolumeParametersTable)

  inventory <- suppressMessages(treeselection(inventory, objective = 20, scenario ="manual",
                                              maintrails = MainTrails, harvestablepolygons = HarvestablePolygons,
                                              fuel = "2", diversification = TRUE, specieslax = FALSE,
                                              objectivelax = TRUE, topography = DTMParacou,
                                              plotslope = PlotSlope,
                                              speciescriteria = SpeciesCriteria,
                                              advancedloggingparameters = loggingparameters())$inventory)
  if (!("DeathCause" %in% names(inventory))){
    inventory <- inventory %>%
      add_column(DeathCause = NA) # if "DeathCause" column doesnt exist create it
  }

  inventory$DeathCause[1] <- "maintrail"
  inventory$DeathCause[2] <- "2ndtrail"
  inventory$DeathCause[3] <- "treefall2nd"
  inventory$DeathCause[4] <- "landing"


  TimberV <- timberharvestedvolume(inventory, scenario = "manual", fuel = "2",
                                   advancedloggingparameters = loggingparameters())

  TimberLoggedVolume <- TimberV$TimberLoggedVolume
  NoHollowTimberLoggedVolume <- TimberV$NoHollowTimberLoggedVolume

  # Test data
  MatrixInventory <- as.matrix(Paracou6_2016)

  Rslt0 <- exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = "0",
                                     advancedloggingparameters = loggingparameters(),
                                     TimberLoggedVolume = TimberLoggedVolume, NoHollowTimberLoggedVolume = NoHollowTimberLoggedVolume)

  Rslt1 <- exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = "1",
                                     advancedloggingparameters = loggingparameters(),
                                     TimberLoggedVolume = TimberLoggedVolume, NoHollowTimberLoggedVolume = NoHollowTimberLoggedVolume)

  Rslt2 <- exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = "2",
                                     advancedloggingparameters = loggingparameters(),
                                     TimberLoggedVolume = TimberLoggedVolume, NoHollowTimberLoggedVolume = NoHollowTimberLoggedVolume)

  advancedloggingparameters = loggingparameters()

  HollowTable <- inventory %>%
    filter(DeathCause == "hollowfuel")

  DamageTable <- inventory %>%
    filter(DeathCause == "maintrail" |
             DeathCause == "2ndtrail" |
             DeathCause == "treefall2nd" |
             DeathCause == "landing")

  DamageVolume <- sum(DamageTable$TreeHarvestableVolume) # only damages (without purge and hollow trees)
  DamagePurge <- sum(DamageVolume + advancedloggingparameters$Purge * TimberLoggedVolume) # damages + purge



  # Check the function arguments

  expect_error(exploitablefuelwoodvolume(MatrixInventory),
               regexp = "The 'inventory' argument of the 'exploitablefuelwoodvolume' function must be a data.frame")

  expect_error(exploitablefuelwoodvolume(inventory, scenario = "RIL"),
               regexp = "The 'scenario' argument of the 'exploitablefuelwoodvolume' function must be
         'RIL1', 'RIL2broken', 'RIL2', 'RIL3', 'RIL3fuel', 'RIL3fuelhollow' or 'manual'")

  expect_error(exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = TRUE),
               regexp = "The 'fuel' argument of the 'exploitablefuelwoodvolume' function must be '0', '1', '2' or NULL")

  expect_error(exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = "2",
                                         advancedloggingparameters = 20),
               regexp = "The 'advancedloggingparameters' argument
         of the 'exploitablefuelwoodvolume' function must be a list")

  expect_error(exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = "2",
                                         advancedloggingparameters = loggingparameters(),
                                         TimberLoggedVolume = TRUE, NoHollowTimberLoggedVolume = TRUE),
               regexp = "The 'TimberLoggedVolume' and 'NoHollowTimberLoggedVolume' arguments
         of the 'exploitablefuelwoodvolume' function must be numeric")

  # fuel == "0"
  expect_true(Rslt0$DamageVolume == DamageVolume)
  expect_true(is.null(Rslt0$FuelVolume)) # no fuel wood exploitation

  # fuel == "1"
  expect_true(Rslt1$DamageVolume == DamageVolume)
  expect_true(Rslt1$FuelVolume == DamagePurge) # doest works

  # fuel == "2"
  expect_true(Rslt2$DamageVolume  == DamageVolume)
  if(nrow(HollowTable) > 0)
    expect_true(Rslt2$FuelVolume == sum(DamageVolume +
                                          advancedloggingparameters$TreeHollowPartForFuel *
                                          sum(HollowTable$TreeHarvestableVolume) +
                                          advancedloggingparameters$Purge * NoHollowTimberLoggedVolume))

  if(nrow(HollowTable) == 0)
    expect_true(Rslt2$FuelVolume == DamagePurge)

})


# DamageVolume = sum(DamageTable$TreeHarvestableVolume) # only damage (without purge and hollow trees)
# DamagePurge <- sum(DamageVolume + advancedloggingparameters$Purge * TimberLoggedVolume)

# fuel == "0"
## DamageVolume = DamageVolume # only damage (without purge and hollow trees)
## FuelVolume = 0

# fuel == "1"
## FuelVolume = DamagePurge

# fuel == "2"
## FuelVolume = sum(DamageVolume +
# advancedloggingparameters$TreeHollowPartForFuel *
#   (HollowTable$TreeHarvestableVolume) +
#   advancedloggingparameters$Purge * NoHollowTimberLoggedVolume)
### -if(nrow(HollowTable) > 0) =
### -if(nrow(HollowTable) == 0) = DamagePurge
thomasgaquiere/Maria documentation built on Dec. 24, 2021, 1:20 a.m.