Metadata-Version: 2.4
Name: nonlineum-theo
Version: 1.0.0rc3
Summary: Python client for the Theo wastewater treatment plant simulator engine
Author: Nonlineum Labs
License-Expression: MIT
Project-URL: Homepage, https://nonlineum.com/
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering
Classifier: Framework :: Jupyter
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: nonlineum/theo/LICENSE
Requires-Dist: websockets
Provides-Extra: notebook
Requires-Dist: ipython; extra == "notebook"
Requires-Dist: ipywidgets; extra == "notebook"
Requires-Dist: nest_asyncio; extra == "notebook"
Requires-Dist: pandas; extra == "notebook"
Requires-Dist: matplotlib; extra == "notebook"
Dynamic: license-file

# nonlineum-theo

The Python client for **Theo**, the wastewater treatment plant simulator by
[Nonlineum Labs](https://nonlineum.com/). From a script or a Jupyter
notebook you can:

- build a plant;
- set its parameters;
- run dynamic simulations;
- read the results.

This package is a client only. It drives a running Theo engine over Theo's
WebSocket API, the same API the Theo desktop application uses. The engine
comes with the Theo application, which is licensed separately.

## Requirements

- Python 3.10 or newer.
- A Theo engine to connect to. Usually that is the one the Theo application
  starts, but it can also be an engine on another machine.

## Installation

```bash
pip install --pre nonlineum-theo
```

`--pre` is needed while the current release is a release candidate. For
notebooks, the `notebook` extra adds the live message panel, tables and
plots:

```bash
pip install --pre "nonlineum-theo[notebook]"
```

## Connecting

`import nonlineum.theo` connects to an engine straight away. It looks for
one in this order:

1. the address in the `THEO_WS` environment variable, e.g.
   `ws://127.0.0.1:19140/theo`;
2. the engines registered in `~/.theo/engines/`:
   - first the session named by `THEO_SESSION`;
   - then the Theo window you last brought to the front;
   - then the newest engine;
3. `ws://127.0.0.1:19140/theo`.

If no engine is running yet, the import still succeeds and the next command
connects.

## A first plant

```python
import nonlineum.theo as theo

plant = theo.plant

plant.add_influent("Influent")
plant.add_cstr("Tank")
plant.add_effluent("Effluent")
plant.connect("Influent.outlet", "Tank.inlet")
plant.connect("Tank.outlet", "Effluent.inlet")

plant.Influent.Q = 1_000     # m³/d
plant.Tank.V = 2_000         # m³

plant.set_stoptime(10)       # d
plant.start()
```

How values work:

- **Reading.** Every unit is an attribute of `plant`, and so is every
  variable of a unit: `plant.Tank.COD`, `plant.Tank.XTSS`, `plant.Tank.pH`.
  In a notebook the client pulls the plant state when a run finishes.
  `plant.pull()` refreshes it at any time.
- **Writing.** Assigning to a parameter sends it to the engine. A value the
  engine refuses, for example one outside the parameter's range, is printed
  with the engine's reason and not kept.

Units:

- **Named helpers.** `add_clarifier`, `add_primary`, `add_splitter`,
  `add_combiner`, `add_anaerobic_digester`, `add_thickener`,
  `add_dewatering`, `add_oxidation_ditch` and `add_plug_flow_tank` add the
  common unit types.
- **Any other type.** `plant.add_unit(type, name)` adds any unit type the
  engine offers. The type names are in `UNIT_TYPES`
  (`from nonlineum.theo.plant import UNIT_TYPES`).

## Scripts

To work on a plant built in the Theo application, open its project file,
run it and read the results:

```python
import time
import nonlineum.theo as theo

codes = []
theo.on_message(lambda text, level, category, code: codes.append(code))

theo.open_project("/path/to/plant.theo")   # a path on the engine's machine
theo.plant.set_stoptime(30)                # d
theo.plant.start()

while "MESSAGE_SIMULATION_FINISHED" not in codes:
    if "ERROR_SIMULATION_TERMINATED_ABNORMALLY" in codes:
        raise RuntimeError("the simulation aborted")
    theo.client.send_sync_command("progress")
    time.sleep(1)

theo.plant.pull()
print(theo.plant.Effluent.COD)
```

Why the loop sends `progress`: outside a notebook, the client takes in the
engine's messages only while a command is in flight, so the poll is what
lets the finish message arrive.

Two more helpers:

- `theo.save_project(path)` writes the session back to a `.theo` file.
- `theo.plant.history(units, variables)` returns the recorded time series
  as a pandas DataFrame (pandas comes with the `notebook` extra).

## Notes

- **Units.** Values are in the engine's units: flows in m³/d, volumes in
  m³, times in days, concentrations in g/m³ (COD in gCOD/m³).
- **Values that are not numbers.** A value that is not a finite number
  comes as the string `"NaN"`, `"Infinite"` or `"-Infinite"`. For example,
  the hydraulic residence time (`HRT`) of a tank with no inflow is
  `"Infinite"`.
  Check the type before computing with a value.
- **Version.** `nonlineum.theo.__version__` is the package version.

## License

The `nonlineum-theo` package is released under the MIT License; the full
text is in the `LICENSE` file that ships with it. The Theo engine and the
Theo desktop application are not covered by it; they are licensed
separately.
