Skip to content

Commit

Permalink
fix(des): Make it easier to regenerate Modelica after editing sys params
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey committed Jan 19, 2025
1 parent 7c55b95 commit bcfa447
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dragonfly_grasshopper/json/DF_Write_Modelica_DES.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.8.3",
"version": "1.8.4",
"nickname": "WriteDES",
"outputs": [
[
Expand Down Expand Up @@ -43,7 +43,7 @@
}
],
"subcategory": "5 :: District Thermal",
"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.config import folders as df_folders\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, give_warning\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nUO_GMT_VERSION = '0.9.1'\nUO_TN_VERSION = '0.3.2'\nMBL_VERSION = '11.0.0'\n\n\nif all_required_inputs(ghenv.Component) and _write:\n # set up the custom python environment\n custom_env = os.environ.copy()\n custom_env['PYTHONHOME'] = ''\n\n # set global 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 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(\n pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)\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(\n pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)\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(\n pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)\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",
"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.config import folders as df_folders\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, give_warning\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\nUO_GMT_VERSION = '0.9.1'\nUO_TN_VERSION = '0.3.2'\nMBL_VERSION = '11.0.0'\n\n\nif all_required_inputs(ghenv.Component) and _write:\n # set up the custom python environment\n custom_env = os.environ.copy()\n custom_env['PYTHONHOME'] = ''\n\n # set global 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 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(\n pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)\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(\n pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)\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(\n pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)\n stderr = process.communicate()\n\n # check the various files in the project folder\n proj_dir = os.path.dirname(_geojson)\n scn_name = os.path.basename(_scenario).replace('.csv', '')\n des_dir = os.path.join(proj_dir, 'run', scn_name, 'des_modelica')\n sys_param = os.path.join(proj_dir, 'system_params.json')\n\n # run the command that adds the building loads to the system parameters\n if not os.path.isdir(des_dir):\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 Write Modelica DES",
"description": "Use an URBANopt geoJSON with a Distric Energy System (DES) loop assigned to it\nalong with the corresponding scenario (containing building loads) to generate\na Modelica model of the district system.\n_\nThe model is generated using the modules of the Modelica Buildings Library (MBL).\nMore information on the MBL can be found here:\nhttps://simulationresearch.lbl.gov/modelica/\n_\nThe Modelica model produced by this component can be opened and edited in any\nof the standard Modelica interfaces (eg. Dymola) or it can be simulated with\nOpenModelica inside a Docker image using the \"DF Run Modelica\" component.\n-"
Expand Down
11 changes: 9 additions & 2 deletions dragonfly_grasshopper/src/DF Write Modelica DES.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

ghenv.Component.Name = 'DF Write Modelica DES'
ghenv.Component.NickName = 'WriteDES'
ghenv.Component.Message = '1.8.3'
ghenv.Component.Message = '1.8.4'
ghenv.Component.Category = 'Dragonfly'
ghenv.Component.SubCategory = '5 :: District Thermal'
ghenv.Component.AdditionalHelpFromDocStrings = '1'
Expand Down Expand Up @@ -156,8 +156,15 @@
pip_cmd, stderr=subprocess.PIPE, shell=shell, env=custom_env)
stderr = process.communicate()

# check the various files in the project folder
proj_dir = os.path.dirname(_geojson)
scn_name = os.path.basename(_scenario).replace('.csv', '')
des_dir = os.path.join(proj_dir, 'run', scn_name, 'des_modelica')
sys_param = os.path.join(proj_dir, 'system_params.json')

# run the command that adds the building loads to the system parameters
sys_param = run_des_sys_param(_geojson, _scenario)
if not os.path.isdir(des_dir):
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 Write Modelica DES.ghuser
Binary file not shown.
Binary file modified samples/ghe_custom_loads.gh
Binary file not shown.
Binary file modified samples/ghe_example.gh
Binary file not shown.
Binary file modified samples/ghe_multiple_example.gh
Binary file not shown.

0 comments on commit bcfa447

Please sign in to comment.