Skip to content

Commit d2cafcd

Browse files
committed
Adpated to latest SignalTables version + SignalTables functions used in TestFirstOrder2.jl (especially, writing signals to file)
1 parent 7dc36f8 commit d2cafcd

File tree

10 files changed

+127
-93
lines changed

10 files changed

+127
-93
lines changed

Manifest.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ version = "0.4.0"
201201

202202
[[deps.DiffEqBase]]
203203
deps = ["ArrayInterfaceCore", "ChainRulesCore", "DataStructures", "Distributions", "DocStringExtensions", "FastBroadcast", "ForwardDiff", "FunctionWrappers", "LinearAlgebra", "Logging", "MuladdMacro", "NonlinearSolve", "Parameters", "Printf", "RecursiveArrayTools", "Reexport", "Requires", "SciMLBase", "Setfield", "SparseArrays", "StaticArrays", "Statistics", "ZygoteRules"]
204-
git-tree-sha1 = "9862c61c6049b0ad5a6b433e31d2c6c8ff373056"
204+
git-tree-sha1 = "f7a479aac5f3917b8472ac5f1b77d6f296fe58f1"
205205
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
206-
version = "6.92.2"
206+
version = "6.92.3"
207207

208208
[[deps.DiffEqCallbacks]]
209209
deps = ["DataStructures", "DiffEqBase", "ForwardDiff", "LinearAlgebra", "NLsolve", "Parameters", "RecipesBase", "RecursiveArrayTools", "SciMLBase", "StaticArrays"]
@@ -533,9 +533,9 @@ version = "0.11.0"
533533

534534
[[deps.MonteCarloMeasurements]]
535535
deps = ["Distributed", "Distributions", "LinearAlgebra", "MacroTools", "Random", "RecipesBase", "Requires", "SLEEFPirates", "StaticArrays", "Statistics", "StatsBase", "Test"]
536-
git-tree-sha1 = "03619e255664666b352a5e5f6b45e8b00d439870"
536+
git-tree-sha1 = "a7e89fde6ff10000e1a8f4d697b978d3908e913a"
537537
uuid = "0987c9cc-fe09-11e8-30f0-b96dd679fdca"
538-
version = "1.0.8"
538+
version = "1.0.9"
539539

540540
[[deps.MozillaCACerts_jll]]
541541
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

Project.toml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,6 @@ name = "Modia"
33
uuid = "cb905087-75eb-5f27-8515-1ce0ec8e839e"
44
version = "0.9.0-dev"
55

6-
[compat]
7-
DiffEqBase = "6.92.2"
8-
DataFrames = "1"
9-
DifferentialEquations = "7"
10-
FiniteDiff = "2"
11-
ForwardDiff = "0.10"
12-
JSON = "0.21"
13-
Measurements = "2"
14-
ModiaBase = "0.11"
15-
MonteCarloMeasurements = "1"
16-
OrderedCollections = "1"
17-
RecursiveFactorization = "0.2"
18-
Reexport = "1"
19-
SignalTables = "0.2"
20-
StaticArrays = "1"
21-
Sundials = "4"
22-
TimerOutputs = "0.5"
23-
Unitful = "1"
24-
julia = "1.7"
25-
266
[deps]
277
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
288
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
@@ -46,5 +26,25 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4626
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
4727
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
4828

29+
[compat]
30+
DiffEqBase = "6.82.0"
31+
DataFrames = "1"
32+
DifferentialEquations = "7"
33+
FiniteDiff = "2"
34+
ForwardDiff = "0.10"
35+
JSON = "0.21"
36+
Measurements = "2"
37+
ModiaBase = "0.11"
38+
MonteCarloMeasurements = "1"
39+
OrderedCollections = "1"
40+
RecursiveFactorization = "0.2"
41+
Reexport = "1"
42+
SignalTables = "0.2, 0.3"
43+
StaticArrays = "1"
44+
Sundials = "4"
45+
TimerOutputs = "0.5"
46+
Unitful = "1"
47+
julia = "1.7"
48+
4949
[extras]
5050
CPUSummary = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"

docs/src/Functions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ showEvaluatedParameters
6060
CurrentModule = Modia
6161
```
6262

63-
The simulation result of a model `instantiatedModel` supports the functions of
63+
The simulation result of a model `instantiatedModel` are provided as *signal table*
64+
(see [SignalTables.jl]()).
65+
Therefore, all functions[SignalTables.jl functions] can be used.
66+
These functions
6467
[ModiaResult](https://modiasim.github.io/ModiaResult.jl/stable/Functions.html) and
6568
exports them, so the functions can be accessed without prefixing them with `Modia`.
6669

src/CodeGeneration.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ mutable struct SimulationOptions{FloatType,TimeType}
184184
rawStopTime = get(kwargs, :stopTime, startTime)
185185
stopTime = convertTimeVariable(TimeType, rawStopTime)
186186
interval = convertTimeVariable(TimeType, get(kwargs, :interval , (stopTime - startTime)/500.0) )
187-
dtmax = get(kwargs, :dtmax, 100*getValue(interval))
187+
dtmax = get(kwargs, :dtmax, 100*getValueOnly(interval))
188188
if ismissing(dtmax) || isnothing(dtmax)
189-
dtmax = 100*getValue(interval)
189+
dtmax = 100*getValueOnly(interval)
190190
end
191191
dtmax = convert(Float64, dtmax)
192192
desiredResultTimeUnit = unit(rawStopTime)
@@ -1676,7 +1676,7 @@ function new_x_segmented_variable!(m::SimulationModel{FloatType,TimeType}, x_nam
16761676
new_result_info = false
16771677
x_info = result.info[x_name]
16781678
@assert(x_info.kind == RESULT_X)
1679-
@assert(basetype(startOrInit) == FloatType)
1679+
@assert(eltypeOrType(startOrInit) == FloatType)
16801680
@assert(length( x_info.id[end].dims ) == ndims(startOrInit)) # Number of dimensions cannot change
16811681
#if typeof(startOrInit) <: Number
16821682
# @assert(xi_info.scalar)
@@ -1713,10 +1713,17 @@ function new_x_segmented_variable!(m::SimulationModel{FloatType,TimeType}, x_nam
17131713
# after function initialStateVector!(...) was called.
17141714
t_unit = get(result.info[result.timeName].signal, :unit, "")
17151715
der_x_unit = x_unit == "" ? SignalTables.unitAsParseableString(unit(1/uparse(t_unit))) : SignalTables.unitAsParseableString(unit(uparse(x_unit)/uparse(t_unit)))
1716-
x_var = Var(unit=x_unit, start=xi_info.startOrInit, fixed=xi_info.fixed, state=true, der=xi_info.der_x_name)
1717-
1718-
result.info[x_name] = ResultInfo(RESULT_X , x_var, FloatType)
1719-
result.info[der_x_name] = ResultInfo(RESULT_DER_X, Var(unit=der_x_unit), FloatType)
1716+
if x_unit == ""
1717+
x_var = Var(start=xi_info.startOrInit, fixed=xi_info.fixed, state=true, der=xi_info.der_x_name)
1718+
else
1719+
x_var = Var(unit=x_unit, start=xi_info.startOrInit, fixed=xi_info.fixed, state=true, der=xi_info.der_x_name)
1720+
end
1721+
result.info[x_name] = ResultInfo(RESULT_X, x_var, FloatType)
1722+
if der_x_unit == ""
1723+
result.info[der_x_name] = ResultInfo(RESULT_DER_X, Var(), FloatType)
1724+
else
1725+
result.info[der_x_name] = ResultInfo(RESULT_DER_X, Var(unit=der_x_unit), FloatType)
1726+
end
17201727
end
17211728
return x_segmented_startIndex
17221729
end
@@ -1765,7 +1772,7 @@ function new_w_segmented_variable!(m::SimulationModel, name::String, w_segmented
17651772
else
17661773
signal = Var(unit=unit)
17671774
end
1768-
result.info[name] = ResultInfo(RESULT_W_SEGMENTED, signal, ValuesID(m.nsegments, w_index, w_size), basetype(w_segmented_default))
1775+
result.info[name] = ResultInfo(RESULT_W_SEGMENTED, signal, ValuesID(m.nsegments, w_index, w_size), eltypeOrType(w_segmented_default))
17691776
end
17701777
#println("new_w_segmented_variable: w_segmented_temp = ", result.w_segmented_temp)
17711778
return w_index
@@ -2012,7 +2019,7 @@ function generate_getDerivatives!(FloatType, TimeType, AST::Vector{Expr}, equati
20122019
end
20132020

20142021
# Generate code of the function
2015-
# temporarily removed: _m.time = $TimeType(Modia.getValue(_time))
2022+
# temporarily removed: _m.time = $TimeType(Modia.getValueOnly(_time))
20162023
code = quote
20172024
function $functionName(_x, _m::Modia.SimulationModel{$FloatType,$TimeType}, _time::$TimeType)::Nothing
20182025
_FloatType = $FloatType

src/EventHandler.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ should be used. Only in special cases, the other flags are useful.
1919

2020

2121
using ForwardDiff
22-
getValue(v) = v
23-
getValue(v::ForwardDiff.Dual) = v.value
24-
getValue(v::Measurements.Measurement) = Measurements.value(v)
22+
getValueOnly(v) = v
23+
getValueOnly(v::ForwardDiff.Dual) = v.value
24+
getValueOnly(v::Measurements.Measurement) = Measurements.value(v)
2525

2626

2727
const nClock = 100
@@ -278,12 +278,12 @@ function after!(h::EventHandler{FloatType,TimeType}, nr::Int, t::Number, tAsStri
278278
end
279279

280280

281-
#positive!(h, nr, crossing, crossingAsString, leq_mode; restart=Restart) = positive!(h, nr, getValue(crossing), crossingAsString, leq_mode; restart=restart)
281+
#positive!(h, nr, crossing, crossingAsString, leq_mode; restart=Restart) = positive!(h, nr, getValueOnly(crossing), crossingAsString, leq_mode; restart=restart)
282282

283283
function positive!(h::EventHandler{FloatType,TimeType}, nr::Int, crossing, crossingAsString::String,
284284
leq::Union{Nothing,Modia.LinearEquations{FloatType}};
285285
restart::EventRestart=Restart)::Bool where {FloatType,TimeType}
286-
crossing = getValue(crossing)
286+
crossing = getValueOnly(crossing)
287287

288288
if h.initial
289289
if !isnothing(leq) && leq.mode >= 0
@@ -330,7 +330,7 @@ end
330330
function negative!(h::EventHandler{FloatType,TimeType}, nr::Int, crossing, crossingAsString::String,
331331
leq::Union{Nothing,Modia.LinearEquations{FloatType}};
332332
restart::EventRestart=Restart)::Bool where {FloatType,TimeType}
333-
crossing = getValue(crossing)
333+
crossing = getValueOnly(crossing)
334334

335335
if h.initial
336336
if !isnothing(leq) && leq.mode >= 0
@@ -419,7 +419,7 @@ function edge!(h::EventHandler{FloatType,TimeType}, nr::Int, crossing, crossingA
419419
"iteration variables $(leq.vTear_names). This is not supported."
420420
end
421421

422-
crossing = getValue(crossing)
422+
crossing = getValueOnly(crossing)
423423

424424
if h.initial
425425
h.zPositive[nr] = crossing > convert(FloatType,0)

src/Modia.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Modia
1010

1111
const path = dirname(dirname(@__FILE__)) # Absolute path of package directory
1212
const Version = "0.9.0-dev"
13-
const Date = "2022-07-03"
13+
const Date = "2022-07-04"
1414
const modelsPath = joinpath(Modia.path, "models")
1515

1616
print(" \n\nWelcome to ")
@@ -83,13 +83,10 @@ export instantiateModel, @instantiateModel, assert, stringifyDefinition
8383
export stripUnit
8484

8585
export simulate!, linearize!, get_result
86-
#export @usingModiaPlot, usePlotPackage, usePreviousPlotPackage, currentPlotPackage
87-
export resultInfo, showResultInfo, rawSignal, getPlotSignal, defaultHeading
88-
export signalNames, timeSignalName, hasSignal
8986
export hasParameter, getParameter, getEvaluatedParameter
9087
export showParameters, showEvaluatedParameters
9188

92-
export SimulationModel, measurementToString, get_lastValue, getLastValue
89+
export SimulationModel, measurementToString, get_lastValue, getLastValue, getStateNames
9390
export positive, negative, previous, edge, after, reinit, pre
9491
export initial, terminal, isInitial, isTerminal, initLinearEquationsIteration!
9592
export get_xNames
@@ -113,11 +110,6 @@ using ModiaBase.BLTandPantelidesUtilities
113110
using ModiaBase.BLTandPantelides
114111
using ModiaBase.Differentiate
115112

116-
#import SignalTables
117-
#import SignalTables.: usePlotPackage, usePreviousPlotPackage, currentPlotPackage
118-
#import SignalTables.: resultInfo, showResultInfo, getPlotSignal, getDefaultHeading
119-
#import SignalTables.: signalNames, timeSignalName, hasSignal
120-
121113
import StaticArrays # Make StaticArrays available for the tests
122114

123115

src/Result.jl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ mutable struct ResultInfo
7272
aliasName::String # Name of non-eliminated variable
7373
aliasNegate::Bool # = true, if info[aliasName] signal must be negated
7474
id::Vector{ValuesID} # Location of the variable values with respect to ResultKind and Result
75-
_basetype # If known, basetype(signal.values/.values); if not known: Nothing
75+
_eltypeOrType # If known, eltypeOrType(signal.values/.values); if not known: Nothing
7676
value::Any # Value of constant variable (without unit)
7777

78-
ResultInfo(kind::ResultKind, signal, _basetype) = new(kind , signal, "" , false , ValuesID[] , _basetype, nothing)
79-
ResultInfo(kind::ResultKind, signal, id::ValuesID, _basetype) = new(kind , signal, "" , false , ValuesID[id], _basetype, nothing)
80-
ResultInfo(signal::SignalTables.SymbolDictType, value) = new(RESULT_CONSTANT , signal, "" , false , ValuesID[] , basetype(value), value)
78+
ResultInfo(kind::ResultKind, signal, _eltypeOrType) = new(kind , signal, "" , false , ValuesID[] , _eltypeOrType, nothing)
79+
ResultInfo(kind::ResultKind, signal, id::ValuesID, _eltypeOrType) = new(kind , signal, "" , false , ValuesID[id], _eltypeOrType, nothing)
80+
ResultInfo(signal::SignalTables.SymbolDictType, value) = new(RESULT_CONSTANT , signal, "" , false , ValuesID[] , eltypeOrType(value), value)
8181
ResultInfo(signal::SignalTables.SymbolDictType, aliasName::String, aliasNegate::Bool) = new(RESULT_ELIMINATED, signal, aliasName, aliasNegate, ValuesID[], Nothing, nothing)
8282
end
8383

@@ -130,15 +130,23 @@ mutable struct Result{FloatType,TimeType}
130130
@assert(!haskey(info, xi_info.der_x_name))
131131
x_unit = xi_info.unit
132132
der_x_unit = x_unit == "" ? "1/s" : unitAsParseableString(uparse(x_unit)/u"s")
133-
x_var = Var(unit=x_unit, start=xi_info.startOrInit, fixed=xi_info.fixed, state=true, der=xi_info.der_x_name)
133+
if x_unit == ""
134+
x_var = Var(start=xi_info.startOrInit, fixed=xi_info.fixed, state=true, der=xi_info.der_x_name)
135+
else
136+
x_var = Var(unit=x_unit, start=xi_info.startOrInit, fixed=xi_info.fixed, state=true, der=xi_info.der_x_name)
137+
end
134138
if !isnan(xi_info.nominal)
135139
x_var[:nominal] = xi_info.nominal
136140
end
137141
if xi_info.unbounded
138142
x_var[:unbounded] = true
139143
end
140-
info[xi_info.x_name] = ResultInfo(RESULT_X , x_var, FloatType)
141-
info[xi_info.der_x_name] = ResultInfo(RESULT_DER_X, Var(unit=der_x_unit), FloatType)
144+
info[xi_info.x_name] = ResultInfo(RESULT_X, x_var, FloatType)
145+
if der_x_unit == ""
146+
info[xi_info.der_x_name] = ResultInfo(RESULT_DER_X, Var(), FloatType)
147+
else
148+
info[xi_info.der_x_name] = ResultInfo(RESULT_DER_X, Var(unit=der_x_unit), FloatType)
149+
end
142150
end
143151

144152
# Fill info with w_invariant
@@ -214,12 +222,12 @@ function signalResultValues(t::AbstractVector, s::AbstractVector, resultInfo::Re
214222
id = resultInfo.id
215223
@assert(length(id) > 0)
216224
inlineValues = resultInfo.kind == RESULT_X || resultInfo.kind == RESULT_DER_X
217-
_basetype = resultInfo._basetype
225+
_eltypeOrType = resultInfo._eltypeOrType
218226
ndims_s = length(id[1].dims)
219227
if id[1].segment == -1 && ndims_s == 0
220228
# Scalar signal that is defined in every segment
221229
index = id[1].index
222-
sc = _basetype[ustrip.(ti[index]) for sk in s for ti in sk]
230+
sc = _eltypeOrType[ustrip.(ti[index]) for sk in s for ti in sk]
223231

224232
else
225233
# Find largest dims = dimsMax in all segments
@@ -248,10 +256,10 @@ function signalResultValues(t::AbstractVector, s::AbstractVector, resultInfo::Re
248256
dims = (dims1, dimsMax...)
249257
if hasMissing
250258
# Allocate target memory with missing values
251-
sc = Array{Union{_basetype,Missing}, length(dims)}(missing, dims)
259+
sc = Array{Union{_eltypeOrType,Missing}, length(dims)}(missing, dims)
252260
else
253261
# Allocate target memory with undef values
254-
sc = Array{_basetype, length(dims)}(undef, dims)
262+
sc = Array{_eltypeOrType, length(dims)}(undef, dims)
255263
end
256264

257265
# Copy subset of s-values to target sc

0 commit comments

Comments
 (0)