-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy patherror_handling.jl
53 lines (45 loc) · 1.6 KB
/
error_handling.jl
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
using Test
using ModelingToolkit
import ModelingToolkit: ExtraVariablesSystemException, ExtraEquationsSystemException
include("../examples/electrical_components.jl")
function UnderdefinedConstantVoltage(; name, V = 1.0)
val = V
@named p = Pin()
@named n = Pin()
@parameters V
eqs = [
V ~ p.v - n.v # Remove equation
# 0 ~ p.i + n.i
]
ODESystem(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val), name = name)
end
function OverdefinedConstantVoltage(; name, V = 1.0, I = 1.0)
val = V
val2 = I
@named p = Pin()
@named n = Pin()
@parameters V I
eqs = [V ~ p.v - n.v
# Overdefine p.i and n.i
n.i ~ I
p.i ~ I]
ODESystem(eqs, t, [], [V], systems = [p, n], defaults = Dict(V => val, I => val2),
name = name)
end
R = 1.0
C = 1.0
V = 1.0
@named resistor = Resistor(R = R)
@named capacitor = Capacitor(C = C)
@named source = UnderdefinedConstantVoltage(V = V)
rc_eqs = [connect(source.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source.n)]
@named rc_model = ODESystem(rc_eqs, t, systems = [resistor, capacitor, source])
@test_throws ModelingToolkit.ExtraVariablesSystemException structural_simplify(rc_model)
@named source2 = OverdefinedConstantVoltage(V = V, I = V / R)
rc_eqs2 = [connect(source2.p, resistor.p)
connect(resistor.n, capacitor.p)
connect(capacitor.n, source2.n)]
@named rc_model2 = ODESystem(rc_eqs2, t, systems = [resistor, capacitor, source2])
@test_throws ModelingToolkit.ExtraEquationsSystemException structural_simplify(rc_model2)