Skip to content

Commit a24f97d

Browse files
Handle automatic state selection when constrains on angles
1 parent 05343aa commit a24f97d

File tree

4 files changed

+55
-17
lines changed

4 files changed

+55
-17
lines changed

src/Modia.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ For more information, see (https://github.com/ModiaSim/Modia.jl/blob/master/READ
4646
module Modia
4747

4848
const Version = "0.2.4-dev"
49-
const Date = "2019-02-13"
49+
const Date = "2019-02-24"
5050

5151

5252
#println(" \n\nWelcome to Modia - Dynamic MODeling and Simulation in julIA")

src/symbolic/DAEquations/BasicStructuralTransform.jl

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,18 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
996996
end
997997

998998
if automaticStateSelection
999+
startValues = []
1000+
fixedFlags = []
1001+
for (name, var) in unknowns
1002+
push!(startValues, var.start)
1003+
push!(fixedFlags, var.fixed)
1004+
end
1005+
@show startValues
1006+
@show fixedFlags
1007+
9991008
components = Array{Array{Int64,1},1}(BLT(IG, assign))
10001009
vNames = makeList(unknownsNames, 1:length(assign), Avar) # ::Vector{String}
1001-
eqGraph = StateSelection.getSortedEquationGraph(IG, Gsolvable, components, assign, Avar, Bequ, vNames; withStabilization=false)
1010+
eqGraph = StateSelection.getSortedEquationGraph(IG, Gsolvable, components, assign, Avar, Bequ, vNames, withStabilization=false)
10021011
end
10031012

10041013
if newStateSelection
@@ -1012,8 +1021,32 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
10121021
if ! automaticStateSelection
10131022
vActive[statesIndices] .= false
10141023
else
1015-
vActive[eqGraph.Vx] .= false
1016-
realStates = [GetField(This(), name) for name in names[eqGraph.Vx]]
1024+
stateIndices = eqGraph.Vx
1025+
@show names Avar stateIndices
1026+
@show Gsolvable
1027+
for i in 1:length(stateIndices)
1028+
j = stateIndices[i]
1029+
if j in Avar # selected state is a derivative
1030+
# search for corresponding variable
1031+
alias = 0
1032+
for ie in 1:length(G)
1033+
g = Gsolvable[ie]
1034+
if j in g && length(g) == 2 && sort(g) == sort(G[ie])
1035+
alias = if g[1] == j; g[2] else g[1] end
1036+
break;
1037+
end
1038+
end
1039+
@show alias i
1040+
stateIndices[i] = alias
1041+
end
1042+
end
1043+
1044+
@show stateIndices
1045+
vActive[stateIndices] .= false
1046+
stateNames = [replace(replace(string(name), "(" => "_"), ")" => "") for name in names[stateIndices]]
1047+
@show stateNames
1048+
@show realStates
1049+
realStates = [GetField(This(), Symbol(name)) for name in stateNames]
10171050
setRealStates(realStates)
10181051
end
10191052

src/symbolic/DAEquations/SymbolicTransform.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,22 +589,26 @@ function differentiate(e)
589589
print("DIFFERENTIATE DERIVATIVE: "); println(e)
590590
end
591591

592-
# @show e.base e.base.name
593-
if true
592+
# @show e e.base e.base.name realStates
593+
state = GetField(This(), Symbol("der_" * string(e.base.name)))
594+
# @show state
595+
i = findfirst(isequal(state), realStates)
596+
# @show i
597+
if i == notFound
594598
diff = GetField(This(), Symbol("der_der_" * string(e.base.name)))
599+
# @show diff
595600
if !noUnits
596601
diff = diff / Unitful.s
597602
end
598603
dummyDerivatives[Symbol("der_der_" * string(e.base.name))] = nothing
599-
elseif e.base.name == Symbol("j.s") # Hack!!!
600-
diff = Der(GetField(This(), Symbol("j.v"))) # GetField(This(), Symbol("j.a")) #
601604
else
602-
diff = Der(e)
605+
diff = Der(state)
606+
dummyDerivatives[Symbol("der_" * string(e.base.name))] = nothing
603607
end
604608

605609
# @show e diff
606610
if logDifferentiateVariable
607-
print(" Differentiated derivative: "); println(diff)
611+
print(" Differentiated derivative: "); println(diff)
608612
end
609613

610614
elseif e.head in [:(=), :(:=)] # Equation

test/models/TestAutomaticStateSelection.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ else
1616
using Modia.Test
1717
end
1818

19-
const logSimulation=false
19+
const logSimulation=true
2020

21+
#=
2122
@model TwoConnectedInertias begin
2223
J1 = 2.0
2324
J2 = 3.0
@@ -101,17 +102,17 @@ plot(result, ("C1.v", "C2.v"), figure=4)
101102
end
102103
result = simulate(ParallelCapacitors2b , 1.0; logTranslation=true, logSimulation=logSimulation, tearing=true, removeSingularities=true, automaticStateSelection=false)
103104
plot(result, ("u1", "u2", "i1", "v1"), figure=6)
105+
=#
104106

105-
#=
106107
# Does not compile
107108
@model TwoInertiasConnectedViaIdealGearWithPositionConstraints begin
108109
J1 = 2.0
109110
J2 = 3.0
110111
ratio = 4.0
111-
phi1 = Float(size=(), start=1.0)
112+
phi1 = Float(size=(), start=1.0, fixed=true)
112113
phi2 = Float(size=(), start=1.0)
113-
w1 = Float(size=(), start=1.0)
114-
w2 = Float(size=(), start=1.0)
114+
w1 = Float(size=(), start=1.0, fixed=true)
115+
w2 = Float(size=(), start=1.0, fixed=true)
115116
tau = Float(size=())
116117
t = Float(size=())
117118
@equations begin
@@ -123,9 +124,9 @@ plot(result, ("u1", "u2", "i1", "v1"), figure=6)
123124
phi1 = ratio*phi2
124125
end
125126
end
126-
result = simulate(TwoInertiasConnectedViaIdealGearWithPositionConstraints, 3.0; logTranslation=true, logSimulation=logSimulation, tearing=true, removeSingularities=true, automaticStateSelection=true)
127+
result = simulate(TwoInertiasConnectedViaIdealGearWithPositionConstraints, 3.0; logTranslation=true, logSimulation=logSimulation, tearing=true, removeSingularities=false, automaticStateSelection=true)
127128
plot(result, [("phi1", "phi2"), ("w1", "w2")], figure=7)
128-
=#
129+
println("after plot")
129130

130131

131132
end

0 commit comments

Comments
 (0)