Metadata-Version: 2.4
Name: floating-dynamics
Version: 0.1.0
Summary: Two-dimensional dynamics of floating bodies, including the free-surface effect of liquid cargo.
Author: Christian Hill
License-Expression: MIT
Project-URL: Homepage, https://github.com/xnx/floating-dynamics
Project-URL: Issues, https://github.com/xnx/floating-dynamics/issues
Keywords: ship stability,free-surface effect,metacentric height,buoyancy,simulation
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Provides-Extra: animation
Requires-Dist: matplotlib; extra == "animation"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Dynamic: license-file

# floating-dynamics

Two-dimensional dynamics of ships floating in the sea (cross-sections, per
metre of length), including the *free-surface effect* of liquid cargo.

The demonstration compares three identical coasters carrying the same mass of
cargo: as a solid, as a liquid free to move across the whole hold, and as a
liquid divided into tanks. Each is heeled slightly and released. The liquid's
free surface reduces the ship's metacentric height, GM, so the ship with the
undivided hold capsizes, while dividing the hold into n tanks reduces the
effect by a factor of n².

With the default cargo, 2.5 m of water (30 t per metre of the ship's
length), heeled by 5°:

| Cargo              | GM      | Roll period | Outcome            |
|--------------------|---------|-------------|--------------------|
| Solid              | 1.00 m  | 13.2 s      | rolls, and settles |
| Liquid             | −0.50 m | –           | capsizes in ~12 s  |
| Liquid, two tanks  | 0.63 m  | 16.5 s      | rolls, and settles |

## Trying it out

Install it with the animation extra (which adds Matplotlib), and run the
demonstration:

    pip install 'floating-dynamics[animation]'
    free-surface

or, with [uv](https://docs.astral.sh/uv/), without installing anything:

    uvx --from 'floating-dynamics[animation]' free-surface

Options choose which ships to show, and set the initial heel, the cargo's
density and depth and the number of tanks, or save the animation instead of
showing it:

    free-surface --cargo free                      # one ship
    free-surface --cargo solid --cargo divided     # two of them
    free-surface --heel 10 --depth 2 --tanks 3
    free-surface --density 13.6                    # mercury
    free-surface free-surface.mp4                  # save the animation
    free-surface --help

`python -m floating_dynamics` does the same as `free-surface`.

### Denser cargoes

`--density` sets the cargo's density in t/m³. Unless `--depth` is also
given, the cargo's mass stays the same (2.5 m of water would weigh 13.6 times
as much as mercury, and sink the ship), so its depth is reduced instead. The
free-surface effect depends on the width of the liquid's surface but not its
depth, so for the same mass of cargo it grows in proportion to the density:

| Cargo (30 t/m)     | Water: GM | Outcome           | Mercury: GM | Outcome           |
|--------------------|-----------|-------------------|-------------|-------------------|
| Solid              | 1.00 m    | stable            | 1.37 m      | stable            |
| Liquid             | −0.50 m   | capsizes in ~12 s | −19.0 m     | capsizes in ~4.5 s |
| Liquid, two tanks  | 0.63 m    | stable            | −3.7 m      | lolls at about 24° |

The mercury in two tanks makes GM negative, but the ship does not capsize: it
heels until its wider waterline makes up the difference, and settles at an
*angle of loll*. A layer of mercury only 0.18 m deep sloshes slowly, though,
so these timings are less reliable than water's (see Assumptions).

## Using it from Python

`floating_dynamics.scenario` sets the parameters, runs the simulations and
returns the results as plain data (lists, dicts and floats), for programs
which draw the ships themselves, such as a web page running
[Pyodide](https://pyodide.org):

```python
from floating_dynamics.scenario import Scenario, run

scenario = Scenario.from_dict({'heel': 10, 'cargoes': ['free', 'divided'],
                               'design': {'cargo_depth': 2, 'tanks': 3}})
result = run(scenario)
for ship in result.ships:
    print(ship.ship.title, ship.summary())
data = result.to_dict()        # JSON-serialisable
```

The main parameters, with their defaults, are:

| Parameter              | Default                       | Meaning                                       |
|------------------------|-------------------------------|-----------------------------------------------|
| `cargoes`              | `['solid', 'free', 'divided']`| which ships to simulate                       |
| `heel`                 | 5                             | initial heel (degrees)                        |
| `duration`, `dt`       | 60, 0.1                       | length of the simulation, time per frame (s)  |
| `design.cargo_depth`   | 2.5                           | depth of cargo when upright (m)               |
| `design.cargo_density` | 1.0                           | density of cargo (t/m³)                       |
| `design.tanks`         | 2                             | number of tanks for the `'divided'` cargo     |

Unlike `free-surface --density`, setting `cargo_density` here does not change
the depth: set `cargo_depth` too to keep the mass the same. The ship's
dimensions, lightship mass and hydrodynamic coefficients are also fields of
`CoasterDesign` (in `floating_dynamics.ships`), with comments. Unknown or
unphysical parameters raise `ValueError`. The format of the results is
described in `scenario.py`. Only NumPy and SciPy are needed for this.

## Modules

- `geometry`: polygon area, centroid, second moment, clipping and filling.
- `body`: `FloatingBody` (hydrostatics, equations of motion, righting lever,
  metacentric height) and `Compartment` (a tank of liquid).
- `ships`: a coaster's midship section and its three ways of carrying cargo.
- `simulation`: integrating the motion with SciPy's `solve_ivp`.
- `scenario`: the parameters and results, as plain data.
- `drawing`: animating ships side by side, with their heel against time.
- `free_surface`: the command-line demonstration.

## Assumptions

- The motion is two-dimensional: a cross-section of a long ship, rolling and
  heaving but not pitching.
- The liquid's surface stays level (quasi-static). This is reasonable while
  the liquid sloshes much faster than the ship rolls; the demonstration
  prints both periods. Shallow layers slosh slowly (the 0.18 m of mercury
  above takes 18 s across the whole hold), and then the real liquid would lag
  behind the roll, so the motion would develop more slowly. Whether the ship
  is stable depends only on GM, which is unaffected.
- The hull is watertight up to its main deck: there is no flooding, so a
  capsized ship floats upside down.
- The surrounding water is represented by added mass and linear damping.

## Development

    git clone https://github.com/xnx/floating-dynamics
    cd floating-dynamics
    pip install -e '.[animation,test]'
    pytest

The editable install (`-e`) uses the source files directly, so changes take
effect without reinstalling. To run the demonstration without installing at
all, from the project folder:

    PYTHONPATH=src python -m floating_dynamics

## Licence

MIT: see `LICENSE`.
