简体   繁体   中英

Montecarlo and rstudio error: NA/NaN/Inf in foreign function call (arg 5)

This will be difficult to replicate because I am using R's Monte Carlo package to call a Fortran subroutine within RStudio and I am not including the subroutine here because it is complicated. In any case, my code below fails before entering that subroutine (I think). Here is the code:

#####Monte Carlo
rsentTinitial=10
rTinitial=15
res_presets = character(0)
res_presets[1] = 'win' 
res_presets2 = eval(parse(text = "res_presets[1]", encoding = "UTF-8"))
noquote(res_presets2)
ntimestep<-c(264)
mySENSRange2 <- function(irflag,rricomp,rrrandom,res_presets2,rtairsent2, rtairback2, irrtimestep) {
  if (!is.loaded('rwrapper')) {
      dyn.load("rwrapper.so")
  }
  retvals <- .Fortran("RSENSRANGE",irflag = as.integer(irflag), icomp = as.integer(rricomp),
                      rCOUPLEVAR = as.numeric(rrrandom), noquote(res_presets2), rtairsent2 = as.array(rtairsent2[1:ntimestep]),
                      rtairback2 = as.array(rtairback2[1:ntimestep]),irrtimestep = as.integer(irrtimestep))
  rmonteresult<-return(list("sentTemps"=retvals$rtairsent2,"CalcTemps"=retvals$rtairback2, "Timesteps"=retvals$irrtimestep))
}
library(MonteCarlo)
irflag_grid<-c(1)
rricomp_grid<-c(1) #Hardwiring for now
rrrandom_grid<-seq(1,2,0.2)
res_presets2_grid<-c(noquote(res_presets2))
rtairsent_grid<-c(data.frame(matrix(1: ntimestep),rsentTinitial))
names(rtairsent_grid)<-c("Timestep","AirTemp")
rtairback_grid<-c(data.frame(matrix(1:ntimestep),rTinitial))
names(rtairback_grid)<-c("Timestep","AirTemp")
irrtimestep_grid<-c(ntimestep)

param_list=list("irflag"=irflag_grid, "rricomp"=rricomp_grid, "rrrandom"=rrrandom_grid,"res_presets2"=as.character(noquote(res_presets2_grid)),
                "rtairsent2"=rtairsent_grid$AirTemp[1:ntimestep],"rtairback2"=rtairback_grid$AirTemp[1:ntimestep],"irrtimestep"=irrtimestep_grid)
monteResult<-MonteCarlo(func=mySENSRange2, nrep=20, param_list=param_list, ncpus=1)
df<-MakeFrame(list(CalcTemps,sentTemps,Timesteps))

and here are the errors:

Error in func(irflag = param_list[[1]][1], rricomp = param_list[[2]][1],  : 
  NA/NaN/Inf in foreign function call (arg 5)

And also from the last line of the code (the Makeframe line) I get this:

Error in MakeFrame(list(CalcTemps, sentTemps, Timesteps)) : 
  object 'CalcTemps' not found

For the first error it appears that I may have a NaN or that I may be dividing by zero somewhere but I simply can not see that. Any suggestions of what may be going wrong will be much appreciated. Thanks.

I will briefly answer my question in case it is useful to someone.

The MakeFrame error above was solved with:

df<-MakeFrame(monteResult)

The NA/NaN/Inf error was because the Monte Carlo package takes scalar inputs and not arrays. So I just send a scalar to my function and within my function I used a 3rd variable to prepare an array for my function to call after the fortran subroutine. The code is a bit long so I keep this clean I will not upload it (but happy to send it to anyone).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM