Water hammer refers to pressure fluctuations caused by changes in fluid speed in hydraulic systems. This research proposes a model using the MacCormack method to study and control water hammer, specifically focusing on managing valve closure to reduce shock wave pressure during transient flow. Numerical simulations compare linear and quadratic closure laws for both rapid and gradual valve closure, with the goal of identifying the optimal laws. The findings show that with faster closure times, the overpressure at the valve section decreases linearly in the second quarter of the return period (t4). The application of the quadratic law results in reduced pressures at the valve compared to the linear law, with a maximum pressure difference of 0.05 bar between the highest and lowest values. When searching for the optimal valve closure law to mitigate overpressure, it is found that the exponent ‘m’ falls within the range of 1.117–1.599. As the slow closing time increases gradually, the range of variation for ‘m’ decreases. Furthermore, in the case of underpressure prevention, the exponent ‘m’ ranges from 0.95 to 1.419, and the range of ‘m’ remains relatively constant with the increase in closing time.