R/simulate.tst.R

Defines functions simulate.tst

#' Simulating tst models
#' 
#' Simulate using the Gaiss-Seodel algorithm an sfc model.
#' 
#' Simulates, via the Gauss-Seidel algorithm, an tst model for the baseline and
#' all scenarios that where added. See tst.addScenario for more on scenarios
#' and sfc.GaussSeidel for more on the Gauss-Seidel algorithm.
#' ore details than the description above ~~
#' 
#' @param model contains the tst model to be simulated
#' @param tolValue minimum variation between two iteration of the Gauss-Seidel
#' algorithm.
#' @param maxIter maximum number of iteration for the Gauss-Seidel algorithm
#' @return %% ~Describe the value returned %% If it is a LIST, use %%

simulate.tst <- function(model, tolValue = 1e-10, maxIter=10000) {

  eval(parse(text=tst.eval(model)))
  
  #variablesMat<-matrix(data=0,nrow=length(model$time),ncol=length(model$variables[,1]),dimnames=list(c(model$time),c(model$variables[,1])))
  
  variablesMat <- model$dat
  
  startInd <- which(dat$Date == model$time[1])-1
  
  for(i in 1:length(model$variables[,1])){
    
    variablesMat[-c(1:startInd),i+1]=as.double(model$variables[i,2])
  
    }
  
  #variablesMat <-  bind_rows(dat,data.frame(variablesMat))

  
  baseline<-tst.runScenario(model,variablesMat,tolValue,maxIter,equations,blocks,variables,prev)
  
  result<-list(baseline=baseline)
  
  if(!is.null(model$scenarios)){
    for(i in 1:length(model$scenarios)){
      variablesMatTemp<-variablesMat
      scen<-model$scenarios[[i]]
      if(sum(is.na(scen$start))!=length(scen$start)){
        for(j in 1:length(names(scen$start))){
          if(!grepl("block",names(scen$start)[j])){
            variablesMatTemp[as.character(seq(from=model$time[1],to=scen$init)),names(scen$start)[j]]=as.double(scen$start[j])	
          }
        }
      }
      
      for(j in 1:length(scen$var)){
        variablesMatTemp[as.character(seq(from=scen$init,to=scen$end)),scen$var[j]]=as.double(scen$value[j])
      }
      scenName<-paste("scenario_",i,sep="")
      
      result[[scenName]]<-tst.runScenario(model,variablesMatTemp,tolValue,maxIter,equations,blocks,variables,prev)
    }
  }
  
  return(result)
}
AdamElderfield/tst_package documentation built on Dec. 5, 2019, 2:08 a.m.