Skip to content

Commit

Permalink
added limiting reagent info
Browse files Browse the repository at this point in the history
  • Loading branch information
harirakul committed Sep 19, 2020
1 parent b407176 commit 800beb1
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 6 deletions.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/reactions.doctree
Binary file not shown.
26 changes: 25 additions & 1 deletion docs/build/html/_modules/chemlib/chemistry.html
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,31 @@ <h1>Source code for chemlib.chemistry</h1><div class="highlight"><pre>
<span class="n">amounts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">compound_list</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">get_amounts</span><span class="p">(</span><span class="n">moles</span> <span class="o">=</span> <span class="n">index_moles</span><span class="o">*</span><span class="n">multipliers</span><span class="p">[</span><span class="n">i</span><span class="p">]))</span>

<span class="n">amounts</span><span class="p">[</span><span class="n">compound_number</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">index_amounts</span>
<span class="k">return</span> <span class="n">amounts</span></div>
<span class="k">return</span> <span class="n">amounts</span>

<span class="k">def</span> <span class="nf">limiting_reagent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;grams&#39;</span><span class="p">):</span>
<span class="k">if</span> <span class="n">mode</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;grams&#39;</span><span class="p">,</span> <span class="s1">&#39;molecules&#39;</span><span class="p">,</span> <span class="s1">&#39;moles&#39;</span><span class="p">]:</span>
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;mode must be either grams, moles, or molecules. Default is grams&quot;</span><span class="p">)</span>

<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_balanced</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">balance</span><span class="p">()</span>

<span class="n">reactants</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">rformulas</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reactants</span><span class="p">:</span>
<span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">formula</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">rformulas</span><span class="p">:</span>
<span class="n">rformulas</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="o">.</span><span class="n">formula</span><span class="p">)</span>
<span class="n">reactants</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">reactants</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Expected {len(reactants)} arguments. The number of arguments should be equal to the number of reactants.&quot;</span><span class="p">)</span>

<span class="n">amounts</span> <span class="o">=</span> <span class="p">[</span><span class="n">reactants</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">get_amounts</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">mode</span><span class="p">:</span> <span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">]})</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">))]</span>
<span class="n">moles</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">[</span><span class="s1">&#39;Moles&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">amounts</span><span class="p">]</span>
<span class="n">chosen_product</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">products</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="n">eq_amounts</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">get_amounts</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">moles</span> <span class="o">=</span> <span class="n">moles</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">))]</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="n">mode</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()]</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">eq_amounts</span><span class="p">]</span>

<span class="k">return</span> <span class="p">(</span><span class="n">reactants</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">data</span><span class="p">)])</span></div>

<span class="k">class</span> <span class="nc">Combustion</span><span class="p">(</span><span class="n">Reaction</span><span class="p">):</span>

Expand Down
27 changes: 26 additions & 1 deletion docs/build/html/_sources/reactions.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,29 @@ Get the amounts of ALL compounds in the above reaction given 5 grams of N₂O₅
Get the amounts of ALL compounds in the above reaction given 3.5 moles of HNO₃. It is the third compound in the reaction by order of appearance (left to right).

>>> r.get_amounts(3, moles=3.5)
[{'Compound': 'N₂O₅', 'Grams': 189.018, 'Moles': 1.75, 'Molecules': 1.054e+24}, {'Compound': 'H₂O₁', 'Grams': 31.518, 'Moles': 1.75, 'Molecules': 1.054e+24}, {'Compound': 'H₁N₁O₃', 'Grams': 220.535, 'Moles': 3.5, 'Molecules': 2.107e+24}]
[{'Compound': 'N₂O₅', 'Grams': 189.018, 'Moles': 1.75, 'Molecules': 1.054e+24}, {'Compound': 'H₂O₁', 'Grams': 31.518, 'Moles': 1.75, 'Molecules': 1.054e+24}, {'Compound': 'H₁N₁O₃', 'Grams': 220.535, 'Moles': 3.5, 'Molecules': 2.107e+24}]

Limiting Reagent
----------------
.. py:function:: chemlib.chemistry.Reaction.limiting_reagent(self, *args, mode = 'grams')
Get the limiting reagent (limiting reactant) in the chemical reaction.

:param args: The amounts of each reactant to use in the chemical reaction.
:param str mode: The units of each amount in args. Default is grams, can also be moles or molecules.
:return: The limiting reagent of the reaction.
:rtype: chemlib.chemistry.Compound
:raises TypeError: If the number of args doesn't match the number of reactants in the reaction.
:raises ValueError: If the mode is not grams, moles, or molecules.

Find the limiting reagent of the reaction when using 50 grams of the first reactant (N₂O₅) and 80 grams of the second reactant (H₂O):

>>> lr = r.limiting_reagent(50, 50)
>>> lr.formula
'N₂O₅'

Find the limiting reagent of the reaction when using 3 moles of the first reactant (N₂O₅) and 1 mole of the second reactant (H₂O):

>>> lr = r.limiting_reagent(3, 1, mode = 'moles')
>>> lr.formula
'H₂O₁'
13 changes: 11 additions & 2 deletions docs/build/html/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ <h2 id="B">B</h2>
<li><a href="reactions.html#chemlib.chemistry.Reaction.balance">chemlib.chemistry.Reaction.balance()</a>
</li>
<li><a href="reactions.html#chemlib.chemistry.Reaction.get_amounts">chemlib.chemistry.Reaction.get_amounts()</a>
</li>
<li><a href="reactions.html#chemlib.chemistry.Reaction.limiting_reagent">chemlib.chemistry.Reaction.limiting_reagent()</a>
</li>
</ul></li>
</ul></td>
Expand Down Expand Up @@ -233,10 +235,10 @@ <h2 id="C">C</h2>
</li>
<li><a href="core.html#chemlib.chemistry.Element.Isotopes">chemlib.chemistry.Element.Isotopes (built-in variable)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="core.html#chemlib.chemistry.Element.MeltingPoint">chemlib.chemistry.Element.MeltingPoint (built-in variable)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="core.html#chemlib.chemistry.Element.Metal">chemlib.chemistry.Element.Metal (built-in variable)</a>
</li>
<li><a href="core.html#chemlib.chemistry.Element.Metalloid">chemlib.chemistry.Element.Metalloid (built-in variable)</a>
Expand Down Expand Up @@ -277,6 +279,13 @@ <h2 id="C">C</h2>
</ul></li>
<li><a href="reactions.html#chemlib.chemistry.Reaction.is_balanced">chemlib.chemistry.Reaction.is_balanced (built-in variable)</a>
</li>
<li>
chemlib.chemistry.Reaction.limiting_reagent()

<ul>
<li><a href="reactions.html#chemlib.chemistry.Reaction.limiting_reagent">built-in function</a>
</li>
</ul></li>
<li><a href="reactions.html#chemlib.chemistry.Reaction.product_formulas">chemlib.chemistry.Reaction.product_formulas (built-in variable)</a>
</li>
<li><a href="reactions.html#chemlib.chemistry.Reaction.reactant_formulas">chemlib.chemistry.Reaction.reactant_formulas (built-in variable)</a>
Expand Down
1 change: 1 addition & 0 deletions docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ <h2>Contents<a class="headerlink" href="#contents" title="Permalink to this head
<li class="toctree-l2"><a class="reference internal" href="reactions.html#combustion-reactions">Combustion Reactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="reactions.html#balancing-the-equation">Balancing the Equation</a></li>
<li class="toctree-l2"><a class="reference internal" href="reactions.html#stoichiometry">Stoichiometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="reactions.html#limiting-reagent">Limiting Reagent</a></li>
</ul>
</li>
</ul>
Expand Down
Binary file modified docs/build/html/objects.inv
Binary file not shown.
42 changes: 42 additions & 0 deletions docs/build/html/reactions.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<li class="toctree-l2"><a class="reference internal" href="#combustion-reactions">Combustion Reactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#balancing-the-equation">Balancing the Equation</a></li>
<li class="toctree-l2"><a class="reference internal" href="#stoichiometry">Stoichiometry</a></li>
<li class="toctree-l2"><a class="reference internal" href="#limiting-reagent">Limiting Reagent</a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -290,6 +291,47 @@ <h2>Stoichiometry<a class="headerlink" href="#stoichiometry" title="Permalink to
<span class="go">[{&#39;Compound&#39;: &#39;N₂O₅&#39;, &#39;Grams&#39;: 189.018, &#39;Moles&#39;: 1.75, &#39;Molecules&#39;: 1.054e+24}, {&#39;Compound&#39;: &#39;H₂O₁&#39;, &#39;Grams&#39;: 31.518, &#39;Moles&#39;: 1.75, &#39;Molecules&#39;: 1.054e+24}, {&#39;Compound&#39;: &#39;H₁N₁O₃&#39;, &#39;Grams&#39;: 220.535, &#39;Moles&#39;: 3.5, &#39;Molecules&#39;: 2.107e+24}]</span>
</pre></div>
</div>
</div>
<div class="section" id="limiting-reagent">
<h2>Limiting Reagent<a class="headerlink" href="#limiting-reagent" title="Permalink to this headline"></a></h2>
<dl class="py function">
<dt id="chemlib.chemistry.Reaction.limiting_reagent">
<code class="sig-prename descclassname">chemlib.chemistry.Reaction.</code><code class="sig-name descname">limiting_reagent</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">self</span></em>, <em class="sig-param"><span class="o">*</span><span class="n">args</span></em>, <em class="sig-param"><span class="n">mode</span><span class="o">=</span><span class="default_value">'grams'</span></em><span class="sig-paren">)</span><a class="headerlink" href="#chemlib.chemistry.Reaction.limiting_reagent" title="Permalink to this definition"></a></dt>
<dd><p>Get the limiting reagent (limiting reactant) in the chemical reaction.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>args</strong> – The amounts of each reactant to use in the chemical reaction.</p></li>
<li><p><strong>mode</strong> (<em>str</em>) – The units of each amount in args. Default is grams, can also be moles or molecules.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The limiting reagent of the reaction.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="compounds.html#chemlib.chemistry.Compound" title="chemlib.chemistry.Compound">chemlib.chemistry.Compound</a></p>
</dd>
<dt class="field-even">Raises</dt>
<dd class="field-even"><ul class="simple">
<li><p><strong>TypeError</strong> – If the number of args doesn’t match the number of reactants in the reaction.</p></li>
<li><p><strong>ValueError</strong> – If the mode is not grams, moles, or molecules.</p></li>
</ul>
</dd>
</dl>
<p>Find the limiting reagent of the reaction when using 50 grams of the first reactant (N₂O₅) and 80 grams of the second reactant (H₂O):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">limiting_reagent</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span><span class="o">.</span><span class="n">formula</span>
<span class="go">&#39;N₂O₅&#39;</span>
</pre></div>
</div>
<p>Find the limiting reagent of the reaction when using 3 moles of the first reactant (N₂O₅) and 1 mole of the second reactant (H₂O):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">limiting_reagent</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">mode</span> <span class="o">=</span> <span class="s1">&#39;moles&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lr</span><span class="o">.</span><span class="n">formula</span>
<span class="go">&#39;H₂O₁&#39;</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>

Expand Down
Loading

0 comments on commit 800beb1

Please sign in to comment.