Skip to content

Commit 2307296

Browse files
test a non-parameterized function
1 parent 6478139 commit 2307296

File tree

5 files changed

+41
-28
lines changed

5 files changed

+41
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
Manifest.toml

Project.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ version = "0.1.0"
55
[deps]
66
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
77
MATLAB = "10e44e05-a98a-55b3-a45b-ba969058deb6"
8+
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
89
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
910

1011
[compat]
11-
Reexport = "0.2"
1212
DiffEqBase = "6.5"
1313
MATLAB = "0.7"
14+
ModelingToolkit = "1.4.2"
15+
Reexport = "0.2"
1416
julia = "1"
1517

1618
[extras]

README.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ Pkg.add("MATLABDiffEq")
2323

2424
## Using MATLABDiffEq.jl
2525

26-
MATLABDiffEq.jl is simply a solver on the DiffEq common interface, so for details see the [DifferentialEquations.jl documentation](https://juliadiffeq.github.io/DiffEqDocs.jl/dev/). However, there are three things to know:
26+
MATLABDiffEq.jl is simply a solver on the DiffEq common interface, so for details see the [DifferentialEquations.jl documentation](https://juliadiffeq.github.io/DiffEqDocs.jl/dev/).
27+
However, the only options implemented are those for error calculations
28+
(`timeseries_error`), `saveat` and tolerances.
2729

28-
1. The only options implemented are those for error calculations (`timeseries_error`), `saveat` and tolerances.
29-
2. The input function must be defined by a `ParameterizedFunction`
30-
3. The input function must not use parameters
31-
32-
Note that the algorithms are defined to have the same name as the MATLAB algorithms, but are not exported. Thus to use `ode45`, you would specify the algorithm as `MATLABDiffEq.ode45()`.
30+
Note that the algorithms are defined to have the same name as the MATLAB algorithms,
31+
but are not exported. Thus to use `ode45`, you would specify the algorithm as
32+
`MATLABDiffEq.ode45()`.
3333

3434
## Example
3535

3636
```julia
3737
using MATLABDiffEq, ParameterizedFunctions
3838

39-
4039
f = @ode_def LotkaVolterra begin
4140
dx = 1.5x - x*y
4241
dy = -3y + x*y
@@ -46,6 +45,16 @@ tspan = (0.0,10.0)
4645
u0 = [1.0,1.0]
4746
prob = ODEProblem(f,u0,tspan)
4847
sol = solve(prob,MATLABDiffEq.ode45())
48+
49+
function lorenz(du,u,p,t)
50+
du[1] = 10.0(u[2]-u[1])
51+
du[2] = u[1]*(28.0-u[3]) - u[2]
52+
du[3] = u[1]*u[2] - (8/3)*u[3]
53+
end
54+
u0 = [1.0;0.0;0.0]
55+
tspan = (0.0,100.0)
56+
prob = ODEProblem(lorenz,u0,tspan)
57+
sol = solve(prob,MATLABDiffEq.ode45())
4958
```
5059

5160
## Measuring Overhead
@@ -56,7 +65,7 @@ is done. Thus you can simply call the same ODE function and time it
5665
directly. This is done by:
5766

5867
```julia
59-
@time MATLABDiffEq.eval_string("[t,u] = $(algstr)(f,tspan,u0,options);")
68+
@time MATLABDiffEq.eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);")
6069
```
6170

6271
To be even more pedantic, you can play around in the actual MATLAB
@@ -97,16 +106,16 @@ julia> @time sol = solve(prob,alg);
97106
julia> @time sol = solve(prob,alg);
98107
0.065460 seconds (38.84 k allocations: 1.556 MB)
99108

100-
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(f,tspan,u0,options);")
109+
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);")
101110
0.058249 seconds (11 allocations: 528 bytes)
102111

103-
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(f,tspan,u0,options);")
112+
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);")
104113
0.060367 seconds (11 allocations: 528 bytes)
105114

106-
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(f,tspan,u0,options);")
115+
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);")
107116
0.060171 seconds (11 allocations: 528 bytes)
108117

109-
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(f,tspan,u0,options);")
118+
julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);")
110119
0.058928 seconds (11 allocations: 528 bytes)
111120
```
112121

src/MATLABDiffEq.jl

+7-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module MATLABDiffEq
22

33
using Reexport
44
@reexport using DiffEqBase
5-
using MATLAB
5+
using MATLAB, ModelingToolkit
66

77
abstract type MATLABAlgorithm <: DiffEqBase.AbstractODEAlgorithm end
88
struct ode23 <: MATLABAlgorithm end
@@ -22,10 +22,6 @@ function DiffEqBase.__solve(
2222

2323
tType = eltype(tupType)
2424

25-
if !(typeof(prob.f) <: DiffEqBase.AbstractParameterizedFunction)
26-
error("Functions must be defined via ParameterizedFunctions.jl to work with this package.")
27-
end
28-
2925
if prob.tspan[end]-prob.tspan[1]<tType(0)
3026
error("final time must be greater than starting time. Aborting.")
3127
end
@@ -43,15 +39,11 @@ function DiffEqBase.__solve(
4339
u0 = prob.u0
4440
end
4541

46-
strs = [string(f.fex.args[2i].args[2]) for i in 1:length(f.syms)]
47-
matstr = ""
48-
for i in 1:length(strs)
49-
matstr *= strs[i]
50-
i < length(strs) && (matstr *= "; ")
51-
end
52-
matstr = replace(matstr,"["=>"(")
53-
matstr = replace(matstr,"]"=>")")
54-
matstr = "f = @(t,internal_var___u) ["*matstr*"];"
42+
sys = first(modelingtoolkitize(prob))
43+
44+
matstr = ModelingToolkit.build_function(sys.eqs,sys.dvs,
45+
sys.ps,sys.iv,
46+
target = ModelingToolkit.MATLABTarget())
5547

5648
# Send the variables
5749
put_variable(get_default_msession(),:tspan,tspan)
@@ -66,7 +58,7 @@ function DiffEqBase.__solve(
6658
eval_string("options = odeset('RelTol',reltol,'AbsTol',abstol);")
6759
algstr = string(typeof(alg).name.name)
6860
#algstr = replace(string(typeof(alg)),"MATLABDiffEq.","")
69-
eval_string("[t,u] = $(algstr)(f,tspan,u0,options);")
61+
eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);")
7062
ts = jvector(get_mvariable(:t))
7163
timeseries_tmp = jarray(get_mvariable(:u))
7264

test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ p = [1.5,1,3,1]
88
tspan = (0.0,10.0)
99
u0 = [1.0,1.0]
1010
prob = ODEProblem(f,u0,tspan,p)
11+
sol = solve(prob,MATLABDiffEq.ode45())
1112

13+
function lorenz(du,u,p,t)
14+
du[1] = 10.0(u[2]-u[1])
15+
du[2] = u[1]*(28.0-u[3]) - u[2]
16+
du[3] = u[1]*u[2] - (8/3)*u[3]
17+
end
18+
u0 = [1.0;0.0;0.0]
19+
tspan = (0.0,100.0)
20+
prob = ODEProblem(lorenz,u0,tspan)
1221
sol = solve(prob,MATLABDiffEq.ode45())
1322

1423
algs = [MATLABDiffEq.ode23

0 commit comments

Comments
 (0)