RunModel_HBANLIB_DMod.R 11.90 KiB
#*****************************************************************************************************************
#' Function which performs a single model run for one of the 25 models in the library of HBAN daily models (HBANLIB_DMod)
#' 
#' For further details on the argument structures and initialisation options, see \code{\link{CreateRunOptions_PLUS}}.
#*****************************************************************************************************************
#' @title RunModel_HBANLIB_DMod
#' @author Laurent Coron (February 2014)
#' @example tests/example_RunModel_HBANLIB_DMod.R
#' @seealso \code{\link{RunModel_GR4J}}, \code{\link{RunModel_GR5J}}, \code{\link{RunModel_GR6J}}, \code{\link{RunModel_CemaNeigeHBANLIB_DMod}}
#' @useDynLib airgr
#' @encoding UTF-8
#' @export
#_FunctionInputs__________________________________________________________________________________________________
#' @param  InputsModel         [object of class \emph{InputsModel}] see \code{\link{CreateInputsModel}} for details
#' @param  RunOptions          [object of class \emph{RunOptions}] see \code{\link{CreateRunOptions}} for details
#' @param  Param               [numeric] vector of n parameters (n depends on the model chosen)                             
#'                             \tabular{ll}{                                                                      
#'                             X1      \tab (refer to fortran code for details)                                \cr
#'                             X2      \tab (refer to fortran code for details)                                \cr
#'                             ...     \tab (refer to fortran code for details)                                \cr
#'                             Xn      \tab (refer to fortran code for details)                                \cr
#'                             }                                                                               
#_FunctionOutputs_________________________________________________________________________________________________
#' @return  [list] list containing the function outputs organised as follows:                                         
#'          \tabular{ll}{                                                                                         
#'          \emph{$DatesR  }          \tab [POSIXlt] series of dates                                                    \cr
#'          \emph{$PotEvap }          \tab [numeric] series of input potential evapotranspiration [mm/d]                 \cr
#'          \emph{$Precip  }          \tab [numeric] series of input total precipitation [mm/d]                          \cr
#'          \emph{$Output03}          \tab [numeric] series of output 03                                                 \cr
#'          \emph{$Output04}          \tab [numeric] series of output 04                                                 \cr
#'          \emph{$...     }          \tab [numeric] series of output ...                                                \cr
#'          \emph{$Qsim    }          \tab [numeric] series of Qsim [mm/d]                                               \cr
#'          \emph{$StateEnd}          \tab [numeric] states at the end of the run (res. levels, HU levels) [mm]          \cr
#'          }                                                                                                     
#*****************************************************************************************************************
RunModel_HBANLIB_DMod <- function(InputsModel,RunOptions,Param){
    ##Multimodel_data
    VectTmp <- c(08,11 , ### IndModel=01 | MOD1  | TOPM | Topmodel
                 06,10 , ### IndModel=02 | MOD2  | IHAC | IHACRES
                 04,14 , ### IndModel=03 | MOD3  | GR4J | GR4J (Perrin)
                 05,14 , ### IndModel=04 | MOD4  | GR5J | GR5J (Le Moine)
                 09,13 , ### IndModel=05 | MOD5  | HBV0 | HBV
                 07,05 , ### IndModel=06 | MOD6  | MOHY | Mohyse
                 03,05 , ### IndModel=07 | MOD7  | GR3P | GR3P
                 06,18 , ### IndModel=08 | MOD8  | MORD | Mordor
                 14,09 , ### IndModel=09 | MOD9  | SACR | Sacramento
                 08,15 , ### IndModel=10 | MOD10 | SIMH | Simhyd
                 09,05 , ### IndModel=11 | MOD11 | SMAR | SMAR
                 10,06 , ### IndModel=12 | MOD12 | TANK | TANK
                 06,08 , ### IndModel=13 | MOD13 | HYMO | HYMOD
                 08,06 , ### IndModel=14 | MOD14 | GARD | Gardenia
                 08,07 , ### IndModel=15 | MOD15 | PDM0 | PDM
                 08,06 , ### IndModel=16 | MOD16 | CREC | CREC
                 09,05 , ### IndModel=17 | MOD17 | CEQU | Cequeau
                 10,07 , ### IndModel=18 | MOD18 | NAM0 | NAM
                 08,07 , ### IndModel=19 | MOD19 | WAGE | Wageningen
                 12,11 , ### IndModel=20 | MOD20 | XINA | Xinanjiang
                 01,05 , ### IndModel=21 | MOD21 | GR1J | GR1J (Michel)
                 02,05 , ### IndModel=22 | MOD22 | GR2J | GR2J (Michel)
                 03,05 , ### IndModel=23 | MOD23 | GR3J | GR3J (Edijatno)
                 04,05 , ### IndModel=24 | MOD24 | GRNJ | GR4J (Nascimento)
                 06,16 ) ### IndModel=25 | MOD25 | GR6J | GR6J
    MatMod <- matrix(VectTmp,ncol=2,byrow=TRUE); colnames(MatMod) <- c("NParamMod","NOutputsMod");
    NParamMod <- MatMod[RunOptions$IndModel,1];
    FortranOutputs <- c("PotEvap","Precip",paste("Output",formatC(3:(MatMod[RunOptions$IndModel,2]-1),width=2,flag="0"),sep=""),"Qsim");
    ##Arguments_check
      if(inherits(InputsModel,"InputsModel")==FALSE){ stop("InputsModel must be of class 'InputsModel' \n"); return(NULL); }  
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
if(inherits(InputsModel,"daily" )==FALSE){ stop("InputsModel must be of class 'daily' \n"); return(NULL); } if(inherits(InputsModel,"GR" )==FALSE){ stop("InputsModel must be of class 'GR' \n"); return(NULL); } if(inherits(RunOptions,"RunOptions" )==FALSE){ stop("RunOptions must be of class 'RunOptions' \n"); return(NULL); } if(inherits(RunOptions,"HBANLIB_DMod")==FALSE){ stop("RunOptions must be of class 'HBANLIB_DMod' \n"); return(NULL); } NParam <- NParamMod; if(!is.vector(Param)){ stop("Param must be a vector \n"); return(NULL); } if(sum(!is.na(Param))!=NParam){ stop(paste("Param must be a vector of length ",NParam," and contain no NA \n",sep="")); return(NULL); } Param <- as.double(Param); ##Input_data_preparation if(identical(RunOptions$IndPeriod_WarmUp,as.integer(0))){ RunOptions$IndPeriod_WarmUp <- NULL; } IndPeriod1 <- c(RunOptions$IndPeriod_WarmUp,RunOptions$IndPeriod_Run); LInputSeries <- as.integer(length(IndPeriod1)) if("all" %in% RunOptions$Outputs_Sim){ IndOutputs <- as.integer(1:length(FortranOutputs)); } else { IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim); } ##Call_fortan if(RunOptions$FortranMode == "single"){ RESULTS <- .Fortran("frun_hbanlib_dmod_s",PACKAGE="airgr",DUP=TRUE, ##inputs IndModel=RunOptions$IndModel, ### model ID LInputs=LInputSeries, ### length of input and output series InputsPrecip=as.single(InputsModel$Precip[IndPeriod1]), ### input series of total precipitation [mm/d] InputsPE=as.single(InputsModel$PotEvap[IndPeriod1]), ### input series potential evapotranspiration [mm/d] NParam=as.integer(length(Param)), ### number of model parameter Param=as.single(Param), ### parameter set NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising StateStart=as.single(RunOptions$IniStates), ### state variables used when the model run starts NOutputs=as.integer(length(IndOutputs)), ### number of output series IndOutputs=IndOutputs, ### indices of output series ##outputs Outputs=matrix(as.single(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] StateEnd=rep(as.single(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run ) } if(RunOptions$FortranMode == "double"){ RESULTS <- .Fortran("frun_hbanlib_dmod_d",PACKAGE="airgr",DUP=TRUE, ##inputs IndModel=RunOptions$IndModel, ### model ID LInputs=LInputSeries, ### length of input and output series InputsPrecip=InputsModel$Precip[IndPeriod1], ### input series of total precipitation [mm/d] InputsPE=InputsModel$PotEvap[IndPeriod1], ### input series potential evapotranspiration [mm/d] NParam=as.integer(length(Param)), ### number of model parameter Param=Param, ### parameter set NStates=as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising StateStart=RunOptions$IniStates, ### state variables used when the model run starts NOutputs=as.integer(length(IndOutputs)), ### number of output series IndOutputs=IndOutputs, ### indices of output series ##outputs Outputs=matrix(as.double(-999.999),nrow=LInputSeries,ncol=length(IndOutputs)), ### output series [mm] StateEnd=rep(as.double(-999.999),length(RunOptions$IniStates)) ### state variables at the end of the model run ) } RESULTS$Outputs[ round(RESULTS$Outputs ,3)==(-999.999)] <- NA; RESULTS$StateEnd[round(RESULTS$StateEnd,3)==(-999.999)] <- NA; ##Output_data_preparation IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries; ExportDatesR <- "DatesR" %in% RunOptions$Outputs_Sim; ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim; ##OutputsModel_only if(ExportDatesR==FALSE & ExportStateEnd==FALSE){ OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]); names(OutputsModel) <- FortranOutputs[IndOutputs]; } ##DatesR_and_OutputsModel_only if(ExportDatesR==TRUE & ExportStateEnd==FALSE){
141142143144145146147148149150151152153154155156157158159160161162163164
OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]) ); names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs]); } ##OutputsModel_and_SateEnd_only if(ExportDatesR==FALSE & ExportStateEnd==TRUE){ OutputsModel <- c( lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), list(RESULTS$StateEnd) ); names(OutputsModel) <- c(FortranOutputs[IndOutputs],"StateEnd"); } ##DatesR_and_OutputsModel_and_SateEnd if((ExportDatesR==TRUE & ExportStateEnd==TRUE) | "all" %in% RunOptions$Outputs_Sim){ OutputsModel <- c( list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2,i]), list(RESULTS$StateEnd) ); names(OutputsModel) <- c("DatesR",FortranOutputs[IndOutputs],"StateEnd"); } ##End # rm(RESULTS); gc(); class(OutputsModel) <- c("OutputsModel","daily","GR"); return(OutputsModel); }