-
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathserialization.jl
More file actions
56 lines (45 loc) · 1.87 KB
/
serialization.jl
File metadata and controls
56 lines (45 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using ModelingToolkitBase, SciMLBase, Serialization, OrdinaryDiffEqSDIRK
using ModelingToolkitBase: t_nounits as t, D_nounits as D
missing_guess_value = MissingGuessValue.Constant(1.0)
@variables x(t)
@named sys = System([D(x) ~ -0.5 * x], t, initial_conditions = Dict(x => 1.0))
sys = complete(sys)
for prob in [
eval(ModelingToolkitBase.ODEProblem{false}(sys, nothing, nothing)),
eval(ModelingToolkitBase.ODEProblem{false}(sys, nothing, nothing; expression = Val{true})),
]
_fn = tempname()
open(_fn, "w") do f
serialize(f, prob)
end
_cmd = "using ModelingToolkitBase, Serialization; deserialize(\"$_fn\")"
run(`$(Base.julia_cmd()) -e $(_cmd)`)
end
include("common/rc_model.jl")
@unpack capacitor = rc_model
io = IOBuffer()
write(io, expand_connections(rc_model))
str = String(take!(io))
sys = include_string(@__MODULE__, str)
rc2 = expand_connections(rc_model)
@test issetequal(full_equations(sys), full_equations(rc2))
@test issetequal(unknowns(sys), unknowns(rc2))
@test issetequal(parameters(sys), parameters(rc2))
# check answer
ss = mtkcompile(rc_model)
all_obs = observables(ss)
prob = ODEProblem(ss, [capacitor.v => 0.0], (0, 0.1); missing_guess_value)
sol = solve(prob, ImplicitEuler())
## Check System with Observables ----------
ss_exp = ModelingToolkitBase.toexpr(ss)
ss_ = complete(eval(ss_exp))
prob_ = ODEProblem(ss_, [capacitor.v => 0.0], (0, 0.1); missing_guess_value)
sol_ = solve(prob_, ImplicitEuler())
@test sol[all_obs] == sol_[all_obs] skip = !@isdefined(ModelingToolkit)
## Check ODEProblemExpr with Observables -----------
# build the observable function expression
# ODEProblemExpr with observedfun_exp included
probexpr = ODEProblem{true}(ss, [capacitor.v => 0.0], (0, 0.1); expr = Val{true}, missing_guess_value);
prob_obs = eval(probexpr)
sol_obs = solve(prob_obs, ImplicitEuler())
@test sol_obs[all_obs] == sol[all_obs]