The System
type
ModelingToolkit.jl uses System
to symbolically represent all types of numerical problems. Users create System
s 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.System
— Typestruct 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 withN
equations andK
independent brownian variables, this should be anN x K
matrix. In the special case whereN == K
and each equation has independent noise, this noise matrix is diagonal. Diagonal noise can be specified by providing anN
length vector. If this field isnothing
, the system does not have noise.
jumps::Vector{Union{JumpProcesses.ConstantRateJump, JumpProcesses.MassActionJump, JumpProcesses.VariableRateJump}}
: Jumps associated with the system. Each jump can be aVariableRateJump
,ConstantRateJump
orMassActionJump
. SeeJumpProcesses.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, thecosts
of this system and the consolidated costs of all subsystems in the order they are present in thesystems
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 usingmtkcompile
intonoise_eqs
.
iv::Union{Nothing, SymbolicUtils.BasicSymbolic{Real}}
: The independent variable for a time-dependent system, ornothing
for a time-independent system.
observed::Vector{Equation}
: Equations that compute variables of a system that have been eliminated from the set of unknowns bymtkcompile
. 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 formobservable ~ 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. SeeSymbolicContinuousCallback
.
discrete_events::Vector{ModelingToolkit.SymbolicDiscreteCallback}
: Symbolic representation of discrete events in a dynamica system. SeeSymbolicDiscreteCallback
.
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 outputNaN
s if violated, but can be made to error usingdebug_system
.
metadata::Base.ImmutableDict{DataType, Any}
: The metadata associated with this system, as aBase.ImmutableDict
. This follows the same interface as SymbolicUtils.jl. Metadata can be queried and updated usingSymbolicUtils.getmetadata
andSymbolicUtils.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
: TheTearingState
of the system post-simplification withmtkcompile
.
namespacing::Bool
: Whether the system namespaces variables accessed viagetproperty
.complete
d systems do not namespace, but this flag can be toggled independently ofcomplete
usingtoggle_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 anIndexCache
which aids in symbolic indexing. If this field isnothing
, the system is either not completed, or completed withsplit = 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.Assignment
s to prepend to all code generated from this system.
parent::Union{Nothing, System}
: After simplification withmtkcompile
, this field contains the unsimplified system with the hierarchical structure. There may be multiple levels ofparent
s. The root parent is used for accessing variables viagetproperty
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.
Utility constructors
Several utility constructors also exist to easily construct alternative system formulations.
ModelingToolkit.NonlinearSystem
— FunctionNonlinearSystem(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
.
ModelingToolkit.SDESystem
— FunctionSDESystem(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.
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.
SDESystem(sys::System, noise; kwargs...)
Attach the given noise matrix noise
to the system sys
.
ModelingToolkit.JumpSystem
— FunctionJumpSystem(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.
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.
ModelingToolkit.OptimizationSystem
— FunctionOptimizationSystem(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.
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.
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.
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.
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.has_eqs
— Functionhas_eqs(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field eqs
.
See also get_eqs
.
ModelingToolkit.get_eqs
— Functionget_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
.
ModelingToolkit.equations
— Functionequations(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
.
ModelingToolkit.equations_toplevel
— Functionequations_toplevel(sys::AbstractSystem)
Replicates the behaviour of equations
, but ignores equations of subsystems.
Notes:
- Cannot be applied to non-complete systems.
ModelingToolkit.full_equations
— Functionfull_equations(
sys::ModelingToolkit.AbstractSystem;
simplify
) -> Any
Like equations(sys)
, but also substitutes the observed equations eliminated from the equations during mtkcompile
. These equations matches generated numerical code.
See also equations
and ModelingToolkit.get_eqs
.
ModelingToolkit.has_noise_eqs
— Functionhas_noise_eqs(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field noise_eqs
.
See also get_noise_eqs
.
ModelingToolkit.get_noise_eqs
— Functionget_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
.
ModelingToolkit.has_jumps
— Functionhas_jumps(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field jumps
.
See also get_jumps
.
ModelingToolkit.get_jumps
— Functionget_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
.
ModelingToolkit.jumps
— Functionjumps(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).
ModelingToolkit.has_constraints
— Functionhas_constraints(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field constraints
.
See also get_constraints
.
ModelingToolkit.get_constraints
— Functionget_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
.
ModelingToolkit.constraints
— Functionconstraints(sys::ModelingToolkit.AbstractSystem) -> Any
Get all constraints in the system sys
and all of its subsystems, appropriately namespaced.
ModelingToolkit.has_costs
— Functionhas_costs(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field costs
.
See also get_costs
.
ModelingToolkit.get_costs
— Functionget_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
.
ModelingToolkit.cost
— Functioncost(sys::ModelingToolkit.AbstractSystem) -> Any
Recursively consolidate the cost vector of sys
and all subsystems of sys
, returning the final scalar cost function.
ModelingToolkit.has_consolidate
— Functionhas_consolidate(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field consolidate
.
See also get_consolidate
.
ModelingToolkit.get_consolidate
— Functionget_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
.
ModelingToolkit.has_unknowns
— Functionhas_unknowns(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field unknowns
.
See also get_unknowns
.
ModelingToolkit.get_unknowns
— Functionget_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
.
ModelingToolkit.unknowns
— Functionunknowns(sys::ModelingToolkit.AbstractSystem) -> Any
Get the unknown variables of the system sys
and its subsystems. These must be explicitly solved for, unlike observables(sys)
.
See also ModelingToolkit.get_unknowns
.
ModelingToolkit.unknowns_toplevel
— Functionunknowns_toplevel(sys::AbstractSystem)
Replicates the behaviour of unknowns
, but ignores unknowns of subsystems.
ModelingToolkit.has_ps
— Functionhas_ps(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field ps
.
See also get_ps
.
ModelingToolkit.get_ps
— Functionget_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
.
ModelingToolkit.parameters
— Functionparameters(
sys::ModelingToolkit.AbstractSystem;
initial_parameters
) -> Any
Get the parameters of the system sys
and its subsystems.
See also @parameters
and ModelingToolkit.get_ps
.
ModelingToolkit.parameters_toplevel
— Functionparameters_toplevel(sys::AbstractSystem)
Replicates the behaviour of parameters
, but ignores parameters of subsystems.
ModelingToolkit.tunable_parameters
— Functiontunable_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
ModelingToolkit.has_brownians
— Functionhas_brownians(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field brownians
.
See also get_brownians
.
ModelingToolkit.get_brownians
— Functionget_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
.
ModelingToolkit.brownians
— Functionbrownians(sys::ModelingToolkit.AbstractSystem) -> Any
Get all of the brownian variables involved in the system sys
and all subsystems, appropriately namespaced.
ModelingToolkit.has_iv
— Functionhas_iv(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field iv
.
See also get_iv
.
ModelingToolkit.get_iv
— Functionget_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
.
ModelingToolkit.has_observed
— Functionhas_observed(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field observed
.
See also get_observed
.
ModelingToolkit.get_observed
— Functionget_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
.
ModelingToolkit.observed
— Functionobserved(sys::ModelingToolkit.AbstractSystem) -> Any
Get the observed equations 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.
See also observables
and ModelingToolkit.get_observed()
.
ModelingToolkit.observables
— Functionobservables(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
.
ModelingToolkit.has_name
— Functionhas_name(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field name
.
See also get_name
.
ModelingToolkit.get_name
— Functionget_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
.
Base.nameof
— Functionnameof(sys::ModelingToolkit.AbstractSystem) -> Any
Obtain the name of sys
.
ModelingToolkit.has_description
— Functionhas_description(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field description
.
See also get_description
.
ModelingToolkit.get_description
— Functionget_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
.
ModelingToolkit.description
— Functiondescription(sys::ModelingToolkit.AbstractSystem) -> Any
Obtain the description associated with sys
if present, and an empty string otherwise.
ModelingToolkit.has_defaults
— Functionhas_defaults(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field defaults
.
See also get_defaults
.
ModelingToolkit.get_defaults
— Functionget_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
.
ModelingToolkit.defaults
— Functiondefaults(sys::ModelingToolkit.AbstractSystem) -> Any
Get the default values of the system sys and its subsystems. If they are not explicitly provided, variables and parameters are initialized to these values.
See also initialization_equations
and ModelingToolkit.get_defaults
.
ModelingToolkit.has_guesses
— Functionhas_guesses(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field guesses
.
See also get_guesses
.
ModelingToolkit.get_guesses
— Functionget_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
.
ModelingToolkit.guesses
— Functionguesses(sys::ModelingToolkit.AbstractSystem) -> Any
Get the guesses for variables in the initialization system of the system sys
and its subsystems.
See also initialization_equations
and ModelingToolkit.get_guesses
.
ModelingToolkit.get_systems
— Functionget_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
.
ModelingToolkit.has_initialization_eqs
— Functionhas_initialization_eqs(
sys::ModelingToolkit.AbstractSystem
) -> Bool
Returns whether the system sys
has the internal field initialization_eqs
.
See also get_initialization_eqs
.
ModelingToolkit.get_initialization_eqs
— Functionget_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
.
ModelingToolkit.initialization_equations
— Functioninitialization_equations(
sys::ModelingToolkit.AbstractSystem
) -> Any
Get the initialization equations of the system sys
and its subsystems.
See also guesses
, defaults
and ModelingToolkit.get_initialization_eqs
.
ModelingToolkit.has_continuous_events
— Functionhas_continuous_events(
sys::ModelingToolkit.AbstractSystem
) -> Bool
Returns whether the system sys
has the internal field continuous_events
.
See also get_continuous_events
.
ModelingToolkit.get_continuous_events
— Functionget_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
.
ModelingToolkit.continuous_events
— Functioncontinuous_events(sys::AbstractSystem)::Vector{SymbolicContinuousCallback}
Returns a vector of all the continuous_events
in an abstract system and its component subsystems. The SymbolicContinuousCallback
s 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.
ModelingToolkit.continuous_events_toplevel
— Functioncontinuous_events_toplevel(sys::AbstractSystem)
Replicates the behaviour of continuous_events
, but ignores events of subsystems.
Notes:
- Cannot be applied to non-complete systems.
ModelingToolkit.has_discrete_events
— Functionhas_discrete_events(
sys::ModelingToolkit.AbstractSystem
) -> Bool
Returns whether the system sys
has the internal field discrete_events
.
See also get_discrete_events
.
ModelingToolkit.get_discrete_events
— Functionget_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
.
ModelingToolkit.discrete_events_toplevel
— Functiondiscrete_events_toplevel(sys::AbstractSystem)
Replicates the behaviour of discrete_events
, but ignores events of subsystems.
Notes:
- Cannot be applied to non-complete systems.
ModelingToolkit.has_assertions
— Functionhas_assertions(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field assertions
.
See also get_assertions
.
ModelingToolkit.get_assertions
— Functionget_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
.
ModelingToolkit.assertions
— Functionassertions(sys::ModelingToolkit.AbstractSystem) -> Any
Get the assertions for a system sys
and its subsystems.
ModelingToolkit.has_metadata
— Functionhas_metadata(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field metadata
.
See also get_metadata
.
ModelingToolkit.get_metadata
— Functionget_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
.
SymbolicUtils.getmetadata
— Methodgetmetadata(
sys::ModelingToolkit.AbstractSystem,
k::DataType,
default
) -> Any
Get the metadata associated with key k
in system sys
or default
if it does not exist.
SymbolicUtils.setmetadata
— Methodsetmetadata(
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.
ModelingToolkit.has_is_dde
— Functionhas_is_dde(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field is_dde
.
See also get_is_dde
.
ModelingToolkit.get_is_dde
— Functionget_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
.
ModelingToolkit.is_dde
— Functionis_dde(sys::AbstractSystem)
Return a boolean indicating whether a system represents a set of delay differential equations.
ModelingToolkit.has_tstops
— Functionhas_tstops(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field tstops
.
See also get_tstops
.
ModelingToolkit.get_tstops
— Functionget_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
.
ModelingToolkit.symbolic_tstops
— Functionsymbolic_tstops(sys::ModelingToolkit.AbstractSystem) -> Any
Get the tstops present in sys
and its subsystems, appropriately namespaced.
ModelingToolkit.has_tearing_state
— Functionhas_tearing_state(
sys::ModelingToolkit.AbstractSystem
) -> Bool
Returns whether the system sys
has the internal field tearing_state
.
See also get_tearing_state
.
ModelingToolkit.get_tearing_state
— Functionget_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
.
ModelingToolkit.does_namespacing
— Functiondoes_namespacing(sys::ModelingToolkit.AbstractSystem) -> Any
Check whether a system performs namespacing.
ModelingToolkit.toggle_namespacing
— Functiontoggle_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.
ModelingToolkit.iscomplete
— Functioniscomplete(sys::ModelingToolkit.AbstractSystem) -> Any
Check whether a system is marked as complete
.
ModelingToolkit.has_preface
— Functionhas_preface(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field preface
.
See also get_preface
.
ModelingToolkit.get_preface
— Functionget_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
.
ModelingToolkit.preface
— Functionpreface(sys::ModelingToolkit.AbstractSystem) -> Any
Obtain the preface associated with sys
and all of its subsystems, appropriately namespaced.
ModelingToolkit.has_parent
— Functionhas_parent(sys::ModelingToolkit.AbstractSystem) -> Bool
Returns whether the system sys
has the internal field parent
.
See also get_parent
.
ModelingToolkit.get_parent
— Functionget_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
.
ModelingToolkit.has_initializesystem
— Functionhas_initializesystem(
sys::ModelingToolkit.AbstractSystem
) -> Bool
Returns whether the system sys
has the internal field initializesystem
.
See also get_initializesystem
.
ModelingToolkit.get_initializesystem
— Functionget_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
.
ModelingToolkit.is_initializesystem
— Functionis_initializesystem(
sys::ModelingToolkit.AbstractSystem
) -> Any
Check if the given system is an initialization system.
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.
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.getproperty
— MethodBase.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
.
Functions for querying system equations
ModelingToolkit.has_diff_eqs
— Functionhas_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`.
ModelingToolkit.has_alg_eqs
— Functionhas_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`.
ModelingToolkit.get_diff_eqs
— Functionget_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[]``.
ModelingToolkit.get_alg_eqs
— Functionget_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)]`.
ModelingToolkit.has_diff_equations
— Functionhas_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`.
ModelingToolkit.has_alg_equations
— Functionhas_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`.
ModelingToolkit.diff_equations
— Functiondiff_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)]
.
ModelingToolkit.alg_equations
— Functionalg_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)]
.
ModelingToolkit.is_alg_equation
— Functionis_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
ModelingToolkit.is_diff_equation
— Functionis_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
String parsing
ModelingToolkit can parse system variables from strings.
ModelingToolkit.parse_variable
— Functionparse_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.
Dumping system data
ModelingToolkit.dump_unknowns
— Functiondump_unknowns(sys::AbstractSystem)
Return an array of NamedTuple
s 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
ModelingToolkit.dump_parameters
— Functiondump_parameters(sys::AbstractSystem)
Return an array of NamedTuple
s 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
ModelingToolkit.dump_variable_metadata
— Functiondump_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)
Inputs and outputs
ModelingToolkit.inputs
— Functioninputs(sys)
Return all variables that mare marked as inputs. See also unbound_inputs
See also bound_inputs
, unbound_inputs
ModelingToolkit.outputs
— Functionoutputs(sys)
Return all variables that mare marked as outputs. See also unbound_outputs
See also bound_outputs
, unbound_outputs
ModelingToolkit.bound_inputs
— Functionbound_inputs(sys)
Return inputs that are bound within the system, i.e., internal inputs See also bound_inputs
, unbound_inputs
, bound_outputs
, unbound_outputs
ModelingToolkit.unbound_inputs
— Functionunbound_inputs(sys)
Return inputs that are not bound within the system, i.e., external inputs See also bound_inputs
, unbound_inputs
, bound_outputs
, unbound_outputs
ModelingToolkit.bound_outputs
— Functionbound_outputs(sys)
Return outputs that are bound within the system, i.e., internal outputs See also bound_inputs
, unbound_inputs
, bound_outputs
, unbound_outputs
ModelingToolkit.unbound_outputs
— Functionunbound_outputs(sys)
Return outputs that are not bound within the system, i.e., external outputs See also bound_inputs
, unbound_inputs
, bound_outputs
, unbound_outputs
ModelingToolkit.is_bound
— Functionis_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
Debugging utilities
ModelingToolkit.debug_system
— Functiondebug_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.