title: Good code architecture for energy simulation problem?
body: <h1>Problem &amp; constraints</h1>
  
  <ul>
  <li><p>I want to build a simulation of multiple entities producing and consuming energy over time</p></li>
  <li><p>entities can have multiple incoming and outgoing energy streams</p></li>
  <li><p>one step in the simulation should simulate a discrete time interval, e.g. 5 minutes</p></li>
  <li><p>some entities in the simulation should be controllable from the outside world</p></li>
  <li><p>examples of entities include batteries, pv arrays and production machines </p></li>
  </ul>
  
  <h1>A first draft</h1>
  
  <p>My first naîve draft code goes like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>battery, machine, pv, grid = entities_from_data(...)
  
  def simulation_step():
      """
      execute one simulation step
      """
  
      for time in day:
  
          energy_battery = battery.get_energy()
          energy_pv = pv.get_energy()
  
          # run machine
          energy_consumption = machine.run()
  
          # let battery decide what it does
          # in the next step
          battery.charge_or_discharge(energy_pv, energy_consumption)
  
          # either buy or sell energy
          energy_diff = energy_battery + energy_pv - energy_consumption
          grid.buy_or_sell(energy_diff)
  </code></pre>
  
  <p>I don't like it:</p>
  
  <ul>
  <li>difficult to test</li>
  <li>not dynamic / modular</li>
  <li>state mutations are scattered all over the code</li>
  </ul>
  
  <h1>Possible architecture</h1>
  
  <p>One thing I thought of is creating a <code>Processor</code> base class which has, depending on its use, multiple input and output streams.</p>
  
  <p>One could maybe then compose such <code>Processor</code>s by connecting inputs with outputs, respectively. This would result in a kind of graph representing the energy flow.</p>
  
  <hr>
  
  <p>Is there any library / framework that is targeted at a similar problem?</p>
  
  <p>Do you know of any architecture that is more suitable to tackling this problem in python?</p>