Adding Logic to Equations
In our example, we created a very simple model telling us how many seconds of water we have stored in our tank.
Handling Divide-By-Zero
If you're doing anything with rates and speeds, you'll likely come up across a divide-by-zero error at some point.
These can cascade and cause chaos with your model, so it's always a good idea to handle these.
Luckily, Python allows you to use in-line conditions, so you can put logic into your functions.
We can change the code….
lambda: Volume.num() / Outflow.num(), [Outflow,Volume]
to something that returns 86400 (a full day in seconds) when there's no flow out of the tank.
lambda: 86400 if Outflow.num() == 0 else Volume.num() / Outflow.num()
An 'easy' way of avoiding divide-by-zero is by wrapping the variable in max(value,0.1), which ensures that the value will always greater than 0.
However, in some cases this can be dangerous - certain equipment (such as pumps) can be run in reverse, where they generate negative values. Using max would prevent these negative values from being used in your calculations.
How about we Connecting a Model to Data?