Skip to content

Commit

Permalink
* Field and Field_new models for longterm simulation & Building model…
Browse files Browse the repository at this point in the history
… modified, python script for field_long modified, Init adaption in progress #33
  • Loading branch information
MStillerEBC committed May 16, 2019
1 parent 3e62c19 commit f2421b3
Show file tree
Hide file tree
Showing 19 changed files with 995 additions and 78 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* linguist-vendored
*.py linguist-vendored=false
50 changes: 35 additions & 15 deletions pyDMPC/ControlFramework/Init.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

""" Settings for BExMoC algorithm """
# So far: For all subsystems the same settings
factors_BCs = [5, 0.03] # order: BC1, BC2, ...
center_vals_BCs = [15, 0.001]
amount_lower_vals = [2, 0]
amount_upper_vals = [2, 1]
factors_BCs = [1.5, 0.03] # order: BC1, BC2, ...
center_vals_BCs = [30, 0.001]
amount_lower_vals = [9, 0]
amount_upper_vals = [9, 1]
exp_BCs = [1, 1]
amount_vals_BCs = [1, 1]

Expand All @@ -40,7 +40,7 @@
sim_time_global = 10000 # -> not used yet
sync_rate = 5*60 # Synchronisation rate in seconds
optimization_interval = 10*60 # After one interval the optimization is repeated
prediction_horizon = 86400 #Common prediction horizon in seconds
prediction_horizon = 3600 #Common prediction horizon in seconds

""" Directories and Modelica libraries """
# Path where the main working directory shall be created
Expand Down Expand Up @@ -122,29 +122,48 @@
IDs_inputs = []
T_set = []
Q_set = []
variation = []

""" Subsystems """
# Ground
name.append('Geothermal_Field')
position.append(2)
#Ground_long
name.append('Field_long')
position.append(1)
type_subSyst.append('generator')
num_DecVars.append(0)
num_VarsOut.append(2)
num_VarsOut.append(1)
bounds_DVs.append([0,0])
model_path.append('ModelicaModels.SubsystemModels.DetailedModels.Geo.Field')
model_path.append('ModelicaModels.SubsystemModels.DetailedModels.Geo.Field_new')
names_DVs.append(None)
output_vars.append(["supplyTemperature.T","massFlow.m_flow"])
output_vars.append(["supplyTemperature.T"])
initial_names.append([])
IDs_initial_values.append([])
IDs_inputs.append(["fieldTemperature_in","fieldMassflow_in"])
cost_par.append("decisionVariables.y[1]")
IDs_inputs.append(["fieldTemperature_in"])
cost_par.append("")
T_set.append(285)
Q_set.append(2200)
variation.append(False)

## Ground_short
#name.append('Field_short')
#position.append(2)
#type_subSyst.append('generator')
#num_DecVars.append(0)
#num_VarsOut.append(2)
#bounds_DVs.append([0,0])
#model_path.append('ModelicaModels.SubsystemModels.DetailedModels.Geo.Field')
#names_DVs.append(None)
#output_vars.append(["supplyTemperature.T","massFlow.m_flow"])
#initial_names.append([])
#IDs_initial_values.append([])
#IDs_inputs.append(["fieldTemperature_in","fieldMassflow_in"])
#cost_par.append("decisionVariables.y[1]")
#T_set.append(285)
#Q_set.append(2200)
#variation.append(True)


# Building
name.append('Building')
position.append(1)
position.append(2)
type_subSyst.append('consumer')
num_DecVars.append(1)
num_VarsOut.append(2)
Expand All @@ -158,3 +177,4 @@
cost_par.append("decisionVariables.y[1]")
T_set.append(295)
Q_set.append(0)
variation.append(True)
Binary file modified pyDMPC/ControlFramework/__pycache__/Init.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file modified pyDMPC/ControlFramework/__pycache__/Subsystem.cpython-36.pyc
Binary file not shown.
Binary file modified pyDMPC/ControlFramework/__pycache__/System.cpython-36.pyc
Binary file not shown.
Binary file modified pyDMPC/ControlFramework/algorithm/__pycache__/BExMoC.cpython-36.pyc
Binary file not shown.
90 changes: 58 additions & 32 deletions pyDMPC/ControlFramework/field_long.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,105 @@
import numpy as np
import matplotlib.pyplot as plt
#import matplotlib.pyplot as plt
import pandas as pd
import xlrd
import scipy
from scipy.integrate import simps
#import xlrd
#import scipy
#from scipy.integrate import simps
import pickle
import os
import sys

import scipy.io as sio
import Init
#
sys.path.insert(0, os.path.join('C:\\', 'Program Files (x86)', 'Dymola 2018', 'Modelica',
'Library', 'python_interface', 'dymola.egg'))

#Simulation of field model
#Import dymola package
from dymola.dymola_interface import DymolaInterface

from modelicares import SimRes
# Start the interface
dymola = DymolaInterface()

# Location of your libraries
path_lib1 = r'C:\mst\pyDMPC\pyDMPC\ModelicaModels\ModelicaModels'
path_lib2 = r'C:\mst\modelica-buildings\Buildings'
path_lib3 = r'C:\mst\AixLib\Aixlib'
path_lib1 = r'C:\\mst\\pyDMPC\\pyDMPC\\ModelicaModels\\ModelicaModels'
path_lib2 = r'C:\\mst\\modelica-buildings\\Buildings'
path_lib3 = r'C:\\mst\\AixLib\\Aixlib'
path_lib = [path_lib1, path_lib2, path_lib3]

# Location where to store the results
path_res = r'C:\mst\dymola\Geo_long'
path_res = r'C:\\mst\\dymola\\Geo_long\\'

# Name of the main working directory
import time
timestr = time.strftime("%Y%m%d_%H%M%S")
name_wkdir = r'pyDMPC_' + 'wkdir' + timestr

# Open AixLib
# Open AixLib, Buildings & pyDMPC Modlica Models
dymola.openModel(path=os.path.join(path_lib3, 'package.mo'))
dymola.openModel(path=os.path.join(path_lib2, 'package.mo'))
dymola.openModel(path=os.path.join(path_lib1, 'package.mo'))

# Translate any model you'd like to simulate
dymola.translateModel('ModelicaModels.SubsystemModels.DetailedModels.Geo.Field')
#obj_fnc_val = 'objectiveFunction'

#parameters of the field model

#Input: combiTable "variation": heatflow of the houses need, m_flow
Q0_heat = np.array([500, 600, 300, 100, 50, 10, 0, 5, 80, 250, 300, 450])
Q0_cold = np.array([25, 25, 110, 200, 400, 600, 650, 600, 450, 300, 150, 50])
Q0_heat = np.array([5000, 6000, 3000, 1000, 500, 100, 0, 50, 800, 2500, 3000, 4500]) #in kWh
Q0_cold = np.array([250, 250, 1100, 2000, 4000, 6000, 6500, 6000, 4500, 3000, 1500, 500]) #in kWh
days = np.array([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
dQ = np.array(Q0_heat/days - Q0_cold/days) #in kWh
dQ = np.array(Q0_heat/days - Q0_cold/days) #in kWh/day
dt = 24
Q_flow = np.divide(dQ*1000,dt) #in W

#Q_set as needed heatflow every hour a year
#Q_set as needed heatflow every hour a year in W
Q_set = [Q_flow[0]] *days[0] *24 + [Q_flow[1]] *days[1] *24 + [Q_flow[2]] *days[2] *24 + \
[Q_flow[3]] *days[3] *24 + [Q_flow[4]] *days[4] *24 + [Q_flow[5]] *days[5] *24 + \
[Q_flow[6]] *days[6] *24 + [Q_flow[7]] *days[7] *24 + [Q_flow[8]] *days[8] *24 + \
[Q_flow[9]] *days[9] *24 + [Q_flow[10]] *days[10] *24 + [Q_flow[11]] *days[11] *24

m_flow = [0.00025]*365 #in m³/s
V_flow = 0.00025 #in m³/s
rho = 1000
m_flow = [V_flow*rho]*8760 #in kg/s

starttime = 0.0
stoptime = 1*365*24*3600
time = np.arange(0, stoptime, 3600)

tab = np.array([time] + [Q_set] + [m_flow])
BC_array = tab.transpose(1,0)

tab1 = np.array([Q_set], [m_flow])
sio.savemat((path_res +'\\'+ Init.fileName_BCsInputTable + '.mat'), {Init.tableName_BCsInputTable :BC_array})

#testList = [variation.column[1],variation.column[2]]
#testValues = [BC_array[1],BC_array[2]]
testList = ['']
testValues = [None]
variable_name=['']
final_names = ['supplyTemperature.T']

# Simulate the model
output = dymola.simulateExtendedModel(
problem='',
startTime=0.0,
stopTime=94608000,
outputInterval=3600,
method="Dassl",
tolerance=0.0001,
resultFile=os.path.join(dir_result, 'demo_results'),
finalNames=[''],
)

problem = 'ModelicaModels.SubsystemModels.DetailedModels.Geo.Field_new',
startTime = starttime,
stopTime = stoptime,
outputInterval = 3600,
method = "Dassl",
tolerance = 1,
resultFile = path_res +'dsres',
finalNames = final_names,
initialNames = testList,
initialValues = testValues
)
dymola.close()

#field temperature regarding the longterm set temperature = trajectory
T0 = 285 #ungestörte Erdreichtemperatur
T_set = T0*np.ones(3*365*24) #Vorlauftemperatur ins Feld (Soll)
pickle_out = open("T_set.pickle","wb")
pickle.dump(T_set, pickle_out)
sim = SimRes(path_res + 'dsres' + '.mat')
sol = sim.to_pandas(variable_name)

#pickling field temperature
#T0 = 285 #ungestörte Erdreichtemperatur
#T_set = T0*np.ones(3*365*24) #Vorlauftemperatur ins Feld (Soll)

#pickle_out = open(path_res + "\\" + "T_out.pickle","wb")
#pickle.dump(T_out, pickle_out)
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ model SimpleModelGeo "extends Modelica.Icons.Example;extends ModelicaModels.Base
Placement(transformation(
extent={{-4,-4},{4,4}},
rotation=90,
origin={12,100})));
origin={12,100}), iconTransformation(
extent={{-4,-4},{4,4}},
rotation=90,
origin={0,16})));
Modelica.Blocks.Interfaces.RealOutput fieldMassflow_in annotation (Placement(
transformation(
extent={{-4,-4},{4,4}},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
within ModelicaModels.SubsystemModels.DetailedModels;
model CoolerML "Detailed model of the cooler for machine learning"

extends ModelicaModels.Subsystems.BaseClasses.CoolerBaseClass;
Modelica.Fluid.Sources.MassFlowSource_T IntakeAirSource(
nPorts=1,
m_flow=0.5,
redeclare package Medium = MediumAir,
X={0.03,0.97},
T=30 + 273.15,
use_X_in=true,
use_T_in=true,
use_m_flow_in=true)
annotation (Placement(transformation(extent={{-60,60},{-40,80}})));
Modelica.Fluid.Sources.Boundary_pT IntakeAirSink(
nPorts=1,
redeclare package Medium = MediumAir,
use_T_in=false,
use_X_in=false,
use_p_in=false,
p(displayUnit="Pa") = 101300)
annotation (Placement(transformation(extent={{120,60},{100,80}})));
AixLib.Utilities.Psychrometrics.ToTotalAir toTotAir
annotation (Placement(transformation(extent={{-110,56},{-90,76}})));
AixLib.Fluid.Sensors.Temperature supplyAirTemperature(redeclare package
Medium = MediumAir) "Temperature of supply air"
annotation (Placement(transformation(extent={{32,88},{52,108}})));
Modelica.Blocks.Sources.RealExpression realExpression3[4](y=hex.ele[:].mas.T)
annotation (Placement(transformation(extent={{52,-124},{72,-104}})));
Modelica.Blocks.Interfaces.RealOutput hexele1masT "Value of Real output"
annotation (Placement(transformation(extent={{92,-108},{112,-88}})));
Modelica.Blocks.Interfaces.RealOutput hexele2masT "Value of Real output"
annotation (Placement(transformation(extent={{92,-118},{112,-98}})));
Modelica.Blocks.Interfaces.RealOutput hexele3masT "Value of Real output"
annotation (Placement(transformation(extent={{92,-128},{112,-108}})));
Modelica.Blocks.Interfaces.RealOutput hexele4masT "Value of Real output"
annotation (Placement(transformation(extent={{92,-138},{112,-118}})));
Modelica.Blocks.Interfaces.RealOutput supplyTemp "Temperature in port medium"
annotation (Placement(transformation(extent={{126,88},{146,108}})));
Modelica.Blocks.Interfaces.RealInput valveOpening "Input signal connector"
annotation (Placement(transformation(extent={{-160,-70},{-120,-30}})));
Modelica.Blocks.Sources.Constant x(k=0.007) annotation (Placement(
transformation(
extent={{10,-10},{-10,10}},
rotation=180,
origin={-130,66})));
Modelica.Blocks.Sources.Constant mflow(k=0.35) annotation (Placement(
transformation(
extent={{10,-10},{-10,10}},
rotation=180,
origin={-100,100})));
Modelica.Blocks.Interfaces.RealInput inflowTemp
"Prescribed fluid temperature"
annotation (Placement(transformation(extent={{-160,0},{-120,40}})));
Modelica.Thermal.HeatTransfer.Celsius.ToKelvin toKelvin annotation (
Placement(transformation(
extent={{10,-10},{-10,10}},
rotation=180,
origin={-84,20})));
Modelica.Thermal.HeatTransfer.Celsius.FromKelvin
fromKelvin
annotation (
Placement(transformation(
extent={{10,-10},{-10,10}},
rotation=180,
origin={82,98})));
equation
connect(toTotAir.XiTotalAir, IntakeAirSource.X_in[1])
annotation (Line(points={{-89,66},{-62,66}}, color={0,0,127}));
connect(toTotAir.XNonVapor, IntakeAirSource.X_in[2]) annotation (Line(points=
{{-89,62},{-82,62},{-82,66},{-62,66}}, color={0,0,127}));
connect(IntakeAirSource.ports[1], hex.port_a2)
annotation (Line(points={{-40,70},{-12,70}}, color={0,127,255}));
connect(hex.port_b2, IntakeAirSink.ports[1])
annotation (Line(points={{8,70},{100,70}}, color={0,127,255}));
connect(realExpression3[1].y, hexele1masT) annotation (Line(points={{73,-114},
{84,-114},{84,-98},{102,-98}}, color={0,0,127}));
connect(realExpression3[2].y, hexele2masT) annotation (Line(points={{73,-114},
{84,-114},{84,-108},{102,-108}}, color={0,0,127}));
connect(realExpression3[3].y, hexele3masT) annotation (Line(points={{73,-114},
{82,-114},{82,-118},{102,-118}}, color={0,0,127}));
connect(realExpression3[4].y, hexele4masT) annotation (Line(points={{73,-114},
{84,-114},{84,-128},{102,-128}}, color={0,0,127}));
connect(hex.port_b2, supplyAirTemperature.port) annotation (Line(points={{8,
70},{26,70},{26,88},{42,88}}, color={0,127,255}));
connect(convertCommand.u, valveOpening)
annotation (Line(points={{-103.2,-50},{-140,-50}}, color={0,0,127}));
connect(toTotAir.XiDry, x.y)
annotation (Line(points={{-111,66},{-119,66}}, color={0,0,127}));
connect(mflow.y, IntakeAirSource.m_flow_in) annotation (Line(points={{-89,100},
{-74,100},{-74,78},{-60,78}}, color={0,0,127}));
connect(inflowTemp, toKelvin.Celsius)
annotation (Line(points={{-140,20},{-96,20}}, color={0,0,127}));
connect(toKelvin.Kelvin, IntakeAirSource.T_in) annotation (Line(points={{-73,
20},{-70,20},{-70,74},{-62,74}}, color={0,0,127}));
connect(supplyAirTemperature.T, fromKelvin.Kelvin)
annotation (Line(points={{49,98},{70,98}}, color={0,0,127}));
connect(fromKelvin.Celsius, supplyTemp)
annotation (Line(points={{93,98},{136,98}}, color={0,0,127}));
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
coordinateSystem(preserveAspectRatio=false)),
experiment(StopTime=100000, Interval=10));
end CoolerML;
Loading

0 comments on commit f2421b3

Please sign in to comment.