|
1 | 1 | %% Script to run HEV_SeriesParallel in parallel
|
| 2 | +% Copyright 2013-2020 The MathWorks(TM), Inc. |
2 | 3 |
|
3 |
| -% Resave model in PCT directory |
4 |
| -cd(fileparts(which('HEV_SeriesParallel.slx'))); |
5 |
| -cd('PCT'); |
| 4 | +% Move to folder where script is saved |
| 5 | +cd(fileparts(which(mfilename))); |
| 6 | + |
| 7 | +% Open model and save under another name for test |
6 | 8 | orig_mdl = 'HEV_SeriesParallel';
|
7 |
| -mdl = [orig_mdl '' ]; % Normally would add suffix for PCT copy |
8 | 9 | open_system(orig_mdl);
|
9 |
| -save_system(orig_mdl,[pwd filesep mdl]); |
| 10 | +mdl = [orig_mdl '_pct_temp' ]; |
| 11 | +save_system(orig_mdl,mdl); |
| 12 | + |
| 13 | +%% Configure model for tests |
10 | 14 |
|
11 | 15 | % Set up parameters
|
12 | 16 | HEV_Vehicle_Mass = HEV_Param.Vehicle.Mass;
|
13 | 17 | HEV_Model_Driver_Ki = 0.04;
|
14 | 18 |
|
15 |
| -% Set up model - vehicle configuration, other settings |
| 19 | +% Model settings |
16 | 20 | Select_HEV_Model_Systems(mdl,'Sys BC VS',HEV_Configs);
|
17 | 21 | HEVSP_tictoc('off');
|
18 | 22 | set_param([mdl '/Vehicle Dynamics/Simple'],'mass','HEV_Vehicle_Mass');
|
|
23 | 27 |
|
24 | 28 | save_system(mdl);
|
25 | 29 |
|
26 |
| -%% GENERATE PARAMETER SETS |
| 30 | +%% Generate parameter sets |
27 | 31 | Mass_array = [1000:40:1600];
|
28 | 32 |
|
29 | 33 | for i=1:length(Mass_array)
|
30 | 34 | simInput(i) = Simulink.SimulationInput(mdl);
|
31 | 35 | simInput(i) = simInput(i).setVariable('HEV_Vehicle_Mass',Mass_array(i));
|
32 | 36 | end
|
33 | 37 |
|
34 |
| -%% Run |
35 |
| -clear simOut |
36 |
| -gcp('nocreate'); |
| 38 | +%% Run one simulation to see time used |
| 39 | +timerVal = tic; |
| 40 | +sim(mdl) |
| 41 | +Elapsed_Sim_Time_single = toc(timerVal); |
| 42 | +disp(['Elapsed Simulation Time Single Run: ' num2str(Elapsed_Sim_Time_single)]); |
| 43 | + |
| 44 | +%% Run parameter sweep in parallel |
| 45 | +timerVal = tic; |
| 46 | +simOut = parsim(simInput,'ShowSimulationManager','on',... |
| 47 | + 'ShowProgress','on','UseFastRestart','on',... |
| 48 | + 'TransferBaseWorkspaceVariables','on'); |
| 49 | +Elapsed_Time_Time_parallel = toc(timerVal); |
| 50 | + |
| 51 | +%% Calculate elapsed time less setup of parallel |
| 52 | +Elapsed_Time_Sweep = ... |
| 53 | + (datenum(simOut(end).SimulationMetadata.TimingInfo.WallClockTimestampStop) - ... |
| 54 | + datenum(simOut(1).SimulationMetadata.TimingInfo.WallClockTimestampStart)) * 86400; |
| 55 | +disp(['Elapsed Sweep Time Total: ' sprintf('%5.2f',Elapsed_Time_Sweep)]); |
| 56 | +disp(['Elapsed Sweep Time/(Num Tests): ' sprintf('%5.2f',Elapsed_Time_Sweep/length(simOut))]); |
| 57 | + |
| 58 | +%% Plot results |
| 59 | +plot_sim_res(simOut,'Parallel Test',Elapsed_Time_Time_parallel) |
| 60 | + |
| 61 | + |
| 62 | +%% Close parallel pool |
| 63 | +delete(gcp); |
| 64 | + |
| 65 | +%% Cleanup directory |
| 66 | + |
| 67 | +bdclose(mdl); |
| 68 | +delete([mdl '.slx']); |
37 | 69 |
|
38 |
| -% Series |
39 |
| -simOut = sim(simInput,'ShowProgress','on','UseFastRestart','on'); |
40 | 70 |
|
41 |
| -%% Plots |
42 |
| -if ~exist('h4_hev_seriesparallel_pct', 'var') || ... |
43 |
| - ~isgraphics(h4_hev_seriesparallel_pct, 'figure') |
44 |
| - h4_hev_seriesparallel_pct = figure('Name', 'hev_seriesparallel_pct'); |
45 |
| -end |
46 |
| -figure(h4_hev_seriesparallel_pct) |
47 |
| -clf(h4_hev_seriesparallel_pct) |
48 | 71 |
|
49 |
| -temp_colororder = get(gca,'defaultAxesColorOrder'); |
50 |
| -set(gcf,'Position',[11 356 545 293]); |
| 72 | +%% Plot Function |
| 73 | +function plot_sim_res(simOut,annotation_str,elapsed_time) |
| 74 | + |
| 75 | +% Plot Results |
| 76 | +fig_handle_name = 'h4_hev_seriesparallel_pct'; |
| 77 | + |
| 78 | +handle_var = evalin('base',['who(''' fig_handle_name ''')']); |
| 79 | +if(isempty(handle_var)) |
| 80 | + evalin('base',[fig_handle_name ' = figure(''Name'', ''' fig_handle_name ''');']); |
| 81 | +elseif ~isgraphics(evalin('base',handle_var{:})) |
| 82 | + evalin('base',[fig_handle_name ' = figure(''Name'', ''' fig_handle_name ''');']); |
| 83 | +end |
| 84 | +figure(evalin('base',fig_handle_name)) |
| 85 | +clf(evalin('base',fig_handle_name)) |
51 | 86 |
|
52 | 87 | for i=length(simOut):-1:1
|
53 | 88 | data = simOut(i).Motor;
|
|
57 | 92 | title('Motor Torque','FontSize',16,'FontWeight','Bold');
|
58 | 93 | xlabel('Time (s)','FontSize',12,'FontWeight','Bold');
|
59 | 94 | ylabel('Motor Torque','FontSize',12,'FontWeight','Bold');
|
| 95 | +Mass_array = evalin('base','Mass_array'); |
60 | 96 | legend(cellstr(num2str(fliplr(Mass_array(1:1:end))')),'FontSize',10);
|
61 | 97 |
|
62 |
| -%% Parallel |
63 |
| -simOut = parsim(simInput,'ShowProgress','on','UseFastRestart','on','ShowSimulationManager','on','TransferBaseWorkspaceVariables','on'); |
64 |
| - |
65 |
| -%% CLOSE PARALLEL POOL |
66 |
| -delete(gcp); |
67 |
| - |
68 |
| -%% CLEANUP DIR |
69 |
| -%{x |
70 |
| - |
71 |
| -bdclose(mdl); |
72 |
| -movefile([mdl '.slx'],[mdl '_pct_temp.slx']); |
73 |
| -!rmdir slprj /S/Q |
74 |
| -delete([mdl '.slxc']); |
75 |
| - |
76 |
| -%}x |
77 |
| - |
78 |
| -% Copyright 2013-2019 The MathWorks(TM), Inc. |
| 98 | +text(0.05,0.05,[annotation_str ', Elapsed Time: ' num2str(elapsed_time)],'Color',[1 1 1]*0.6,'Units','Normalized'); |
| 99 | +end |
79 | 100 |
|
0 commit comments