  The prompt D$^0$ meson azimuthal anisotropy coefficients, $v_2$ and $v_3$,
are measured at midrapidity ($|y| < 1.0$) in PbPb collisions at a
center-of-mass energy $\sqrt{s_\mathrm{NN}} = $5.02 TeV per nucleon pair with
data collected by the CMS experiment. The measurement is performed in the
transverse momentum ($p_\mathrm{T}$) range of 1 to 40 GeV/c, for central and
midcentral collisions. The $v_2$ coefficient is found to be positive throughout
the $p_\mathrm{T}$ range studied. The first measurement of the prompt D$^0$
meson $v_3$ coefficient is performed, and values up to 0.07 are observed for
$p_\mathrm{T}$ around 4 GeV/c. Compared to measurements of charged particles, a
similar $p_\mathrm{T}$ dependence, but smaller magnitude for $p_\mathrm{T} < $6
GeV/c, is found for prompt D$^0$ meson $v_2$ and $v_3$ coefficients. The
results are consistent with the presence of collective motion of charm quarks
at low $p_\mathrm{T}$ and a path length dependence of charm quark energy loss
at high $p_\mathrm{T}$, thereby providing new constraints on the theoretical
description of the interactions between charm quarks and the quark-gluon
plasma.
