---
title: "Algorithm for water hammer using MOC"
subject: "Computational Techniques in Civil Engineering"
chapter: 6
key: "semester_8/computational_techniques_in_civil_engineering:water-hammer-algorithm-moc"
source: https://www.bctengineer.com/q/semester_8/computational_techniques_in_civil_engineering/water-hammer-algorithm-moc
site: BCT Engineering
---

# Algorithm for water hammer using MOC

Computational Techniques in Civil Engineering · Chapter 6: Method of Characteristics

- Asked in: 2072 Asoj, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_8/computational_techniques_in_civil_engineering/water-hammer-algorithm-moc
- Whole chapter: https://www.bctengineer.com/md/semester_8/computational_techniques_in_civil_engineering/chapter-6

## Question

Write an algorithm for simulation of the water hammer process using the method of characteristics.

## Short answer

Read data, set grid with dx=L/N and dt=dx/a, and compute constants Bp and Rp. Initialize steady Q and H including friction. At each time step compute interior K1, K2, Q_P, H_P; apply reservoir C- and valve C+ with valve law; update and store until t<T.

## Answer

**Problem:** a reservoir at the upstream end, a pipe of length $L$ with a valve at the downstream end that closes in a given time.

#### Algorithm
1. **Input data:** $L$, $D$, wave speed $a$, friction factor $f$, reservoir head $H_{res}$, initial steady discharge $Q_0$, valve closure law $\tau(t)$ (relative opening), total simulation time $T$.
2. **Grid:** choose the number of reaches $N$; set $\Delta x=L/N$ and $\Delta t=\Delta x/a$ (characteristics along the diagonal).
3. **Constants:** $A_p=\pi D^2/4$, $B_p=a/(gA_p)$, $R_p=f\Delta x/(2gDA_p^2)$.
4. **Initial conditions ($t=0$):** $Q_i=Q_0$ at all nodes $i=1,\dots,N+1$; $H_i=H_{res}-R_pQ_0^2(i-1)$ (steady friction loss along the pipe).
5. **Time loop:** set $t=t+\Delta t$.
6. **Interior nodes $i=2,\dots,N$:**
   - $K_1=H_{i-1}+B_pQ_{i-1}-R_pQ_{i-1}|Q_{i-1}|$
   - $K_2=H_{i+1}-B_pQ_{i+1}+R_pQ_{i+1}|Q_{i+1}|$
   - $Q_{P_i}=(K_1-K_2)/(2B_p)$, $H_{P_i}=(K_1+K_2)/2$
7. **Upstream node ($i=1$, reservoir):** $H_{P_1}=H_{res}$; $K_2=H_2-B_pQ_2+R_pQ_2|Q_2|$; $Q_{P_1}=(H_{P_1}-K_2)/B_p$.
8. **Downstream node ($i=N+1$, valve):** $K_1=H_N+B_pQ_N-R_pQ_N|Q_N|$. With the valve law $Q_P=Q_0\tau\sqrt{H_P/H_0}$ the solution is
   $$
   Q_P=-B_pC_v+\sqrt{(B_pC_v)^2+2C_vK_1},\qquad C_v=\frac{(Q_0\tau)^2}{2H_0},\qquad H_P=K_1-B_pQ_P
   $$
   For complete closure ($\tau=0$): $Q_P=0$, $H_P=K_1$.
9. **Update:** replace old values by the new ones, $Q_i\leftarrow Q_{P_i}$, $H_i\leftarrow H_{P_i}$.
10. **Store/print** $H_i$ and $Q_i$ (especially the maximum $H$ at the valve and mid-pipe).
11. If $t<T$ go to step 5; otherwise stop. Plot $H$ against $t$ to read the maximum pressure rise.

```text
 Start -> read data -> grid, B, R -> initial Q, H
   -> t = t + dt
   -> interior nodes (C+ and C-)
   -> reservoir node (C-) , valve node (C+ and valve law)
   -> update Q, H -> output
   -> t < T ?  yes: back to "t = t + dt"   no: stop
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. In the water hammer algorithm, how are dx and dt chosen?
   A. dx = L/N and dt = dx/a, so characteristics run along the grid diagonal
   B. dt is arbitrary and dx = a dt
   C. dt = L/a for any N
   D. dx = a/L
   Answer: A. Dividing the pipe into N reaches and using dt = dx/a keeps C = 1 so no interpolation is needed. (Key point: Grid and time step)

2. How is the reservoir node (i = 1) treated in each time step?
   A. H_P1 = H_res is fixed and Q_P1 = (H_P1 - K2)/B_p using the C- equation
   B. Q_P1 = 0 and H from C+
   C. Both Q and H are found by the average formulas (K1 +/- K2)
   D. It is skipped and kept at initial values
   Answer: A. The head is fixed by the reservoir, and the C- line from node 2 supplies Q_P1. (Key point: Upstream reservoir node)

3. For complete closure of the valve (tau = 0), what is applied at the downstream node?
   A. Q_P = Q_0 and H_P = H_res
   B. Q_P = 0 and H_P = K1 from the C+ equation
   C. Q_P = K1/B_p and H_P = 0
   D. H_P = 0 and Q_P from C-
   Answer: B. With the valve shut Q_P = 0 so H_P = K1, which gives the pressure rise. (Key point: Complete valve closure)

4. What is done at the end of each time step before advancing?
   A. The old values are kept and new ones discarded
   B. Only the maximum head is stored
   C. dt is halved
   D. Old Q and H are replaced by the new Q_P and H_P, then t is compared with T
   Answer: D. New values become the old values for the next step, and the loop repeats until t reaches T. (Key point: Loop and update)

## Related in Computational Techniques in Civil Engineering

- [MOC numerical, characteristics not on diagonal (25 cm)](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Amoc-numerical-off-diagonal-j) — Computational Techniques in Civil Engineering, chapter 6, asked 2×
- [MOC numerical, characteristics not on diagonal (30 cm)](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Amoc-numerical-off-diagonal-a2) — Computational Techniques in Civil Engineering, chapter 6, asked 2×
- [Water hammer: rectangular grid and specified intervals](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Awater-hammer-numerical-solution) — Computational Techniques in Civil Engineering, chapter 6
- [MOC single reach, reservoir pipe (2 s)](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Amoc-reservoir-pipe-sin-2) — Computational Techniques in Civil Engineering, chapter 6
- [MOC numerical: discharge and head at P (30 cm pipe)](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Amoc-pipe-discharge-head-pc-30cm) — Computational Techniques in Civil Engineering, chapter 6
- [MOC single reach, reservoir pipe (3 s)](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Amoc-reservoir-pipe-sin-3) — Computational Techniques in Civil Engineering, chapter 6

## Related in other subjects

- [Water hammer and how to minimize it](https://www.bctengineer.com/question.md?key=semester_7%2Fpower_plant_equipment%3Awater-hammer-minimize) — Power Plant Equipment, chapter 1, asked 4×
- [Surge and water hammer: 25 m³/s, 15 m surge tank](https://www.bctengineer.com/question.md?key=semester_7%2Fhydropower_engineering%3Asurge-numerical-25cumec-2-penstocks-15m-tank) — Hydropower Engineering, chapter 6, asked 3×
- [Water hammer in penstock and surge tank](https://www.bctengineer.com/question.md?key=semester_7%2Fpower_plant_equipment%3Awater-hammer-surge-tank) — Power Plant Equipment, chapter 1, asked 2×
- [One cycle of water hammer pressure wave](https://www.bctengineer.com/question.md?key=semester_4%2Fhydraulics%3Awater-hammer-one-cycle-wave-sketch) — Hydraulics, chapter 4, asked 2×
