-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathThermalHouse.scala
412 lines (386 loc) · 12.1 KB
/
ThermalHouse.scala
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.simona.model.thermal
import edu.ie3.datamodel.models.OperationTime
import edu.ie3.datamodel.models.input.OperatorInput
import edu.ie3.datamodel.models.input.thermal.{
ThermalBusInput,
ThermalHouseInput,
}
import edu.ie3.simona.model.participant.HpModel.HpRelevantData
import edu.ie3.simona.model.thermal.ThermalGrid.ThermalEnergyDemand
import edu.ie3.simona.model.thermal.ThermalHouse.ThermalHouseThreshold.{
HouseTemperatureLowerBoundaryReached,
HouseTemperatureTargetOrUpperBoundaryReached,
}
import edu.ie3.simona.model.thermal.ThermalHouse.{
ThermalHouseState,
temperatureTolerance,
}
import edu.ie3.util.quantities.PowerSystemUnits
import edu.ie3.util.scala.quantities.DefaultQuantities._
import edu.ie3.util.scala.quantities.{ThermalConductance, WattsPerKelvin}
import squants.energy.KilowattHours
import squants.thermal.{Kelvin, ThermalCapacity}
import squants.time.{Hours, Seconds}
import squants.{Energy, Power, Temperature, Time}
import tech.units.indriya.unit.Units
import java.util.UUID
/** A thermal house model
*
* @param uuid
* the element's uuid
* @param id
* the element's human-readable id
* @param operatorInput
* Operator input
* @param operationTime
* Operation time
* @param bus
* Thermal bus input
* @param ethLosses
* transmission coefficient of heat storage, usually in [kW/K]
* @param ethCapa
* heat energy storage capability of thermal house, usually in [kWh/K]
* @param targetTemperature
* Target room temperature [K]
* @param lowerBoundaryTemperature
* Lower temperature boundary [K]
* @param upperBoundaryTemperature
* Upper boundary temperature [K]
*/
final case class ThermalHouse(
uuid: UUID,
id: String,
operatorInput: OperatorInput,
operationTime: OperationTime,
bus: ThermalBusInput,
ethLosses: ThermalConductance,
ethCapa: ThermalCapacity,
targetTemperature: Temperature,
lowerBoundaryTemperature: Temperature,
upperBoundaryTemperature: Temperature,
) extends ThermalSink(
uuid,
id,
operatorInput,
operationTime,
bus,
) {
/** Calculate the energy demand at the instance in question. If the inner
* temperature is at or above the lower boundary temperature, there is no
* demand. If it is below the target temperature, the demand is the energy
* needed to heat up the house to the maximum temperature. The current
* (external) thermal infeed is not accounted for, as we assume, that after
* determining the thermal demand, a change in external infeed will take
* place.
*
* @param relevantData
* Data of heat pump including state of the heat pump.
* @param state
* Most recent state, that is valid for this model.
* @return
* The needed energy in the questioned tick.
*/
def energyDemand(
relevantData: HpRelevantData,
state: ThermalHouseState,
): ThermalEnergyDemand = {
/* Calculate the inner temperature of the house, at the questioned instance in time */
val duration = Seconds(relevantData.currentTick - state.tick)
val currentInnerTemp = newInnerTemperature(
state.qDot,
duration,
state.innerTemperature,
relevantData.ambientTemperature,
)
/* Determine, which temperature boundary triggers a needed energy to reach the temperature constraints */
val temperatureToTriggerRequiredEnergy =
if (
currentInnerTemp <= state.innerTemperature &&
state.qDot <= zeroKW
) {
// temperature has been decreasing and heat source has been turned off
// => we have reached target temp before and are now targeting lower temp
lowerBoundaryTemperature
} else targetTemperature
val requiredEnergy =
if (
isInnerTemperatureTooLow(
currentInnerTemp,
temperatureToTriggerRequiredEnergy,
)
) energy(targetTemperature, currentInnerTemp)
else
zeroMWh
val possibleEnergy =
if (!isInnerTemperatureTooHigh(currentInnerTemp)) {
// if upper boundary has not been reached,
// there is an amount of optional energy that could be stored
energy(upperBoundaryTemperature, currentInnerTemp)
} else
zeroMWh
ThermalEnergyDemand(requiredEnergy, possibleEnergy)
}
/** Calculate the needed energy to change from start temperature to target
* temperature.
*
* In edge cases, i.e. within the tolerance margin of target temperatures,
* the temperature difference can be negative. For these cases we set the
* temperature difference to zero, resulting in an energy demand of 0 kWh.
*
* @param targetTemperature
* The target temperature to reach.
* @param startTemperature
* The starting temperature.
* @return
* The needed energy to change.
*/
private def energy(
targetTemperature: Temperature,
startTemperature: Temperature,
): Energy = {
val temperatureDiff =
Kelvin(targetTemperature.toKelvinScale - startTemperature.toKelvinScale)
.max(Kelvin(0))
ethCapa * temperatureDiff
}
/** Check if inner temperature is higher than preferred maximum temperature
* @param innerTemperature
* The inner temperature of the house
* @param boundaryTemperature
* The applied boundary temperature to check against
*
* @return
* True, if inner temperature is too high.
*/
def isInnerTemperatureTooHigh(
innerTemperature: Temperature,
boundaryTemperature: Temperature = upperBoundaryTemperature,
): Boolean =
innerTemperature > (
boundaryTemperature - temperatureTolerance
)
/** Check if inner temperature is lower than preferred minimum temperature
*
* @return
* true, if inner temperature is too low
*/
def isInnerTemperatureTooLow(
innerTemperature: Temperature,
boundaryTemperature: Temperature = lowerBoundaryTemperature,
): Boolean =
innerTemperature < (
boundaryTemperature + temperatureTolerance
)
/** Calculate the new inner temperature of the thermal house.
*
* @param thermalPower
* added thermal power (e.g. of heat pump)
* @param duration
* time step length
* @param currentInnerTemperature
* current inner temperature
* @param ambientTemperature
* ambient temperature of thermal house
* @return
* new inner temperature
*/
def newInnerTemperature(
thermalPower: Power,
duration: Time,
currentInnerTemperature: Temperature,
ambientTemperature: Temperature,
): Temperature = {
val thermalEnergyGain = thermalPower * duration
// thermal energy loss due to the deviation between outside and inside temperature
val thermalEnergyLoss = ethLosses.calcThermalEnergyChange(
currentInnerTemperature,
ambientTemperature,
duration,
)
val energyChange = thermalEnergyGain - thermalEnergyLoss
// temperature change calculated from energy change(WattHours) and thermal capacity(Joules per Kelvin)
val temperatureChange = energyChange / ethCapa
// return value new inner temperature
currentInnerTemperature + temperatureChange
}
/** Update the current state of the house.
*
* @param relevantData
* Data of heat pump including state of the heat pump.
* @param state
* Currently applicable state
* @param lastAmbientTemperature
* Ambient temperature valid up until (not including) the current tick
* @param qDot
* New thermal influx
* @return
* Updated state and the tick in which the next threshold is reached
*/
def updateState(
relevantData: HpRelevantData,
state: ThermalHouseState,
lastAmbientTemperature: Temperature,
qDot: Power,
): (ThermalHouseState, Option[ThermalThreshold]) = {
val duration = Seconds(relevantData.currentTick - state.tick)
val updatedInnerTemperature = newInnerTemperature(
state.qDot,
duration,
state.innerTemperature,
lastAmbientTemperature,
)
/* Calculate the next given threshold */
val threshold =
nextThreshold(
relevantData.currentTick,
qDot,
updatedInnerTemperature,
relevantData.ambientTemperature,
)
(
state.copy(
tick = relevantData.currentTick,
innerTemperature = updatedInnerTemperature,
qDot = qDot,
),
threshold,
)
}
/** Determine the next threshold, that will be reached.
* @param tick
* The current tick.
* @param qDotExternal
* The external influx
* @param innerTemperature
* The inner temperature
* @param ambientTemperature
* The ambient temperature
* @return
* The next threshold, that will be reached
*/
private def nextThreshold(
tick: Long,
qDotExternal: Power,
innerTemperature: Temperature,
ambientTemperature: Temperature,
): Option[ThermalThreshold] = {
val artificialDuration = Hours(1d)
val loss = ethLosses.calcThermalEnergyChange(
innerTemperature,
ambientTemperature,
artificialDuration,
) / artificialDuration
val resultingQDot = qDotExternal - loss
if (
resultingQDot < zeroMW && !isInnerTemperatureTooLow(
innerTemperature
)
) {
/* House has more losses than gain */
nextActivation(
tick,
innerTemperature,
lowerBoundaryTemperature,
resultingQDot,
).map(HouseTemperatureLowerBoundaryReached)
} else if (
resultingQDot > zeroMW && !isInnerTemperatureTooHigh(
innerTemperature
)
) {
/* House has more gain than losses */
nextActivation(
tick,
upperBoundaryTemperature,
innerTemperature,
resultingQDot,
).map(HouseTemperatureTargetOrUpperBoundaryReached)
} else {
/* House is in perfect balance */
None
}
}
private def nextActivation(
tick: Long,
higherTemperature: Temperature,
lowerTemperature: Temperature,
qDot: Power,
): Option[Long] = {
val flexibleEnergy = energy(higherTemperature, lowerTemperature)
if (flexibleEnergy < zeroMWh)
None
else {
val duration = Math
.floor(
(flexibleEnergy / (qDot * math.signum(qDot.toWatts))).toSeconds
)
.toLong
Some(tick + duration)
}
}
}
object ThermalHouse {
protected def temperatureTolerance: Temperature = Kelvin(0.01d)
def apply(input: ThermalHouseInput): ThermalHouse = new ThermalHouse(
input.getUuid,
input.getId,
input.getOperator,
input.getOperationTime,
input.getThermalBus,
WattsPerKelvin(
input.getEthLosses
.to(PowerSystemUnits.KILOWATT_PER_KELVIN)
.getValue
.doubleValue
* 1000 // kW/K to W/K
),
KilowattHours(
input.getEthCapa
.to(PowerSystemUnits.KILOWATTHOUR_PER_KELVIN)
.getValue
.doubleValue
) / Kelvin(1d),
Kelvin(
input.getTargetTemperature.to(Units.KELVIN).getValue.doubleValue
),
Kelvin(
input.getLowerTemperatureLimit.to(Units.KELVIN).getValue.doubleValue
),
Kelvin(
input.getUpperTemperatureLimit.to(Units.KELVIN).getValue.doubleValue
),
)
/** State of a thermal house
*
* @param tick
* Last tick of temperature change
* @param innerTemperature
* Inner temperature of the house
* @param qDot
* Continuous external infeed of thermal energy since the given tick
*/
final case class ThermalHouseState(
tick: Long,
innerTemperature: Temperature,
qDot: Power,
)
def startingState(house: ThermalHouse): ThermalHouseState =
ThermalHouseState(
-1L,
house.targetTemperature,
zeroMW,
)
object ThermalHouseThreshold {
final case class HouseTemperatureLowerBoundaryReached(
override val tick: Long
) extends ThermalThreshold
final case class HouseTemperatureTargetOrUpperBoundaryReached(
override val tick: Long
) extends ThermalThreshold
}
}