From 374a2eea02742c11f9afd81da7f76a0ca3a0878d Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Fri, 19 Jul 2024 10:30:44 +0200 Subject: [PATCH] format and exception --- .../simona/model/thermal/ThermalGrid.scala | 144 +++++++++--------- 1 file changed, 75 insertions(+), 69 deletions(-) diff --git a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala index 25e2cbfa2e..6a5ffeb8b6 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -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) = { @@ -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) @@ -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(