Skip to content

Commit 12cf512

Browse files
committed
post-merge
2 parents ae4e6f7 + baefe85 commit 12cf512

File tree

86 files changed

+4870
-2107
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+4870
-2107
lines changed

.github/workflows/Downstream.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ jobs:
3737
- {user: SciML, repo: MethodOfLines.jl, group: Interface}
3838
- {user: SciML, repo: MethodOfLines.jl, group: 2D_Diffusion}
3939
- {user: SciML, repo: MethodOfLines.jl, group: DAE}
40-
- {user: ai4energy, repo: Ai4EComponentLib.jl, group: Downstream}
4140
steps:
4241
- uses: actions/checkout@v4
4342
- uses: julia-actions/setup-julia@v1

.github/workflows/Tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
- Extensions
3939
- Downstream
4040
- RegressionI
41+
- FMI
4142
uses: "SciML/.github/.github/workflows/tests.yml@v1"
4243
with:
4344
julia-version: "${{ matrix.version }}"

Project.toml

Lines changed: 8 additions & 5 deletions
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 = "9.61.0"
4+
version = "9.64.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -64,6 +64,7 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
6464
BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
6565
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
6666
DeepDiffs = "ab62b9b5-e342-54a8-a765-a90f495de1a6"
67+
FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac"
6768
HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327"
6869
InfiniteOpt = "20393b10-9daf-11e9-18c9-8db751c92c57"
6970
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
@@ -72,6 +73,7 @@ LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
7273
MTKBifurcationKitExt = "BifurcationKit"
7374
MTKChainRulesCoreExt = "ChainRulesCore"
7475
MTKDeepDiffsExt = "DeepDiffs"
76+
MTKFMIExt = "FMI"
7577
MTKHomotopyContinuationExt = "HomotopyContinuation"
7678
MTKInfiniteOptExt = "InfiniteOpt"
7779
MTKLabelledArraysExt = "LabelledArrays"
@@ -106,6 +108,7 @@ FindFirstFunctions = "1"
106108
ForwardDiff = "0.10.3"
107109
FunctionWrappers = "1.1"
108110
FunctionWrappersWrappers = "0.1"
111+
FMI = "0.14"
109112
Graphs = "1.5.2"
110113
HomotopyContinuation = "2.11"
111114
InfiniteOpt = "0.5"
@@ -132,7 +135,7 @@ RecursiveArrayTools = "3.26"
132135
Reexport = "0.2, 1"
133136
RuntimeGeneratedFunctions = "0.5.9"
134137
SCCNonlinearSolve = "1.0.0"
135-
SciMLBase = "2.71.1"
138+
SciMLBase = "2.73"
136139
SciMLStructures = "1.0"
137140
Serialization = "1"
138141
Setfield = "0.7, 0.8, 1"
@@ -142,9 +145,9 @@ SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0, 2"
142145
StaticArrays = "0.10, 0.11, 0.12, 1.0"
143146
StochasticDiffEq = "6.72.1"
144147
StochasticDelayDiffEq = "1.8.1"
145-
SymbolicIndexingInterface = "0.3.36"
146-
SymbolicUtils = "3.10"
147-
Symbolics = "6.22.1"
148+
SymbolicIndexingInterface = "0.3.37"
149+
SymbolicUtils = "3.14"
150+
Symbolics = "6.29"
148151
URIs = "1"
149152
UnPack = "0.1, 1.0"
150153
Unitful = "1.1"

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
1515
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
1616
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
1717
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
18+
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
1819
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
1920
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
2021
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
@@ -38,6 +39,7 @@ Optimization = "3.9, 4"
3839
OptimizationOptimJL = "0.1, 0.4"
3940
OrdinaryDiffEq = "6.31"
4041
Plots = "1.36"
42+
PreallocationTools = "0.4"
4143
SciMLStructures = "1.1"
4244
Setfield = "1"
4345
StochasticDiffEq = "6"

docs/pages.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pages = [
4141
"systems/JumpSystem.md",
4242
"systems/NonlinearSystem.md",
4343
"systems/OptimizationSystem.md",
44-
"systems/PDESystem.md"],
44+
"systems/PDESystem.md",
45+
"systems/DiscreteSystem.md"],
4546
"comparison.md",
4647
"internals.md"
4748
]

docs/src/basics/Debugging.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ dsol = solve(dprob, Tsit5());
3535
Now we see that it crashed because `u1` decreased so much that it became negative and outside the domain of the `` function.
3636
We could have figured that out ourselves, but it is not always so obvious for more complex models.
3737

38+
Suppose we also want to validate that `u1 + u2 >= 2.0`. We can do this via the assertions functionality.
39+
40+
```@example debug
41+
@mtkbuild sys = ODESystem(eqs, t; defaults, assertions = [(u1 + u2 >= 2.0) => "Oh no!"])
42+
```
43+
44+
The assertions must be an iterable of pairs, where the first element is the symbolic condition and
45+
the second is a message to be logged when the condition fails. All assertions are added to the
46+
generated code and will cause the solver to reject steps that fail the assertions. For systems such
47+
as the above where the assertion is guaranteed to eventually fail, the solver will likely exit
48+
with a `dtmin` failure..
49+
50+
```@example debug
51+
prob = ODEProblem(sys, [], (0.0, 10.0))
52+
sol = solve(prob, Tsit5())
53+
```
54+
55+
We can use `debug_system` to log the failing assertions in each call to the RHS function.
56+
57+
```@repl debug
58+
dsys = debug_system(sys; functions = []);
59+
dprob = ODEProblem(dsys, [], (0.0, 10.0));
60+
dsol = solve(dprob, Tsit5());
61+
```
62+
63+
Note the logs containing the failed assertion and corresponding message. To temporarily disable
64+
logging in a system returned from `debug_system`, use `ModelingToolkit.ASSERTION_LOG_VARIABLE`.
65+
66+
```@repl debug
67+
dprob[ModelingToolkit.ASSERTION_LOG_VARIABLE] = false;
68+
solve(drob, Tsit5());
69+
```
70+
3871
```@docs
3972
debug_system
4073
```

docs/src/basics/MTKLanguage.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ equations.
2323

2424
`@mtkmodel` definition contains begin blocks of
2525

26+
- `@description`: for describing the whole system with a human-readable string
2627
- `@components`: for listing sub-components of the system
2728
- `@constants`: for declaring constants
2829
- `@defaults`: for passing `defaults` to ODESystem
@@ -42,20 +43,23 @@ using ModelingToolkit
4243
using ModelingToolkit: t
4344
4445
@mtkmodel ModelA begin
46+
@description "A component with parameters `k` and `k_array`."
4547
@parameters begin
4648
k
4749
k_array[1:2]
4850
end
4951
end
5052
5153
@mtkmodel ModelB begin
54+
@description "A component with parameters `p1` and `p2`."
5255
@parameters begin
5356
p1 = 1.0, [description = "Parameter of ModelB"]
5457
p2 = 1.0, [description = "Parameter of ModelB"]
5558
end
5659
end
5760
5861
@mtkmodel ModelC begin
62+
@description "A bigger system that contains many more things."
5963
@icon "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png"
6064
@constants begin
6165
c::Int = 1, [description = "Example constant."]
@@ -91,6 +95,10 @@ end
9195
end
9296
```
9397

98+
#### `@description`
99+
100+
A documenting `String` that summarizes and explains what the model is.
101+
94102
#### `@icon`
95103

96104
An icon can be embedded in 3 ways:

docs/src/basics/Variable_metadata.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
5454
5555
@variables i(t) [connect = Flow]
5656
@variables k(t) [connect = Stream]
57+
hasconnect(i)
58+
```
59+
60+
```@example connect
61+
getconnect(k)
5762
```
5863

5964
## Input or output
@@ -177,8 +182,46 @@ A variable can be marked `irreducible` to prevent it from being moved to an
177182
`observed` state. This forces the variable to be computed during solving so that
178183
it can be accessed in [callbacks](@ref events)
179184

180-
```julia
181-
@variable important_value [irreducible = true]
185+
```@example metadata
186+
@variables important_value [irreducible = true]
187+
isirreducible(important_value)
188+
```
189+
190+
## State Priority
191+
192+
When a model is structurally simplified, the algorithm will try to ensure that the variables with higher state priority become states of the system. A variable's state priority is a number set using the `state_priority` metadata.
193+
194+
```@example metadata
195+
@variables important_dof [state_priority = 10] unimportant_dof [state_priority = -2]
196+
state_priority(important_dof)
197+
```
198+
199+
## Units
200+
201+
Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions.
202+
203+
```@example metadata
204+
using DynamicQuantities
205+
@variables speed [unit = u"m/s"]
206+
hasunit(speed)
207+
```
208+
209+
```@example metadata
210+
getunit(speed)
211+
```
212+
213+
## Miscellaneous metadata
214+
215+
User-defined metadata can be added using the `misc` metadata. This can be queried
216+
using the `hasmisc` and `getmisc` functions.
217+
218+
```@example metadata
219+
@variables u [misc = :conserved_parameter] y [misc = [2, 4, 6]]
220+
hasmisc(u)
221+
```
222+
223+
```@example metadata
224+
getmisc(y)
182225
```
183226

184227
## Additional functions

docs/src/examples/remake.md

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,22 @@ parameters to optimize.
4545

4646
```@example Remake
4747
using SymbolicIndexingInterface: parameter_values, state_values
48-
using SciMLStructures: Tunable, replace, replace!
48+
using SciMLStructures: Tunable, canonicalize, replace, replace!
49+
using PreallocationTools
4950
5051
function loss(x, p)
5152
odeprob = p[1] # ODEProblem stored as parameters to avoid using global variables
5253
ps = parameter_values(odeprob) # obtain the parameter object from the problem
53-
ps = replace(Tunable(), ps, x) # create a copy with the values passed to the loss function
54+
diffcache = p[5]
55+
# get an appropriately typed preallocated buffer to store the `x` values in
56+
buffer = get_tmp(diffcache, x)
57+
# copy the current values to this buffer
58+
copyto!(buffer, canonicalize(Tunable(), ps)[1])
59+
# create a copy of the parameter object with the buffer
60+
ps = replace(Tunable(), ps, buffer)
61+
# set the updated values in the parameter object
62+
setter = p[4]
63+
setter(ps, x)
5464
# remake the problem, passing in our new parameter object
5565
newprob = remake(odeprob; p = ps)
5666
timesteps = p[2]
@@ -81,49 +91,22 @@ We can perform the optimization as below:
8191
```@example Remake
8292
using Optimization
8393
using OptimizationOptimJL
94+
using SymbolicIndexingInterface
8495
8596
# manually create an OptimizationFunction to ensure usage of `ForwardDiff`, which will
8697
# require changing the types of parameters from `Float64` to `ForwardDiff.Dual`
8798
optfn = OptimizationFunction(loss, Optimization.AutoForwardDiff())
99+
# function to set the parameters we are optimizing
100+
setter = setp(odeprob, [α, β, γ, δ])
101+
# `DiffCache` to avoid allocations
102+
diffcache = DiffCache(canonicalize(Tunable(), parameter_values(odeprob))[1])
88103
# parameter object is a tuple, to store differently typed objects together
89104
optprob = OptimizationProblem(
90-
optfn, rand(4), (odeprob, timesteps, data), lb = 0.1zeros(4), ub = 3ones(4))
105+
optfn, rand(4), (odeprob, timesteps, data, setter, diffcache),
106+
lb = 0.1zeros(4), ub = 3ones(4))
91107
sol = solve(optprob, BFGS())
92108
```
93109

94-
To identify which values correspond to which parameters, we can `replace!` them into the
95-
`ODEProblem`:
96-
97-
```@example Remake
98-
replace!(Tunable(), parameter_values(odeprob), sol.u)
99-
odeprob.ps[[α, β, γ, δ]]
100-
```
101-
102-
`replace!` operates in-place, so the values being replaced must be of the same type as those
103-
stored in the parameter object, or convertible to that type. For demonstration purposes, we
104-
can construct a loss function that uses `replace!`, and calculate gradients using
105-
`AutoFiniteDiff` rather than `AutoForwardDiff`.
106-
107-
```@example Remake
108-
function loss2(x, p)
109-
odeprob = p[1] # ODEProblem stored as parameters to avoid using global variables
110-
newprob = remake(odeprob) # copy the problem with `remake`
111-
# update the parameter values in-place
112-
replace!(Tunable(), parameter_values(newprob), x)
113-
timesteps = p[2]
114-
sol = solve(newprob, AutoTsit5(Rosenbrock23()); saveat = timesteps)
115-
truth = p[3]
116-
data = Array(sol)
117-
return sum((truth .- data) .^ 2) / length(truth)
118-
end
119-
120-
# use finite-differencing to calculate derivatives
121-
optfn2 = OptimizationFunction(loss2, Optimization.AutoFiniteDiff())
122-
optprob2 = OptimizationProblem(
123-
optfn2, rand(4), (odeprob, timesteps, data), lb = 0.1zeros(4), ub = 3ones(4))
124-
sol = solve(optprob2, BFGS())
125-
```
126-
127110
# Re-creating the problem
128111

129112
There are multiple ways to re-create a problem with new state/parameter values. We will go

docs/src/systems/DiscreteSystem.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# DiscreteSystem
2+
3+
## System Constructors
4+
5+
```@docs
6+
DiscreteSystem
7+
```
8+
9+
## Composition and Accessor Functions
10+
11+
- `get_eqs(sys)` or `equations(sys)`: The equations that define the discrete system.
12+
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the discrete system.
13+
- `get_ps(sys)` or `parameters(sys)`: The parameters of the discrete system.
14+
- `get_iv(sys)`: The independent variable of the discrete system
15+
- `discrete_events(sys)`: The set of discrete events in the discrete system.
16+
17+
## Transformations
18+
19+
```@docs; canonical=false
20+
structural_simplify
21+
```
22+
23+
## Problem Constructors
24+
25+
```@docs; canonical=false
26+
DiscreteProblem(sys::DiscreteSystem, u0map, tspan)
27+
DiscreteFunction(sys::DiscreteSystem, args...)
28+
```

0 commit comments

Comments
 (0)