% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/import_desolve.R
\name{import_desolve}
\alias{import_desolve}
\title{Import a deSolve model}
\usage{
import_desolve(model, params, init, times, method = "lsoda", name = NULL)
}
\arguments{
\item{model}{A deSolve-style ODE function with arguments \verb{(t, state, parameters)}.}

\item{params}{Named numeric vector of model parameters (constants).}

\item{init}{Named numeric vector of initial state values (stocks).}

\item{times}{Numeric vector of time points. Must be evenly spaced
(e.g., from \code{seq(start, stop, by = dt)}).}

\item{method}{Integration method. Defaults to \code{"lsoda"}. See \code{\link[=sim_methods]{sim_methods()}}.}

\item{name}{Optional model name. Character scalar.}
}
\value{
A stock-and-flow model of class \code{\link{stockflow}}.
}
\description{
Convert a model written for \href{https://cran.r-project.org/package=deSolve}{deSolve}
into a stock-and-flow model of class \code{\link{stockflow}}.
}
\details{
The model function must follow the canonical deSolve convention:

\if{html}{\out{<div class="sourceCode r">}}\preformatted{model <- function(t, state, parameters) \{
  with(as.list(c(state, parameters)), \{
    dX <- <rate expression>   # d<VarName> for each state in init
    list(c(dX))
  \})
\}
}\if{html}{\out{</div>}}

State variable names are taken from \code{names(init)}, parameter names from
\code{names(params)}. Each \verb{d<VarName>} assignment inside the \code{with()} block is
parsed as the net rate of change for stock \code{VarName} and becomes a flow in
the sfm. Any other assignments in the \code{with()} block (intermediate
calculations) are imported as auxiliary variables in the order they appear.
}
\examples{
logistic_model <- function(t, state, parameters) {
  with(as.list(c(state, parameters)), {
    dN <- r * N * (1 - N / K)
    list(c(dN))
  })
}
sfm <- import_desolve(
  model  = logistic_model,
  params = c(r = 0.3, K = 100),
  init   = c(N = 10),
  times  = seq(0, 50, by = 0.1),
  method = "lsoda",
  name   = "Logistic growth"
)
\dontshow{
sfm <- sim_settings(sfm, save_by = 5)
}
sim <- simulate(sfm)
plot(sim)
}
\seealso{
\code{\link[=import_insightmaker]{import_insightmaker()}}, \code{\link[=export_model]{export_model()}}, \code{\link[=update]{update()}}
}
\concept{importExport}
