I'm wondering to what extent Lagrangian/Hamiltonian formulations are used outside of mechanics. So far I've found that geometrical optics has such formulations and I can imagine thermodynamics has something like this as well because I have seen the concepts phase space and conjugate variables being used there. I have not, however, been able to find these kind of descriptions for electromagnetism. Is there such a thing as a "Lagrangian/Hamiltonian formulation of electromagnetism"? And if so, could you say that these formulations exist for any subfield of physics?