%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First plot Electric motor map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Electric motor
% --------------

scale_EM = 3.5;

load eta_EM_map                     % Efficiency map                [-]

load w_EM_row                       % Motor speed range             [rad/s]
load T_EM_col                       % Motor torque range            [Nm]
load w_EM_max                       % Maximum motor speed           [rad/s]    
load T_EM_max                       % Maximum motor torque          [Nm]

w_EM_upper = max(w_EM_max);         % Upper limit motor speed       [rad/s]

T_EM_col = scale_EM * T_EM_col;     % Scale motor torque
T_EM_max = scale_EM * T_EM_max;


fig = figure;
set(fig,'NumberTitle', 'off')
set(fig,'Name', 'Electric Motor')

posIdx = find(eta_EM_map > 1);
eta_EM_map(posIdx) = 1./eta_EM_map(posIdx);


vvv        = [0.7,0.75,0.8,0.825,0.85,0.875,0.9,1.0,1.1,1.15,1.2,1.3,1.4];
[cc,hh]    = contour(w_EM_row, T_EM_col, eta_EM_map', vvv);
clabel(cc,hh)
hold on
plot(w_EM_max, T_EM_max)
plot(w_EM_max, -T_EM_max)
xlabel('\omega_{EM} [rad/s]')
ylabel('T_{EM} [Nm]')
title('Electric Motor map')
%plot(w_EM, T_EM, 'o')
%hold off


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Then plot Electric generator map
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Electric generator
% ------------------
scale_EG=4.2;

load eta_EG_map                     % Efficiency map                    [-]

load w_EG_row                       % Generator speed range             [rad/s]
load T_EG_col                       % Generator torque range            [Nm]
load w_EG_max                       % Maximum generator speed           [rad/s]    
load T_EG_max                       % Maximum generator torque          [Nm]

w_EG_upper = max(w_EG_max);         % Upper limit generator speed       [rad/s]

T_EG_col = scale_EG * T_EG_col;     % Scale generator torque
T_EG_max = scale_EG * T_EG_max;

fig = figure;
set(fig,'NumberTitle', 'off')
set(fig,'Name', 'Electric Generator')

vvv     = [0.7,0.75,0.8,0.83,0.85,0.87,0.88,0.89,0.90,0.91,0.92,0.93,0.94];
[cc,hh] = contour(w_EG_row, T_EG_col, eta_EG_map', vvv);
clabel(cc,hh)
hold on
plot(w_EG_max, T_EG_max)
%plot(w_EG,T_EG, 'o')
xlabel('\omega_{EG} [rad/s]')
ylabel('T_{EG} [Nm]')
title('Electric Generator map')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Generate COMBUSTION ENGINE map data 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Engine parameters
% V_d=2.54e-3;
% w_CE_idle=750/60*2*3.14;
% P_CE_idle=1.5e5*2.54e-3*750/60/2;
% P_aux=321;
% e=0.35;
% PI=1;
% p_me0g=0.5e5;

%T_CE_idle = P_CE_idle / w_CE_idle;      % [Nm]

load p_me_col;
load p_me_max;
load w_CE_row;
load w_CE_max;
load eta_CE_map;

V_d = 2.5e-3;

T_CE_col = p_me_col .* V_d/(4*pi);
T_CE_max = p_me_max .* V_d/(4*pi); 

load w_CE_max;
load eta_CE_map;

% Make a contour plot 
fig = figure;
set(fig,'NumberTitle', 'off')
set(fig,'Name', 'Combustion Engine usage (Ordinary Vehicle)')

val_vec=[0.325 0.3 0.275 0.2  0.1];
cs=contour(w_CE_row, T_CE_col, eta_CE_map, val_vec);

% Set some titles etc.
title('Combustion Engine map and usage (ordinary)')
xlabel('\omega_{CE} [rad/s]')
ylabel('T_{CE} [Nm]')
clabel(cs)

% Plot the max torque characteristics and the operating points from the simulation
hold on
plot(w_CE_row, T_CE_max)
%plot(w_CE, T_CE, 'o')
hold off
