Modeling a leaking tank

Hello !

I’m currently working on modeling a system that resembles an open-topped tank with a leak, where the tank is also being supplied, so if the leak isn’t sufficient, it overflows.

The system’s equations (reasonably simple) look something like this:

V(t)' = input(t) - output(t)


output(t) = \begin{cases} cste,& \text{if } 0 < V(t) < 1\\ cste + input(t),& \text{if } V(t) \geq 1\\ 0, & \text{if } V(t) \geq 1 \end{cases}

with the three cases: filling, overflowing, and empty.

I’ve written a small code using DifferentialEquations.jl to solve it, but the outputs aren’t nice, not the step function I believe it should be.
Specifically, I’m interested in the output(t) function.

Simple reproducible code
input(t) = 0.5 + 0.25*sin(π*t)

function output(t, V, base_output, input)
    if 0.0 < V < 1.0
        return base_output
    elseif V >= 1.0
        return base_output .+ input(t)
        return 0.0

function dVdt(V, p, t)
    input = p[1]
    base_output = p[2]

    input(t) - output(t, V, base_output, input)

base_output = 0.45
p = (input, base_output)
V₀ = 0.5

tspan = (0.0, 25.0)
prob = ODEProblem(dVdt, V₀, tspan, p)
sol = solve(prob, saveat = 0.001)
plot(sol.t, sol.u, label="vol", ylims=(-0.1, 1.1))
out = output.(sol.t, sol.u, base_output, input)
plot!(sol.t, out, label="output", c=:black)
plot!(sol.t, input.(sol.t), label="input")


Do any of you have a solution for this? I’d greatly appreciate your help!

Thank you!

Hello! :wave:

A few small comments

  • I think you have some problem in your conditions for the output in the post (not in the code)
  • Tanks with outlets often follow Torricelli’s law, is this relevant for your tank?
  • The fact that output looks bad might be related to the dynamics being discontinuous. Try making use of a continuous callback indicating where the discontinuities in V are

In your model you assume that the outflow is constant, independently of the volume, but it is not quite realistic/physical, is it? The pressure with which the water is pushed out of the tank grows with the water level and so does the (out)flow rate (see Torricelli’s law).

in theory there is no difference between theory and practice, while in practice there is

(ascribed to Brewster, known for Brewster angle)

If the problem of the OP is a school assignment, then it should be solved according to given conditions, whether physically sensible or not.

If it is about a real engineering system, then we know yet nothing about it.

As for Torricelli’s law - as somebody who dealt quite a lot with real life fluids and with leakages, small and large, I’d maintain that (almost) no real-life tank follows Torricelli’s law, and that approximately constant outflow can be a realistic case :slightly_smiling_face:

Which solver are you using?

I use something like:

    tspan = (0.0, duration)
    dt = 1e-3
    ts    = 0:dt:duration

    # run the simulation
    prob = ODEProblem(sys, u0, tspan, p)
    sol = solve(prob, Rodas5(), dt=dt, abstol=tol, dtmax=1, reltol = tol, saveat=ts)

For discontinous problems the choice of the solver and low values for abstol and reltol are important.

I am not using any physical law at the moment because I am just exploring possible behaviors for my system (whose physical equations are unknown).

Anyway, I found a fix using low tols and removing the discrete aspect by using a very steep logistic function.

Thank you for your help :slight_smile:

1 Like