Skip to content

Commit eb16b40

Browse files
Merge pull request #1558 from OceanParcels/error_when_dt_too_high_precision
Throwing an error when dt has too high precision
2 parents 6d53f60 + 2749bc8 commit eb16b40

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

docs/examples/example_globcurrent.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,6 @@ def test_globcurrent_time_extrapolation_error(mode, use_xarray):
173173
pset.execute(AdvectionRK4, runtime=delta(days=1), dt=delta(minutes=5))
174174

175175

176-
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
177-
@pytest.mark.parametrize('use_xarray', [True, False])
178-
def test_globcurrent_dt0(mode, use_xarray):
179-
fieldset = set_globcurrent_fieldset(use_xarray=use_xarray)
180-
pset = ParticleSet(fieldset, pclass=ptype[mode], lon=[25], lat=[-35])
181-
pset.execute(AdvectionRK4, dt=0.)
182-
183-
184176
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
185177
@pytest.mark.parametrize('dt', [-300, 300])
186178
@pytest.mark.parametrize('with_starttime', [True, False])
@@ -260,3 +252,22 @@ def test_globcurrent_pset_fromfile(mode, dt, pid_offset, tmpdir):
260252

261253
for var in ['lon', 'lat', 'depth', 'time', 'id']:
262254
assert np.allclose([getattr(p, var) for p in pset], [getattr(p, var) for p in pset_new])
255+
256+
257+
@pytest.mark.parametrize('mode', ['scipy', 'jit'])
258+
def test_error_outputdt_not_multiple_dt(mode, tmpdir):
259+
# Test that outputdt is a multiple of dt
260+
fieldset = set_globcurrent_fieldset()
261+
262+
filepath = tmpdir.join("pfile_error_outputdt_not_multiple_dt.zarr")
263+
264+
dt = 81.2584344538292 # number for which output writing fails
265+
266+
pset = ParticleSet(fieldset, pclass=ptype[mode], lon=[0], lat=[0])
267+
ofile = pset.ParticleFile(name=filepath, outputdt=delta(days=1))
268+
269+
def DoNothing(particle, fieldset, time):
270+
pass
271+
272+
with pytest.raises(ValueError):
273+
pset.execute(DoNothing, runtime=delta(days=10), dt=dt, output_file=ofile)

parcels/particleset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,10 @@ def execute(self, pyfunc=AdvectionRK4, pyfunc_inter=None, endtime=None, runtime=
878878
runtime = runtime.total_seconds()
879879
if isinstance(dt, delta):
880880
dt = dt.total_seconds()
881-
if dt > 0 and dt <= 1e-6:
881+
if abs(dt) <= 1e-6:
882882
raise ValueError('Time step dt is too small')
883+
if (dt * 1e6) % 1 != 0:
884+
raise ValueError('Output interval should not have finer precision than 1e-6 s')
883885
outputdt = output_file.outputdt if output_file else np.infty
884886
if isinstance(outputdt, delta):
885887
outputdt = outputdt.total_seconds()

tests/test_interaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def f(p):
169169

170170
@pytest.mark.parametrize('runtime, dt',
171171
[(1, 1e-2),
172-
(1, -2.1234e-3),
172+
(1, -2.123e-3),
173173
(1, -3.12452-3)])
174174
def test_pseudo_interaction(runtime, dt):
175175
# A linear field where advected particles are moving at

0 commit comments

Comments
 (0)