In [ ]:
import sys
import pandas as pd
sys.path.append("/home/docker/data/Optimization/")
from matplotlib import pyplot as plt
import numpy as np
%matplotlib inline

In [ ]:
from collections import OrderedDict
from building_opt import building_opt as bui

In [ ]:
# Define the parameters for the different cases
pars_A = OrderedDict([("A_s", 0.9), ("A_n", 0.0), ("A_e", 0.1), ("A_w", 0.0), ("P_solar", 6.0e5)])
pars_B = OrderedDict([("A_s", 0.7), ("A_n", 0.0), ("A_e", 0.15), ("A_w", 0.15), ("P_solar", 5.5e5)])
pars_C = OrderedDict([("A_s", 0.7), ("A_n", 0.0), ("A_e", 0.3), ("A_w", 0.0), ("P_solar", 4.5e5)])

# Run the simulations using the different parametrizations
sim_res_ctrl = bui.run_simulation('ElectricNetwork.SingleBuildingElectricCtrl', P_hvac = 0.0, P_batt = 0.0)
sim_res = bui.run_simulation('ElectricNetwork.SingleBuildingElectric', P_hvac = -2.0e5, P_batt = 0.0)
sim_res_pv = bui.run_simulation('ElectricNetwork.SingleBuildingElectricPV', P_hvac = -2.0e5, P_batt = 0.0)
sim_A_res_ctrl_pv = bui.run_simulation('ElectricNetwork.SingleBuildingElectricCtrlPV', P_hvac = 0.0, P_batt = 0.0, fixpars = pars_A)
sim_B_res_ctrl_pv = bui.run_simulation('ElectricNetwork.SingleBuildingElectricCtrlPV', P_hvac = 0.0, P_batt = 0.0, fixpars = pars_B)
sim_C_res_ctrl_pv = bui.run_simulation('ElectricNetwork.SingleBuildingElectricCtrlPV', P_hvac = 0.0, P_batt = 0.0, fixpars = pars_C)

In [ ]:
sim_res_ctrl_relay = bui.run_simulation('ElectricNetwork.SingleBuildingElectricCtrlRelay', P_hvac = 0.0, P_batt = 0.0)

In [ ]:
bui.plot_sim_res(sim_res_ctrl)
bui.plot_sim_res(sim_res_ctrl_relay)
bui.plot_sim_res(sim_res)

In [ ]:
plt.plot(sim_res_ctrl["time"], sim_res_ctrl["S_pow"], 'c')

plt.plot(sim_A_res_ctrl_pv["time"], sim_A_res_ctrl_pv["S_pow"], "b")
plt.plot(sim_A_res_ctrl_pv["time"], sim_A_res_ctrl_pv["pv.P"], "g")
plt.plot(sim_B_res_ctrl_pv["time"], sim_B_res_ctrl_pv["S_pow"], "b--")
plt.plot(sim_B_res_ctrl_pv["time"], sim_B_res_ctrl_pv["pv.P"], "g--")
plt.plot(sim_C_res_ctrl_pv["time"], sim_C_res_ctrl_pv["S_pow"], "b+")
plt.plot(sim_C_res_ctrl_pv["time"], sim_C_res_ctrl_pv["pv.P"], "g+")

sim_res_ctrl["Money"][-1]

In [ ]:
plt.plot(sim_res_ctrl_relay["time"], sim_res_ctrl_relay["u[1]"], 'r')
plt.plot(sim_res_ctrl_relay["time"], sim_res_ctrl_relay["P_m2"], 'b')

In [ ]:
opt_res_energy = bui.run_optimization(sim_res, "ElectricNetwork.BuildingMngmtOpt_E", n_e = 24)

In [ ]:
plt.plot(opt_res_energy["time"], opt_res_energy["P_hvac"], "r")
plt.plot(sim_res_ctrl["time"], sim_res_ctrl["Pctrl"], 'c')

In [ ]:
plt.plot(opt_res_energy["time"], opt_res_energy["S_pow"], 'c')
plt.plot(sim_res_ctrl["time"], sim_res_ctrl["S_pow"], 'r')
plt.plot(sim_res_ctrl_relay["time"], sim_res_ctrl_relay["S_pow"], 'r--')
print opt_res_energy["E"][-1]
print sim_res_ctrl["E"][-1]
print sim_res_ctrl_relay["E"][-1]
print 1e-12*np.dot(opt_res_energy["P_hvac"], opt_res_energy["P_hvac"])

In [ ]:
opt_res_money = bui.run_optimization(sim_res, "ElectricNetwork.BuildingMngmtOpt_Money", n_e = 24)

In [ ]:
plt.plot(opt_res_money["time"], opt_res_money["Money"])

In [ ]:
plt.plot(opt_res_energy["time"], opt_res_energy["S_pow"], 'c')
plt.plot(opt_res_money["time"], opt_res_money["S_pow"], 'r')
plt.plot(sim_res_ctrl["time"], sim_res_ctrl["S_pow"], 'g')
print opt_res_money["Money"][-1]
print sim_res_ctrl["Money"][-1]
print sim_res_ctrl_relay["Money"][-1]

In [ ]:
opt_A_res_energy_pv = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_E_PV", fixpars = pars_A, n_e = 24)

In [ ]:
plt.plot(opt_A_res_energy_pv["time"], opt_A_res_energy_pv["P_hvac"])
print 0.1e-12*np.dot(opt_A_res_energy_pv["P_hvac"],opt_A_res_energy_pv["P_hvac"])
print opt_A_res_energy_pv["E"][-1]

In [ ]:
opt_B_res_energy_pv = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_E_PV", fixpars = pars_B)

In [ ]:
plt.plot(opt_B_res_energy_pv["time"], opt_B_res_energy_pv["P_hvac"])
print 0.1e-12*np.dot(opt_B_res_energy_pv["P_hvac"],opt_B_res_energy_pv["P_hvac"])
print opt_B_res_energy_pv["E"][-1]

In [ ]:
opt_C_res_energy_pv = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_E_PV", fixpars = pars_C)

In [ ]:
plt.plot(opt_C_res_energy_pv["time"], opt_C_res_energy_pv["P_hvac"])
print 0.1e-12*np.dot(opt_C_res_energy_pv["P_hvac"],opt_C_res_energy_pv["P_hvac"])
print opt_C_res_energy_pv["E"][-1]

In [ ]:
opt_A_res_money_pv = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_Money_PV", fixpars = pars_A)

In [ ]:
plt.plot(opt_A_res_money_pv["time"], opt_A_res_money_pv["P_hvac"])
print 1e-14*np.dot(opt_A_res_money_pv["P_hvac"],opt_A_res_money_pv["P_hvac"])
print opt_A_res_money_pv["Money"][-1]

In [ ]:
opt_B_res_money_pv = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_Money_PV", fixpars = pars_B)

In [ ]:
opt_C_res_money_pv = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_Money_PV", fixpars = pars_C)

In [ ]:
bui.print_details(opt_res_money, opt_res_energy)
bui.print_details(opt_A_res_money_pv, opt_A_res_energy_pv)
bui.print_details(opt_B_res_money_pv, opt_B_res_energy_pv)
bui.print_details(opt_C_res_money_pv, opt_C_res_energy_pv)

In [ ]:
plt.plot(opt_res_money["Money"] - opt_res_money["Money"][0], 'r')
plt.plot(opt_A_res_money_pv["Money"] - opt_A_res_money_pv["Money"][0], 'r--')
plt.plot(opt_res_energy["Money"] - opt_res_energy["Money"][0], 'b')
plt.plot(opt_A_res_energy_pv["Money"] - opt_A_res_energy_pv["Money"][0], 'b--')

In [ ]:
plt.plot(opt_res_money["E"] - opt_res_money["E"][0], 'r')
plt.plot(opt_A_res_money_pv["E"] - opt_A_res_money_pv["E"][0], 'r--')
plt.plot(opt_res_energy["E"] - opt_res_energy["E"][0], 'b')
plt.plot(opt_A_res_energy_pv["E"] - opt_A_res_energy_pv["E"][0], 'b--')

In [ ]:
plt.plot(opt_res_money["P_el"], 'r')
plt.plot(opt_A_res_money_pv["P_el"], 'r--')
plt.plot(opt_res_energy["P_el"], 'b')
plt.plot(opt_A_res_energy_pv["P_el"], 'b--')

In [ ]:
plt.plot(opt_res_money["P_hvac"], 'r')
plt.plot(opt_A_res_money_pv["P_hvac"], 'r--')
plt.plot(opt_res_energy["P_hvac"], 'b')
plt.plot(opt_A_res_energy_pv["P_hvac"], 'b--')

In [ ]:
opt_A_res_energy_pv_batt = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_E_PV", use_battery = True, fixpars = pars_A)
#opt_res_energy_pv_batt = bui.run_optimization(opt_res_energy_pv_batt, "ElectricNetwork.BuildingMngmtOpt_E_PV", use_battery = True)

In [ ]:
opt_B_res_energy_pv_batt = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_E_PV", use_battery = True, fixpars = pars_B)

In [ ]:
opt_C_res_energy_pv_batt = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_E_PV", use_battery = True, fixpars = pars_C)

In [ ]:
opt_A_res_money_pv_batt = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_Money_PV", use_battery = True, fixpars = pars_A)

In [ ]:
opt_B_res_money_pv_batt = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_Money_PV", use_battery = True, fixpars = pars_B)

In [ ]:
opt_C_res_money_pv_batt = bui.run_optimization(sim_res_pv, "ElectricNetwork.BuildingMngmtOpt_Money_PV", use_battery = True, fixpars = pars_C)

In [ ]:
plt.plot(opt_A_res_energy_pv_batt["time"]/3600, opt_A_res_energy_pv_batt["batt.SOC"], 'b')
plt.plot([opt_A_res_energy_pv_batt["time"][0]/3600, opt_A_res_energy_pv_batt["time"][-1]/3600], [opt_A_res_energy_pv_batt["batt.SOC"][0], opt_A_res_energy_pv_batt["batt.SOC"][-1]], 'b--')

plt.plot(opt_A_res_money_pv_batt["time"]/3600, opt_A_res_money_pv_batt["batt.SOC"], 'k')
plt.plot([opt_A_res_money_pv_batt["time"][0]/3600, opt_A_res_money_pv_batt["time"][-1]/3600], [opt_A_res_money_pv_batt["batt.SOC"][0], opt_A_res_money_pv_batt["batt.SOC"][-1]], 'k--')
plt.figure()
plt.plot(opt_A_res_energy_pv_batt["time"]/3600, opt_A_res_energy_pv_batt["S_pow"], 'b')
plt.plot(opt_A_res_money_pv_batt["time"]/3600, opt_A_res_money_pv_batt["S_pow"], 'k')
#plot(opt_res_money["time"], opt_res_money["S_pow"], 'r')
#plot(sim_res_ctrl["time"], sim_res_ctrl["S_pow"], 'g')
#print 1e-11*np.dot(opt_res_energy_pv_batt["P_batt"], opt_res_energy_pv_batt["P_batt"]), opt_res_energy_pv_batt["E"][-1]
#print 1e-13*np.dot(opt_res_money_pv_batt["P_batt"], opt_res_money_pv_batt["P_batt"]), opt_res_money_pv_batt["Money"][-1]
#print 1e-12*np.dot(opt_res_energy_pv_batt["P_hvac"], opt_res_energy_pv_batt["P_hvac"]), opt_res_energy_pv_batt["E"][-1]
#print 1e-14*np.dot(opt_res_money_pv_batt["P_hvac"], opt_res_money_pv_batt["P_hvac"]), opt_res_money_pv_batt["Money"][-1]

In [ ]:
plot(sim_res_ctrl["time"], sim_res_ctrl["bldg.Vrms"]/4800, 'c')
plot(sim_res_ctrl_pv["time"], sim_res_ctrl_pv["bldg.Vrms"]/4800, 'k')

plot(opt_res_energy["time"], opt_res_energy["bldg.Vrms"]/4800, 'b--')
plot(opt_res_money["time"], opt_res_money["bldg.Vrms"]/4800, 'y--')

plot(opt_res_energy_pv["time"], opt_res_energy_pv["bldg.Vrms"]/4800, 'b')
plot(opt_res_money_pv["time"], opt_res_money_pv["bldg.Vrms"]/4800, 'y')

plot(opt_res_money_pv_batt["time"], opt_res_money_pv_batt["bldg.Vrms"]/4800, 'g')
plot(opt_res_energy_pv_batt["time"], opt_res_energy_pv_batt["bldg.Vrms"]/4800, 'r')

In [ ]:
# Save the data for case 1 building A: no PV, no battery and regular PI controller
df_case_1_A_ctrl = pd.DataFrame(sim_res_ctrl["Tmix"], index = sim_res_ctrl["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P"]:
    df_case_1_A_ctrl[n] = sim_res_ctrl[n]
    col_names.append(n)
    df_case_1_A_ctrl.columns = col_names

# Save the data for case 1 building A: no PV, no battery and optimize for energy
df_case_1_A_energy = pd.DataFrame(opt_res_energy["Tmix"], index = opt_res_energy["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P"]:
    df_case_1_A_energy[n] = opt_res_energy[n]
    col_names.append(n)
    df_case_1_A_energy.columns = col_names
    
# Save the data for case 1 building A: no PV, no battery and optimize for money
df_case_1_A_money = pd.DataFrame(opt_res_money["Tmix"], index = opt_res_money["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P"]:
    df_case_1_A_money[n] = opt_res_money[n]
    col_names.append(n)
    df_case_1_A_money.columns = col_names

# Dump to csv
df_case_1_A_ctrl.to_csv("case_1_A_ctrl.csv")
df_case_1_A_energy.to_csv("case_1_A_energy.csv")
df_case_1_A_money.to_csv("case_1_A_money.csv")

In [ ]:
# Save the data for case 2 building A: yes PV, no battery and regular PI controller
df_case_2_A_ctrl = pd.DataFrame(sim_A_res_ctrl_pv["Tmix"], index = sim_A_res_ctrl_pv["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_2_A_ctrl[n] = sim_A_res_ctrl_pv[n]
    col_names.append(n)
    df_case_2_A_ctrl.columns = col_names

# Save the data for case 2 building A: yes PV, no battery and optimal controller
df_case_2_A_energy = pd.DataFrame(opt_A_res_energy_pv["Tmix"], index = opt_A_res_energy_pv["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_2_A_energy[n] = opt_A_res_energy_pv[n]
    col_names.append(n)
    df_case_2_A_energy.columns = col_names
    
# Save the data for case 2 building A: yes PV, no battery and optimal controller
df_case_2_A_money = pd.DataFrame(opt_A_res_money_pv["Tmix"], index = opt_A_res_money_pv["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_2_A_money[n] = opt_A_res_money_pv[n]
    col_names.append(n)
    df_case_2_A_money.columns = col_names

# Dump to csv
df_case_2_A_ctrl.to_csv("case_2_A_ctrl.csv")
df_case_2_A_energy.to_csv("case_2_A_energy.csv")
df_case_2_A_money.to_csv("case_2_A_money.csv")

In [ ]:
# Save the data for case 3 building A: yes PV, yes battery
df_case_3_A_energy = pd.DataFrame(opt_A_res_energy_pv_batt["Tmix"], index = opt_A_res_energy_pv_batt["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_3_A_energy[n] = opt_A_res_energy_pv_batt[n]
    col_names.append(n)
    df_case_3_A_energy.columns = col_names
    
# Save the data for case 3 building A: yes PV, yes battery
df_case_3_A_money = pd.DataFrame(opt_A_res_money_pv_batt["Tmix"], index = opt_A_res_money_pv_batt["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_3_A_money[n] = opt_A_res_money_pv_batt[n]
    col_names.append(n)
    df_case_3_A_money.columns = col_names

# Dump to csv
df_case_3_A_energy.to_csv("case_3_A_energy.csv")
df_case_3_A_money.to_csv("case_3_A_money.csv")

In [ ]:
# Save the data for case 3 building B: yes PV, yes battery
df_case_3_B_energy = pd.DataFrame(opt_B_res_energy_pv_batt["Tmix"], index = opt_B_res_energy_pv_batt["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_3_B_energy[n] = opt_B_res_energy_pv_batt[n]
    col_names.append(n)
    df_case_3_B_energy.columns = col_names
    
# Save the data for case 3 building A: yes PV, yes battery
df_case_3_B_money = pd.DataFrame(opt_B_res_money_pv_batt["Tmix"], index = opt_B_res_money_pv_batt["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_3_B_money[n] = opt_B_res_money_pv_batt[n]
    col_names.append(n)
    df_case_3_B_money.columns = col_names

# Dump to csv
df_case_3_B_energy.to_csv("case_3_B_energy.csv")
df_case_3_B_money.to_csv("case_3_B_money.csv")

In [ ]:
# Save the data for case 3 building C: yes PV, yes battery
df_case_3_C_energy = pd.DataFrame(opt_C_res_energy_pv_batt["Tmix"], index = opt_C_res_energy_pv_batt["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_3_C_energy[n] = opt_C_res_energy_pv_batt[n]
    col_names.append(n)
    df_case_3_C_energy.columns = col_names
    
# Save the data for case 3 building C: yes PV, yes battery
df_case_3_C_money = pd.DataFrame(opt_C_res_money_pv_batt["Tmix"], index = opt_C_res_money_pv_batt["time"], columns = ["Tmix"])
col_names = ["Tmix"]
for n in ["S_pow", "E", "Money", "P_hvac", "Pctrl", "batt.P", "bldg.P", "pv.P"]:
    df_case_3_C_money[n] = opt_C_res_money_pv_batt[n]
    col_names.append(n)
    df_case_3_C_money.columns = col_names

# Dump to csv
df_case_3_C_energy.to_csv("case_3_C_energy.csv")
df_case_3_C_money.to_csv("case_3_C_money.csv")

In [ ]:
print sim_res_ctrl["time"][-1]
print opt_res_energy["time"][-1]

In [ ]:
from scipy import signal
import matplotlib.pyplot as plt

In [ ]:
A = np.array([[-1.13701970e-05, 5.64289098e-06],[ 5.64289098e-07, -3.08438960e-06]])
B = np.array([[1.15740741e-05],
	         [0.00000000e+00]])

C = np.array([[1.0, 0.0]])
D = np.zeros((1,1))

s2 = signal.lti(A, B, C, D)
w, mag, phase = signal.bode(s2)

s1 = signal.lti([1.14], [100000, 1])
w_1, mag_1, phase_1 = signal.bode(s1, w = w)

plt.figure()
plt.semilogx(w, mag)    # Bode magnitude plot
plt.semilogx(w_1, mag_1)
plt.figure()
plt.semilogx(w, phase)  # Bode phase plot
plt.semilogx(w_1, phase_1)
plt.show()

In [ ]:
10**(mag[0]/20)

In [ ]:
(6.28*1e5)/100

In [ ]:


In [ ]:
import matplotlib as mpl
mpl.rc('font', serif='Times New Roman', size=13)

def plot_res(res_E, res_M, img_name = 'test2png.png'):
    """
    This function plots the results of a simulation
    """
    time = res_E["time"]
    time_M = res_M["time"]
    Tbui_E = res_E["Tmix"]
    Tbui_M = res_M["Tmix"]
    Tamb = res_E["u[2]"]
    price = res_E["price"]
    price_max = np.max(price)
    price_min = np.min(price)
 
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(time/3600/24, Tamb, 'g', alpha=0.5, linewidth=2, label="$T_{amb}$")
    ax.plot(time/3600/24, Tbui_E, 'r', alpha=0.5, linewidth=2, label="$T_R^{E}$")
    ax.plot(time_M/3600/24, Tbui_M, 'k', alpha=0.5, linewidth=2, label="$T_R^{M}$")
    ax.plot(time/3600/24, np.ones(len(time))*(273.15 + 20), 'b--', alpha=0.5, linewidth=2, label="$T_R^{min,max}$")
    ax.plot(time/3600/24, np.ones(len(time))*(273.15 + 24), 'b--', alpha=0.5, linewidth=2)
    
    t0 = time[0]
    t1 = time[-1]
    T = np.arange(t0, t1, 3600.0)
    price_T = np.interp(T, time, price)
    alpha_max = 0.6
    for i in range(2,len(T)):
        a = (((price_T[i-1]+price_T[i])/2)-price_min)/(price_max - price_min)*alpha_max 
        ax.fill_between([(i-1)/24.0, i/24.0], [0,0], [350,350], facecolor="#CC3300", alpha = a, linewidth = 0)  

    ax.set_xlabel('Time [days]', size= '16')
    ax.set_ylabel('Temperature [K]', size= '16')
    ax.set_ylim([286,310])
    ax.set_xlim([0,5])
    plt.tick_params(axis='both', which='major', labelsize=16)
    plt.tick_params(axis='both', which='minor', labelsize=16)
    plt.legend(loc="upper right", ncol=4)
    fig = plt.gcf()
    fig.set_size_inches(9,5)
    fig.savefig(img_name, dpi=300)
    plt.show()
    
    return

def plot_res_batt(res_E, res_M, img_name = 'test2png.png'):
    """
    This function plots the results of a simulation
    """
    time = res_E["time"]
    time_M = res_M["time"]
    soc_E = res_E["batt.SOC"]
    soc_M = res_M["batt.SOC"]
    price = res_E["price"]
    price_max = np.max(price)
    price_min = np.min(price)
 
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(time/3600/24, soc_E, 'r', alpha=0.5, linewidth=2, label="$SOC^{E}$")
    ax.plot(time_M/3600/24, soc_M, 'k', alpha=0.5, linewidth=2, label="$SOC^{M}$")
    ax.plot(time/3600/24, np.ones(len(time))*0.2, 'b--', alpha=0.5, linewidth=2, label="$SOC^{min,max}$")
    ax.plot(time/3600/24, np.ones(len(time))*1.0, 'b--', alpha=0.5, linewidth=2)
    
    t0 = time[0]
    t1 = time[-1]
    T = np.arange(t0, t1, 3600.0)
    price_T = np.interp(T, time, price)
    alpha_max = 0.6
    for i in range(2,len(T)):
        a = (((price_T[i-1]+price_T[i])/2)-price_min)/(price_max - price_min)*alpha_max 
        ax.fill_between([(i-1)/24.0, i/24.0], [0,0], [350,350], facecolor="#CC3300", alpha = a, linewidth = 0)  

    ax.set_xlabel('Time [days]', size= '16')
    ax.set_ylabel('State of charge [1]', size= '16')
    ax.set_ylim([0,1.2])
    ax.set_xlim([0,5])
    plt.tick_params(axis='both', which='major', labelsize=16)
    plt.tick_params(axis='both', which='minor', labelsize=16)
    plt.legend(loc="lower right", ncol=4)
    fig = plt.gcf()
    fig.set_size_inches(9,5)
    fig.savefig(img_name, dpi=300)
    plt.show()
    
    return

In [ ]:
plot_res(opt_res_energy, opt_res_money, "case_1.png")

In [ ]:
plot_res(opt_A_res_energy_pv, opt_A_res_money_pv, "case_2_A.png")

In [ ]:
plot_res(opt_B_res_energy_pv, opt_B_res_money_pv, "case_2_B.png")

In [ ]:
plot_res(opt_C_res_energy_pv, opt_C_res_money_pv, "case_2_C.png")

In [ ]:
plot_res(opt_A_res_energy_pv_batt, opt_A_res_money_pv_batt, "case_3_A.png")

In [ ]:
plot_res(opt_B_res_energy_pv_batt, opt_B_res_money_pv_batt, "case_3_B.png")

In [ ]:
plot_res(opt_C_res_energy_pv_batt, opt_C_res_money_pv_batt, "case_3_C.png")

In [ ]:
plot_res_batt(opt_A_res_energy_pv_batt, opt_A_res_money_pv_batt, "case_3_A_soc.png")

In [ ]:
plot_res_batt(opt_B_res_energy_pv_batt, opt_B_res_money_pv_batt, "case_3_B_soc.png")

In [ ]:
plot_res_batt(opt_C_res_energy_pv_batt, opt_C_res_money_pv_batt, "case_3_C_soc.png")

In [ ]:
results_M = {"Case 1": opt_res_money, "Case 2": opt_B_res_money_pv, \
           "Case 3": opt_B_res_money_pv_batt}

results_E = {"Case 1": opt_res_energy, "Case 2": opt_B_res_energy_pv, \
           "Case 3": opt_B_res_energy_pv_batt}

def plot_P_hvac(results, img_name):
    colrs = "rgb"
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ind = 0
    for key, value in results.iteritems():
        ax.plot(value["time"]/3600/24, -value["P_hvac"]/1.0e3, colrs[ind], alpha=0.5, linewidth=2, label=key)
        ind +=1
        
    t0 = value["time"][0]
    t1 = value["time"][-1]
    T = np.arange(t0, t1, 3600.0)
    price_max = np.max(value["price"])
    price_min = np.min(value["price"])
    price_T = np.interp(T, value["time"], value["price"])
    alpha_max = 0.6
    for i in range(2,len(T)):
        a = (((price_T[i-1]+price_T[i])/2)-price_min)/(price_max - price_min)*alpha_max 
        ax.fill_between([(i-1)/24.0, i/24.0], [0,0], [350,350], facecolor="#CC3300", alpha = a, linewidth = 0)
        
    ax.set_xlabel('Time [days]', size= '16')
    ax.set_ylabel('Cooling power [kW]', size= '16')
    ax.set_ylim([0,255])
    ax.set_xlim([0,5])
    plt.tick_params(axis='both', which='major', labelsize=16)
    plt.tick_params(axis='both', which='minor', labelsize=16)
    plt.legend(loc="lower right", ncol=4)
    fig = plt.gcf()
    fig.set_size_inches(10,5)
    fig.savefig(img_name, dpi=300)
    plt.show()

In [ ]:
plot_P_hvac(results_E, "P_hvac_energy.png")

In [ ]:
plot_P_hvac(results_M, "P_hvac_money.png")

In [ ]:
bui.print_details(opt_res_money,sim_res_ctrl_relay)
print "-"*20
bui.print_details(opt_res_money, opt_res_energy)
print "-"*20
bui.print_details(opt_B_res_money_pv, opt_B_res_energy_pv)
print "-"*20
bui.print_details(opt_B_res_money_pv_batt, opt_B_res_energy_pv_batt)
print "-"*20

In [ ]:
def print_E_details(opt_res_money, opt_res_energy):
    """
    This function prints some details about the different optimziation results
    """
    E_mon = opt_res_money["E"][-1] - opt_res_money["E"][0]
    E_en = opt_res_energy["E"][-1] - opt_res_energy["E"][0]
    print "Energy consumed while optimizing energy [$]: {0}".format(E_en)
    print "Energy consumed while optimizing cost [$]: {0}".format(E_mon)
    print "Savings [kWh, %]: {0} -- {1}%".format(E_en - E_mon, 100*(E_en - E_mon)/E_en)
    return

In [ ]:
print_E_details(opt_res_money,sim_res_ctrl_relay)
print "-"*20
print_E_details(opt_res_money, opt_res_energy)
print "-"*20
print_E_details(opt_B_res_money_pv, opt_B_res_energy_pv)
print "-"*20
print_E_details(opt_B_res_money_pv_batt, opt_B_res_energy_pv_batt)
print "-"*20

In [ ]:


In [ ]: