From 37bc66af44a6b7089f08302911e31c611c1969ec Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Sat, 24 Aug 2024 08:36:59 +0200 Subject: [PATCH] Fixed ThermalStorageResults having multiple entries --- CHANGELOG.md | 1 + .../simona/model/thermal/ThermalGrid.scala | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b43cb834c8..eaf070f2f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,6 +101,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) +- Fixed ThermalStorageResults having multiple entries [#924](https://github.com/ie3-institute/simona/issues/924) ## [3.0.0] - 2023-08-07 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 85b41b35ff..01ccdcc16d 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -386,6 +386,44 @@ final case class ThermalGrid( 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. + * */ + + val houseResultTick: Option[Long] = house + .zip(state.houseState) + .headOption + .flatMap { + case ( + thermalHouse, + ThermalHouseState(tick, _, _), + ) => + Some(tick) + case _ => None + } + + val storageResultTick: Option[Long] = storage + .zip(state.storageState) + .headOption + .flatMap { + case ( + thermalStorage, + 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 { @@ -394,7 +432,7 @@ final case class ThermalGrid( ThermalHouseState(tick, innerTemperature, thermalInfeed), ) => Seq.empty[ResultEntity] :+ new ThermalHouseResult( - tick.toDateTime, + actualResultTick.toDateTime, thermalHouse.uuid, thermalInfeed.toMegawatts.asMegaWatt, innerTemperature.toKelvinScale.asKelvin, @@ -410,7 +448,7 @@ final case class ThermalGrid( ThermalStorageState(tick, storedEnergy, qDot), ) => houseResults :+ new CylindricalStorageResult( - tick.toDateTime, + actualResultTick.toDateTime, storage.uuid, storedEnergy.toMegawattHours.asMegaWattHour, qDot.toMegawatts.asMegaWatt,