Skip to main content

A library for chemical mixture calculations and composition conversions

Project description

ChemCalc

ChemCalc is a Python library to convert between different chemical amount types and to compute mole fractions for complex mixtures, including mixtures of mixtures.

Features

  • Convert between amount types (mass, volume, moles, concentrations, fractions, etc.)
  • Calculate mole fractions from various mixture specifications
  • Handle complex mixture compositions including entities (ions, fragments) and stoichiometry
  • Support for mixtures of mixtures at any level of nesting (recursive mixture trees)
  • Unit cell population calculations for molecular simulation
  • Support for molality calculations with multiple solutes
  • Utility algebra functions for building custom workflows

Installation

pip install chemcalc_lib

Quick Start

Basic example: water/ethanol/NaCl solution

import ChemCalc_lib as cc

# Define the mixture components
names             = ["Water", "Ethanol", "NaCl"        ]
amounts           = [70.0   , 30.0     , 1.0           ]
amount_types      = ["φ"    , "φ"      , "c"           ]  # volume fractions and molarity
units             = ["%"    , "%"      , "mol/L"       ]
molar_weights     = [18.02  , 46.07    , 58.44         ]  # g/mol
molar_volumes     = [18.0   , 58.5     , 27.0          ]  # mL/mol
entities          = [[]     , []       , ["Na+", "Cl-"]]
stoichiometries   = [[]     , []       , [1.0  , 1.0  ]]

# Prepare the mixture
component_data = cc.create_mixture(
    names, amounts, amount_types, units,
    Mw=molar_weights, Vm=molar_volumes,
    entities=entities, stoichiometries=stoichiometries,
)

# Mole fractions (rounded for readability)
result = cc.get_mole_fractions(component_data, include_entities=True)
print("Mole fractions       :",
      {k: round(v, 3) for k, v in result["mole_fractions"].items()})
print("Entity mole fractions:",
      {k: round(v, 3) for k, v in result["entity_mole_fractions"].items()})

# Convert to practical amounts for preparing 1 L of solution
target_types      = ["V", "V", "m"]   # solvents in volume, salt in mass
total_amount      = 1.0               # 1 L total
total_amount_type = "V"

conversion = cc.convert(component_data, target_types, total_amount, total_amount_type)

print("To prepare 1 L solution:")
for comp_name, data in conversion["converted_amounts"].items():
    if data["amount_type"] == "V":
        print(comp_name, round(data["amount"], 2), "L")
    elif data["amount_type"] == "m":
        print(comp_name, round(data["amount"], 2), "g")

Advanced examples

The examples/ folder contains several fully worked scripts:

  • basic_examples.py

    • Example 1: 1 mol/L NaCl in water/ethanol 7:3 v/v
    • Example 2: multiple solutes with molalities in a mixed solvent
    • Includes a unit-cell population example.
  • recursive_example.py

    • Toy example of recursive mixtures built from basic components (water, ethanol, glycerol, several salts).
    • Demonstrates how to define a mixture tree manually and obtain mole fractions and practical preparation amounts for terminal mixtures.
  • paper_examples.py

    • Reproduces the two first application examples from Section 4.2 of the manuscript (“Arithmetic of Mixing”):
      • 4.2.1 Battery electrolyte formulation
      • 4.2.2 Microemulsion with surfactant mixture

You can run any of these from the repository root with:

python examples/basic_examples.py
python examples/recursive_example.py
python examples/paper_examples.py

(adjust paths as needed depending on where you place the files.)

Supported amount types

Symbol Type Standard unit
m Mass g
V Volume L
n Moles mol
w Weight fraction – (0–1)
φ Volume fraction – (0–1)
x Mole fraction – (0–1)
c Molarity mol/L
b Molality mol/kg
ρ Mass concentration g/L
v Specific volume L/g

Unit handling

The library automatically handles common unit conversions:

  • Mass: kg, g, mg, μg
  • Volume: m³, L, mL, μL, cc, cm³
  • Amount: mol, mmol, μmol
  • Concentrations: M, mM, μM, mol/L, etc.
  • Fractions: decimal (0–1) or percentage (%)

API overview

Main functions

  • create_mixture() – build the internal data structure for a mixture.
  • get_mole_fractions() – calculate mole fractions (and optionally entity mole fractions).
  • convert() – convert a mixture to a set of target amount types for a specified total amount.
  • get_mole_fractions_recursive() – handle mixtures of mixtures defined as a mixture tree.
  • populate_unit_cell() – calculate the population of entities in a crystallographic unit cell.

Utilities

  • create_amount_matrix() – convert mixture specifications into matrix form.
  • entities_mole_fraction_algebra() – perform the algebra for entity mole fractions.
  • amount_conversion_algebra() – core conversion engine between amount types.

For full details, see the docstrings in the source code and the example scripts.

Requirements

  • Python ≥ 3.8
  • NumPy ≥ 1.19.0

License

MIT License

Contributing

Contributions are welcome. Please open issues or pull requests on the GitHub repository:

https://github.com/TheophileGaudin/chemcalc-lib

Citation

If you use ChemCalc in your research, please cite:

Théophile Gaudin, Arithmetic of Mixing, in preparation.
GitHub: https://github.com/TheophileGaudin/chemcalc-lib, doi: 10.5281/zenodo.17525109 

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

chemcalc_lib-0.1.3.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

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

chemcalc_lib-0.1.3-py3-none-any.whl (31.0 kB view details)

Uploaded Python 3

File details

Details for the file chemcalc_lib-0.1.3.tar.gz.

File metadata

  • Download URL: chemcalc_lib-0.1.3.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for chemcalc_lib-0.1.3.tar.gz
Algorithm Hash digest
SHA256 32cd2cbc19ccc3459e6751f9639cb21328ad0b8d5982a193536147c697504515
MD5 908c23f3b130e61e16b97585f867288c
BLAKE2b-256 27c8514d3c68533ccc8ed04ac88007163de22cdd78d38720fac88946010897fd

See more details on using hashes here.

File details

Details for the file chemcalc_lib-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: chemcalc_lib-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 31.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for chemcalc_lib-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b72eaa0dd91403f8d409ebac1d28eae7e3d3b12d150cdbff36efd3ccd2fc008
MD5 8ebc8821a16a129ea2a88ad3b6eaa74c
BLAKE2b-256 3ad3ff192c9ecf5f6bd2b0d6d15db400d61b34149fc0614506a57ee2a9d3543b

See more details on using hashes here.

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