Let $u(x,t)=\sum\limits_{n=1}^\infty C(n,t)\sin n\pi x$ so that it automatically satisfies $u(0,t)=u(1,t)=0$ ,
Then $\sum\limits_{n=1}^\infty C_{tt}(n,t)\sin n\pi x+\sum\limits_{n=1}^\infty n^2\pi^2C(n,t)\sin n\pi x=\cos2t$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty2\cos2t\int_k^{k+1}\sin n\pi x~dx~\sin n\pi x$ , $\forall k\in\mathbb{Z}$ , $x\in(k,k+1)$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty2\cos2t\left[-\dfrac{\cos n\pi x}{n\pi}\right]_k^{k+1}\sin n\pi x$ , $\forall k\in\mathbb{Z}$ , $x\in(k,k+1)$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty\dfrac{2((-1)^{nk}-(-1)^{n(k+1)})\cos2t\sin n\pi x}{n\pi}$ , $\forall k\in\mathbb{Z}$ , $x\in(k,k+1)$
$\sum\limits_{n=1}^\infty(C_{tt}(n,t)+n^2\pi^2C(n,t))\sin n\pi x=\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\cos2t\sin n\pi x}{n\pi}$
$\therefore C_{tt}(n,t)+n^2\pi^2C(n,t)=\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\cos2t}{n\pi}$
$C(n,t)=C_1(n)\sin n\pi t+C_2(n)\cos n\pi t+\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\cos2t}{n\pi(n^2\pi^2-4)}$
$\therefore u(x,t)=\sum\limits_{n=1}^\infty C_1(n)\sin n\pi x\sin n\pi t+\sum\limits_{n=1}^\infty C_2(n)\sin n\pi x\cos n\pi t+\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\sin n\pi x\cos2t}{n\pi(n^2\pi^2-4)}$
$u(x,0)=0$ :
$\sum\limits_{n=1}^\infty C_2(n)\sin n\pi x+\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\sin n\pi x}{n\pi(n^2\pi^2-4)}=0$
$\sum\limits_{n=1}^\infty C_2(n)\sin n\pi x=\sum\limits_{n=1}^\infty\dfrac{2((-1)^n-1)\text{sgn}(\sin\pi x)\sin n\pi x}{n\pi(n^2\pi^2-4)}$
$C_2(n)=\dfrac{2((-1)^n-1)\text{sgn}(\sin\pi x)}{n\pi(n^2\pi^2-4)}$
$\therefore u(x,t)=\sum\limits_{n=1}^\infty C_1(n)\sin n\pi x\sin n\pi t+\sum\limits_{n=1}^\infty\dfrac{2((-1)^n-1)\text{sgn}(\sin\pi x)\sin n\pi x\cos n\pi t}{n\pi(n^2\pi^2-4)}+\sum\limits_{n=1}^\infty\dfrac{2(1-(-1)^n)\text{sgn}(\sin\pi x)\sin n\pi x\cos2t}{n\pi(n^2\pi^2-4)}=\sum\limits_{n=1}^\infty C_1(n)\sin n\pi x\sin n\pi t-\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos((2n-1)\pi t)}{(2n-1)\pi((2n-1)^2\pi^2-4)}+\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos2t}{(2n-1)\pi((2n-1)^2\pi^2-4)}$
$u_t(x,t)=\sum\limits_{n=1}^\infty n\pi C_1(n)\sin n\pi x\cos n\pi t+\sum\limits_{n=1}^\infty\dfrac{2(2n-1)\pi~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\sin((2n-1)\pi t)}{(2n-1)\pi((2n-1)^2\pi^2-4)}-\sum\limits_{n=1}^\infty\dfrac{4~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\sin2t}{(2n-1)\pi((2n-1)^2\pi^2-4)}$
$u_t(x,0)=\sum\limits_{n=1}^\infty\sin2n\pi x$ :
$\sum\limits_{n=1}^\infty n\pi C_1(n)\sin n\pi x=\sum\limits_{n=1}^\infty\sin2n\pi x$
$n\pi C_1(n)=\begin{cases}0&\text{when}~n~\text{is an odd integer}\\1&\text{when}~n~\text{is an even integer}\end{cases}$
$C_1(n)=\begin{cases}0&\text{when}~n~\text{is an odd integer}\\\dfrac{1}{n\pi}&\text{when}~n~\text{is an even integer}\end{cases}$
$\therefore u(x,t)=\sum\limits_{n=1}^\infty\dfrac{\sin2n\pi x\sin2n\pi t}{2n\pi}-\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos((2n-1)\pi t)}{(2n-1)\pi((2n-1)^2\pi^2-4)}+\sum\limits_{n=1}^\infty\dfrac{2~\text{sgn}(\sin\pi x)\sin((2n-1)\pi x)\cos2t}{(2n-1)\pi((2n-1)^2\pi^2-4)}$
Best Answer
$f$ is just a source term. It can be modeled with a term of the form $zf$ in the Lagrangian $L$. See also e.g. this related Phys.SE post.