Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix determineState of ThermalHouse #927

Merged
merged 15 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed FixedFeedModelSpec [#861](https://github.com/ie3-institute/simona/issues/861)
- Fixing duration calculation in result events [#801](https://github.com/ie3-institute/simona/issues/801)
- Handle MobSim requests for current prices [#892](https://github.com/ie3-institute/simona/issues/892)
- Fix determineState of ThermalHouse [#926](https://github.com/ie3-institute/simona/issues/926)

## [3.0.0] - 2023-08-07

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ final case class HpModel(
relevantData.currentTick,
state.thermalGridState,
state.ambientTemperature.getOrElse(relevantData.ambientTemperature),
relevantData.ambientTemperature,
newThermalPower,
)

Expand Down
35 changes: 29 additions & 6 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ final case class ThermalGrid(
* Instance in time
* @param state
* Currently applicable state
* @param lastAmbientTemperature
* Ambient temperature valid up until (not including) the current tick
* @param ambientTemperature
* Ambient temperature
* Current ambient temperature
* @param qDot
* Thermal energy balance
* @return
Expand All @@ -115,19 +117,28 @@ final case class ThermalGrid(
def updateState(
tick: Long,
state: ThermalGridState,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
qDot: Power,
): (ThermalGridState, Option[ThermalThreshold]) = if (qDot > zeroKW)
handleInfeed(tick, ambientTemperature, state, qDot)
handleInfeed(tick, lastAmbientTemperature, ambientTemperature, state, qDot)
else
handleConsumption(tick, ambientTemperature, state, qDot)
handleConsumption(
tick,
lastAmbientTemperature,
ambientTemperature,
state,
qDot,
)

/** Handles the case, when a grid has infeed. First, heat up all the houses to
* their maximum temperature, then fill up the storages
* @param tick
* Current tick
* @param lastAmbientTemperature
* Ambient temperature valid up until (not including) the current tick
* @param ambientTemperature
* Ambient temperature
* Current ambient temperature
* @param state
* Current state of the houses
* @param qDot
Expand All @@ -137,6 +148,7 @@ final case class ThermalGrid(
*/
private def handleInfeed(
tick: Long,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
state: ThermalGridState,
qDot: Power,
Expand All @@ -163,6 +175,7 @@ final case class ThermalGrid(
thermalHouse.determineState(
tick,
lastHouseState,
lastAmbientTemperature,
ambientTemperature,
qDot,
)
Expand All @@ -177,6 +190,7 @@ final case class ThermalGrid(
thermalHouse.determineState(
tick,
lastHouseState,
lastAmbientTemperature,
ambientTemperature,
zeroKW,
)
Expand Down Expand Up @@ -248,8 +262,10 @@ final case class ThermalGrid(
*
* @param tick
* Current tick
* @param lastAmbientTemperature
* Ambient temperature valid up until (not including) the current tick
* @param ambientTemperature
* Ambient temperature
* Current ambient temperature
* @param state
* Current state of the houses
* @param qDot
Expand All @@ -259,6 +275,7 @@ final case class ThermalGrid(
*/
private def handleConsumption(
tick: Long,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
state: ThermalGridState,
qDot: Power,
Expand All @@ -269,6 +286,7 @@ final case class ThermalGrid(
house.determineState(
tick,
houseState,
lastAmbientTemperature,
ambientTemperature,
zeroMW,
)
Expand All @@ -287,6 +305,7 @@ final case class ThermalGrid(
maybeUpdatedStorageState,
state.houseState,
state.storageState,
lastAmbientTemperature,
ambientTemperature,
qDot,
)
Expand Down Expand Up @@ -319,8 +338,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 valid up until (not including) the current tick
* @param ambientTemperature
* Ambient temperature
* Current ambient temperature
* @param qDot
* Thermal influx
* @return
Expand All @@ -334,6 +355,7 @@ final case class ThermalGrid(
],
formerHouseState: Option[ThermalHouseState],
formerStorageState: Option[ThermalStorageState],
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
qDot: Power,
): (
Expand Down Expand Up @@ -367,6 +389,7 @@ final case class ThermalGrid(
"Impossible to find no house state"
)
),
lastAmbientTemperature,
ambientTemperature,
thermalStorage.getChargingPower,
)
Expand Down
13 changes: 8 additions & 5 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalHouse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,22 @@ final case class ThermalHouse(
/** Update the current state of the house
*
* @param tick
* current instance in time
* Current instance in time
* @param state
* currently applicable state
* Currently applicable state
* @param lastAmbientTemperature
* Ambient temperature valid up until (not including) the current tick
* @param ambientTemperature
* Ambient temperature
* Current ambient temperature
* @param qDot
* new thermal influx
* New thermal influx
* @return
* Updated state and the tick in which the next threshold is reached
*/
def determineState(
tick: Long,
state: ThermalHouseState,
lastAmbientTemperature: Temperature,
ambientTemperature: Temperature,
qDot: Power,
): (ThermalHouseState, Option[ThermalThreshold]) = {
Expand All @@ -319,7 +322,7 @@ final case class ThermalHouse(
state.qDot,
duration,
state.innerTemperature,
ambientTemperature,
lastAmbientTemperature,
)

/* Calculate the next given threshold */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class ThermalGridWithHouseAndStorageSpec
thermalGrid invokePrivate handleConsumption(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
externalQDot,
)
Expand Down Expand Up @@ -201,6 +202,7 @@ class ThermalGridWithHouseAndStorageSpec
thermalGrid invokePrivate handleConsumption(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
externalQDot,
)
Expand Down Expand Up @@ -254,6 +256,7 @@ class ThermalGridWithHouseAndStorageSpec
maybeHouseState.map(_._1),
None,
testGridAmbientTemperature,
testGridAmbientTemperature,
testGridQDotConsumption,
) match {
case (maybeRevisedHouseState, maybeRevisedStorageState) =>
Expand Down Expand Up @@ -296,6 +299,7 @@ class ThermalGridWithHouseAndStorageSpec
maybeHouseState.map(_._1),
maybeStorageState.map(_._1),
ambientTemperature,
ambientTemperature,
zeroInflux,
) match {
case (maybeRevisedHouseState, maybeRevisedStorageState) =>
Expand Down Expand Up @@ -338,6 +342,7 @@ class ThermalGridWithHouseAndStorageSpec
maybeHouseState.map(_._1),
maybeStorageState.map(_._1),
ambientTemperature,
ambientTemperature,
testGridQDotInfeed,
) match {
case (maybeRevisedHouseState, maybeRevisedStorageState) =>
Expand Down Expand Up @@ -380,6 +385,7 @@ class ThermalGridWithHouseAndStorageSpec
maybeHouseState.map(_._1),
maybeStorageState.map(_._1),
ambientTemperature,
ambientTemperature,
zeroInflux,
) match {
case (maybeRevisedHouseState, maybeRevisedStorageState) =>
Expand Down Expand Up @@ -441,6 +447,7 @@ class ThermalGridWithHouseAndStorageSpec
formerHouseState,
formerStorageState,
ambientTemperature,
ambientTemperature,
zeroInflux,
) match {
case (
Expand Down Expand Up @@ -487,6 +494,7 @@ class ThermalGridWithHouseAndStorageSpec
thermalGrid invokePrivate handleInfeed(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
initialGridState,
externalQDot,
)
Expand Down Expand Up @@ -532,6 +540,7 @@ class ThermalGridWithHouseAndStorageSpec
thermalGrid invokePrivate handleInfeed(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
externalQDot,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ThermalGridWithHouseOnlySpec extends UnitSpec with ThermalHouseTestData {
thermalGrid invokePrivate handleConsumption(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
externalQDot,
)
Expand Down Expand Up @@ -133,6 +134,7 @@ class ThermalGridWithHouseOnlySpec extends UnitSpec with ThermalHouseTestData {
thermalGrid invokePrivate handleConsumption(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
testGridQDotConsumption,
)
Expand Down Expand Up @@ -167,6 +169,7 @@ class ThermalGridWithHouseOnlySpec extends UnitSpec with ThermalHouseTestData {
thermalGrid invokePrivate handleInfeed(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
testGridQDotInfeed,
)
Expand All @@ -193,6 +196,7 @@ class ThermalGridWithHouseOnlySpec extends UnitSpec with ThermalHouseTestData {
0L,
ThermalGrid.startingState(thermalGrid),
testGridAmbientTemperature,
testGridAmbientTemperature,
testGridQDotInfeed,
) match {
case (
Expand All @@ -215,6 +219,7 @@ class ThermalGridWithHouseOnlySpec extends UnitSpec with ThermalHouseTestData {
0L,
ThermalGrid.startingState(thermalGrid),
testGridAmbientTemperature,
testGridAmbientTemperature,
testGridQDotConsumption,
) match {
case (
Expand All @@ -237,6 +242,7 @@ class ThermalGridWithHouseOnlySpec extends UnitSpec with ThermalHouseTestData {
0L,
ThermalGrid.startingState(thermalGrid),
testGridAmbientTemperature,
testGridAmbientTemperature,
Megawatts(0d),
) match {
case (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class ThermalGridWithStorageOnlySpec
thermalGrid invokePrivate handleConsumption(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
testGridQDotConsumptionHigh,
)
Expand Down Expand Up @@ -147,6 +148,7 @@ class ThermalGridWithStorageOnlySpec
thermalGrid invokePrivate handleInfeed(
tick,
testGridAmbientTemperature,
testGridAmbientTemperature,
gridState,
testGridQDotInfeed,
)
Expand All @@ -171,6 +173,7 @@ class ThermalGridWithStorageOnlySpec
0L,
ThermalGrid.startingState(thermalGrid),
testGridAmbientTemperature,
testGridAmbientTemperature,
testGridQDotInfeed,
)

Expand Down Expand Up @@ -203,6 +206,7 @@ class ThermalGridWithStorageOnlySpec
)
),
testGridAmbientTemperature,
testGridAmbientTemperature,
testGridQDotConsumptionHigh,
) match {
case (
Expand All @@ -225,6 +229,7 @@ class ThermalGridWithStorageOnlySpec
0L,
ThermalGrid.startingState(thermalGrid),
testGridAmbientTemperature,
testGridAmbientTemperature,
Kilowatts(0d),
)
updatedState match {
Expand Down
31 changes: 31 additions & 0 deletions src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@

package edu.ie3.simona.model.thermal

import edu.ie3.simona.model.thermal.ThermalHouse.ThermalHouseThreshold.HouseTemperatureLowerBoundaryReached
import edu.ie3.simona.model.thermal.ThermalHouse.{
ThermalHouseState,
startingState,
}
import edu.ie3.simona.test.common.UnitSpec
import edu.ie3.simona.test.common.input.HpInputTestData
import edu.ie3.util.scala.quantities.DefaultQuantities.zeroKW
import edu.ie3.util.scala.quantities.WattsPerKelvin
import org.scalatest.prop.TableFor3
import squants.energy._
Expand Down Expand Up @@ -92,6 +98,31 @@ class ThermalHouseSpec extends UnitSpec with HpInputTestData {
newInnerTemperature should approximate(Temperature(29, Celsius))
}

"Check for the correct state of house when ambient temperature changes" in {
val house = thermalHouse(18, 22)
val initialHousestate = startingState(house)
val lastAmbientTemperature = Temperature(15, Celsius)
val ambientTemperature = Temperature(-20, Celsius)

val (thermalHouseState, threshold) = house.determineState(
3600L,
initialHousestate,
lastAmbientTemperature,
ambientTemperature,
zeroKW,
)

thermalHouseState match {
case ThermalHouseState(tick, temperature, qDot) =>
tick shouldBe 3600L
temperature should approximate(Kelvin(292.64986111))
qDot shouldBe zeroKW
case unexpected =>
fail(s"Expected a thermalHouseState but got none $unexpected.")
}
threshold shouldBe Some(HouseTemperatureLowerBoundaryReached(4967))
}

"Check build method" in {

val thermalTestHouse = thermalHouse(18, 22)
Expand Down