Skip to content

Commit

Permalink
Merge branch 'df/#930-refactor-handleInfeed' into df/#878-thermalGridIT
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfeismann committed Feb 5, 2025
2 parents f4e79d5 + 0af10b3 commit 63e63da
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 90 deletions.
25 changes: 11 additions & 14 deletions src/main/scala/edu/ie3/simona/model/participant/HpModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ final case class HpModel(
currentState: HpState,
data: HpRelevantData,
): Power =
// only if Hp is running qDot will be from Hp, else qDot results from other source, e.g. some storage
if (currentState.isRunning)
currentState.qDot
else
zeroKW
currentState.qDot

/** Given a [[HpRelevantData]] object and the last [[HpState]], this function
* calculates the heat pump's next state to get the actual active power of
Expand Down Expand Up @@ -153,9 +149,9 @@ final case class HpModel(
)

// Updating the HpState
val updatedState =
val updatedHpState =
calcState(lastHpState, relevantData, turnOn, thermalDemandWrapper)
(canOperate, canBeOutOfOperation, updatedState)
(canOperate, canBeOutOfOperation, updatedHpState)
}

/** Depending on the input, this function decides whether the heat pump will
Expand Down Expand Up @@ -232,19 +228,20 @@ final case class HpModel(
.map(_.qDot)
.getOrElse(zeroKW)

val (newActivePower, newThermalPower) = {
val (newActivePowerHp, newThermalPowerHp, qDotIntoGrid) = {
if (isRunning)
(pRated, pThermal)
(pRated, pThermal, pThermal)
else if (lastStateStorageQDot < zeroKW)
(zeroKW, lastStateStorageQDot * (-1))
(zeroKW, zeroKW, lastStateStorageQDot * (-1))
else if (
lastStateStorageQDot == zeroKW && (demandWrapper.houseDemand.hasRequiredDemand || demandWrapper.heatStorageDemand.hasRequiredDemand)
)
(
zeroKW,
zeroKW,
thermalGrid.storage.map(_.getChargingPower: squants.Power).get,
)
else (zeroKW, zeroKW)
else (zeroKW, zeroKW, zeroKW)
}

/* Push thermal energy to the thermal grid and get its updated state in return */
Expand All @@ -254,16 +251,16 @@ final case class HpModel(
lastState.thermalGridState,
lastState.ambientTemperature.getOrElse(relevantData.ambientTemperature),
isRunning,
newThermalPower,
qDotIntoGrid,
demandWrapper,
)

HpState(
isRunning,
relevantData.currentTick,
Some(relevantData.ambientTemperature),
newActivePower,
newThermalPower,
newActivePowerHp,
newThermalPowerHp,
thermalGridState,
maybeThreshold,
)
Expand Down
139 changes: 63 additions & 76 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ final case class ThermalGrid(
* @param isRunning
* determines whether the heat pump is running or not
* @param qDot
* Infeed to the grid from thermal generation (e.g. heat pump)
* Infeed to the grid from thermal generation (e.g. heat pump) or thermal
* storages
* @param thermalDemands
* holds the thermal demands of the thermal units (house, storage)
* @return
Expand Down Expand Up @@ -202,7 +203,8 @@ final case class ThermalGrid(
* @param isRunning
* determines whether the heat pump is running or not
* @param qDot
* Infeed to the grid from thermal generation (e.g. heat pump)
* Infeed to the grid from thermal generation (e.g. heat pump) or thermal
* storages
* @param thermalDemands
* holds the thermal demands of the thermal units (house, storage)
* @return
Expand Down Expand Up @@ -245,13 +247,13 @@ final case class ThermalGrid(
val (updatedStorageState, thermalStorageThreshold) = {
// In case the ThermalHouse could not handle the infeed it will be used for the storage.
if (remainingQDotHouse > qDotStorageLastState) {
handleInfeedStorage(
handleStorageCases(
relevantData.currentTick,
lastThermalGridState,
remainingQDotHouse,
)
} else {
handleInfeedStorage(
handleStorageCases(
relevantData.currentTick,
lastThermalGridState,
qDotStorageLastState,
Expand Down Expand Up @@ -336,14 +338,13 @@ final case class ThermalGrid(
* | false | false | false | true | false | true |
* | false | false | false | false | false | false |
*
* This can be simplified to five cases
* | No | Conditions | Result |
* |:---|:-------------------------------------------------------------------------|:----------|
* | 1 | if(house.reqD) => house | house |
* | 2 | if(!house.reqD && !storage.reqD) => storage | storage |
* | 3 | if(!house.reqD && !storage.reqD && storage.addD) => storage | storage |
* | 4 | if(!house.reqD && house.addD && !storage.reqD && !storage.addD) => house | house |
* | 5 | if(all == false) => no output | no output |
* This can be simplified to four cases
* | No | Conditions | Result |
* |:---|:-------------------------------------|:----------|
* | 1 | if house.reqD | house |
* | 2 | else if storage.reqD OR storage.addD | storage |
* | 3 | else if house.addD | house |
* | 4 | else | no output |
*
* @param thermalDemands
* holds the thermal demands of the thermal units (house, storage)
Expand All @@ -354,7 +355,8 @@ final case class ThermalGrid(
* @param gridState
* Current state of the thermalGrid
* @param qDot
* Infeed to the grid from thermal generation (e.g. heat pump)
* Infeed to the grid from thermal generation (e.g. heat pump) or thermal
* storages
* @return
* Updated thermal grid state and the thermalThreshold if there is one
*/
Expand All @@ -365,63 +367,42 @@ final case class ThermalGrid(
gridState: ThermalGridState,
qDot: Power,
): (ThermalGridState, Option[ThermalThreshold]) = {
(
thermalDemands.houseDemand.hasRequiredDemand,
thermalDemands.houseDemand.hasAdditionalDemand,
thermalDemands.heatStorageDemand.hasRequiredDemand,
thermalDemands.heatStorageDemand.hasAdditionalDemand,
) match {

case (true, _, _, _) =>
// house first
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
qDot,
zeroKW,
)
// then heatStorage after heating House
case (_, _, true, _) =>
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
zeroKW,
qDot,
)
// charge storage if house has no requiredDemand
case (false, _, false, true) =>
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
zeroKW,
qDot,
)
// heat house if storage has no additionalDemand
case (_, true, false, false) =>
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
qDot,
zeroKW,
)
// if there is no demand, don't heat anything
case (false, false, false, false) =>
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
zeroKW,
zeroKW,
)
case _ =>
throw new InconsistentStateException(
"There should be at least a house or a storage state."
)
}

if (thermalDemands.houseDemand.hasRequiredDemand)
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
qDot,
zeroKW,
)
else if (
thermalDemands.heatStorageDemand.hasRequiredDemand || thermalDemands.heatStorageDemand.hasAdditionalDemand
)
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
zeroKW,
qDot,
)
else if (thermalDemands.houseDemand.hasAdditionalDemand)
handleCases(

Check warning on line 390 in src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala

View check run for this annotation

SonarQubeGithubPRChecks / simona Sonarqube Results

src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala#L390

This branch's code block is the same as the block for the branch on line 372.
relevantData,
lastAmbientTemperature,
gridState,
qDot,
zeroKW,
)
else
handleCases(
relevantData,
lastAmbientTemperature,
gridState,
zeroKW,
zeroKW,
)

}

/** Handles the different cases, of thermal flows from and into the thermal
Expand Down Expand Up @@ -457,7 +438,7 @@ final case class ThermalGrid(
)

val (updatedStorageState, thermalStorageThreshold) =
handleInfeedStorage(relevantData.currentTick, state, qDotHeatStorage)
handleStorageCases(relevantData.currentTick, state, qDotHeatStorage)

val nextThreshold = determineMostRecentThreshold(
thermalHouseThreshold,
Expand Down Expand Up @@ -523,7 +504,7 @@ final case class ThermalGrid(
}

/** Handles the cases, when the storage has heat demand and will be filled up
* here (positive qDot) or will be return its stored energy into the thermal
* here (positive qDot) or will return its stored energy into the thermal
* grid (negative qDot).
* @param tick
* Current tick
Expand All @@ -535,7 +516,7 @@ final case class ThermalGrid(
* @return
* Updated thermal grid state
*/
private def handleInfeedStorage(
private def handleStorageCases(
tick: Long,
state: ThermalGridState,
qDotStorage: Power,
Expand Down Expand Up @@ -585,7 +566,8 @@ final case class ThermalGrid(
* @param lastThermalGridState
* state of the thermalGrid until this tick
* @param qDot
* Infeed to the grid from thermal generation (e.g. heat pump)
* Infeed to the grid from thermal generation (e.g. heat pump) or thermal
* storages
* @return
* Updated thermal grid state
*/
Expand Down Expand Up @@ -657,7 +639,8 @@ final case class ThermalGrid(
* @param lastAmbientTemperature
* Ambient temperature valid up until (not including) the current tick
* @param qDot
* Infeed to the grid from thermal generation (e.g. heat pump)
* Infeed to the grid from thermal generation (e.g. heat pump) or thermal
* storages
* @return
* Options to revised thermal house and storage state
*/
Expand Down Expand Up @@ -837,7 +820,11 @@ object ThermalGrid {
* than or equal to the absolutely required energy. Thus, this class can only
* be instantiated via factory.
* @param required
* The absolutely required energy to reach target state
* The absolutely required energy to reach target state. For
* [[ThermalHouse]] this would be the energy demand to reach the boundary
* or targetTemperature. For [[ThermalStorage]] this would be the amount of
* energy to get fully charged when empty. If the [[ThermalStorage]] is not
* empty, the required energy is zero.
* @param possible
* The maximum possible energy, that can be handled
*/
Expand Down
12 changes: 12 additions & 0 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalStorage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ abstract class ThermalStorage(
}

object ThermalStorage {

/** State of a thermal storage
*
* @param tick
* Last tick of storage state change
* @param storedEnergy
* Energy stored in the storage at this tick
* @param qDot
* Infeed to the heat storage (positive: Storage is charging, negative:
* Storage is discharging)
*/

final case class ThermalStorageState(
tick: Long,
storedEnergy: Energy,
Expand Down

0 comments on commit 63e63da

Please sign in to comment.