# QG Regularization (Math)
###### tags: `interpolation`
> In this note, we will construct a *QG regularization* on sea surface height by using the QG PDE which relates SSH to potential vortity and the stream function. We will convert all variables within the QG PDE to SSH.
---
## Edits
> I document some mistakes I made from the derivation as pointed out by me or others
**4-09-2022**
* **FIXED**: *Redouane*: I made a sign error for the PV in terms of the stream function
* Wrong: $q = \nabla^2\psi + c_2\psi$ $\rightarrow$ Correction: $q = \nabla^2\psi - c_2\psi$
* **FIXED**: *Redouane*: I made an error with one of the constants
* Wrong: $c_1 = f/g$ $\rightarrow$ Correction: $c_1 = g/f$
* Wrong: $c_3 = f/gL_r^2$ $\rightarrow$ Correction: $c_3 = g/fL_r^2$
* **FIXED**: *Redouane*: The the $\det\boldsymbol{J}(u,u)=0$, duh...
* $\det\boldsymbol{J}(u,u) = \partial_x u \partial_y u - \partial_y u \partial_x u = 0$
**22-09-2022**
* **FIXED**: *Emmanuel C.*: I made another sign error when factoring out the constant, $c_1$, which resulted in a wrong final term:
* Wrong: $\partial_t \nabla^2 u {\color{red}{+ c_2}} \partial_t u + c_1\det \boldsymbol{J}(u, \nabla^2 u) = 0$
* Corrected: $\partial_t \nabla^2 u {\color{green}{- c_2}} \partial_t u + c_1\det \boldsymbol{J}(u, \nabla^2 u) = 0$
---
## Function
The learned function, $\boldsymbol{f_\theta}$, will map the spatial coordinates, $\mathbf{x}_\phi \in \mathbb{R}^{D\phi}$, and time coordinate, $t \in \mathbb{R}$, to sea surface height, $u \in \mathbb{R}$.
$$
u = \boldsymbol{f_\theta}(\mathbf{x}_\phi, t)
$$
---
## Loss
The standard loss term is data-driven
$$
\mathcal{L}_{data} = \text{MSE}(u, \hat{u}) = \frac{1}{N} \sum_{n=1}^N \left(u - \boldsymbol{f_\phi}(\mathbf{x}_\phi, t) \right)^2
$$
However, there is no penalization to make the field behave the way we would expect. We also want a regularization which makes the field, $u$, behave how we would expect. This can be achieved by adding a physics-informed loss regularization term to the total loss.
$$
\mathcal{L} = \mathcal{L}_{data} + \lambda \mathcal{L}_{phy}
$$
This loss term can be minimized by effectively minimizing a PDE function. For example:
$$
\boldsymbol{f}_{phy}(\mathbf{x},t):= \partial_t u(\mathbf{x},t) + \mathcal{N}[u(\mathbf{x},t)] = 0
$$
where $\partial_t$ is the derivative of the field, $u$, wrt to time and $\mathcal{N}[\cdot]$ are some partial differential equations. We are interested in minimizing the full PDE, which we denote $\boldsymbol{f}_{phy}$, st it is 0. So the standard loss function applies.
$$
\mathcal{L}_{phy} = \frac{1}{N}\sum_{n=1}^N(\boldsymbol{f}_{phy}(\mathbf{x},t))^2
$$
The PDE for this method will be the quasi-geostrophic (QG) equation which the potential vorticity and a stream function. In the following section, we will show how this equation can be modified to act as a physics-informed regularization term on the loss function.
---
## QG Equation
We have the following PDE for the QG dynamics:
$$
\partial_t q + \det J(\psi, q) = 0
$$
where $q(x,t) \in \mathbb{R}^2 \times \mathbb{R} \rightarrow \mathbb{R}$ is the potential vorticity (PV), $\psi(x,t) \in \mathbb{R}^2 \times \mathbb{R} \rightarrow \mathbb{R}$ is the stream function, $\partial_t$ is the partial derivative wrt $t$, $\boldsymbol{J}$, is the Jacobian operator and $\det \boldsymbol{J}(\cdot,\cdot)$ is the *determinant* of the Jacobian.
**Objective**: We want to convert this PDE in terms of sea surface height (SSH) instead of PV and the stream function.
---
## QG Equation 4 SSH (TLDR)
**Note**: For the ease of notation, let's denote $u$ as the SSH. The above PDE can be written in terms of $u$
$$
c_2 \partial_t u - \partial_t \nabla^2 u - c_1\det \boldsymbol{J}(u, \nabla^2 u)= 0
$$
where $c_1 = \frac{g}{f}$ and $c_2= \frac{1}{L_R^2}$.
---
## Derivation
### Stream Function
Let's define the relationship between the SSH, $u$, and the stream function $\psi$.
$$
\psi = \frac{g}{f}u = c_1 u
$$
where $c_1 = \frac{g}{f}$. If we plug in the SSH into the PDE, we get:
$$
\partial_t q + \det J(c_1 u, q) = 0
$$
To simplify the notation, we will factor out the constant, $c_1$, from the determinant Jacobian term.
$$
\partial_t q + c_1\det J(u, q) = 0
$$
---
#### <span style="color:red">**Proof**</span>: Constants and determinant Jacobians
$$
\begin{aligned}
\det J(c_1 u, q) &= \partial_x c_1 u \partial_y q - \partial_y c_1 u \partial_x q \\
\det J(c_1 u, q) &= c_1 \partial_x u \partial_y q - c_1 \partial_y u \partial_x q \\
c_1\det J(u, q) &= c_1 \left(\partial_x u \partial_y q - \partial_y u \partial_x q\right) \\
\end{aligned}
$$
**Note**: we used the property that $\partial (c f) = c \partial f$.
<span style="color:red">**QED**</span>.
---
### Potential Vorticity
Now, let's define the relationship between the stream function and the PV. This is given by:
$$
\begin{aligned}
q &= \nabla^2 \psi - \frac{1}{L_R^2} \psi \\
&= \nabla^2 \psi - c_2 \psi
\end{aligned}
$$
where $\nabla^2$ is the Laplacian operator and $c_2 = \frac{1}{L_R^2}$. If we plug in SSH, $u$, into the stream function, as defined above, we get:
$$
\begin{aligned}
q &= \nabla^2 (c_1 u) - c_2 (c_1 u) \\
&= c_1 \nabla^2 u - c_3 u
\end{aligned}
$$
where $c_3 = c_1 c_2 = \frac{g}{f L_R^2}$. We can plug in the PV, $q$, into the PDE in terms of SSH, $u$.
$$
\partial_t \left(c_1 \nabla^2 u - c_3 u \right) + c_1\det J \left(u, c_1 \nabla^2 u - c_3 u \right) = 0
$$
---
Now, we can expand this equation but first let's break this up into two terms:
$$
\underbrace{\partial_t \left(c_1 \nabla^2 u - c_3 u \right)}_{\text{Term I}} + c_1\underbrace{\det J \left( u, c_1 \nabla^2 u - c_3 u \right)}_{\text{Term II}} = 0
$$
Now we will tackle both of these terms one at a time.
---
**Term I**
So term I is:
$$
f_1 := \partial_t \left(c_1 \nabla^2 u - c_3 u \right)
$$
We can expand this via the partial derivative, $\partial_t$.
$$
f_1 := c_1 \partial_t \nabla^2 u - c_3 \partial_t u
$$
So plugging this back into the PDE gives us:
$$
c_1 \partial_t \nabla^2 u + c_3 \partial_t u +c_1 \underbrace{\det J \left(u, c_1 \nabla^2 u - c_3 u \right)}_{\text{Term II}} = 0
$$
---
**Term II**
We can factorize this determinant Jacobian term.
$$
c_1\det J \left(u, c_1 \nabla^2 u - c_3 u \right) = c_1\det \boldsymbol{J}(u, c_1 \nabla^2 u) - \det \boldsymbol{J}(c_1 u, c_3 u)
$$
And furthermore, we can factor out the constants
$$
c_1\det J \left( u, c_1 \nabla^2 u - c_3 u \right) = c_1^2\det \boldsymbol{J}(u, \nabla^2 u) - c_1c_3 \det \boldsymbol{J}(u, u)
$$
And lastly, we can remove the final term because the determinant Jacobian of the same function is zero. So we have:
$$
c_1\det J \left( u, c_1 \nabla^2 u - c_3 u \right) = c_1^2\det \boldsymbol{J}(u, \nabla^2 u)
$$
---
#### <span style="color:red">**Proof**</span>: Determinant Jacobian Expansion
$$
\det \boldsymbol{J}(u,u) = \partial_x u \partial_y u - \partial_y u \partial_x u = \partial_x u \partial_y u - \partial_x u \partial_y u =0
$$
<span style="color:red">**QED**</span>.
---
#### <span style="color:red">**Proof**</span>: Determinant Jacobian Expansion
So term II is:
$$
f_2 := \det J \left(c_1 u, c_1 \nabla^2 u - c_3 u \right)
$$
We know the definition of the determinant of the Jacobian for a vector-valued function, $\boldsymbol{f} = [f_1(x,y), f_2(x,y)]^\top: \mathbb{R}^2 \rightarrow \mathbb{R}^2$, as there is an identity.
$$
\det J \left( f_1(x,y), f_2(x,y)\right) =
\partial_x f_1 \partial_y f_2 - \partial_y f_1 \partial_x f_2
$$
If use this identity with term II, we get:
$$
\det J \left(c_1 u, c_1 \nabla^2 u - c_3 u \right) = \partial_x (c_1 u) \partial_y (c_1 \nabla^2u - c_3 u) - \partial_y (c_1 u) \partial_x (c_1 \nabla^2u - c_3 u)
$$
Again, let's split this up into two subterms and tackle them one-by-one.
$$
\det J \left(c_1 u, c_1 \nabla^2 u - c_3 u \right) = \underbrace{\partial_x (c_1 u) \partial_y (c_1 \nabla^2u - c_3 u)}_{\text{Term IIa}} - \underbrace{\partial_y (c_1 u) \partial_x (c_1 \nabla^2u - c_3 u)}_{\text{Term IIa}}
$$
---
**Term IIa**
We have the following term for Term IIa:
$$
f_{2a} := \partial_x (c_1 u) \partial_y (c_1 \nabla^2u - c_3 u)
$$
We can expand the terms to get the following:
$$
f_{2a} := \partial_x (c_1 u) \partial_y \nabla^2 (c_1 u) - \partial_x (c_1 u) \partial_y (c_3 u)
$$
And we can simplify, by factoring out constants, to get:
$$
f_{2a} := c_1^2 \partial_x u \partial_y \nabla^2 u - c_1c_3 \partial_x u \partial_y u
$$
---
**Term IIb**
$$
f_{2b} := \partial_y (c_1 u) \partial_x (c_1 \nabla^2u - c_3 u)
$$
We can expand the terms to get the following:
$$
f_{2b} := c_1^2 \partial_y u \partial_x \nabla^2u - c_1 c_3 \partial_y u \partial_x u
$$
---
**Combined** (Term IIa, IIb)
We can substitute all of these two terms into our original expression
$$
\det J \left(c_1 u, c_1 \nabla^2 u + c_3 u \right) = \\
= f_{2a} - f_{2b}\\
= \left(c_1^2 \partial_x u \partial_y \nabla^2 u - c_1c_3 \partial_x u \partial_y u\right) - \left(c_1^2 \partial_y u \partial_x \nabla^2u - c_1 c_3 \partial_y u \partial_x u\right)\\
= c_1^2 \partial_x u \partial_y \nabla^2 u - c_1c_3 \partial_x u \partial_y u - c_1^2 \partial_y u \partial_x \nabla^2u + c_1 c_3 \partial_y u \partial_x u
$$
If we group the terms by operators, $\partial, \nabla^2$, then we get:
$$
\det J \left(c_1 u, c_1 \nabla^2 u - c_3 u \right) = \\
\underbrace{c_1^2 \partial_x u \partial_y \nabla^2 u - c_1^2 \partial_y u \partial_x \nabla^2u}_{\nabla^2} - \underbrace{c_1c_3 \partial_x u \partial_y u - c_1c_3\partial_y u \partial_x u}_{\partial}
$$
So each of these terms are determinant Jacobian terms, $\det \boldsymbol J$.
$$
\det J \left(c_1 u, c_1 \nabla^2 u - c_3 u \right) = c_1^2\det \boldsymbol{J}(u, \nabla^2 u) - c_1c_3 \det \boldsymbol{J}(u, u)
$$
We have the final form for our PDE in terms of SSH, $u$, which combines terms I and II.
Finally, we can see that the final term is zero because it's the determinant Jacobian of itself. So we are left with:
$$
\det J \left(c_1 u, c_1 \nabla^2 u - c_3 u \right) = c_1^2\det \boldsymbol{J}(u, \nabla^2 u)
$$
<span style="color:red">**QED**</span>.
---
### Final Form
So we have the final form for our PDE in terms of SSH.
$$
c_1 \partial_t \nabla^2 u - c_3 \partial_t u + c_1^2\det \boldsymbol{J}(u, \nabla^2 u) = 0
$$
We will factor out a constant term
$$
\partial_t \nabla^2 u - c_2 \partial_t u + c_1\det \boldsymbol{J}(u, \nabla^2 u)= 0
$$
I will multiply everything by a negative 1 so that we have a PDE of the form: $\partial_t u + \mathcal{N}()=0$ This give us:
$$
c_2 \partial_t u - \partial_t \nabla^2 u - c_1\det \boldsymbol{J}(u, \nabla^2 u)= 0
$$
<!-- ---
### Expanded Form
It is also good to do the expanded form with all of the partial derivatives because then we can see the computational portions with the gradient operations.
$$
\partial_t \nabla^2 u - c_2 \partial_t u + c_1 \partial_x u \partial_y \nabla^2 u - c_1 \partial_y u \partial_x \nabla^2u = 0
$$
Again, we will group the terms together in terms of the order of their derivatives, $(\nabla^2, \partial)$.
$$
\partial_t \nabla^2 u + c_1 \partial_x u \partial_y \nabla^2 u - c_1 \partial_y u \partial_x \nabla^2u - c_2 \partial_t u = 0
$$
Now, another grouping:
$$
\underbrace{\partial_t \nabla^2 u + c_1 \partial_x u \partial_y \nabla^2 u - c_1 \partial_y u \partial_x \nabla^2u}_{\nabla^2} - \underbrace{c_2 \partial_t u}_{\partial} = 0
$$ -->
<!-- ---
**Gradient** (order 1)
$$
\nabla u =
\begin{bmatrix}
\nabla_t u \\ \nabla_x u \\ \nabla_y u
\end{bmatrix}
$$
---
**Gradient** (order 3)
$$
\nabla^3 u =
\begin{bmatrix}
\nabla^3_t u \\ \nabla^3_x u \\ \nabla^3_y u
\end{bmatrix}
$$
$$
\nabla \cdot \nabla^2 u =
\begin{bmatrix}
\nabla_t & \nabla_x & \nabla_y
\end{bmatrix}
\cdot
\begin{bmatrix}
1 \\ c_1 \\ -c_1
\end{bmatrix}
\circ
\begin{bmatrix}
\nabla^2_t u \\ \nabla^2_x u \\ \nabla^2_y u
\end{bmatrix}
$$
-->