Skip to content

Commit

Permalink
Merge branch 'refs/heads/df/#878-thermalGridIT' into df/#856-tap-water
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/main/scala/edu/ie3/simona/model/participant/HpModel.scala
#	src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
#	src/test/scala/edu/ie3/simona/model/thermal/ThermalGridTestData.scala
#	src/test/scala/edu/ie3/simona/model/thermal/ThermalGridWithHouseOnlySpec.scala
#	src/test/scala/edu/ie3/simona/model/thermal/ThermalGridWithStorageOnlySpec.scala
  • Loading branch information
danielfeismann committed Aug 20, 2024
2 parents e770997 + 0c36ce5 commit c584f84
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 121 deletions.
18 changes: 18 additions & 0 deletions src/main/scala/edu/ie3/simona/model/participant/HpModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ final case class HpModel(
) =
thermalGrid.energyDemandAndUpdatedState(
relevantData.currentTick,
state.ambientTemperature.getOrElse(relevantData.ambientTemperature),
relevantData.ambientTemperature,
state.thermalGridState,
relevantData.simulationStart,
Expand Down Expand Up @@ -249,7 +250,9 @@ final case class HpModel(
thermalGrid.updateState(
relevantData.currentTick,
state.thermalGridState,
state.ambientTemperature.getOrElse(relevantData.ambientTemperature),
relevantData.ambientTemperature,
isRunning,
newThermalPower,
houseDemand,
thermalStorageDemand,
Expand Down Expand Up @@ -286,6 +289,7 @@ final case class HpModel(
) =
thermalGrid.energyDemandAndUpdatedState(
data.currentTick,
lastState.ambientTemperature.getOrElse(data.ambientTemperature),
data.ambientTemperature,
lastState.thermalGridState,
data.simulationStart,
Expand Down Expand Up @@ -349,6 +353,7 @@ final case class HpModel(
) =
thermalGrid.energyDemandAndUpdatedState(
data.currentTick,
lastState.ambientTemperature.getOrElse(data.ambientTemperature),
data.ambientTemperature,
lastState.thermalGridState,
data.simulationStart,
Expand All @@ -370,6 +375,19 @@ final case class HpModel(
val domesticHotWaterStorageDemand =
(thermalEnergyDemandDomesticHotWaterStorage.hasRequiredDemand) || (lastState.isRunning && thermalEnergyDemandDomesticHotWaterStorage.hasAdditionalDemand)

implicit val tolerance: Energy = KilowattHours(1e-3)
val noThermalStorageOrThermalStorageIsEmpty: Boolean =
updatedThermalGridState.storageState.isEmpty.||(
updatedThermalGridState.storageState.exists(
_.storedEnergy =~ zeroKWH
)
)

val houseDemand =
(thermalEnergyDemandHouse.hasRequiredDemand && noThermalStorageOrThermalStorageIsEmpty) || (lastState.isRunning && thermalEnergyDemandHouse.hasAdditionalDemand)
val heatStorageDemand =
(thermalEnergyDemandStorage.hasRequiredDemand) || (lastState.isRunning && thermalEnergyDemandStorage.hasAdditionalDemand)

val updatedHpState: HpState =
calcState(
lastState,
Expand Down
79 changes: 71 additions & 8 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ final case class ThermalGrid(
*
* @param tick
* Questioned instance in time
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature in the instance in question
* @param state
Expand All @@ -67,6 +69,8 @@ final case class ThermalGrid(
*/
def energyDemandAndUpdatedState(
tick: Long,
// FIXME this is also in state
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
state: ThermalGridState,
simulationStart: ZonedDateTime,
Expand All @@ -86,11 +90,12 @@ final case class ThermalGrid(
thermalHouse.determineState(
tick,
lastHouseState,
lastAmbientTemperature,
ambientTemperature,
lastHouseState.qDot,
)
val (heatDemand, newHouseState) = if (
updatedHouseState.innerTemperature < thermalHouse.targetTemperature
updatedHouseState.innerTemperature < thermalHouse.targetTemperature | (lastHouseState.qDot > zeroKW && updatedHouseState.innerTemperature < thermalHouse.upperBoundaryTemperature)
) {
(
thermalHouse.energyDemandHeating(
Expand Down Expand Up @@ -224,8 +229,12 @@ final case class ThermalGrid(
* Instance in time
* @param state
* Currently applicable state
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature
* actual ambient temperature
* @param isRunning
* determines whether the heat pump is running or not
* @param qDot
* Thermal energy balance
* @param houseDemand
Expand All @@ -244,7 +253,9 @@ final case class ThermalGrid(
def updateState(
tick: Long,
state: ThermalGridState,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
isRunning: Boolean,
qDot: Power,
houseDemand: Boolean,
storageDemand: Boolean,
Expand All @@ -254,8 +265,10 @@ final case class ThermalGrid(
): (ThermalGridState, Option[ThermalThreshold]) = if (qDot > zeroKW)
handleInfeed(
tick,
lastAmbientTemperature,
ambientTemperature,
state,
isRunning,
qDot,
houseDemand,
storageDemand,
Expand All @@ -264,6 +277,7 @@ final case class ThermalGrid(
else
handleConsumption(
tick,
lastAmbientTemperature,
ambientTemperature,
state,
qDot,
Expand All @@ -276,10 +290,14 @@ final case class ThermalGrid(
*
* @param tick
* Current tick
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature
* actual ambient temperature
* @param state
* Current state of the houses
* @param isRunning
* determines whether the heat pump is running or not
* @param qDot
* Infeed to the grid
* @param houseDemand
Expand All @@ -293,8 +311,10 @@ final case class ThermalGrid(
*/
private def handleInfeed(
tick: Long,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
state: ThermalGridState,
isRunning: Boolean,
qDot: Power,
houseDemand: Boolean,
heatStorageDemand: Boolean,
Expand Down Expand Up @@ -329,10 +349,16 @@ final case class ThermalGrid(
}

if (
(qDotHouseLastState > zeroKW && qDotHouseLastState == qDot) | qDotStorageLastState > zeroKW | qDotDomesticWaterStorageLastState > zeroKW
(qDotHouseLastState > zeroKW && !(qDotStorageLastState < zeroKW)) | (qDotStorageLastState > zeroKW & heatStorageDemand | qDotDomesticWaterStorageLastState > zeroKW)
) {
val (updatedHouseState, thermalHouseThreshold, remainingQDotHouse) =
handleInfeedHouse(tick, ambientTemperature, state, qDotHouseLastState)
handleInfeedHouse(
tick,
lastAmbientTemperature,
ambientTemperature,
state,
qDotHouseLastState,
)
val (updatedStorageState, thermalStorageThreshold) =
if (
qDotStorageLastState >= zeroKW && remainingQDotHouse > qDotStorageLastState
Expand Down Expand Up @@ -375,6 +401,29 @@ final case class ThermalGrid(
),
nextThreshold,
)
}
// Handle edge case where house get heated from storage and HP will be activated in between
else if ((qDotHouseLastState > zeroKW && qDotStorageLastState < zeroKW)) {
if (isRunning) {
handleCases(
tick,
lastAmbientTemperature,
ambientTemperature,
state,
qDot,
zeroKW,
)
} else {

handleCases(
tick,
lastAmbientTemperature,
ambientTemperature,
state,
qDotHouseLastState,
qDotStorageLastState,
)
}
} else {

(houseDemand, heatStorageDemand, domesticHotWaterStorageDemand) match {
Expand Down Expand Up @@ -469,8 +518,10 @@ final case class ThermalGrid(
*
* @param tick
* Current tick
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature
* actual ambient temperature
* @param state
* Current state of the houses
* @param qDot
Expand All @@ -480,6 +531,7 @@ final case class ThermalGrid(
*/
private def handleInfeedHouse(
tick: Long,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
state: ThermalGridState,
qDot: Power,
Expand All @@ -489,6 +541,7 @@ final case class ThermalGrid(
val (newState, threshold) = thermalHouse.determineState(
tick,
lastHouseState,
lastAmbientTemperature,
ambientTemperature,
qDot,
)
Expand All @@ -502,6 +555,7 @@ final case class ThermalGrid(
thermalHouse.determineState(
tick,
lastHouseState,
lastAmbientTemperature,
ambientTemperature,
zeroKW,
)
Expand Down Expand Up @@ -579,8 +633,10 @@ final case class ThermalGrid(
*
* @param tick
* Current tick
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature
* actual ambient temperature
* @param state
* Current state of the houses
* @param qDot
Expand All @@ -594,6 +650,7 @@ final case class ThermalGrid(
*/
private def handleConsumption(
tick: Long,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
state: ThermalGridState,
qDot: Power,
Expand All @@ -606,6 +663,7 @@ final case class ThermalGrid(
house.determineState(
tick,
houseState,
lastAmbientTemperature,
ambientTemperature,
zeroMW,
)
Expand All @@ -624,6 +682,7 @@ final case class ThermalGrid(
maybeUpdatedStorageState,
state.houseState,
state.storageState,
lastAmbientTemperature,
ambientTemperature,
qDot,
)
Expand Down Expand Up @@ -732,8 +791,10 @@ final case class ThermalGrid(
* Previous thermal house state before a first update was performed
* @param formerStorageState
* Previous thermal storage state before a first update was performed
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature
* actual ambient temperature
* @param qDot
* Thermal influx
* @return
Expand All @@ -747,6 +808,7 @@ final case class ThermalGrid(
],
formerHouseState: Option[ThermalHouseState],
formerStorageState: Option[ThermalStorageState],
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
qDot: Power,
): (
Expand Down Expand Up @@ -780,6 +842,7 @@ final case class ThermalGrid(
"Impossible to find no house state"
)
),
lastAmbientTemperature,
ambientTemperature,
thermalStorage.getChargingPower,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,10 @@ final case class ThermalHouse(
* current instance in time
* @param state
* currently applicable state
* @param lastAmbientTemperature
* Ambient temperature until this tick
* @param ambientTemperature
* Ambient temperature
* actual ambient temperature
* @param qDot
* new thermal influx
* @return
Expand All @@ -515,6 +517,7 @@ final case class ThermalHouse(
def determineState(
tick: Long,
state: ThermalHouseState,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
qDot: Power,
): (ThermalHouseState, Option[ThermalThreshold]) = {
Expand All @@ -523,7 +526,7 @@ final case class ThermalHouse(
state.qDot,
duration,
state.innerTemperature,
ambientTemperature,
lastAmbientTemperature,
)

/* Calculate the next given threshold */
Expand Down
Loading

0 comments on commit c584f84

Please sign in to comment.