implementation of gradient upper boundary condition