coffeemuggler/sandbox

make_Sample() - crash if packing density becomes negative by chance

Closed this issue · 1 comments

Observation

Randomly make_Sample() crashes with

Error in seq.default(from = 1, to = length(d_sample), by = 1e+06) : 
wrong sign in 'by' argument

This seems to be related to the parameter w_z sampled from a normal distribution, for which by chance (so randomly) the estimate becomes negative if the standard deviation is big enough and/or the mean too close to 0.

sandbox/R/make_Sample.R

Lines 154 to 158 in 9b24f16

} else if (book$packing[[p_z + 1]]$type == "normal") {
w_z <- stats::rnorm(
n = 1,
mean = book$packing[[p_z + 1]]$mean(z),
sd = book$packing[[p_z + 1]]$sd(z))

Reproducible example

set.seed(1007)
sample_01 <- make_Sample(
  book = get_RuleBook(), 
  depth = 1, 
  geometry = "cuboid",
  height = 0.001,
  n_cores = 1,
  width = 0.001, 
  length = 0.001)

Suggested solution

Difficult, perhaps we should truncate the distribution, hence discard values < 0.

Update

I encountered this also in other situations, including the examples in the vignette. We should add a treatment of such values otherwise using 'sandbox' might become very frustrating.

@coffeemuggler I might have a solution, we can add a line

  ## remove values below zero
  v_estimate[unlist(v_estimate) < 0] <- NULL

just before:

  ## estimate 110 % of total number of grains for sample volume
  n_grains <- round(V_sample * n_estimate / sum(unlist(v_estimate)) * 1.1)

However, I am not entirely sure whether this makes sense, it would, however, avoid getting the crashes.