Skip to main content

Water Chemistry Engine

PyPI version Python versions CI License Documentation

A reusable, scientifically grounded Python engine for characterizing, blending, treating, comparing, and optimizing water for brewing, fermentation, and other validated uses.

The engine is intentionally independent of web frameworks, databases, graphical interfaces, and product-specific persistence. End-user applications consume this package from separate projects.

Project status

The 0.4 automatic-treatment-optimizer milestone is complete, and 0.5 is under active development. The engine can resolve reported source-water chemistry, optimize or preserve supported source blends, choose bounded practical mineral doses, calculate the resulting water, compare it with target/reference criteria, and return auditable candidate plans, contributions, instructions, diagnostics, and notices.

Python 3.11 is the project compatibility baseline, with CI coverage through Python 3.14. Public APIs remain pre-1.0 and may evolve as real consumer applications exercise the engine.

0.4.1 compatibility warning: optimizer admission is stricter for carbonate-system inputs. Requests targeting Ion.BICARBONATE or Ion.CARBONATE, requests permitting a material that contributes either species (including SODIUM_BICARBONATE), and requests targeting total alkalinity are now rejected as unsupported with stable diagnostic codes. Manual forward sodium-bicarbonate calculations remain available, with explicit formal-inventory limitations. See the consumer API behavior-change note before upgrading from 0.4.0.

Version 0.3 establishes a supported package-root consumer facade covering both the deterministic forward-result graph and the complete source-reporting and provenance input graph. Its compatibility expectations are documented in the consumer API guide. Reported and target pH use FermUnits' semantic PHValue; calculated working-water pH remains explicitly deferred until a validated reusable model is ready.

Version 0.3.1 expands the supported FermUnits dependency range to include the stable 1.x series while retaining the compatible 0.1.3 floor; the Engine continues to import unit functionality only through FermUnits.

Version 0.4 implements the first bounded automatic treatment optimizer. It supports fixed blends, proportional dilution with an explicitly characterized diluent, and bounded source-volume selection; chooses whole caller-declared material dose increments; returns structured forward-replayed plans; offers an equally close fewest-products alternative when materially different; and can return an independently solved no-dilution best-effort plan. The optimizer contract remains pre-1.0 and may evolve under the documented consumer API compatibility policy.

Version 0.4.1 repairs carbonate-system support boundaries. Explicitly reported bicarbonate, carbonate, and total alkalinity remain distinct and lossless, but bicarbonate/carbonate results are labeled as formal inventory rather than equilibrium speciation. Automatic optimization rejects carbonate-system targets and sodium bicarbonate until a validated model exists; deterministic manual sodium-bicarbonate accounting remains available with structured limitations.

Version 0.5 expands practical treatment-material semantics, profile provenance, target-comparison interpretation, and a named conservative-equivalent total-alkalinity balance. The in-progress alkalinity workflow now covers source resolution, blending, reviewed sodium-bicarbonate contributions, final modeled alkalinity, target comparison, and contribution reporting. Report-native alkalinity can preserve its statistic, original labels, total-alkalinity-versus-ANC identity, filtration state, method, and titration endpoint. Explicit ANC remains unresolved rather than being silently treated as total alkalinity, and the balance does not claim to simulate a laboratory titration. Consumer-supplied target/reference collections can optionally use an in-memory versioned catalog validator that requires evidence-based per-profile provenance and exact version selection without silently merging conflicting sources. Source and target/reference profile storage, curation, licensing records, and distribution belong to consumer applications such as Water Chemistry Designer. Optimizer support now spans fixed blends, proportional dilution, and bounded source-volume selection when alkalinity is resolvable and an appropriate criterion is present. A caller may also attach a versioned, sourced practical- use policy to a material constraint; its batch-scaled maximum is validated without silently replacing the caller's explicit maximum or claiming universal applicability. Its denominator is explicitly the optimizer's requested total- water volume; other process-volume bases are not silently reinterpreted. Unreleased 0.5 work should be consumed only from an exact commit or locally built artifact; ordinary applications should continue to pin the 0.4.1 PyPI release.

Forward-calculator capabilities established in 0.2

  • source-water and target/reference profile models;
  • preservation of exact values, ranges, bounds, ND, reported statistics, reporting context, source-document metadata, and chlorine/chloramine data;
  • explicit source-resolution policy, including opt-in exact-range midpoints;
  • fixed multi-source blending with conservative unknown propagation;
  • supported mineral-addition stoichiometry and deterministic treatment;
  • source, blend, and final target/reference comparison;
  • combined source/treatment ion-contribution reporting;
  • structured preparation instructions;
  • structured notices for assumptions, unresolved inputs, model limitations, and deferred target-pH calculation.

Release path

  • 0.3.1: FermUnits 1.0 compatibility and release-workflow maintenance;
  • 0.4: automatic treatment optimization with practical candidate plans, explicit feasibility/deviation diagnostics, and the minimum exact-composition treatment-material semantics needed for safe recommendations;
  • 0.5: conservative-equivalent total-alkalinity calculation, broader practical treatment materials, target/reference provenance, and richer comparison semantics;
  • 0.6: optimizer and public-contract hardening beyond the first useful strategy set;
  • 0.7: reusable working-water pH if a defensible model is ready;
  • 0.8: BeerJSON/FermentationJSON interchange, conformance work, and 1.0 hardening.

AI-assisted document ingestion, accounts, persistence, browser UI, and native application code belong to separate consumer applications rather than this repository.

Repository structure

water-chemistry-engine/
├── src/
│   └── water_chemistry_engine/
├── tests/
├── docs/
├── reference-data/  # engine scientific validation data, not app profiles
├── schemas/
├── scripts/
└── test-vectors/

src/water_chemistry_engine

The importable Python engine package, using the standard src/ layout. It owns scientific calculations, domain models, validation, warnings/notices, optimization as it is added, and structured calculation results.

External applications such as web, automation, Mechani-Brew, and future native clients can consume the engine while providing their own interfaces and persistence.

Installation

Water Chemistry Engine requires Python 3.11 or newer. Install the published package with uv or pip:

uv add water-chemistry-engine==0.4.1

or:

python -m pip install water-chemistry-engine==0.4.1

Version 0.3 exposes a supported package-root facade. APIs remain pre-1.0 and may evolve under the consumer API compatibility policy.

Quickstart

This example resolves and blends equal volumes of two reported source waters. Calcium therefore blends from 40 mg/L and 80 mg/L to 60 mg/L:

from fermunits import Q_

from water_chemistry_engine import (
    ForwardWaterSource,
    Ion,
    IonConcentration,
    SourceResolutionPolicy,
    SourceWaterProfile,
    calculate_forward_water,
)

source_a = SourceWaterProfile(
    name="Source A",
    concentrations=(IonConcentration.mg_per_liter(Ion.CALCIUM, 40.0),),
)
source_b = SourceWaterProfile(
    name="Source B",
    concentrations=(IonConcentration.mg_per_liter(Ion.CALCIUM, 80.0),),
)

result = calculate_forward_water(
    (
        ForwardWaterSource(source_a, Q_(1.0, "liter")),
        ForwardWaterSource(source_b, Q_(1.0, "liter")),
    ),
    source_resolution_policy=SourceResolutionPolicy(allow_exact_range_midpoints=False),
)

calcium = result.final_state.concentration_for(Ion.CALCIUM)
assert calcium is not None
print(calcium)

Expected output:

60.0 milligram / liter

The explicit source-resolution policy prevents the example from silently choosing representative values for ranges. See the consumer API guide for the complete supported workflow, including treatments, targets, notices, and audit results.

Development stack

  • Python 3.11+ (CI tests 3.11–3.14; 3.11 is the compatibility baseline)
  • uv
  • FermUnits
  • pytest and Hypothesis
  • Ruff
  • mypy
  • GitHub Actions

Development

Install uv, then run from the repository root. The checked-in .python-version selects the Python 3.11 compatibility baseline:

uv sync --dev
uv run pytest

The full CI gate tests Python 3.11, 3.12, 3.13, and 3.14. It also checks the lockfile, formatting, linting, strict typing against Python 3.11 semantics, and builds the engine distribution.

Documentation

Primary project documents are stored under docs/:

  • WATER_CHEM_DESIGN.md — scientific and architectural design;
  • WATER_CHEM_REFERENCES.md — source and reference register;
  • ROADMAP.md — active engine release path;
  • PROJECT_STRUCTURE.md — repository/package boundaries;
  • CONSUMER_API.md — supported 0.4 package-root facade and integration guide;
  • reviews/ — point-in-time external review records.

Release history is summarized in CHANGELOG.md.

License

This project is licensed under the Mozilla Public License 2.0.

Metadata

Release files for water-chemistry-engine 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for water-chemistry-engine 0.5.0
File Size Uploaded Provenance Actions
water_chemistry_engine-0.5.0.tar.gz 81.3 kB With provenance Details for water_chemistry_engine-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for water-chemistry-engine 0.5.0
File Interpreter ABI Platform Actions
water_chemistry_engine-0.5.0-py3-none-any.whl Python 3 none any Details for water_chemistry_engine-0.5.0-py3-none-any.whl

Total release size: 180.3 kB

Release files / water_chemistry_engine-0.5.0.tar.gz

Download URL water_chemistry_engine-0.5.0.tar.gz
Size 81.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f46863100efd53a4740b909bfdc0113069f2cf13c2252eb01b387a8c4bfec9db
BLAKE2b-256 checksum
How to use checksums
6d5b9be852e2132752c2a1f60fae8a0795686174f436675259bf6e4772153191
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Release files / water_chemistry_engine-0.5.0-py3-none-any.whl

Download URL water_chemistry_engine-0.5.0-py3-none-any.whl
Size 99.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a5d35f1afb92bc2624c320e8389992fc0e816beeaa1c6eeca9e8ff9feffb3ea
BLAKE2b-256 checksum
How to use checksums
06825d44c59c75d179661b306cb95e2fee5ada5aad540fd4a903ef4db02d8115
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Security for water-chemistry-engine 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for water_chemistry_engine-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page