Skip to content

Commit 917cd8e

Browse files
committed
add plot()
1 parent 54255c0 commit 917cd8e

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
1010
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1111
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
1212
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
13+
NamedTupleTools = "d9ec5142-1e00-5aa0-9d6a-321866360f50"
1314
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
1415
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1516
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
@@ -34,6 +35,7 @@ JSON3 = "1.14"
3435
LinearAlgebra = "1"
3536
MLStyle = "0.4"
3637
MacroTools = "0.5"
38+
NamedTupleTools = "0.14.3"
3739
OrderedCollections = "1.8.0"
3840
Parameters = "0.12"
3941
Plots = "1.40"

ext/plot.jl

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,47 @@ function Plots.plot!(
11121112
kwargs...,
11131113
)
11141114

1115+
model = CTModels.model(sol)
1116+
1117+
# check if the plot is empty
1118+
if isempty(p.series_list)
1119+
1120+
attr = NamedTuple((Symbol(key),value) for (key,value) in p.attr if key != :layout)
1121+
1122+
pnew = __initial_plot(
1123+
sol,
1124+
description...;
1125+
layout=layout,
1126+
control=control,
1127+
model=model,
1128+
size=__size_plot(
1129+
sol,
1130+
model,
1131+
control,
1132+
layout,
1133+
description...;
1134+
state_style=state_style,
1135+
control_style=control_style,
1136+
costate_style=costate_style,
1137+
path_style=path_style,
1138+
dual_style=dual_style,
1139+
),
1140+
state_style=state_style,
1141+
control_style=control_style,
1142+
costate_style=costate_style,
1143+
path_style=path_style,
1144+
dual_style=dual_style,
1145+
attr...,
1146+
kwargs...,
1147+
)
1148+
1149+
# replace p by pnew, must have a side effect
1150+
for k fieldnames(typeof(p))
1151+
setfield!(p, k, getfield(pnew, k))
1152+
end
1153+
1154+
end
1155+
11151156
# plot the solution with infos from the model
11161157
return __plot!(
11171158
p,
@@ -1124,7 +1165,7 @@ function Plots.plot!(
11241165
state_style=state_style,
11251166
control_style=control_style,
11261167
costate_style=costate_style,
1127-
model=CTModels.model(sol),
1168+
model=model,
11281169
state_bounds_style=state_bounds_style,
11291170
control_bounds_style=control_bounds_style,
11301171
time_style=time_style,

test/extras/plot_manual.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using Pkg
2+
Pkg.activate(".")
3+
14
using CTModels
25
using Plots
36

@@ -37,6 +40,8 @@ function get_solution()
3740
end
3841
CTModels.definition!(pre_ocp, definition) # does not correspond to the solution
3942

43+
CTModels.time_dependence!(pre_ocp; autonomous=false)
44+
4045
# build model
4146
ocp = CTModels.build_model(pre_ocp)
4247

@@ -127,7 +132,15 @@ end;
127132

128133
sol = get_solution();
129134

135+
#
136+
plt = plot(size=(800,800))
137+
pp = plot!(plt, sol)
138+
139+
pp
140+
plt
141+
130142
# layout = :group
143+
131144
plot(sol; layout=:group, control=:components)
132145
plot(sol; layout=:group, control=:norm)
133146
plot(sol; layout=:group, control=:all)

0 commit comments

Comments
 (0)