Skip to content

Commit a9f91ef

Browse files
committed
Moved ModiaBase.Symbolic.makeDerVar to Modia
1 parent 5a7097f commit a9f91ef

File tree

4 files changed

+9
-92
lines changed

4 files changed

+9
-92
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModiaBase"
22
uuid = "ec7bf1ca-419d-4510-bbab-199861c55244"
33
authors = ["Hilding Elmqvist <[email protected]>", "Martin Otter <[email protected]>"]
4-
version = "0.10.0"
4+
version = "0.11.0"
55

66
[deps]
77
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

docs/src/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ julia> ]add ModiaBase
7777

7878
## Release Notes
7979

80+
### Version 0.11.0
81+
82+
- Moved ModiaBase.Symbolic.makeDerVar to Modia (because makeDerVar needs FloatType for
83+
generating type-stable code and FloatType is available in Modia but not in ModiaBase).
84+
85+
8086
### Version 0.10.0
8187

8288
**Non-backwards** compatible changes

src/ModiaBase.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Main module of ModiaBase.
99
module ModiaBase
1010

1111
const path = dirname(dirname(@__FILE__)) # Absolute path of package directory
12-
const Version = "0.10.0"
13-
const Date = "2022-03-01"
12+
const Version = "0.11.0"
13+
const Date = "2022-03-08"
1414

1515
#println("\nImporting ModiaBase Version $Version ($Date)")
1616

src/Symbolic.jl

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -56,95 +56,6 @@ function removeQuoteNode(ex::Expr)
5656
Expr(ex.head, [removeQuoteNode(arg) for arg in ex.args]...)
5757
end
5858

59-
prependPar(ex, prefix, parameters=[], inputs=[]) = ex
60-
61-
function prependPar(ex::Symbol, prefix, parameters=[], inputs=[])
62-
if prefix == nothing; ex elseif ex in [:time, :instantiatedModel, :_leq_mode, :_x]; ex else Expr(:ref, prefix, QuoteNode(ex)) end
63-
end
64-
65-
function prependPar(ex::Expr, prefix, parameters=[], inputs=[])
66-
if isexpr(ex, :.)
67-
Expr(:ref, prependPar(ex.args[1], prefix, parameters, inputs), QuoteNode(ex.args[2].value))
68-
else
69-
Expr(ex.head, [prependPar(arg, prefix, parameters, inputs) for arg in ex.args]...)
70-
end
71-
end
72-
73-
"""
74-
e = castToFloatType(ex,value)
75-
76-
If manageable, cast `ex` to FloatType, if this is an `AbstractFloat` (`typeof(value) <: AbstractFloat`)
77-
and define it to be FloatType (called _FloatType in getDerivatives), so `FloatType(ex)::FloatType`.
78-
79-
If this is not manageable, cast `ex` to `valueType = typeof(value)` if this is a numeric data datatype
80-
(`eltype(value) <: Number`), and define it to be `valueType`, so `valueType(ex)::valueType`.
81-
82-
As a result, the generated code of `ex` will have the correct type instead of `Any`,
83-
so will be more efficient and no unnecessary memory will be calculated at run-time.
84-
85-
Note, this function should only be used on parameter, init, or start values.
86-
"""
87-
function castToFloatType(ex,value)
88-
if eltype(value) <: Number
89-
valueType = typeof(value)
90-
if valueType <: AbstractFloat && !(valueType <: Unitful.AbstractQuantity ||
91-
valueType <: Measurements.Measurement ||
92-
valueType <: MonteCarloMeasurements.AbstractParticles)
93-
:( _FloatType($ex)::_FloatType )
94-
else
95-
:( $valueType($ex)::$valueType )
96-
end
97-
else
98-
ex
99-
end
100-
end
101-
102-
"""
103-
e = makeDerVar(ex)
104-
105-
Recursively converts der(x) to Symbol(:(der(x))) in expression `ex`
106-
107-
* `ex`: Expression or array of expressions
108-
* `return `e`: ex with der(x) converted
109-
"""
110-
function makeDerVar(ex, parameters, inputs, evaluateParameters=false)
111-
if typeof(ex) in [Symbol, Expr]
112-
if ex in keys(parameters)
113-
castToFloatType( prependPar(ex, :(_p), parameters, inputs), parameters[ex] )
114-
elseif ex in keys(inputs)
115-
prependPar(ex, :(_p), parameters, inputs)
116-
else
117-
ex
118-
end
119-
else
120-
ex
121-
end
122-
end
123-
124-
function makeDerVar(ex::Expr, parameters, inputs, evaluateParameters=false)
125-
if ex.head == :call && ex.args[1] == :der
126-
Symbol(ex)
127-
elseif isexpr(ex, :.) && ex in keys(parameters)
128-
if evaluateParameters
129-
parameters[ex]
130-
else
131-
castToFloatType( prependPar(ex, :(_p), parameters, inputs), parameters[ex] )
132-
end
133-
elseif isexpr(ex, :.) && ex in keys(inputs)
134-
if evaluateParameters
135-
inputs[ex]
136-
else
137-
prependPar(ex, :(_p), parameters, inputs)
138-
end
139-
elseif ex.head == :.
140-
Symbol(ex)
141-
elseif ex.head == :call # Don't change dot-notation for function calls
142-
Expr(ex.head, ex.args[1], [makeDerVar(arg, parameters, inputs, evaluateParameters) for arg in ex.args[2:end]]...)
143-
else
144-
Expr(ex.head, [makeDerVar(arg, parameters, inputs, evaluateParameters) for arg in ex.args]...)
145-
end
146-
end
147-
14859
removeUnits(ex) = if typeof(ex) <: Unitful.Quantity; @show ex; ustrip(ex) else ex end
14960
removeUnits(ex::Expr) = if ex.head == :macrocall && ex.args[1] == Symbol("@u_str"); 1 else Expr(ex.head, [removeUnits(arg) for arg in ex.args]...) end
15061

0 commit comments

Comments
 (0)