Skip to content

Commit b70b6b1

Browse files
committed
Merge remote-tracking branch 'origin' into implicit_discrete_system
merge master
2 parents 397298f + a2db412 commit b70b6b1

Some content is hidden

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

49 files changed

+1205
-710
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

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ StaticArrays = "0.10, 0.11, 0.12, 1.0"
146146
StochasticDiffEq = "6.72.1"
147147
StochasticDelayDiffEq = "1.8.1"
148148
SymbolicIndexingInterface = "0.3.37"
149-
SymbolicUtils = "3.10.1"
150-
Symbolics = "6.25"
149+
SymbolicUtils = "3.14"
150+
Symbolics = "6.29"
151151
URIs = "1"
152152
UnPack = "0.1, 1.0"
153153
Unitful = "1.1"

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: 44 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,45 @@ 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+
@variables speed [unit = u"m/s"]
205+
hasunit(speed)
206+
```
207+
208+
```@example metadata
209+
getunit(speed)
210+
```
211+
212+
## Miscellaneous metadata
213+
214+
User-defined metadata can be added using the `misc` metadata. This can be queried
215+
using the `hasmisc` and `getmisc` functions.
216+
217+
```@example metadata
218+
@variables u [misc = :conserved_parameter] y [misc = [2, 4, 6]]
219+
hasmisc(u)
220+
```
221+
222+
```@example metadata
223+
getmisc(y)
182224
```
183225

184226
## Additional functions

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+
```

docs/src/tutorials/acausal_components.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ end
8484
end
8585
8686
@mtkmodel RCModel begin
87+
@description "A circuit with a constant voltage source, resistor and capacitor connected in series."
8788
@components begin
8889
resistor = Resistor(R = 1.0)
8990
capacitor = Capacitor(C = 1.0)
@@ -251,6 +252,7 @@ make all of our parameter values 1.0. As `resistor`, `capacitor`, `source` lists
251252

252253
```@example acausal
253254
@mtkmodel RCModel begin
255+
@description "A circuit with a constant voltage source, resistor and capacitor connected in series."
254256
@components begin
255257
resistor = Resistor(R = 1.0)
256258
capacitor = Capacitor(C = 1.0)

docs/src/tutorials/initialization.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ principles of initialization of DAE systems. Take a DAE written in semi-explicit
1414
form:
1515

1616
```math
17-
x' = f(x,y,t)\\
18-
0 = g(x,y,t)
17+
\begin{aligned}
18+
x^\prime &= f(x,y,t) \\
19+
0 &= g(x,y,t)
20+
\end{aligned}
1921
```
2022

2123
where ``x`` are the differential variables and ``y`` are the algebraic variables.

src/ModelingToolkit.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ include("systems/connectors.jl")
151151
include("systems/analysis_points.jl")
152152
include("systems/imperative_affect.jl")
153153
include("systems/callbacks.jl")
154+
include("systems/codegen_utils.jl")
154155
include("systems/problem_utils.jl")
155156

156157
include("systems/nonlinear/nonlinearsystem.jl")
@@ -249,7 +250,9 @@ export initial_state, transition, activeState, entry, ticksInState, timeInState
249250
export @component, @mtkmodel, @mtkbuild
250251
export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance,
251252
istunable, getdist, hasdist,
252-
tunable_parameters, isirreducible, getdescription, hasdescription
253+
tunable_parameters, isirreducible, getdescription, hasdescription,
254+
hasunit, getunit, hasconnect, getconnect,
255+
hasmisc, getmisc, state_priority
253256
export ode_order_lowering, dae_order_lowering, liouville_transform
254257
export PDESystem
255258
export Differential, expand_derivatives, @derivatives

0 commit comments

Comments
 (0)