mrc-ide / odin

ᚩ A DSL for describing and solving differential equations in R
https://mrc-ide.github.io/odin
Other
104 stars 13 forks source link

Compile error when output variable does not occur in any of the derivatives #14

Closed BlackEdder closed 8 years ago

BlackEdder commented 8 years ago

I'm trying to run a model where Recovered never lose immunity, but this results in an compile error if I try to use the same variable in the output:

library(odin)
gen_seeiir <- odin::odin({
  initial(S) <- N - I0
  initial(E1) <- 0
  initial(E2) <- 0
  initial(I1) <- I0
  initial(I2) <- 0
  initial(R) <- 0

  N <- 1e7
  I0 <- 1

  lambda <- 0.00001 * (I1 + I2)
  gamma1 <- 2.5
  gamma2 <- 1.1

  deriv(S) <- -lambda * S
  deriv(E1) <- lambda * S - gamma1 * E1
  deriv(E2) <- gamma1 * (E1 - E2)
  deriv(I1) <- gamma1 * E2  - gamma2 * I1
  deriv(I2) <- gamma2 * (I1 - I2)
  deriv(R) <- gamma2 * I2

  output(tot) <- S + E1 + E2 + I1 + I2 + R
})

mod <- gen_seeiir()
t <- seq(0, 360, length.out = 100)
View(mod$run(t))

Results in the following error:

gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c odin6bf5f67b52f.c -o odin6bf5f67b52f.o
odin6bf5f67b52f.c: In function ‘odin6bf5f67b52f_deriv’:
odin6bf5f67b52f.c:104:41: error: ‘R’ undeclared (first use in this function)
     output[0] = S + E1 + E2 + I1 + I2 + R;
                                         ^
odin6bf5f67b52f.c:104:41: note: each undeclared identifier is reported only once for each function it appears in
/usr/lib/R/etc/Makeconf:132: recipe for target 'odin6bf5f67b52f.o' failed
make: *** [odin6bf5f67b52f.o] Error 1
Error in compile(path, verbose, load) : Error compiling source

If I add R to one of the derivs, i.e.:

  deriv(R) <- gamma2 * I2 + 0*R

it does work.

richfitz commented 8 years ago

Magic, thanks. I think I know why this is happening and will get this fixed tomorrow probably.

richfitz commented 8 years ago

Thanks for such a clear example to trigger the bug

BlackEdder commented 8 years ago

Thanks for fixing it so quickly :)