Skip to content

Commit 88eb065

Browse files
Adding support and test for RK45 advection
1 parent a9be1f5 commit 88eb065

4 files changed

Lines changed: 15 additions & 13 deletions

File tree

parcels/application_kernels/advection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def AdvectionRK45(particle, fieldset, time): # pragma: no cover
113113
Time-step dt is halved if error is larger than fieldset.RK45_tol,
114114
and doubled if error is smaller than 1/10th of tolerance.
115115
"""
116-
dt = min(particle.next_dt, fieldset.RK45_max_dt) / np.timedelta64(1, "s") # noqa TODO improve API for converting dt to seconds
116+
dt = min(particle.next_dt / np.timedelta64(1, "s"), fieldset.RK45_max_dt) # noqa TODO improve API for converting dt to seconds
117117
c = [1.0 / 4.0, 3.0 / 8.0, 12.0 / 13.0, 1.0, 1.0 / 2.0]
118118
A = [
119119
[1.0 / 4.0, 0.0, 0.0, 0.0, 0.0],

parcels/kernel.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,12 @@ def evaluate_particle(self, p, endtime):
384384
return p
385385

386386
pre_dt = p.dt
387-
# TODO implement below later again
388-
# try: # Use next_dt from AdvectionRK45 if it is set
389-
# if abs(endtime - p.time_nextloop) < abs(p.next_dt) - 1e-6:
390-
# p.next_dt = abs(endtime - p.time_nextloop) * sign_dt
391-
# except AttributeError:
392-
if sign_dt * (endtime - p.time_nextloop) <= p.dt:
393-
p.dt = sign_dt * (endtime - p.time_nextloop)
387+
try: # Use next_dt from AdvectionRK45 if it is set
388+
if abs(endtime - p.time_nextloop) < abs(p.next_dt) - np.timedelta64(1000, "ns"):
389+
p.next_dt = sign_dt * (endtime - p.time_nextloop)
390+
except KeyError:
391+
if sign_dt * (endtime - p.time_nextloop) <= p.dt:
392+
p.dt = sign_dt * (endtime - p.time_nextloop)
394393
res = self._pyfunc(p, self._fieldset, p.time_nextloop)
395394

396395
if res is None:

parcels/particleset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __init__(
156156
if isinstance(v.initial, attrgetter):
157157
initial = v.initial(self)
158158
else:
159-
initial = v.initial * np.ones(len(trajectory_ids), dtype=v.dtype)
159+
initial = [np.array(v.initial, dtype=v.dtype)] * len(trajectory_ids)
160160
self._data[v.name] = initial
161161

162162
# update initial values provided on ParticleSet creation

tests/v4/test_advection.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
from parcels._datasets.structured.generic import simple_UV_dataset
5-
from parcels.application_kernels import AdvectionEE, AdvectionRK4, AdvectionRK4_3D
5+
from parcels.application_kernels import AdvectionEE, AdvectionRK4, AdvectionRK4_3D, AdvectionRK45
66
from parcels.field import Field, VectorField
77
from parcels.fieldset import FieldSet
88
from parcels.particle import Particle, Variable
@@ -101,7 +101,7 @@ def TriLinear( # TODO move to interpolation file
101101
"EE": AdvectionEE,
102102
"RK4": AdvectionRK4,
103103
"RK4_3D": AdvectionRK4_3D,
104-
# "RK45": AdvectionRK45,
104+
"RK45": AdvectionRK45,
105105
# "AA": AdvectionAnalytical,
106106
# "AdvDiffEM": AdvectionDiffusionEM,
107107
# "AdvDiffM1": AdvectionDiffusionM1,
@@ -222,7 +222,7 @@ def SubmergeParticle(particle, fieldset, time): # pragma: no cover
222222
# ("AdvDiffM1", 1e-2),
223223
("RK4", 1e-5),
224224
("RK4_3D", 1e-5),
225-
# ("RK45", 1e-5),
225+
("RK45", 1e-5),
226226
],
227227
)
228228
def test_moving_eddy(method, rtol):
@@ -258,7 +258,10 @@ def truth_moving(x_0, y_0, t):
258258
fieldset = FieldSet([U, V, UV])
259259
start_depth = 0
260260

261-
RK45Particles = Particle.add_variable(Variable("next_dt", initial=dt))
261+
if method == "RK45":
262+
# Use RK45Particles to set next_dt
263+
RK45Particles = Particle.add_variable(Variable("next_dt", initial=dt, dtype=np.timedelta64))
264+
fieldset.add_constant("RK45_tol", 1e-6)
262265

263266
pclass = RK45Particles if method == "RK45" else Particle
264267
pset = ParticleSet(

0 commit comments

Comments
 (0)