Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Average flow states #1906

Closed
SouthEndMusic opened this issue Oct 16, 2024 · 5 comments
Closed

Average flow states #1906

SouthEndMusic opened this issue Oct 16, 2024 · 5 comments
Labels
core Issues related to the computational core in Julia performance Relates to runtime performance or convergence

Comments

@SouthEndMusic
Copy link
Collaborator

Now we define a state like this:

$$ u = \int_{t_0}^t q\text{d}\tilde{t} $$

which is increasing with time for most node types. So what if instead we define states like this:

$$ u = \frac{1}{t - t_0 + 1} \int_{t_0}^t q\text{d}\tilde{t} $$

which is almost the same as the average of the flow rate since the start of the simulation. The $+1$ in the denominator is just there to avoid division problems at the start of the simulation.

The differential equation for a state is then, using that the cumulative flow is given by $(t - t_0 + 1)u$,

$$ \left[(t - t_0 + 1)u\right]' = q, $$

or in $u'= f(u,p,t)$ form

$$ u'= \frac{q - u}{t - t_0 + 1}. $$

This might introduce different accuracy problems, but it's worth an experiment. This would be a fix of #1897, in combination with #1904.

@SouthEndMusic SouthEndMusic added core Issues related to the computational core in Julia performance Relates to runtime performance or convergence labels Oct 16, 2024
@github-project-automation github-project-automation bot moved this to To do in Ribasim Oct 16, 2024
@SouthEndMusic
Copy link
Collaborator Author

In this formulation, the total flow over an edge over the latest timestep is given by

$$ (t_\text{prev} + \Delta t - t_0 + 1)u - (t_\text{prev} - t_0 + 1)u_\text{prev} = (t_\text{prev} - t_0 + 1)(u - u_\text{prev}) + u\Delta t $$

@SouthEndMusic SouthEndMusic moved this from To do to 🏗 In progress in Ribasim Oct 16, 2024
@SouthEndMusic
Copy link
Collaborator Author

When a node has a flow between $q_\min$ and $q_\max$ during a timestep, the cumulative flow $(t - t_0 + 1)u$ is bounded as

$$ u_\text{prev}(t_\text{prev} - t_0 + 1) + q_\min \Delta t \le (t - t_0 + 1)u \le u_\text{prev}(t_\text{prev} - t_0 + 1) + q_\max \Delta t $$

and so

$$ u_\text{prev}\frac{t_\text{prev} - t_0 + 1}{t - t_0 + 1} + q_\min \frac{\Delta t}{t - t_0 + 1} \le u \le u_\text{prev}\frac{t_\text{prev} - t_0 + 1}{t - t_0 + 1} + q_\max \frac{\Delta t}{t - t_0 + 1} $$

@evetion
Copy link
Member

evetion commented Oct 17, 2024

Github mobile has no math support, so this issue was completely unreadable. On desktop though, this seems like an interesting experiment. It's part of the normalization approach, but nice thought to be doing so over time instead of relating it to total flow or similar.

My main concerns are 1, we should improve our benchmarks suite first, otherwise these investigations are not that efficient. 2 would be readability, but I assume we can hide the $(t - t_0 + 1)u$ in our code a bit.

@visr
Copy link
Member

visr commented Oct 17, 2024

Seems like a good idea to try out. I guess we'd be able to keep the cumulative BMI variables around and just calculate them on the spot.

@SouthEndMusic
Copy link
Collaborator Author

SouthEndMusic commented Oct 19, 2024

A better formulation of the states might be

$$ u = \frac{1}{t + \tau}\left(\tau q|_{t=0} + \int_0^t q \text{d} \tilde{t}\right), $$

where $\tau$ is some length of time which we can hardcode at e.g. 1 second, and using that always $t_0 = 0$ in the core. This formulation has the nice properties that:

  1. Division by zero is prevented
  2. It doesn't introduce as much non-linear behavior at the start of the simulation as the formulation above
  3. If a flow is constant over the simulation, this state will always be equal to that constant flow

What it essentially computes is the average flow as if there was a period of length $\tau$ before the start of the simulation with constant flow $q|_{t=0}$ (the flow at $t = 0$).

Which gives the DE

$$ \left[(t+\tau)u - \tau q|_{t=0}\right]' = q \quad\Leftrightarrow\quad u' = \frac{q - u}{t + \tau}. $$

In this formulation, the total flow over an edge over the latest timestep is given by

$$ \left[(t_\text{prev} + \Delta t +\tau)u - \tau q|_{t=0}\right] - \left[(t_\text{prev}+\tau)u_\text{prev} - \tau q|_{t=0}\right] = u\Delta t + (t_\text{prev}+\tau)(u - u_\text{prev}). $$

Bounding the cumulative flow yields

$$ (t_\text{prev}+\tau)u_\text{prev} - \tau q|_{t=0} + q_\min\Delta t \le (t+\tau)u - \tau q|_{t=0} \le (t_\text{prev}+\tau)u_\text{prev} - \tau q|_{t=0} + q_\max\Delta t, $$

and so

$$ \frac{t_\text{prev}+\tau}{t+\tau}u_\text{prev} + q_\min\frac{\Delta t}{t+\tau} \le u \le \frac{t_\text{prev}+\tau}{t+\tau}u_\text{prev} + q_\max\frac{\Delta t}{t+\tau}. $$

@SouthEndMusic SouthEndMusic closed this as not planned Won't fix, can't repro, duplicate, stale Oct 21, 2024
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Ribasim Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Issues related to the computational core in Julia performance Relates to runtime performance or convergence
Projects
Status: Done
Development

No branches or pull requests

3 participants