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 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
- 4.2.3 Preparation of a reaction mixture from stoichiometric rules
- Reproduces the application examples from Section 4.2 of the manuscript (“Arithmetic of Mixing”):
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chemcalc_lib-0.1.2.tar.gz.
File metadata
- Download URL: chemcalc_lib-0.1.2.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fddde73531e5b4933b4eba57252e0f2e129a796b3ca6d6813a2a14bb44005d3e
|
|
| MD5 |
87231dcc0b1e646cedc68fed60646e0e
|
|
| BLAKE2b-256 |
d896ce1fad410f11d5e88c4be2bb096cd01c4cfd98f5e73d6961339b92224c04
|
File details
Details for the file chemcalc_lib-0.1.2-py3-none-any.whl.
File metadata
- Download URL: chemcalc_lib-0.1.2-py3-none-any.whl
- Upload date:
- Size: 32.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa161042ece8f4202c264a4741e713efa237a5493eb0d9fe06fefc1aea1bdd0a
|
|
| MD5 |
3700791a0a5fbd909e65a8ad837b58c6
|
|
| BLAKE2b-256 |
3b18c713e9b0c79c67cccf19ca5115188e2b81e6b8843009d881f0c13b81c00f
|