  The elliptic and triangular flow coefficients $v_2$ and $v_3$ of prompt
D$^{0}$, D$^{+}$, and D$^{*+}$ mesons were measured at midrapidity ($|y|<0.8$)
in Pb-Pb collisions at the centre-of-mass energy per nucleon pair of
$\sqrt{s_{NN}} = 5.02$ TeV with the ALICE detector at the LHC. The D mesons
were reconstructed via their hadronic decays in the transverse momentum
interval $1< p_{\rm T}<36$ GeV/$c$ in central (0-10%) and semi-central (30-50%)
collisions. Compared to pions, protons, and J/$\psi$ mesons, the average
D-meson $v_{n}$ harmonics are compatible within uncertainties with a mass
hierarchy for $p_{\rm T} \lesssim 3$ GeV/$c$, and are similar to those of
charged pions for higher $p_{\rm T}$. The coupling of the charm quark to the
light quarks in the underlying medium is further investigated with the
application of the event-shape engineering (ESE) technique to the D-meson $v_2$
and $p_{\rm T}$-differential yields. The D-meson $v_2$ is correlated with
average bulk elliptic flow in both central and semi-central collisions. Within
the current precision, the ratios of per-event D-meson yields in the
ESE-selected and unbiased samples are found to be compatible with unity. All
the measurements are found to be reasonably well described by theoretical
calculations including the effects of charm-quark transport and the
recombination of charm quarks with light quarks in a hydrodynamically expanding
medium.
