From 7a49690eb95a81a56bd6b5fbfbebc7e3f0d3cac4 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 16 Sep 2024 13:23:06 +0200 Subject: [PATCH 1/7] Improve code quality in fixedloadmodelspec and other tests --- CHANGELOG.md | 1 + .../simona/model/grid/SystemComponentSpec.scala | 5 +---- .../simona/model/participant/WecModelSpec.scala | 15 ++++++++------- .../participant/load/FixedLoadModelSpec.scala | 10 ++++------ .../simona/model/thermal/ThermalHouseSpec.scala | 2 +- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ff86ccca2..f305e7ee61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,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) +- Improve code quality in fixedloadmodelspec and other tests [#919](https://github.com/ie3-institute/simona/issues/919) ## [3.0.0] - 2023-08-07 diff --git a/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala b/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala index dce98587ad..6149b6d014 100644 --- a/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala @@ -103,12 +103,10 @@ class SystemComponentSpec extends UnitSpec with DefaultTestData { ), ) - for ((operationStart, operationEnd, expected) <- testCases) { + testCases.foreach { case (operationStart, operationEnd, expected) => val operationTimeBuilder = setup() - operationStart.foreach(operationTimeBuilder.withStart) operationEnd.foreach(operationTimeBuilder.withEnd) - val operationTime: OperationTime = operationTimeBuilder.build() val interval: OperationInterval = @@ -117,7 +115,6 @@ class SystemComponentSpec extends UnitSpec with DefaultTestData { simulationEnd, operationTime, ) - interval should be(expected) } } diff --git a/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala b/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala index 8933819773..4e7ab8e76f 100644 --- a/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala @@ -126,7 +126,7 @@ class WecModelSpec extends UnitSpec with DefaultTestData { -24573.39638823692, 0) velocities.zip(expectedPowers).foreach { case (velocity, power) => - val wecData = new WecRelevantData( + val wecData = WecRelevantData( MetersPerSecond(velocity), Celsius(20), Some(Pascals(101325d)), @@ -174,17 +174,18 @@ class WecModelSpec extends UnitSpec with DefaultTestData { val expectedPowers = Seq(-23377.23862017266, -24573.41320418286, -29029.60338829823) - temperatures.zip(expectedPowers).foreach { case (temperature, power) => - val wecData = new WecRelevantData( + for (i <- temperatures.indices) { + val temperature = temperatures(i) + val expectedPower = expectedPowers(i) + val wecData = WecRelevantData( MetersPerSecond(3.0), Celsius(temperature), Some(Pascals(101325d)), ) - val result = { + val result = wecModel.calculateActivePower(ModelState.ConstantState, wecData) - } - val expectedPower = Watts(power) - result shouldBe expectedPower + val expectedPowerInWatts = Watts(expectedPower) + result shouldBe expectedPowerInWatts } } } diff --git a/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala b/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala index 98fd066841..d3492ea6f2 100644 --- a/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala @@ -94,13 +94,12 @@ class FixedLoadModelSpec reference, ) - for (_ <- 0 until 10000) { + (1 to 10000).foreach { _ => val calculatedPower = dut .calculateActivePower( ModelState.ConstantState, FixedLoadModel.FixedLoadRelevantData, ) - calculatedPower should approximate(expectedPower) } } @@ -116,8 +115,9 @@ class FixedLoadModelSpec forAll(testData) { (reference, expectedPower: Power) => val relevantData = FixedLoadModel.FixedLoadRelevantData - var scale = 0.0 - while (scale <= 2) { + val scales = (0 to 20).map(_ * 0.1) + + scales.foreach { scale => val scaledSRated = Kilowatts( loadInput.getsRated .to(PowerSystemUnits.KILOWATT) @@ -142,8 +142,6 @@ class FixedLoadModelSpec val expectedScaledPower = expectedPower * scale calculatedPower should approximate(expectedScaledPower) - - scale += 0.1 } } } diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala index 56e1cac144..b084257b83 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala @@ -36,7 +36,7 @@ class ThermalHouseSpec extends UnitSpec with HpInputTestData { (23d, true, false), ) - testCases.foreach { case (innerTemperature, isTooHigh, isTooLow) => + forAll(testCases) { (innerTemperature: Double, isTooHigh: Boolean, isTooLow: Boolean) => val innerTemp = Temperature(innerTemperature, Celsius) val isHigher = thermalHouseTest.isInnerTemperatureTooHigh(innerTemp) val isLower = thermalHouseTest.isInnerTemperatureTooLow(innerTemp) From e3bce9ad1eddced7ceae5ad736be5ca88df59675 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 16 Sep 2024 13:23:59 +0200 Subject: [PATCH 2/7] fmt --- .../simona/model/thermal/ThermalHouseSpec.scala | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala index b084257b83..944889abe2 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalHouseSpec.scala @@ -36,13 +36,14 @@ class ThermalHouseSpec extends UnitSpec with HpInputTestData { (23d, true, false), ) - forAll(testCases) { (innerTemperature: Double, isTooHigh: Boolean, isTooLow: Boolean) => - val innerTemp = Temperature(innerTemperature, Celsius) - val isHigher = thermalHouseTest.isInnerTemperatureTooHigh(innerTemp) - val isLower = thermalHouseTest.isInnerTemperatureTooLow(innerTemp) - - isHigher shouldBe isTooHigh - isLower shouldBe isTooLow + forAll(testCases) { + (innerTemperature: Double, isTooHigh: Boolean, isTooLow: Boolean) => + val innerTemp = Temperature(innerTemperature, Celsius) + val isHigher = thermalHouseTest.isInnerTemperatureTooHigh(innerTemp) + val isLower = thermalHouseTest.isInnerTemperatureTooLow(innerTemp) + + isHigher shouldBe isTooHigh + isLower shouldBe isTooLow } } From 1021c287ade7972244651139bfff044910c35d94 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Tue, 17 Sep 2024 17:40:14 +0200 Subject: [PATCH 3/7] Code improved --- .../model/grid/SystemComponentSpec.scala | 6 +-- .../model/participant/WecModelSpec.scala | 42 ++++++++++++------- .../participant/load/FixedLoadModelSpec.scala | 3 +- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala b/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala index 6149b6d014..99505d928e 100644 --- a/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala @@ -103,7 +103,7 @@ class SystemComponentSpec extends UnitSpec with DefaultTestData { ), ) - testCases.foreach { case (operationStart, operationEnd, expected) => + testCases.forall { case (operationStart, operationEnd, expected) => val operationTimeBuilder = setup() operationStart.foreach(operationTimeBuilder.withStart) operationEnd.foreach(operationTimeBuilder.withEnd) @@ -115,8 +115,8 @@ class SystemComponentSpec extends UnitSpec with DefaultTestData { simulationEnd, operationTime, ) - interval should be(expected) - } + interval == expected + } shouldBe true } "reject an operation end that is before the operation start" in { diff --git a/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala b/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala index 4e7ab8e76f..04efdbec59 100644 --- a/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala @@ -118,14 +118,23 @@ class WecModelSpec extends UnitSpec with DefaultTestData { "calculate active power output depending on velocity" in { val wecModel = buildWecModel() - val velocities = - Seq(1.0, 2.0, 3.0, 7.0, 9.0, 13.0, 15.0, 19.0, 23.0, 27.0, 34.0, 40.0) - val expectedPowers = - Seq(0, -2948.8095851378266, -24573.41320418286, -522922.2325710509, - -1140000, -1140000, -1140000, -1140000, -1140000, -1140000, - -24573.39638823692, 0) - - velocities.zip(expectedPowers).foreach { case (velocity, power) => + val testCases = Table( + ("velocity", "expectedPower"), + (1.0, 0.0), + (2.0, -2948.8095851378266), + (3.0, -24573.41320418286), + (7.0, -522922.2325710509), + (9.0, -1140000.0), + (13.0, -1140000.0), + (15.0, -1140000.0), + (19.0, -1140000.0), + (23.0, -1140000.0), + (27.0, -1140000.0), + (34.0, -24573.39638823692), + (40.0, 0.0), + ) + + forAll(testCases) { (velocity: Double, expectedPower: Double) => val wecData = WecRelevantData( MetersPerSecond(velocity), Celsius(20), @@ -133,9 +142,9 @@ class WecModelSpec extends UnitSpec with DefaultTestData { ) val result = wecModel.calculateActivePower(ModelState.ConstantState, wecData) - val expectedPower = Watts(power) + val expectedPowerInWatts = Watts(expectedPower) - result should be(expectedPower) + result should be(expectedPowerInWatts) } } @@ -170,13 +179,14 @@ class WecModelSpec extends UnitSpec with DefaultTestData { "calculate active power output depending on temperature" in { val wecModel = buildWecModel() - val temperatures = Seq(35.0, 20.0, -25.0) - val expectedPowers = - Seq(-23377.23862017266, -24573.41320418286, -29029.60338829823) + val testCases = Table( + ("temperature", "expectedPower"), + (35.0, -23377.23862017266), + (20.0, -24573.41320418286), + (-25.0, -29029.60338829823), + ) - for (i <- temperatures.indices) { - val temperature = temperatures(i) - val expectedPower = expectedPowers(i) + forAll(testCases) { (temperature: Double, expectedPower: Double) => val wecData = WecRelevantData( MetersPerSecond(3.0), Celsius(temperature), diff --git a/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala b/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala index d3492ea6f2..1682d21b0c 100644 --- a/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala @@ -115,7 +115,8 @@ class FixedLoadModelSpec forAll(testData) { (reference, expectedPower: Power) => val relevantData = FixedLoadModel.FixedLoadRelevantData - val scales = (0 to 20).map(_ * 0.1) + val scales: LazyList[Double] = + LazyList.iterate(0.0)(_ + 0.1).takeWhile(_ <= 2.0) scales.foreach { scale => val scaledSRated = Kilowatts( From 0d6b21655b7668cbf927c8249322ba8c57864cc1 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Thu, 19 Sep 2024 00:02:34 +0200 Subject: [PATCH 4/7] Use forall(testCases) --- .../model/grid/SystemComponentSpec.scala | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala b/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala index 99505d928e..098282e388 100644 --- a/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/grid/SystemComponentSpec.scala @@ -75,7 +75,8 @@ class SystemComponentSpec extends UnitSpec with DefaultTestData { val simulationEnd: ZonedDateTime = TimeUtil.withDefaults.toZonedDateTime("2019-01-02T00:00:00Z") - val testCases = Seq( + val testCases = Table( + ("operationStart", "operationEnd", "expectedInterval"), ( Some(TimeUtil.withDefaults.toZonedDateTime("2019-01-01T00:00:00Z")), Some(TimeUtil.withDefaults.toZonedDateTime("2019-01-02T00:00:00Z")), @@ -103,20 +104,24 @@ class SystemComponentSpec extends UnitSpec with DefaultTestData { ), ) - testCases.forall { case (operationStart, operationEnd, expected) => - val operationTimeBuilder = setup() - operationStart.foreach(operationTimeBuilder.withStart) - operationEnd.foreach(operationTimeBuilder.withEnd) - val operationTime: OperationTime = operationTimeBuilder.build() - - val interval: OperationInterval = - SystemComponent.determineOperationInterval( - defaultSimulationStart, - simulationEnd, - operationTime, - ) - interval == expected - } shouldBe true + forAll(testCases) { + ( + operationStart: Option[ZonedDateTime], + operationEnd: Option[ZonedDateTime], + expected: OperationInterval, + ) => + val operationTimeBuilder = setup() + operationStart.foreach(operationTimeBuilder.withStart) + operationEnd.foreach(operationTimeBuilder.withEnd) + val operationTime: OperationTime = operationTimeBuilder.build() + val interval: OperationInterval = + SystemComponent.determineOperationInterval( + defaultSimulationStart, + simulationEnd, + operationTime, + ) + interval should be(expected) + } } "reject an operation end that is before the operation start" in { From f73b11f325bb793167059e0001acfb7395dcdb31 Mon Sep 17 00:00:00 2001 From: Sebastian Peter Date: Thu, 19 Sep 2024 10:27:09 +0200 Subject: [PATCH 5/7] Adapted one more test in WecModelSpec Signed-off-by: Sebastian Peter --- .../model/participant/WecModelSpec.scala | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala b/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala index 04efdbec59..82f69d3b0d 100644 --- a/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/participant/WecModelSpec.scala @@ -104,15 +104,22 @@ class WecModelSpec extends UnitSpec with DefaultTestData { "determine Betz coefficient correctly" in { val wecModel = buildWecModel() - val velocities = Seq(2.0, 2.5, 18.0, 27.0, 34.0, 40.0) - val expectedBetzResults = Seq(0.115933516, 0.2010945555, 0.108671106, - 0.032198846, 0.000196644, 0.0) - velocities.zip(expectedBetzResults).foreach { - case (velocity, betzResult) => - val windVel = MetersPerSecond(velocity) - val betzFactor = wecModel.determineBetzCoefficient(windVel) - val expected = Each(betzResult) - betzFactor shouldEqual expected + + val testCases = Table( + ("velocity", "expectedBetzResult"), + (2.0, 0.115933516), + (2.5, 0.2010945555), + (18.0, 0.108671106), + (27.0, 0.032198846), + (34.0, 0.000196644), + (40.0, 0.0), + ) + + forAll(testCases) { case (velocity: Double, expectedBetzResult: Double) => + val windVel = MetersPerSecond(velocity) + val betzFactor = wecModel.determineBetzCoefficient(windVel) + + betzFactor shouldEqual Each(expectedBetzResult) } } From 20ee6e35d2043ad531de371711d73a26630ddbdf Mon Sep 17 00:00:00 2001 From: Johannes Bao Date: Thu, 19 Sep 2024 14:26:34 +0200 Subject: [PATCH 6/7] fix power flow --- .../simona/agent/participant/ParticipantAgentFundamentals.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/scala/edu/ie3/simona/agent/participant/ParticipantAgentFundamentals.scala b/src/main/scala/edu/ie3/simona/agent/participant/ParticipantAgentFundamentals.scala index 0b0ad206e0..984815a846 100644 --- a/src/main/scala/edu/ie3/simona/agent/participant/ParticipantAgentFundamentals.scala +++ b/src/main/scala/edu/ie3/simona/agent/participant/ParticipantAgentFundamentals.scala @@ -808,6 +808,7 @@ protected trait ParticipantAgentFundamentals[ nextActivation, ) + unstashAll() stay() using stateDataFinal } From f7dc403b198ca38b25f59009fb7dae1f384d3032 Mon Sep 17 00:00:00 2001 From: Johannes Bao Date: Thu, 19 Sep 2024 14:36:00 +0200 Subject: [PATCH 7/7] added changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e5d0b8a71..5faecc0281 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -111,6 +111,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix activation of Hp when not under control of an EM [#922](https://github.com/ie3-institute/simona/issues/922) - Fix expected secondaryData in baseStateData [#955](https://github.com/ie3-institute/simona/issues/955) - Improve code quality in fixedloadmodelspec and other tests [#919](https://github.com/ie3-institute/simona/issues/919) +- Fix power flow calculation with em agents [#962](https://github.com/ie3-institute/simona/issues/962) ## [3.0.0] - 2023-08-07