Skip to contents

mtkmodel(name, ...) returns a constructor f(name, ...) of systems, the counterpart of ModelingToolkit's @mtkmodel blocks. Each call builds the model in a fresh environment, in this order:

  1. structural_parameters: a named list of values;

  2. parameters, constants and variables: declarations, strings such as "R = 1.0" or "v(t) = 0", or list("i(t)", connect = Flow) to add metadata; each is bound to its name;

  3. extend: a named list of quoted calls building base systems, whose variables and subsystems become available unnamespaced (as @extend and @unpack), and which the model extends;

  4. components: a named list of quoted calls building subsystems, bound to their names; the name argument is added;

  5. equations, initial_conditions, guesses, continuous_events and discrete_events: quoted expressions (or functions without arguments) evaluated in that environment, returning lists.

The constructor's arguments override structural parameters and the defaults of declared parameters and variables; comp__arg = value passes arg = value to the component comp; arguments named after the declarations of an extended model are passed to it.

Usage

mtkmodel(
  name,
  structural_parameters = list(),
  parameters = list(),
  constants = list(),
  variables = list(),
  components = list(),
  extend = list(),
  equations = NULL,
  initial_conditions = NULL,
  guesses = NULL,
  continuous_events = NULL,
  discrete_events = NULL,
  description = "",
  connector = FALSE,
  iv = NULL,
  metadata = list(),
  .env = parent.frame()
)

Arguments

name

The model name.

structural_parameters

Named list of structural parameters.

parameters, constants, variables

Declarations.

components, extend

Named lists of quoted constructor calls.

equations, initial_conditions, guesses, continuous_events, discrete_events

Quoted expressions or functions without arguments.

description

A string.

connector

Build a connector.

iv

The independent variable.

metadata

A list of system metadata.

.env

The environment in which blocks see other functions and variables.

Value

A constructor function of class Model.

Examples

Decay <- mtkmodel("Decay",
  parameters = list("k = 1"),
  variables = list("x(t) = 1"),
  equations = quote(list(D_nounits(x) ~ -k * x)))
sys <- Decay(name = "decay", k = 2)
equations(sys)
#> [[1]]
#> Derivative(x(t), t) ~ -k*x(t) 
#>