-
Notifications
You must be signed in to change notification settings - Fork 2
/
FMUs.m
187 lines (165 loc) · 6.79 KB
/
FMUs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
%% init
clear
clf
clc
current_folder = fileparts(mfilename('fullpath'));
% timesn(1, :) = cell2mat(times)
t = 50;
rep = 10;
% timeseries = [1*ones(rep, 1); 5*ones(rep, 1); 10*ones(rep, 1)];
timeseries = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
%%
for j = 1:rep
for i = 1:length(timeseries)
k = i*j;
t = timeseries(i);
s = 0.01;
clear names times;
names = {};
times = {};
clf
clc
disp(['COMMENCING RUN n.', num2str(k), ', simulating for ', num2str(t), ' seconds for ', num2str(j), 'x time'])
%% DYMOLA ME
fmu_path=fullfile(current_folder,'SmithDymolaME.fmu');
tic
fmu=FMUModelME2(fmu_path);
disp('Loading FMU - Dymola ME:');
toc
output.name={'aorta.q_in.pressure','aorticValve.q_in.q'};
[time, yout, yname] = simulate(fmu, [0,t], 'Output', output);
disp('simulating FMU Dymola ME:')
toc
times{end+1} = toc;
names{end+1} = "DYMOLA ME";
figure(1);subplot(211);hold off;plot(time, yout(:, 1)/133.32);
figure(1);subplot(212);hold off;plot(time, yout(:, 2)*1000*1000/60);
titlies(names);
%% DYMOLA CS
fmu_path=fullfile(current_folder,'SmithDymolaCS.fmu');
tic
fmu=FMUModelCS2(fmu_path);
disp('FMU - Dymola CS loading:');
toc
output.name={'aorta.q_in.pressure','aorticValve.q_in.q'};
[time, yout, yname] = simulate(fmu, 0:s:t, 'Output', output);
disp('Dymola CS:')
toc
times{end+1} = toc;
names{end+1} = "DYMOLA CS";
figure(1);subplot(211);hold on;plot(time, yout(:, 1)/133.32);
figure(1);subplot(212);hold on;plot(time, yout(:, 2)*1000*1000/60);
titlies(names);
%% DYMOLA CS CVODE
fmu_path=fullfile(current_folder,'SmithDymolaCvodeCS.fmu');
tic
fmu=FMUModelCS2(fmu_path);
disp('FMU - Dymola Cvode CS:');
toc
output.name={'aorta.q_in.pressure','aorticValve.q_in.q'};
[time, yout, yname] = simulate(fmu, 0:s:t, 'Output', output);
disp('FMU Dymola Cvode CS:')
toc
times{end+1} = toc;
names{end+1} = "DYMOLA CS CVODE";
figure(1);subplot(211);hold on;plot(time, yout(:, 1)/133.32);
figure(1);subplot(212);hold on;plot(time, yout(:, 2)*1000*1000/60);
titlies(names);
%% SIMULINK FMIToolbox CS - output
tic
simout = sim('SmithCS', 'StopTime', num2str(t));
disp('Simulink FMIToolbox CS')
toc
times{end+1} = toc;
names{end+1} = "Simulink FMIToolbox CS";
figure(1);subplot(211);hold on;plot(simout.simout.Time, simout.simout.Data/133.32);
figure(1);subplot(212);hold on;plot(simout.simout.Time, zeros(size(simout.simout.Time)));
titlies(names);
%% OpenModelica simulator
[time, yout, elapsed] = RunOMSim('FMITest.Smith_patSpec.exe', {'aorta.q_in.pressure','aorticValve.q_in.q'}, t, s);
times{end+1} = elapsed;
names{end+1} = "OpenModelica EXE";
figure(1);subplot(211);hold on;plot(time, yout(:, 1)/133.32);
figure(1);subplot(212);hold on;plot(time, yout(:, 2)*1000*1000/60);
titlies(names);
%% REFERENCE MATLAB IMLPEMENTATION
[time, yout, elapsed] = Smith_Script(t);
times{end+1} = elapsed;
names{end+1} = "Matlab ODE15";
figure(1);subplot(211);hold on;plot(time, yout(:, 1));
figure(1);subplot(212);hold on;plot(time, zeros(size(time)));
titlies(names);
%% commented out - crashes or not working
%% SIMULINK FMIToolbox ME - output is rubbish and error occures
% An error occurred while running the simulation and the simulation was terminated
% Caused by:
% Solver encountered an error while simulating model 'SmithME' at time 9.1569641639858244 and cannot continue. Please check the model for errors.
% Nonlinear iteration is not converging with step size reduced to hmin (0.0000000000000000E+000) at time 9.1569641639857900E+000. Try reducing the minimum step size and/or relax the relative error tolerance.
%% SIMULINK 2018a FMI block - not working
% % Value type mismatch for parameter 'aorta' in 'SmithCS/FMU'.
% % Caused by:
% % Value type does not match the structure of variable 'aorta' defined in the modelDescription.xml file.
% % Component:Simulink | Category:Block error
%
%% OPENMODELICA ME1 - broken output
%
% fmu_path=fullfile(current_folder,'SmithOMME1.fmu');
% tic
% fmu=FMUModelME1(fmu_path);
% disp('Loading FMU:');
% toc
% output.name={'aorta.q_in.pressure','aorticValve.q_in.q'};
% [time, yout, yname] = simulate(fmu, 0:s:t, 'Output', output);
% disp('simulating FMU Dymola Cvode CS:')
% toc
% figure(1);subplot(211);hold on;plot(time, yout(:, 1)/133.32);
% figure(1);subplot(212);hold on;plot(time, yout(:, 2)*1000*1000/60);
%
%% OPENMODELICA ME2 - not working
%
% fmu_path=fullfile(current_folder,'SmithOMME2.fmu');
% tic
% fmu=FMUModelME2(fmu_path);
% disp('Loading FMU:');
% toc
% output.name={'aorta.q_in.pressure','aorticValve.q_in.q'};
% [time, yout, yname] = simulate(fmu, [0 t], 'Output', output);
% disp('simulating FMU Dymola Cvode CS:')
% toc
% figure(1);subplot(211);hold on;plot(time, yout(:, 1)/133.32);
% figure(1);subplot(212);hold on;plot(time, yout(:, 2)*1000*1000/60);
%
%
%% OPENMODELICA CS2 - crashes the matlab (2018a)
%
% fmu_path=fullfile(current_folder,'SmithOMCS2.fmu');
% tic
% fmu=FMUModelME2(fmu_path);
% disp('Loading FMU:');
% toc
% output.name={'aorta.q_in.pressure','aorticValve.q_in.q'};
% [time, yout, yname] = simulate(fmu, 0:s:t, 'Output', output);
% disp('simulating FMU Dymola Cvode CS:')
% toc
% figure(1);subplot(211);hold on;plot(time, yout(:, 1)/133.32);
% figure(1);subplot(212);hold on;plot(time, yout(:, 2)*1000*1000/60);
%% output legends
%% populate timeseries
timesn(i, j, :) = cell2mat(times);
end
end
%% Plot the time results
colors = get(gca,'ColorOrder');
figure(2);clf;hold on;
markers = {'.', 'o', 'x', '+', 'v', '*', 's', 'd', '^', '<', '>', 'p', 'h'};
handles = [];
for method = 1:length(names)
for j = 1:size(timesn, 2)
plot(timeseries, squeeze(timesn(:, j, method)), markers{method}, 'Color', colors(method, :));
end
handles(method) = plot(timeseries, mean(squeeze(timesn(:, :, method)), 2), 'LineWidth', 2, 'Color', colors(method, :));
end
title('Simulation times');
xlabel('Simulated time [s]');
ylabel('Elapsed time [s]')
legend(handles, names);