Skip to content

Conversation

@sajjadazimi
Copy link
Member

@sajjadazimi sajjadazimi commented Dec 3, 2025

Purpose

  • Improve stability by helping Newton converge,
  • clean up code for sgs advection terms in manual sparse Jacobian.

In this PR we add Jacobian elements for $\partial u^{3j}_err/\partial q^j$ and $\partial \rho a_err/\partial q^j$ through variations of $\rho^j$ with respect to condensate masses $q$.

$$\rho = \frac{p}{R_m T}$$

so

$$\frac{\partial \rho}{\partial q} = -\frac{p}{(R_m T)^2}\frac{\partial R_m T}{\partial q} = - \frac{\rho^2}{p}\frac{\partial R_m T}{\partial q}.$$

We know that

$$\begin{align*} R_m T =& R_m T_0 + \frac{R_m}{c_{p, m}}\left[mse - \phi - (q_t - q_l - q_i) LH_{v0} + q_i LH_{f0}\right]\\\ =& \left[(1-q_t)R_d + (q_t - q_l - q_i)R_v\right]T_0\\\ +& \frac{ \left[(1-q_t)R_d + (q_t - q_l - q_i)R_v\right]}{(1- q_t)c_{p, d} + (q_t - q_l - q_i)c_{p, v} + q_l c_{p, l} + q_i c_{p, i}}\left[mse - \phi - (q_t - q_l - q_i) LH_{v0} + q_i LH_{f0}\right].\\\ \end{align*}$$

The derivate of $R_m T$ with respect to the condensate mass $q$ is

$$\begin{align*} \frac{\partial R_m T}{\partial q} &= -R_v T_0 + \frac{R_m}{c_{p, m}} LH + \left[\frac{-R_v c_{p, m} - R_m \Delta c_p}{c_{p, m}^2}\right]c_{p, m} (T-T_0)\\\ &= -R_v T_0 + \frac{R_m}{c_{p, m}} LH -R_v (T-T_0) - \frac{R_m}{c_{p, m}} \Delta c_p (T-T_0)\\\ &=\frac{R_m}{c_{p, m}} \left[LH - \Delta c_p (T-T_0)\right] - R_v T\\\ &= \frac{\kappa_m}{\kappa_m + 1} \left[LH - \Delta c_p (T-T_0)\right] - R_v T, \end{align*}$$

where $\Delta c_p$ is the difference of $c_p$ of the condensate and water vapor, $LH$ is the latent heat of the condensate ($LH_{v0}$ for liquid and $LH_{v0} + LH_{f0}$ for ice) and $\kappa_m = R_m/c_{v, m}$. Therefore,

$$\begin{align*} \frac{\partial \rho}{\partial q} = - \frac{\rho^2}{p}\left[ \frac{\kappa_m}{\kappa_m + 1} \left[LH - \Delta c_p (T-T_0)\right] - R_v T\right]. \end{align*}$$
  • I have read and checked the items on the review checklist.

@sajjadazimi sajjadazimi force-pushed the sa/improve_stability branch 4 times, most recently from 095d9ba to a4255f8 Compare December 4, 2025 21:58
@sajjadazimi sajjadazimi requested a review from szy21 December 4, 2025 22:00
Comment on lines 909 to 912
∂ᶜρaʲ_err_∂ᶜq_totʲ =
matrix[@name(c.sgsʲs.:(1).ρa), @name(c.sgsʲs.:(1).q_tot)]
# set to zero because in the following loop we do +=
@. ∂ᶜρaʲ_err_∂ᶜq_totʲ = zero(typeof(∂ᶜρaʲ_err_∂ᶜq_totʲ))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this (I think in the loop we are doing =)?

@szy21 szy21 requested a review from costachris December 5, 2025 00:18
# term to be NaN (0 * ... / 0). The ifelse handles this case explicitly.
return ρa == 0 ? ρχ / ρ : weight * ρaχ / ρa + (1 - weight) * ρχ / ρ
# In practice the condition ρa < eps is used to avoid NaNs in autodiff.
return ρa < eps(typeof(ρ)) ? ρχ / ρ : weight * ρaχ / ρa + (1 - weight) * ρχ / ρ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sets it to grid-mean if ρa is smaller than eps - is that what you want? If so could you update the comment?

Copy link
Member

@szy21 szy21 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just some minor comments.

@sajjadazimi sajjadazimi added this pull request to the merge queue Dec 5, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 5, 2025
add Jacobain elements for du3^j_err_dq^j and drhoa_err_dq^j
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Dec 5, 2025
@sajjadazimi sajjadazimi added this pull request to the merge queue Dec 5, 2025
Merged via the queue into main with commit 866c94a Dec 5, 2025
16 of 17 checks passed
@sajjadazimi sajjadazimi deleted the sa/improve_stability branch December 5, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants