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

Fixed ThermalStorageResults having multiple entries #925

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
fixing result of thermal grids
  • Loading branch information
danielfeismann committed Sep 16, 2024
commit e438ca06260f8580db54b094363f451d8a4b1811
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ trait HpAgentFundamentals
)

val accompanyingResults = baseStateData.model.thermalGrid.results(
updatedState.thermalGridState
tick,
updatedState.thermalGridState,
)(baseStateData.startDate)
val result = AccompaniedSimulationResult(power, accompanyingResults)

Expand Down Expand Up @@ -252,7 +253,8 @@ trait HpAgentFundamentals
relevantData,
)
val accompanyingResults = baseStateData.model.thermalGrid.results(
lastModelState.thermalGridState
currentTick,
lastModelState.thermalGridState,
)(baseStateData.startDate)
val result = AccompaniedSimulationResult(power, accompanyingResults)

Expand Down
121 changes: 57 additions & 64 deletions src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final case class ThermalGrid(

/** Determine the energy demand of the total grid at the given instance in
* time
*
* @param tick
* Questioned instance in time
* @param ambientTemperature
Expand Down Expand Up @@ -101,6 +102,7 @@ final case class ThermalGrid(
}

/** Update the current state of the grid
*
* @param tick
* Instance in time
* @param state
Expand Down Expand Up @@ -133,6 +135,7 @@ final case class ThermalGrid(

/** 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
Expand Down Expand Up @@ -328,6 +331,7 @@ final case class ThermalGrid(
* <li>the house has reached it's lower temperature boundary,</li> <li>there
* is no infeed from external and</li> <li>the storage is not empty
* itself</li> </ul>
*
* @param tick
* The current tick
* @param maybeHouseState
Expand Down Expand Up @@ -399,90 +403,79 @@ final case class ThermalGrid(

/** Convert the given state of the thermal grid into result models of it's
* constituent models
*
* @param currentTick
* Actual simulation tick
* @param state
* State to be converted
* @param startDateTime
* Start date time of the simulation
* @return
* A [[Seq]] of results of the constituent thermal model
*/
def results(
state: ThermalGridState
)(implicit startDateTime: ZonedDateTime): Seq[ResultEntity] = {
/* FIXME: We only want to write results when there is a change within the participant.
* At the moment we write an storage result when the house result gets updated and vice versa.
* */
def results(currentTick: Long, state: ThermalGridState)(implicit
startDateTime: ZonedDateTime
): Seq[ResultEntity] = {

val houseResultTick: Option[Long] = house
.zip(state.houseState)
.headOption
.flatMap {
case (
thermalHouse,
ThermalHouseState(tick, _, _),
) =>
Some(tick)
case _ => None
case (_, ThermalHouseState(tick, _, _)) => Some(tick)
case _ => None
}

val storageResultTick: Option[Long] = storage
.zip(state.storageState)
.headOption
.flatMap {
case (
thermalStorage,
ThermalStorageState(tick, _, _),
) =>
Some(tick)
case _ => None
case (_, ThermalStorageState(tick, _, _)) => Some(tick)
case _ => None
}

val actualResultTick: Long = (houseResultTick, storageResultTick) match {
case (Some(hTick), Some(sTick)) => math.max(hTick, sTick)
case (Some(hTick), None) => hTick
case (None, Some(sTick)) => sTick
case (None, None) =>
throw new RuntimeException(
"ThermalGrid result should be carried out but it was not possible to get the tick for the result"
)
}

val houseResults = house
.zip(state.houseState)
.map {
case (
thermalHouse,
ThermalHouseState(tick, innerTemperature, thermalInfeed),
) =>
Seq.empty[ResultEntity] :+ new ThermalHouseResult(
actualResultTick.toDateTime,
thermalHouse.uuid,
thermalInfeed.toMegawatts.asMegaWatt,
innerTemperature.toKelvinScale.asKelvin,
)
val houseResults =
if (houseResultTick.contains(currentTick)) {
house
.zip(state.houseState)
.map {
case (
thermalHouse,
ThermalHouseState(tick, innerTemperature, thermalInfeed),
) =>
Seq.empty[ResultEntity] :+ new ThermalHouseResult(
tick.toDateTime,
thermalHouse.uuid,
thermalInfeed.toMegawatts.asMegaWatt,
innerTemperature.toKelvinScale.asKelvin,
)
}
.getOrElse(Seq.empty[ResultEntity])
} else {
Seq.empty[ResultEntity]
}
.getOrElse(Seq.empty[ResultEntity])

storage
.zip(state.storageState)
.map {
case (
storage: CylindricalThermalStorage,
ThermalStorageState(tick, storedEnergy, qDot),
) =>
houseResults :+ new CylindricalStorageResult(
actualResultTick.toDateTime,
storage.uuid,
storedEnergy.toMegawattHours.asMegaWattHour,
qDot.toMegawatts.asMegaWatt,
(storedEnergy / storage.maxEnergyThreshold).asPu,
)
case _ =>
throw new NotImplementedError(
s"Result handling for storage type '${storage.getClass.getSimpleName}' not supported."
)
}
.getOrElse(houseResults)
if (storageResultTick.contains(currentTick)) {
storage
.zip(state.storageState)
.map {
case (
storage: CylindricalThermalStorage,
ThermalStorageState(tick, storedEnergy, qDot),
) =>
houseResults :+ new CylindricalStorageResult(
tick.toDateTime,
storage.uuid,
storedEnergy.toMegawattHours.asMegaWattHour,
qDot.toMegawatts.asMegaWatt,
(storedEnergy / storage.maxEnergyThreshold).asPu,
)
case _ =>
throw new NotImplementedError(
s"Result handling for storage type '${storage.getClass.getSimpleName}' not supported."
)
}
.getOrElse(houseResults)
} else {
houseResults
}
}
}

Expand Down