-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (121 loc) · 4.63 KB
/
main.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
# Author: Nicholas Long / Sourav Dey, modified by Amy Allen
import datetime
import os
import random
import sys
import time
from multiprocessing import Process, freeze_support
from decimal import Decimal
import pandas as pd
from alfalfa_client.alfalfa_client import AlfalfaClient
seconds_day = 86400
sec_hour = 3600
# def Controller(object):
def initialize_control():
'''Initialize the control input u.
Parameters
----------
None
Returns
-------
u : dict
Defines the control input to be used for the next step.
{<input_name> : <input_value>}
'''
u = {'oveClgSP_u': 23 + 273,
'oveClgSP_activate': True,
'oveClgSP_v2_u': 23 + 273,
'oveClgSP_v2_activate': True}
return u
def change_setpoint():
'''change the control input u.
Parameters
----------
None
Returns
-------
u : dict
Defines the control input to be used for the next step.
{<input_name> : <input_value>}
'''
u = {'oveClgSP_u': 27 + 273,
'oveClgSP_activate': True,
'oveClgSP_v2_u': 27 + 273,
'oveClgSP_v2_activate': True}
return u
def main():
alfalfa = AlfalfaClient(url='http://localhost')
length = 604800 #168 hours
step = 300 # 5 minutes -- this may be hard coded in step_fmu.py
u = initialize_control()
heating_setpoint = 21
file='FMUs/wrapped_2021.12.09_bldg2.fmu'
print(f"Uploading test case {file}")
site = alfalfa.submit(file)
print('Starting simulation')
start_time=13824000
alfalfa.start(
site,
start_datetime=start_time,
external_clock=True,
)
time.sleep(10.0)
print(alfalfa.status(site))
history = {
'elapsed_seconds': [],
'hour':[],
'Teaser_mtg_zone_air_temp_v2':[],
'Teaser_office_zone_air_temp':[],
'Teaser_clg_del_y':[],
'chiller_power_draw_y':[],
'Teaser_clg_SP_air':[],
'Teaser_clg_SP_air_bldg2':[],
'OA_DB':[]
}
u2 = change_setpoint()
print("alfalfa advancing")
alfalfa.advance([site])
current_time = alfalfa.get_sim_time(site)
print('Stepping through time')
while Decimal(current_time) <= (start_time + length):
current_time = alfalfa.get_sim_time(site)
model_outputs = alfalfa.outputs(site)
sec_into_day = Decimal(current_time) % seconds_day
hour = sec_into_day/sec_hour
history['hour'].append(hour)
u=initialize_control()
datetime_obj=pd.to_datetime(int(float(current_time)), unit='s', origin=pd.Timestamp('2017-01-01'))
dow=datetime_obj.weekday()
if hour >=15 and hour <=17 and dow<5: #going in to DR condition on weekday afternoons
alfalfa.setInputs(site, u2) #Increase cooling setpoint
print("dr condition")
else:
alfalfa.setInputs(site, u) #Keep base case setpoint
alfalfa.advance([site])
current_time = alfalfa.get_sim_time(site)
datetime=pd.to_datetime(int(float(current_time)))
history['elapsed_seconds'].append(current_time)
#history['datetime'].append(datetime.datetime.fromtimestamp(int(float(current_time)))) # From Jan 1, 1970, which is probably not the correct year
history['Teaser_mtg_zone_air_temp_v2'].append(model_outputs['Teaser_mtg_zone_air_temp_v2'])
history['Teaser_office_zone_air_temp'].append(model_outputs['Teaser_office_zone_air_temp'])
history['Teaser_clg_del_y'].append(model_outputs['Teaser_clg_del_y'])
history['chiller_power_draw_y'].append(model_outputs['chiller_power_draw_y'])
history['Teaser_clg_SP_air'].append(model_outputs['Teaser_clg_SP_air'])
history['Teaser_clg_SP_air_bldg2'].append(model_outputs['Teaser_clg_SP_air_bldg2'])
history['OA_DB'].append(model_outputs['Teaser_OA_DB'])
alfalfa.stop(site)
# storage for results
file_basename = os.path.splitext(os.path.basename(__file__))[0]
result_dir = f'results_{file_basename}'
os.makedirs(result_dir, exist_ok=True)
history_df = pd.DataFrame.from_dict(history)
history_df.to_csv(f'{result_dir}/{file_basename}.csv')
# In windows you must include this to allow alfalfa client to multiprocess
if __name__ == '__main__':
if os.name == 'nt':
freeze_support()
p = Process(target=main)
p.start()
else:
# Running the debugger doesn't work on mac with freeze_support()
main()