Skip to content

Commit 35b6f27

Browse files
committed
Tables interface for FMUSolution
- Column access defined - Restricting JLD2 to version smaller 0.4.37
1 parent f93ebd4 commit 35b6f27

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

src/FMI.jl

+8-7
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,17 @@ function __init__()
179179
import .JLD2
180180
include("extensions/JLD2.jl")
181181
end
182-
@require DataFrames="a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
183-
import .DataFrames
184-
@require CSV="336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
185-
import .CSV
186-
include("extensions/CSV.jl")
187-
end
182+
@require CSV="336ed68f-0bac-5ca0-87d4-7b16caf5d00b" begin
183+
import .CSV
184+
include("extensions/CSV.jl")
188185
end
189186
@require MAT="23992714-dd62-5051-b70f-ba57cb901cac" begin
190187
import .MAT
191-
include("extensions/MAT.jl")
188+
include("extensions/MAT.jl")
189+
end
190+
@require Tables="bd369af6-aec1-5ad0-b16a-f7cc5008161c" begin
191+
import .Tables
192+
include("extensions/Tables.jl")
192193
end
193194
end
194195

src/extensions/CSV.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
using FMIImport: FMUSolution
77

88
"""
9-
ToDo: DocString.
9+
fmiSaveSolutionCSV(solution::FMUSolution, filepath::AbstractString)
1010
1111
Saves a FMUSolution to a csv file.
12+
13+
# Arguments
14+
- `solution::FMUSolution`: The simulation results that should be saved
15+
- `filepath::AbstractString`: The path specifing where to save the results, also indicating the file format. Supports *.mat, *.csv, *.JLD2
1216
"""
13-
function fmiSaveSolutionCSV(solution::FMUSolution, filepath::AbstractString)
14-
df = DataFrames.DataFrame(time = solution.values.t)
15-
for i in 1:length(solution.values.saveval[1])
16-
df[!, Symbol(fmi2ValueReferenceToString(solution.component.fmu, solution.valueReferences[i]))] = [val[i] for val in solution.values.saveval]
17-
end
18-
CSV.write(filepath, df)
17+
function fmiSaveSolutionCSV(solution::FMUSolution, filepath::AbstractString)
18+
CSV.write(filepath, solution)
1919
end

src/extensions/JLD2.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Loads a FMUSolution. Returns a previously saved `FMUSolution`.
2121
"""
2222
function fmiLoadSolutionJLD2(filepath::AbstractString; keyword="solution")
2323
return JLD2.load(filepath, keyword)
24-
end
24+
end

src/extensions/MAT.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ function fmiSaveSolutionMAT(solution::FMUSolution, filepath::AbstractString)
2020
MAT.write(file, replace(fmi2ValueReferenceToString(solution.component.fmu, solution.valueReferences[i-1])[1], "." => "_"), v[:,i])
2121
# df[!, Symbol(fmi2ValueReferenceToString(solution.component.fmu, solution.valueReferences[i]))] = [val[i] for val in solution.values.saveval]
2222
end
23-
23+
2424
MAT.close(file)
25-
end
25+
end

src/extensions/Tables.jl

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#
2+
# Copyright (c) 2023 Andreas Heuermann
3+
# Licensed under the MIT license. See LICENSE file in the project root for details.
4+
#
5+
6+
using FMIImport: FMUSolution
7+
8+
# Declare that FMUSolution is a table
9+
Tables.istable(::Type{<:FMUSolution}) = true
10+
11+
# TODO: Define [optional] schema
12+
13+
# Column interface
14+
Tables.columnaccess(table) = true
15+
16+
function Tables.columns(solution::FMUSolution)
17+
return solution
18+
end
19+
20+
"""
21+
Retrieve a column by index.
22+
"""
23+
function Tables.getcolumn(solution::FMUSolution, i::Int)::Vector{Float64}
24+
if i == 1 # Time
25+
return solution.values.t
26+
end
27+
# Variables
28+
return [val[i-1] for val in solution.values.saveval]
29+
end
30+
31+
"""
32+
Retrieve a column by name.
33+
"""
34+
function Tables.getcolumn(solution::FMUSolution, nm::Symbol)
35+
if nm == :time # Time
36+
return solution.values.t
37+
end
38+
# Variables
39+
vr = first(fmi2StringToValueReference(solution.component.fmu, string(nm)))
40+
idx = findfirst(idx -> idx == vr, solution.valueReferences)
41+
return [val[idx] for val in solution.values.saveval]
42+
end
43+
44+
"""
45+
Return column names for a table as an indexable collection.
46+
"""
47+
function Tables.columnnames(solution::FMUSolution)
48+
names = Symbol[]
49+
push!(names, Symbol("time"))
50+
for i in 1:length(solution.values.saveval[1])
51+
var = fmi2ValueReferenceToString(solution.component.fmu, solution.valueReferences[i])
52+
append!(names, Symbol.(var))
53+
end
54+
return unique(names)
55+
end

test/Project.toml

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
[compat]
2-
julia = "1.6"
3-
41
[deps]
5-
CSV="336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
6-
DataFrames="a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
2+
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
3+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
74
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
85
FMIZoo = "724179cf-c260-40a9-bd27-cccc6fe2f195"
96
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
107
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
11-
MAT="23992714-dd62-5051-b70f-ba57cb901cac"
8+
MAT = "23992714-dd62-5051-b70f-ba57cb901cac"
129
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1310
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
11+
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1412
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1513
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
1614

15+
[compat]
16+
julia = "1.6"
17+
Tables = "1"
18+
JLD2 = "< 0.4.37"
19+
1720
[extras]
1821
CPUSummary = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"

0 commit comments

Comments
 (0)