Skip to content

Changing dt inside a kernel does not work as expected #2765

Description

@erikvansebille

Parcels version

v4

Description

Changing the particles.dt inside a kernel does not carry over to the next iteration in the Kernel loop. This can be unexpected for users.

Note that this is also why AdvectionRK45() requires the particles.next_dt parameter.

I don't see an easy solution for this Issue - as we don't inspect Kernel functions anymore in v4 we can't use that point to throw a warning if a user changes particles.dt. Perhaps it can be added in the ParticleSetView getattr?

Code sample

# Paste your code within this block
def test_dt_modify_in_kernel(fieldset):
    TestParticle = Particle.add_variable(Variable("age", dtype=np.float32, initial=0))
    pset = ParticleSet(fieldset, pclass=TestParticle, x=[0.5], y=[0])

    def ModifyDt(particles, fieldset):  # pragma: no cover
        particles.age += particles.dt
        particles.dt = 2

    runtime = 10
    expected_age = 1 + 2 * (runtime - 2)  # 1 for the first step; 2 for the remaining steps (except last)
    pset.execute(ModifyDt, runtime=runtime, dt=1.0)
    np.testing.assert_allclose(pset.t[0], runtime)
    np.testing.assert_allclose(pset.age[0], expected_age)
# Paste your error message within this block
E       AssertionError: 
E       Not equal to tolerance rtol=1e-07, atol=0
E       
E       Mismatched elements: 1 / 1 (100%)
E       Max absolute difference among violations: 12.
E       Max relative difference among violations: 0.70588235
E        ACTUAL: array(5., dtype=float32)
E        DESIRED: array(17)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions