bjodah / chempy

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

How to plot sum of concentrations (question)? #162

Closed Konjkov closed 4 years ago

Konjkov commented 4 years ago

I found this wonderful package ideal for modeling the kinetics of well known iodine clock reaction - https://arxiv.org/pdf/1808.06010.pdf """ H+ + OH- -> H2O; 1.4e+11 H2O -> H+ + OH-; 2.5e-5 I- + H2O2 -> HIO + OH-; 0.0115 I- + HIO -> I2 + OH-; 3.6e+9 I2 + OH- -> I- + HIO; 1.8e+11 I2 + I- -> I3-; 6.2e+9 I3- -> I2 + I-; 8.5e+6 I2 + C6H8O6 -> 2 H+ + 2 I- + C6H6O6; 1.2e+5 I3- + C6H8O6 -> 2 H+ + 3 I- + C6H6O6; 5.9e+5 """ However, due to reversibility in the reaction I- + I2 <-> I3- it is much more convenient to plot the total concentration iodine as sum of I2 and I3- it is possible to plot not only the concentration of individual reagents, but also the sum of concentration or difference? Best Vladimir.

niklastoe commented 4 years ago

I don't think there's a convenience function to do this but the following works for example https://github.com/bjodah/chempy#chemical-kinetics-system-of-ordinary-differential-equations

  1. Run the entire example code

  2. Now you can access result.yout which is a numpy.ndarray containing concentrations for every species (columns) at every timestep (rows). Select the two species you're interested in and add them summed_concentration = result.yout[:,index_A] + result.yout[:,index_B]

  3. Simple plot: plt.plot(summed_concentration)

bjodah commented 4 years ago

Thank you @niklastoe for helping out here! (and sorry @Konjkov for my radio silence, I have been, and am, quite busy with some projects taking nearly all of my time).