# weak variational formulation of Poisson equation with some restrictions.

The variational formulation of Poisson equation $$\begin{cases} -\Delta u = f & \text{in } \Omega \\ u = 0 & \text{on } \partial \Omega \end{cases}$$ is that: Find $$u\in H_0^1(\Omega)$$, such that $$a(u,v) = (f,v),\ \forall v\in H_0^1(\Omega)$$. If I add a restriction $$u\geq 0$$, it seems that the weak formulation is that: Find $$u\in K:=\{v\in H_0^1(\Omega), v\geq 0\}$$, such that $$a(u,v-u) \geq(f,v-u), \ \forall v\in K$$ I don't know how to derive this variational inequality in a strict way, and especially why the test function space alse has to satisfy $$v\geq 0.$$

It's essential to observe here that "add the restriction $$u\geq 0$$" is a statement which is ambiguous, or lacking in meaning. Solutions to this Poisson equation are unique; they might or might not satisfy $$u \geq 0$$ (depending on $$f$$), but one cannot simply produce solutions which do satisfy $$u \geq 0$$ for a given $$f$$ at will.
What you are doing here is actually the following: note that the Poisson equation has a Dirichlet principle; the solution $$u$$ is characterized as the unique minimizer of $$\{\int \frac{|\nabla u|^2}{2} - f u : u \in H^1_0(\Omega)\}.$$ It is certainly possible to restrict the class of $$u$$ we are minimizing over here. I mean, you can minimize this over whatever set you like, if you can show the minimum is attained. One choice is to minimize over all $$u \in H^1_0(\Omega)$$ with the pointwise constraint $$u \geq 0$$. It is elementary to show that the minimum is attained over this set.
When you do this, you change the PDE which minimizers satisfy. You can attempt to compute the PDE in the same way you do for Laplace equation, by considering competitors $$v$$ with $$v \in H^1_0(\Omega)$$ and $$v\geq 0$$. Using $$u + t(v - u)$$ as a competitor for $$t \in (0, 1)$$ (this is a convex combination of $$u$$ and $$v$$), in particular, leads to $$t\int \nabla (v - u) \cdot \nabla u - f(v - u) \geq - \frac{t^2}{2}\int |\nabla v|^2,$$ so dividing by $$t$$ and sending it to zero leads to the variational inequality you stated. In fact the converse is also true: any function satisfying the variational inequality is a minimizer to this functional over all $$u\in H^1_0(\Omega)$$ with $$u \geq 0$$. Actually so far this has very little to do with PDE or the function spaces; these kinds of variational inequalities are what you get as optimality conditions for minimizing convex functions over convex sets, in any context that these concepts make sense.
So this gives a kind of Euler-Lagrange equation, but is perhaps unsatisfying (it's not a pointwise property and only an inequality). One thing you can note is that if $$h \geq 0$$ and say $$h\in H^1_0(\Omega$$, then $$v = u + h$$ is a valid competitor, so plugging it into the variational inequality gives $$\int \nabla h \cdot \nabla u - h f \geq 0,$$ i.e. $$u$$ is a (weak) supersolution, $$- \Delta u \geq f$$. If we assume that $$u$$ is continuous, then on the interior of $$\{u > 0\}$$ we will have that $$u$$ actually solves this PDE, because for an $$h$$ compactly supported on $$\{u > 0\}$$ we will have, for very small $$t$$, that $$u - t h \geq 0$$, which then gives the subsolution inquality when used as a test function. On the interior of $$\{ u = 0\}$$, well, $$u = 0$$, it's not solving $$-\Delta u = f$$ except by accident.
The behaviors of $$u$$ on the interiors of $$\{ u > 0\}$$ and $$\{ u = 0\}$$ do not fully specify a PDE for $$u$$, because $$u$$ could have a variety of behaviors along $$\partial \{ u > 0\}$$ in principle (try it in 1D). However, the conditions $$-\Delta u \geq f$$ weakly (notice how this holds "along" the boundary just fine) and $$u \geq 0$$ turn out to be enough to characterize $$u$$ completely: they amount to an extra condition that "the normal derivative of $$u$$ along $$\partial \{ u > 0\}$$ vanishes," roughly speaking, so $$-\Delta u = f 1_{\{u > 0\}}$$ weakly. Notice how this is a nonlinear PDE: the dependence on $$u$$ on the right-hand side is not linear in $$u$$.
To get back to the question asked: the test function space is what it is because you chose it to be this; this was the starting choice. You could have started with the PDE, but not with your PDE: with my PDE, $$-\Delta u = f 1_{\{u > 0\}}$$, if you knew that this was the right PDE to begin with. The variational inequality is the Euler-Lagrange equation to a variational problem, and because everything is convex here it is equivalent to that variational problem. Since the variational problem is a fairly natural constrained minimization, it is a good place to begin.