pure subroutine update_reservoir_zonal_west(hTr, h_layer, mass_flux_x, &
tres_w, T_data, &
nx_total, ny_total, nz, &
i_w, j0, j1, &
L_out, L_in, dt, it, n_tr)
!! Update reservoir for the west open edge. Wall-face/interior cell = i_w.
!! Outward normal = -x: u_n = -mass_flux_x(i_w,j,k)/max(h,hmin).
integer, intent(in) :: nx_total, ny_total, nz, n_tr, it
integer, intent(in) :: i_w, j0, j1
real(wp), intent(in) :: hTr(nx_total, ny_total, nz)
real(wp), intent(in) :: h_layer(nx_total, ny_total, nz)
real(wp), intent(in) :: mass_flux_x(nx_total + 1, ny_total, nz)
real(wp), intent(inout) :: tres_w(ny_total, nz, n_tr)
real(wp), intent(in) :: T_data
real(wp), intent(in) :: L_out, L_in, dt
integer :: j, k
real(wp) :: h_int, T_int, u_n, c_out, c_in, tres_old, denom
do concurrent(j=j0:j1, k=1:nz) local(h_int, T_int, u_n, c_out, c_in, tres_old, denom)
h_int = max(h_layer(i_w, j, k), RES_H_MIN)
T_int = hTr(i_w, j, k)/h_int
! mass_flux_x(i_w,...) > 0 = eastward into domain (inflow at west).
u_n = -mass_flux_x(i_w, j, k)/h_int
tres_old = tres_w(j, k, it)
! Degenerate limits: L_out==0 & u_n>0 ⇒ T_int; L_in==0 & u_n<0 ⇒ T_data
! instantly. Both L zero ⇒ implicit form is a no-op (tres unchanged).
if (L_out == 0.0_wp .and. u_n > 0.0_wp) then
tres_w(j, k, it) = T_int
else if (L_in == 0.0_wp .and. u_n < 0.0_wp) then
tres_w(j, k, it) = T_data
else
c_out = merge(max(0.0_wp, u_n)*dt/L_out, 0.0_wp, L_out > 0.0_wp)
c_in = merge(max(0.0_wp, -u_n)*dt/L_in, 0.0_wp, L_in > 0.0_wp)
denom = 1.0_wp + c_out + c_in
tres_w(j, k, it) = (tres_old + c_out*T_int + c_in*T_data)/denom
end if
end do
end subroutine update_reservoir_zonal_west