%% =========================================================================
%% SIMULASI MOTOR LISTRIK 1-FASA & KONVERSI ENERGI TERMAL-OPTIK INDUSTRI
%% Mata Kuliah   : Teknik Tenaga Listrik (Program Studi S-1 Teknik Mesin)
%% Dosen Pengampu: Novalio Daratha, S.T., M.Sc., Ph.D.
%% Institusi     : Fakultas Teknik, Universitas Bengkulu
%% =========================================================================
%% Fitur Pemodelan:
%% 1. karakteristik_motor_1fasa.png:
%%    (a) Teori Medan Putar Ganda (Double-Revolving Field): Torsi Maju (T_f),
%%        Torsi Mundur (-T_b), dan Torsi Bersih (T_net) dengan T_start = 0.
%%    (b) Komparasi Kurva Torsi-Kecepatan: Split-Phase, Capacitor-Start
%%        (Saklar Sentrifugal lepas pada 75% n_s), PSC, dan Shaded-Pole.
%% 2. profil_pemanas_furnace.png:
%%    (a) Profil Kenaikan Temperatur T(t) & Konsumsi Energi Dapur Pemanas
%%        Perlakuan Panas Baja (500 kg, 25 C -> 850 C) untuk variasi daya
%%        elemen pemanas (Star vs Delta) & efisiensi termal (\eta_{th}).
%%    (b) Komparasi Efikasi Luminus (lm/W) & Biaya Siklus Hidup (Life-Cycle
%%        Cost 50.000 Jam) Teknologi Pencahayaan Bengkel/Pabrik Industri.
%% =========================================================================

clear; clc; close all;
graphics_toolkit('qt');
set(0, 'defaultaxesfontname', 'Helvetica');
set(0, 'defaultaxesfontsize', 10);

fprintf('=== Memulai Simulasi Motor 1-Fasa & Sistem Termal-Optik Industri ===\n');

%% =========================================================================
%% BAGIAN 1: KARAKTERISTIK MOTOR INDUKSI 1-FASA (karakteristik_motor_1fasa.png)
%% =========================================================================
ns = 1500;                          % Kecepatan sinkron 4-kutub, 50 Hz (rpm)
n_vec = linspace(0, ns, 400);       % Rentang kecepatan putar (0 s.d. 1500 rpm)
s = (ns - n_vec) ./ ns;             % Slip maju s_f = s
s(end) = 1e-5;                      % Hindari pembagian nol pada n = ns
sb = 2 - s;                         % Slip mundur s_b = 2 - s

% --- 1A. Model Teori Medan Putar Ganda (Double-Revolving Field Theory) ---
% Parameter Kloss / Thevenin dengan s_max = R2 / Xeq = 0.25 (n_pullout = 1125 rpm)
R2_drf = 0.25;
Xeq_drf = 1.00;

% Torsi medan maju (Forward torque) & Torsi medan mundur (Backward torque)
Tf_raw = (R2_drf ./ s) ./ ((R2_drf ./ s).^2 + Xeq_drf^2);
Tb_raw = (R2_drf ./ sb) ./ ((R2_drf ./ sb).^2 + Xeq_drf^2);
T_net_raw = Tf_raw - Tb_raw;

% Normalisasi sehingga torsi puncak (pull-out) T_net mencapai ~205% T_FL
scale_fl = 205.0 / max(T_net_raw);

Tf = Tf_raw .* scale_fl;
Tb = Tb_raw .* scale_fl;
T_net = T_net_raw .* scale_fl;
Tf(end) = 0;
T_net(end) = -Tb(end);

% Titik kerja beban penuh (T_net = 100% pada daerah kerja stabil n > 1200 rpm)
idx_fl = find(n_vec > 1200 & T_net <= 100, 1);
n_fl = n_vec(idx_fl);

% --- 1B. Komparasi 4 Jenis Motor Induksi 1-Fasa (% Torsi Beban Penuh) ---
T_main_run = max(T_net, 0);
n_sw = 0.75 * ns;                   % Saklar sentrifugal membuka pada 75% ns = 1125 rpm
idx_sw = find(n_vec >= n_sw, 1);

% 1. Motor Capacitor-Start (T_start = 330%, puncak ~355%, lepas saklar di 1125 rpm)
T_aux_cap = 330 + 45 .* sin(0.75 * pi .* (n_vec ./ n_sw)) - 75 .* (n_vec ./ n_sw).^2.2;
T_capstart = T_main_run;
T_capstart(1:idx_sw) = T_aux_cap(1:idx_sw);

% 2. Motor Split-Phase / Fasa-Belah (T_start = 160%, puncak ~245%, lepas saklar di 1125 rpm)
T_aux_split = 160 + 85 .* sin(0.72 * pi .* (n_vec ./ n_sw));
T_split = T_main_run;
T_split(1:idx_sw) = T_aux_split(1:idx_sw);

% 3. Motor Permanent-Split Capacitor / PSC (Tanpa saklar, T_start = 85%, halus & efisien)
u_psc = (n_vec ./ ns);
T_psc = (85 .* (1 - u_psc) + 275 .* (u_psc.^0.9) .* (1 - u_psc.^3.2)) .* (1 - u_psc.^10);
T_psc(end) = 0;

% 4. Motor Shaded-Pole / Kutub Bayangan (Tanpa saklar, T_start = 50%, slip tinggi)
T_shaded = (50 .* (1 - u_psc) + 150 .* (u_psc.^0.85) .* (1 - u_psc.^2.2)) .* (1 - u_psc.^6);
T_shaded(end) = 0;

% --- Plot Figur 1 ---
hFig1 = figure('visible', 'off', 'position', [50, 50, 1200, 490]);

% Subplot 1: Teori Medan Putar Ganda
subplot(1, 2, 1);
plot(n_vec, Tf, 'b--', 'linewidth', 2.0); hold on;
plot(n_vec, -Tb, 'r-.', 'linewidth', 2.0);
plot(n_vec, T_net, 'k-', 'linewidth', 2.5);
plot([0 ns], [0 0], 'color', [0.45 0.45 0.45], 'linewidth', 1.1);
plot(0, 0, 'ro', 'markersize', 8, 'markerfacecolor', 'y');
plot(n_fl, 100, 'bo', 'markersize', 7, 'markerfacecolor', 'g');

text(35, -55, sprintf('Titik Start (n = 0 rpm):\nT_{start} = T_f(0) - T_b(0) = 0%%\n(Tidak Bisa Start Sendiri!)'), ...
     'fontsize', 8, 'fontweight', 'bold', 'color', [0.75 0 0]);
text(680, -108, 'Torsi Mundur -T_b (s_b = 2 - s)', 'fontsize', 8.5, 'color', [0.8 0 0], 'fontweight', 'bold');
text(740, 108, sprintf('Beban Penuh (%.0f rpm, 100%%)', n_fl), 'fontsize', 8, 'fontweight', 'bold', 'color', [0 0.45 0.15]);

hold off; grid on;
title('(a) Teori Medan Putar Ganda (Belitan Tunggal)', 'fontsize', 10, 'fontweight', 'bold');
xlabel('Kecepatan Putar Rotor, n (rpm)', 'fontsize', 10);
ylabel('Torsi Elektromagnetik (% Torsi Nominal)', 'fontsize', 10);
legend({'Torsi Medan Maju, +T_f (Slip s)', ...
        'Torsi Medan Mundur, -T_b (Slip 2-s)', ...
        'Torsi Bersih, T_{net} = T_f - T_b', ...
        'Garis Torsi Nol', ...
        'Titik Start (n=0, T_{start}=0)'}, 'location', 'northwest', 'fontsize', 7.8);
xlim([0 1500]); ylim([-150 285]);

% Subplot 2: Perbandingan Kurva Torsi-Kecepatan Motor 1-Fasa
subplot(1, 2, 2);
plot(n_vec, T_capstart, 'r-', 'linewidth', 2.4); hold on;
plot(n_vec, T_split, 'b-', 'linewidth', 2.2);
plot(n_vec, T_psc, 'g-.', 'linewidth', 2.1);
plot(n_vec, T_shaded, 'm--', 'linewidth', 2.0);
plot([n_sw n_sw], [0 380], 'k:', 'linewidth', 1.4);
plot([0 1500], [100 100], 'k--', 'linewidth', 1.0);
plot(n_sw, T_aux_cap(idx_sw), 'ko', 'markersize', 6.5, 'markerfacecolor', 'y');
plot(n_sw, T_aux_split(idx_sw), 'ko', 'markersize', 6.5, 'markerfacecolor', 'y');

text(610, 342, sprintf('Saklar Sentrifugal Lepas\n(75%% n_s = 1125 rpm)'), ...
     'fontsize', 8, 'fontweight', 'bold', 'color', [0.15 0.15 0.15]);
text(45, 112, 'Beban Penuh (100% T_{FL})', 'fontsize', 8, 'color', [0.25 0.25 0.25]);

hold off; grid on;
title('(b) Kurva Torsi-Kecepatan 4 Jenis Motor Induksi 1-Fasa', 'fontsize', 10, 'fontweight', 'bold');
xlabel('Kecepatan Putar Rotor, n (rpm)', 'fontsize', 10);
ylabel('Torsi Poros (% Torsi Beban Penuh)', 'fontsize', 10);
legend({'Capacitor-Start (T_{st}\approx330%, Kompresor/Pompa)', ...
        'Split-Phase (T_{st}\approx160%, Gergaji/Mesin Cuci)', ...
        'Permanent-Split Capacitor / PSC (Blower/AC)', ...
        'Shaded-Pole (T_{st}\approx50%, Kipas Kecil)'}, 'location', 'southwest', 'fontsize', 7.6);
xlim([0 1500]); ylim([0 385]);

print(hFig1, 'karakteristik_motor_1fasa.png', '-dpng', '-r300');
fprintf('1. Grafik karakteristik_motor_1fasa.png berhasil diperbarui (300 DPI).\n');
close(hFig1);

%% =========================================================================
%% BAGIAN 2: DAPUR PEMANAS BAJA & FOTOMETRI INDUSTRI (profil_pemanas_furnace.png)
%% =========================================================================
m_steel = 500;                      % Massa benda kerja baja (kg)
cp_steel = 0.50;                    % Kalor jenis baja (kJ/kg.K)
T_amb = 25;                         % Suhu ruang awal (C)
T_target = 850;                     % Suhu austenisasi hardening baja (C)
C_th = m_steel * cp_steel;          % Kapasitansi termal beban = 250 kJ/K

t_min = linspace(0, 120, 300);      % Waktu pemanasan (menit: 0 s.d. 120 menit)
t_sec = t_min .* 60;                % Waktu dalam detik
U_loss = 0.012;                     % Koefisien rugi panas dinding tungku (kW/K)

% Skenario 1: Bank Pemanas Hubung Delta (P_Delta = 90 kW, Efisiensi Termal 82%)
P1 = 90; eta1 = 0.82;
T_inf1 = T_amb + (eta1 * P1) / U_loss;
T_prof1 = min(T_amb + (T_inf1 - T_amb) .* (1 - exp(-(U_loss / C_th) .* t_sec)), T_target);

% Skenario 2: Bank Pemanas Standar (P = 60 kW, Efisiensi Termal 78%)
P2 = 60; eta2 = 0.78;
T_inf2 = T_amb + (eta2 * P2) / U_loss;
T_prof2 = min(T_amb + (T_inf2 - T_amb) .* (1 - exp(-(U_loss / C_th) .* t_sec)), T_target);

% Skenario 3: Isolasi Aus (P = 60 kW, Efisiensi Termal Turun 58%)
P3 = 60; eta3 = 0.58; U_loss3 = 0.022;
T_inf3 = T_amb + (eta3 * P3) / U_loss3;
T_prof3 = T_amb + (T_inf3 - T_amb) .* (1 - exp(-(U_loss3 / C_th) .* t_sec));

% Skenario 4: Bank Pemanas Hubung Bintang / Star (P_Star = 1/3 P_Delta = 30 kW)
P4 = 30; eta4 = 0.82;
T_inf4 = T_amb + (eta4 * P4) / U_loss;
T_prof4 = T_amb + (T_inf4 - T_amb) .* (1 - exp(-(U_loss / C_th) .* t_sec));

idx_t1 = find(T_prof1 >= T_target, 1);
t_reach1 = t_min(idx_t1); E_kwh1 = P1 * (t_reach1 / 60);

idx_t2 = find(T_prof2 >= T_target, 1);
t_reach2 = t_min(idx_t2); E_kwh2 = P2 * (t_reach2 / 60);

% --- 2B. Komparasi Teknologi Pencahayaan Bengkel Mesin (50.000 Jam Operasi) ---
tech_idx = 1:4;
efficacy_lmW = [18, 82, 95, 155];
lcc_juta_rp  = [415, 108, 92, 52];

hFig2 = figure('visible', 'off', 'position', [50, 50, 1200, 490]);

% Subplot 1: Profil Kenaikan Temperatur Dapur Baja
subplot(1, 2, 1);
plot(t_min, T_prof1, 'r-', 'linewidth', 2.4); hold on;
plot(t_min, T_prof2, 'b-', 'linewidth', 2.2);
plot(t_min, T_prof3, 'm--', 'linewidth', 2.0);
plot(t_min, T_prof4, 'g-.', 'linewidth', 2.0);
plot([0 120], [T_target T_target], 'k:', 'linewidth', 1.4);
plot(t_reach1, T_target, 'ro', 'markersize', 7, 'markerfacecolor', 'y');
plot(t_reach2, T_target, 'bo', 'markersize', 7, 'markerfacecolor', 'y');

text(t_reach1 - 2, 785, sprintf('%.0f mnt\n(%.1f kWh)', t_reach1, E_kwh1), ...
     'fontsize', 8, 'fontweight', 'bold', 'color', [0.75 0 0]);
text(t_reach2 + 2, 785, sprintf('%.0f mnt\n(%.1f kWh)', t_reach2, E_kwh2), ...
     'fontsize', 8, 'fontweight', 'bold', 'color', [0 0.2 0.7]);
text(4, 882, 'Target Austenisasi Baja (850^\circC)', 'fontsize', 8.5, 'fontweight', 'bold');

hold off; grid on;
title('(a) Profil Pemanasan Tungku Baja 500 kg', 'fontsize', 10, 'fontweight', 'bold');
xlabel('Waktu Pemanasan, t (menit)', 'fontsize', 10);
ylabel('Temperatur Ruang Dapur, T (^\circC)', 'fontsize', 10);
legend({'Hubung Delta: P_\Delta=90 kW, \eta_{th}=82% (Cepat)', ...
        'Standar: P=60 kW, \eta_{th}=78% (Optimal)', ...
        'Isolasi Aus: P=60 kW, \eta_{th}=58% (Rugi Tinggi)', ...
        'Hubung Star: P_Y=30 kW, \eta_{th}=82% (P_Y = 1/3 P_\Delta)'}, ...
       'location', 'southeast', 'fontsize', 7.6);
xlim([0 120]); ylim([0 950]);

% Subplot 2: Efikasi Luminus vs Biaya Siklus Hidup Pencahayaan Industri
subplot(1, 2, 2);
[ax, hBar, hLine] = plotyy(tech_idx, efficacy_lmW, tech_idx, lcc_juta_rp, @bar, @plot);
set(hBar, 'facecolor', [0.15 0.45 0.78], 'barwidth', 0.46);
set(hLine, 'color', [0.82 0.12 0.12], 'linewidth', 2.4, 'marker', 's', ...
           'markersize', 8, 'markerfacecolor', [1 0.85 0.2]);
grid on;
title('(b) Efikasi (lm/W) & Biaya Siklus Hidup Lampu', 'fontsize', 10, 'fontweight', 'bold');
xlabel('Teknologi Pencahayaan Bengkel Industri', 'fontsize', 10);
ylabel(ax(1), 'Efikasi Luminus, \eta_L (Lumen / Watt)', 'fontsize', 10, 'color', [0.1 0.35 0.68]);
ylabel(ax(2), 'Total Biaya Energi 50.000 Jam (Juta Rp)', 'fontsize', 10, 'color', [0.82 0.12 0.12]);
set(ax(1), 'xlim', [0.4 4.6], 'ylim', [0 220], 'xtick', 1:4, ...
           'xticklabel', {'Halogen', 'TL-T8', 'HID/HPS', 'LED High-Bay'}, 'ycolor', [0.1 0.35 0.68]);
set(ax(2), 'xlim', [0.4 4.6], 'ylim', [0 550], 'xtick', [], 'ycolor', [0.82 0.12 0.12]);

for k = 1:4
    text(k - 0.20, efficacy_lmW(k) + 8, sprintf('%d lm/W', efficacy_lmW(k)), ...
         'fontsize', 8, 'fontweight', 'bold', 'color', [0.05 0.25 0.55]);
end
legend([hBar; hLine], {'Efikasi Luminus (lm/W) [Batang]', ...
                       'Biaya Siklus Hidup 50.000 Jam (Juta Rp) [Garis]'}, ...
       'location', 'north', 'fontsize', 7.8);

print(hFig2, 'profil_pemanas_furnace.png', '-dpng', '-r300');
fprintf('2. Grafik profil_pemanas_furnace.png berhasil diperbarui (300 DPI).\n');
close(hFig2);
fprintf('=== Simulasi Selesai ===\n');
