Skip to content

Commit

Permalink
fix(energy): Add support for generating Modelica files
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey committed Sep 6, 2023
1 parent 4c7cf0b commit b471c31
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
17 changes: 12 additions & 5 deletions dragonfly_grasshopper/json/DF_Run_Modelica_DES.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"version": "1.6.0",
"version": "1.6.1",
"nickname": "RunDES",
"outputs": [
[
{
"access": "None",
"name": "sys_param",
"description": "A JSON file containing all of the specifications of the District\nEnergy System, including the detailed Building load profiles.",
"type": null,
"default": null
},
{
"access": "None",
"name": "modelica",
Expand All @@ -29,15 +36,15 @@
},
{
"access": "item",
"name": "_run",
"description": "Set to \"True\" to simulate the Distric Energy System.",
"name": "_write",
"description": "Set to \"True\" to run the component, install any missing dependencies,\nand write the Modelica files for the Distric Energy System.",
"type": "bool",
"default": null
}
],
"subcategory": "3 :: Energy",
"code": "\nimport os\nimport subprocess\n\ntry:\n from ladybug.futil import nukedir\n from ladybug.config import folders as lb_folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry:\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the dragonfly_energy dependencies\n from dragonfly_energy.run import run_default_report\nexcept ImportError as e:\n raise ImportError('\\nFailed to import dragonfly_energy:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.download import download_file_by_name\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nUO_GMT_VERSION = '0.6.0rc1'\nUO_TN_VERSION = '0.2.1'\nMBL_VERSION = '9.1.1'\n\n\nif all_required_inputs(ghenv.Component) and _run:\n # set clobal values\n ext = '.exe' if os.name == 'nt' else ''\n executor_path = os.path.join(\n lb_folders.ladybug_tools_folder, '{{plugin}}',\n 'ladybug_{{plugin}}_dotnet', 'Ladybug.Executor.exe')\n\n # check to see if the geojson-modelica-translator is installed\n uo_gmt = '{}/uo_des{}'.format(folders.python_scripts_path, ext)\n uo_gmt_pack = '{}/geojson_modelica_translator-{}.dist-info'.format(\n folders.python_package_path, UO_GMT_VERSION)\n if not os.path.isfile(uo_gmt) or not os.path.isdir(uo_gmt_pack):\n #install_cmd = 'pip install geojson-modelica-translator=={}'.format(UO_GMT_VERSION)\n install_cmd = 'pip install git+https://github.com/urbanopt/geojson-modelica-translator@develop'\n if os.name == 'nt' and os.path.isfile(executor_path) and \\\n 'Program Files' in executor_path:\n pip_cmd = [\n executor_path, folders.python_exe_path, '-m {}'.format(install_cmd)\n ]\n else:\n pip_cmd = '\"{py_exe}\" -m {uo_cmd}'.format(\n py_exe=folders.python_exe_path, uo_cmd=install_cmd)\n shell = True if os.name == 'nt' else False\n process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n # check to see if the ThermalNetwork package is installed\n uo_tn = '{}/thermalnetwork{}'.format(folders.python_scripts_path, ext)\n uo_tn_pack = '{}/ThermalNetwork-{}.dist-info'.format(\n folders.python_package_path, UO_TN_VERSION)\n if not os.path.isfile(uo_tn) or not os.path.isdir(uo_tn_pack):\n install_cmd = 'pip install thermalnetwork=={}'.format(UO_TN_VERSION)\n if os.name == 'nt' and os.path.isfile(executor_path) and \\\n 'Program Files' in executor_path:\n pip_cmd = [\n executor_path, folders.python_exe_path, '-m {}'.format(install_cmd)\n ]\n else:\n pip_cmd = '\"{py_exe}\" -m {uo_cmd}'.format(\n py_exe=folders.python_exe_path, uo_cmd=install_cmd)\n shell = True if os.name == 'nt' else False\n process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n # check to see if the Modelica Buildings Library is installed\n install_directory = os.path.join(lb_folders.ladybug_tools_folder, 'resources')\n final_dir = os.path.join(install_directory, 'mbl')\n version_file = os.path.join(final_dir, 'version.txt')\n already_installed = False\n if os.path.isdir(final_dir) and os.path.isfile(version_file):\n with open(version_file, 'r') as vf:\n install_version = vf.read()\n if install_version == MBL_VERSION:\n already_installed = True\n else:\n nukedir(final_dir, True)\n if not already_installed:\n install_cmd = 'dragonfly_energy install mbl --version {}'.format(MBL_VERSION)\n if os.name == 'nt' and os.path.isfile(executor_path) and \\\n 'Program Files' in executor_path:\n pip_cmd = [\n executor_path, folders.python_exe_path, '-m {}'.format(install_cmd)\n ]\n else:\n pip_cmd = '\"{py_exe}\" -m {uo_cmd}'.format(\n py_exe=folders.python_exe_path, uo_cmd=install_cmd)\n shell = True if os.name == 'nt' else False\n process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n \"\"\"\n # delete any existing files in the result folder\n scen_name = os.path.basename(_scenario).replace('.csv', '')\n run_folder = os.path.join(os.path.dirname(_geojson), 'run', scen_name)\n result_folder = os.path.join(run_folder, 'modelica')\n nukedir(result_folder)\n\n # prepare the Modelica-running command\n command = '\"{uo_ditto}\" run-opendss -f \"{feature_file}\" ' \\\n '-s \"{scenario_file}\"'.format(\n uo_ditto=uo_ditto, feature_file=_geojson, scenario_file=_scenario)\n\n # execute the command to run everything through Modelica\n shell = False if os.name == 'nt' else True\n process = subprocess.Popen(command, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n # gather together all of the result files\n bldg_folder = os.path.join(result_folder, 'results', 'Features')\n \"\"\"\n",
"code": "\nimport os\nimport subprocess\n\ntry:\n from ladybug.futil import nukedir\n from ladybug.config import folders as lb_folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry:\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry: # import the dragonfly_energy dependencies\n from dragonfly_energy.run import run_des_sys_param, run_des_modelica\nexcept ImportError as e:\n raise ImportError('\\nFailed to import dragonfly_energy:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.download import download_file_by_name\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nUO_GMT_VERSION = '0.6.0rc1'\nUO_TN_VERSION = '0.2.1'\nMBL_VERSION = '10.0.0'\n\n\nif all_required_inputs(ghenv.Component) and _write:\n # set clobal values\n ext = '.exe' if os.name == 'nt' else ''\n executor_path = os.path.join(\n lb_folders.ladybug_tools_folder, '{{plugin}}',\n 'ladybug_{{plugin}}_dotnet', 'Ladybug.Executor.exe')\n\n # check to see if the geojson-modelica-translator is installed\n uo_gmt = '{}/uo_des{}'.format(folders.python_scripts_path, ext)\n uo_gmt_pack = '{}/geojson_modelica_translator-{}.dist-info'.format(\n folders.python_package_path, UO_GMT_VERSION)\n if not os.path.isfile(uo_gmt) or not os.path.isdir(uo_gmt_pack):\n #install_cmd = 'pip install geojson-modelica-translator=={}'.format(UO_GMT_VERSION)\n install_cmd = 'pip install git+https://github.com/urbanopt/geojson-modelica-translator@develop'\n if os.name == 'nt' and os.path.isfile(executor_path) and \\\n 'Program Files' in executor_path:\n pip_cmd = [\n executor_path, folders.python_exe_path, '-m {}'.format(install_cmd)\n ]\n else:\n pip_cmd = '\"{py_exe}\" -m {uo_cmd}'.format(\n py_exe=folders.python_exe_path, uo_cmd=install_cmd)\n shell = True if os.name == 'nt' else False\n process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n # check to see if the ThermalNetwork package is installed\n uo_tn = '{}/thermalnetwork{}'.format(folders.python_scripts_path, ext)\n uo_tn_pack = '{}/ThermalNetwork-{}.dist-info'.format(\n folders.python_package_path, UO_TN_VERSION)\n if not os.path.isfile(uo_tn) or not os.path.isdir(uo_tn_pack):\n #install_cmd = 'pip install thermalnetwork=={}'.format(UO_TN_VERSION)\n install_cmd = 'pip install git+https://github.com/NREL/ThermalNetwork'\n if os.name == 'nt' and os.path.isfile(executor_path) and \\\n 'Program Files' in executor_path:\n pip_cmd = [\n executor_path, folders.python_exe_path, '-m {}'.format(install_cmd)\n ]\n else:\n pip_cmd = '\"{py_exe}\" -m {uo_cmd}'.format(\n py_exe=folders.python_exe_path, uo_cmd=install_cmd)\n shell = True if os.name == 'nt' else False\n process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n # check to see if the Modelica Buildings Library is installed\n install_directory = os.path.join(lb_folders.ladybug_tools_folder, 'resources')\n final_dir = os.path.join(install_directory, 'mbl')\n version_file = os.path.join(final_dir, 'version.txt')\n already_installed = False\n if os.path.isdir(final_dir) and os.path.isfile(version_file):\n with open(version_file, 'r') as vf:\n install_version = vf.read()\n if install_version == MBL_VERSION:\n already_installed = True\n else:\n nukedir(final_dir, True)\n if not already_installed:\n install_cmd = 'dragonfly_energy install mbl --version {}'.format(MBL_VERSION)\n if os.name == 'nt' and os.path.isfile(executor_path) and \\\n 'Program Files' in executor_path:\n pip_cmd = [\n executor_path, folders.python_exe_path, '-m {}'.format(install_cmd)\n ]\n else:\n pip_cmd = '\"{py_exe}\" -m {uo_cmd}'.format(\n py_exe=folders.python_exe_path, uo_cmd=install_cmd)\n shell = True if os.name == 'nt' else False\n process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)\n stderr = process.communicate()\n\n # run the command that adds the building loads to the system parameters\n sys_param = run_des_sys_param(_geojson, _scenario)\n\n # run the command that generates the modelica files\n modelica = run_des_modelica(sys_param, _geojson, _scenario)\n",
"category": "Dragonfly",
"name": "DF Run Modelica DES",
"description": "Run a an URBANopt geoJSON and scenario through Modelica DES simulation.\n_\nThe geoJSON must have a valid DES Loop assigned to it in order to run correctly\nthrough Modelica DES simulation.\n-"
"description": "Run a an URBANopt geoJSON and scenario through Modelica Distric Energy System\n(DES) simulation.\n_\nThe geoJSON must have a valid DES Loop assigned to it in order to run correctly\nthrough Modelica DES simulation.\n-"
}
44 changes: 17 additions & 27 deletions dragonfly_grasshopper/src/DF Run Modelica DES.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
# @license AGPL-3.0-or-later <https://spdx.org/licenses/AGPL-3.0-or-later>

"""
Run a an URBANopt geoJSON and scenario through Modelica DES simulation.
Run a an URBANopt geoJSON and scenario through Modelica Distric Energy System
(DES) simulation.
_
The geoJSON must have a valid DES Loop assigned to it in order to run correctly
through Modelica DES simulation.
Expand All @@ -21,17 +22,20 @@
assigned to it in order to run correctly through the DES simulation.
_scenario: The path to an URBANopt .csv file for the scenario. This CSV
file can be obtained form the "DF Run URBANopt" component.
_run: Set to "True" to simulate the Distric Energy System.
_write: Set to "True" to run the component, install any missing dependencies,
and write the Modelica files for the Distric Energy System.
Returns:
report: Reports, errors, warnings, etc.
sys_param: A JSON file containing all of the specifications of the District
Energy System, including the detailed Building load profiles.
modelica: A folder where all of the Modelica files of the District Energy
System (DES) are written.
"""

ghenv.Component.Name = 'DF Run Modelica DES'
ghenv.Component.NickName = 'RunDES'
ghenv.Component.Message = '1.6.0'
ghenv.Component.Message = '1.6.1'
ghenv.Component.Category = 'Dragonfly'
ghenv.Component.SubCategory = '3 :: Energy'
ghenv.Component.AdditionalHelpFromDocStrings = '0'
Expand All @@ -51,7 +55,7 @@
raise ImportError('\nFailed to import honeybee:\n\t{}'.format(e))

try: # import the dragonfly_energy dependencies
from dragonfly_energy.run import run_default_report
from dragonfly_energy.run import run_des_sys_param, run_des_modelica
except ImportError as e:
raise ImportError('\nFailed to import dragonfly_energy:\n\t{}'.format(e))

Expand All @@ -63,10 +67,10 @@

UO_GMT_VERSION = '0.6.0rc1'
UO_TN_VERSION = '0.2.1'
MBL_VERSION = '9.1.1'
MBL_VERSION = '10.0.0'


if all_required_inputs(ghenv.Component) and _run:
if all_required_inputs(ghenv.Component) and _write:
# set clobal values
ext = '.exe' if os.name == 'nt' else ''
executor_path = os.path.join(
Expand Down Expand Up @@ -97,7 +101,8 @@
uo_tn_pack = '{}/ThermalNetwork-{}.dist-info'.format(
folders.python_package_path, UO_TN_VERSION)
if not os.path.isfile(uo_tn) or not os.path.isdir(uo_tn_pack):
install_cmd = 'pip install thermalnetwork=={}'.format(UO_TN_VERSION)
#install_cmd = 'pip install thermalnetwork=={}'.format(UO_TN_VERSION)
install_cmd = 'pip install git+https://github.com/NREL/ThermalNetwork'
if os.name == 'nt' and os.path.isfile(executor_path) and \
'Program Files' in executor_path:
pip_cmd = [
Expand Down Expand Up @@ -136,23 +141,8 @@
process = subprocess.Popen(pip_cmd, stderr=subprocess.PIPE, shell=shell)
stderr = process.communicate()

"""
# delete any existing files in the result folder
scen_name = os.path.basename(_scenario).replace('.csv', '')
run_folder = os.path.join(os.path.dirname(_geojson), 'run', scen_name)
result_folder = os.path.join(run_folder, 'modelica')
nukedir(result_folder)
# prepare the Modelica-running command
command = '"{uo_ditto}" run-opendss -f "{feature_file}" ' \
'-s "{scenario_file}"'.format(
uo_ditto=uo_ditto, feature_file=_geojson, scenario_file=_scenario)
# execute the command to run everything through Modelica
shell = False if os.name == 'nt' else True
process = subprocess.Popen(command, stderr=subprocess.PIPE, shell=shell)
stderr = process.communicate()
# gather together all of the result files
bldg_folder = os.path.join(result_folder, 'results', 'Features')
"""
# run the command that adds the building loads to the system parameters
sys_param = run_des_sys_param(_geojson, _scenario)

# run the command that generates the modelica files
modelica = run_des_modelica(sys_param, _geojson, _scenario)
Binary file modified dragonfly_grasshopper/user_objects/DF Run Modelica DES.ghuser
Binary file not shown.
Binary file added samples/des_example.gh
Binary file not shown.
Binary file modified samples/ghe_example.gh
Binary file not shown.

0 comments on commit b471c31

Please sign in to comment.