# "Scenario: a flooded cellar"

# Scenario: a flooded cellar

A pipe has burst in a cellar. A sump pump on the cellar's own circuit is fighting it, but the pump is not strong enough and the water rises. At 30 cm it reaches the sockets. The circuit shorts, the breaker trips, and the pump, which depended on that circuit, stops. From then on nothing opposes the flood.

This is how two sensible systems fail together. It takes exactly one rule to join them: *a socket below the water level is a short.*

## The chain

| Stage | Capability | Object |
|---|---|---|
| Burst main into the cellar | liquids (skill `ferret`) | a link with a `main` supply |
| The cellar | liquids | a `Reservoir`; the level follows the volume |
| Sump pump | liquids and electricity | a link with a `pump`, whose `valve` is open while the breaker holds |
| The circuit | [electricity](../reference/electric.md) | `VSource`, `Breaker`, a `Switch` standing for the socket under water, a `Resistor` for the pump |
| The joining rule | your code | `socket.closed = cellar.level > 0.3` |

## Building it

```ts
const net = new LiquidNetwork(new LiquidLibrary(EXAMPLE_LIQUIDS), 9.80665);
const cellar = net.addReservoir({ id: 'cellar', liquid: 'water', container: boxContainer(4, 4, 3), level: 0 });
net.addLink({ id: 'burst', from: 'air', to: 'cellar', diameter: 0.04, length: 1,
              main: { liquid: 'water', pressure: 101325 + 150000 } });
const sump = net.addLink({ id: 'sump', from: 'cellar', to: 'air', toElevation: 1, diameter: 0.03, length: 2,
                           pump: { pressure: 60000 } });

const wiring = new Circuit();
wiring.add(new VSource('V', 'a', '0', 230));
const breaker = wiring.add(new Breaker('B', 'a', 'b', 16, 10, 8));
wiring.add(new Resistor('Pump', 'b', '0', 230 * 230 / 400));        // a 400 W pump
const socket = wiring.add(new Switch('Socket', 'b', '0', false));    // closes when the water is up to it
```

Each step the rule connects the two worlds:

```ts
if (cellar.level > 0.3 && !socket.closed) { socket.closed = true; wiring.restart(); }
wiring.step(dt);
sump.valve = breaker.tripped ? 0 : 1;      // the pump runs while the breaker holds
net.step(dt);
```

## What happens

With the pump running, the cellar rises by about 5 cm a minute: the pump takes part of the inflow. When the water reaches the socket the short trips the breaker at once (the magnetic element), the pump stops, and the cellar then rises about 6.5 cm a minute, the full flow of the burst over the floor area. The flood ends above the socket. The pump in this scenario is a modest one on purpose: the point is the order of events, not the margin.

## What the capabilities said to each other

- **Liquids to electricity.** One number crosses: the cellar's level. Your rule turns it into the state of a switch. `wiring.restart()` makes the next step a backward-Euler one, so that closing a switch does not ring.
- **Electricity to liquids.** One number crosses back: the breaker's state decides the sump valve. The valve stands for whether the pump has power; its `pump.pressure` is the head it adds.
- **Time scales.** The circuit has a time constant of milliseconds and the flood takes minutes. Stepping both at 0.5 s is fine here because the circuit is quasi-static: the breaker's thermal element takes about 10 s and the short's magnetic trip is instant. For fast electrical transients, sub-step the circuit; the electric capability does this itself with `maxStep`.

## Variations

| Change | Effect |
|---|---|
| A bigger pump (`pressure`, `diameter`) | the pump outruns the leak and the water never reaches the socket |
| Raise the socket to 1.2 m | the flood needs more time and the pump gets longer to work |
| A float switch that cuts the supply at 0.25 m | the breaker never trips, and neither does the pump, if it shares the circuit |
| A second, battery-backed pump on its own circuit (`Battery`) | the water is held where the two flows meet |
| An `arcFlash` link on `circuit:short` | the short becomes a small blast: a cellar with gas has a worse day |

The complete run, with the numbers it must produce, is in `tests/unit/examples/physics-scenarios-flooded-cellar.test.ts`.
