Skip to content

Commit

Permalink
New version 0.3.1; Two new views on results added (SignalView and Fla…
Browse files Browse the repository at this point in the history
…ttenedSignalView).
  • Loading branch information
MartinOtter committed Jun 28, 2021
1 parent 96f2fbb commit b30a69c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ModiaResult"
uuid = "16a87621-1533-42f6-8e19-4a825980cec2"
authors = ["[email protected] <[email protected]>"]
version = "0.3.1-dev"
version = "0.3.1"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
4 changes: 4 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ you might utilize the following approach.

## Release Notes

### Version 0.3.1

- Two new views on results added (SignalView and FlattenedSignalView).


### Version 0.3

Expand Down
75 changes: 73 additions & 2 deletions src/ResultViews.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ using Unitful


"""
OneValueVector{T}(value,nvalues) < AbstractVector{T}
vec = OneValueVector(value,nvalues)
Return a vector with identical elements
Provide a vector view of one value.
# Example
```julia
vec1 = OneValueVector(2.0, 10) # = Vector{Float64} with length(vec) = 10
vec2 = OneValueVector(true, 4) # = Vector{Bool} with length(vec) = 4
```
"""
struct OneValueVector{T} <: AbstractVector{T}
value::T
Expand All @@ -26,3 +33,67 @@ end
Base.getindex(v::FlattendVector, i::Int) = view(v.vectorOfVector[i], v.ibeg:v.iend)
Base.size( v::FlattendVector) = (length(v.vectorOfVector), )
Base.IndexStyle(::Type{<:FlattendVector}) = IndexLinear()





"""
signal = SignalView(result,signalIndex,negate)
Return a view of signal `result[ti][signalIndex]` where `ti` is the index
of the time instant and `signalIndex` is the index of the variable.
If negate=true, negate the signal values.
```julia
signal[ti] = negate ? -result[ti][signalIndex] : result[ti][signalIndex]
```
"""
struct SignalView{T} <: AbstractVector{T}
result
signalIndex::Int
negate::Bool
SignalView{T}(result,signalIndex,negate) where {T} = new(result,signalIndex,negate)
end
SignalView(result,signalIndex,negate) = SignalView{typeof(result[1][signalIndex])}(result,signalIndex,negate)

Base.getindex(signal::SignalView, ti::Int) = signal.negate ? -signal.result[ti][signal.signalIndex] : signal.result[ti][signal.signalIndex]
Base.size( signal::SignalView) = (length(signal.result),)
Base.IndexStyle(::Type{<:SignalView}) = IndexLinear()



"""
signal = FlattenedSignalView(result,signalStartIndex,signalSize,negate)
Return a view of flattened signal `result[ti][signalStartIndex:signalEndIndex]`
where `ti` is the index of the time instant and the signal of size `signalSize` has been
flattened into a vector (`signalEndIndex = signalStartIndex + prod(signalSize) - 1`).
If negate=true, negate the signal values.
```julia
baseSignal = reshape(result[ti][signalStartIndex:signalStartIndex+prod(signalSize)-1], signalSize)
signal[ti] = negate ? -baseSignal : baseSignal
```
"""
struct FlattenedSignalView{T} <: AbstractVector{T}
result
signalStartIndex::Int
signalEndIndex::Int
signalSize::Tuple
negate::Bool
FlattenedSignalView{T}(result,signalStartIndex,signalSize,negate) where {T} =
new(result,signalStartIndex,signalStartIndex+prod(signalSize)-1,signalSize,negate)
end
FlattenedSignalView(result,signalStartIndex,signalSize,negate) = FlattenedSignalView{typeof(result[1][signalStartIndex])}(result,signalStartIndex,signalSize,negate)

Base.getindex(signal::FlattenedSignalView, ti::Int) = signal.signalSize == () ?
(signal.negate ? -signal.result[ti][signal.signalStartIndex]
: signal.result[ti][signal.signalStartIndex]) :
(signal.negate ? -reshape(signal.result[ti][signal.signalStartIndex:signal.signalEndIndex],signal.signalSize) :
reshape(signal.result[ti][signal.signalStartIndex:signal.signalEndIndex],signal.signalSize))
Base.size( signal::FlattenedSignalView) = (length(signal.result),)
Base.IndexStyle(::Type{<:FlattenedSignalView}) = IndexLinear()



0 comments on commit b30a69c

Please sign in to comment.