Skip to content

Commit 1cbf504

Browse files
committed
Merge branch 'master' into myb/sub_sys
2 parents bf63c1a + cc7a102 commit 1cbf504

File tree

10 files changed

+257
-76
lines changed

10 files changed

+257
-76
lines changed

.github/workflows/Documentation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
build:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/checkout@v3
14+
- uses: actions/checkout@v4
1515
- uses: julia-actions/setup-julia@latest
1616
with:
1717
version: '1'

.github/workflows/Downstream.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ jobs:
3131
- {user: SciML, repo: MethodOfLines.jl, group: DAE}
3232
- {user: ai4energy, repo: Ai4EComponentLib.jl, group: Downstream}
3333
steps:
34-
- uses: actions/checkout@v3
34+
- uses: actions/checkout@v4
3535
- uses: julia-actions/setup-julia@v1
3636
with:
3737
version: ${{ matrix.julia-version }}
3838
arch: x64
3939
- uses: julia-actions/julia-buildpkg@latest
4040
- name: Clone Downstream
41-
uses: actions/checkout@v3
41+
uses: actions/checkout@v4
4242
with:
4343
repository: ${{ matrix.package.user }}/${{ matrix.package.repo }}
4444
path: downstream

.github/workflows/FormatCheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
with:
2222
version: ${{ matrix.julia-version }}
2323

24-
- uses: actions/checkout@v3
24+
- uses: actions/checkout@v4
2525
- name: Install JuliaFormatter and format
2626
# This will use the latest version by default but you can set the version like so:
2727
#

.github/workflows/Invalidations.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ jobs:
1919
- uses: julia-actions/setup-julia@v1
2020
with:
2121
version: '1'
22-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
2323
- uses: julia-actions/julia-buildpkg@v1
2424
- uses: julia-actions/julia-invalidations@v1
2525
id: invs_pr
2626

27-
- uses: actions/checkout@v3
27+
- uses: actions/checkout@v4
2828
with:
2929
ref: ${{ github.event.repository.default_branch }}
3030
- uses: julia-actions/julia-buildpkg@v1

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- '1'
1818
- '1.6'
1919
steps:
20-
- uses: actions/checkout@v3
20+
- uses: actions/checkout@v4
2121
- uses: julia-actions/setup-julia@v1
2222
with:
2323
version: ${{ matrix.version }}

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Yingbo Ma <[email protected]>", "Chris Rackauckas <[email protected]> and contributors"]
4-
version = "8.67.0"
4+
version = "8.68.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -32,6 +32,7 @@ MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
3232
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
3333
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
3434
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
35+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
3536
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
3637
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
3738
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
@@ -80,6 +81,7 @@ MLStyle = "0.4.17"
8081
MacroTools = "0.5"
8182
NaNMath = "0.3, 1"
8283
OrdinaryDiffEq = "6"
84+
PrecompileTools = "1"
8385
RecursiveArrayTools = "2.3"
8486
Reexport = "0.2, 1"
8587
RuntimeGeneratedFunctions = "0.5.9"

docs/src/basics/MTKModel_Connector.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
## Defining components with `@mtkmodel`
2+
3+
`@mtkmodel` is a convenience macro to define ModelingToolkit components. It returns `ModelingToolkit.Model`, which includes a constructor that returns an ODESystem, a `structure` dictionary with metadata and flag `isconnector` which is set to `false`.
4+
5+
### What can an MTK-Model definition have?
6+
7+
`@mtkmodel` definition contains begin blocks of
8+
9+
- `@components`: for listing sub-components of the system
10+
- `@equations`: for the list of equations
11+
- `@extend`: for extending a base system and unpacking its states
12+
- `@parameters`: for specifying the symbolic parameters
13+
- `@structural_parameters`: for specifying non-symbolic parameters
14+
- `@variables`: for specifing the states
15+
16+
Let's explore these in more detail with the following example:
17+
18+
```@example mtkmodel-example
19+
using ModelingToolkit
20+
21+
@mtkmodel ModelA begin
22+
@parameters begin
23+
k1
24+
k2
25+
end
26+
end
27+
28+
@mtkmodel ModelB begin
29+
@parameters begin
30+
p1 = 1.0, [description = "Parameter of ModelB"]
31+
p2 = 1.0, [description = "Parameter of ModelB"]
32+
end
33+
end
34+
35+
@mtkmodel ModelC begin
36+
@structural_parameters begin
37+
f = sin
38+
end
39+
begin
40+
v_var = 1.0
41+
end
42+
@variables begin
43+
v(t) = v_var
44+
end
45+
@extend p1, p2 = model_b = ModelB(; p1)
46+
@components begin
47+
model_a = ModelA(; k1)
48+
end
49+
@equations begin
50+
model_a.k1 ~ f(v)
51+
end
52+
end
53+
```
54+
55+
#### `@parameters` and `@variables` begin block
56+
57+
- Parameters and variables are declared with respective begin blocks.
58+
- Variables must be functions of an independent variable.
59+
- Optionally, default values and metadata can be specified for these parameters and variables. See `ModelB` in the above example.
60+
- Along with creating parameters and variables, keyword arguments of same name with default value `nothing` are created.
61+
- Whenever a parameter or variable has default value, for example `v(t) = 0.0`, a symbolic variable named `v` with default value 0.0 and a keyword argument `v`, with default value `nothing` are created. <br> This way, users can optionally pass new value of `v` while creating a component.
62+
63+
```julia
64+
julia > @named model_c = ModelC(; v = 2.0);
65+
66+
julia > ModelingToolkit.getdefault(model_c.v)
67+
2.0
68+
```
69+
70+
#### `@structural_parameters` begin block
71+
72+
- This block is for non symbolic input arguements. These are for inputs that usually are not meant to be part of components; but influence how they are defined. One can list inputs like boolean flags, functions etc... here.
73+
- Whenever default values are specified, unlike parameters/variables, they are reflected in the keyword argument list.
74+
75+
#### `@extend` block
76+
77+
To extend a partial system,
78+
79+
- List the variables to unpack. If there is a single variable, explicitly specify it as a tuple.
80+
- Give a name to the base system
81+
- List the kwargs of the base system that should be listed as kwargs of the main component.
82+
- Note that in above example, `p1` is promoted as an argument of `ModelC`. Users can set the value of `p1` as
83+
84+
```julia
85+
julia> @named model_c = ModelC(; p1 = 2.0)
86+
87+
```
88+
89+
However, as `p2` isn't listed in the model definition, its default can't be modified by users.
90+
91+
#### `@components` begin block
92+
93+
- Declare the subcomponents within `@components` begin block.
94+
- The arguments in these subcomponents are promoted as keyword arguments as `subcomponent_name__argname` with `nothing` as default value.
95+
- Whenever components are created with `@named` macro, these can be accessed with `.` operator as `subcomponent_name.argname`
96+
- In the above example, `k1` of `model_a` can be set in following ways:
97+
98+
```julia
99+
julia> @named model_c1 = ModelC(; model_a.k1 = 1);
100+
# or as
101+
102+
julia> model_c2 = ModelC(; name = :model_c, model_a__k1 = 1);
103+
104+
```
105+
106+
And as `k2` isn't listed in the sub-component definition of `ModelC`, its default value can't be modified by users.
107+
108+
#### `@equations` begin block
109+
110+
- List all the equations here
111+
112+
#### A begin block
113+
114+
- Any other Julia operations can be included with dedicated begin blocks.
115+
116+
## Defining connectors with `@connector`
117+
118+
`@connector` returns `ModelingToolkit.Model`. It includes a constructor that returns a connector ODESystem, a `structure` dictionary with metadata and flag `isconnector` which is set to `true`.
119+
120+
A simple connector can be defined with syntax similar to following example:
121+
122+
```julia
123+
using ModelingToolkit
124+
125+
@connector Pin begin
126+
v(t) = 0.0, [description = "Voltage"]
127+
i(t), [connect = Flow]
128+
end
129+
```
130+
131+
- Variables (as function of independent variable) are listed out in the definition. These variables can optionally have default values and metadata like `descrption`, `connect` and so on.
132+
133+
`@connector`s accepts begin blocks of `@components`, `@equations`, `@extend`, `@parameters`, `@structural_parameters`, `@variables`. These keywords mean the same as described above for `@mtkmodel`.
134+
135+
!!! note
136+
137+
For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)
138+
139+
* * *
140+
141+
### What's a `structure` dictionary?
142+
143+
For components defined with `@mtkmodel` or `@connector`, a dictionary with metadata is created. It lists `:components` (sub-component list), `:extend` (the extended states and base system), `:parameters`, `:variables`, ``:kwargs`` (list of keyword arguments), `:independent_variable`, `:equations`.
144+
145+
For example, the structure of `ModelC` is:
146+
147+
```julia
148+
julia> ModelC.structure
149+
Dict{Symbol, Any} with 6 entries:
150+
:components => [[:model_a, :ModelA]]
151+
:variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var))
152+
:kwargs => Dict{Symbol, Any}(:f=>:sin, :v=>:v_var, :p1=>nothing, :model_a__k1=>nothing)
153+
:independent_variable => t
154+
:extend => Any[[:p1, :p2], :model_b, :ModelB]
155+
:equations => ["model_a.k1 ~ f(v)"]
156+
```

src/ModelingToolkit.jl

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,79 @@
22
$(DocStringExtensions.README)
33
"""
44
module ModelingToolkit
5-
using DocStringExtensions
6-
using Compat
7-
using AbstractTrees
8-
using DiffEqBase, SciMLBase, ForwardDiff, Reexport
9-
using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap
10-
using Distributed
11-
using StaticArrays, LinearAlgebra, SparseArrays, LabelledArrays
12-
using InteractiveUtils
13-
using Latexify, Unitful, ArrayInterface
14-
using MacroTools
15-
@reexport using UnPack
16-
using Setfield, ConstructionBase
17-
using JumpProcesses
18-
using DataStructures
19-
using SpecialFunctions, NaNMath
20-
using RuntimeGeneratedFunctions
21-
using RuntimeGeneratedFunctions: drop_expr
22-
using Base.Threads
23-
using DiffEqCallbacks
24-
using Graphs
25-
import MacroTools: splitdef, combinedef, postwalk, striplines
26-
import Libdl
27-
using DocStringExtensions
28-
using Base: RefValue
29-
using Combinatorics
30-
import IfElse
31-
import Distributions
32-
import FunctionWrappersWrappers
33-
using URIs: URI
34-
5+
using PrecompileTools, Reexport
6+
@recompile_invalidations begin
7+
using DocStringExtensions
8+
using Compat
9+
using AbstractTrees
10+
using DiffEqBase, SciMLBase, ForwardDiff
11+
using SciMLBase: StandardODEProblem, StandardNonlinearProblem, handle_varmap
12+
using Distributed
13+
using StaticArrays, LinearAlgebra, SparseArrays, LabelledArrays
14+
using InteractiveUtils
15+
using Latexify, Unitful, ArrayInterface
16+
using MacroTools
17+
using Setfield, ConstructionBase
18+
using JumpProcesses
19+
using DataStructures
20+
using SpecialFunctions, NaNMath
21+
using RuntimeGeneratedFunctions
22+
using RuntimeGeneratedFunctions: drop_expr
23+
using Base.Threads
24+
using DiffEqCallbacks
25+
using Graphs
26+
import MacroTools: splitdef, combinedef, postwalk, striplines
27+
import Libdl
28+
using DocStringExtensions
29+
using Base: RefValue
30+
using Combinatorics
31+
import IfElse
32+
import Distributions
33+
import FunctionWrappersWrappers
34+
using URIs: URI
35+
36+
using RecursiveArrayTools
37+
38+
import SymbolicIndexingInterface
39+
import SymbolicIndexingInterface: independent_variables, states, parameters
40+
export independent_variables, states, parameters
41+
import SymbolicUtils
42+
import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype,
43+
Symbolic, isadd, ismul, ispow, issym, FnType,
44+
@rule, Rewriters, substitute, metadata, BasicSymbolic,
45+
Sym, Term
46+
using SymbolicUtils.Code
47+
import SymbolicUtils.Code: toexpr
48+
import SymbolicUtils.Rewriters: Chain, Postwalk, Prewalk, Fixpoint
49+
import JuliaFormatter
50+
51+
using MLStyle
52+
53+
using Reexport
54+
using Symbolics: degree
55+
@reexport using Symbolics
56+
using Symbolics: _parse_vars, value, @derivatives, get_variables,
57+
exprs_occur_in, solve_for, build_expr, unwrap, wrap,
58+
VariableSource, getname, variable, Connection, connect,
59+
NAMESPACE_SEPARATOR
60+
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
61+
jacobian_sparsity, isaffine, islinear, _iszero, _isone,
62+
tosymbol, lower_varname, diff2term, var_from_nested_derivative,
63+
BuildTargets, JuliaTarget, StanTarget, CTarget, MATLABTarget,
64+
ParallelForm, SerialForm, MultithreadedForm, build_function,
65+
rhss, lhss, prettify_expr, gradient,
66+
jacobian, hessian, derivative, sparsejacobian, sparsehessian,
67+
substituter, scalarize, getparent
68+
69+
import DiffEqBase: @add_kwonly
70+
71+
import Graphs: SimpleDiGraph, add_edge!, incidence_matrix
72+
73+
@reexport using UnPack
74+
end
3575
RuntimeGeneratedFunctions.init(@__MODULE__)
3676

37-
using RecursiveArrayTools
38-
39-
import SymbolicIndexingInterface
40-
import SymbolicIndexingInterface: independent_variables, states, parameters
41-
export independent_variables, states, parameters
42-
import SymbolicUtils
43-
import SymbolicUtils: istree, arguments, operation, similarterm, promote_symtype,
44-
Symbolic, isadd, ismul, ispow, issym, FnType,
45-
@rule, Rewriters, substitute, metadata, BasicSymbolic,
46-
Sym, Term
47-
using SymbolicUtils.Code
48-
import SymbolicUtils.Code: toexpr
49-
import SymbolicUtils.Rewriters: Chain, Postwalk, Prewalk, Fixpoint
50-
import JuliaFormatter
51-
52-
using MLStyle
53-
54-
using Reexport
55-
using Symbolics: degree
56-
@reexport using Symbolics
5777
export @derivatives
58-
using Symbolics: _parse_vars, value, @derivatives, get_variables,
59-
exprs_occur_in, solve_for, build_expr, unwrap, wrap,
60-
VariableSource, getname, variable, Connection, connect,
61-
NAMESPACE_SEPARATOR
62-
import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
63-
jacobian_sparsity, isaffine, islinear, _iszero, _isone,
64-
tosymbol, lower_varname, diff2term, var_from_nested_derivative,
65-
BuildTargets, JuliaTarget, StanTarget, CTarget, MATLABTarget,
66-
ParallelForm, SerialForm, MultithreadedForm, build_function,
67-
rhss, lhss, prettify_expr, gradient,
68-
jacobian, hessian, derivative, sparsejacobian, sparsehessian,
69-
substituter, scalarize, getparent
70-
71-
import DiffEqBase: @add_kwonly
72-
73-
import Graphs: SimpleDiGraph, add_edge!, incidence_matrix
7478

7579
for fun in [:toexpr]
7680
@eval begin

src/structural_transformation/partial_state_selection.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ function dummy_derivative_graph!(state::TransformationState, jac = nothing;
175175
dummy_derivative_graph!(state.structure, var_eq_matching, jac, state_priority, log)
176176
end
177177

178-
function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac,
179-
state_priority, ::Val{log} = Val(false)) where {log}
178+
function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, jac = nothing,
179+
state_priority = nothing, ::Val{log} = Val(false)) where {log}
180180
@unpack eq_to_diff, var_to_diff, graph = structure
181181
diff_to_eq = invview(eq_to_diff)
182182
diff_to_var = invview(var_to_diff)

0 commit comments

Comments
 (0)