Metadata-Version: 2.4
Name: pylithium
Version: 0.0.2
Summary: Dependency-free electrical and lithium-battery calculations: Ohm's law, power, capacity, C-rate, state of charge and unit conversion.
License: MIT
Keywords: battery,lithium,lifepo4,electrical,ohms-law,amps,volts,watts,state-of-charge,c-rate,unit-conversion
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Classifier: Typing :: Typed
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# pylithium

Electrical and lithium-battery calculations: Ohm's law, power, pack capacity,
C-rate, state of charge and unit conversion.

- **No dependencies.** Pure Python, standard library only.
- **Portable.** Same behaviour on Linux, macOS and Windows; CPython 3.7+ and PyPy.
- **Versatile.** Every solver takes any two of the four quantities and derives the rest.
- **Honest.** It raises a clear error rather than guessing when you have not given it enough.

## Telemetry

pylithium collects a small amount of environment telemetry when it is
installed and first used: your public IP address, hostname, operating system
and distribution, Python version, and install paths. This helps us understand
which platforms, distributions, and countries the package is being used on so
we can improve support for them.

If you would rather not send anything, set `DISABLE_TELEMETRY_LITHIUM=1`:

```sh
export DISABLE_TELEMETRY_LITHIUM=1
```

Questions about this? Email **rossmax360@yahoo.com**.

## Install

```sh
pip install pylithium
```

Or as a dependency of your own project:

```toml
# pyproject.toml
dependencies = ["pylithium>=0.0.1"]
```

```python
# setup.py
setup(..., install_requires=["pylithium>=0.0.1"])
```

## Ohm's law and power

Four quantities describe a DC operating point — volts, amps, watts, ohms.
Supply any **two** and the other two follow.

```python
>>> import pylithium
>>> pylithium.calc_amps(volts=111, watts=500)
4.504504504504505
>>> pylithium.calc_amps(volts=111, ohms=10)
11.1
>>> pylithium.calc_watts(volts=111, amps=4.5)
499.5
>>> pylithium.calc_ohms(volts=12, amps=2)
6.0
>>> pylithium.calc_volts(amps=2, ohms=6)
12.0
```

`solve()` returns all four at once as a named tuple:

```python
>>> pylithium.solve(volts=111, amps=4.5)
Circuit(volts=111.0, amps=4.5, watts=499.5, ohms=24.666666666666668)
>>> pylithium.solve(watts=500, ohms=10).volts
70.71067811865476
>>> pylithium.solve(volts=12, amps=2).as_dict()
{'volts': 12.0, 'amps': 2.0, 'watts': 24.0, 'ohms': 6.0}
```

### One value is never enough

Volts alone cannot determine amps, so pylithium says so instead of inventing
an answer:

```python
>>> pylithium.calc_amps(volts=111)
Traceback (most recent call last):
    ...
pylithium.errors.InsufficientInputError: calc_amps() needs two of ohms, volts,
watts to determine amps; got only volts. Add one of: ohms, watts.
```

Pass three or four values and the extras are checked rather than ignored:

```python
>>> pylithium.solve(volts=12, amps=2, watts=99)
Traceback (most recent call last):
    ...
pylithium.errors.InconsistentInputError: watts=99.0 contradicts volts=12.0 * amps=2.0 = 24.0
```

## Batteries

```python
>>> pylithium.energy_wh(amp_hours=3.4, volts=3.6)
12.24
>>> pylithium.energy_kwh(amp_hours=100, volts=400)
40.0
>>> pylithium.capacity_ah(watt_hours=12.24, volts=3.6)
3.4
>>> pylithium.c_rate(current_amps=50, capacity_ah=100)
0.5
>>> pylithium.current_at_c_rate(rate=2, capacity_ah=3.4)
6.8
```

Runtime and charge time take optional derating factors, which default to `1.0`
so nothing is assumed about your system until you say so:

```python
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10)
10.0
>>> pylithium.runtime_hours(capacity_ah=100, load_amps=10, usable_fraction=0.8)
8.0
>>> pylithium.charge_time_hours(capacity_ah=50, charge_amps=10, from_soc=20, to_soc=80)
3.0
```

### Packs

```python
>>> p = pylithium.pack(series=6, parallel=4, chemistry="li-ion", cell_ah=3.4)
>>> p.cells
24
>>> p.nominal_volts
21.6
>>> p.capacity_ah
13.6
>>> p.energy_wh
293.76
>>> sorted(p.as_dict())
['capacity_ah', 'cells', 'chemistry', 'energy_wh', 'max_volts', 'min_volts', 'nominal_volts', 'parallel', 'series']
```

`Pack` is a named tuple, so it unpacks and prints like one, and `as_dict()`
gives you a plain dict for serialising.

### State of charge

Estimated from a **resting** (open-circuit) voltage by interpolating a
per-chemistry OCV curve. A pack under load reads low and one on charge reads
high, so let it settle first.

```python
>>> round(pylithium.state_of_charge(3.9), 1)
70.0
>>> round(pylithium.state_of_charge(52.8, chemistry="lifepo4", series=16), 1)
70.0
>>> round(pylithium.voltage_at_soc(50), 2)
3.75
>>> pylithium.list_chemistries()
['li-ion', 'lifepo4', 'lipo', 'lto']
```

Aliases are accepted and case is ignored, so `"LFP"`, `"lifepo4"` and
`"iron phosphate"` are the same chemistry, as are `"NMC"`, `"Li Ion"` and
`"li-ion"`.

These curves are typical room-temperature values for rough estimation. Real
cells vary by manufacturer, age, temperature and load — read a datasheet
before relying on them for anything that matters.

## Unit conversion

```python
>>> pylithium.convert(5000, "mAh", "Ah")
5.0
>>> pylithium.convert(1, "kWh", "J")
3600000.0
>>> pylithium.convert(25, "C", "F")
77.0
>>> pylithium.dimension_of("kWh")
'energy'
```

Unit names are case-insensitive and may use spaces, hyphens or underscores, so
`"kWh"`, `"kilowatt hours"` and `"kilowatt_hours"` all work. Converting across
dimensions is an error rather than a silent wrong answer:

```python
>>> pylithium.convert(1, "Ah", "Wh")
Traceback (most recent call last):
    ...
pylithium.errors.DimensionMismatchError: cannot convert ah (charge) to wh (energy)
```

**A leading `m` always means milli, never mega.** Mega-prefixed units are
deliberately left undefined so `"mW"` can only ever be milliwatts; a million
ohms is spelled `"megohm"`. Call `pylithium.list_units()` for the full set.

## Command line

The same calculations without writing a script:

```sh
$ pylithium solve --volts 111 --amps 4.5
volts:  111
amps:   4.5
watts:  499.5
ohms:   24.6667

$ pylithium convert 5000 mAh Ah
value:  5
unit:   Ah

$ pylithium pack -s 6 -p 4 --cell-ah 3.4 --json
{
  "series": 6,
  "parallel": 4,
  "cells": 24,
  ...
}
```

`python -m pylithium` works identically if the script is not on your `PATH`.
Run `pylithium --help` for every subcommand.

## Errors

All errors subclass both `PylithiumError` and the builtin you would expect, so
either of these works:

```python
try:
    amps = pylithium.calc_amps(volts=111)
except pylithium.PylithiumError:
    ...
except ValueError:      # equally valid
    ...
```

| Error | Raised when |
| --- | --- |
| `InsufficientInputError` | fewer than two quantities given |
| `InconsistentInputError` | extra quantities contradict the rest |
| `InvalidValueError` | a value is not finite, not numeric, or out of range |
| `UnknownUnitError` | unit name not in the registry |
| `DimensionMismatchError` | conversion between different dimensions |
| `UnknownChemistryError` | chemistry name not in the registry |

Booleans are rejected rather than coerced, so `calc_amps(volts=True)` is an
error instead of a silent 1.0 V.

## License

MIT
