-
Notifications
You must be signed in to change notification settings - Fork 10
/
uo_des.py
199 lines (169 loc) · 6.88 KB
/
uo_des.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# :copyright (c) URBANopt, Alliance for Sustainable Energy, LLC, and other contributors.
# See also https://github.com/urbanopt/geojson-modelica-translator/blob/develop/LICENSE.md
from pathlib import Path
from shutil import rmtree
import click
from geojson_modelica_translator.geojson_modelica_translator import (
GeoJsonModelicaTranslator
)
from geojson_modelica_translator.modelica.modelica_runner import ModelicaRunner
from geojson_modelica_translator.system_parameters.system_parameters import (
SystemParameters
)
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.group(context_settings=CONTEXT_SETTINGS)
def cli():
"""URBANopt District Energy Systems"""
@cli.command(short_help="Create sys-param file")
@click.argument(
"scenario_file",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.argument(
"feature_file",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.argument(
"model_type",
default='time_series',
)
@click.option(
'-o',
'--overwrite',
is_flag=True,
help="Delete and replace any existing file of the same name & location",
default=False
)
@click.option(
'-m',
'--microgrid',
is_flag=True,
help="If specified, microgrid inputs will be added to system parameters file",
default=False
)
@click.option(
'-g',
'--ghe',
is_flag=True,
help="If specified, Ground Heat Exchanger properties will be added to System Parameters File. HELLO",
default=False
)
def build_sys_param(model_type: str, scenario_file: Path, feature_file: Path, ghe: bool, overwrite: bool, microgrid: bool):
"""
Create system parameters file using uo_sdk output
SCENARIO_FILE: Path to sdk scenario file.
FEATURE_FILE: Path to sdk json feature file with data about the buildings.
\b
MODEL_TYPE: selection for which kind of simulation this sys-param file will support.
Valid choices for MODEL_TYPE: "time_series"
\f
:param model_type: string, selection of which model type to use in the GMT
:param ghe: Boolean, flag to add Ground Heat Exchanger properties to System Parameter File
:param scenario_file: Path, location of SDK scenario_file
:param feature_file: Path, location of SDK feature_file
:param overwrite: Boolean, flag to overwrite an existing file of the same name/location
"""
# Use scenario_file to be consistent with sdk
scenario_name = Path(scenario_file).stem
scenario_dir = Path(scenario_file).parent / 'run' / scenario_name
sys_param_filepath = Path(scenario_dir / 'system_parameter.json')
sp = SystemParameters()
sp.csv_to_sys_param(
model_type=model_type,
sys_param_filename=Path(sys_param_filepath),
scenario_dir=Path(scenario_dir),
feature_file=Path(feature_file),
ghe=ghe,
overwrite=overwrite,
microgrid=microgrid
)
if Path(sys_param_filepath).exists():
print(f"\nSystem parameters file {sys_param_filepath} successfully created.")
else:
raise SystemExit(f"{sys_param_filepath} failed. Please check your inputs and try again.")
@cli.command(short_help="Create Modelica model")
@click.argument(
"sys_param_file",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.argument(
"geojson_feature_file",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
)
@click.argument(
"project_path",
default="model_from_sdk",
type=click.Path(exists=False, file_okay=False, dir_okay=True),
)
@click.option(
'-o',
'--overwrite',
is_flag=True,
help="Delete and replace any existing folder of the same name & location",
default=False
)
def create_model(sys_param_file: Path, geojson_feature_file: Path, project_path: Path, overwrite: bool):
"""Build Modelica model from user data
SYS_PARAM_FILE: Path/name to sys-param file, possibly created with this CLI.
GEOJSON_FEATURE_FILE: Path to sdk json feature file with data about the buildings.
PROJECT_PATH: Path for Modelica project directory created with this command
\f
:param model_type: String, type of model to create
:param sys_param_file: Path, location and name of file created with this cli
:param geojson_feature_file: Path, location and name of sdk feature_file
:param project_path: Path, location and name of Modelica model dir to be created
:param overwrite: Boolean, flag to overwrite an existing file of the same name/location
"""
project_path = Path(project_path)
if project_path.exists():
if overwrite:
rmtree(project_path, ignore_errors=True)
else:
raise SystemExit(f"Output dir '{project_path}' already exists and overwrite flag is not given")
if len(project_path.name.split()) > 1: # Modelica can't handle spaces in project name
raise SystemExit(
f"\n'{project_path}' failed. Modelica does not support spaces in project names or paths. "
"Please choose a different 'project_path'")
gmt = GeoJsonModelicaTranslator(
geojson_feature_file,
sys_param_file,
project_path.parent,
project_path.name
)
gmt.to_modelica()
@cli.command(short_help="Run Modelica model")
@click.argument(
"modelica_project",
default="./model_from_sdk",
required=True,
type=click.Path(exists=True, file_okay=False, dir_okay=True),
)
def run_model(modelica_project: Path):
"""
\b
Run the Modelica project in a docker-based environment.
Results are saved at the same level as the project path that is passed.
The model that runs is hard coded to be the Districts/DistrictEnergySystem.mo within the package.
\b
MODELICA_PROJECT: Path to the Modelica project, possibly created by this cli
default = ./model_from_sdk
\f
:param modelica_project: Path, name & location of modelica project, possibly created with this cli
"""
run_path = Path(modelica_project).resolve()
project_name = run_path.stem
if len(str(run_path).split()) > 1: # Modelica can't handle spaces in project name or path
raise SystemExit(
f"\n'{run_path}' failed. Modelica does not support spaces in project names or paths. "
"Please update your directory tree to not include spaces in any name")
# setup modelica runner
mr = ModelicaRunner()
mr.run_in_docker('compile_and_run',
f'{project_name}.Districts.DistrictEnergySystem',
file_to_load=run_path / 'package.mo',
run_path=run_path
)
if (run_path.parent / f'{project_name}/{project_name}.Districts.DistrictEnergySystem_results' / f'{project_name}_Districts_DistrictEnergySystem_res.mat').exists():
print(f"\nModelica model {project_name} ran successfully")
else:
raise SystemExit(f"\n{project_name} failed. Check the error log at {project_name}_results/stdout.log for more info.")