Repository navigation
Add example of limiting particle timestep by local cfl in schism tuto… - #3016
fluidnumericsJoe wants to merge 11 commits into
Conversation
for more information, see https://pre-commit.ci
erikvansebille
left a comment
There was a problem hiding this comment.
Thanks for adding this extra context, @fluidnumericsJoe! Below a few small comments (I can't put comments directly on the notebook in GitHub)
- Is "altitude" the right word in the comment of cell [5]? That to me suggests (3D) height?
- Avoid variable names like
h,eandpin that same cell - make them more explicit so users understand what's going on? - CFL number hasn't been introduced in cell [5]. Could you move that whole cell down to the section of cell [9]? That will also make it easier to refer to this part of the tutorial directly, because all information is in one subsection
- Make explicit that the math symbol
$C$ is the same asCFL_target - Should we add a link to the CFL-section on the
tutorial_dt_integratorstoo?
|
erikvansebille
left a comment
There was a problem hiding this comment.
Thanks for making these updates, however I don't see the link back from the dt_integration notebook to this notebook?
It's in the section titled "Limit the timestep by the local CFL number" under the text "See a priori estimation of dt for the same reasoning applied to a structured grid." It's also in the last cell titled "Adapting this tutorial to your own SCHISM setup" under the text " choosing a timestep and integration method" |
|
Sorry, I meant the other way around. So a link from the https://docs.parcels-code.org/en/latest/user_guide/examples/tutorial_dt_integrators.html notebook to this new notebook |
I have now added such a link in e88f57d - as I was working on merging your code with the new |
|
I now realise that the def CFLLimitedDt(particles, fieldset):
"""Shrink each particle's dt so it moves at most CFL_target cell widths per step."""
cell_width = fieldset.cell_width[particles]
u, v = fieldset.UV[particles]
dt_cfl = (
fieldset.CFL_target * cell_width / np.maximum(np.sqrt(u * u + v * v), 1e-12)
)
particles.dt = np.sign(particles.dt) * np.minimum(np.abs(particles.dt), dt_cfl)Should the @fluidnumericsJoe, any thoughts? |
|
I got the "old" behaviour back by using the diff --git a/docs/user_guide/convert_schism.ipynb b/docs/user_guide/convert_schism.ipynb
index 5692fb20..661a7c7a 100644
--- a/docs/user_guide/convert_schism.ipynb
+++ b/docs/user_guide/convert_schism.ipynb
@@ -271,7 +271,10 @@
"SchismParticle = parcels.Particle.add_variable(\n",
- " parcels.Variable(\"out_of_bounds\", dtype=np.int32, initial=0)\n",
+ " [\n",
+ " parcels.Variable(\"out_of_bounds\", dtype=np.int32, initial=0),\n",
+ " parcels.Variable(\"next_dt\", dtype=np.float32, initial=0),\n",
+ " ]\n",
")\n",
@@ -363,6 +366,7 @@
" dt_cfl = (\n",
" fieldset.CFL_target * cell_width / np.maximum(np.sqrt(u * u + v * v), 1e-12)\n",
" )\n",
+ " particles.next_dt = particles.dt.copy()\n",
" particles.dt = np.sign(particles.dt) * np.minimum(np.abs(particles.dt), dt_cfl)\n",
diff --git a/src/parcels/_core/kernel.py b/src/parcels/_core/kernel.py
index 5d216749..ebd7c6e2 100644
--- a/src/parcels/_core/kernel.py
+++ b/src/parcels/_core/kernel.py
@@ -127,8 +127,8 @@ class Kernel:
- if hasattr(self.fieldset, "RK45_tol"):
- # Update dt in case it's increased in RK45 kernel
+ if "next_dt" in particles._data:
+ # Update dt in case it's set by the kernel
particles.dt = particles.next_dtThis is of course not ideal, as it requires using the |
Since #3019, particles keep their own dt between steps. The SCHISM tutorial's CFLLimitedDt kernel set dt = min(previous dt, dt_cfl), so dt could only shrink: once a particle passed through a small, fast cell it kept that tiny dt for the rest of the execute() call. This made the tutorial much slower. Setting dt = dt_cfl directly instead failed with OutsideTimeInterval. Kernel.execute clips dt to the next output/end time before the kernels run and again before the position update, but not in between. A kernel that increases dt therefore hands the unclipped value to the next kernel, and AdvectionRK4 samples the fields past endtime (past the end of the dataset on the last output interval, or on the first step when dt_cfl exceeds the dataset length). Clip dt again after each kernel function, so every kernel sees a dt that stops at endtime. Advection kernels then compute the displacement with the same dt that _position_update adds to t. Clipped values are restored after the step as before. A regression test runs a kernel that sets dt past endtime ahead of AdvectionRK4 to the end of a dataset, with and without intermediate output times. In the tutorial, CFLLimitedDt now sets dt = min(dt_max, dt_cfl), with dt_max = 60 s as a fieldset context value, so dt shrinks in small cells and fast flow and grows again elsewhere. The dt passed to execute() only gives the direction of integration. The cap is needed because the CFL estimate uses the speed at the start of the step: without it, 77 particles were removed instead of the 43 removed by a small-step reference run (CFL_target = 0.25, dt <= 60 s). With the 60 s cap the removals and final positions match that reference, and notebook execution time drops from ~243 s to ~50 s.
Description
This PR adds an example of limiting particle timestep by local cfl in schism tutorial
Checklist
mainfor normal development,v3-supportfor v3 support)