Solution (source code)

= Solution

The terms independent of $v$ in the substituted PDE satisfy
$$
B'(t)+\frac r2+aA(t)=r,
$$
so
$$
B'(t)=\frac r2-aA(t),
\qquad B(T)=0.
$$
Integrating backward from $T$ yields
$$
\boxed{
B(t)=-\frac12(T-t)r
+a\int_t^TA(s)ds.}
$$
Thus the requested constant is
$$
\boxed{k=a.}
$$