Skip to content

Commit

Permalink
* deleted first Controlled Geo Model "SimpleModelGeo", added and rest…
Browse files Browse the repository at this point in the history
…ructured HeatPump Models including TEASER Model, #55
  • Loading branch information
MStillerEBC committed Aug 29, 2019
1 parent 117794e commit 30fee76
Show file tree
Hide file tree
Showing 32 changed files with 10,116 additions and 982 deletions.
2 changes: 1 addition & 1 deletion pyDMPC/ControlFramework/Init.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
commands.append(range(0,105,5))
traj_points.append([])
traj_var.append([])
cost_fac.append([-1.0, 0.0, 55.0, -55.0, 0.0, 0.0, 0.0, 0.0])
cost_fac.append([-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3000.0, -3000.0])
factors.append([1, -60./4.18/8./100.])

sys_id.append(1)
Expand Down
88 changes: 45 additions & 43 deletions pyDMPC/ControlFramework/Init_complex.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Global paths
glob_lib_paths = [r'N:\Forschung\EBC0377_BMWi-GeoBase_GA\Students\mba-mst\05-Python\pyDMPC\pyDMPC\ModelicaModels\ModelicaModels',
r'N:\Forschung\EBC0377_BMWi-GeoBase_GA\Students\mba-mst\02-Models\modelica-buildings\Buildings',
r'N:\Forschung\EBC0377_BMWi-GeoBase_GA\Students\mba-mst\02-Models\AixLib\AixLib']
glob_res_path = r'C:\TEMP\Dymola'
glob_dym_path = r'C:\Program Files\Dymola 2019\Modelica\Library\python_interface\dymola.egg'
glob_lib_paths = [r'C:\mst\pyDMPC\pyDMPC\ModelicaModels\ModelicaModels',
r'C:\mst\modelica-buildings\Buildings',
r'C:\mst\AixLib\AixLib']
glob_res_path = r'C:\mst\dymola'
glob_dym_path = r'C:\Program Files (x86)\Dymola 2018\Modelica\Library\python_interface\dymola.egg'

# Working directory
import time
Expand Down Expand Up @@ -48,6 +48,7 @@

# Modifiers
cost_fac = []
factors = []

# Variation
min_var = []
Expand All @@ -63,46 +64,17 @@

# Subsystems
sys_id.append(0)
name.append("Field")
model_type.append("Modelica")
name.append("Building")
model_type.append("Linear")
ups_neigh.append(None)
downs_neigh.append(1)
input_names.append(["returnTemperature.T"])
input_variables.append(["external"])
inputs.append([])
output_names.append(["movMea.y"])
set_points.append([285.65])
state_var_names.append(["supplyTemperature.T"])
model_state_var_names.append(["vol1.T_start"])
start.append(0.)
stop.append(3600.0*24*365.25*3)
incr.append(3600.)
opt_time.append(86400)
samp_time.append(10)
lib_paths.append(glob_lib_paths)
res_path.append(glob_res_path + "\\" + name_wkdir)
dym_path.append(glob_dym_path)
mod_path.append(r'ModelicaModels.SubsystemModels.DetailedModels.Geo.Field2')
command_names.append(["traj"])
command_variables.append(["decisionVariables.table[1,2]"])
commands.append(range(0,100,1))
traj_points.append(range(283,291,1))
traj_var.append(["fromKelvin.Celsius"])
cost_fac.append([0.0, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0])
#cost factors: ([0: reale Kosten Feld, 1: reale Kosten sek. Erzeuger, 2: reale Kosten WP, 3: downstream neigh, 4: Abweichung setpoint, 5: I-Anteil Bestrafung, 6: I-Anteil Belohnung, 7: D-Anteil Bestrafung, 8: D-Anteil Vergütung])

sys_id.append(1)
name.append("Building")
model_type.append("Modelica")
ups_neigh.append(0)
downs_neigh.append(None)
input_names.append(["supplyTemperature.T"])
input_variables.append([r"variation.table[1,2]"])
inputs.append(range(280.15,290.15,0.05))
inputs.append(range(280,290,5))
output_names.append(["returnTemperature"])
set_points.append([287])
state_var_names.append(["sine.y"])
model_state_var_names.append(["const.k"])
state_var_names.append(["const.y"])
model_state_var_names.append(["const.k"]) #"const.k"
start.append(0.)
stop.append(7200.)
incr.append(10.)
Expand All @@ -113,9 +85,39 @@
dym_path.append(glob_dym_path)
mod_path.append(r'ModelicaModels.SubsystemModels.DetailedModels.Geo.GeothermalHeatPump')
command_names.append(["heatShare"])
command_variables.append(["TStorageSet"])
commands.append(range(0,100,5))
command_variables.append(["decisionVariables.table[1,2]"])
commands.append(range(35,45,5))
traj_points.append([])
traj_var.append([])
cost_fac.append([-0.5, 0.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0])
#cost factors: ([0: reale Kosten Feld, 1: reale Kosten sek. Erzeuger, 2: reale Kosten WP, 3: downstream neigh, 4: Abweichung setpoint, 5: I-Anteil Bestrafung, 6: I-Anteil Belohnung, 7: D-Anteil Bestrafung, 8: D-Anteil Vergütung])
cost_fac.append([-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3000.0, -3000.0])
factors.append([1, -60./4.18/8./100.])

sys_id.append(1)
name.append("Field")
model_type.append("Modelica")
ups_neigh.append(0)
downs_neigh.append(None)
input_names.append(["returnTemperature.T"])
input_variables.append(["external"])
inputs.append([])
output_names.append(["movMea.y"])
set_points.append([285.65])
state_var_names.append(["supplyTemperature.T"])
model_state_var_names.append(["vol.T_start"])
start.append(0.)
stop.append(3600.0*24*365.25*3)
incr.append(3600.)
opt_time.append(86400)
samp_time.append(10)
lib_paths.append(glob_lib_paths)
res_path.append(glob_res_path + "\\" + name_wkdir)
dym_path.append(glob_dym_path)
mod_path.append(r'ModelicaModels.SubsystemModels.DetailedModels.Geo.Field')
command_names.append(["traj"])
command_variables.append(["decisionVariables.table[1,2]"])
commands.append(range(0,105,5))
traj_points.append(range(280,290,1))
traj_var.append(["supplyTemperature.T"])
cost_fac.append([-1.0, 0.0, 10.0, -10.0, 0.0, 0.0, 0.0, 0.0 ])
factors.append([0,0])
#Legend: cost_fac.append([real_cost, cost_downstr_neigh, cost_dev_setpoint_penalty, cost_dev_setpoint_reward, cost_integ_penalty, cost_integ_reward, cost_diff_penalty, cost_diff_reward])
38 changes: 19 additions & 19 deletions pyDMPC/ControlFramework/Subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,33 +288,33 @@ def calc_cost(self, command, outputs):
#Integralteil
self.err_integ += outputs - setpoint

if self.cost_rec != []:
#if self.cost_rec != []:
#if self.phase > 0: #if field cools off
if self.err_integ < 0:
cost += self.cost_fac[4] * (-(self.err_integ)) #Integral penalty
else:
cost += self.cost_fac[5] * self.err_integ #Integral reward
# #else:
if self.err_integ < 0:
cost += self.cost_fac[4] * (-(self.err_integ)) #Integral penalty
else:
cost += self.cost_fac[5] * self.err_integ #Integral reward
# else:
# if self.err_integ > 0:
# cost += self.cost_fac[4] * self.err_integ #Integral penalization
# else:
# cost += self.cost_fac[5] * (-(self.err_integ)) #Integral reward
#Differentialteil
self.err_prev = self.err_curr
self.err_curr = outputs - setpoint #same as proportional error
self.err_diff = self.err_curr - self.err_prev #differentieller Fehler
self.err_prev = self.err_curr
self.err_curr = outputs - setpoint #same as proportional error
self.err_diff = self.err_curr - self.err_prev #differentieller Fehler

#if self.phase > 0:#field cools off (heating demand building)
if self.err_prop < 0:
if self.err_diff > 0:
cost += self.cost_fac[6] * self.err_diff #Differential penalization
else:
cost += self.cost_fac[7] * (-(self.err_diff)) #Differential reward
#if self.phase > 0:#field cools off (heating demand building)
if self.err_prop < 0:
if self.err_diff > 0:
cost += self.cost_fac[6] * self.err_diff #Differential penalization
else:
if self.err_diff > 0:
cost += self.cost_fac[7] * self.err_diff
else:
cost += self.cost_fac[6] * (-(self.err_diff))
cost += self.cost_fac[7] * (-(self.err_diff)) #Differential reward
else:
if self.err_diff > 0:
cost += self.cost_fac[7] * self.err_diff
else:
cost += self.cost_fac[6] * (-(self.err_diff))
#else:#field heats up (cooling demand building)
# if self.err_prop > 0:
# if self.err_diff > 0:
Expand Down
106 changes: 79 additions & 27 deletions pyDMPC/ControlFramework/Subsystem_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Modeling
import System
import Time
import numpy as np

class Subsystem:

Expand Down Expand Up @@ -80,8 +81,10 @@ def __init__(self, sys_id):
self.cost_rec = []
self.setpoint_rec = []
self.setpoint_send = []
self.setpoint_prev = []
self.command_send = []
self.command_rec = []
self.err_prop = 0
self.err_integ = 0
self.err_prev = 0
self.err_curr = 0
Expand All @@ -95,7 +98,7 @@ def __init__(self, sys_id):
self.fin_command = 0
self.traj_var = Init.traj_var[sys_id]
self.traj_points = Init.traj_points[sys_id]
self.counter = 0
self.phase = 0

def prepare_model(self):
"""Prepares the model of a subsystem according to the subsystem's model
Expand All @@ -118,6 +121,8 @@ def prepare_model(self):
model.load_mod()
elif self.model_type == "Linear":
model = Modeling.LinMod(self.sys_id)
elif self.model_type == "const":
model = Modeling.ConstMod(self.sys_id)
else:
model = Modeling.FuzMod(self.sys_id)
return model
Expand Down Expand Up @@ -197,8 +202,9 @@ def interp_minimize(self, interp):
opt_command.append(self.commands[min_ind])

if self.traj_var != []:
traj_costs = []
#traj_costs = []
traj = self.model.get_results(self.traj_var[0])
self.setpoint_prev = self.setpoint_send
self.setpoint_send = traj[10]

else:
Expand Down Expand Up @@ -232,48 +238,94 @@ def interp_minimize(self, interp):
else:
self.coup_vars_send = opt_outputs[0]


def calc_cost(self, command, outputs):
import scipy.interpolate

cost = self.cost_fac[0] * command

#if self.cost_rec != []:
#energy_heater = self.model.get_results("chemicalEnergy[-1]")
#cost += self.cost_fac[1] * energy_heater #Realkosten Chiller/Heater
#energy_heatpump = self.model.get_results("heatPumpEnergy[-1]")
#cost += self.cost_fac[2] * energy_heatpump #Realkosten Strom Wärmepumpe

if self.cost_rec != []:
cost += self.cost_fac[1] * self.model.get_results("chemicalEnergy[-1]") #Realkosten Chiller/Heater
cost += self.cost_fac[2] * self.model.get_results("heatPumpEnergy[-1]") #Realkosten Electricity HP

if type(self.cost_rec) is scipy.interpolate.interpolate.interp1d:
cost += self.cost_fac[3] * self.cost_rec(outputs)
cost += self.cost_fac[1] * self.cost_rec(outputs)
elif type(self.cost_rec) is list:
cost += self.cost_fac[3] * self.cost_rec[0]
cost += self.cost_fac[1] * self.cost_rec[0]
else:
cost += self.cost_fac[3] * self.cost_rec
cost += self.cost_fac[1] * self.cost_rec

if self.setpoint_rec != []:
setpoint = self.setpoint_rec
else:
setpoint = self.model.states.set_points[0]

if self.model.states.set_points != []:
cost += (self.cost_fac[4] * (outputs - setpoint)**2)
#
# if self.setpoint_rec != []:
# self.

if self.cost_rec != []:
self.err_integ += (outputs - setpoint)
if self.err_integ > 0:
cost += self.cost_fac[5] * self.err_integ
else:
cost += self.cost_fac[6] * self.err_integ
#self.phase = self.setpoint_prev - self.setpoint_rec
#
#if > 0: field for heating the building, if < 0: field for cooling the building


#Regelung in Anlehnung an PID Regelverhalten
#Proportionalanteil
self.err_prop = outputs - setpoint #Deviation from setpoint (proportional & integral)

#if self.phase > 0:#if field cools off
if self.err_prop < 0:
cost += self.cost_fac[2]*(setpoint - outputs) #Penalty Deviation (proportional)
else:
cost += self.cost_fac[3]*(setpoint - outputs) #Reward Deviation (proportional)
#else:#if field heats up
# if self.err_prop > 0:
# cost += self.cost_fac[2]*self.err_prop #Penalty Deviation (proportional)
# else:
# cost += self.cost_fac[3]*(setpoint - outputs) #Reward Deviation (proportional)

self.counter += 1
self.err_prev = self.err_curr
self.err_curr = outputs - setpoint
#Integralteil
self.err_integ += outputs - setpoint

#if self.cost_rec != []:
#if self.phase > 0: #if field cools off
if self.err_integ < 0:
cost += self.cost_fac[4] * (-(self.err_integ)) #Integral penalty
else:
cost += self.cost_fac[5] * self.err_integ #Integral reward
# else:
# if self.err_integ > 0:
# cost += self.cost_fac[4] * self.err_integ #Integral penalization
# else:
# cost += self.cost_fac[5] * (-(self.err_integ)) #Integral reward
#Differentialteil
self.err_prev = self.err_curr
self.err_curr = outputs - setpoint #same as proportional error
self.err_diff = self.err_curr - self.err_prev #differentieller Fehler

if self.counter > 1:
self.err_diff = self.err_curr - self.err_prev
if self.err_diff > 0:
cost += self.cost_fac[7] * self.err_diff
else:
cost += self.cost_fac[8] * self.err_diff
#if self.phase > 0:#field cools off (heating demand building)
if self.err_prop < 0:
if self.err_diff > 0:
cost += self.cost_fac[6] * self.err_diff #Differential penalization
else:
cost += self.cost_fac[7] * (-(self.err_diff)) #Differential reward
else:
if self.err_diff > 0:
cost += self.cost_fac[7] * self.err_diff
else:
cost += self.cost_fac[6] * (-(self.err_diff))
#else:#field heats up (cooling demand building)
# if self.err_prop > 0:
# if self.err_diff > 0:
# cost += self.cost_fac[6] * self.err_diff
# else:
# cost += self.cost_fac[7] * (-(self.err_diff))
# else:
# if self.err_diff > 0:
# cost += self.cost_fac[7] * self.err_diff
# else:
# cost += self.cost_fac[6] * (-(self.err_diff))

return cost

Expand Down
Loading

0 comments on commit 30fee76

Please sign in to comment.