Skip to content

Commit 7509706

Browse files
committed
add geostrophic adjustment exmple
1 parent 7a7681a commit 7509706

File tree

11 files changed

+467
-43
lines changed

11 files changed

+467
-43
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ This is a list of the problems that can be solved by `xinvert`:
7070
| <img src="./pics/Gallery_SWMReference.png" width="380"><br/>[invert PV balance equation for<br/> steady reference state](./docs/source/notebooks/05_reference_SWM.ipynb)| <img src="./pics/Gallery_GillMatsuno.png" width="380"><br/>[invert Gill-Matsuno model for<br/> wind and mass fields](./docs/source/notebooks/07_Gill_Matsuno_model.ipynb) |
7171
| <img src="./pics/Gallery_StommelMunk.png" width="380"><br/>[invert Stommel-Munk model for<br/> wind-driven ocean circulation](./docs/source/notebooks/08_Stommel_Munk_model.ipynb) | <img src="./pics/Gallery_Fofonoff.png" width="380"><br/>[invert Fofonoff model for<br/> inviscid/adiabatic steady state](./docs/source/notebooks/09_Fofonoff_flow.ipynb) |
7272
| <img src="./pics/Gallery_Bretherton.png" width="380"><br/>[invert Bretherton model for<br/> steady flow over topography](./docs/source/notebooks/10_Bretherton_flow_over_topography.ipynb) | <img src="./pics/Gallery_Omega.png" width="380"><br/>[invert Omega equation for<br/> QG vertical velocity](./docs/source/notebooks/11_Omega_equation.ipynb) |
73+
| <img src="./pics/Gallery_GeoAdjust.png" width="380"><br/>[invert geostrophic-adjustment problem](./docs/source/notebooks/12_GeostrophicAdjustment.ipynb) | ... add more examples ... |
7374

7475

7576

docs/source/notebooks/00_Introduction.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"name": "python",
133133
"nbconvert_exporter": "python",
134134
"pygments_lexer": "ipython3",
135-
"version": "3.10.9"
135+
"version": "3.13.2"
136136
}
137137
},
138138
"nbformat": 4,

docs/source/notebooks/12_GeostrophicAdjustment.ipynb

Lines changed: 420 additions & 0 deletions
Large diffs are not rendered by default.

pics/Gallery_GeoAdjust.png

29.6 KB
Loading

pics/adjustment.png

29.6 KB
Loading

tests/test_GeoAdjustment.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,37 @@
88
#%% classical cases
99
import numpy as np
1010
import xarray as xr
11-
from xinvert.xinvert import invert_GeoAdjustment
12-
13-
# def test_adjustment():
14-
yc = 501
15-
ydef = np.linspace(-75, -25, yc)
16-
ydef = xr.DataArray(ydef, dims='lat', coords={'lat':ydef})
17-
18-
R = 6371200
19-
O = 7.292e-5
20-
h0 = ydef - ydef + 1500
21-
h0[int(yc/2):] = 1520
22-
g = 9.81
23-
24-
f = 2 * O * np.sin(np.deg2rad(ydef))
25-
PV0 = f / h0
26-
27-
# invert
28-
iParams = {
29-
'BCs' : ['extend'],
30-
'mxLoop' : 100000,
31-
'tolerance': -1e-11,
32-
'optArg' : 1.8,
33-
'undef' : -9999,
34-
}
35-
36-
deg2m = R/180*np.pi
37-
38-
h = invert_GeoAdjustment(h0, dims=['lat'], coords='lat', iParams=iParams)
39-
u = - h.differentiate('lat') / deg2m * g / f
40-
PV = (f - u.differentiate('lat') / deg2m) / h
11+
from xinvert import invert_GeoAdjustment
12+
13+
def test_adjustment():
14+
yc = 501
15+
ydef = np.linspace(-75, -25, yc)
16+
ydef = xr.DataArray(ydef, dims='lat', coords={'lat':ydef})
17+
18+
R = 6371200
19+
O = 7.292e-5
20+
h0 = ydef - ydef + 1500
21+
h0[int(yc/2):] = 1520
22+
g = 9.81
23+
24+
f = 2 * O * np.sin(np.deg2rad(ydef))
25+
PV0 = f / h0
26+
27+
# invert
28+
iParams = {
29+
'BCs' : ['extend'],
30+
'mxLoop' : 100000,
31+
'tolerance': -1e-11,
32+
'optArg' : 1.8,
33+
'undef' : -9999,
34+
}
35+
36+
deg2m = R/180*np.pi
37+
38+
h = invert_GeoAdjustment(h0, dims=['lat'], coords='lat', iParams=iParams)
39+
u = - h.differentiate('lat') / deg2m * g / f
40+
PV = (f - u.differentiate('lat') / deg2m) / h
41+
42+
assert h.dims == h0.dims
43+
assert h.shape == h0.shape
4144

41 Bytes
Binary file not shown.
47 Bytes
Binary file not shown.
20 Bytes
Binary file not shown.

xinvert/apps.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def invert_RefState(PV, dims, coords='z-lat', icbc=None,
145145
icbc, ['Ang0', 'Gamma', 'g', 'Omega', 'Rearth'], mParams, iParams)
146146

147147

148-
def invert_GeoAdjustment(h0, dims, coords='lat', icbc=None,
148+
def invert_GeoAdjustment(PV0, dims, coords='lat', icbc=None,
149149
mParams=default_mParams, iParams=default_iParams):
150150
r"""(PV) inversion for a geostrophic adjustment model.
151151
@@ -161,18 +161,18 @@ def invert_GeoAdjustment(h0, dims, coords='lat', icbc=None,
161161
.. math::
162162
163163
A = \cos\phi / f
164-
B = - f / g / h0
164+
B = - q_0 \ cos\phi / g
165165
F = - f \cos\phi / g
166166
167167
Invert this equation for geostrophically adjusted free surface :math:`h` given
168-
the initial free surface distribution.
168+
the initial PV distribution :math:`q_0`.
169169
170170
Parameters
171171
----------
172-
h0: xarray.DataArray
173-
Initial free surface.
172+
PV0: xarray.DataArray
173+
Initial PV distribution.
174174
dims: list
175-
Dimension combination for the inversion e.g., ['lat', 'lon'].
175+
Dimension name for the inversion e.g., ['lat'].
176176
coords: {'lat-lon', 'cartesian'}, optional
177177
Coordinate combinations in which inversion is performed.
178178
icbc: xarray.DataArray, optional
@@ -185,9 +185,9 @@ def invert_GeoAdjustment(h0, dims, coords='lat', icbc=None,
185185
Returns
186186
-------
187187
xarray.DataArray
188-
Results (angular momentum Λ) of the SOR inversion.
188+
Results (free surface after adjustment) of the SOR inversion.
189189
"""
190-
return __template(__coeffs_GeoAdjustment, inv_standard1D, 1, h0, dims, coords,
190+
return __template(__coeffs_GeoAdjustment, inv_standard1D, 1, PV0, dims, coords,
191191
icbc, ['g', 'Rearth', 'Omega'], mParams, iParams)
192192

193193

@@ -1524,12 +1524,12 @@ def diff_2nd(M, cosH, delY):
15241524
return F, initS, (A, B)
15251525

15261526

1527-
def __coeffs_GeoAdjustment(h0, dims, coords, mParams, iParams, icbc):
1527+
def __coeffs_GeoAdjustment(PV0, dims, coords, mParams, iParams, icbc):
15281528
"""Calculating coefficients for geostrophic adjustment model."""
15291529
g = mParams['g']
15301530
Omega = mParams['Omega']
15311531

1532-
maskF, initS, zero = __mask_FS(h0, dims, iParams, icbc)
1532+
maskF, initS, zero = __mask_FS(PV0, dims, iParams, icbc)
15331533

15341534
if coords.lower() == 'lat': # dims[0] is θ, dims[1] is lat
15351535
lats = np.deg2rad(maskF[dims[0]])
@@ -1539,7 +1539,7 @@ def __coeffs_GeoAdjustment(h0, dims, coords, mParams, iParams, icbc):
15391539
fH = 2 * Omega * np.sin((lats+lats.shift({dims[0]:1}))/2.0)
15401540

15411541
A = zero + cosH / fH
1542-
B = zero - f * cosG / g / h0
1542+
B = zero - PV0 * cosG / g
15431543
F = zero - f * cosG / g
15441544

15451545
elif coords.lower() == 'cartesian': # dims[0] is θ, dims[1] is r

0 commit comments

Comments
 (0)