Error in ectorun(ecto) : NA/NaN/Inf in foreign function call (arg 4)

36 views
Skip to first unread message

Maya Viiala

unread,
Aug 4, 2025, 2:30:03 AM8/4/25
to NicheMapR
Hello,

I'm trying to run this code:
ecto<-ectotherm(DEB=1,
                pantmax = pantmax,
                pct_mouth = pct_mouth,
                pct_eyes = pct_eyes,
                pct_cond = pct_cond,
                fluid = fluid,
                Ww_g = Ww_g,
                stages = stages,
                S_instar = S_instar,
                s_j = s.j,
                metab_mode = metab_mode,
                k_EV = k.EV,
                kap_V = kap.V,
                soilnode = soilnode,
                E_sm = E.sm,
                X = X,
                p_Xm = p.Xm,
                pct_H_X = pct_H_X,
                pct_H_N = pct_H_N,
                pct_H_R = pct_H_R,
                pct_H_P = pct_H_P,
                pct_H_death = pct_H_death,
                K_egg = K_egg,
                PE = abs(micro$PE[seq(1, 19, 2)])*-1,
                #K_skin = 0,
                spec_hyd_egg = spec_hyd_egg,
                psi_egg = psi_egg,
                pct_cond_egg = pct_cond_egg,
                gutfill = gutfill,
                raindrink = raindrink,
                viviparous=viviparous,
                clutchsize=clutchsize,
                z.mult=z.mult,
                shape=shape,
                alpha_max=alpha_max,
                alpha_min=alpha_min,
                T_F_min=T_F_min,
                T_F_max=T_F_max,
                T_B_min=T_B_min,
                T_RB_min=T_RB_min,
                T_pref=T_pref,
                CT_max=CT_max,
                CT_min=CT_min,
                diurn=diurn,
                nocturn=nocturn,
                crepus=crepus,
                shade_seek=shade_seek,
                burrow=burrow,
                climb=climb,
                mindepth=mindepth,
                maxdepth=maxdepth,
                pct_wet=pct_wet,
                z=z*z.mult,
                del_M=del.M,
                kap_X=kap.X,
                v=v/24,
                kap=kap,
                p_M=p.M/24,
                E_G=E.G,
                kap_R=kap.R,
                k_J=k.J/24,
                E_Hb=E.Hb*z.mult^3,
                E_Hj=E.Hj*z.mult^3,
                E_Hp=E.Hp*z.mult^3,
                E_He=E.He*z.mult^3,
                h_a=h.a/(24^2),
                s_G=s.G,
                T_REF=T.ref,
                T_A=T.A,
                T_AL=T.AL,
                T_AH=T.AH,
                T_L=T.L,
                T_H=T.H,
                E_0=E.0*z.mult^4,
                f=f,
                d_V=d.V,
                d_E=d.E,
                d_Egg=d.E,
                mu_X=mu.X,
                mu_E=mu.E,
                mu_V=mu.V,
                mu_P=mu.P,
                kap_X_P=kap.P,
                n_X=c(n.CX,n.HX,n.OX,n.NX),
                n_E=c(n.CE,n.HE,n.OE,n.NE),
                n_V=c(n.CV,n.HV,n.OV,n.NV),
                n_P=c(n.CP,n.HP,n.OP,n.NP),
                n_M_nitro=c(n.CN,n.HN,n.ON,n.NN),
                L_b=L.b,
                V_init=V_init,
                E_init=E_init,
                E_H_init=E_H_init,
                stage=stage,
                photostart = photostart,
                photofinish = photofinish,
                starvemode = starvemode,
                nyears = 1,
                minshades = rep(micro$minshade[-(1:180)], each = 24),
                maxshades = rep(micro$maxshade[-(1:180)], each = 24),
                metout = micro$metout[-(1:(24*180)), ],
                shadmet = micro$shadmet[-(1:(24*180)), ],
                soil = micro$soil[-(1:(24*180)), ],
                shadsoil = micro$shadsoil[-(1:(24*180)), ],
                soilmoist = micro$soilmoist[-(1:(24*180)), ],
                shadmoist = micro$shadmoist[-(1:(24*180)), ],
                humid = micro$humid[-(1:(24*180)), ],
                shadhumid = micro$shadhumid[-(1:(24*180)), ],
                soilpot = micro$soilpot[-(1:(24*180)), ],
                shadpot = micro$shadpot[-(1:(24*180)), ],
                tcond = micro$tcond[-(1:(24*180)), ],
                shadtcond = micro$shadtcond[-(1:(24*180)), ],
                rainfall = micro$RAINFALL[-(1:180)],
                rainhr = rep(-1,nrow(micro$metout[-(1:(24*180)), ])),
                preshr = rep(101325 * ((1 - (0.0065 * as.numeric(micro$elev) / 288)) ^ (1/0.190284)), nrow(micro$metout[-(1:(24*180)), ]))
)
but keep getting this error message: Error in ectorun(ecto) : NA/NaN/Inf in foreign function call (arg 4)

I've tried reinstalling NicheMapR, but that didn't work. In micro$metout and micro$shadmet, the column 'POOLDEP' returns all NaNs. I also get NaNs in other outputs (soilpot, shadpot etc.,). The microclimate model runs with no issues. I'm a bit stuck on how to fix this.

Thanks,
Maya 

NicheMapR

unread,
Aug 4, 2025, 2:36:35 AM8/4/25
to NicheMapR

Hi Maya,

Yes it’s to do with something going wrong in the microclimate simulation. There shouldn’t be NaNs in the output. I'll have to see the code you’re using to simulate the microclimate. Are you running the Shiny app and then modifying the code?

All the best,

Mike

Maya Viiala

unread,
Aug 4, 2025, 2:40:08 AM8/4/25
to NicheMapR
Hi Mike,

Thanks re. rocks. 

I am using the shiny apps then modifying the code from there? Is that what's causing the problem?

This is my microclimate code:
dstart <- "31/12/1979"
dfinish <- "31/12/2024"
minshade <- 0
maxshade <- 30
Thcond <- 2.5
SpecHeat <- 870
Density <- 2.56
BulkDensity <- 2.56
windfac <- 1
REFL <- 0.2
cap <- 0
SLE <- 0.95
warm <- 0
Usrhyt <- 0.01
clearsky <- 0
lon <- 117.32987
lat <- -32.54115997
cat('downloading DEM via package elevatr \n')
dem <- microclima::get_dem(r = NA, lat = lat, lon = lon, resolution = 5, zmin = -20, xdims = 100, ydims = 100)
if(FALSE){
  elev <- raster::extract(dem, c(lon, lat))[1]        
  xy <- data.frame(x = lon, y = lat)
  sp::coordinates(xy) = ~x + y
  sp::proj4string(xy) = "+init=epsg:4326"
  xy <- as.data.frame(sp::spTransform(xy, raster::crs(dem)))
  slope <- raster::terrain(dem, unit = "degrees")
  slope <- raster::extract(slope, xy)
  aspect <- raster::terrain(dem, opt = "aspect", unit = "degrees")
  aspect <- raster::extract(aspect, xy)
  ha36 <- 0
  for (i in 0:35) {
    har <- microclima::horizonangle(dem, i * 10, raster::res(dem)[1])
    ha36[i + 1] <- atan(raster::extract(har, xy)) * (180/pi)
  }
  hori <- spline(x = ha36, n = 24, method =  'periodic')$y
  hori[hori < 0] <- 0
  hori[hori > 90] <- 90
}else{
  slope <- 0
  aspect <- 0
  hori<- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  ha36 <- spline(x = hori, n = 36, method =  'periodic')$y
  ha36[ha36 < 0] <- 0
  ha36[ha36 > 90] <- 90
  elev <- NA
}
rainmult = 1
DEP=c(0, 2.5, 5, 10, 15, 20, 30, 50, 100, 200)
claypan = FALSE
if(claypan){
  soilpro <- matrix(data = 0, nrow = 6, ncol = 5)
  colnames(soilpro) <- c("depth", "blkdens", "clay", "silt", "sand")
  soilpro[, 1] <- c(2.5, 7.5, 22.5, 45, 80, 150)
  soilpro[, 2] <- 1.4 # bulk density (Mg/m3)
  soilpro[, 3] <- 90 # % clay
  soilpro[, 4] <- 0 # % silit
  soilpro[, 5] <- 10 # % sand
  soil.hydro <- pedotransfer(soilpro = as.data.frame(soilpro), DEP = DEP)
  PE <- soil.hydro$PE
  BB <- soil.hydro$BB
  BD <- soil.hydro$BD
  KS <- soil.hydro$KS
  BulkDensity <- BD[seq(1, 19, 2)] * 1000 #rep(1360,10) # soil bulk density (kg/m3)
  DD = rep(Density, 19)
}else{
  PE = rep(1.1, 19)
  KS = rep(0.0037, 19)
  BB = rep(4.5, 19)
  BD = rep(BulkDensity, 19)
  DD = rep(Density, 19)
}
soilgrids <- 0
ERR <- 1
tz <- tz_lookup_coords(lat, lon, method = "fast")
dataset <- 'SILO'
if(dataset == 'SILO'){
  micro <- micro_silo(dem = dem, SLE = SLE, ERR = ERR, Thcond = Thcond, SpecHeat = SpecHeat, dstart = dstart, dfinish = dfinish, Usrhyt = Usrhyt, soilgrids = soilgrids,
                       windfac = windfac, slope = slope, aspect = aspect, REFL = REFL,
                       hori = hori, minshade = minshade,
                       maxshade = maxshade, loc = c(lon, lat), runshade = 1,
                       run.gads = 1, snowmodel = 0, BulkDensity =  BulkDensity, Density = Density,
                       cap = cap, warm = warm,
                       email = "maya....@outlook.com", PE = PE, BB = BB, KS = KS, BD = BD, DD = DD)
while(min(micro$metout[,1]) == 0 & ERR <= 3){
  cat("model crashed, trying a higher error tolerance \n")
  ERR <- ERR + 0.5
  micro <- micro_silo(dem = dem, SLE = SLE, ERR = ERR, Thcond = Thcond, SpecHeat = SpecHeat, dstart = dstart, dfinish = dfinish, Usrhyt = Usrhyt, soilgrids = soilgrids,
                       windfac = windfac, slope = slope, aspect = aspect, REFL = REFL,
                       hori = hori, minshade = minshade,
                       maxshade = maxshade, loc = c(lon, lat), runshade = 1,
                       run.gads = 1, snowmodel = 0, BulkDensity =  BulkDensity, Density = Density,
                       cap = cap, warm = warm,
                       email = "maya....@outlook.com", PE = PE, BB = BB, KS = KS, BD = BD, DD = DD)
}
}else{
  micro <- micro_micro.in(ERR = ERR, soilgrids = soilgrids, dstart = dstart, dfinish = dfinish,
                          Usrhyt = Usrhyt, slope = slope, aspect = aspect, REFL = REFL,
                          hori = hori, minshade = minshade, maxshade = maxshade,
                          loc = c(lon, lat), runshade = 1, run.gads = 1, snowmodel = 0,
                          BulkDensity =  BulkDensity, cap = cap, elev = elev,
                          Density = Density, Thcond = Thcond, SpecHeat = SpecHeat,
                          windfac = windfac, PE = PE, BB = BB, KS = KS, BD = BD, DD = DD)
  while(min(micro$metout[,1]) == 0 & ERR <= 3){
    cat("model crashed, trying a higher error tolerance \n")
    ERR <- ERR + 0.5
    micro <- micro_micro.in(ERR = ERR, soilgrids = soilgrids, dstart = dstart, dfinish = dfinish,
                            Usrhyt = Usrhyt, slope = slope, aspect = aspect, REFL = REFL,
                            hori = hori, minshade = minshade, maxshade = maxshade,
                            loc = c(lon, lat), runshade = 1, run.gads = 1, snowmodel = 0,
                            BulkDensity =  BulkDensity, cap = cap, elev = elev,
                            Density = Density, Thcond = Thcond, SpecHeat = SpecHeat,
                            windfac = windfac, PE = PE, BB = BB, KS = KS, BD = BD, DD = DD)
  }
}

apologies for the mass of text.

Thanks,
Maya

Reply all
Reply to author
Forward
0 new messages