Skip to main content

C++ powered chemical equilibrium solver

Project description

equilibrium-solver

C++ (pybind11) chemical equilibrium solver with a Python API.

The project computes equilibrium composition by minimizing a Gibbs-energy-based objective under linear element-balance constraints.

Features

  • C++17 core solver for performance.
  • Python bindings via pybind11.
  • High-level Python wrappers in equilibrium_solver/api.py.
  • Input validation for matrix/vector dimensions and thermodynamic parameters.

Repository Layout

  • src/solver_core.cpp: core numerical method implementation.
  • src/python_bindings.cpp: pybind11 bridge exposing _native.calculate_equilibrium.
  • equilibrium_solver/api.py: public Python functions and validation helpers.
  • include/equilibrium_solver.hpp: C++ public data structures and interfaces.

Requirements

  • Python 3.10+
  • CMake 3.18+
  • A C++17 compiler:
    • Linux: GCC/Clang
    • Windows: MSVC (Build Tools or Visual Studio)
  • Build dependencies are declared in pyproject.toml:
    • scikit-build-core>=0.9.7
    • pybind11>=2.13.0

Installation

1. Create and activate a virtual environment (recommended)

Windows (PowerShell):

python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip

Linux/macOS:

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip

2. Install package from source

From the repository root:

pip install .

For development (editable install):

pip install -e .

This step builds the C++ extension module and installs it as equilibrium_cpp._native.

Quick Start (Python)

from equilibrium_solver import calculate_equilibrium

S = [
		[2.0, 0.0, 1.0],  # element 1 stoichiometry by species
		[0.0, 2.0, 1.0],  # element 2 stoichiometry by species
]

phases = [0, 0, 1]                 # phase id per species
elements = [1.0, 1.0]              # total element amounts
gibbs_energies = [-10000.0, -9500.0, -12000.0]  # J/mol-like values
T = 1200.0
Pa = 1.0

x_eq = calculate_equilibrium(S, phases, elements, gibbs_energies, T, Pa)
print(x_eq)

Public API:

  • calculate_equilibrium(...): snake_case entry point.
  • calculateEquilibrium(...): camelCase alias.
  • calc_equilibrium_from_db(...): convenience function for database-driven workflows.

Input Contract

For calculate_equilibrium(S, phases, elements, GibbsEnergies, T, Pa=1.0):

  • S: non-empty 2D matrix with shape (n_elements, n_species).
  • phases: length n_species, integer phase id for each species.
  • elements: length n_elements, non-negative and finite.
  • GibbsEnergies: length n_species, finite.
  • T > 0, Pa > 0.

The solver raises ValueError/RuntimeError (Python side) or std::invalid_argument/std::runtime_error (C++ side) when inputs are invalid or no feasible solution is found.

Numerical Method (Implemented in src/solver_core.cpp)

This solver is a custom constrained optimization routine with simplex-like basis updates and phase-coupled logarithmic terms.

Problem Form

The method targets minimization of an objective of the form:

  • linear Gibbs contribution per species;
  • entropy-like phase mixing term x_i * log(x_i / sum_phase) for multi-species phases;
  • linear element-balance constraints A * x = b.

Slack variables are introduced internally to enforce feasibility during iterations.

Core Ideas

  1. Species are reordered so each phase forms a contiguous block.
  2. A basis is initialized from slack variables.
  3. Reduced costs are computed for non-basis columns.
  4. Candidate moves are tested in two directions (increase/decrease variable).
  5. Step sizes are bounded by:
    • positivity of variables;
    • phase-coupled stability limits.
  6. A safeguarded line search (biased large-step then bisection) chooses the step.
  7. Basis replacement (replace_basis) performs a pivot in decomposition space.
  8. If no entering column is suitable, fallback phase-specific recovery moves are applied.
  9. At convergence, slack variables must be near zero; then post-processing restores non-basis values in multi-species phases.

Numerical Stabilization

  • Values are clamped to >= 1e-60 before log operations.
  • Many pivot/projection decisions use tolerances around 1e-5 and 1e-12.
  • Element totals are normalized by max(elements) before solving and rescaled back.

Pressure Handling

Dimensionless transformed Gibbs energies are assembled as:

  • -G / (R * T) at Pa == 1.0;
  • additional -log(Pa) correction is applied for selected phase entries in the current model branch.

Build/Packaging Notes

  • The extension is created by CMake via pybind11_add_module(_native ...).
  • Install target places _native into the equilibrium_cpp package.
  • scikit-build-core drives CMake build from pip install.

Troubleshooting

  • Build fails with pybind11 not found:
    • ensure internet access for pip dependency resolution;
    • verify CMake can find Python and compiler toolchain.
  • Import error equilibrium_cpp._native:
    • reinstall with pip install -e . in an active environment;
    • confirm that installation uses the same interpreter where code is executed.
  • Runtime error Solution not found:
    • check physical consistency of S, elements, phase assignments, and Gibbs data;
    • test with a simpler, known-feasible system first.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

equilibrium_solver-0.1.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

equilibrium_solver-0.1.0-cp313-cp313-win_amd64.whl (100.6 kB view details)

Uploaded CPython 3.13Windows x86-64

equilibrium_solver-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

equilibrium_solver-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (84.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

equilibrium_solver-0.1.0-cp313-cp313-macosx_10_13_x86_64.whl (93.8 kB view details)

Uploaded CPython 3.13macOS 10.13+ x86-64

equilibrium_solver-0.1.0-cp312-cp312-win_amd64.whl (100.6 kB view details)

Uploaded CPython 3.12Windows x86-64

equilibrium_solver-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (120.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

equilibrium_solver-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (84.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

equilibrium_solver-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl (93.7 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

equilibrium_solver-0.1.0-cp311-cp311-win_amd64.whl (100.0 kB view details)

Uploaded CPython 3.11Windows x86-64

equilibrium_solver-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (121.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

equilibrium_solver-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (84.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

equilibrium_solver-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl (93.2 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

equilibrium_solver-0.1.0-cp310-cp310-win_amd64.whl (99.4 kB view details)

Uploaded CPython 3.10Windows x86-64

equilibrium_solver-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (119.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

equilibrium_solver-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (82.9 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

equilibrium_solver-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl (91.8 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

File details

Details for the file equilibrium_solver-0.1.0.tar.gz.

File metadata

  • Download URL: equilibrium_solver-0.1.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for equilibrium_solver-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2bd9a49e1929348287ca6107cd39bd816a1c26c295b815fa2e8831d471f6ccf1
MD5 aa73414c7e8fb374182be7002c9cd091
BLAKE2b-256 d5e729d36ccf90bc0301d6b67969bbf51e0cf246722f307433a8207813a3c0ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0.tar.gz:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 598de2ae2814994c4199fb581aa2c7c480c49c83c209732c2bb56c4b304f604b
MD5 7f2dd23a98374d353c8ebf84a90d79a0
BLAKE2b-256 0612a420a4ba7b12bac7aa5ada239a0105c1174873b3757e1feb140234039e26

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bb2432980e85f535a96422358aaff6ce14a41be7946fd93c9357ca785f4e6f9c
MD5 90d9ab9b47337b37505b6099e3e96eff
BLAKE2b-256 f87ab366c684f6c0cad7eb1b79eeec53522f221a5d037f4ebd6c24daf100ac64

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6f83cbd17ed2b5e822c98ccb451d7085f43b726675f6115ca29d0b18d945f5a7
MD5 f0697478f2d38e3be36367329913d82c
BLAKE2b-256 db0009243da536ed8ec7a407b8db5d56768f89caa697ada42c5b7ae8edb9af45

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp313-cp313-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp313-cp313-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 cb87b2e0e5e33017c8cb81ce590de5488f00f84fb7a83a026c6ebc63041a8f92
MD5 26504bd1957c661ac8b80b542aa5af59
BLAKE2b-256 15480479d0e28fcb36b609d9a4fcdcbacb122e418c6d852296f2beede1e525b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp313-cp313-macosx_10_13_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 175ecd52b9433f0768342d7c3b12e2e7a21e666573dfd061fd4494ebb282d997
MD5 939fcc226f1d017cabb889856bc76845
BLAKE2b-256 433221f3da530aaf0236f9ff584deefef33320f1b296acb2e818a3b50c725231

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0a357eecdc2d6f6d0c877eed345b3fe732b0fbd123608eb32473ee148436fcd2
MD5 21b4318659d4a93d82069a04ba2087fc
BLAKE2b-256 9e3cacf533e3973695184355f2787ed8ea8d378927e43f6b4f3bdded939b7666

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1235a1372ec802919ab0d7e5c6182adb5c8623f820426f13073d32a1f7c092d8
MD5 b5061eaabf8cd4d628d77e80cbf6dc4e
BLAKE2b-256 4f98ebbaf50ddfefe55466e55294e07492dca51723db2ffcb3c02dcd935e224b

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 1a40014018dc988c4096d6deb9962ad0920f16d637ae051c57838d0250b04e7f
MD5 8e229d6ef1082e91fb2dcc7390df0bd3
BLAKE2b-256 2d80c076b2bb65d229a126a97114a3f0eb19773e351839efacbc6eac0b83c59a

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 42cdf38fe0a91667bb25e56cc9b8e863d530602b13122aa48f7b4d41b6f776dc
MD5 c3e0ff0d9d04219c88a4ecc572cb14df
BLAKE2b-256 608936f1c968ff642b8f2a72556082d34f6734781caa7bea6de5fc681c7a1fe2

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b0255182abcf2c155643b3d06aad638ae74a0af360998215036b98e8d6bb1c71
MD5 d501765d76069b21c651873c995800d7
BLAKE2b-256 0b370956772da1cd82e8344f13de61f63fbc0e88264ad14c059c2c0dfcc51404

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9977ba4ab59dd5f81b8deeb14f01320c65343fa01beb35a811095f3d1533c4a0
MD5 365b9603a4a44080937cbfe0204ba7bf
BLAKE2b-256 8c75a24675b3cf7cbe1b2640e6cca4d5d326b296bcee50a5aa03b38c9faa30d8

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 92c2394943326c516aab2c59ac4f06f3074f857563fd3b9fe985b5b6a25c2912
MD5 80b3d7fc6304eae8d67df916b3320fd7
BLAKE2b-256 8655a78e0e420872483dd2d538ccdf757ff653ca222e17151e32a7aa5b0825d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f2635a200a4e3a4012d9f7ca4d761e39c01db82e041a65d4a6c717b5295fda02
MD5 82b33d2f1c42243dd1d7b737c24a59a0
BLAKE2b-256 090c3efe1e93070d27ffd4a19f94dcd1d3938db845a51eabe0ed160e322ef7b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp310-cp310-win_amd64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 832e05d08ce753bf72bddb26513ed354029a63a9381b8cfe2b2e284a03b121d4
MD5 3ca308069dadc1cd7392a571b219d612
BLAKE2b-256 22d37e23c4d37aac90ab9033fb42b4f3712895840beabb66ac416894ef1ed6ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3b4d847a177c88ca751eb8660d951a3574854e9a646b790935a2e40889820c2d
MD5 74cdefa9e3e41ef0999d71a112cc25f8
BLAKE2b-256 3ecb3e8278f427927bf380612d24ca3e27af91b1f9097bc0d5ff751983156921

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file equilibrium_solver-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for equilibrium_solver-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d77efc1846b6e3e1eb5a362906b61909b6da5a0b9d8ce07206f59e290fde3eac
MD5 a23149b68d6656e18a9524100abd3b62
BLAKE2b-256 ece795fc309003b2de66d4a8a482cb5204af1e4bae6ee9bcd0da27a7b8300c4d

See more details on using hashes here.

Provenance

The following attestation bundles were made for equilibrium_solver-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl:

Publisher: publish.yml on username-111/solver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page