Skip to content

Commit 3218321

Browse files
authored
Merge pull request #75 from Parcels-code/fix_PolyTEOS_kernel
Bugfix PolyTEOS10_bsq kernel
2 parents d87b952 + 8f73e87 commit 3218321

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

plasticparcels/constructors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ def create_particleset(fieldset, settings, release_locations):
232232
else:
233233
plastic_amounts = np.full_like(lons, np.nan)
234234

235+
if 'depths' in release_locations.keys():
236+
depths = release_locations['depths']
237+
else:
238+
depths = np.full_like(lons, 0.)
239+
235240
# Set particle properties
236241
plastic_densities = np.full(lons.shape, settings['plastictype']['plastic_density'])
237242
plastic_diameters = np.full(lons.shape, settings['plastictype']['plastic_diameter'])
@@ -254,6 +259,7 @@ def create_particleset(fieldset, settings, release_locations):
254259
PlasticParticle,
255260
lon=lons,
256261
lat=lats,
262+
depth=depths,
257263
plastic_diameter=plastic_diameters,
258264
plastic_density=plastic_densities,
259265
wind_coefficient=wind_coefficients,

plasticparcels/kernels.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,24 @@ def PolyTEOS10_bsq(particle, fieldset, time):
402402
Oceanic Technology, 20, 730-741.
403403
404404
"""
405-
Z = - particle.depth # note: use negative depths!
405+
Z = - math.fabs(particle.depth) # note: use negative depths!
406406
SA = fieldset.absolute_salinity[time, particle.depth, particle.lat, particle.lon]
407407
CT = fieldset.conservative_temperature[time, particle.depth, particle.lat, particle.lon]
408408

409409
SAu = 40. * 35.16504 / 35.
410410
CTu = 40.
411411
Zu = 1.0e+04
412412
deltaS = 32.
413+
414+
zz = -Z / Zu
415+
R00 = 4.6494977072e+01
416+
R01 = -5.2099962525e+00
417+
R02 = 2.2601900708e-01
418+
R03 = 6.4326772569e-02
419+
R04 = 1.5616995503e-02
420+
R05 = -1.7243708991e-03
421+
r0 = (((((R05 * zz + R04) * zz + R03) * zz + R02) * zz + R01) * zz + R00) * zz
422+
413423
R000 = 8.0189615746e+02
414424
R100 = 8.6672408165e+02
415425
R200 = -1.7864682637e+03
@@ -469,8 +479,9 @@ def PolyTEOS10_bsq(particle, fieldset, time):
469479
rz2 = (R022 * tt + R112 * ss + R012) * tt + (R202 * ss + R102) * ss + R002
470480
rz1 = (((R041 * tt + R131 * ss + R031) * tt + (R221 * ss + R121) * ss + R021) * tt + ((R311 * ss + R211) * ss + R111) * ss + R011) * tt + (((R401 * ss + R301) * ss + R201) * ss + R101) * ss + R001
471481
rz0 = (((((R060 * tt + R150 * ss + R050) * tt + (R240 * ss + R140) * ss + R040) * tt + ((R330 * ss + R230) * ss + R130) * ss + R030) * tt + (((R420 * ss + R320) * ss + R220) * ss + R120) * ss + R020) * tt + ((((R510 * ss + R410) * ss + R310) * ss + R210) * ss + R110) * ss + R010) * tt + (((((R600 * ss + R500) * ss + R400) * ss + R300) * ss + R200) * ss + R100) * ss + R000
472-
particle.seawater_density = ((rz3 * zz + rz2) * zz + rz1) * zz + rz0
473-
# particle.sw_surface_density = rz0
482+
r = ((rz3 * zz + rz2) * zz + rz1) * zz + rz0
483+
484+
particle.seawater_density = r0 + r
474485

475486

476487
def VerticalMixing(particle, fieldset, time):

tests/test_kernels.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,50 @@ def test_mixing():
243243

244244
# Assert that the particles move from their initial location
245245
assert (np.sum(np.abs(pset.lon - pset_mixing.lon)) > 0.) & (np.sum(np.abs(pset.lat - pset_mixing.lat)) > 0.)
246+
247+
@pytest.mark.parametrize("mode", ["scipy", "jit"])
248+
def test_TEOSdensity_kernels(mode):
249+
""" Adapted test from Parcels v3 codebase.
250+
"""
251+
settings_file = 'tests/test_data/test_settings.json'
252+
settings = pp.utils.load_settings(settings_file)
253+
254+
settings['simulation'] = make_standard_simulation_settings()
255+
settings['plastictype'] = make_standard_plastictype_settings()
256+
257+
# Turn off all other processes
258+
settings['use_3D'] = False
259+
settings['use_biofouling'] = False
260+
settings['use_stokes'] = False
261+
settings['use_wind'] = False
262+
settings['use_mixing'] = False
263+
264+
def generate_fieldset(xdim=2, ydim=2, zdim=2, tdim=1):
265+
lon = np.linspace(0.0, 10.0, xdim, dtype=np.float32)
266+
lat = np.linspace(0.0, 10.0, ydim, dtype=np.float32)
267+
depth = np.linspace(0, 2000, zdim, dtype=np.float32)
268+
time = np.zeros(tdim, dtype=np.float64)
269+
U = np.ones((tdim, zdim, ydim, xdim))
270+
V = np.ones((tdim, zdim, ydim, xdim))
271+
abs_salinity = 30 * np.ones((tdim, zdim, ydim, xdim))
272+
cons_temperature = 10 * np.ones((tdim, zdim, ydim, xdim))
273+
dimensions = {"lat": lat, "lon": lon, "depth": depth, "time": time}
274+
data = {
275+
"U": np.array(U, dtype=np.float32),
276+
"V": np.array(V, dtype=np.float32),
277+
"absolute_salinity": np.array(abs_salinity, dtype=np.float32),
278+
"conservative_temperature": np.array(cons_temperature, dtype=np.float32),
279+
}
280+
return (data, dimensions)
281+
282+
data, dimensions = generate_fieldset()
283+
fieldset = parcels.FieldSet.from_data(data, dimensions)
284+
285+
release_locations = {'lons': [5], 'lats': [5], 'depths': [1000],
286+
'plastic_amount': [1]}
287+
288+
pset = pp.constructors.create_particleset(fieldset, settings, release_locations)
289+
290+
pset.execute(pp.kernels.PolyTEOS10_bsq, runtime=1)
291+
292+
assert np.allclose(pset[0].seawater_density, 1027.45140)

0 commit comments

Comments
 (0)