Skip to main content

Chemical synthesis calculator

Python package for calculating the masses of substances required for chemical synthesis directly from the reaction string. It includes solutions for all intermediate steps, including chemical formula parsing, molar mass calculation and reaction balancing with different matrix methods.

Prerequisites

Installation

Install from pypi:

pip install chemsynthcalc

Documentation

Read the documentation

Example use

Let's say that we need to prepare 3 grams of YBCO by solid-state synthesis from respective carbonates. The reaction string will look something like this (to simplify, let's leave it without oxygen nonstoichiometry):

from chemsynthcalc import ChemicalReaction

reaction_string = "BaCO3 + Y2(CO3)3 + CuCO3 + O2 → YBa2Cu3O7 + CO2"

Now, we can create a chemical reaction object of the ChemicalReaction class, which will be used in the calculation. We need to specify the arguments for our particular case:

>>> reaction = ChemicalReaction(
    reaction = reaction_string, # our reaction string
    target = 0, # index of target compound in the product list
    target_mass = 3, # desired mass of target compound,
    mode = "balance" # mode of coefficients calculations,
)

Now, to perform the automatic calculation, all we need to do is to put:

>>> reaction.print_results(print_rounding_order=4) 
# assuming that we use analytical balances with 4 digit presicion

And we get our output in the terminal:

initial reaction: BaCO3+Y2(CO3)3+CuCO3+O2→YBa2Cu3O7+CO2
reaction matrix:
 [[1. 0. 0. 0. 2. 0.]
 [1. 3. 1. 0. 0. 1.]
 [3. 9. 3. 2. 7. 2.]
 [0. 2. 0. 0. 1. 0.]
 [0. 0. 1. 0. 3. 0.]]
mode: balance
formulas: ['BaCO3', 'Y2(CO3)3', 'CuCO3', 'O2', 'YBa2Cu3O7', 'CO2']
coefficients: [8, 2, 12, 1, 4, 26]
normalized coefficients: [2, 0.5, 3, 0.25, 1, 6.5]
algorithm: inverse
is balanced: True
final reaction: 8BaCO3+2Y2(CO3)3+12CuCO3+O2→4YBa2Cu3O7+26CO2
final reaction normalized: 2BaCO3+0.5Y2(CO3)3+3CuCO3+0.25O2→YBa2Cu3O7+6.5CO2
molar masses: [197.335, 357.835676, 123.554, 31.998, 666.190838, 44.009]
target: YBa2Cu3O7
masses: [1.7773, 0.8057, 1.6692, 0.036, 3.0, 1.2882]
BaCO3: M = 197.3350 g/mol, m = 1.7773 g
Y2(CO3)3: M = 357.8357 g/mol, m = 0.8057 g
CuCO3: M = 123.5540 g/mol, m = 1.6692 g
O2: M = 31.9980 g/mol, m = 0.0360 g
YBa2Cu3O7: M = 666.1908 g/mol, m = 3.0000 g
CO2: M = 44.0090 g/mol, m = 1.2882 g

Now, we've got all the masses ready for our planned synthesis!

Features

  • Formula parsing
    >>> from chemsynthcalc import ChemicalFormula
    
    >>> ChemicalFormula("C2H5OH").parsed_formula
    {'C': 2.0, 'H': 6.0, 'O': 1.0}
    
  • Calculation of the molar mass
    >>> ChemicalFormula("C2H5OH").molar_mass
    46.069
    
  • Mass, atomic, and oxide percent calculations with mass_percent, atomic_percent and oxide_percent (including user-defined oxides) properties of ChemicalFormula.
  • Auto-balancing chemical equations by 4 different matrix methods in balance mode:
    >>> from chemsynthcalc import ChemicalReaction
    
    >>> reaction_string = "K4Fe(CN)6 + KMnO4 + H2SO4 = KHSO4 + Fe2(SO4)3 + MnSO4 + HNO3 + CO2 + H2O"
    
    >>> ChemicalReaction(reaction_string, mode="balance").final_reaction
    10K4Fe(CN)6+122KMnO4+299H2SO4=162KHSO4+5Fe2(SO4)3+122MnSO4+60HNO3+60CO2+188H2O
    
  • Calculation of masses for user-defined coefficients in force (calculates regardless of balance) and check (checks if reaction is balanced by user-defined coefficients) modes.
    >>> ChemicalReaction("BaCO3+TiO2=BaTiO3", mode="force").masses #we can drop CO2 product and still get masses in this mode. 
    [0.84623763, 0.34248749, 1.0]
    
    >>> ChemicalReaction("2H2+O2=2H2O", mode="check").coefficients #we can be sure that reaction is balanced with our coefficients in this mode
    [2, 1, 2]
    
    Setting the coefficients directly into ChemicalReaction instance:
    >>> reaction = ChemicalReaction("H2+O2=H2O", mode="check")
    >>> reaction.coefficients = [2,1,2]
    >>> reaction.coefficients
    [2, 1, 2]
    >>> reaction.is_balanced
    True
    
  • Calculation of coefficients with ChemicalReaction.balance object individually by each of 4 different algorithms (inverse, general pseudoinverse, partial pseudoinverse and combinatorial algorithms).
  • Export of results of both ChemicalFormula and ChemicalReaction into .txt file (with .to_txt()), into JSON object (with .to_json()) or JSON file (with .to_json_file()).

License

The code is provided under the MIT license.

Contact

If you have any questions, please contact Egor Syrov at syrov_ev@mail.ru or create an issue at github https://github.com/Syrov-Egor/chemsynthcalc/issues.

Metadata

Release files for chemsynthcalc 2.1.3

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

Source distribution (sdist)

Source distribution for chemsynthcalc 2.1.3
File Size Uploaded Provenance Actions
chemsynthcalc-2.1.3.tar.gz 24.9 kB Without provenance Details for chemsynthcalc-2.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chemsynthcalc 2.1.3
File Interpreter ABI Platform Actions
chemsynthcalc-2.1.3-py3-none-any.whl Python 3 none any Details for chemsynthcalc-2.1.3-py3-none-any.whl

Total release size: 58.6 kB

Release files / chemsynthcalc-2.1.3.tar.gz

Download URL chemsynthcalc-2.1.3.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f1bdc536b8076359589af6e3c8b238caf0739d7daaa15fb014c70afb6e2e6018
BLAKE2b-256 checksum
How to use checksums
c68d19130b11c72558323bf3ea94c2a56f0a428386d15a6dee19d6cb8c7145dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / chemsynthcalc-2.1.3-py3-none-any.whl

Download URL chemsynthcalc-2.1.3-py3-none-any.whl
Size 33.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb411bc86e05e218d8812c8fad2f85d3027c220eed80bd1eba3c06a8fc9c835e
BLAKE2b-256 checksum
How to use checksums
b2ed0e643c17a63a1db14ed9efdca25fdcea8dff0a06a8926a87a36bd4e8fbee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for chemsynthcalc 2.1.3

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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