Scalar Time Advancement

Source Code: SCL_TimeAdvancement

SCL_TimeAdvancement.py
 1# Copyright (C) 2025 Sukanta Basu
 2#
 3# This program is free software: you can redistribute it and/or modify
 4# it under the terms of the GNU General Public License as published by
 5# the Free Software Foundation, either version 3 of the License, or
 6# (at your option) any later version.
 7#
 8# This program is distributed in the hope that it will be useful,
 9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16"""
17File: SCL_TimeAdvancement.py
18=============================
19
20:Author: Sukanta Basu
21:AI Assistance: Claude Code (Anthropic) and Codex (OpenAI) are used for documentation,
22                code restructuring, and performance optimization
23:Date: 2025-4-29
24:Description: implements Adams-Bashforth (AB2) for time integration
25of scalar equations
26"""
27
28# ============================================================
29#  Imports
30# ============================================================
31
32import jax
33
34# Import configuration from namelist
35from ..config.ConfigLoader import *
36
37# Import derived variables
38from ..config.DerivedVars import *
39
40
41# ============================================================
42#  Time advancement for potential temperature
43# ============================================================
44
45@jax.jit
46def AB2_TH(TH,
47           RHS_TH, RHS_TH_previous):
48    """
49    Parameters:
50    -----------
51    TH : ndarray of shape (nx, ny, nz)
52        Current potential temperature field
53    RHS_TH : ndarray of shape (nx, ny, nz)
54        Current right-hand side terms for potential temperature
55    RHS_TH_previous : ndarray of shape (nx, ny, nz)
56        Previous right-hand side terms for potential temperature
57
58    Returns:
59    --------
60    TH_new : ndarray of shape (nx, ny, nz)
61        Updated potential temperature field after time advancement
62    """
63
64    TH_new = TH + dt_nondim * (1.5 * RHS_TH - 0.5 * RHS_TH_previous)
65
66    # Boundary condition for top of the domain:
67    TH_new = TH_new.at[:, :, nz - 1].set(
68        TH_new[:, :, nz - 2] + inversion_nondim * dz
69    )
70
71    return TH_new
72
73
74# ============================================================
75#  Time advancement for specific humidity
76# ============================================================
77
78@jax.jit
79def AB2_Q(Q,
80          RHS_Q, RHS_Q_previous):
81    """
82    Parameters:
83    -----------
84    Q : ndarray (nx, ny, nz) — current specific humidity (kg/kg)
85    RHS_Q : ndarray (nx, ny, nz) — current RHS for Q
86    RHS_Q_previous : ndarray (nx, ny, nz) — previous RHS for Q
87
88    Returns:
89    --------
90    Q_new : ndarray (nx, ny, nz) — updated specific humidity
91    """
92    Q_new = Q + dt_nondim * (1.5 * RHS_Q - 0.5 * RHS_Q_previous)
93
94    # Top BC: apply free-atmosphere Q gradient (default q_inversion=0: zero gradient)
95    Q_new = Q_new.at[:, :, nz - 1].set(
96        Q_new[:, :, nz - 2] + q_inversion_nondim * dz
97    )
98
99    return Q_new