Skip to content

Commit

Permalink
format and exception
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfeismann committed Jul 19, 2024
1 parent e4457dc commit 374a2ee
Showing 1 changed file with 75 additions and 69 deletions.
144 changes: 75 additions & 69 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,31 @@ final case class ThermalGrid(
ambientTemperature: Temperature,
state: ThermalGridState,
): ThermalEnergyDemand = {
/* First get the energy demand of the houses but only if below target temperature */
/* First get the energy demand of the houses but only if inner temperature is below target temperature */

val innerTemperatureBelowTarget: Boolean = house.zip(state.houseState).headOption match {
case Some((thermalHouse, lastHouseState)) =>
lastHouseState.innerTemperature < thermalHouse.targetTemperature
case None =>
false // Default value when the collections cannot be zipped
}
val innerTemperatureBelowTarget: Boolean =
house.zip(state.houseState).headOption match {
case Some((thermalHouse, lastHouseState)) =>
lastHouseState.innerTemperature < thermalHouse.targetTemperature
case None =>
throw new RuntimeException(
"Can't progress thermal house last state to determin energy demand."
)
}

val houseDemand =
if (innerTemperatureBelowTarget) {
house
.zip(state.houseState)
.map { case (house, state) =>
house.energyDemand(
tick,
ambientTemperature,
state,
)
}
.getOrElse(ThermalEnergyDemand.noDemand)}
else {ThermalEnergyDemand.noDemand}
house
.zip(state.houseState)
.map { case (house, state) =>
house.energyDemand(
tick,
ambientTemperature,
state,
)
}
.getOrElse(ThermalEnergyDemand.noDemand)
} else { ThermalEnergyDemand.noDemand }

/* Then go over the storages, see what they can provide and what they might be able to charge */
val (storedEnergy, remainingCapacity) = {
Expand Down Expand Up @@ -153,7 +156,6 @@ final case class ThermalGrid(
qDot: Power,
): (ThermalGridState, Option[ThermalThreshold]) = {

/* Handle the case we had some infeed the last state of where running Hp was heating up house in the last state*/
val (qDotHouseLastState, qDotStorageLastState) =
(state.houseState, state.storageState) match {
case (Some(house), Some(storage)) => (house.qDot, storage.qDot)
Expand All @@ -162,65 +164,69 @@ final case class ThermalGrid(
case (None, None) => (zeroKW, zeroKW)
}

/*
if (qDotHouseLastState > zeroKW | qDotStorageLastState > zeroKW) {
val updatedState = state.copy(
// Update houseState and storageState with the same values as in lastState
houseState = state.houseState.map(_.copy(qDot = qDotHouseLastState)),
storageState = state.storageState.map(_.copy(qDot = qDotStorageLastState))
)
*/

// When there is a significant qDot from the last state
if (qDotHouseLastState > zeroKW || qDotStorageLastState > zeroKW) {
if (qDotStorageLastState > zeroKW) {
{
storage.zip(state.storageState) match {
case Some((thermalStorage, lastStorageState)) =>
val (
updatedHouseState: Option[ThermalHouseState],
maybeHouseThreshold: Option[ThermalThreshold],
) =
/* Set thermal power exchange with house to zero */
house.zip(state.houseState) match {
case Some((thermalHouse, houseState)) =>
val nextHouseState =
thermalHouse.determineState(
tick,
houseState,
ambientTemperature,
zeroKW,
)
(Some(nextHouseState._1), nextHouseState._2)

case _ => state.houseState
}

val (updatedStorageState, maybeStorageThreshold) =
thermalStorage.updateState(
tick,
qDot,
lastStorageState,
)

// Handle updated house state
val (updatedHouseState, maybeFullHouseThreshold) =
house.zip(state.houseState) match {
case Some((thermalHouse, lastHouseState)) =>
thermalHouse.determineState(
tick,
lastHouseState,
ambientTemperature,
qDotHouseLastState,
/* Both house and storage are updated. Determine what reaches the next threshold */
val nextThreshold = determineMostRecentThreshold(
maybeHouseThreshold,
maybeStorageThreshold,
)
case None =>
throw new InconsistentStateException("No house state available")
}

// Handle updated storage state
val (updatedStorageState, maybeStorageThreshold) =
storage.zip(state.storageState) match {
case Some((thermalStorage, lastStorageState)) =>
thermalStorage.updateState(
tick,
qDotStorageLastState,
lastStorageState,
(
state.copy(
houseState = updatedHouseState,
storageState = Some(updatedStorageState),
),
nextThreshold,
)

case None =>
throw new InconsistentStateException("No storage state available")
storage.zip(state.storageState) match {
case Some((thermalStorage, storageState)) =>
val (updatedStorageState, maybeStorageThreshold) =
thermalStorage.updateState(tick, qDot, storageState)
(
state.copy(storageState = Some(updatedStorageState)),
maybeStorageThreshold,
)
case None =>
throw new InconsistentStateException(
"A thermal grid has to contain either at least a house or a storage."
)
}
}

// Determine the most recent threshold
val nextThreshold = determineMostRecentThreshold(
maybeFullHouseThreshold,
maybeStorageThreshold,
)

(
state.copy(
houseState = Some(updatedHouseState),
storageState = Some(updatedStorageState),
),
nextThreshold,
)
}
} else {

house.zip(state.houseState) match {
case Some((thermalHouse, lastHouseState)) =>
/* Set thermal power exchange with storage to zero */
// TODO: We would need to issue a storage result model here...
val updatedStorageState = storage.zip(state.storageState) match {
case Some((thermalStorage, storageState)) =>
Some(
Expand Down

0 comments on commit 374a2ee

Please sign in to comment.