[R-sig-dyn-mod] better implementation of variable species ode.2D model
Thomas Petzoldt
thomas.petzoldt at tu-dresden.de
Fri Feb 24 17:43:46 CET 2017
Am 24.02.2017 um 16:31 schrieb Jeremy Chacon:
> Hello list,
>
> I am running ode.2D models (from deSolve, using ReacTran to manage
> diffusion) in which I wish to have a variable number of "species"
> (differential equations) which a single function can handle. I've managed
> to come up with a method that uses lists and loops internally, see the code
> below for an example with a simpler reaction than I will be using in
> practice.
>
> However, it's quite slow, especially when I scale up the number of species
> (or the number of boxes, but that is expected).
>
> I would greatly appreciate any tips on how to better implement this idea.
> I know if it was a "mean-field" model, I could simply use matrix algebra
> inside the function to allow a variable number of species, but I'm not sure
> how that can be extended to a 2D situation.
2D works nicely if you flatten your 2D matrix into a 1D vector (with
"as.vector") and back to a matrix with "matrix" when needed. Reshaping
matrices is very fast and my experience shows that also deSolve with
multidimensional models can be fast, if vectorization is consequently
used. If this is not possible, you may consider to use compiled code (C
or Fortran).
Regards, Thomas
More information about the R-sig-dynamic-models
mailing list