How to include "hitting the ground" in a simple pendulum Lagrangian? Consider the following system:

I derived the equations of motion for a basic pendulum using the following Lagrangian:
$$\begin{align}
L & = T - V\\
&= \frac{1}{2} m \ell^2 \dot{\theta}^2 + mg\ell\cos(\theta) \\
\end{align}$$
So the Euler Lagrange equations are
$$\begin{align} 
\frac{d}{dt}\left(\frac{\partial L}{\partial \dot{\theta}}\right)-\frac{\partial L}{\partial \theta} &= 0\\
m\ell^2\ddot{\theta}-(-mg\ell \sin(\theta))&=0\\
\ell\ddot{\theta}+g\sin(\theta)&=0
\end{align}
$$
However, I want to consider a system where the pendulum could hit the ground and the ground would impede any further motion. How would  I go about incorporating that into the Lagrangian?

Obviously, once it hits the ground, it stops moving anywhere. Could I add some kind of constraint angle on $\theta$? But how would I include that in my Lagrangian?
 A: This is an example of a non "holonomic" constraint. As far as I know there is no nice way to do this. The only way I can think of is to make some external potential
$$
V(y) = V_0 f(y)
$$
where $f(y)$ is a sigmoid
$$
f(y) = \frac{1}{2} + \frac{1}{2} \tanh(-a y)
$$
and then take $V_0 \to \infty$, $a \mapsto \infty$. This will make an infinitely large and steep step function for you pendulum to bounce off of. It'll be of no use towards actually solving the system of equations, however. To do that you'll have to implement your bouncing condition in by hand.
I'm interested if anyone else knows of anything better.
A: You would want to model the ground with springs. Theoretically then I guess you could incorporate that into the Lagrangian with a potential energy that has a step function (an on/off switch).
Practically, you need to integrate these equations using a numerical method (unless you want to deal with elliptic integrals). If I were you, I would probe each time value to determine when the pendulum first penetrates the ground, exit the previous physics (equations of motion), store the final conditions from the pendulum at the last time step, and use those as initial conditions for the new physics of the thing bouncing on the ground.
Then it is a matter of prescribing an appropriate spring loading and unloading curve.
If you simply want the pendulum to stick, then just stop your integration once the pendulum passes through the ground.
