The System type

ModelingToolkit.jl uses System to symbolically represent all types of numerical problems. Users create Systems representing the problem they want to solve and mtkcompile transforms them into a format ModelingToolkit.jl can generate code for (alongside performing other optimizations).

ModelingToolkit.SystemType
struct System <: ModelingToolkit.IntermediateDeprecationSystem

A symbolic representation of a numerical system to be solved. This is a recursive tree-like data structure - each system can contain additional subsystems. As such, it implements the AbstractTrees.jl interface to enable exploring the hierarchical structure.

Fields

  • tag::UInt64: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    A unique integer tag for the system.

  • eqs::Vector{Equation}: The equations of the system.
  • noise_eqs::Union{Nothing, AbstractMatrix, AbstractVector}: The noise terms for each equation of the system. This field is only used for flattened systems. To represent noise in a hierarchical system, use brownians. In a system with N equations and K independent brownian variables, this should be an N x K matrix. In the special case where N == K and each equation has independent noise, this noise matrix is diagonal. Diagonal noise can be specified by providing an N length vector. If this field is nothing, the system does not have noise.
  • jumps::Vector{Union{JumpProcesses.ConstantRateJump, JumpProcesses.MassActionJump, JumpProcesses.VariableRateJump}}: Jumps associated with the system. Each jump can be a VariableRateJump, ConstantRateJump or MassActionJump. See JumpProcesses.jl for more information.
  • constraints::Vector{Union{Equation, Inequality}}: The constraints of the system. This can be used to represent the constraints in an optimal-control problem or boundary-value differential equation, or the constraints in a constrained optimization.
  • costs::Vector{<:Union{Real, SymbolicUtils.BasicSymbolic}}: The costs of the system. This can be the cost in an optimal-control problem, or the loss of an optimization problem. Scalar loss values must also be provided as a single- element vector.
  • consolidate::Any: A function which combines costs into a scalar value. This should take two arguments, the costs of this system and the consolidated costs of all subsystems in the order they are present in the systems field. It should return a scalar cost that combines all of the individual values. This defaults to a function that simply sums all cost values.
  • unknowns::Vector: The variables being solved for by this system. For example, in a differential equation system, this contains the dependent variables.
  • ps::Vector: The parameters of the system. Parameters can either be variables that parameterize the problem being solved for (e.g. the spring constant of a mass-spring system) or additional unknowns not part of the main dynamics of the system (e.g. discrete/clocked variables in a hybrid ODE).
  • brownians::Vector: The brownian variables of the system, created via @brownians. Each brownian variable represents an independent noise. A system with brownians cannot be simulated directly. It needs to be compiled using mtkcompile into noise_eqs.
  • iv::Union{Nothing, SymbolicUtils.BasicSymbolic{Real}}: The independent variable for a time-dependent system, or nothing for a time-independent system.
  • observed::Vector{Equation}: Equations that compute variables of a system that have been eliminated from the set of unknowns by mtkcompile. More generally, this contains all variables that can be computed from the unknowns and parameters and do not need to be solved for. Such variables are termed as "observables". Each equation must be of the form observable ~ expression and observables cannot appear on the LHS of multiple equations. Equations must be sorted such that every observable appears on the left hand side of an equation before it appears on the right hand side of any other equation.
  • parameter_dependencies::Vector{Equation}: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    All the explicit equations relating parameters. Equations here only contain parameters and are in the same format as observed.

  • var_to_name::Dict{Symbol, Any}: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    A mapping from the name of a variable to the actual symbolic variable in the system. This is used to enable getproperty syntax to access variables of a system.

  • name::Symbol: The name of the system.
  • description::String: An optional description for the system.
  • defaults::Dict: Default values that variables (unknowns/observables/parameters) should take when constructing a numerical problem from the system. These values can be overridden by initial values provided to the problem constructor. Defaults of parent systems take priority over those in child systems.
  • guesses::Dict: Guess values for variables of a system that are solved for during initialization.
  • systems::Vector{System}: A list of subsystems of this system. Used for hierarchically building models.
  • initialization_eqs::Vector{Equation}: Equations that must be satisfied during initialization of the numerical problem created from this system. For time-dependent systems, these equations are not valid after the initial time.
  • continuous_events::Vector{ModelingToolkit.SymbolicContinuousCallback}: Symbolic representation of continuous events in a dynamical system. See SymbolicContinuousCallback.
  • discrete_events::Vector{ModelingToolkit.SymbolicDiscreteCallback}: Symbolic representation of discrete events in a dynamica system. See SymbolicDiscreteCallback.
  • connector_type::Any: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    If this system is a connector, the type of connector it is.

  • assertions::Dict{SymbolicUtils.BasicSymbolic, String}: A map from expressions that must be through throughout the solution process to an associated error message. By default these assertions cause the generated code to output NaNs if violated, but can be made to error using debug_system.
  • metadata::Base.ImmutableDict{DataType, Any}: The metadata associated with this system, as a Base.ImmutableDict. This follows the same interface as SymbolicUtils.jl. Metadata can be queried and updated using SymbolicUtils.getmetadata and SymbolicUtils.setmetadata respectively.
  • gui_metadata::Any: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    Metadata added by the @mtkmodel macro.

  • is_dde::Bool: Whether the system contains delay terms. This is inferred from the equations, but can also be provided explicitly.
  • tstops::Vector{Any}: Extra time points for the integrator to stop at. These can be numeric values, or expressions of parameters and time.
  • tearing_state::Any: The TearingState of the system post-simplification with mtkcompile.
  • namespacing::Bool: Whether the system namespaces variables accessed via getproperty. completed systems do not namespace, but this flag can be toggled independently of complete using toggle_namespacing.
  • complete::Bool: Whether the system is marked as "complete". Completed systems cannot be used as subsystems.
  • index_cache::Union{Nothing, ModelingToolkit.IndexCache}: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    For systems simplified or completed with split = true (the default) this contains an IndexCache which aids in symbolic indexing. If this field is nothing, the system is either not completed, or completed with split = false.

  • ignored_connections::Union{Nothing, Vector{Connection}}: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    Connections that should be ignored because they were removed by an analysis point transformation. The first element of the tuple contains all such "standard" connections (ones between connector systems) and the second contains all such causal variable connections.

  • preface::Any: SymbolicUtils.Code.Assignments to prepend to all code generated from this system.
  • parent::Union{Nothing, System}: After simplification with mtkcompile, this field contains the unsimplified system with the hierarchical structure. There may be multiple levels of parents. The root parent is used for accessing variables via getproperty syntax.
  • initializesystem::Union{Nothing, System}: A custom initialization system to use if no initial conditions are provided for the unknowns or observables of this system.
  • is_initializesystem::Bool: Whether the current system is an initialization system.
  • is_discrete::Bool

  • isscheduled::Bool: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    Whether the system has been simplified by mtkcompile.

  • schedule::Union{Nothing, ModelingToolkit.Schedule}: This field is internal API. It may be removed or changed without notice in a non-breaking release. Usage of this field is not advised.

    The Schedule containing additional information about the simplified system.

source

Utility constructors

Several utility constructors also exist to easily construct alternative system formulations.

ModelingToolkit.NonlinearSystemFunction
NonlinearSystem(sys::System) -> Any

Given a time-dependent system sys of ODEs, convert it to a time-independent system of nonlinear equations that solve for the steady-state of the unknowns. This is done by replacing every derivative D(x) of an unknown x with zero. Note that this process does not retain noise equations, brownian terms, jumps or costs associated with sys. All other information such as defaults, guesses, observed and initialization equations are retained. The independent variable of sys becomes a parameter of the returned system.

If sys is hierarchical (it contains subsystems) this transformation will be applied recursively to all subsystems. The output system will be marked as complete if and only if the input system is also complete. This also retains the split flag passed to complete.

See also: complete.

source
ModelingToolkit.SDESystemFunction
SDESystem(eqs::Vector{Equation}, noise, iv; is_scalar_noise = false, kwargs...)

Construct a system of equations with associated noise terms. Instead of specifying noise using @brownians variables, it is specified using a noise matrix noise. iv is the independent variable of the system.

In the general case, noise should be a N x M matrix where N is the number of equations (length(eqs)) and M is the number of independent random variables. noise[i, j] is the diffusion term for equation i and random variable j. If the noise is diagonal (N == M and noise[i, j] == 0 for all i != j) it can be specified as a Vector of length N corresponding to the diagonal of the noise matrix. As a special case, if all equations have the same noise then all rows of noise are identical. This is known as "scalar noise". In this case, noise can be a Vector corresponding to the repeated row and is_scalar_noise must be true.

Note that systems created in this manner cannot be used hierarchically. This should only be used to construct flattened systems. To use such a system hierarchically, it must be converted to use brownian variables using noise_to_brownians. mtkcompile will automatically perform this conversion.

All keyword arguments are the same as those of the System constructor.

source
SDESystem(eqs::Vector{Equation}, noise, iv, dvs, ps; is_scalar_noise = false, kwargs...)

Identical to the 3-argument SDESystem constructor, but uses the explicitly provided dvs and ps for unknowns and parameters of the system.

source
SDESystem(sys::System, noise; kwargs...)

Attach the given noise matrix noise to the system sys.

source
ModelingToolkit.JumpSystemFunction
JumpSystem(jumps, iv; kwargs...)

Construct a System to solve a system of jump equations. jumps is an array of jumps, expressed using JumpProcesses.MassActionJump, JumpProcesses.ConstantRateJump and JumpProcesses.VariableRateJump. It can also include standard equations to simulate jump-diffusion processes. iv should be the independent variable of the system.

All keyword arguments are the same as those of the System constructor.

source
JumpSystem(jumps, iv, dvs, ps; kwargs...)

Identical to the 2-argument JumpSystem constructor, but uses the explicitly provided dvs and ps for unknowns and parameters of the system.

source
ModelingToolkit.OptimizationSystemFunction
OptimizationSystem(cost; kwargs...)

Construct a time-independent System for optimizing the specified scalar cost. The system will have no equations.

Unknowns and parameters of the system are inferred from the cost and other values (such as defaults) passed to it.

All keyword arguments are the same as those of the System constructor.

source
OptimizationSystem(cost, dvs, ps; kwargs...)

Identical to the corresponding single-argument OptimizationSystem constructor, except the unknowns and parameters are specified by passing arrays of symbolic variables to dvs and ps respectively.

source
OptimizationSystem(cost::Array; kwargs...)

Construct a time-independent System for optimizing the specified multi-objective cost. The cost will be reduced to a scalar using the consolidate function. This defaults to summing the specified cost and that of all subsystems. The system will have no equations.

Unknowns and parameters of the system are inferred from the cost and other values (such as defaults) passed to it.

All keyword arguments are the same as those of the System constructor.

source
OptimizationSystem(cost::Array, dvs, ps; kwargs...)

Identical to the corresponding single-argument OptimizationSystem constructor, except the unknowns and parameters are specified by passing arrays of symbolic variables to dvs and ps respectively.

source

Accessor functions

Several accessor functions exist to query systems for the information they contain. In general, for every field x there exists a has_x function which checks if the system contains the field and a get_x function for obtaining the value in the field. Note that fields of a system cannot be accessed via getproperty - that is reserved for accessing variables, subsystems or analysis points of the hierarchical system.

ModelingToolkit.get_eqsFunction
get_eqs(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field eqs of a system sys. It only includes eqs local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_eqs.

source
ModelingToolkit.equationsFunction
equations(sys::ModelingToolkit.AbstractSystem) -> Any

Get the flattened equations of the system sys and its subsystems. It may include some abbreviations and aliases of observables. It is often the most useful way to inspect the equations of a system.

See also full_equations and ModelingToolkit.get_eqs.

source
ModelingToolkit.equations_toplevelFunction
equations_toplevel(sys::AbstractSystem)

Replicates the behaviour of equations, but ignores equations of subsystems.

Notes:

  • Cannot be applied to non-complete systems.
source
ModelingToolkit.get_noise_eqsFunction
get_noise_eqs(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field noise_eqs of a system sys. It only includes noise_eqs local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_noise_eqs.

source
ModelingToolkit.get_jumpsFunction
get_jumps(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field jumps of a system sys. It only includes jumps local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_jumps.

source
ModelingToolkit.jumpsFunction
jumps(sys::ModelingToolkit.AbstractSystem) -> Any

Get the flattened jumps of the system. In other words, obtain all of the jumps in sys and all the subsystems of sys (appropriately namespaced).

source
ModelingToolkit.get_constraintsFunction
get_constraints(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field constraints of a system sys. It only includes constraints local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_constraints.

source
ModelingToolkit.constraintsFunction
constraints(sys::ModelingToolkit.AbstractSystem) -> Any

Get all constraints in the system sys and all of its subsystems, appropriately namespaced.

source
ModelingToolkit.get_costsFunction
get_costs(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field costs of a system sys. It only includes costs local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_costs.

source
ModelingToolkit.costFunction
cost(sys::ModelingToolkit.AbstractSystem) -> Any

Recursively consolidate the cost vector of sys and all subsystems of sys, returning the final scalar cost function.

source
ModelingToolkit.get_consolidateFunction
get_consolidate(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field consolidate of a system sys. It only includes consolidate local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_consolidate.

source
ModelingToolkit.get_unknownsFunction
get_unknowns(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field unknowns of a system sys. It only includes unknowns local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_unknowns.

source
ModelingToolkit.get_psFunction
get_ps(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field ps of a system sys. It only includes ps local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_ps.

source
ModelingToolkit.tunable_parametersFunction
tunable_parameters(sys, p = parameters(sys; initial_parameters = true); default=true)

Get all parameters of sys that are marked as tunable.

Keyword argument default indicates whether variables without tunable metadata are to be considered tunable or not.

Create a tunable parameter by

@parameters u [tunable=true]

For systems created with split = true (the default) and default = true passed to this function, the order of parameters returned is the order in which they are stored in the tunables portion of MTKParameters. Note that array variables will not be scalarized. To obtain the flattened representation of the tunables portion, call Symbolics.scalarize(tunable_parameters(sys)) and concatenate the resulting arrays.

See also getbounds, istunable, MTKParameters, complete

source
ModelingToolkit.get_browniansFunction
get_brownians(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field brownians of a system sys. It only includes brownians local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_brownians.

source
ModelingToolkit.browniansFunction
brownians(sys::ModelingToolkit.AbstractSystem) -> Any

Get all of the brownian variables involved in the system sys and all subsystems, appropriately namespaced.

source
ModelingToolkit.get_ivFunction
get_iv(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field iv of a system sys. It only includes iv local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_iv.

source
ModelingToolkit.get_observedFunction
get_observed(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field observed of a system sys. It only includes observed local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_observed.

source
ModelingToolkit.observablesFunction
observables(sys::ModelingToolkit.AbstractSystem) -> Any

Get the observed variables of the system sys and its subsystems. These can be expressed in terms of unknowns(sys), and do not have to be explicitly solved for. It is equivalent to all left hand sides of observed(sys).

See also observed.

source
ModelingToolkit.get_nameFunction
get_name(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field name of a system sys. It only includes name local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_name.

source
Base.nameofFunction
nameof(sys::ModelingToolkit.AbstractSystem) -> Any

Obtain the name of sys.

source
ModelingToolkit.get_descriptionFunction
get_description(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field description of a system sys. It only includes description local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_description.

source
ModelingToolkit.descriptionFunction
description(sys::ModelingToolkit.AbstractSystem) -> Any

Obtain the description associated with sys if present, and an empty string otherwise.

source
ModelingToolkit.get_defaultsFunction
get_defaults(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field defaults of a system sys. It only includes defaults local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_defaults.

source
ModelingToolkit.get_guessesFunction
get_guesses(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field guesses of a system sys. It only includes guesses local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_guesses.

source
ModelingToolkit.get_systemsFunction
get_systems(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field systems of a system sys. It only includes systems local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_systems.

source
ModelingToolkit.get_initialization_eqsFunction
get_initialization_eqs(
    sys::ModelingToolkit.AbstractSystem
) -> Any

Get the internal field initialization_eqs of a system sys. It only includes initialization_eqs local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_initialization_eqs.

source
ModelingToolkit.get_continuous_eventsFunction
get_continuous_events(
    sys::ModelingToolkit.AbstractSystem
) -> Any

Get the internal field continuous_events of a system sys. It only includes continuous_events local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_continuous_events.

source
ModelingToolkit.continuous_eventsFunction
continuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback}

Returns a vector of all the continuous_events in an abstract system and its component subsystems. The SymbolicContinuousCallbacks in the returned vector are structs with two fields: eqs and affect which correspond to the first and second elements of a Pair used to define an event, i.e. eqs => affect.

See also get_continuous_events, which only returns the events of the top-level system.

source
ModelingToolkit.get_discrete_eventsFunction
get_discrete_events(
    sys::ModelingToolkit.AbstractSystem
) -> Any

Get the internal field discrete_events of a system sys. It only includes discrete_events local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_discrete_events.

source
ModelingToolkit.discrete_events_toplevelFunction
discrete_events_toplevel(sys::AbstractSystem)

Replicates the behaviour of discrete_events, but ignores events of subsystems.

Notes:

  • Cannot be applied to non-complete systems.
source
ModelingToolkit.get_assertionsFunction
get_assertions(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field assertions of a system sys. It only includes assertions local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_assertions.

source
ModelingToolkit.get_metadataFunction
get_metadata(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field metadata of a system sys. It only includes metadata local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_metadata.

source
SymbolicUtils.getmetadataMethod
getmetadata(
    sys::ModelingToolkit.AbstractSystem,
    k::DataType,
    default
) -> Any

Get the metadata associated with key k in system sys or default if it does not exist.

source
SymbolicUtils.setmetadataMethod
setmetadata(
    sys::ModelingToolkit.AbstractSystem,
    k::DataType,
    v
) -> Any

Set the metadata associated with key k in system sys to value v. This is an out-of-place operation, and will return a shallow copy of sys with the appropriate metadata values.

source
ModelingToolkit.get_is_ddeFunction
get_is_dde(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field is_dde of a system sys. It only includes is_dde local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_is_dde.

source
ModelingToolkit.is_ddeFunction
is_dde(sys::AbstractSystem)

Return a boolean indicating whether a system represents a set of delay differential equations.

source
ModelingToolkit.get_tstopsFunction
get_tstops(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field tstops of a system sys. It only includes tstops local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_tstops.

source
ModelingToolkit.symbolic_tstopsFunction
symbolic_tstops(sys::ModelingToolkit.AbstractSystem) -> Any

Get the tstops present in sys and its subsystems, appropriately namespaced.

source
ModelingToolkit.get_tearing_stateFunction
get_tearing_state(
    sys::ModelingToolkit.AbstractSystem
) -> Any

Get the internal field tearing_state of a system sys. It only includes tearing_state local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_tearing_state.

source
ModelingToolkit.toggle_namespacingFunction
toggle_namespacing(
    sys::ModelingToolkit.AbstractSystem,
    value::Bool;
    safe
) -> Any

Return a new sys with namespacing enabled or disabled, depending on value. The keyword argument safe denotes whether systems that do not support such a toggle should error or be ignored.

source
ModelingToolkit.get_prefaceFunction
get_preface(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field preface of a system sys. It only includes preface local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_preface.

source
ModelingToolkit.prefaceFunction
preface(sys::ModelingToolkit.AbstractSystem) -> Any

Obtain the preface associated with sys and all of its subsystems, appropriately namespaced.

source
ModelingToolkit.get_parentFunction
get_parent(sys::ModelingToolkit.AbstractSystem) -> Any

Get the internal field parent of a system sys. It only includes parent local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_parent.

source
ModelingToolkit.get_initializesystemFunction
get_initializesystem(
    sys::ModelingToolkit.AbstractSystem
) -> Any

Get the internal field initializesystem of a system sys. It only includes initializesystem local to sys; not those of its subsystems, like unknowns(sys), parameters(sys) and equations(sys) does.

See also has_initializesystem.

source

getproperty syntax

ModelingToolkit allows obtaining in a system using getproperty. For a system sys with a subcomponent inner containing variable var, sys.inner.var will obtain the appropriately namespaced version of var. Note that this can also be used to access subsystems (sys.inner) or analysis points.

Note

By default, top-level systems not marked as complete will apply their namespace. Systems marked as complete will not do this namespacing. This namespacing behavior can be toggled independently of whether the system is completed using toggle_namespacing and the current namespacing behavior can be queried via ModelingToolkit.does_namespacing.

Base.getpropertyMethod
Base.getproperty(sys::AbstractSystem, name::Symbol)

Access the subsystem, variable or analysis point of sys named name. To check if sys will namespace the returned value, use ModelingToolkit.does_namespacing(sys).

See also: ModelingToolkit.does_namespacing.

source

Functions for querying system equations

ModelingToolkit.has_diff_eqsFunction
has_diff_eqs(sys::AbstractSystem)

Return true if a system contains at least one differential equation (i.e. an equation with a differential term). Note that this does not consider subsystems, and only takes into account equations in the top-level system.

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = tem([eq1], t)
@named osys2 = tem([eq2], t)
osys12 = compose(osys1, [osys2])
osys21 = compose(osys2, [osys1])

has_diff_eqs(osys12) # returns `true`.
has_diff_eqs(osys21) # returns `false`.
source
ModelingToolkit.has_alg_eqsFunction
has_alg_eqs(sys::AbstractSystem)

For a system, returns true if it contain at least one algebraic equation (i.e. that does not contain any differentials) in its top-level system.

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = System([eq1], t)
@named osys2 = System([eq2], t)
osys12 = compose(osys1, [osys2])
osys21 = compose(osys2, [osys1])

has_alg_eqs(osys12) # returns `false`.
has_alg_eqs(osys21) # returns `true`.
source
ModelingToolkit.get_diff_eqsFunction
get_diff_eqs(sys::AbstractSystem)

For a system, returns a vector of all differential equations (i.e. that does contain a differential) in its top-level system.

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = tem([eq1], t)
@named osys2 = tem([eq2], t)
osys12 = compose(osys1, [osys2])
osys21 = compose(osys2, [osys1])

get_diff_eqs(osys12) # returns `[Differential(t)(X(t)) ~ p - d*X(t)]`.
get_diff_eqs(osys21) # returns `Equation[]``.
source
ModelingToolkit.get_alg_eqsFunction
get_alg_eqs(sys::AbstractSystem)

For a system, returns a vector of all algebraic equations (i.e. that does not contain any differentials) in its top-level system.

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = ([eq1], t)
@named osys2 = ([eq2], t)
osys12 = compose(sys1, [osys2])
osys21 = compose(osys2, [osys1])

get_alg_eqs(osys12) # returns `Equation[]`.
get_alg_eqs(osys21) # returns `[0 ~ p - d*X(t)]`.
source
ModelingToolkit.has_diff_equationsFunction
has_diff_equations(sys::AbstractSystem)

For a system, returns true if it contain at least one differential equation (i.e. that contain a differential).

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = System([eq1], t)
@named osys2 = System([eq2], t)

has_diff_equations(osys1) # returns `true`.
has_diff_equations(osys2) # returns `false`.
source
ModelingToolkit.has_alg_equationsFunction
has_alg_equations(sys::AbstractSystem)

For a system, returns true if it contain at least one algebraic equation (i.e. that does not contain any differentials).

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = System([eq1], t)
@named osys2 = System([eq2], t)

has_alg_equations(osys1) # returns `false`.
has_alg_equations(osys2) # returns `true`.
source
ModelingToolkit.diff_equationsFunction
diff_equations(sys::AbstractSystem)

For a system, returns a vector of all its differential equations (i.e. that does contain a differential).

Example: ```julia using ModelingToolkit using ModelingToolkit: tnounits as t, Dnounits as D @parameters p d @variables X(t) eq1 = D(X) ~ p - dX eq2 = 0 ~ p - dX @named osys = System([eq1, eq2], t)

diff_equations(osys) # returns [Differential(t)(X(t)) ~ p - d*X(t)].

source
ModelingToolkit.alg_equationsFunction
alg_equations(sys::AbstractSystem)

For a system, returns a vector of all its algebraic equations (i.e. that does not contain any differentials).

Example: ```julia using ModelingToolkit using ModelingToolkit: tnounits as t, Dnounits as D @parameters p d @variables X(t) eq1 = D(X) ~ p - dX eq2 = 0 ~ p - dX @named osys = System([eq1, eq2], t)

alg_equations(osys) # returns [0 ~ p - d*X(t)].

source
ModelingToolkit.is_alg_equationFunction
is_alg_equation(eq)

Return true if the input is an algebraic equation, i.e. an equation that does not contain any differentials.

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X

is_alg_equation(eq1) # false
is_alg_equation(eq2) # true
source
ModelingToolkit.is_diff_equationFunction
is_diff_equation(eq)

Return true if the input is a differential equation, i.e. an equation that contains a differential term.

Example:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X

is_diff_equation(eq1) # true
is_diff_equation(eq2) # false
source

String parsing

ModelingToolkit can parse system variables from strings.

ModelingToolkit.parse_variableFunction
parse_variable(
    sys::ModelingToolkit.AbstractSystem,
    str::AbstractString
) -> Any

Return the variable in sys referred to by its string representation str. Roughly supports the following CFG:

varname                  = "D(" varname ")" | "Differential(" iv ")(" varname ")" | arrvar | maybe_dummy_var
arrvar                   = maybe_dummy_var "[idxs...]"
idxs                     = int | int "," idxs
maybe_dummy_var          = namespacedvar | namespacedvar "(" iv ")" |
                           namespacedvar "(" iv ")" "ˍ" ts | namespacedvar "ˍ" ts |
                           namespacedvar "ˍ" ts "(" iv ")"
ts                       = iv | iv ts
namespacedvar            = ident "₊" namespacedvar | ident "." namespacedvar | ident

Where iv is the independent variable, int is an integer and ident is an identifier.

source

Dumping system data

ModelingToolkit.dump_unknownsFunction
dump_unknowns(sys::AbstractSystem)

Return an array of NamedTuples containing the metadata associated with each unknown in sys. Also includes the default value of the unknown, if provided.

using ModelingToolkit
using DynamicQuantities
using ModelingToolkit: t, D

@parameters p = 1.0, [description = "My parameter", tunable = false] q = 2.0, [description = "Other parameter"]
@variables x(t) = 3.0 [unit = u"m"]
@named sys = System(Equation[], t, [x], [p, q])
ModelingToolkit.dump_unknowns(sys)

See also: ModelingToolkit.dump_variable_metadata, ModelingToolkit.dump_parameters

source
ModelingToolkit.dump_parametersFunction
dump_parameters(sys::AbstractSystem)

Return an array of NamedTuples containing the metadata associated with each parameter in sys. Also includes the default value of the parameter, if provided.

using ModelingToolkit
using DynamicQuantities
using ModelingToolkit: t, D

@parameters p = 1.0, [description = "My parameter", tunable = false] q = 2.0, [description = "Other parameter"]
@variables x(t) = 3.0 [unit = u"m"]
@named sys = System(Equation[], t, [x], [p, q])
ModelingToolkit.dump_parameters(sys)

See also: ModelingToolkit.dump_variable_metadata, ModelingToolkit.dump_unknowns

source
ModelingToolkit.dump_variable_metadataFunction
dump_variable_metadata(var)

Return all the metadata associated with symbolic variable var as a NamedTuple.

using ModelingToolkit

@parameters p::Int [description = "My description", bounds = (0.5, 1.5)]
ModelingToolkit.dump_variable_metadata(p)
source

Inputs and outputs

ModelingToolkit.is_boundFunction
is_bound(sys, u)

Determine whether input/output variable u is "bound" within the system, i.e., if it's to be considered internal to sys. A variable/signal is considered bound if it appears in an equation together with variables from other subsystems. The typical usecase for this function is to determine whether the input to an IO component is connected to another component, or if it remains an external input that the user has to supply before simulating the system.

See also bound_inputs, unbound_inputs, bound_outputs, unbound_outputs

source

Debugging utilities

ModelingToolkit.debug_systemFunction
debug_system(sys::AbstractSystem; functions = [log, sqrt, (^), /, inv, asin, acos], error_nonfinite = true)

Wrap functions in sys so any error thrown in them shows helpful symbolic-numeric information about its input. If error_nonfinite, functions that output nonfinite values (like Inf or NaN) also display errors, even though the raw function itself does not throw an exception (like 1/0). For example:

julia> sys = debug_system(complete(sys))

julia> prob = ODEProblem(sys, [0.0, 2.0], (0.0, 1.0))

julia> prob.f(prob.u0, prob.p, 0.0)
ERROR: Function /(1, sin(P(t))) output non-finite value Inf with input
  1 => 1
  sin(P(t)) => 0.0

Additionally, all assertions in the system are optionally logged when they fail. A new parameter is also added to the system which controls whether the message associated with each assertion will be logged when the assertion fails. This parameter defaults to true and can be toggled by symbolic indexing with ModelingToolkit.ASSERTION_LOG_VARIABLE. For example, prob.ps[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false will disable logging.

source