% Define optimization options | for algorithm: sqp or
% interior point or active set
options = optimoptions('fmincon', 'Display', 'iter-detailed', 'Algorithm', 'interior-point','ConstraintTolerance',1e-4);
% Run the optimization
[optimal_params, neg_n_th] = fmincon(@(params) -calculate_n_th(params), initial_guess, [], [], [], [], lb, ub, @(params) nonlinear_constraints(params), options);
% Save each field of the struct as a separate variable in the workspace
fields = fieldnames(results); % Get all field names in the struct
for ii = 1:numel(fields)
varName = fields{ii}; % Current field name
assignin('base', varName, results.(varName)); % Assign to base workspace
end
% Adding labels for each point
for i = 1:length(temperature_K)
text(entropy(i), temperature_K(i), num2str(i), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right')
end
Assistant
ylabel('Temperature (K)')
xlabel('Entropy (kJ/kgK)')
title('Temperature vs. Entropy Diagram')
grid on;
hold off;
m_t = 275; %initial guess
% Struct initialization in order to save all the variables in Workspace
results = struct;
% Calculating intermediate variables
addpath("C:\Program Files (x86)\REFPROP")
Substance = 'CO2';
%n_m_T = 0.97; %mechanical efficiency of turbine
n_is_T = 0.92; %isentropic efficiency of turbine %%CUSTOM
n_is_Comp = 0.85; %isentropic efficiency of compressor
n_is_ReComp = 0.85; %isentropic efficiency of re-compressor
e_cooler = 0.95; % efficiency of cooler - see below
e_ltr = 0.93; % efficiency of LTR
e_htr = 0.95; % efficiency of HTR
T_min = 35; %Temperature of supercritical CO2 at cooler in C --------- Compressor Inlet Temperature (CIT)
T_min = T_min + 273.15; %in Kelvin (ready for refpropm)
T_seawater = 32; % Temperature of sea water in inlet
T_seawater = T_seawater + 273.15; %in Kelvin (ready for refpropm)
if T_min < T_seawater + 1
T_min = T_seawater + 1;
end
T_max = 600; %Temperature of supercritical CO2 at turbine entrance in C -------- Turbine Inlet Temperature (TIT)
T_max = T_max + 273.15; %in Kelvin (ready for refpropm)
P_min = 74; %Vaccum pressure in compressor in bar SET AS THE MINIMUM CRITICAL POINT PRESSURE (74 bar)
P_min = P_min*100; %in kPa (ready for refpropm)
P_mc = 250; %CO2 pressure at Main Compressor outlet in bar ---------- Compressor Outlet Pressure (COP)
P_mc = P_mc*100; %in kPa (ready for refpropm)
%PR = P_mc / P_min; % MUST be above 1
%PR = 1.715;
P_lowest = P_mc / PR; %-------- lowest pressure point of the cycle - after the Main Compressor
%k = 1.0229; % Helper value to calculate h_8
%lamda = 0.416; % lamda = m_b / m_t; % flow split ratio -> flow directed to re-compressor over the total flow
Q_in = 60 * 10^6; % reactor power in watts - INITIAL ESTIMATION
W_net_target = 29.13 * 10^6; % Net power in watts - INITIAL TARGET
dP_hx = 0; % Pressure difference in heat exchanger in kPa
dP_HTR_h = 100; % Pressure difference in HTR hot side in kPa
dP_HTR_c = 150; % Pressure difference in HTR cold side in kPa
dP_LTR_h = 100; % Pressure difference in LTR hot side in kPa
dP_LTR_c = 150; % Pressure difference in LTR cold side in kPa
dP_cooler = 0; % Pressure difference in cooler in kPa
Cp_g = 1050; %specific heat capacity of gases, stable and irrelevant of Temperasture in J/(kg*K)
U_htr = 2040; %HTR coeff of heat transport in W/(m^2*K)
U_ltr = 2150; %LTR coeff of heat transport in W/(m^2*K)
%%%%%%%%%%%%%%%%%%%%%%% Pressure CALCULATIONS PER POINT %%%%%%%%%%%%%%%%%%%%%%%
% Initialize variables
tolerance = 10; % Convergence threshold
max_iterations = 10000; % Maximum number of iterations
diff_Temp4 = abs(h_11 - h_4_old);
diff_h12_h11 = abs(h_12 - h_11);
% Check for convergence
if abs(diff_Temp4_old - diff_Temp4) < tolerance
flag_repeat = 1; % Stop the loop
disp('Convergence achieved.');
elseif ii >= max_iterations
warning('Maximum iterations reached without convergence.');
flag_repeat = 1; % Stop the loop
else
% Log current iteration details
fprintf('Iteration %d: Difference = %.4f\n', ii, diff_Temp4);
code
% Update variables for the next iteration
%T_4 = T_4 + diff_Temp4 * 0.000005;
T_4 = T_4 + 0.05;
diff_Temp4_old = diff_Temp4;
%h_4_old = h_4_old + 10; % Adjust this logic as needed
ii = ii + 1; % Increment iteration counter
[h_4,S_4] = refpropm('HS','T',T_4,'P',P_4,Substance); %in J/kg and J/kgK
end
h_4_old = h_4;
% Check for divergence or NaN
if isnan(diff_Temp4) || isinf(diff_Temp4)
error('Divergence detected: h_11 or h_4_old became NaN or Inf.');
end
%%%%%%%%%%%%%%%%%%%%%%% POWER CALCULATION %%%%%%%%%%%%%%%%%%%%%%%
W_t = m_t * (h_1-h_2); % mechanical power of turbine in W
W_c = m_a * (h_7 - h_6); % work of main compressor in W
W_rc = m_b * (h_12 - h_11); % work of re-compressor in W
disp("W_turbine = "+W_t/10^6+" MW" + " W_compressor = "+W_c/10^6+" MW" + " W_re-compressor = "+W_rc/10^6+" MW")
Q_c = m_a * (h_5 - h_6); % Heat deducted by cooler in W
Q_htr = m_t * (h_2 - h_3); % heat energy of High Temp Recuperator in W
Q_ltr = m_t * (h_3 - h_4); % heat energy of Low Temp Recuperator in W
disp("Q_cooler = "+Q_c/10^6+" MW" + " Q_HTR = "+Q_htr/10^6+" MW" + " Q_LTR = "+Q_ltr/10^6+" MW")
Q_in = m_t * (h_1 - h_10);
%%%%%%%%%%%%%%%%%%%%%%% OVERALL SYSTEM EFFICIENCY %%%%%%%%%%%%%%%%%%%%%%%
n_th = (W_t - W_c - W_rc) / (Q_in);
code
%%%%%%%%%%%%%%%%%%%%%%% Heat Exchange SYSTEM REQ AREA CALCULATION %%%%%%%%%%%%%%%%%%%%%%%
% function Cp_a_b = Cp_a_b(h_a,h_b,T_a,T_b)
% Cp_a_b = (h_a - h_b) / (T_a - T_b);
% end
Cp_9_10 = Cp_a_b(h_9,h_10,T_9,T_10);
Cp_2_3 = Cp_a_b(h_2,h_3,T_2,T_3);
Cp_8_7 = Cp_a_b(h_8,h_7,T_8,T_7);
Cp_3_4 = Cp_a_b(h_3,h_4,T_3,T_4);
e_htr_calc = Q_htr / (min(m_t*Cp_9_10,m_t*Cp_2_3) * (T_2 - T_9));
e_ltr_calc = Q_ltr / (min(m_a*Cp_8_7,m_t*Cp_3_4) * (T_3 - T_7));
% function Dthetam = D8m(Thot_in,Thot_out,Tcold_in,Tcold_out)
% %given that the WHR system works like enalaktis antirrois
% Dthetam = ((Thot_out-Tcold_in)-(Thot_in-Tcold_out))/(log((Thot_out-Tcold_in)/(Thot_in-Tcold_out)));
% end
Dthetam_LTR = D8m(T_3,T_4,T_7,T_8);
A_LTR = Q_ltr/(U_ltr*Dthetam_LTR); %in meters squared
Dthetam_HTR = D8m(T_2,T_3,T_9,T_10);
A_HTR = Q_htr/(U_htr*Dthetam_HTR); %in meters squared
% Q_ev = m_6 * (h_7-h_6);
% Thot_out_ev = Thot_out_sh - Q_ev/(m_g*Cp_g);
% Dthetam_ev = D8m(Thot_out_sh,Thot_out_ev,T_6,T_7);
% A_ev = Q_ev/(U_ev*Dthetam_ev); %in meters squared
%
% Q_ph = m_5 * (h_6-h_5);
% Thot_out_ph = Thot_out_ev - Q_ph/(m_g*Cp_g);
% Dthetam_ph = D8m(Thot_out_ev,Thot_out_ph,T_5,T_6);
% A_ph = Q_ph/(U_ph*Dthetam_ph); %in meters squared
A_total = A_LTR + A_HTR; %total heat exchanger area
% Add the results. prefix to every variable so that they get submitted
% to the results struct
varList = whos;
for ii = 1:length(varList)
varName = varList(ii).name;
results.(varName) = eval(varName);
end
clear tolerance system solution options x0;
% Save each field of the struct as a separate variable in the workspace
fields = fieldnames(results); % Get all field names in the struct
for ii = 1:numel(fields)
varName = fields{ii}; % Current field name
assignin('base', varName, results.(varName)); % Assign to base workspace
end
function Cp_a_b = Cp_a_b(h_a,h_b,T_a,T_b)
Cp_a_b = (h_a - h_b) / (T_a - T_b);
end
function Dthetam = D8m(Thot_in,Thot_out,Tcold_in,Tcold_out)
%given that the WHR system works like enalaktis antirrois
Dthetam = ((Thot_out-Tcold_in)-(Thot_in-Tcold_out))/(log((Thot_out-Tcold_in)/(Thot_in-Tcold_out)));
end
% Calculate n_th and intermediate variables
[n_th, results] = calculate_n_th(params);
% Inequality constraints: c <= 0
c(1) = results.e_htr_calc - 1; % e_htr_calc must be <= 1
c(2) = results.e_ltr_calc - 1; % e_ltr_calc must be <= 1
c(3) = - results.e_htr_calc; % e_htr_calc must be >= 0
c(4) = - results.e_ltr_calc; % e_ltr_calc must be >= 0
c(5) = -results.A_HTR; % A_HTR must be > 0
c(6) = -results.A_LTR; % A_LTR must be > 0
% Inequality constraints to ensure A_HTR and A_LTR are real (imaginary part is zero)
c(7) = abs(imag(results.A_HTR)); % Imaginary part of A_HTR should be zero
c(8) = abs(imag(results.A_LTR)); % Imaginary part of A_LTR should be zero
c(9) = -n_th; % n_th must be >= 0
c(10) = n_th - 1; % n_th must be <= 1
% Additional inequality constraints for check
c(11) = -lamda + 0; % lambda >= 0
c(12) = lamda - 1; % lambda <= 1
c(13) = -PR + 1.75; % PR >= 1.75
c(14) = PR - 3.3; % PR <= 3.3 = 250/74
%c(15) = -m_t + 100; % m_t >= 100
%c(16) = m_t - 10000; % m_t <= 10000
% Inequality constraints for A_total. | WARNING!!!
% : MAY NEED CHANGE IN SELECTED ALGORITHM WHEN IN FORCE
c(17) = results.A_total - 2*10^4; % A_total should be smaller than 20.000 m^2
c(18) = -results.A_total; % A_total should be greater than 0 m^2
% c(19) = -results.h_3 + 10000;
% c(20) = -results.h_8 + 10000;
% c(21) = -results.h_9 + 10000;
% c(22) = results.h_3 - results.h_2;
% c(23) = results.h_8 - results.h_3;
% c(24) = results.h_9 - results.h_3;
c(19) = -(results.h_9 - (1-lamda)*results.h_8 - lamda*results.h_12) - 10;
c(20) = (results.h_9 - (1-lamda)*results.h_8 - lamda*results.h_12) - 10;
c(21) = -(results.h_3 - (1-lamda)*(results.h_8-results.h_7) - results.h_4) - 10;
c(22) = (results.h_3 - (1-lamda)*(results.h_8-results.h_7) - results.h_4) - 10;
c(23) = -(results.h_10 + results.h_3 - results.h_9 - results.h_2) - 10;
c(24) = (results.h_10 + results.h_3 - results.h_9 - results.h_2) - 10;
% Equality constraints
ceq = [];
%ceq(1) = results.h_9 - (1-lamda)*results.h_8 - lamda*results.h_12;
%ceq(2) = results.h_3 - (1-lamda)*(results.h_8-results.h_7) - results.h_4;
%ceq(3) = results.h_10 + results.h_3 - results.h_9 - results.h_2;
end
Run this code and correct any bugs
I have reviewed and corrected the code to fix any bugs and ensure it runs smoothly. Below is the corrected code with explanations for the changes made:
Function Definitions: All function definitions (calculate_n_th, Cp_a_b, D8m, system_of_equations, nonlinear_constraints) are moved to the end of the script to follow MATLAB's scripting rules.
Removal of Extraneous Code: Lines like EQUATIONS FOR CHECK and standalone equations have been removed or integrated into functions to avoid syntax errors.
Corrections in Loops and Convergence Checks: The iterative process in calculate_n_th ensures convergence by checking the difference in h_4 values and includes a maximum iteration limit to prevent infinite loops.
Addition of Convergence Criteria: A tolerance for convergence in the loop and in the inequality constraints has been added to ensure numerical stability.
Proper Use of Variables: Variables are correctly initialized and updated within loops and functions. Mass flow rates (m_t, m_a, m_b) are recalculated with each iteration.
Constraints Handling: In nonlinear_constraints, the constraints are properly defined to guide the optimizer without causing errors.
Ensuring Real Outputs: Constraints are added to check that calculated areas (A_HTR, A_LTR) are real numbers (non-imaginary).
Please ensure that the REFPROP library is properly installed and accessible from MATLAB for the refpropm function to work correctly.
Note: This corrected code should run without syntax errors. However, due to the complexity of the thermodynamic calculations and the dependency on REFPROP, you may need to verify the numerical results and adjust parameters or models as necessary for your specific application.