function [rho_rhs,u_rhs,v_rhs,p_rhs] = ODEs_RHS_PML(rho,u,v,p,q_rho,q_u,q_v,q_p,time_RK)

Globals2D;

% local Lax-Friedrichs flux - strong form - fact that v_0 = 0 used for simplification

% computing fluxes on the boundaries within the elements
rho_flux = (u_0 * rho(vmapM) + u(vmapM)) .* n_x(:) + v(vmapM) .* n_y(:);
u_flux = (u_0 * u(vmapM) + p(vmapM)) .* n_x(:);
v_flux = (u_0 * v(vmapM)) .* n_x(:) + p(vmapM) .* n_y(:);
p_flux = (u(vmapM) + u_0 * p(vmapM)) .* n_x(:) + v(vmapM) .* n_y(:);

rho_flux(mapPML_M) = rho_flux(mapPML_M) + (u_0 * q_rho(vmapPML_M) + q_u(vmapPML_M)) .* sigma_y_PML_times_n_x(mapPML_M) + q_v(vmapPML_M) .* sigma_x_PML_times_n_y(mapPML_M);
u_flux(mapPML_M) = u_flux(mapPML_M) + (u_0 * q_u(vmapPML_M) + q_p(vmapPML_M)) .* sigma_y_PML_times_n_x(mapPML_M);
v_flux(mapPML_M) = v_flux(mapPML_M) + (u_0 * q_v(vmapPML_M)) .* sigma_y_PML_times_n_x(mapPML_M) + q_p(vmapPML_M) .* sigma_x_PML_times_n_y(mapPML_M);
p_flux(mapPML_M) = p_flux(mapPML_M) + (q_u(vmapPML_M) + u_0 * q_p(vmapPML_M)) .* sigma_y_PML_times_n_x(mapPML_M) + q_v(vmapPML_M) .* sigma_x_PML_times_n_y(mapPML_M);

% computing numerical fluxes
rho_numerical_flux = ((u_0 * (rho(vmapM) + rho(vmapP)) + (u(vmapM) + u(vmapP))) .* n_x(:) + (v(vmapM) + v(vmapP)) .* n_y(:) + (rho(vmapM) - rho(vmapP)) .* C_flux(:)) / 2;
u_numerical_flux = ((u_0 * (u(vmapM) + u(vmapP)) + (p(vmapM) + p(vmapP))) .* n_x(:) + (u(vmapM) - u(vmapP)) .* C_flux(:)) / 2;
v_numerical_flux = ((u_0 * (v(vmapM) + v(vmapP))) .* n_x(:) + ((1 / rho_0) * (p(vmapM) + p(vmapP))) .* n_y(:) + (v(vmapM) - v(vmapP)) .* C_flux(:)) / 2;
p_numerical_flux = (((u(vmapM) + u(vmapP)) + u_0 * (p(vmapM) + p(vmapP))) .* n_x(:) + (v(vmapM) + v(vmapP)) .* n_y(:) + (p(vmapM) - p(vmapP)) .* C_flux(:)) / 2;

rho_numerical_flux(mapPML_M) = rho_numerical_flux(mapPML_M) + ((u_0 * (q_rho(vmapPML_M) + q_rho(vmapPML_P)) + (q_u(vmapPML_M) + q_u(vmapPML_P))) .* sigma_y_PML_times_n_x(mapPML_M) + (q_v(vmapPML_M) + q_v(vmapPML_P)) .* sigma_x_PML_times_n_y(mapPML_M)) / 2;
u_numerical_flux(mapPML_M) = u_numerical_flux(mapPML_M) + ((u_0 * (q_u(vmapPML_M) + q_u(vmapPML_P)) + (q_p(vmapPML_M) + q_p(vmapPML_P))) .* sigma_y_PML_times_n_x(mapPML_M)) / 2;
v_numerical_flux(mapPML_M) = v_numerical_flux(mapPML_M) + ((u_0 * (q_v(vmapPML_M) + q_v(vmapPML_P))) .* sigma_y_PML_times_n_x(mapPML_M) + (q_p(vmapPML_M) + q_p(vmapPML_P)) .* sigma_x_PML_times_n_y(mapPML_M)) / 2;
p_numerical_flux(mapPML_M) = p_numerical_flux(mapPML_M) + (((q_u(vmapPML_M) + q_u(vmapPML_P)) + u_0 * (q_p(vmapPML_M) + q_p(vmapPML_P))) .* sigma_y_PML_times_n_x(mapPML_M) + (q_v(vmapPML_M) + q_v(vmapPML_P)) .* sigma_x_PML_times_n_y(mapPML_M)) / 2;

% closing the PML with characteristic-based BC - pro tuto implementaci je potreba dopocitat vlastni vektory
%for i = 1:length(mapB)
%    node_position_faces = mapB(i);
%    node_position_total = vmapB(i);
%    variables_vector = [rho(node_position_total); u(node_position_total); v(node_position_total); p(node_position_total)];
%    numerical_flux_face = TDplusTinv_flux_cell{node_position_faces} * variables_vector;
%    rho_numerical_flux(node_position_faces) = numerical_flux_face(1);
%    u_numerical_flux(node_position_faces) = numerical_flux_face(2);
%    v_numerical_flux(node_position_faces) = numerical_flux_face(3);
%    p_numerical_flux(node_position_faces) = numerical_flux_face(4);
%end

% closing the PML with Dirichlet BC, viz. Hu 2001
% inflow - rho(vmapP) = u(vmapP) = v(vmapP) = 0
nonPMLpart = u(vmapI) .* n_x(mapI);
PMLpart = (u_0 * q_rho(vmapI) + q_u(vmapI)) .* sigma_y_PML_times_n_x(mapI) + q_v(vmapI) .* sigma_x_PML_times_n_y(mapI);
rho_numerical_flux(mapI) = nonPMLpart + PMLpart;
nonPMLpart = (u_0 * u(vmapI)) .* n_x(mapI);
PMLpart = (u_0 * q_u(vmapI) + q_p(vmapI)) .* sigma_y_PML_times_n_x(mapI);
u_numerical_flux(mapI) = nonPMLpart + PMLpart;
%nonPMLpart = zeros(length(mapI),1);
PMLpart = (u_0 * q_v(vmapI)) .* sigma_y_PML_times_n_x(mapI) + q_p(vmapI) .* sigma_x_PML_times_n_y(mapI);
%v_numerical_flux(mapI) = nonPMLpart + PMLpart; % no nonPML part
v_numerical_flux(mapI) = PMLpart;
nonPMLpart = u(vmapI) .* n_x(mapI);
PMLpart = (q_u(vmapI) + u_0 * q_p(vmapI)) .* sigma_y_PML_times_n_x(mapI) + q_v(vmapI) .* sigma_x_PML_times_n_y(mapI);
p_numerical_flux(mapI) = nonPMLpart + PMLpart;
% outflow - p(vmapP) = 0
nonPMLpart = (u_0 * rho(vmapO) + u(vmapO)) .* n_x(mapO) + v(vmapO) .* n_y(mapO);
PMLpart = (u_0 * q_rho(vmapO) + q_u(vmapO)) .* sigma_y_PML_times_n_x(mapO) + q_v(vmapO) .* sigma_x_PML_times_n_y(mapO);
rho_numerical_flux(mapO) = nonPMLpart + PMLpart;
nonPMLpart = (u_0 * u(vmapO)) .* n_x(mapO);
PMLpart = (u_0 * q_u(vmapO) + q_p(vmapO)) .* sigma_y_PML_times_n_x(mapO);
u_numerical_flux(mapO) = nonPMLpart + PMLpart;
nonPMLpart = (u_0 * v(vmapO)) .* n_x(mapO);
PMLpart = (u_0 * q_v(vmapO)) .* sigma_y_PML_times_n_x(mapO) + q_p(vmapO) .* sigma_x_PML_times_n_y(mapO);
v_numerical_flux(mapO) = nonPMLpart + PMLpart;
nonPMLpart = u(vmapO) .* n_x(mapO) + v(vmapO) .* n_y(mapO);
PMLpart = (q_u(vmapO) + u_0 * q_p(vmapO)) .* sigma_y_PML_times_n_x(mapO) + q_v(vmapO) .* sigma_x_PML_times_n_y(mapO);
p_numerical_flux(mapO) = nonPMLpart + PMLpart;

% resizing both normal and numerical fluxes to appropiate arrays
rho_flux = reshape(rho_flux,N_faces * N_fp,K);
u_flux = reshape(u_flux,N_faces * N_fp,K);
v_flux = reshape(v_flux,N_faces * N_fp,K);
p_flux = reshape(p_flux,N_faces * N_fp,K);

rho_numerical_flux = reshape(rho_numerical_flux,N_faces * N_fp,K);
u_numerical_flux = reshape(u_numerical_flux,N_faces * N_fp,K);
v_numerical_flux = reshape(v_numerical_flux,N_faces * N_fp,K);
p_numerical_flux = reshape(p_numerical_flux,N_faces * N_fp,K);

% building the right-hand side of the ODE system
[rho_x,rho_y] = Grad2D(rho); [u_x,u_y] = Grad2D(u); [v_x,v_y] = Grad2D(v); [p_x,p_y] = Grad2D(p);
rho_rhs = -u_0 * rho_x - rho_0 * (u_x + v_y) + LIFT * (Fscale .* (rho_flux - rho_numerical_flux)) + rho_RHS(x,y,time_RK);
u_rhs = -u_0 * u_x - p_x + LIFT * (Fscale .* (u_flux - u_numerical_flux));
v_rhs = -u_0 * v_x - p_y + LIFT * (Fscale .* (v_flux - v_numerical_flux));
p_rhs = -u_x - u_0 * p_x - v_y + LIFT * (Fscale .* (p_flux - p_numerical_flux)) + p_RHS(x,y,time_RK);

[q_rho_x,q_rho_y] = Grad2D(q_rho); [q_u_x,q_u_y] = Grad2D(q_u); [q_v_x,q_v_y] = Grad2D(q_v); [q_p_x,q_p_y] = Grad2D(q_p);
derivatives = sigma_y_PML(:,elements_PML) .* (u_0 * q_rho_x(:,elements_PML) + q_u_x(:,elements_PML)) + sigma_x_PML(:,elements_PML) .* q_v_y(:,elements_PML);
non_derivatives = sigma_x_PML_plus_sigma_y_PML(:,elements_PML) .* rho(:,elements_PML) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* q_rho(:,elements_PML);
stabilization = coef_PML * (sigma_x_PML(:,elements_PML) .* (u_0 * rho(:,elements_PML) + u(:,elements_PML)) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* (u_0 * q_rho(:,elements_PML) + q_u(:,elements_PML)));;
rho_rhs(:,elements_PML) = rho_rhs(:,elements_PML) - derivatives - non_derivatives - stabilization; 
derivatives = sigma_y_PML(:,elements_PML) .* (u_0 * q_u_x(:,elements_PML) + q_p_x(:,elements_PML));
non_derivatives = sigma_x_PML_plus_sigma_y_PML(:,elements_PML) .* u(:,elements_PML) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* q_u(:,elements_PML);
stabilization = coef_PML * (sigma_x_PML(:,elements_PML) .* (u_0 * u(:,elements_PML) + p(:,elements_PML)) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* (u_0 * q_u(:,elements_PML) + q_p(:,elements_PML)));
u_rhs(:,elements_PML) = u_rhs(:,elements_PML) - derivatives - non_derivatives - stabilization;
derivatives = sigma_y_PML(:,elements_PML) .* (u_0 * q_v_x(:,elements_PML)) + sigma_x_PML(:,elements_PML) .* q_p_y(:,elements_PML);
non_derivatives = sigma_x_PML_plus_sigma_y_PML(:,elements_PML) .* v(:,elements_PML) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* q_v(:,elements_PML);
stabilization = coef_PML * (sigma_x_PML(:,elements_PML) .* (u_0 * v(:,elements_PML)) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* (u_0 * q_v(:,elements_PML)));
v_rhs(:,elements_PML) = v_rhs(:,elements_PML) - derivatives - non_derivatives - stabilization;
derivatives = sigma_y_PML(:,elements_PML) .* (q_u_x(:,elements_PML) + u_0 * q_p_x(:,elements_PML)) + sigma_x_PML(:,elements_PML) .* q_v_y(:,elements_PML);
non_derivatives = sigma_x_PML_plus_sigma_y_PML(:,elements_PML) .* p(:,elements_PML) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* q_p(:,elements_PML);
stabilization = coef_PML * (sigma_x_PML(:,elements_PML) .* (u(:,elements_PML) + u_0 * p(:,elements_PML)) + sigma_x_PML_times_sigma_y_PML(:,elements_PML) .* (q_u(:,elements_PML) + u_0 * q_p(:,elements_PML)));
p_rhs(:,elements_PML) = p_rhs(:,elements_PML) - derivatives - non_derivatives - stabilization;

end