bjodah / chempy

⚗ A package useful for chemistry written in Python
BSD 2-Clause "Simplified" License
544 stars 78 forks source link

Why is my kinetics chempy code not running? #167

Closed cynthiarm17 closed 4 years ago

cynthiarm17 commented 4 years ago

from chempy import ReactionSystem # The rate constants below are arbitrary rsys = ReactionSystem.from_string("""Li + Li2CO3 -> Li+ + Li2CO3; 1e-4 2Li2CO3 -> 4Li+ + 2CO2 + O2; 17 Li2O2 -> 2Li+ + O2-; 2.5 Li -> Li+; 1e-7 4Li + 2C + 3O2 -> 2Li2CO3; 1e3 2Li+ + O2 -> Li2O2; 2.5""") # "[Li2O2]" = 7.9e-3 (actually 0.025 at RT) from chempy.kinetics.ode import get_odesys odesys, extra = getodesys(rsys) from collections import defaultdict import numpy as np tout = sorted(np.concatenate((np.linspace(0, 23), np.logspace(-8, 1)))) c0 = defaultdict(float, {'Li': 0.05, 'O2': 1e-12, 'Li2CO3': 1e-2, 'Li2O2': 2.5e-10, 'CO2': 1e-12}) result = odesys.integrate(tout, c0, atol=1e-12, rtol=1e-14) import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 2, figsize=(12, 5)) for ax in axes: = result.plot(names=[k for k in rsys.substances if k != 'Li2O2'], ax=ax) = ax.legend(loc='best', prop={'size': 9}) = ax.setxlabel('Time (hours)') = ax.setylabel('Concentration') = axes[1].setylim([1e-13, 1e-1]) = axes[1].setxscale('log') = axes[1].setyscale('log') = fig.tightlayout() = fig.savefig('examples/attempt.png', dpi=72)

cynthiarm17 commented 4 years ago

Help please

cynthiarm17 commented 4 years ago

from chempy import ReactionSystem # The rate constants below are arbitrary rsys = ReactionSystem.from_string("""Li -> 1 Li+; 0.078 Li2CO3 + H2O -> CO3-2 + 2 Li+; 0.5 Li2O2 -> 2 Li+ + O2-; 2.5 2 Li+ + O2 -> Li2O2; 2.5""") # "[Li2O2]" = 0.05 (actually 10 at RT) from chempy.kinetics import get_odesys odesys, extra = get_odesys(rsys) from collections import defaultdict import numpy as np tout = sorted(np.concatenate((np.linspace(0, 23), np.logspace(-8, 1)))) c0 = defaultdict(float, {'Li+': 0.05, 'O2': 1e-12, 'Li2CO3': 1e-2, 'Li2O2': 2.5e-10}) result = odesys.integrate(tout, c0, atol=1e-12, rtol=1e-14) import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 2, figsize=(12, 5)) for ax in axes: _ = result.plot(names=[k for k in rsys.substances if k != 'Li2O2'], ax=ax) _ = ax.legend(loc='best', prop={'size': 9}) _ = ax.set_xlabel('Time') _ = ax.set_ylabel('Concentration') _ = axes[1].set_ylim([1e-13, 1e-1]) _ = axes[1].set_xscale('log') _ = axes[1].set_yscale('log') _ = fig.tight_layout() _ = fig.savefig('examples/attempt.png', dpi=72)

cynthiarm17 commented 4 years ago

Help please, this is the error it says I have WhatsApp Image 2020-05-03 at 9 24 58 AM

jeremyagray commented 4 years ago

Several problems.

First, your chemical equations aren’t balanced in several places. Your error is telling you that it doesn’t like the equation not balanced with an ‘e-‘ to complete the mass and charge balance of the lithium ionization. There are other errors as well.

Second, you are trying to import get_odesys, and it is not exposed for import in kinetics/init.py. I don’t know which function you need, but that is where to look first.

cynthiarm17 commented 4 years ago

Thank you so much. I'm trying to write the equations for the Li-O2 battery. Li2CO3 is the electrolyte, and I'm trying to write the reactions that occur in the cathode, anode and electrolyte of the battery. I know this is too much to ask, but can you please show me where are my mistakes in the balance? image (I have not fixed the kinetics/init.py part)