API Reference
Parameter Object Generation
ModelingToolkitParameters.MTKParams — Type
MTKParams(Model::Function; kwargs...)
MTKParams(sys::System; kwargs...)A mutable, hierarchical parameter container for a ModelingToolkit System. Each field mirrors a parameter or sub-system of the underlying model and can be read or mutated with normal getproperty/setproperty! syntax (e.g. pars.resistor.R = 2). Bounds attached to parameters via @parameters X, [bounds=(lo, hi)] are enforced on assignment.
The system must NOT be structurally simplified — construct it with @named (@mtkcompile/@mtkbuild will throw). Initial values come from ModelingToolkit.initial_conditions(sys). Any keyword arguments are applied as parameter overrides after construction.
Use pmap (or model => pars) to convert a MTKParams into the parameter map expected by ODEProblem/SciMLBase.remake, and cache + update! for fast in-place updates.
Examples
pars = MTKParams(RCModel)
pars.resistor.R = 2.0
pars = MTKParams(ConstantVoltage; V = 20)ModelingToolkitParameters.@mtkparams — Macro
@mtkparams name = Model(; sub = ChildComponent(p = 1), kw = value, ...)
@mtkparams const name = Model(; ...)
@mtkparams Model(; ...) # bare-call formConvenience macro that rewrites Model(...) into MTKParams(Model; ...), recursively transforming nested component constructor calls into nested MTKParams calls. Wrapping an assignment lets the catalog name appear in front of the macro so the declaration reads top-to-bottom; const is also supported. The bare-call form (name = @mtkparams Model(...)) still works.
Example
@mtkparams seat_pars = MassSpringDamper(
body = Mass(m = 100),
spring = Spring(k = 1000),
damper = Damper(d = 1),
)expands (roughly) to
seat_pars = MTKParams(MassSpringDamper;
body = MTKParams(Mass; m = 100),
spring = MTKParams(Spring; k = 1000),
damper = MTKParams(Damper; d = 1),
)Parameter Map Generation
ModelingToolkitParameters.pmap — Function
pmap(model::System, pars::MTKParams) -> DictBuild a Dict{symbolic_parameter, value} keyed by the symbolic parameters of model. This is the form accepted by update! and the cache-aware SciMLBase.remake(prob, setters, param_dict) method.
For the flat Vector{Pair} form expected by ODEProblem and the standard SciMLBase.remake(prob; p = ...), write model => pars instead.
Core.Pair — Method
Pair(model::System, pars::MTKParams) -> Vector{Pair}Flatten pars against model into a Vector{Pair} of symbolic_parameter => value entries (recursively walking sub-systems). This is the form accepted by ODEProblem and SciMLBase.remake(prob; p = ...). Equivalent to writing model => pars.
Fields of pars that don't have a matching property on model produce a warning and are skipped.
Caching and Updates
ModelingToolkitParameters.cache — Function
cache(model::System, x::MTKParams; parent = model) -> Vector{ParameterHookWrapper}Pre-build a vector of SymbolicIndexingInterface setter functions, one per parameter field reachable from x (recursing into sub-systems). Pass the result, together with a parameter map from pmap, to update! or SciMLBase.remake to mutate an ODEProblem without rebuilding the setters on each call.
parent is the top-level system used when constructing each setp setter; it only differs from model when cache recurses into a sub-system.
ModelingToolkitParameters.update! — Function
update!(prob::ODEProblem,
setters::Vector{ParameterHookWrapper},
param_dict::Dict) -> probMutate prob in place by applying every setter in setters whose target parameter appears in param_dict. setters is produced by cache and param_dict by pmap. Entries with missing values are skipped (the underlying setters do not accept missing). Returns prob.
Serialization
ModelingToolkitParameters.save_parameters — Function
save_parameters(x::MTKParams, filepath::String)Write x to filepath as a hierarchical TOML file. missing values are stored as the string "missing" so they round-trip through load_parameters.
ModelingToolkitParameters.load_parameters — Function
load_parameters(filepath::String, model::Function) -> MTKParamsConstruct a fresh MTKParams(model) and apply the values stored in the TOML file at filepath (typically written by save_parameters).
load_parameters(filepath::String, sys::System) -> MTKParamsConstruct a fresh MTKParams(sys) and apply the values stored in the TOML file at filepath (typically written by save_parameters).
load_parameters(filepath::String, x::MTKParams) -> MTKParamsApply the values stored in the TOML file at filepath (typically written by save_parameters) to the x::MTKParams parameter object.
ModelingToolkitParameters.parameters_to_string — Function
parameters_to_string(x::MTKParams) -> StringReturn the TOML representation of x as a String. Same format as save_parameters writes, but without touching the filesystem.
ModelingToolkitParameters.string_to_parameters — Function
string_to_parameters(contents::String, x::MTKParams) -> MTKParamsApply parameter values parsed from the TOML string contents to the existing MTKParams instance x, mutating it in place. Returns x.
Type Conversions
Base.Dict(::MTKParams)
Base.setproperty!(::MTKParams, ::Dict)
Base.copy(::MTKParams)Internals
Use this functions to access the internal properties of a MTKParams
ModelingToolkitParameters.get_parent — Function
get_parent(p::MTKParams) -> SystemReturn the underlying (un-simplified) System that backs p. Use this instead of p.parent, since getproperty on a MTKParams looks up parameters by name.
ModelingToolkitParameters.get_defs — Function
get_defs(p::MTKParams) -> DictReturn the internal symbolic_parameter => value dictionary holding the current overrides for p. Mutating the returned dict mutates p.