Skip to content

Commit

Permalink
add internal load computation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophGehbauer committed Apr 4, 2024
1 parent 74ecd07 commit 2ad3a6b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ These full list of outputs is here:
- `termination`: Status of optimization indicating whether the solver has reached an optimal solution, as string.


The `output-data` is a special entry as it contains time series data for all the inputs and outputs of the optimization process. It is not necessary for a general user to understand these, but might be important for developers or debugging. The `output-data` can be formatted into a pandas dataframe and subsequently easily plotted and analyzed:
The `output-data` is a special entry as it contains time series data for all the inputs and outputs of the optimization process. It is not necessary for a general user to understand these, but might be important for developers or for debugging. The `output-data` can be formatted into a pandas dataframe and subsequently easily plotted and analyzed:

```python
# convert df_outputs to pandas
df = pd.DataFrame(ctrl.get_output(keys=['output-data'])['output-data'])
df = pd.read_json(io.StringIO(ctrl.get_output(keys=['output-data'])['output-data']))
df.index = pd.to_datetime(df.index, unit='ms')

# make exmaple plot of results
Expand Down
24 changes: 16 additions & 8 deletions afc/ctrlWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# pylint: disable=too-many-branches, too-many-statements, broad-exception-caught
# pylint: disable=unused-variable

import io
import os
import sys
import time
Expand Down Expand Up @@ -143,12 +144,16 @@ def compute(self):
self.msg = ''

# Parse input dataframe
inputs = pd.DataFrame().from_dict(self.input['input-data'])
inputs.index = pd.to_datetime(inputs.index)#, unit='ms')
inputs = pd.read_json(io.StringIO(self.input['input-data']))
inputs.index = pd.to_datetime(inputs.index)

# configuration
self.parameter = self.input['parameter']

# add demand profiles
if self.parameter['wrapper']['compute_loads']:
inputs = make_inputs(self.parameter, inputs, return_json=False)['input-data']

# Setup controller
if self.init:
self.init_functions()
Expand Down Expand Up @@ -380,9 +385,8 @@ def compute(self):
#uShade = df[['Tint Bottom [-]', 'Tint Middle [-]', 'Tint Top [-]']].iloc[0].values
self.output['ctrl-facade'] = [round(float(u),1) for u in uShade]

df.index = (df.index.astype(np.int64) / 10 ** 6).astype(str)
df = df.astype(float).fillna(-1)
self.output['output-data'] = df.to_dict()
self.output['output-data'] = df.to_json()
self.output['duration']['outputs'] = time.time() - st1
self.output['duration']['all'] = time.time() - st

Expand All @@ -399,7 +403,7 @@ def compute(self):
self.output[k] = None
return self.msg

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

df = df.copy(deep=True)
Expand All @@ -422,6 +426,8 @@ def make_inputs(parameter, df, ext_df=pd.DataFrame()):
raise NotImplementedError

# Default inputs
if not 'wind_speed' in df.columns:
df.loc[:, 'wind_speed'] = 0
df.loc[:, 'generation_pv'] = 0
df.loc[:, 'load_demand'] = 0
df.loc[:, 'temp_slab_max'] = 1e3
Expand All @@ -436,7 +442,10 @@ def make_inputs(parameter, df, ext_df=pd.DataFrame()):

# Map parameter and make Inputs object
inputs = {}
inputs['input-data'] = df.to_dict()
if return_json:
inputs['input-data'] = df.to_json()
else:
inputs['input-data'] = df
inputs['wf-all'] = None
inputs['facade-initial'] = parameter['facade']['fstate_initial']
inputs['temps-initial'] = parameter['zone']['temps_initial']
Expand Down Expand Up @@ -477,8 +486,7 @@ def make_inputs(parameter, df, ext_df=pd.DataFrame()):
print('Log-message:\n', ctrl.do_step(inputs=inputs))
print('Duration:\n', ctrl.get_output(keys=['duration']))
print('Optimization:\n', ctrl.get_output(keys=['opt-stats']))
df = pd.DataFrame(ctrl.get_output(keys=['output-data'])['output-data'])
df.index = pd.to_datetime(pd.to_numeric(df.index), unit='ms')
df = pd.read_json(io.StringIO(ctrl.get_output(keys=['output-data'])['output-data']))

try:
plot_standard1(pd.concat([wf, df], axis=1).ffill().iloc[:-1])
Expand Down
1 change: 1 addition & 0 deletions afc/defaultConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def default_parameter(tariff_name='e19-2020', hvac_control=True,
output_list = default_output_list(parameter) + afc_output_list()
parameter['wrapper']['output_list'] = output_list
parameter['wrapper']['tariff_name'] = tariff_name
parameter['wrapper']['compute_loads'] = False

return parameter

Expand Down
28 changes: 18 additions & 10 deletions dev/Development-ControllerWrapper.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
"CO2 Emissions [kg]\t\t0.0\n",
"\n",
"Duration:\n",
" {'duration': {'radiance': 30.463333129882812, 'glare': 69.8858585357666, 'varts': 0.13012290000915527, 'optall': 0.5115549564361572, 'outputs': 0.00562286376953125, 'all': 105.02531147003174}}\n",
" {'duration': {'radiance': 31.48572826385498, 'glare': 69.95852065086365, 'varts': 0.1322014331817627, 'optall': 0.5194482803344727, 'outputs': 0.001718282699584961, 'all': 106.26495265960693}}\n",
"Optimization:\n",
" {'opt-stats': {'duration': 0.20725703239440918, 'termination': 'optimal', 'objective': 20.28974844}}\n"
" {'opt-stats': {'duration': 0.20727014541625977, 'termination': 'optimal', 'objective': 20.28974844}}\n"
]
},
{
Expand Down Expand Up @@ -97,6 +97,7 @@
"# pylint: disable=too-many-branches, too-many-statements, broad-exception-caught\n",
"# pylint: disable=unused-variable\n",
"\n",
"import io\n",
"import os\n",
"import sys\n",
"import time\n",
Expand Down Expand Up @@ -228,12 +229,16 @@
" self.msg = ''\n",
"\n",
" # Parse input dataframe\n",
" inputs = pd.DataFrame().from_dict(self.input['input-data'])\n",
" inputs.index = pd.to_datetime(inputs.index)#, unit='ms')\n",
" inputs = pd.read_json(io.StringIO(self.input['input-data']))\n",
" inputs.index = pd.to_datetime(inputs.index)\n",
"\n",
" # configuration\n",
" self.parameter = self.input['parameter']\n",
"\n",
" # add demand profiles\n",
" if self.parameter['wrapper']['compute_loads']:\n",
" inputs = make_inputs(self.parameter, inputs, return_json=False)['input-data']\n",
"\n",
" # Setup controller\n",
" if self.init:\n",
" self.init_functions()\n",
Expand Down Expand Up @@ -465,9 +470,8 @@
" #uShade = df[['Tint Bottom [-]', 'Tint Middle [-]', 'Tint Top [-]']].iloc[0].values\n",
" self.output['ctrl-facade'] = [round(float(u),1) for u in uShade]\n",
"\n",
" df.index = (df.index.astype(np.int64) / 10 ** 6).astype(str)\n",
" df = df.astype(float).fillna(-1)\n",
" self.output['output-data'] = df.to_dict()\n",
" self.output['output-data'] = df.to_json()\n",
" self.output['duration']['outputs'] = time.time() - st1\n",
" self.output['duration']['all'] = time.time() - st\n",
"\n",
Expand All @@ -484,7 +488,7 @@
" self.output[k] = None\n",
" return self.msg\n",
"\n",
"def make_inputs(parameter, df, ext_df=pd.DataFrame()):\n",
"def make_inputs(parameter, df, ext_df=pd.DataFrame(), return_json=True):\n",
" \"\"\"Utility function to make inputs.\"\"\"\n",
"\n",
" df = df.copy(deep=True)\n",
Expand All @@ -507,6 +511,8 @@
" raise NotImplementedError\n",
"\n",
" # Default inputs\n",
" if not 'wind_speed' in df.columns:\n",
" df.loc[:, 'wind_speed'] = 0\n",
" df.loc[:, 'generation_pv'] = 0\n",
" df.loc[:, 'load_demand'] = 0\n",
" df.loc[:, 'temp_slab_max'] = 1e3\n",
Expand All @@ -521,7 +527,10 @@
"\n",
" # Map parameter and make Inputs object\n",
" inputs = {}\n",
" inputs['input-data'] = df.to_dict()\n",
" if return_json:\n",
" inputs['input-data'] = df.to_json()\n",
" else:\n",
" inputs['input-data'] = df\n",
" inputs['wf-all'] = None\n",
" inputs['facade-initial'] = parameter['facade']['fstate_initial']\n",
" inputs['temps-initial'] = parameter['zone']['temps_initial']\n",
Expand Down Expand Up @@ -562,8 +571,7 @@
" print('Log-message:\\n', ctrl.do_step(inputs=inputs))\n",
" print('Duration:\\n', ctrl.get_output(keys=['duration']))\n",
" print('Optimization:\\n', ctrl.get_output(keys=['opt-stats']))\n",
" df = pd.DataFrame(ctrl.get_output(keys=['output-data'])['output-data'])\n",
" df.index = pd.to_datetime(pd.to_numeric(df.index), unit='ms')\n",
" df = pd.read_json(io.StringIO(ctrl.get_output(keys=['output-data'])['output-data']))\n",
"\n",
" try:\n",
" plot_standard1(pd.concat([wf, df], axis=1).ffill().iloc[:-1])\n",
Expand Down

0 comments on commit 2ad3a6b

Please sign in to comment.