Solution (source code)

= Solution

The temporal <integration by parts> terms are
$$
-[\,(\mathbf u^\dagger,\delta\mathbf u)+(\theta^\dagger,\delta\theta)\,]_0^T
-(\mathbf u_0^\dagger,\delta\mathbf u(0)-\delta\mathbf u_0)
+2(\Theta(T),\delta\theta(T)).
$$
The terminal states are free and the objective has no explicit terminal-<velocity> dependence. Therefore
$$
\boxed{\mathbf u^\dagger(T)=\mathbf0,\qquad
\theta^\dagger(T)=2\Theta(T).}
$$
The factor two follows from the displayed objective without a $1/2$ prefactor. The initial <scalar field> perturbation is fixed, so $\delta\theta(0)=0$ and there is no independently prescribed adjoint-scalar condition at $t=0$. Its value there is obtained by backward integration. Independent variation of the initial <velocity> state gives $\mathbf u^\dagger(0)=\mathbf u_0^\dagger$.

Variation of the candidate $\mathbf u_0$ in the energy-augmented functional gives the <fixed-energy initial-condition optimality> condition. For the usual divergence-free no-slip control space $H$, write $P_H$ for its <orthogonal projection>, the <Leray-Helmholtz projection> in the usual incompressible <velocity> space. This is the standard formal control condition when <pressure> is determined by <incompressible flow> and normal momentum. If the independently imposed direct <pressure> flux is retained as an additional constraint, the admissible control variations must also satisfy its compatibility conditions along the trajectory; they need not fill the usual space $H$. The standard projection formula alone does not prove stationarity for that more restricted problem. Then
$$
\boxed{P_H\mathbf u^\dagger(0)=\lambda\mathbf u_0,\qquad
\frac12\|\mathbf u_0\|^2=E_0.}
$$
For the natural solenoidal adjoint space, $P_H\mathbf u^\dagger(0)=\mathbf u^\dagger(0)$. Equivalently, its component tangent to the <sphere in a normed vector space> of fixed <kinetic energy> vanishes:
$$
g_{\mathrm{tan}}=P_H\mathbf u^\dagger(0)
-\frac{(P_H\mathbf u^\dagger(0),\mathbf u_0)}{2E_0}\mathbf u_0=0.
$$
Here $E_0>0$ is needed for the <sphere in a normed vector space> to be regular and for division by $2E_0$. The multiplier is real and can have either sign; normalizing the initial adjoint with a prescribed positive sign is not a general necessary condition for minimization. If $E_0=0$, the only feasible initial <velocity> is zero and this tangent formula for the <sphere in a normed vector space> is inapplicable. First-order stationarity alone also allows maxima or saddles; a local minimum requires the appropriate nonnegative constrained second variation.

The spatial boundary terms vanish with periodic adjoints in $x,z$, homogeneous <no-slip boundary conditions> $\mathbf u^\dagger=0$ at $y=\pm1$, and $\partial_y\theta^\dagger=0$ there. To see this, <velocity> variations vanish at the wall while their normal <derivatives> need not, so the viscous boundary term forces the adjoint <velocity> to vanish. Scalar variations have zero normal <derivative> but free values, forcing the adjoint scalar's normal <derivative> to vanish. Pressure variation gives adjoint <incompressible flow>; no independent terminal or initial datum is assigned to $p^\dagger$. Its additive time-dependent constant may be fixed by a zero spatial mean.

In particular, a homogeneous Neumann condition for the direct <pressure> does not by variational transposition require $\partial_yp^\dagger=0$. For a smooth adjoint solution, the wall-normal adjoint momentum equation instead supplies
$$
\partial_yp^\dagger=-\nu\Delta u_y^\dagger
$$
at the flat walls, because the direct scalar normal <derivative> is zero there. A zero adjoint-<pressure> <derivative> would be an additional compatibility choice, valid only when this right-hand side vanishes.

There are also literal direct-data compatibility qualifications in the printed setup. The initial total <scalar field> is $\overline\theta=-\operatorname{erf}(30y)$, whose wall <derivative> is $-60e^{-900}/\sqrt\pi\ne0$. It is extraordinarily small but mathematically not zero. Thus exact initial scalar data and exact zero wall flux are incompatible for a classical solution smooth at $t=0$. One may use a parabolic <mild solution of an abstract Cauchy problem> with the boundary condition enforced for $t>0$, or replace the initial profile by an exactly compatible smooth one; these are conventions, not an equality $e^{-900}=0$. The reference <scalar field> is also not a stationary profile of the <diffusion equation> at finite $Pe$: the full $\Delta\Theta$ term must be retained.

The <pressure compatibility at a no-slip wall> is similarly important. Direct normal momentum gives $\partial_yp=\nu\Delta u_y-Ri_B\theta$. The separately imposed zero <pressure> <derivative> requires this right side to vanish. It is not automatic for arbitrary fixed-energy initial <velocities>. For example, the divergence-free no-slip field generated by the periodic <stream function> $S=A(1-y^2)^2\sin(\alpha x)$ with $\alpha=\pi/L_x>0$ has $u_x=\partial_yS$, $u_y=-\partial_xS$ and $\Delta u_y|_{y=\pm1}=-8A\alpha\cos(\alpha x)\ne0$, while $\theta(0)=0$. With $A\ne0$ adjusted to any positive energy, no classical solution smooth up to the initial wall can satisfy that extra <pressure> condition. The displayed adjoints are the formal necessary equations along admissible smooth trajectories; a well-posed physical formulation normally determines <pressure> from <incompressible flow> and normal momentum, rather than imposing independent homogeneous <pressure> flux for every control.