Skip to content

Commit 04415f1

Browse files
Some fixes of the new replace function (for useKinsol=true). linspace->range. parse->Meta.parse. Fixed problem with differentiating time.
1 parent 9bd9cc3 commit 04415f1

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

src/Modia.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ For more information, see (https://github.com/ModiaSim/Modia.jl/blob/master/READ
4545
"""
4646
module Modia
4747

48-
const Version = "0.3.0"
49-
const Date = "2019-04-07"
48+
const Version = "0.3.1-dev"
49+
const Date = "2019-07-31"
5050

5151

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

src/symbolic/DAEquations/BasicStructuralTransform.jl

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ function analyzeStructurally(equations, params, unknowns_indices, deriv, unknown
12761276
equation_code = nothing
12771277
end
12781278

1279-
if newStateSelection || useKinsol && false ## Disable simulation for 0.7
1279+
if newStateSelection || useKinsol # && false ## Disable simulation for 0.7
12801280
generateCode(newStateSelection, useKinsol, params, realStates, unknownsNames, deriv, equations, componentsIG, assignIG, Avar, Bequ, VSizes, ESizes, invAssign, sortedEquations, equation_code, unknowns)
12811281

12821282
return nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing
@@ -1511,7 +1511,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
15111511
func *= " # Set parameters\n"
15121512

15131513
for (p, v) in params
1514-
pName = replace(string(p), ".", "_")
1514+
pName = replace(string(p), "." => "_")
15151515
func *= " " * pName * " = " * string(v) * "\n"
15161516
if logFDAE
15171517
func *= " " * "@show $pName" * "\n"
@@ -1523,7 +1523,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
15231523
vNames = makeList(unknownsNames, 1:length(assignIG), Avar)
15241524

15251525
for i in 1:length(sortedEquations.Vx)
1526-
xName = replace(string(vNames[sortedEquations.Vx[i]]), ".", "_")
1526+
xName = replace(string(vNames[sortedEquations.Vx[i]]), "." => "_")
15271527
func *= " $(xName) = _x[$i]\n"
15281528
if logFDAE
15291529
func *= " " * "@show $xName" * "\n"
@@ -1532,7 +1532,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
15321532

15331533
for i in 1:length(sortedEquations.Vderx)
15341534
if sortedEquations.Vderx[i] > 0
1535-
der_xName = replace(string(vNames[sortedEquations.Vderx[i]]), ".", "_")
1535+
der_xName = replace(string(vNames[sortedEquations.Vderx[i]]), "." => "_")
15361536
func *= " $(der_xName) = _der_x[$i]\n"
15371537
if logFDAE
15381538
func *= " " * "@show $der_xName" * "\n"
@@ -1549,8 +1549,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
15491549
# func *= " " * "println(\"next\")" * "\n"
15501550
for (solution, solved) in equation_code
15511551
pp = prettyPrint(solution)
1552-
pp = replace(string(pp), "der(", "(der_")
1553-
pp = replace(string(pp), "der_der_", "der2_")
1552+
pp = replace(string(pp), "der(" => "(der_")
1553+
pp = replace(string(pp), "der_der_" => "der2_")
15541554
if logFDAE
15551555
func *= " " * "println(\"$pp\")" * "\n"
15561556
end
@@ -1564,10 +1564,12 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
15641564
loglnModia()
15651565
end
15661566

1567+
func *= " time = t0\n"
1568+
15671569
func *= " # Copy state vector and derivative vector to each state and derivative\n"
15681570
for i in 1:length(realStates)
1569-
xName = replace(string(realStates[i]), ".", "_")
1570-
xName = replace(xName, "this_", "")
1571+
xName = replace(string(realStates[i]), "." => "_")
1572+
xName = replace(xName, "this_" => "")
15711573
derName = "der_" * xName
15721574
func *= " $(xName) = _x[$i]\n"
15731575
func *= " $(derName) = _der_x[$i]\n"
@@ -1660,8 +1662,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
16601662
@show orgIndexVar
16611663
end
16621664
vName = variables[orgIndexVar[varIndex]] ### [1]
1663-
vName = replace(string(vName), ".", "_")
1664-
vName = replace(vName, "this_", "")
1665+
vName = replace(string(vName), "." => "_")
1666+
vName = replace(vName, "this_" => "")
16651667
if derOrderVar[varIndex] == 1 ### Fix for higher order needed
16661668
vName = "der_"*vName
16671669
elseif derOrderVar[varIndex] == 2 ### Fix for higher order needed
@@ -1671,7 +1673,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
16711673
if varIndex > 0
16721674

16731675
vName = vNames[varIndex]
1674-
vName = replace(string(vName), ".", "_")
1676+
vName = replace(string(vName), "." => "_")
16751677

16761678
if VSizes[varIndex] == ()
16771679
vIndex += 1
@@ -1718,8 +1720,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
17181720
# e = :(_res[$globalResidualIndex] = $(e.args[1]) - $(e.args[2]))
17191721
end
17201722
pp = prettyPrint(e)
1721-
pp = replace(string(pp), "der(", "(der_")
1722-
pp = replace(string(pp), "der_der_", "der2_")
1723+
pp = replace(string(pp), "der(" => "(der_")
1724+
pp = replace(string(pp), "der_der_" => "der2_")
17231725

17241726
if logFDAE
17251727
if length(c) > 1
@@ -1751,8 +1753,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
17511753
#=
17521754
(orgIndexVar, derOrderVar) = invertDer(Avar)
17531755
vName = variables[orgIndexVar[varIndex]] ### [1]
1754-
vName = replace(string(vName), ".", "_")
1755-
vName = replace(vName, "this_", "")
1756+
vName = replace(string(vName), "." => "_")
1757+
vName = replace(vName, "this_" => "")
17561758
if derOrderVar[varIndex] == 1 ### Fix for higher order needed
17571759
vName = "der_"*vName
17581760
elseif derOrderVar[varIndex] == 2 ### Fix for higher order needed
@@ -1762,7 +1764,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
17621764
if varIndex > 0
17631765

17641766
vName = vNames[varIndex]
1765-
vName = replace(string(vName), ".", "_")
1767+
vName = replace(string(vName), "." => "_")
17661768

17671769
if VSizes[varIndex] == ()
17681770
vIndex += 1
@@ -1789,8 +1791,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
17891791
func *= " # Create residual vector for derivatives\n"
17901792

17911793
for i in 1:length(realStates)
1792-
xName = replace(string(realStates[i]), ".", "_")
1793-
xName = replace(xName, "this_", "")
1794+
xName = replace(string(realStates[i]), "." => "_")
1795+
xName = replace(xName, "this_" => "")
17941796
derName = "der_" * xName
17951797
globalResidualIndex += 1
17961798
func *= " " * "_res[$globalResidualIndex] = _der_x[$i] - $derName" * "\n"
@@ -1812,7 +1814,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
18121814
loglnModia(func)
18131815

18141816
# Allow editing of FDAE
1815-
if false
1817+
if true
18161818
open("FDAE.jl", "w") do f
18171819
write(f, func)
18181820
end
@@ -1824,7 +1826,7 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
18241826
end
18251827
end
18261828

1827-
FUNC = parse(func)
1829+
FUNC = Meta.parse(func)
18281830
fdae = eval(FUNC)
18291831

18301832
if newStateSelection
@@ -1870,7 +1872,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
18701872

18711873
if length(x0) > 0
18721874
println("\nSimulate")
1873-
t = linspace(0.0, 50, 1000)
1875+
# t = linspace(0.0, 50, 1000)
1876+
t = range(0.0, stop=50.0, length=1000)
18741877
result = ModiaMath.ModiaToModiaMath.simulate(m, t; log=false, tolRel=1E-5)
18751878

18761879
#= Temporarily removed due to problem with PyPlot

src/symbolic/DAEquations/SymbolicTransform.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ differentiate(e::Irrational) = zero
528528
function differentiate(e)
529529
if logDifferentiate
530530
# print("\nDIFFERENTIATE: "); println(prettyPrint(e))
531-
# @show typeof(e)
531+
# @show e, typeof(e)
532532
end
533533

534534
if typeof(e) in [Float64, Int64, String, Bool]
@@ -542,7 +542,7 @@ function differentiate(e)
542542
elseif typeof(e) in [Array{Float64}, Array{Int64}]
543543
# diff = :($zero*$e) # zero*e
544544
diff = :($zero) # zero*e
545-
elseif e == :time
545+
elseif e == time_global || e == :time
546546
diff = one
547547
elseif e in timeInvariants
548548
# println("TIME INVARIANT" )
@@ -557,7 +557,7 @@ function differentiate(e)
557557
diff = :($zero)
558558
else
559559
if logDifferentiateVariable
560-
print("DIFFERENTIATE VARIABLE: "); println(e)
560+
print("DIFFERENTIATE VARIABLE: "); println(e)
561561
end
562562
# println("\nGetField:")
563563
# @show e
@@ -815,7 +815,7 @@ function differentiate(e)
815815

816816
if mod == ""
817817
println("Derivative function ", string(f_der), " not found.")
818-
elseif ! (f_der in names(getfield(Main, Symbol(mod))))
818+
elseif false # ! (f_der in names(getfield(Main, Symbol(mod))))
819819
error("Derivative function ", string(f_der), " not found.")
820820
else
821821
f_der = getfield(getfield(Main, Symbol(mod)), f_der)

src/symbolic/Utilities.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function showVariable(v)
164164
first = false
165165
end
166166

167-
if v.flow != nothing
167+
if v.flow != nothing && v.flow
168168
if !first; logModia(", ") end
169169
logModia("flow = ", v.flow)
170170
first = false

0 commit comments

Comments
 (0)