Debian Codegraph @

docs/source/_static/processes.txt

raw
1 # coding=utf8
2 """
3 This file contains classes for simulating, controlling and observing a fridge.
4
5 @author: Stefan Scherfke
6 @contact: stefan.scherfke at uni-oldenburg.de
7 """
8
9 from math import exp
10 import logging
11 import random
12
13 from SimPy.Simulation import Process, Simulation, \
14 activate, hold, initialize, now, simulate
15
16 log = logging.getLogger('Processes')
17
18 class Fridge(Process):
19 """
20 This class represents a simulated fridge.
21
22 It's temperature T for and equidistant series of time steps is computed by
23 $T_{i+1} = \epsilon \cdot T_i + (1 - \epsilon) \cdot \left(T^O - \eta
24 \cdot \frac{q_i}{A}\right)$ with $\epsilon = e^{-\frac{\tau A}{m_c}}$.
25 """
26
27 def __init__(self, sim, T_O = 20.0, A = 3.21, m_c = 15.97, tau = 1.0/60,
28 eta = 3.0, q_i = 0.0, q_max = 70.0,
29 T_i = 5.0, T_range = [5.0, 8.0], noise = False):
30 """
31 Init all required variables.
32
33 @param sim: The SimPy simulation this process belongs to
34 @type sim: SimPy.Simulation
35 @param T_O: Outside temperature
36 @param A: Insulation
37 @param m_c: Thermal mass/thermal storage capacity
38 @param tau: Time span between t_i and t_{i+1}
39 @param eta: Efficiency of the cooling device
40 @param q_i: Initial/current electrical power
41 @param q_max: Power required during cool-down
42 @param T_i: Initial/current temperature
43 @param T_range: Allowed range for T_i
44 @param noise: Add noise to the fridge's parameters, if True
45 @type noise: bool
46 """
47 Process.__init__(self, sim = sim)
48 self.T_O = T_O
49 self.A = A
50 self.m_c = random.normalvariate(20, 4.5) if noise else m_c
51 self.tau = tau
52 self.eta = eta
53 self.q_i = q_i
54 self.q_max = q_max
55 self.T_i = random.uniform(T_range[0], T_range[1]) if noise else T_i
56 self.T_range = T_range
57
58 def run(self):
59 """
60 Calculate the fridge's temperature for the current time step.
61 """
62 while True:
63 epsilon = exp(-(self.tau * self.A) / self.m_c)
64 self.T_i = epsilon * self.T_i + (1 - epsilon) \
65 * (self.T_O - self.eta * (self.q_i / self.A))
66 if self.T_i >= self.T_range[1]:
67 self.q_i = self.q_max # Cool down
68 elif self.T_i <= self.T_range[0]:
69 self.q_i = 0.0 # Stop cooling
70 log.debug('T_i: %2.2f°C at %.2f' % (self.T_i, self.sim.now()))
71 yield hold, self, self.tau
72
73 def coolDown(self):
74 """
75 Start cooling down now!
76 """
77 self.q_i = self.q_max
78
79
80 class FridgeObserver(Process):
81 """
82 This process observes the temperature and power consumption of a set of
83 fridges.
84 """
85
86 def __init__(self, sim, fridges, tau, aggSteps):
87 """
88 Init the observer.
89
90 @param sim: The SimPy simulation this process belongs to
91 @type sim: SimPy.Simulation
92 @param fridges: A list of fridges to be observed
93 @type fridges: tuple of Fridge
94 @param tau: Time interval for observations
95 @type tau: float
96 @param aggSteps: Specifies after how many timesteps tau the collected
97 data is aggregated and stored.
98 @type aggSteps: int
99 """
100 Process.__init__(self, sim = sim)
101 self._fridges = fridges
102 self._tau = tau
103 self._aggSteps = aggSteps
104 self._data = []
105
106 def run(self):
107 """
108 Start observation
109 """
110 aggSteps = 0
111 consumption = 0
112 lastProgUpdate = 0
113 while True:
114 prog = self.sim.now() * 100 / self.sim._endtime
115 if int(prog) > lastProgUpdate:
116 log.info('Progress: %d%%' % prog)
117 lastProgUpdate = prog
118 if (aggSteps >= self._aggSteps):
119 log.debug('Aggregating at %.2f' % self.sim.now())
120 self._data.append(consumption/self._aggSteps)
121 consumption = 0
122 aggSteps = 0
123
124 for fridge in self._fridges:
125 consumption += fridge.q_i
126 aggSteps += 1
127 yield hold, self, self._tau
128
129 def getData(self):
130 """
131 Return the collected data
132
133 @return: a list with the collected data
134 """
135 return self._data
136
137
138 if __name__ == '__main__':
139 logging.basicConfig(
140 level = logging.DEBUG,
141 format = '%(levelname)-8s %(asctime)s %(name)s: %(message)s')
142
143 tau = 1./60 # Step size 1min
144 aggSteps = 15 # Aggregate consumption in 15min blocks
145 params = {'tau': tau}
146
147 sim = Simulation()
148
149 fridge = Fridge(sim, **params)
150 observer = FridgeObserver(sim, [fridge], tau, aggSteps)
151
152 sim.activate(fridge, fridge.run(), at = 0)
153 sim.activate(observer, observer.run(), at = 0)
154 sim.simulate(until = 4 + tau)
155 print observer.getData()