Skip to content

Commit 683b4a3

Browse files
Added possibility to use Variables with attributes also for scripting. Fixed problem with lookup of derivative function in Julia 1.3.
1 parent 04415f1 commit 683b4a3

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

src/language/Execution.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,9 @@ function prepare_ida(instance::Instance, first_F_args, initial_bindings::Abstrac
348348
s = get_start(var)
349349
if isa(s, AbstractArray)
350350
append!(x0, vec(s))
351-
append!(x_nominal, vec(var.nominal === nothing ? fill(1.0, var.size) : var.nominal))
351+
append!(x_nominal,
352+
vec(var.nominal !== nothing ? var.nominal : var.size !== nothing ?
353+
fill(1.0, var.size) : fill(1.0, length(s))) )
352354
# @show name, vec(s)
353355
append!(diffstates, fill(is_diffstate, length(s)))
354356
else

src/language/Instantiation.jl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ Variable(;
174174
Variable(variability, T, size, value,
175175
unit, displayUnit, min, max, start, fixed, nominal, info, flow, state, property)
176176

177+
Variable(v; args...) = if typeof(v)==Variable; n=deepcopy(v); @show n; for (k,val) in args n[k]=val end; n else Variable(value=v; args...) end
178+
179+
function Base.size(v::Variable)
180+
return size(v.value)
181+
end
182+
177183
function Base.show(io::IO, v::Variable)
178184
print(io, "Variable(")
179185
first = true
@@ -208,7 +214,7 @@ function Base.show(io::IO, v::Variable)
208214
first = false
209215
end
210216

211-
if v.fixed != nothing
217+
if v.fixed != false
212218
if !first; print(io, ", ") end
213219
print(io, "fixed = ", v.fixed)
214220
first = false
@@ -250,9 +256,39 @@ function Base.show(io::IO, v::Variable)
250256
first = false
251257
end
252258

259+
if v.info != ""
260+
if !first; print(io, ", ") end
261+
print(io, "info = ", "\"$(v.info)\"")
262+
first = false
263+
end
264+
253265
println(io, ")")
254266
end
255267

268+
#Base.:+(v1::Variable, v2::Variable) = Variable(value=v1.value+v2.value)
269+
Base.:+(v1::Variable, v2::Variable) = Variable(value=v1.value+v2.value,
270+
info=if v1.info==nothing && v2.info==nothing; nothing else "("*v1.info*") + ("*v2.info*")" end,
271+
min=if v1.min==nothing || v2.min==nothing; nothing else v1.min+v2.min end,
272+
max=if v1.max==nothing || v2.max==nothing; nothing else v1.max+v2.max end)
273+
Base.:+(v1, v2::Variable) = Variable(value=v1+v2.value)
274+
Base.:+(v1::Variable, v2) = Variable(value=v1.value+v2)
275+
Base.:+(v1::Variable) = Variable(value=+v1.value)
276+
277+
Base.:-(v1::Variable, v2::Variable) = Variable(value=v1.value-v2.value)
278+
Base.:-(v1, v2::Variable) = Variable(value=v1-v2.value)
279+
Base.:-(v1::Variable, v2) = Variable(value=v1.value-v2)
280+
Base.:-(v1::Variable) = Variable(value=-v1.value)
281+
282+
Base.:*(v1::Variable, v2::Variable) = Variable(value=v1.value*v2.value,
283+
info=if v1.info==nothing && v2.info==nothing; nothing else "("*v1.info*") * ("*v2.info*")" end,
284+
min=if v1.min==nothing || v2.min==nothing; nothing else min(v1.min*v2.min, v1.max*v2.min, v1.min*v2.max, v1.max*v2.max) end,
285+
max=if v1.max==nothing || v2.max==nothing; nothing else max(v1.min*v2.min, v1.max*v2.min, v1.min*v2.max, v1.max*v2.max) end)
286+
Base.:*(v1, v2::Variable) = Variable(value=v1*v2.value)
287+
Base.:*(v1::Variable, v2) = Variable(value=v1.value*v2)
288+
289+
Base.:/(v1::Variable, v2::Variable) = Variable(value=v1.value/v2.value)
290+
Base.:/(v1, v2::Variable) = Variable(value=v1/v2.value)
291+
Base.:/(v1::Variable, v2) = Variable(value=v1.value/v2)
256292

257293
"Check that a start value (possibly default) exists for the var, or give an error."
258294
function check_start(var::Variable, name)

src/models/ModiaBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using .Synchronous: sample, Clock, previous, hold, positive, positiveChange, pos
1111
Shortcut for `Variable`
1212
"""
1313
Var(; args...) = Variable(; args...)
14+
Var(value; args...) = Variable(value=value; args...)
1415

1516
"""
1617
Create a floating-point `Variable`

src/symbolic/DAEquations/BasicStructuralTransform.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,8 +1822,8 @@ function generateCode(newStateSelection, useKinsol, params, realStates, unknowns
18221822

18231823
if false
18241824
open("FDAE.jl") do f
1825-
func = readstring(f)
1826-
end
1825+
func = read(f, String)
1826+
end
18271827
end
18281828

18291829
FUNC = Meta.parse(func)

src/symbolic/DAEquations/SymbolicTransform.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,12 @@ function differentiate(e)
804804
for i in 1:length(arguments)
805805
arg_der = differentiate(arguments[i])
806806
if arg_der != zero
807-
path = split(string(op), ".")
807+
# op does not contain full path to function in Julia 1.3, use typeof()
808+
typeofop = string(typeof(op))
809+
# remove typeof(...)
810+
typeofop = split(typeofop, "(")[2]
811+
typeofop = split(typeofop, ")")[1]
812+
path = split(typeofop, ".")
808813
if length(path) >= 2
809814
mod = path[end-1]
810815
else

0 commit comments

Comments
 (0)