mrc-ide / odin

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

Simplify when inputting a vector #288

Closed CGMossa closed 1 year ago

CGMossa commented 1 year ago

Is it possible to simplify this?

  deriv(S[1:N]) <- -beta * S[i] * I[i]
  deriv(I[1:N]) <- +beta * S[i] * I[i]

  beta <- user(0.005)
  N <- user()
  S0[] <- user()
  I0[] <- user()
  initial(S[1:N]) <- S0[i]
  initial(I[1:N]) <- I0[i]
  dim(S) <- N
  dim(I) <- N
  dim(S0) <- N
  dim(I0) <- N

To me, it seems that it is necessary to use S0 and I0 to initialize S and I from the outside ( user ). Furthermore dim is necessary.

Thus,

model_generator$new(S0 = site_S, I0 = site_I, N = length(site_S)) -> model

I'd love to know if this is it. I'm also reading the examples in tests, but none of them shows something like this exactly.

richfitz commented 1 year ago

One option, though perhaps not that much simpler, is

  deriv(S[]) <- -beta * S[i] * I[i]
  deriv(I[]) <- +beta * S[i] * I[i]

  beta <- user(0.005)
  S0[] <- user()
  I0[] <- user()
  initial(S[]) <- S0[i]
  initial(I[]) <- I0[i]
  dim(S0) <- user() # this lets the size of the system be determined by S0
  dim(S) <- N
  dim(I) <- N
  dim(I0) <- N # this enforces that I0 is passed in as the right size
  N <- length(S0) # this converts the user-supplied vector into a length

which you can then initialise like

model <- model_generator$new(S0 = site_S, I0 = site_I)

I've also removed the 1:N from the lhs, that is always implied

CGMossa commented 1 year ago

Perfect. I don't fully the difference yet. But I'll close it and study this