@@ -71,15 +71,29 @@ function prependPar(ex::Expr, prefix, parameters=[], inputs=[])
71
71
end
72
72
73
73
"""
74
- e = castToValueType (ex,value)
74
+ e = castToFloatType (ex,value)
75
75
76
- Cast `ex` to `typeof(value)` if this is a numeric data datatype (`eltype(value) <: Number`).
77
- As a result, the generated code of `ex` will have the correct type instead of `Any`, so will be more efficient.
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.
78
86
"""
79
- function castToValueType (ex,value)
87
+ function castToFloatType (ex,value)
80
88
if eltype (value) <: Number
81
89
valueType = typeof (value)
82
- :($ valueType ($ ex))
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
83
97
else
84
98
ex
85
99
end
@@ -96,7 +110,7 @@ Recursively converts der(x) to Symbol(:(der(x))) in expression `ex`
96
110
function makeDerVar (ex, parameters, inputs, evaluateParameters= false )
97
111
if typeof (ex) in [Symbol, Expr]
98
112
if ex in keys (parameters)
99
- castToValueType ( prependPar (ex, :(_p), parameters, inputs), parameters[ex] )
113
+ castToFloatType ( prependPar (ex, :(_p), parameters, inputs), parameters[ex] )
100
114
elseif ex in keys (inputs)
101
115
prependPar (ex, :(_p), parameters, inputs)
102
116
else
@@ -114,7 +128,7 @@ function makeDerVar(ex::Expr, parameters, inputs, evaluateParameters=false)
114
128
if evaluateParameters
115
129
parameters[ex]
116
130
else
117
- castToValueType ( prependPar (ex, :(_p), parameters, inputs), parameters[ex] )
131
+ castToFloatType ( prependPar (ex, :(_p), parameters, inputs), parameters[ex] )
118
132
end
119
133
elseif isexpr (ex, :.) && ex in keys (inputs)
120
134
if evaluateParameters
0 commit comments