If, during any single phase other than the Combat phase, a unit loses more than a quarter (25%) of the models it contained at the start of that phase, it must make a Panic test.
A unit that fails a Panic test caused by heavy casualties will flee directly away from the enemy unit that caused those casualties. If the casualties were not caused by an enemy unit, the unit will instead flee from the nearest enemy unit which is not itself fleeing.
Note that heavy casualties caused during the Combat phase do not cause Panic tests. During combat, the urgency of defeating the enemy and driving them back (represented by calculating combat results and making Break tests) takes precedence.