PHYEX 0.9.5
PHYsique EXternalisée
Loading...
Searching...
No Matches
case_xarray_simple.py
Go to the documentation of this file.
1"""
2CaseXarray demo using SimplePhysicsTQ (no PHYEX required)
3
4Creates an idealised 90-level atmosphere and runs a short simulation
5with the all-or-nothing saturation adjustment and K-gradient turbulence.
6
7Usage:
8 python case_xarray_simple.py
9"""
10
11import logging
12import os
13
14import numpy as np
15import xarray as xr
16from matplotlib import pyplot as plt
17
18from pyphyex.phyex1d import CaseXarray, ExperimentRunner, SimplePhysicsTQ
19
20logging.basicConfig(level=logging.INFO)
21
22
24 """Create an idealised 90-level atmosphere up to 20 km."""
25 nz = 90
26 z = np.linspace(0, 20000, nz)
27 time = np.array([0., 3600.])
28
29 T = np.broadcast_to(290. - z * 0.0065, (2, nz))
30 qv = np.broadcast_to(0.01 * np.exp(-z / 3000.), (2, nz))
31 u = np.zeros((2, nz))
32 v = np.zeros((2, nz))
33 w = np.zeros((2, nz))
34 tke = np.zeros((2, nz))
35
36 ds = xr.Dataset(
37 {
38 'Zs': ('time', np.array([0., 0.])),
39 'zh': (('time', 'z'), np.broadcast_to(z, (2, nz))),
40 'orog': ('time', np.array([0., 0.])),
41 'Ps': ('time', np.array([101325., 101325.])),
42 'Ts': ('time', np.array([290., 290.])),
43 'T': (('time', 'z'), T),
44 'qv': (('time', 'z'), qv),
45 'u': (('time', 'z'), u),
46 'v': (('time', 'z'), v),
47 'w': (('time', 'z'), w),
48 'tke': (('time', 'z'), tke),
49 'lat': ('time', np.array([0., 0.])),
50 'lon': ('time', np.array([0., 0.])),
51 'ts_forc': ('time', np.array([290., 290.])),
52 'ps_forc': ('time', np.array([101325., 101325.])),
53 'zh_forc': (('time', 'z'), np.broadcast_to(z, (2, nz))),
54 },
55 coords={'time': time, 'z': z},
56 attrs={
57 'adv_theta': 0,
58 'adv_ta': 0,
59 'adv_rv': 0,
60 'adv_qv': 0,
61 'adv_ua': 0,
62 'adv_va': 0,
63 'nudging_theta': 0,
64 'nudging_ta': 0,
65 'nudging_rv': 0,
66 'nudging_qv': 0,
67 'nudging_ua': 0,
68 'nudging_va': 0,
69 'forc_wa': 0,
70 'forc_geo': 0,
71 'radiation': 'none',
72 'surface_forcing_temp': 'ts',
73 'surface_forcing_moisture': 'none',
74 'surface_forcing_wind': 'none',
75 'surface_type': 'ocean',
76 'start_date': '2000-01-01T00:00:00',
77 },
78 )
79 return ds
80
81
82def make_plots(runner, output_dir):
83 """Create overview plots of the simulation."""
84 plots = [
85 ('T.png', ['T'], {'y_var': 'P'}),
86 ('qv.png', ['qv'], {'y_var': 'P'}),
87 ('qc.png', ['qc'], {'y_var': 'P', 'vmin': 0., 'vmax': 5.e-5}),
88 ('Theta.png', ['Theta'], {'y_var': 'P'}),
89 ]
90 for filename, var_names, options in plots:
91 fig, ax = plt.subplots()
92 kwargs_contourf = {}
93 vmin = options.pop('vmin', None)
94 vmax = options.pop('vmax', None)
95 if vmin is not None:
96 kwargs_contourf['vmin'] = vmin
97 if vmax is not None:
98 kwargs_contourf['vmax'] = vmax
99 if kwargs_contourf:
100 options['kwargs_contourf'] = kwargs_contourf
101 runner.plot_evol(ax, var_names, **options)
102 if options.get('y_var', 'X') == 'P':
103 ax.invert_yaxis()
104 filepath = os.path.join(output_dir, filename)
105 fig.savefig(filepath)
106 plt.close(fig)
107 print(f' Saved {filepath}')
108
109
110def main():
112 case = CaseXarray(ds)
113
114 print(f'Created CaseXarray (duration: {case.duration} s)')
115 print(f'Variables: {list(ds.data_vars)}')
116
117 output_dir = './output_casexarray_simple'
118 os.makedirs(output_dir, exist_ok=True)
119
120 runner = ExperimentRunner(
121 case,
122 [
123 {
124 'dt': 60,
125 'grid': 'L90mesonh',
126 'name': 'simple_demo',
127 'class': SimplePhysicsTQ,
128 },
129 ],
130 output_dir=output_dir,
131 comp_name='demo',
132 )
133
134 print('\nRunning experiment (no PHYEX needed)...')
135 runner.run()
136 print('Done!')
137
138 print('\nCreating plots...')
139 make_plots(runner, output_dir)
140 print('\nAll plots saved in', output_dir)
141
142
143if __name__ == '__main__':
144 main()
make_plots(runner, output_dir)