/
example_stommel.py
216 lines (180 loc) · 9.4 KB
/
example_stommel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import math
from argparse import ArgumentParser
from datetime import timedelta as delta
import numpy as np
import pytest
from parcels import AdvectionEE
from parcels import AdvectionRK4
from parcels import AdvectionRK45
from parcels import FieldSet
from parcels import JITParticle
from parcels import ScipyParticle
from parcels import BaseKernel
from parcels import ParticleSetSOA, ParticleFileSOA, KernelSOA # noqa
from parcels import ParticleSetAOS, ParticleFileAOS, KernelAOS # noqa
from parcels import ParticleSetNodes, ParticleFileNodes, KernelNodes # noqa
from parcels import GenerateID_Service, SequentialIdGenerator, LibraryRegisterC # noqa
from parcels import timer
from parcels import Variable
pset_modes = ['soa', 'aos']
ptype = {'scipy': ScipyParticle, 'jit': JITParticle}
method = {'RK4': AdvectionRK4, 'EE': AdvectionEE, 'RK45': AdvectionRK45}
pset_type = {'soa': {'pset': ParticleSetSOA, 'pfile': ParticleFileSOA, 'kernel': KernelSOA},
'aos': {'pset': ParticleSetAOS, 'pfile': ParticleFileAOS, 'kernel': KernelAOS},
'nodes': {'pset': ParticleSetNodes, 'pfile': ParticleFileNodes, 'kernel': KernelNodes}}
def stommel_fieldset(xdim=200, ydim=200, grid_type='A'):
"""Simulate a periodic current along a western boundary, with significantly
larger velocities along the western edge than the rest of the region
The original test description can be found in: N. Fabbroni, 2009,
Numerical Simulation of Passive tracers dispersion in the sea,
Ph.D. dissertation, University of Bologna
http://amsdottorato.unibo.it/1733/1/Fabbroni_Nicoletta_Tesi.pdf
"""
a = b = 10000 * 1e3
scalefac = 0.05 # to scale for physically meaningful velocities
dx, dy = a / xdim, b / ydim
# Coordinates of the test fieldset (on A-grid in deg)
lon = np.linspace(0, a, xdim, dtype=np.float32)
lat = np.linspace(0, b, ydim, dtype=np.float32)
# Define arrays U (zonal), V (meridional) and P (sea surface height)
U = np.zeros((lat.size, lon.size), dtype=np.float32)
V = np.zeros((lat.size, lon.size), dtype=np.float32)
P = np.zeros((lat.size, lon.size), dtype=np.float32)
beta = 2e-11
r = 1/(11.6*86400)
es = r/(beta*a)
for j in range(lat.size):
for i in range(lon.size):
xi = lon[i] / a
yi = lat[j] / b
P[j, i] = (1 - math.exp(-xi / es) - xi) * math.pi * np.sin(math.pi * yi) * scalefac
if grid_type == 'A':
U[j, i] = -(1 - math.exp(-xi / es) - xi) * math.pi ** 2 * np.cos(math.pi * yi) * scalefac
V[j, i] = (math.exp(-xi / es) / es - 1) * math.pi * np.sin(math.pi * yi) * scalefac
if grid_type == 'C':
V[:, 1:] = (P[:, 1:] - P[:, 0:-1]) / dx * a
U[1:, :] = -(P[1:, :] - P[0:-1, :]) / dy * b
data = {'U': U, 'V': V, 'P': P}
dimensions = {'lon': lon, 'lat': lat}
fieldset = FieldSet.from_data(data, dimensions, mesh='flat')
if grid_type == 'C':
fieldset.U.interp_method = 'cgrid_velocity'
fieldset.V.interp_method = 'cgrid_velocity'
return fieldset
def InitP(particle, fieldset, time):
if particle.age < particle.dt:
particle.p_start = fieldset.P[time, particle.depth, particle.lat, particle.lon]
def UpdateP(particle, fieldset, time):
particle.p = fieldset.P[time, particle.depth, particle.lat, particle.lon]
def AgeP(particle, fieldset, time):
particle.age += particle.dt
if particle.age > fieldset.maxage:
particle.delete()
def idgenenerator(pset_mode):
idgen = None
if pset_mode == 'nodes':
idgen = GenerateID_Service(SequentialIdGenerator)
idgen.setDepthLimits(0., 1.0)
idgen.setTimeLine(0.0, 1.0)
return idgen
def clib_register(pset_mode):
c_lib_register = None
if pset_mode == 'nodes':
c_lib_register = LibraryRegisterC()
return c_lib_register
def stommel_example(npart=1, mode='jit', verbose=False, method=AdvectionRK4, grid_type='A',
outfile="StommelParticle.nc", repeatdt=None, maxage=None, write_fields=True, pset_mode='soa',
idgen=None, c_lib_register=None):
timer.fieldset = timer.Timer('FieldSet', parent=timer.stommel)
fieldset = stommel_fieldset(grid_type=grid_type)
if write_fields:
filename = 'stommel'
fieldset.write(filename)
timer.fieldset.stop()
# Determine particle class according to mode
timer.pset = timer.Timer('Pset', parent=timer.stommel)
timer.psetinit = timer.Timer('Pset_init', parent=timer.pset)
ParticleClass = JITParticle if mode == 'jit' else ScipyParticle
class MyParticle(ParticleClass):
p = Variable('p', dtype=np.float32, initial=0.)
p_start = Variable('p_start', dtype=np.float32, initial=fieldset.P)
age = Variable('age', dtype=np.float32, initial=0.)
if pset_mode != 'nodes':
pset = pset_type[pset_mode]['pset'].from_line(fieldset, size=npart, pclass=MyParticle, repeatdt=repeatdt,
start=(10e3, 5000e3), finish=(100e3, 5000e3), time=0)
else:
pset = pset_type[pset_mode]['pset'].from_line(fieldset, size=npart, pclass=MyParticle, repeatdt=repeatdt,
start=(10e3, 5000e3), finish=(100e3, 5000e3), time=0,
idgen=idgen, c_lib_register=c_lib_register)
if verbose:
print("Initial particle positions:\n%s" % pset)
# Execute for 30 days, with 1hour timesteps and 12-hourly output
runtime = delta(days=600)
dt = delta(hours=1)
outputdt = delta(days=5)
maxage = runtime.total_seconds() if maxage is None else maxage
fieldset.add_constant('maxage', maxage)
print("Stommel: Advecting %d particles for %s" % (npart, runtime))
timer.psetinit.stop()
timer.psetrun = timer.Timer('Pset_run', parent=timer.pset)
kernel = method
if pset_mode == 'nodes' and not isinstance(method, BaseKernel):
kernel = pset.Kernel(kernel)
pset.execute(kernel + pset.Kernel(UpdateP) + pset.Kernel(AgeP), runtime=runtime, dt=dt,
moviedt=None, output_file=pset.ParticleFile(name=outfile, outputdt=outputdt))
if verbose:
print("Final particle positions:\n%s" % pset)
timer.psetrun.stop()
timer.pset.stop()
return pset
@pytest.mark.parametrize('pset_mode', pset_modes)
@pytest.mark.parametrize('grid_type', ['A', 'C'])
@pytest.mark.parametrize('mode', ['jit', 'scipy'])
def test_stommel_fieldset(pset_mode, mode, grid_type, tmpdir):
timer.root = timer.Timer('Main')
idgen = None
c_lib_register = None
timer.stommel = timer.Timer('Stommel', parent=timer.root)
outfile = tmpdir.join("StommelParticle")
# ---- Comment CK: ----------------------------------------------------------------------------------------------- #
# the testing issue here is associated to the C-grid type with node-based psets - hence skipping that test for now #
psetRK4 = stommel_example(1, mode=mode, method=method['RK4'], grid_type=grid_type, outfile=outfile, write_fields=False, pset_mode=pset_mode, idgen=idgen, c_lib_register=c_lib_register)
psetRK45 = stommel_example(1, mode=mode, method=method['RK45'], grid_type=grid_type, outfile=outfile, write_fields=False, pset_mode=pset_mode, idgen=idgen, c_lib_register=c_lib_register)
assert np.allclose(psetRK4.lon, psetRK45.lon, rtol=1e-3)
assert np.allclose(psetRK4.lat, psetRK45.lat, rtol=1.1e-3)
err_adv = np.abs(psetRK4.p_start - psetRK4.p)
assert(err_adv <= 1.e-1).all()
err_smpl = np.array([abs(psetRK4.p[i] - psetRK4.fieldset.P[0., psetRK4.lon[i], psetRK4.lat[i], psetRK4.depth[i]]) for i in range(psetRK4.size)])
assert(err_smpl <= 1.e-1).all()
timer.stommel.stop()
timer.root.stop()
timer.root.print_tree()
if __name__ == "__main__":
timer.root = timer.Timer('Main')
timer.args = timer.Timer('Args', parent=timer.root)
p = ArgumentParser(description="""
Example of particle advection in the steady-state solution of the Stommel equation""")
p.add_argument('mode', choices=('scipy', 'jit'), nargs='?', default='jit',
help='Execution mode for performing computation')
p.add_argument('-p', '--particles', type=int, default=1,
help='Number of particles to advect')
p.add_argument('-v', '--verbose', action='store_true', default=False,
help='Print particle information before and after execution')
p.add_argument('-m', '--method', choices=('RK4', 'EE', 'RK45'), default='RK4',
help='Numerical method used for advection')
p.add_argument('-o', '--outfile', default='StommelParticle.nc',
help='Name of output file')
p.add_argument('-r', '--repeatdt', default=None, type=int,
help='repeatdt of the ParticleSet')
p.add_argument('-a', '--maxage', default=None, type=int,
help='max age of the particles (after which particles are deleted)')
p.add_argument('-psm', '--pset_mode', choices=('soa', 'aos', 'nodes'), default='soa',
help='max age of the particles (after which particles are deleted)')
args = p.parse_args()
timer.args.stop()
timer.stommel = timer.Timer('Stommel', parent=timer.root)
stommel_example(args.particles, mode=args.mode, verbose=args.verbose, method=method[args.method],
outfile=args.outfile, repeatdt=args.repeatdt, maxage=args.maxage)
timer.stommel.stop()
timer.root.stop()
timer.root.print_tree()