-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfinc.py
299 lines (231 loc) · 11 KB
/
infinc.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'''
Copyright 2021 Lok Yan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import math #for sqrt
import time #for sleep
import threading #Thread
class SimpleThermometer :
def __init__ (self, source) :
self.source = source
def setSource(self, source) :
self.source = source
def getTemperature(self) :
return self.source.getTemperature()
'''
Very simple heater that turns on and pumps the powerOutput (watts).
'''
class SimpleHeatGenerator :
def __init__ (self, powerOutput, setTemperature, thermometer) :
self.power = powerOutput
self.setTemperature = setTemperature
self.thermometer = thermometer
def setThermometer(self, thermo) :
self.thermometer = thermo
'''
Returns the number of Watts output by this heater for this timestep.
'''
def getOutput(self) :
if self.thermometer :
if self.thermometer.getTemperature() < self.setTemperature :
return self.power
return 0
BODY_TEMP = (273 + 37)
ROOM_TEMP = (273 + 20)
'''
This class represents a very simple human body (in the general sense).
'''
class Human :
DENSITY = 1000 # UNIT: kg / m**3
SPECIFIC_HEAT = 3500 # UNIT: J / kg / degK
THERMAL_TRANSFER = 5.5 # UNIT: J / s / m**2 / degK
def __init__ (self, mass, length, temperature) :
threading.Thread.__init__(self) #init the thread parts
self.mass = mass # UNIT: kg
self.length = length # UNIT: m
self.bodyHeater = SimpleHeatGenerator(100, BODY_TEMP, self)
# UNIT: W = J / s
self.temperature = temperature # UNIT: degK
self.surfaceArea = math.sqrt(self.mass * self.length * 100 / 3600)
# UNIT: m**2
self.volume = self.mass / Human.DENSITY # UNIT: m**3, by density
self.energy = self.calculateEnergy() # UNIT: J
def setHeater(self, bhg) :
self.bodyHeater = bhg
def calculateEnergy(self) :
# The energy content is calculated based on the current temperature
return (Human.SPECIFIC_HEAT * self.mass * self.temperature)
def calculateTemperature(self) :
# The temperature is based on the amount of energy
return (self.energy / Human.SPECIFIC_HEAT / self.mass)
def getEnergy(self) :
return self.energy
def getTemperature(self) :
return self.temperature
def getVolume(self) :
return self.volume
def addEnergy(self, e) :
self.energy += e
#update the temperature
self.temperature = self.calculateTemperature()
def simulateTransferWithChamber(self, timestep, envTemp) :
# envTemp is the temperature of the environment
# so we will have to calculate the temperature difference first
tempDiff = envTemp - self.temperature
# then figure out how much energy was generated by heater
if self.bodyHeater :
energyHeater = self.bodyHeater.getOutput() * timestep # UNIT: J
else :
energyHeater = 0
# then using temperature difference how much energy transferred
energyTransfer = Human.THERMAL_TRANSFER * timestep * self.surfaceArea * tempDiff
#update the energy
self.addEnergy(energyHeater + energyTransfer)
# notice that energyTransfer can be negative return that so it can be
# used to either add or remove energy from the environment
# Return the negation of calculated energy transfer
return (-energyTransfer)
'''
Not smart yet, but what it does is poll the source for the temperature
and then cache it for later retrieval.
'''
class SmartThermometer (threading.Thread) :
def __init__ (self, source, updatePeriod) :
threading.Thread.__init__(self, daemon = True)
#set daemon to be true, so it doesn't block program from exiting
self.source = source
self.updatePeriod = updatePeriod
self.curTemperature = 0
self.updateTemperature()
def setSource(self, source) :
self.source = source
def setUpdatePeriod(self, updatePeriod) :
self.updatePeriod = updatePeriod
def updateTemperature(self) :
self.curTemperature = self.source.getTemperature()
def getTemperature(self) :
return self.curTemperature
def run(self) : #the running function
while True :
self.updateTemperature()
time.sleep(self.updatePeriod)
'''
Not smart yet, but at least it runs as a thread
'''
class SmartHeater (threading.Thread) :
def __init__ (self, powerOutput, setTemperature, thermometer, updatePeriod) :
threading.Thread.__init__(self, daemon = True)
self.power = powerOutput
self.setTemperature = setTemperature
self.thermometer = thermometer
self.updatePeriod = updatePeriod
self.curOutput = 0
def setThermometer(self, thermo) :
self.thermometer = thermo
def setUpdatePeriod(self, updatePeriod) :
self.updatePeriod = updatePeriod
'''
Returns the number of Watts output by this heater for this timestep.
'''
def getOutput(self) :
return self.curOutput
def run(self) :
while True :
if self.thermometer :
if self.thermometer.getTemperature() < self.setTemperature :
self.curOutput = self.power
else :
self.curOutput = 0
time.sleep(self.updatePeriod)
'''
This class represents the incubator / chamber
'''
class Incubator :
DENSITY = 1.2041 # UNIT: kg / m**3
SPECIFIC_HEAT = 1012 # UNIT: J / kg / degK
THERMAL_TRANSFER = 5.1 # UNIT: J / s / m**2 / degK
def __init__ (self, width, depth, height, temperature, roomTemperature) :
self.width = width # UNIT: m
self.depth = depth # UNIT: m
self.height = height # UNIT: m
self.incuHeater = None # UNIT: W = J / s
self.temperature = temperature # UNIT: degK
self.roomTemperature = roomTemperature # UNIT: degK
self.volume = self.depth * self.width * self.height
# UNIT: m**3
self.mass = Incubator.DENSITY * self.volume # UNIT: kg
self.energy = self.calculateEnergy() # UNIT: J
self.surfaceArea = self.width * self.depth + 2 * self.width * self.height + 2 * self.width * self.depth
self.infant = None
def setHeater(self, ihg) :
self.incuHeater = ihg
def calculateEnergy(self) :
# The energy content is calculated based on the current temperature
return (Incubator.SPECIFIC_HEAT * self.mass * self.temperature)
def calculateTemperature(self) :
# The temperature is based on the amount of energy
return (self.energy / Incubator.SPECIFIC_HEAT / self.mass)
def getEnergy(self) :
return self.energy
def getTemperature(self) :
return self.temperature
def addEnergy(self, e) :
self.energy += e
#update the temperature
self.temperature = self.calculateTemperature()
def openIncubator(self) :
#let's assume that when you open the incubator
# the temperature settles half way to room temperature
self.temperature += (self.roomTemperature - self.temperature) / 2
#update energy
self.energy = self.calculateEnergy()
def addInfant(self, newInfant) :
# Let's see if someone catches these
self.infant = newInfant
#First, lets calculate the displacement in volume so we can update energy
airVolume = self.volume - self.infant.volume
#now update the energy content based on the current temperature
airMass = Incubator.DENSITY * airVolume
energy = Incubator.SPECIFIC_HEAT * airMass * self.temperature
def closeIncubator(self) :
pass #nothing to do here for the simulation
def hasInfant(self) :
return not self.infant is None
def simulateTransferWithRoom(self, timestep, envTemp) :
# envTemp is the temperature of the environment
# so we will have to calculate the temperature difference first
tempDiff = envTemp - self.temperature
# then figure out how much energy was generated by heater
if self.incuHeater :
energyHeater = self.incuHeater.getOutput() * timestep # UNIT: J
else :
energyHeater = 0
# then using temperature difference how much energy transferred
energyTransfer = Incubator.THERMAL_TRANSFER * timestep * self.surfaceArea * tempDiff
#update the energy
self.addEnergy(energyHeater + energyTransfer)
# notice that energyTransfer can be negative return that so it can be
# used to either add or remove energy from the environment
# Return the negation of calculated energy transfer
return (-energyTransfer)
class Simulator (threading.Thread) :
def __init__ (self, infant, incubator, roomTemp, timeStep, sleepTime) :
#infWeight, infLength, infTemp, infant, incWidth, incDepth, incHeight, incTemp, roomTemp, timeStep) :
threading.Thread.__init__(self, daemon = True)
self.infant = infant #Human(infWeight, infLength, infTemp)
self.incubator = incubator #Incubator(incWidth, incDepth, incHeight, incTemp, roomTemp)
self.roomTemperature = roomTemp
self.iteration = 0
self.timeStep = timeStep
self.sleepTime = sleepTime
def run(self) :
while True :
#1. Simulate infant using incubator temperature
e = self.infant.simulateTransferWithChamber(self.timeStep, self.incubator.getTemperature())
#2. Infant is updated, now incubator with room
e2 = self.incubator.simulateTransferWithRoom(self.timeStep, self.roomTemperature)
#3. Add the energy gain or loss from infant
self.incubator.addEnergy(e)
time.sleep(self.sleepTime)