|
| 1 | +""" |
| 2 | +This module implements the Syed-Mann (2024) low-frequency wind fluctuation model. |
| 3 | +""" |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from scipy import integrate |
| 9 | + |
| 10 | + |
| 11 | +def _compute_kappa(k1: float, k2: float, psi: float) -> float: |
| 12 | + """ |
| 13 | + Subroutine to compute the horizontal wavevector :math:`\kappa`, defined by |
| 14 | +
|
| 15 | + .. math:: |
| 16 | + \kappa = \sqrt{2(k_1^2 \cos^2(\psi) + k_2^2 \sin^2(\psi))} |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + k1 : float |
| 21 | + Wavenumber k1 |
| 22 | +
|
| 23 | + k2 : float |
| 24 | + Wavenumber k2 |
| 25 | +
|
| 26 | + psi : float |
| 27 | + "Anisotropy parameter" angle :math:`\psi`, in radians |
| 28 | +
|
| 29 | + Returns |
| 30 | + ------- |
| 31 | + float |
| 32 | + Computed kappa value |
| 33 | + """ |
| 34 | + |
| 35 | + return np.sqrt(2.0 * ((k1**2) * np.cos(psi) ** 2 + (k2**2) * np.sin(psi) ** 2)) |
| 36 | + |
| 37 | + |
| 38 | +def _compute_E(kappa: float, c: float, L2D: float, z_i: float) -> float: |
| 39 | + """ |
| 40 | + Subroutine to compute the energy spectrum :math:`E(\kappa)` with the attenuation factor, |
| 41 | + defined by |
| 42 | +
|
| 43 | + .. math:: |
| 44 | + E(\kappa) = \frac{c \kappa^3}{(L_{2\textrm{D}}^{-2} + \kappa^2)^{7/3}} \cdot |
| 45 | + \frac{1}{1 + \kappa^2 z_i^2} |
| 46 | +
|
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | + kappa : float |
| 50 | + Replacement "wavenumber" :math:`\kappa` |
| 51 | +
|
| 52 | + c : float |
| 53 | + Scaling factor :math:`c` used to correct the variance |
| 54 | +
|
| 55 | + L2D : float |
| 56 | + Length scale :math:`L_{2\textrm{D}}` |
| 57 | + """ |
| 58 | + if np.isclose(kappa, 0.0): |
| 59 | + return 0.0 |
| 60 | + |
| 61 | + denom = (1.0 / (L2D**2) + kappa**2) ** (7.0 / 3.0) |
| 62 | + atten = 1.0 / (1.0 + (kappa * z_i) ** 2) |
| 63 | + return c * (kappa**3) / denom * atten |
| 64 | + |
| 65 | + |
| 66 | +def _estimate_c(sigma2: float, L2D: float, z_i: float) -> float: |
| 67 | + """ |
| 68 | + Subroutine to estimate the scaling factor :math:`c` from the target variance :math:`\sigma^2`. |
| 69 | +
|
| 70 | + This is achieved by approximating the integral of :math:`E(\kappa)` from :math:`\kappa=0` to |
| 71 | + :math:`\infty` by quadrature, since |
| 72 | + .. math:: |
| 73 | + \int_0^\infty E(\kappa) |
| 74 | + = c \int_0^\infty \frac{\kappa^3}{(L_{2\textrm{D}}^{-2} + \kappa^2)^{7/3}} \cdot |
| 75 | + \frac{1}{1 + \kappa^2 z_i^2} |
| 76 | + = \sigma^2 |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + sigma2 : float |
| 81 | + Target variance :math:`\sigma^2` |
| 82 | +
|
| 83 | + L2D : float |
| 84 | + Length scale :math:`L_{2\textrm{D}}` |
| 85 | +
|
| 86 | + z_i : float |
| 87 | + Height :math:`z_i` |
| 88 | + """ |
| 89 | + |
| 90 | + def integrand(kappa: float) -> float: |
| 91 | + return kappa**3 / ((1.0 / (L2D**2) + kappa**2) ** (7.0 / 3.0)) * (1.0 / (1.0 + (kappa * z_i) ** 2)) |
| 92 | + |
| 93 | + val, err = integrate.quad(integrand, 0, np.inf) |
| 94 | + |
| 95 | + return sigma2 / val |
| 96 | + |
| 97 | + |
| 98 | +def generate_2D_lowfreq( |
| 99 | + Nx: int, |
| 100 | + Ny: int, |
| 101 | + L1: float, |
| 102 | + L2: float, |
| 103 | + psi_degs: float, |
| 104 | + sigma2: float, |
| 105 | + L2D: float, |
| 106 | + z_i: float, |
| 107 | + c: Optional[float] = None, |
| 108 | +) -> np.ndarray: |
| 109 | + """ |
| 110 | + Generates the 2D low-frequency wind fluctuation component of the Syed-Mann (2024) 2D+3D model. |
| 111 | +
|
| 112 | + Parameters |
| 113 | + ---------- |
| 114 | + Nx : int |
| 115 | + Number of grid points in the x-direction |
| 116 | + Ny : int |
| 117 | + Number of grid points in the y-direction |
| 118 | + L1 : float |
| 119 | + Length of the domain in the x-direction |
| 120 | + L2 : float |
| 121 | + Length of the domain in the y-direction |
| 122 | + psi_degs : float |
| 123 | + "Anisotropy parameter" angle :math:`\psi`, in degrees |
| 124 | + sigma2 : float |
| 125 | + Target variance :math:`\sigma^2` |
| 126 | + L2D : float |
| 127 | + Length scale :math:`L_{2\textrm{D}}` |
| 128 | + z_i : float |
| 129 | + Height :math:`z_i` |
| 130 | + c : float |
| 131 | + Scaling factor :math:`c` to use for the energy spectrum. If not provided, it is |
| 132 | + estimated by quadrature from the provided target variance :math:`\sigma^2`. |
| 133 | +
|
| 134 | + Returns |
| 135 | + ------- |
| 136 | + np.ndarray |
| 137 | + Generated 2D low-frequency wind fluctuation component. This is `Nx` by `Ny` by 2, |
| 138 | + where the third dimension is the u- (longitudinal) and v-components (transverse). |
| 139 | +
|
| 140 | + TODO ^^ |
| 141 | + """ |
| 142 | + |
| 143 | + assert 0 < psi_degs and psi_degs < 90, "Anisotropy parameter psi_degs must be between 0 and 90 degrees" |
| 144 | + |
| 145 | + psi = np.deg2rad(psi_degs) |
| 146 | + |
| 147 | + if c is None: |
| 148 | + c = _estimate_c(sigma2, L2D, z_i) |
| 149 | + |
| 150 | + dx = L1 / Nx |
| 151 | + dy = L2 / Ny |
| 152 | + |
| 153 | + kx_arr = 2.0 * np.pi * np.fft.fftfreq(Nx, d=dx) |
| 154 | + ky_arr = 2.0 * np.pi * np.fft.fftfreq(Ny, d=dy) |
| 155 | + kx_arr = np.fft.fftshift(kx_arr) |
| 156 | + ky_arr = np.fft.fftshift(ky_arr) |
| 157 | + |
| 158 | + amp2 = np.zeros((Nx, Ny), dtype=np.float64) |
| 159 | + |
| 160 | + factor_16 = (2.0 * np.pi**2) / L1 |
| 161 | + |
| 162 | + for ix in range(Nx): |
| 163 | + for iy in range(Ny): |
| 164 | + kx = kx_arr[ix] |
| 165 | + ky = ky_arr[iy] |
| 166 | + |
| 167 | + kappa = _compute_kappa(kx, ky, psi) |
| 168 | + E_val = _compute_E(kappa, c, L2D, z_i) |
| 169 | + |
| 170 | + if kappa < 1e-12: |
| 171 | + phi_11 = 0.0 |
| 172 | + else: |
| 173 | + phi_11 = E_val / (np.pi * kappa) |
| 174 | + |
| 175 | + amp2[ix, iy] = factor_16 * phi_11 |
| 176 | + |
| 177 | + Uhat = np.zeros((Nx, Ny), dtype=np.complex128) |
| 178 | + |
| 179 | + for ix in range(Nx): |
| 180 | + for iy in range(Ny): |
| 181 | + amp = np.sqrt(amp2[ix, iy]) |
| 182 | + phase = (np.random.normal() + 1j * np.random.normal()) / np.sqrt(2.0) |
| 183 | + Uhat[ix, iy] = amp * phase |
| 184 | + |
| 185 | + Uhat_unshift = np.fft.ifftshift(Uhat, axes=(0, 1)) |
| 186 | + u_field_complex = np.fft.ifft2(Uhat_unshift, s=(Nx, Ny)) |
| 187 | + u_field = np.real(u_field_complex) |
| 188 | + |
| 189 | + var_now = np.var(u_field) |
| 190 | + if var_now > 1e-12: |
| 191 | + u_field *= np.sqrt(sigma2 / var_now) |
| 192 | + |
| 193 | + return u_field |
0 commit comments