%File to run the simulation, open FedBatchModel_EH.m, runSimulation.m and
%TerminationFunction.m

clear all;
%Reading experimental values from an excel file
T = readtable('Excel.xlsx'); %Imports the experimental data as a table
z = T{:,1}; %Time (h) import from the first column named z
x = T.x; %Biomass (g/L) import from the second column named x
s = T.s; %Substrate (g/L) import from the third column named s
p = T.p; %Product (g/L) import from the fourth column named p
a = T.a; %Acetate (g/L) import from the fifth column named a

%Params structure for model parameters ("value" needs to be filled)
%Explanation of every model parameter is provided in Table 1
params = struct('X_0', value, 'S_0', value, 'V_0', value, 'v_0', value, 'P_0', value, 'A_0', value, ...
                'Y_p_x', value, 'Y_p_s', value, 'Y_a_s', value, 'Y_x_a', value, 'Y_x_s_true', value, 'Y_x_s_batch', Inf, ...
                'my_s_max', value, 'my_a_max', value, 'my_f', value, ...
                'm', value, 'm_a', value, ...
                'K_s', value, 'K_a', value, 'K_i', value, ...
                't_FS', Inf, 'X_FS', Inf, 'P_FS', Inf, 'S_f', value, 'F_0', Inf, 'rho_F', value, ...
                'b_max', value, 'C_s_crit_a_1', value, 'C_s_crit_a_2', value, ...
                'i', 1, 'C_s_crit', value, 'c', value, 't_lag', value);


%Run the simulation with the given model parameters
%t is the time vector of the simulation
%y is solution matrix
%updatedParams are the modified parameters during simulation
%extraOutputs are additional outputs like yields and rates
[t, y, updatedParams, extraOutputs] = runSimulation(params);

% Extract results
X=y(:,1); %Biomass (g)
S=y(:,2); %Substrate (g)
V=y(:,3); %Volume (L)
v=y(:,4); %Substrate feeding volume level (L)
P=y(:,5); %Product (g)
A=y(:,6); %Acetate (g)


%Coefficients of determination (MAE, MSE, RMSE and R^2) calculation for X, S, P and A
X_interp = interp1(t/3600, X./V, z, 'linear'); %Interpolates biomass concentration (X/V) at time points z (in hours) using linear interpolation
S_interp = interp1(t/3600, S./V, z, 'linear'); %Interpolates substrate concentration (S/V) at time points z (in hours) using linear interpolation
P_interp = interp1(t/3600, P./V, z, 'linear'); %Interpolates product concentration (P/V) at time points z (in hours) using linear interpolation
A_interp = interp1(t/3600, A./V, z, 'linear'); %%Interpolates acetate concentration (A/V) at time points z (in hours) using linear interpolation
r_x = corrcoef(x, X_interp);
R2_x = r_x(1,2)^2;
disp(['R^2 for biomass X: ', num2str(R2_x)]);
MAE_X = mean(abs(x - X_interp));
disp(['MAE for biomass X: ', num2str(MAE_X)]);
MSE_X = mean((x - X_interp).^2);
disp(['MSE for biomass X: ', num2str(MSE_X)]);
RMSE_X = sqrt(MSE_X);
disp(['RMSE for biomass X: ', num2str(RMSE_X)]);
r_s = corrcoef(s, S_interp);
R2_s = r_s(1,2)^2;
disp(['R^2 for substrate S: ', num2str(R2_s)]);
MAE_S = mean(abs(s - S_interp));
disp(['MAE for substrate S: ', num2str(MAE_S)]);
MSE_S = mean((s - S_interp).^2);
disp(['MSE for substrate S: ', num2str(MSE_S)]);
RMSE_S = sqrt(MSE_S);
disp(['RMSE for substrate S: ', num2str(RMSE_S)]);
r_p = corrcoef(p, P_interp);
R2_p = r_p(1,2)^2;
disp(['R^2 for product P: ', num2str(R2_p)]);
MAE_P = mean(abs(p - P_interp));
disp(['MAE for product P: ', num2str(MAE_P)]);
MSE_P = mean((p - P_interp).^2);
disp(['MSE for product P: ', num2str(MSE_P)]);
RMSE_P = sqrt(MSE_P);
disp(['RMSE for product P: ', num2str(RMSE_P)]);
r_a = corrcoef(a, A_interp);
R2_a = r_a(1,2)^2;
disp(['R^2 for acetate A: ', num2str(R2_a)]);
MAE_A = mean(abs(a - A_interp));
disp(['MAE for acetate A: ', num2str(MAE_A)]);
MSE_A = mean((a - A_interp).^2);
disp(['MSE for acetate a: ', num2str(MSE_A)]);
RMSE_A = sqrt(MSE_A);
disp(['RMSE for acetate A: ', num2str(RMSE_A)]);

