-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
165 lines (141 loc) · 4.87 KB
/
demo.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
160
161
162
163
164
165
import itertools
import random
from mosaik.util import connect_randomly, connect_many_to_one
import mosaik
sim_config = {
'CSV': {
'python': 'mosaik_csv:CSV',
},
'DB': {
'cmd': 'mosaik-hdf5 %(addr)s',
},
'HouseholdSim': {
'python': 'householdsim.mosaik:HouseholdSim',
# 'cmd': 'mosaik-householdsim %(addr)s',
},
'PyPower': {
'python': 'mosaik_pypower.mosaik:PyPower',
# 'cmd': 'mosaik-pypower %(addr)s',
},
'WebVis': {
'cmd': 'mosaik-web -s 0.0.0.0:8000 %(addr)s',
},
'Ethereum': {
'python': 'model.mosaik_ethereum:Ethereum'
},
}
# START = '2014-06-17 00:00:00'
START = '2014-01-01 00:00:00'
END = 1 * 24 * 3600 # 1 day -- originaly 31
PV_DATA = 'data/pv_2kw_rolling15.csv'
# PROFILE_FILE = 'data/profiles_truncated.data'
PROFILE_FILE = 'data/profiles.data.gz'
GRID_NAME = 'demo_lv_grid'
GRID_FILE = 'data/%s.json' % GRID_NAME
BLOCKCHAIN = False
def main():
random.seed(23)
world = mosaik.World(sim_config)
create_scenario(world)
import pdb; pdb.set_trace()
world.run(until=END) # As fast as possilbe
# world.run(until=END, rt_factor=1/(60 * 15)) # Real-time 15min -> 1sec
def create_scenario(world):
# Start simulators
pypower = world.start('PyPower', step_size=15*60)
hhsim = world.start('HouseholdSim')
pvsim = world.start('CSV', sim_start=START, datafile=PV_DATA)
if BLOCKCHAIN:
ethereum_sim = world.start('Ethereum', step_size=15*60)
# Instantiate models
grid = pypower.Grid(gridfile=GRID_FILE).children
houses = hhsim.ResidentialLoads(sim_start=START,
profile_file=PROFILE_FILE,
grid_name=GRID_NAME).children
pvs = pvsim.PV.create(15)
if BLOCKCHAIN:
ethereum = ethereum_sim.Ethereum.create(10)
else:
ethereum = None
# Connect entities
connect_buildings_to_grid(world, houses, pvs, ethereum, grid)
# Database
db = world.start('DB', step_size=60, duration=END)
hdf5 = db.Database(filename='demo.hdf5')
connect_many_to_one(world, houses, hdf5, 'P_out')
connect_many_to_one(world, pvs, hdf5, 'P')
nodes = [e for e in grid if e.type in ('RefBus, PQBus')]
connect_many_to_one(world, nodes, hdf5, 'P', 'Q', 'Vl', 'Vm', 'Va')
branches = [e for e in grid if e.type in ('Transformer', 'Branch')]
connect_many_to_one(world, branches, hdf5,
'P_from', 'Q_from', 'P_to', 'P_from')
# Web visualization
webvis = world.start('WebVis', start_date=START, step_size=60)
webvis.set_config(ignore_types=['Topology', 'ResidentialLoads', 'Grid',
'Database'])
vis_topo = webvis.Topology()
connect_many_to_one(world, nodes, vis_topo, 'P', 'Vm')
webvis.set_etypes({
'RefBus': {
'cls': 'refbus',
'attr': 'P',
'unit': 'P [W]',
'default': 0,
'min': 0,
'max': 30000,
},
'PQBus': {
'cls': 'pqbus',
'attr': 'Vm',
'unit': 'U [V]',
'default': 230,
'min': 0.99 * 230,
'max': 1.01 * 230,
},
})
connect_many_to_one(world, houses, vis_topo, 'P_out')
webvis.set_etypes({
'House': {
'cls': 'load',
'attr': 'P_out',
'unit': 'P [W]',
'default': 0,
'min': 0,
'max': 3000,
},
})
connect_many_to_one(world, pvs, vis_topo, 'P')
webvis.set_etypes({
'PV': {
'cls': 'gen',
'attr': 'P',
'unit': 'P [W]',
'default': 0,
'min': -10000,
'max': 0,
},
})
def connect_buildings_to_grid(world, houses, pvs, ethereum, grid):
# Get all bus with PQBus in a dictionnary {id: bus element}
buses = filter(lambda e: e.type == 'PQBus', grid)
buses = {b.eid.split('-')[1]: b for b in buses}
# Get the house data
house_data = world.get_data(houses, 'node_id')
# Assign node and buildings (pv + house)
for index, house in enumerate(houses):
# Get the node id
node_id = house_data[house]['node_id']
# Assign a PV to the node
if index < len(pvs):
world.connect(pvs[index], buses[node_id], 'P')
# Assign a house load to the node
world.connect(house, buses[node_id], ('P_out', 'P'))
# Connect Ethereum database to a node with PV and house load
if BLOCKCHAIN:
if (index in range(10, 20)) and index < len(pvs):
world.connect(house, ethereum[index - 10], ('P_out', 'load'))
world.connect(pvs[index], ethereum[index - 10], ('P', 'gene'))
elif (index in range(10, 20)):
world.connect(house, ethereum[index - 10], ('P_out', 'load'))
if __name__ == '__main__':
main()