# R/emissions_model_simple.R In cbrown5/MangroveCarbon: Estimate carbon emissions from mangrove deforestation

#' Simulate cumulative emissions
#'
#'
#' @param t \code{vector} vector of time steps for integration
#' @param Ainit \code{numeric} Initial area of forest
#' @param d \code{numeric} Instantaneous deforestation rate
#' @param g \code{numeric} Instantaneous degradation rate
#' @param CPerHadef \code{numeric} Carbon emission potential for deforestation
#'
#' @return A \code{vector} of cumulative carbon emissions.
#'
#' @details Numerical integration of carbon emissions. Includes
#' degradation and deforestation. Assumes all carbon is emitted as
#' soon as deforestation or degradation occur.
#' There is no deforestation of degraded forest.
#' Primarily used to check against analytical equations.
#'
#'
#' @author Christopher J. Brown
#' @examples
#' years <- seq(0, 50, by = 0.1)
#' emissions <- emissions_model_simple(years, 100, 0.05, 0.1, 776, 200)
#' plot(years, emissions)
#' @rdname emissions_model_simple
#' @export
#
# Preallocation
#

dt <- diff(t)[1]
tmax <- length(t)
A <- rep(NA, tmax)
A[1] <- Ainit
D <- rep(0, tmax)
G <- rep(0, tmax)
E <- rep(0, tmax)

#apportioning loss between deforestation and degradation
relrate <- min(c(d,g))/max(c(d,g))
relrateg <- abs((d>g)-relrate)
relrated <- abs((g>d)-relrate)
for (t in 2:tmax){

A[t] <- A[t-1] - (A[t-1] * dt*(d + g - d*g))

#Model carbon emissions from deforested areas
D[t] <- A[t-1]*dt*(d - d*g*relrated) * CPerHadef