|
| 1 | +# EnergyPlus, Copyright (c) 1996-2024, The Board of Trustees of the University |
| 2 | +# of Illinois, The Regents of the University of California, through Lawrence |
| 3 | +# Berkeley National Laboratory (subject to receipt of any required approvals |
| 4 | +# from the U.S. Dept. of Energy), Oak Ridge National Laboratory, managed by UT- |
| 5 | +# Battelle, Alliance for Sustainable Energy, LLC, and other contributors. All |
| 6 | +# rights reserved. |
| 7 | +# |
| 8 | +# NOTICE: This Software was developed under funding from the U.S. Department of |
| 9 | +# Energy and the U.S. Government consequently retains certain rights. As such, |
| 10 | +# the U.S. Government has been granted for itself and others acting on its |
| 11 | +# behalf a paid-up, nonexclusive, irrevocable, worldwide license in the |
| 12 | +# Software to reproduce, distribute copies to the public, prepare derivative |
| 13 | +# works, and perform publicly and display publicly, and to permit others to do |
| 14 | +# so. |
| 15 | +# |
| 16 | +# Redistribution and use in source and binary forms, with or without |
| 17 | +# modification, are permitted provided that the following conditions are met: |
| 18 | +# |
| 19 | +# (1) Redistributions of source code must retain the above copyright notice, |
| 20 | +# this list of conditions and the following disclaimer. |
| 21 | +# |
| 22 | +# (2) Redistributions in binary form must reproduce the above copyright notice, |
| 23 | +# this list of conditions and the following disclaimer in the documentation |
| 24 | +# and/or other materials provided with the distribution. |
| 25 | +# |
| 26 | +# (3) Neither the name of the University of California, Lawrence Berkeley |
| 27 | +# National Laboratory, the University of Illinois, U.S. Dept. of Energy nor |
| 28 | +# the names of its contributors may be used to endorse or promote products |
| 29 | +# derived from this software without specific prior written permission. |
| 30 | +# |
| 31 | +# (4) Use of EnergyPlus(TM) Name. If Licensee (i) distributes the software in |
| 32 | +# stand-alone form without changes from the version obtained under this |
| 33 | +# License, or (ii) Licensee makes a reference solely to the software |
| 34 | +# portion of its product, Licensee must refer to the software as |
| 35 | +# "EnergyPlus version X" software, where "X" is the version number Licensee |
| 36 | +# obtained under this License and may not use a different name for the |
| 37 | +# software. Except as specifically required in this Section (4), Licensee |
| 38 | +# shall not use in a company name, a product name, in advertising, |
| 39 | +# publicity, or other promotional activities any name, trade name, |
| 40 | +# trademark, logo, or other designation of "EnergyPlus", "E+", "e+" or |
| 41 | +# confusingly similar designation, without the U.S. Department of Energy's |
| 42 | +# prior written consent. |
| 43 | +# |
| 44 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 45 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 46 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 47 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 48 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 49 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 50 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 51 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 52 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 53 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 54 | +# POSSIBILITY OF SUCH DAMAGE. |
| 55 | + |
| 56 | +from pathlib import Path |
| 57 | +from queue import Queue |
| 58 | +from sys import argv, path |
| 59 | +from tempfile import mkdtemp |
| 60 | +from threading import Thread |
| 61 | +from tkinter import Tk, Button, StringVar, Label, BOTH |
| 62 | + |
| 63 | + |
| 64 | +class EPGui(Tk): |
| 65 | + def __init__(self): |
| 66 | + super().__init__() |
| 67 | + if len(argv) == 3: # if running in a source tree, pass in the build directory and the IDF to run |
| 68 | + self.install_or_build_dir: Path = Path(argv[1]) / 'Products' |
| 69 | + self.example_file_to_run: str = argv[2] |
| 70 | + else: # if running in an installation, it will live at the installation root, and you don't need any args |
| 71 | + self.install_or_build_dir: Path = Path(__file__).resolve().parent |
| 72 | + self.example_file_to_run: str = str(self.install_or_build_dir / 'ExampleFiles' / '5ZoneAirCooled.idf') |
| 73 | + path.insert(0, str(self.install_or_build_dir)) |
| 74 | + # noinspection PyUnresolvedReferences |
| 75 | + from pyenergyplus.api import EnergyPlusAPI |
| 76 | + self.api = EnergyPlusAPI() |
| 77 | + self.title("API/GUI Demonstration") |
| 78 | + Button(self, text='Run EnergyPlus', command=lambda: Thread(target=self._run_ep, daemon=True).start()).pack() |
| 79 | + self._tk_var_message = StringVar(value="<E+ Outputs>") |
| 80 | + Label(self, textvariable=self._tk_var_message).pack(fill=BOTH, expand=True) |
| 81 | + self.geometry('400x100') |
| 82 | + self._gui_queue = Queue() |
| 83 | + self._check_queue() |
| 84 | + self.mainloop() |
| 85 | + |
| 86 | + def _check_queue(self) -> None: |
| 87 | + while True: |
| 88 | + # noinspection PyBroadException |
| 89 | + try: |
| 90 | + task = self._gui_queue.get(block=False) |
| 91 | + self.after_idle(task) |
| 92 | + except Exception: |
| 93 | + break |
| 94 | + self.after(80, self._check_queue) |
| 95 | + |
| 96 | + def _run_ep(self) -> None: |
| 97 | + run_directory = mkdtemp() |
| 98 | + state = self.api.state_manager.new_state() |
| 99 | + self.api.runtime.callback_message(state, lambda x: self._gui_queue.put(lambda: self._tk_var_message.set(x))) |
| 100 | + self.api.runtime.run_energyplus(state, ['-d', run_directory, '-D', self.example_file_to_run]) |
| 101 | + print(f"Finished running EnergyPlus, results available in {run_directory}") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + EPGui() |
0 commit comments