Skip to content

Commit a590632

Browse files
Support for positive, Clock and sample.
1 parent 96e7a19 commit a590632

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Differentiate.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Form the derivative of the expressions `ex` with regards to time. Time derivativ
2222
* `return `der`: Time derivative of ex
2323
"""
2424
derivative(ex, timeInvariants=[]) = 0#u"1/s"
25-
derivative(s::Symbol, timeInvariants=[]) = if s == :time; 1 elseif s in timeInvariants; 0 else :(der($s)) end
25+
derivative(s::Symbol, timeInvariants=[]) = if s == :time; 1 elseif s in timeInvariants; 0 else :(der($s)) end # 0u"1/s"
2626
function derivative(ex::Expr, timeInvariants=[])
2727
if isexpr(ex, :call) && ex.args[1] == :der
2828
:(der($ex))

src/Symbolic.jl

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Examples of use can be found in TestSymbolic.jl
1111
module Symbolic
1212

1313
export removeBlock, makeDerVar, append, prepend, Incidence, findIncidence!, linearFactor, solveEquation,
14-
isLinear, getCoefficients, substitute, removeUnits
14+
isLinear, getCoefficients, substitute, removeUnits, resetCounters
1515

1616
using Base.Meta: isexpr
1717
#using OrderedCollections
@@ -73,12 +73,27 @@ prepend(ex, prefix) = ex
7373
#prepend(ex::Symbol, prefix::Array{Symbol,1}) = if length(prefix) == 0; ex else prepend(prepend(ex, prefix[end]), prefix[1:end-1]) end
7474
#prepend(ex::QuoteNode, prefix::Symbol) = Expr(:., prefix, QuoteNode(ex))
7575
prepend(ex::Symbol, prefix) = if prefix == nothing; ex elseif ex in [:time, :sim, :leq_mode]; ex else Expr(:., prefix, QuoteNode(ex)) end # TODO: Fix sim
76-
#prepend(ex::Symbol, prefix::Nothing) = (println("Prepend"); ex)
76+
prepend(ex::Symbol, prefix::Nothing) = ex
7777
prepend(arr::Array{Expr,1}, prefix) = [prepend(a, prefix) for a in arr]
7878
#prepend(dict::OrderedCollections.OrderedDict{Symbol,Expr}, prefix) = OrderedDict([prepend(k, prefix) => prepend(k, prefix) for (k,v) in dict])
7979

80+
nCrossingFunctions = 0
81+
nClocks = 0
82+
nSamples = 0
83+
84+
function resetCounters()
85+
global nCrossingFunctions
86+
global nClocks
87+
global nSamples
88+
nCrossingFunctions = 0
89+
nClocks = 0
90+
nSamples = 0
91+
end
92+
8093
function prepend(ex::Expr, prefix)
8194
global nCrossingFunctions
95+
global nClocks
96+
global nSamples
8297
if typeof(prefix) == Array{Any,1} && length(prefix) == 0
8398
ex
8499
elseif ex.head == :. && ex.args[1] == :up
@@ -89,7 +104,13 @@ function prepend(ex::Expr, prefix)
89104
:($prefix.$e)
90105
elseif ex.head == :call && ex.args[1] == :positive
91106
nCrossingFunctions += 1
92-
:(ModiaMath.positive!(sim, $nCrossingFunctions, $(prepend(ex.args[2], prefix)), $(string(prepend(ex.args[2], prefix))), leq_mode=leq_mode))
107+
:(positive(instantiatedModel, $nCrossingFunctions, $(prepend(ex.args[2], prefix)), $(string(prepend(ex.args[2], prefix))), _leq_mode))
108+
elseif ex.head == :call && ex.args[1] == :Clock
109+
nClocks += 1
110+
:(Clock($(prepend(ex.args[2], prefix)), instantiatedModel, $nClocks))
111+
elseif ex.head == :call && ex.args[1] == :sample
112+
nSamples += 1
113+
:(sample($(prepend(ex.args[2], prefix)), $(prepend(ex.args[3], prefix)), instantiatedModel, $nSamples))
93114
else
94115
Expr(ex.head, ex.args[1], [prepend(arg, prefix) for arg in ex.args[2:end]]...)
95116
end
@@ -112,7 +133,7 @@ Traverses an expression and finds incidences of Symbols and der(...)
112133
* `incidence`: array of incidences. New incidences of `ex` are pushed.
113134
"""
114135
findIncidence!(ex, incidence::Array{Incidence,1}) = nothing
115-
findIncidence!(s::Symbol, incidence::Array{Incidence,1}) = push!(incidence, s)
136+
findIncidence!(s::Symbol, incidence::Array{Incidence,1}) = begin if s != :(:); push!(incidence, s) end end
116137
findIncidence!(arr::Array{Any,1}, incidence::Array{Incidence,1}) = [findIncidence!(a, incidence) for a in arr]
117138
findIncidence!(arr::Array{Expr,1}, incidence::Array{Incidence,1}) = [findIncidence!(a, incidence) for a in arr]
118139
function findIncidence!(ex::Expr, incidence::Array{Incidence,1})

0 commit comments

Comments
 (0)