Skip to content

Commit 09728d7

Browse files
authored
Merge pull request #168 from SciML/MTK4_updates
MTK 4 Update
2 parents 13621fb + 6fc7b9b commit 09728d7

23 files changed

+468
-481
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
88
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"
99
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
1010
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
11+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1112
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
1213
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1314
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
@@ -22,8 +23,9 @@ Compat = "2.2, 3.0"
2223
DSP = "0.6"
2324
DataInterpolations = "3.1"
2425
DiffEqBase = "6.45"
26+
DocStringExtensions = "0.7, 0.8"
2527
FiniteDifferences = "0.9.6, 0.10"
26-
ModelingToolkit = "3.17"
28+
ModelingToolkit = "4"
2729
ProximalOperators = "0.11, 0.12"
2830
QuadGK = "2.4"
2931
StatsBase = "0.32.0, 0.33"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ end
5454
################################################################
5555

5656
@variables x y z
57-
u = Operation[x; y; z]
58-
polys = Operation[]
57+
u = [x; y; z]
58+
polys = Any[]
5959
for i 0:4
6060
for j 0:i
6161
for k 0:j

docs/src/basis.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ Many of the methods require the definition of a `Basis` on observables or
44
functional forms. A `Basis` is generated via:
55

66
```julia
7-
Basis(h, u, parameters = [], iv = nothing)
7+
Basis(eqs::AbstractVector, states::AbstractVector;
8+
parameters::AbstractArray = [], iv = nothing,
9+
simplify = false, linear_independent = false, name = gensym(:Basis),
10+
pins = [], observed = [], eval_expression = false,
11+
kwargs...)
812
```
13+
where `eqs` is either a vector containing symbolic functions using 'ModelingToolkit.jl' or a general function with the typical DiffEq signature `h(u,p,t)`, which can be used with an `Num` or vector of `Num`. `states` are the dependent variables used to describe the Basis, and
14+
`parameters` are the optional parameters in the `Basis`. `iv` represents the independent variable of the system - in most cases the time. Additional arguments are `simplify`, which simplifies `eqs` before creating a `Basis`. `linear_dependent` breaks up `eqs` in linear independent elements which are unique. `name` is an optional name for the `Basis`, `pins` and `observed` can be using in accordance to ModelingToolkits documentation. `eval_expression` is used to generate a callable function from the eqs. If set to `false`, callable code will be returned. `true` will use `eval` on code returned from the function, which might cause worldage issues.
915

10-
where `h` is either a vector of ModelingToolkit `Operation`s for the valid functional
11-
forms or a general function with the typical DiffEq signature `h(u,p,t)`, which can be used with an `Operation` or vector of `Operation`. `u` are the ModelingToolkit `Variable`s used to describe the Basis, and
12-
`parameters` are the optional ModelingToolkit `Variable`s used to describe the
13-
parameters in the basis elements. `iv` represents the independent variable of the system - the time.
1416

1517
```@docs
1618
Basis
1719
```
1820

1921
## Example
2022

21-
We start by crearting some `Variables` and `Parameters` using `ModelingToolkit`.
23+
We start by crearting some variables and parameters using `ModelingToolkit`.
2224
```@example basis
2325
using LinearAlgebra
2426
using DataDrivenDiffEq
@@ -29,14 +31,11 @@ using ModelingToolkit
2931
@parameters w[1:2]
3032
```
3133

32-
To define a basis, simply write down the equations you want to be included as a
33-
`Vector{Operation}`. Possible used parameters have to be given to the constructor.
34+
To define a basis, simply write down the equations you want to be included as a `Vector`. Possible used parameters have to be given to the constructor.
3435
```@example basis
3536
h = [u[1]; u[2]; cos(w[1]*u[2]+w[2]*u[3])]
3637
b = Basis(h, u, parameters = w)
3738
```
38-
What can a `Basis` do? Can it do stuff? Let's find out!
39-
4039
`Basis` are callable with the signature of functions to be used in `DifferentialEquations`.
4140
So, the function value at a single point looks like:
4241
```@example basis
@@ -70,7 +69,7 @@ push!(b, sin(u[1]))
7069
size(b)
7170
```
7271

73-
We can also define functions of time and add them
72+
We can also define functions of the independent variable and add them
7473

7574
```@example basis
7675
t = independent_variable(b)
@@ -116,11 +115,11 @@ b_f = Basis(f, u, parameters = w)
116115
println(b_f)
117116
```
118117

119-
This works for every function defined over `Operation`s. So to create a `Basis` from a `Flux` model, simply extend the activations used:
118+
This works for every function defined over `Num`s. So to create a `Basis` from a `Flux` model, simply extend the activations used:
120119

121120
```julia
122121
using Flux
123-
NNlib.σ(x::Operation) = 1 / (1+exp(-x))
122+
NNlib.σ(x::Num) = 1 / (1+exp(-x))
124123

125124
c = Chain(Dense(3,2,σ), Dense(2, 1, σ))
126125
ps, re = Flux.destructure(c)
@@ -135,9 +134,6 @@ b = Basis(g, u, parameters = p)
135134
## Functions
136135

137136
```@docs
138-
parameters
139-
variables
140-
DataDrivenDiffEq.independent_variable
141137
jacobian
142138
dynamics
143139
push!

docs/src/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ So, let's create a bunch of basis functions for our problem first
238238
239239
@variables u[1:2]
240240
241-
h = Operation[u; u.^2; u.^3; sin.(u); cos.(u); 1]
241+
h = [u; u.^2; u.^3; sin.(u); cos.(u); 1]
242242
243243
basis = Basis(h, u)
244244
nothing # hide

docs/src/sparse_identification/isindy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ As before, we will also need a `Basis` to derive our equations from:
135135

136136
```@example iSINDy_2
137137
@variables u[1:4] t
138-
polys = Operation[]
138+
polys = Any[]
139139
for i ∈ 0:4
140140
if i == 0
141141
push!(polys, u[1]^0)
@@ -198,7 +198,7 @@ Alternatively, we can also use the input as an extended state `x`.
198198

199199
```@example iSINDy_2
200200
@variables u[1:4] t x
201-
polys = Operation[]
201+
polys = Any[]
202202
# Lots of basis functions -> sindy pi can handle more than ADM()
203203
for i ∈ 0:4
204204
if i == 0

docs/src/sparse_identification/sindy.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ To generate the symbolic equations, we need to define a ` Basis` over the variab
5959

6060
```@example SINDy_1
6161
@variables x y z
62-
u = Operation[x; y; z]
63-
polys = Operation[]
62+
u = [x; y; z]
63+
polys = Any[]
6464
for i ∈ 0:4
6565
for j ∈ 0:i
6666
for k ∈ 0:j

examples/Basis_Creation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ f(u, p, t) = [u[3]; u[2]*u[1]; sin(u[1])*u[2]]
7777
b = Basis(f, u)
7878

7979
using Flux
80-
NNlib.σ(x::Operation) = 1 / (1+exp(-x))
80+
NNlib.σ(x::Num) = 1 / (1+exp(-x))
8181
# Build a fully
8282
c = Chain(Dense(3,5,σ), Dense(5, 2, σ))
8383
ps, re = Flux.destructure(c)

examples/DMDc_Examples.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ eigvecs(sys)
2121
prob = DiscreteProblem(sys, X[:, 1], (0., 10.))
2222
sol_unforced = solve(prob, FunctionMap())
2323
plot(sol_unforced)
24-
sol_unforced[:,:]

examples/ISInDy_Examples.jl

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,7 @@ end
2626

2727
# Create a basis
2828
@variables u[1:3]
29-
polys = Operation[]
30-
# Lots of basis functions
31-
for i 0:6
32-
if i == 0
33-
push!(polys, u[1]^0)
34-
end
35-
for ui in u
36-
if i > 0
37-
push!(polys, ui^i)
38-
end
39-
end
40-
end
29+
polys = [monomial_basis(u, 6);1]
4130

4231
basis= Basis(polys, u)
4332
Ψ = ISINDy(X, DX, basis, ADM(1e-2), maxiter = 10, rtol = 0.1)
@@ -47,24 +36,14 @@ print_equations(Ψ)
4736
# Lets use SINDy pi for a parallel implicit implementation
4837
# Create a basis
4938
@variables u[1:3]
50-
polys = Operation[]
51-
# Lots of basis functions -> SINDy pi can handle more than ADM()
52-
for i 0:10
53-
if i == 0
54-
push!(polys, u[1]^0)
55-
end
56-
for ui in u
57-
if i > 0
58-
push!(polys, ui^i)
59-
end
60-
end
61-
end
39+
polys = [monomial_basis(u, 10); 1]
6240

6341
basis= Basis(polys, u)
6442

6543
# Simply use any optimizer you would use for SINDy
6644
Ψ = ISINDy(X[:, :], DX[:, :], basis, STRRidge(1e-1), maxiter = 100, normalize = true)
67-
45+
println(Ψ)
46+
print_equations(Ψ)
6847

6948
# Transform into ODE System
7049
sys = ODESystem(Ψ)
@@ -80,4 +59,4 @@ plot(sol.t[:], sol[:,:]', color = :red, label = nothing)
8059
plot!(sol_.t, sol_[:, :]', color = :green, label = "Estimation")
8160

8261
plot(sol.t, abs.(sol-sol_)')
83-
norm(sol[:,:]-sol_[:,:], 2) # approx 9e-7
62+
norm(sol[:,:]-sol_[:,:], 2) # approx 1.2e-13

examples/ISInDy_Examples2.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ end
3535
plot(DX')
3636

3737
@variables u[1:4] t
38-
polys = Operation[]
38+
polys = Any[]
3939
# Lots of basis functions -> sindy pi can handle more than ADM()
4040
for i 0:4
4141
if i == 0

0 commit comments

Comments
 (0)