Skip to content

Commit a6c852b

Browse files
committed
fix: fix some tests
2 parents 7b646ab + c412cd7 commit a6c852b

27 files changed

+634
-105
lines changed

Project.toml

Lines changed: 5 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.64.2"
4+
version = "9.64.3"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -23,7 +23,6 @@ DomainSets = "5b8099bc-c8ec-5219-889f-1d9e522a28bf"
2323
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
2424
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
2525
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
26-
Expronicon = "6b7a57c9-7cc1-4fdf-b7f5-e857abae3636"
2726
FindFirstFunctions = "64ca27bc-2ba2-4a57-88aa-44e436879224"
2827
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2928
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
@@ -36,6 +35,7 @@ Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
3635
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
3736
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
3837
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
38+
Moshi = "2e0e35c7-a2e4-4343-998d-7ef72827ed2d"
3939
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
4040
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
4141
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
@@ -103,7 +103,6 @@ DomainSets = "0.6, 0.7"
103103
DynamicQuantities = "^0.11.2, 0.12, 0.13, 1"
104104
EnumX = "1.0.4"
105105
ExprTools = "0.1.10"
106-
Expronicon = "0.8"
107106
FMI = "0.14"
108107
FindFirstFunctions = "1"
109108
ForwardDiff = "0.10.3"
@@ -121,6 +120,7 @@ LinearAlgebra = "1"
121120
Logging = "1"
122121
MLStyle = "0.4.17"
123122
ModelingToolkitStandardLibrary = "2.19"
123+
Moshi = "0.3"
124124
NaNMath = "0.3, 1"
125125
NonlinearSolve = "4.3"
126126
OffsetArrays = "1"
@@ -135,7 +135,7 @@ RecursiveArrayTools = "3.26"
135135
Reexport = "0.2, 1"
136136
RuntimeGeneratedFunctions = "0.5.9"
137137
SCCNonlinearSolve = "1.0.0"
138-
SciMLBase = "2.73"
138+
SciMLBase = "2.75"
139139
SciMLStructures = "1.0"
140140
Serialization = "1"
141141
Setfield = "0.7, 0.8, 1"
@@ -190,4 +190,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
190190
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
191191

192192
[targets]
193-
test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging"]
193+
test = ["AmplNLWriter", "BenchmarkTools", "BoundaryValueDiffEqMIRK", "BoundaryValueDiffEqAscher", "ControlSystemsBase", "DataInterpolations", "DelayDiffEq", "NonlinearSolve", "ForwardDiff", "Ipopt", "Ipopt_jll", "ModelingToolkitStandardLibrary", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "OrdinaryDiffEqCore", "OrdinaryDiffEqDefault", "REPL", "Random", "ReferenceTests", "SafeTestsets", "StableRNGs", "Statistics", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials", "StochasticDelayDiffEq", "Pkg", "JET", "OrdinaryDiffEqNonlinearSolve", "Logging"]

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
66
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
77
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
88
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
9+
FMI = "14a09403-18e3-468f-ad8a-74f8dda2d9ac"
10+
FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195"
911
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1012
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1113
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"

docs/pages.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pages = [
1414
"tutorials/SampledData.md",
1515
"tutorials/domain_connections.md",
1616
"tutorials/callable_params.md",
17-
"tutorials/linear_analysis.md"],
17+
"tutorials/linear_analysis.md",
18+
"tutorials/fmi.md"],
1819
"Examples" => Any[
1920
"Basic Examples" => Any["examples/higher_order.md",
2021
"examples/spring_mass.md",

docs/src/basics/DependencyGraphs.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ asdigraph
2222
eqeq_dependencies
2323
varvar_dependencies
2424
```
25+
26+
# Miscellaneous
27+
28+
```@docs
29+
map_variables_to_equations
30+
```

docs/src/basics/Linearization.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The `linearize` function expects the user to specify the inputs ``u`` and the ou
2222
```@example LINEARIZE
2323
using ModelingToolkit
2424
using ModelingToolkit: t_nounits as t, D_nounits as D
25-
@variables x(t)=0 y(t)=0 u(t)=0 r(t)=0
25+
@variables x(t)=0 y(t) u(t) r(t)=0
2626
@parameters kp = 1
2727
2828
eqs = [u ~ kp * (r - y) # P controller
@@ -43,7 +43,7 @@ using ModelingToolkit: inputs, outputs
4343

4444
!!! note "Inputs must be unconnected"
4545

46-
The model above has 4 variables but only three equations, there is no equation specifying the value of `r` since `r` is an input. This means that only unbalanced models can be linearized, or in other words, models that are balanced and can be simulated _cannot_ be linearized. To learn more about this, see [How to linearize a ModelingToolkit model (YouTube)](https://www.youtube.com/watch?v=-XOux-2XDGI&t=395s). Also see [ModelingToolkitStandardLibrary: Linear analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/) for utilities that make linearization of completed models easier.
46+
The model above has 4 variables but only three equations, there is no equation specifying the value of `r` since `r` is an input. This means that only unbalanced models can be linearized, or in other words, models that are balanced and can be simulated _cannot_ be linearized. To learn more about this, see [How to linearize a ModelingToolkit model (YouTube)](https://www.youtube.com/watch?v=-XOux-2XDGI&t=395s). Also see [ModelingToolkitStandardLibrary: Linear analysis](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/linear_analysis/) for utilities that make linearization of completed models easier.
4747

4848
!!! note "Un-simplified system"
4949

@@ -57,6 +57,74 @@ The operating point to linearize around can be specified with the keyword argume
5757

5858
If linearization is to be performed around multiple operating points, the simplification of the system has to be carried out a single time only. To facilitate this, the lower-level function [`ModelingToolkit.linearization_function`](@ref) is available. This function further allows you to obtain separate Jacobians for the differential and algebraic parts of the model. For ODE models without algebraic equations, the statespace representation above is available from the output of `linearization_function` as `A, B, C, D = f_x, f_u, h_x, h_u`.
5959

60+
All variables that will be fixed by an operating point _must_ be provided in the operating point to `linearization_function`. For example, if the operating points fix the value of
61+
`x`, `y` and `z` then an operating point with constant values for these variables (e.g. `Dict(x => 1.0, y => 1.0, z => 1.0)`) must be provided. The constant values themselves
62+
do not matter and can be changed by subsequent operating points.
63+
64+
One approach to batch linearization would be to call `linearize` in a loop, providing a different operating point each time. For example:
65+
66+
```@example LINEARIZE
67+
using ModelingToolkitStandardLibrary
68+
using ModelingToolkitStandardLibrary.Blocks
69+
70+
@parameters k=10 k3=2 c=1
71+
@variables x(t)=0 [bounds = (-0.5, 1.5)]
72+
@variables v(t) = 0
73+
74+
@named y = Blocks.RealOutput()
75+
@named u = Blocks.RealInput()
76+
77+
eqs = [D(x) ~ v
78+
D(v) ~ -k * x - k3 * x^3 - c * v + 10u.u
79+
y.u ~ x]
80+
81+
@named duffing = ODESystem(eqs, t, systems = [y, u], defaults = [u.u => 0])
82+
83+
# pass a constant value for `x`, since it is the variable we will change in operating points
84+
linfun, simplified_sys = linearization_function(duffing, [u.u], [y.u]; op = Dict(x => NaN));
85+
86+
println(linearize(simplified_sys, linfun; op = Dict(x => 1.0)))
87+
println(linearize(simplified_sys, linfun; op = Dict(x => 0.0)))
88+
89+
@time linearize(simplified_sys, linfun; op = Dict(x => 0.0))
90+
91+
nothing # hide
92+
```
93+
94+
However, this route is still expensive since it has to repeatedly process the symbolic map provided to `op`. `linearize` is simply a wrapper for creating and solving a
95+
[`ModelingToolkit.LinearizationProblem`](@ref). This object is symbolically indexable, and can thus integrate with SymbolicIndexingInterface.jl for fast updates.
96+
97+
```@example LINEARIZE
98+
using SymbolicIndexingInterface
99+
100+
# The second argument is the value of the independent variable `t`.
101+
linprob = LinearizationProblem(linfun, 1.0)
102+
# It can be mutated
103+
linprob.t = 0.0
104+
# create a setter function to update `x` efficiently
105+
setter! = setu(linprob, x)
106+
107+
function fast_linearize!(problem, setter!, value)
108+
setter!(problem, value)
109+
solve(problem)
110+
end
111+
112+
println(fast_linearize!(linprob, setter!, 1.0))
113+
println(fast_linearize!(linprob, setter!, 0.0))
114+
115+
@time fast_linearize!(linprob, setter!, 1.0)
116+
117+
nothing # hide
118+
```
119+
120+
Note that `linprob` above can be interacted with similar to a normal `ODEProblem`.
121+
122+
```@repl LINEARIZE
123+
prob[x]
124+
prob[x] = 1.5
125+
prob[x]
126+
```
127+
60128
## Symbolic linearization
61129

62130
The function [`ModelingToolkit.linearize_symbolic`](@ref) works similar to [`ModelingToolkit.linearize`](@ref) but returns symbolic rather than numeric Jacobians. Symbolic linearization have several limitations and no all systems that can be linearized numerically can be linearized symbolically.
@@ -75,7 +143,7 @@ If the modeled system is actually proper (but MTK failed to find a proper realiz
75143

76144
## Tools for linear analysis
77145

78-
[ModelingToolkitStandardLibrary](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/) contains a set of [tools for more advanced linear analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/). These can be used to make it easier to work with and analyze causal models, such as control and signal-processing systems.
146+
ModelingToolkit contains a set of [tools for more advanced linear analysis](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/linear_analysis/). These can be used to make it easier to work with and analyze causal models, such as control and signal-processing systems.
79147

80148
Also see [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/dev/) for an interface to [ControlSystems.jl](https://github.com/JuliaControl/ControlSystems.jl) that contains tools for linear analysis and frequency-domain analysis.
81149

@@ -89,4 +157,5 @@ Pages = ["Linearization.md"]
89157
linearize
90158
ModelingToolkit.linearize_symbolic
91159
ModelingToolkit.linearization_function
160+
ModelingToolkit.LinearizationProblem
92161
```

docs/src/examples/remake.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ using SymbolicIndexingInterface
9898
optfn = OptimizationFunction(loss, Optimization.AutoForwardDiff())
9999
# function to set the parameters we are optimizing
100100
setter = setp(odeprob, [α, β, γ, δ])
101-
# `DiffCache` to avoid allocations
102-
diffcache = DiffCache(canonicalize(Tunable(), parameter_values(odeprob))[1])
101+
# `DiffCache` to avoid allocations.
102+
# `copy` prevents the buffer stored by `DiffCache` from aliasing the one in
103+
# `parameter_values(odeprob)`.
104+
diffcache = DiffCache(copy(canonicalize(Tunable(), parameter_values(odeprob))[1]))
103105
# parameter object is a tuple, to store differently typed objects together
104106
optprob = OptimizationProblem(
105107
optfn, rand(4), (odeprob, timesteps, data, setter, diffcache),

0 commit comments

Comments
 (0)