Skip to main content

A library for chemical mixture calculations and composition conversions

Project description

ChemCalc

A Python library for to convert between different chemical amount types.

Features

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

Installation

pip install chemcalc_lib

Quick Start

Basic Example: Water/Ethanol/Salt 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-"]]  # NaCl dissociates            
stoichiometries   = [[]     , []       , [1.0  , 1.0  ]]  # 1:1 ratio

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

# Get mole fractions
result = cc.get_mole_fractions(component_data, include_entities=True)
print("Mole fractions       :", result["mole_fractions"])
print("Entity mole fractions:", result["entity_mole_fractions"])

# Convert to practical amounts for preparing 1L solution
target_types = ["V", "V", "m"]  # volumes for solvents, mass for salt
conversion = cc.convert(component_data, target_types, 1.0, "V")  # 1L total

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

Advanced Example: Molality Calculations

# Multiple solutes with molalities
names             = ["Water", "Ethanol", "NaCl", "Urea", "Hydroxybenzoic acid"]
amounts           = [70.0   , 30.0     , 1.0   , 0.5   , 0.8                  ]
amount_types      = ["φ"    , "φ"      , "c"   , "b"   , "b"                  ]  # b = molality
units             = ["%"    , "%"      ,"mol/L", "mol/kg", "mol/kg"           ]

component_data = cc.create_mixture(names, amounts, amount_types, units, ...)
result = cc.get_mole_fractions(component_data)

# Returns separate results for each molal solute
for i, res in enumerate(result):
    print(f"Solution {i+1} mole fractions:", res["mole_fractions"])

Recursive Mixtures

For complex solutions prepared in multiple steps:

# Define base components
components = {
    "Water": {"name": "Water", "mw": 18.015, "vm": 18.0},
    "Ethanol": {"name": "Ethanol", "mw": 46.07, "vm": 58.0},
    "NaCl": {
        "name": "NaCl", "mw": 58.44, "vm": 27.0,
        "properties": {
            "entities": [
                {"name": "Na⁺", "stoichiometry": 1.0},
                {"name": "Cl⁻", "stoichiometry": 1.0}
            ]
        }
    }
}

# Define intermediate mixtures
water_ethanol = {
    "name": "Water-Ethanol", 
    "parents": [
        {"name": "Water", "amount": 70, "amount_type": "φ", "unit": "%"},
        {"name": "Ethanol", "amount": 30, "amount_type": "φ", "unit": "%"}
    ]
}

# Define final mixture
saline_solution = {
    "name": "Saline-Solution",
    "parents": [
        {"name": "Water-Ethanol", "amount": 95, "amount_type": "V", "unit": "mL"},
        {"name": "NaCl", "amount": 0.9, "amount_type": "m", "unit": "g"}
    ]
}

# Combine all nodes
all_nodes = {**components, "Water-Ethanol": water_ethanol, "Saline-Solution": saline_solution}

# Calculate terminal mixtures
results = cc.get_mole_fractions_recursive(all_nodes, include_entities=True)

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 Conversions

The library automatically handles 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 Reference

Main Functions

  • create_mixture(): Create mixture data structure
  • get_mole_fractions(): Calculate mole fractions from mixture
  • convert(): Convert to target amount types
  • get_mole_fractions_recursive(): Handle mixtures of mixtures at any level of nesting
  • populate_unit_cell(): Calculate entities in unit cell

Utilities

  • create_amount_matrix(): Convert mixture composition to matrix format
  • entities_mole_fraction_algebra(): Calculate entity mole fractions
  • amount_conversion_algebra(): Core conversion calculations

Requirements

  • Python >= 3.8
  • NumPy >= 1.19.0

License

MIT License

Contributing

Contributions welcome! Please read our contributing guidelines and submit pull requests to our GitHub repository.

Citation

If you use ChemCalc in your research, please cite:

Théophile Gaudin, Arithmetic of Mixing, in preparation.
GitHub: https://github.com/yourusername/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

chemcalc_lib-0.1.1.tar.gz (27.0 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.1-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: chemcalc_lib-0.1.1.tar.gz
  • Upload date:
  • Size: 27.0 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.1.tar.gz
Algorithm Hash digest
SHA256 4526e65e73a716ef84dd5daf517acfa31636c61a957e32a45a858cb2f827847f
MD5 e54758c30bbcc034b436395dd87ca5a9
BLAKE2b-256 30a981829506730dd9ad866984a74da2a768e6e97661fafeffbfbdd0cafb38ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: chemcalc_lib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 315092f708dd2e94ce82de4df599a129549a8673e74dd5ff7c010259e8c592da
MD5 d1f1502f78578787faabda8ccc8f7049
BLAKE2b-256 9ce057603131ef4f57f9c3c4c64a01d9de67117100a9a27ff14b6fc3d55b3089

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