Skip to content

Commit

Permalink
updated error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophGehbauer committed Mar 28, 2024
1 parent fc014ee commit 32c9ca9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
19 changes: 13 additions & 6 deletions afc/ctrlWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,21 @@ def compute(self):
# with open('cfg_{}.json'.format(data.index[0]), 'w') as f:
# f.write(json.dumps(cfg))

print_error = self.input['parameter']['wrapper']['printing']
printing = self.input['parameter']['wrapper']['printing']
self.res = \
self.controller.do_optimization(self.data, parameter=self.input['parameter'],
self.controller.do_optimization(self.data,
parameter=self.input['parameter'],
options=self.input['parameter']['solver_options'],
tee=self.input['parameter']['wrapper']['printing'],
print_error=print_error)
tee=printing,
print_error=printing)
duration, objective, df, model, result, termination, parameter = self.res
self.output['optall_duration'] = time.time() - st1

# Write outputs
st1 = time.time()
self.output['opt_duration'] = float(duration)
self.output['opt_termination'] = str(termination)
self.output['opt_objective'] = float(objective) if objective else np.nan
self.output['opt_objective'] = float(objective) if objective else None
self.output['glaremode'] = list(gmodes)
if objective:
uShade = df[[f'Facade State {z}' for \
Expand All @@ -366,9 +367,11 @@ def compute(self):

except Exception as e:
self.msg += f'\nERROR: {e}\n\n{traceback.format_exc()}'
for k in self.output:
self.output[k] = None
return self.msg

def make_inputs(parameter, df):
def make_inputs(parameter, df, ext_df=pd.DataFrame()):
"""Utility function to make inputs."""

df = df.copy(deep=True)
Expand Down Expand Up @@ -398,6 +401,10 @@ def make_inputs(parameter, df):
df.loc[:, 'temp_wall_max'] = 1e3
df.loc[:, 'temp_wall_min'] = 0

# Add External inputs (if any)
for c in ext_df:
df[c] = ext_df[c]

# Map parameter and make Inputs object
inputs = {}
inputs['radiance'] = parameter['radiance']
Expand Down
28 changes: 17 additions & 11 deletions dev/Development-ControllerWrapper.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
"source": [
"# configure mpl\n",
"import matplotlib as mpl\n",
"mpl.rcParams['figure.dpi'] = 72\n",
"%matplotlib inline"
"mpl.rcParams['figure.dpi'] = 72"
]
},
{
Expand All @@ -62,15 +61,15 @@
"All Input variables: dict_keys(['paths', 'radiance', 'df_input', 'wf_all', 'facade_initial', 'temps_initial', 'parameter'])\n",
"WARNING: view_angle: Window below viewpoint.\n",
"Log-message:\n",
" Duration [s]\t\t0.19\n",
" Duration [s]\t\t0.29\n",
"Objective [$]\t\t20.29\t\t\t7.58 (Total Cost)\n",
"Cost [$]\t\t13.32 (Energy)\t6.98 (Demand)\n",
"CO2 Emissions [kg]\t\t0.0\n",
"\n",
"Duration:\n",
" {'rad_duration': 7.62345027923584, 'varts_duration': 0.12901592254638672, 'optall_duration': 0.2766547203063965, 'glare_duration': 69.99816942214966, 'opt_duration': 0.19001984596252441, 'outputs_duration': 0.006535053253173828, 'duration': 78.04731726646423}\n",
" {'rad_duration': 17.785902976989746, 'varts_duration': 0.18678975105285645, 'optall_duration': 0.43532633781433105, 'glare_duration': 96.1897702217102, 'opt_duration': 0.2924964427947998, 'outputs_duration': 0.009624481201171875, 'duration': 114.62892770767212}\n",
"Optimization:\n",
" {'opt_objective': 20.29202698, 'opt_duration': 0.19001984596252441, 'opt_termination': 'optimal', 'duration': 78.04731726646423}\n"
" {'opt_objective': 20.29202698, 'opt_duration': 0.2924964427947998, 'opt_termination': 'optimal', 'duration': 114.62892770767212}\n"
]
},
{
Expand Down Expand Up @@ -416,20 +415,21 @@
" # with open('cfg_{}.json'.format(data.index[0]), 'w') as f:\n",
" # f.write(json.dumps(cfg))\n",
"\n",
" print_error = self.input['parameter']['wrapper']['printing']\n",
" printing = self.input['parameter']['wrapper']['printing']\n",
" self.res = \\\n",
" self.controller.do_optimization(self.data, parameter=self.input['parameter'],\n",
" self.controller.do_optimization(self.data,\n",
" parameter=self.input['parameter'],\n",
" options=self.input['parameter']['solver_options'],\n",
" tee=self.input['parameter']['wrapper']['printing'],\n",
" print_error=print_error)\n",
" tee=printing,\n",
" print_error=printing)\n",
" duration, objective, df, model, result, termination, parameter = self.res\n",
" self.output['optall_duration'] = time.time() - st1\n",
"\n",
" # Write outputs\n",
" st1 = time.time()\n",
" self.output['opt_duration'] = float(duration)\n",
" self.output['opt_termination'] = str(termination)\n",
" self.output['opt_objective'] = float(objective) if objective else np.nan\n",
" self.output['opt_objective'] = float(objective) if objective else None\n",
" self.output['glaremode'] = list(gmodes)\n",
" if objective:\n",
" uShade = df[[f'Facade State {z}' for \\\n",
Expand All @@ -453,9 +453,11 @@
"\n",
" except Exception as e:\n",
" self.msg += f'\\nERROR: {e}\\n\\n{traceback.format_exc()}'\n",
" for k in self.output:\n",
" self.output[k] = None\n",
" return self.msg\n",
"\n",
"def make_inputs(parameter, df):\n",
"def make_inputs(parameter, df, ext_df=pd.DataFrame()):\n",
" \"\"\"Utility function to make inputs.\"\"\"\n",
"\n",
" df = df.copy(deep=True)\n",
Expand Down Expand Up @@ -485,6 +487,10 @@
" df.loc[:, 'temp_wall_max'] = 1e3\n",
" df.loc[:, 'temp_wall_min'] = 0\n",
"\n",
" # Add External inputs (if any)\n",
" for c in ext_df:\n",
" df[c] = ext_df[c]\n",
"\n",
" # Map parameter and make Inputs object\n",
" inputs = {}\n",
" inputs['radiance'] = parameter['radiance']\n",
Expand Down

0 comments on commit 32c9ca9

Please sign in to comment.