Skip to content

Commit b8d31f8

Browse files
Merge pull request #857 from mohamed82008/mt/fix_841
Fix #841
2 parents 1de2713 + c4fd2cd commit b8d31f8

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
2828
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
2929
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
3030
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
31+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
3132
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
3233
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3334
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ variable and parameter vectors, respectively.
199199
"""
200200
struct ODEFunctionExpr{iip} end
201201

202+
struct ODEFunctionClosure{O, I} <: Function
203+
f_oop::O
204+
f_iip::I
205+
end
206+
(f::ODEFunctionClosure)(u, p, t) = f.f_oop(u, p, t)
207+
(f::ODEFunctionClosure)(du, u, p, t) = f.f_iip(du, u, p, t)
208+
202209
function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = states(sys),
203210
ps = parameters(sys), u0 = nothing;
204211
version = nothing, tgrad=false,
@@ -209,20 +216,13 @@ function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = states(sys),
209216

210217
f_oop, f_iip = generate_function(sys, dvs, ps; expression=Val{true}, kwargs...)
211218
fsym = gensym(:f)
212-
_f = quote
213-
$fsym(u,p,t) = $f_oop(u,p,t)
214-
$fsym(du,u,p,t) = $f_iip(du,u,p,t)
215-
end
216-
219+
_f = :($fsym = ModelingToolkit.ODEFunctionClosure($f_oop, $f_iip))
217220
tgradsym = gensym(:tgrad)
218221
if tgrad
219222
tgrad_oop, tgrad_iip = generate_tgrad(sys, dvs, ps;
220223
simplify=simplify,
221224
expression=Val{true}, kwargs...)
222-
_tgrad = quote
223-
$tgradsym(u,p,t) = $tgrad_oop(u,p,t)
224-
$tgradsym(J,u,p,t) = $tgrad_iip(J,u,p,t)
225-
end
225+
_tgrad = :($tgradsym = ModelingToolkit.ODEFunctionClosure($tgrad_oop, $tgrad_iip))
226226
else
227227
_tgrad = :($tgradsym = nothing)
228228
end
@@ -232,10 +232,7 @@ function ODEFunctionExpr{iip}(sys::AbstractODESystem, dvs = states(sys),
232232
jac_oop,jac_iip = generate_jacobian(sys, dvs, ps;
233233
sparse=sparse, simplify=simplify,
234234
expression=Val{true}, kwargs...)
235-
_jac = quote
236-
$jacsym(u,p,t) = $jac_oop(u,p,t)
237-
$jacsym(J,u,p,t) = $jac_iip(J,u,p,t)
238-
end
235+
_jac = :($jacsym = ModelingToolkit.ODEFunctionClosure($jac_oop, $jac_iip))
239236
else
240237
_jac = :($jacsym = nothing)
241238
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ println("Last test requires gcc available in the path!")
3737
@safetestset "C Compilation Test" begin include("ccompile.jl") end
3838
@safetestset "Latexify recipes Test" begin include("latexify.jl") end
3939
@safetestset "StructuralTransformations" begin include("structural_transformation/runtests.jl") end
40+
@testset "Serialization" begin include("serialization.jl") end

test/serialization.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using ModelingToolkit, SciMLBase, Serialization
2+
3+
@parameters t
4+
@variables x(t)
5+
D = Differential(t)
6+
7+
sys = ODESystem([D(x) ~ -0.5*x])
8+
for prob in [
9+
eval(ModelingToolkit.ODEProblem{false}(sys, nothing, nothing, SciMLBase.NullParameters())),
10+
eval(ModelingToolkit.ODEProblemExpr{false}(sys, nothing, nothing, SciMLBase.NullParameters()))
11+
]
12+
_fn = tempname()
13+
14+
open(_fn, "w") do f
15+
serialize(f, prob)
16+
end
17+
18+
_cmd = "using ModelingToolkit, Serialization; deserialize(\"$_fn\")"
19+
20+
run(`$(Base.julia_cmd()) -e $(_cmd)`)
21+
end

0 commit comments

Comments
 (0)