forked from casemsee/EnergyManagementSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_ems.py
159 lines (140 loc) · 7.17 KB
/
local_ems.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
156
157
158
159
# The main entrance of local energy management system (UEMS)
# Date: 21/Jan/2018
# Authors: Tianyang Zhao
# Mail: [email protected]
from apscheduler.schedulers.blocking import BlockingScheduler # Time scheduler
import zmq # The package for information and communication
from start_up import start_up_lems
from modelling.information import static_information_pb2 as static_info
from copy import deepcopy
from database_management.database_functions import db_session
from configuration.configuration_database import history_data
from utils import Logger
from configuration.configuration_global import default_operation_mode
from configuration.configuration_database import rtc_local
logger = Logger("Local_ems")
class Main():
"""
Main class of local energy management system
"""
def __init__(self,socket):
"""
:param socket: The input socket of local ems
"""
from start_up import app
self.socket = socket
# self.logger = Logger("Local_ems_start_up")
ems_main = app.StartUpLems(socket)
self.Operation_mode = ems_main.run()
# database start-up operation
self.Session = ems_main.database_start_up()
self.Session_history_data = db_session(history_data)
self.Session_real_time = db_session(rtc_local)
(self.microgrid,self.microgrid_middle,self.microgrid_long) = start_up_lems.start_up() # Generate local ems models
# self.logger.info("Database has been started up!")
# operation mode
if self.Operation_mode == default_operation_mode["UEMS"]:# Local EMS work as the slave of UEMS.
self.status = ems_main.information_send(self.microgrid, static_info)
def local_ems(self):# Local ems
from real_time_operation.app import RealTimeSimulation
from short_term_operation.app import ShortTermOperation
from middle_term_operation.app import MiddleTermOperation
from long_term_operation.app import LongTermOperation
# S1: Initialize information models
microgrid = self.microgrid # Obtain the information model
microgrid_short = deepcopy(microgrid)
microgrid_middle = self.microgrid_middle
microgrid_long = self.microgrid_long
# S2: Initialize databases
session = self.Session()
session_short = self.Session()
session_middle = self.Session()
session_long = self.Session()
session_history_data = self.Session_history_data()
session_history_data_short = self.Session_history_data()
session_history_data_middle = self.Session_history_data()
session_history_data_long = self.Session_history_data()
session_real_time = self.Session_real_time
# S3: Initialize target functions
real_time_simulation = RealTimeSimulation()
short_term_operation = ShortTermOperation()
middle_term_operation = MiddleTermOperation()
long_term_operation = LongTermOperation()
# Start-up the test
for i in range(1):
# 1) real-time simulation
real_time_simulation.run(microgrid, session, session_short,session_history_data)# Real-time simulation has pasted test!
# 2) short-term operation
short_term_operation.run(microgrid_short, session_short,session_history_data_short,session_real_time) # Short-term operation has pasted test!
# 3) middle-term operation
middle_term_operation.run(microgrid_middle, session_middle,session_history_data_middle) # Middle-term operation has pasted test!
# 4) long-term operation
long_term_operation.run(microgrid_long, session_long,session_history_data_long)
# S4: Functions scheduling
sched = BlockingScheduler()
# 1) real-time simulation
# sched.add_job(lambda: real_time_simulation.run(microgrid, session, session,session_history_data),'cron', minute='0-59', second='*/5')
# 2) short-term operation
sched.add_job(lambda: short_term_operation.run(microgrid_short, session_short,session_history_data_short,session_real_time),'cron', minute='*/1', second='30')
# 3) middle-term operation
sched.add_job(lambda: middle_term_operation.run(microgrid_middle, session_middle,session_history_data_middle),'cron', minute='*/5', second='10')
# 4) long-term operation
sched.add_job(lambda: long_term_operation.run(microgrid_long, session_long,session_history_data_long),'cron', minute='*/30', second='30')
# 5) start simulation
sched.start()
def run():
# Define the local models
# Start the information connection
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
socket_upload = context.socket(zmq.REQ)
socket_upload.connect("tcp://localhost:5556")
socket_upload_ed = context.socket(zmq.REQ)
socket_upload_ed.connect("tcp://localhost:5557")
socket_upload_uc = context.socket(zmq.REQ)
socket_upload_uc.connect("tcp://localhost:5558")
socket_download = context.socket(zmq.REP)
socket_download.connect("tcp://localhost:5559")
initialize = Main(socket)
initialize.local_ems()# The local version of EMS
# session_short = initialize.Session()
# session_middle = initialize.Session()
# session_long = initialize.Session()
#
# info_ed = dynamic_information_pb2.microgrid() # Dynamic information for economic dispatch
# info_uc = dynamic_information_pb2.microgrid() # Dynamic information for unit commitment
# info_opf = single_period_information_pb2.microgrid() # Optimal power flow modelling
# local_model_short = initialize.microgrid
# # By short-term operation process
# logger.info("The short-term process in local ems starts!")
# sched_lems = BlockingScheduler() # The schedulor for the optimal power flow
# sched_lems.add_job(
# lambda: short_term_operation.short_term_operation_lems(local_model_short, socket_upload, socket_download,
# info_opf,
# session_lems_short),
# 'cron', minute='0-59', second='1') # The operation is triggered minutely
#
# logger.info("The middle-term process in local EMS starts!")
# sched_lems.add_job(
# lambda: middle_term_operation.middle_term_operation_lems(local_model_middle, socket_upload_ed, socket_download,
# info_ed,
# session_lems_middle),
# 'cron', minute='*/5', second='5') # The operation is triggered every five minute
#
# logger.info("The long term process in local EMS starts!")
# sched_lems.add_job(
# lambda: long_term_operation.long_term_operation_lems(local_model_long, socket_upload_uc, socket_download,
# info_uc,
# session_lems_long),
# 'cron', minute='*/30', second='30') # The operation is triggered every half an hour
# sched_lems.start()
# for i in range(10):
# # long_term_operation.long_term_operation_lems(local_model_long, socket_upload_uc, socket_download, info_uc,
# # session_lems_long)
# # middle_term_operation.middle_term_operation_lems(local_model_middle, socket_upload_ed, socket_download, info_ed,
# # session_lems_middle)
# short_term_operation_lems(local_model_short, socket_upload, socket_download, info_opf, session_short, logger)
if __name__ == "__main__":
# Start the main process of local energy management system
run()