|
|
Line 343: |
Line 343: |
| </div></div></div></div> | | </div></div></div></div> |
| | | |
− | <div class="second-section">
| |
− | <div class="container">
| |
− | <div class="row">
| |
− | <div class="col-md-12 col-centered">
| |
− | <h3>ODE Generator</h3>
| |
− |
| |
− | <div class="highlight"><pre><span class="k">class</span> <span class="nc">Reaction</span><span class="p">:</span>
| |
− | <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reaction_</span><span class="p">,</span> <span class="n">cst_name_</span><span class="p">,</span> <span class="n">cst_value_</span><span class="p">,</span> <span class="n">description_</span><span class="p">):</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">reaction</span> <span class="o">=</span> <span class="n">reaction_</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">cst_name</span> <span class="o">=</span> <span class="n">cst_name_</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">cst_value</span> <span class="o">=</span> <span class="n">cst_value_</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="n">description_</span>
| |
− |
| |
− | <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
| |
− | <span class="k">return</span> <span class="s">"{}</span><span class="se">\n</span><span class="s">{} = {}</span><span class="se">\n</span><span class="s">{}"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reaction</span><span class="p">,</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">cst_name</span><span class="p">,</span>
| |
− | <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cst_value</span><span class="p">),</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">description</span><span class="p">)</span>
| |
− |
| |
− | <span class="k">class</span> <span class="nc">ReactionParser</span><span class="p">:</span>
| |
− |
| |
− | <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_</span><span class="p">,</span> <span class="n">sep_</span><span class="o">=</span><span class="s">":"</span><span class="p">,</span> <span class="n">comment_</span><span class="o">=</span><span class="s">"#"</span><span class="p">):</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">sep</span> <span class="o">=</span> <span class="n">sep_</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">comment</span> <span class="o">=</span> <span class="n">comment_</span>
| |
− |
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">input_</span><span class="p">,</span> <span class="s">'r'</span><span class="p">)</span> <span class="c"># TODO: OPEN SAFELY</span>
| |
− |
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">reactions</span> <span class="o">=</span> <span class="p">[]</span>
| |
− |
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">_load</span><span class="p">()</span>
| |
− |
| |
− | <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
| |
− |
| |
− | <span class="k">def</span> <span class="nf">_load</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
| |
− | <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">file</span><span class="p">:</span>
| |
− | <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span> <span class="ow">or</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">""</span><span class="p">:</span> <span class="c"># Check for empty lines</span>
| |
− | <span class="k">continue</span>
| |
− |
| |
− | <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">comment</span><span class="p">:</span> <span class="c"># Check for comments</span>
| |
− | <span class="k">continue</span>
| |
− |
| |
− | <span class="n">splitted</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">":"</span><span class="p">)</span>
| |
− |
| |
− | <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">splitted</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">4</span><span class="p">:</span>
| |
− | <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">'ReactionParser: problem with the input file (not enough arguments)'</span><span class="p">)</span>
| |
− |
| |
− | <span class="n">reaction</span> <span class="o">=</span> <span class="n">Reaction</span><span class="p">(</span><span class="n">splitted</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(),</span>
| |
− | <span class="n">splitted</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">(),</span>
| |
− | <span class="nb">float</span><span class="p">(</span><span class="n">splitted</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()),</span>
| |
− | <span class="n">splitted</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
| |
− |
| |
− | <span class="bp">self</span><span class="o">.</span><span class="n">reactions</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reaction</span><span class="p">)</span>
| |
− |
| |
− | <span class="k">def</span> <span class="nf">print_reactions</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
| |
− | <span class="k">for</span> <span class="n">reaction</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reactions</span><span class="p">:</span>
| |
− | <span class="k">print</span><span class="p">(</span><span class="n">reaction</span><span class="p">)</span>
| |
− | <span class="k">print</span><span class="p">()</span>
| |
− |
| |
− | <span class="k">def</span> <span class="nf">reaction_table</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
| |
− | <span class="n">kin</span> <span class="o">=</span> <span class="p">[]</span>
| |
− |
| |
− | <span class="k">for</span> <span class="n">reaction</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reactions</span><span class="p">:</span>
| |
− | <span class="n">splitted</span> <span class="o">=</span> <span class="n">reaction</span><span class="o">.</span><span class="n">reaction</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">"->"</span><span class="p">)</span>
| |
− |
| |
− | <span class="n">left</span> <span class="o">=</span> <span class="n">splitted</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
| |
− | <span class="n">right</span> <span class="o">=</span> <span class="n">splitted</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
| |
− |
| |
− | <span class="n">kin</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="p">([</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">],</span> <span class="p">(</span><span class="n">reaction</span><span class="o">.</span><span class="n">cst_name</span><span class="p">,</span> <span class="n">reaction</span><span class="o">.</span><span class="n">description</span><span class="p">,</span> <span class="n">reaction</span><span class="o">.</span><span class="n">cst_value</span><span class="p">)</span> <span class="p">)</span> <span class="p">)</span>
| |
− |
| |
− | <span class="k">return</span> <span class="n">kin</span>
| |
− |
| |
− |
| |
− |
| |
− | <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
| |
− | <span class="n">myParser</span> <span class="o">=</span> <span class="n">ReactionParser</span><span class="p">(</span><span class="s">"reactions.dat"</span><span class="p">)</span>
| |
− |
| |
− | <span class="c">#myParser.load()</span>
| |
− | <span class="n">myParser</span><span class="o">.</span><span class="n">print_reactions</span><span class="p">()</span>
| |
− |
| |
− | <span class="n">kin</span> <span class="o">=</span> <span class="n">myParser</span><span class="o">.</span><span class="n">reaction_table</span><span class="p">()</span>
| |
− |
| |
− | <span class="k">print</span><span class="p">(</span><span class="n">kin</span><span class="p">)</span>
| |
− | </pre></div>
| |
| | | |
− |
| |
− | </div>
| |
− | </div>
| |
− | </div>
| |
− | </div>
| |
| | | |
| | | |