From 3dc7523f0d975e90488a375ca06ec0a539955d3b Mon Sep 17 00:00:00 2001 From: marvinheintze Date: Wed, 27 Nov 2024 17:06:26 +0100 Subject: [PATCH 1/6] adapted tests to new hasAdditionalDemand and put negative cases in brackets --- .../simona/model/thermal/ThermalGrid.scala | 9 +++- .../model/thermal/ThermalGridSpec.scala | 44 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 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 4f8f3e0631..dda5eb149a 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -485,6 +485,13 @@ object ThermalGrid { def hasRequiredDemand: Boolean = required > zeroMWH def hasAdditionalDemand: Boolean = possible > zeroMWH + + /* for possible negative energies + def hasRequiredNegativeDemand: Boolean = required < zeroMWH + + def hasAdditionalNegativeDemand: Boolean = possible < zeroMWH + + */ } object ThermalEnergyDemand { @@ -502,7 +509,7 @@ object ThermalGrid { possible: Energy, ): ThermalEnergyDemand = { if ( - math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) + math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) && math.signum(possible.toKilowattHours) <= math.signum(required.toKilowattHours) ) throw new InvalidParameterException( s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala index b59e6000fb..0171817efe 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala @@ -30,6 +30,8 @@ class ThermalGridSpec extends UnitSpec { }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } + /* negative values test: + "throw exception for non-sensible input (negative)" in { val possible = MegawattHours(-40d) val required = MegawattHours(-42d) @@ -39,6 +41,8 @@ class ThermalGridSpec extends UnitSpec { }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } + */ + "set the correct values, if they are sensible" in { val possible = MegawattHours(45d) val required = MegawattHours(42d) @@ -67,6 +71,11 @@ class ThermalGridSpec extends UnitSpec { val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe false energyDemand.hasAdditionalDemand shouldBe false + /* negative values test: + + energyDemand.hasRequiredNegativeDemand shouldBe false + energyDemand.hasAdditionalNegativeDemand shouldBe false + */ } "return proper information, if no required but additional demand is apparent" in { @@ -76,6 +85,11 @@ class ThermalGridSpec extends UnitSpec { val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe false energyDemand.hasAdditionalDemand shouldBe true + /* negative values test: + + energyDemand.hasRequiredNegativeDemand shouldBe false + energyDemand.hasAdditionalNegativeDemand shouldBe false + */ } "throw exception, if required demand is higher than possible demand" in { @@ -93,6 +107,11 @@ class ThermalGridSpec extends UnitSpec { val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe true energyDemand.hasAdditionalDemand shouldBe true + /* negative values test: + + energyDemand.hasRequiredNegativeDemand shouldBe false + energyDemand.hasAdditionalNegativeDemand shouldBe false + */ } // FIXME: Think about "negative demand", maybe add more cases as well @@ -102,7 +121,12 @@ class ThermalGridSpec extends UnitSpec { val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe false - energyDemand.hasAdditionalDemand shouldBe true + energyDemand.hasAdditionalDemand shouldBe false + /*negative values test: + + energyDemand.hasRequiredNegativeDemand shouldBe true + energyDemand.hasAdditionalNegativeDemand shouldBe true + */ } } @@ -122,6 +146,24 @@ class ThermalGridSpec extends UnitSpec { totalDemand.required should approximate(MegawattHours(68d)) totalDemand.possible should approximate(MegawattHours(75d)) } + /*negative values test: + + "deliver proper negative results" in { + val energyDemand1 = ThermalEnergyDemand( + MegawattHours(-45d), + MegawattHours(-47d), + ) + val energyDemand2 = ThermalEnergyDemand( + MegawattHours(-23d), + MegawattHours(-28d), + ) + + val totalDemand = energyDemand1 + energyDemand2 + + totalDemand.required should approximate(MegawattHours(-68d)) + totalDemand.possible should approximate(MegawattHours(-75d)) + } + */ } } } From b00f5f837c6442f11d2e4cf254fe495337230bd6 Mon Sep 17 00:00:00 2001 From: marvinheintze Date: Wed, 27 Nov 2024 17:21:39 +0100 Subject: [PATCH 2/6] spotless --- .../scala/edu/ie3/simona/model/thermal/ThermalGrid.scala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 c84d1a6e78..e37fcfb679 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -602,7 +602,11 @@ object ThermalGrid { possible: Energy, ): ThermalEnergyDemand = { if ( - math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) && math.signum(possible.toKilowattHours) <= math.signum(required.toKilowattHours) + math.abs(possible.toKilowattHours) < math.abs( + required.toKilowattHours + ) && math.signum(possible.toKilowattHours) <= math.signum( + required.toKilowattHours + ) ) throw new InvalidParameterException( s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." From 22cc86b08587bac9eec8609a2ffbc2f750a2bacc Mon Sep 17 00:00:00 2001 From: marvinheintze Date: Tue, 3 Dec 2024 00:06:02 +0100 Subject: [PATCH 3/6] added exception for negative values + removed negative cases --- .../simona/model/thermal/ThermalGrid.scala | 24 +++--- .../model/thermal/ThermalGridSpec.scala | 84 ++++++------------- 2 files changed, 35 insertions(+), 73 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 e37fcfb679..6e59c48f4f 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -31,6 +31,7 @@ import squants.{Energy, Power, Temperature} import java.time.ZonedDateTime import scala.jdk.CollectionConverters.SetHasAsScala +import scala.language.postfixOps /** Calculation model for a thermal grid. It is assumed, that all elements are * connected directly with exactly one thermal bus @@ -579,12 +580,6 @@ object ThermalGrid { def hasAdditionalDemand: Boolean = possible > zeroMWh - /* for possible negative energies - def hasRequiredNegativeDemand: Boolean = required < zeroMWH - - def hasAdditionalNegativeDemand: Boolean = possible < zeroMWH - - */ } object ThermalEnergyDemand { @@ -602,17 +597,20 @@ object ThermalGrid { possible: Energy, ): ThermalEnergyDemand = { if ( - math.abs(possible.toKilowattHours) < math.abs( - required.toKilowattHours - ) && math.signum(possible.toKilowattHours) <= math.signum( - required.toKilowattHours - ) + math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) ) throw new InvalidParameterException( s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." ) - else - new ThermalEnergyDemand(required, possible) + else { + if (possible.toKilowattHours < 0 || required.toKilowattHours < 0) + throw new InvalidParameterException( + s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + ) + else { + new ThermalEnergyDemand(required, possible) + } + } } def noDemand: ThermalEnergyDemand = ThermalEnergyDemand( diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala index fe43d4d43a..aa7c0868f3 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala @@ -36,19 +36,6 @@ class ThermalGridSpec }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } - /* negative values test: - - "throw exception for non-sensible input (negative)" in { - val possible = MegawattHours(-40d) - val required = MegawattHours(-42d) - - intercept[InvalidParameterException] { - ThermalEnergyDemand(required, possible) - }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." - } - - */ - "set the correct values, if they are sensible" in { val possible = MegawattHours(45d) val required = MegawattHours(42d) @@ -77,11 +64,6 @@ class ThermalGridSpec val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe false energyDemand.hasAdditionalDemand shouldBe false - /* negative values test: - - energyDemand.hasRequiredNegativeDemand shouldBe false - energyDemand.hasAdditionalNegativeDemand shouldBe false - */ } "return proper information, if no required but additional demand is apparent" in { @@ -91,11 +73,6 @@ class ThermalGridSpec val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe false energyDemand.hasAdditionalDemand shouldBe true - /* negative values test: - - energyDemand.hasRequiredNegativeDemand shouldBe false - energyDemand.hasAdditionalNegativeDemand shouldBe false - */ } "throw exception, if required demand is higher than possible demand" in { @@ -106,6 +83,30 @@ class ThermalGridSpec }.getMessage shouldBe s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." } + "throw exception, if required demand is smaller than zero" in { + val required = MegawattHours(-2d) + val possible = MegawattHours(5d) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + } + + "throw exception, if possible demand is smaller than zero" in { + val required = MegawattHours(2d) + val possible = MegawattHours(-5d) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + } + + "throw exception, if possible and required demand are smaller than zero" in { + val required = MegawattHours(-2d) + val possible = MegawattHours(-5d) + intercept[InvalidParameterException] { + ThermalEnergyDemand(required, possible) + }.getMessage shouldBe s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." + } + "return proper information, if required and additional demand is apparent" in { val required = MegawattHours(45d) val possible = MegawattHours(47d) @@ -113,27 +114,8 @@ class ThermalGridSpec val energyDemand = ThermalEnergyDemand(required, possible) energyDemand.hasRequiredDemand shouldBe true energyDemand.hasAdditionalDemand shouldBe true - /* negative values test: - - energyDemand.hasRequiredNegativeDemand shouldBe false - energyDemand.hasAdditionalNegativeDemand shouldBe false - */ } - // FIXME: Think about "negative demand", maybe add more cases as well - "return proper information, if no required but additional demand is apparent (negative)" in { - val required = MegawattHours(-10d) - val possible = MegawattHours(-45d) - - val energyDemand = ThermalEnergyDemand(required, possible) - energyDemand.hasRequiredDemand shouldBe false - energyDemand.hasAdditionalDemand shouldBe false - /*negative values test: - - energyDemand.hasRequiredNegativeDemand shouldBe true - energyDemand.hasAdditionalNegativeDemand shouldBe true - */ - } } "adding two demands" should { @@ -152,24 +134,6 @@ class ThermalGridSpec totalDemand.required should approximate(MegawattHours(68d)) totalDemand.possible should approximate(MegawattHours(75d)) } - /*negative values test: - - "deliver proper negative results" in { - val energyDemand1 = ThermalEnergyDemand( - MegawattHours(-45d), - MegawattHours(-47d), - ) - val energyDemand2 = ThermalEnergyDemand( - MegawattHours(-23d), - MegawattHours(-28d), - ) - - val totalDemand = energyDemand1 + energyDemand2 - - totalDemand.required should approximate(MegawattHours(-68d)) - totalDemand.possible should approximate(MegawattHours(-75d)) - } - */ } } "ThermalGridState" should { From ff4a0bc41ecb465e61f7ee678934a3342eed7226 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Thu, 16 Jan 2025 12:39:25 +0100 Subject: [PATCH 4/6] refactor nested if-clause --- .../ie3/simona/model/thermal/ThermalGrid.scala | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 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 6e59c48f4f..f9d9e5a2a6 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -600,17 +600,15 @@ object ThermalGrid { math.abs(possible.toKilowattHours) < math.abs(required.toKilowattHours) ) throw new InvalidParameterException( - s"The possible amount of energy {$possible} is smaller than the required amount of energy {$required}. This is not supported." + s"The possible amount of energy $possible is smaller than the required amount of energy $required. This is not supported." ) - else { - if (possible.toKilowattHours < 0 || required.toKilowattHours < 0) - throw new InvalidParameterException( - s"The possible {$possible} or required {$required} amount of energy is smaller than zero. This is not supported." - ) - else { - new ThermalEnergyDemand(required, possible) - } - } + + if (possible.toKilowattHours < 0 || required.toKilowattHours < 0) + throw new InvalidParameterException( + s"The possible $possible or required $required amount of energy cannot be negative. This is not supported." + ) + + new ThermalEnergyDemand(required, possible) } def noDemand: ThermalEnergyDemand = ThermalEnergyDemand( From f86f22107ba591e4b05f40376d442c4117386ee9 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Thu, 16 Jan 2025 12:40:42 +0100 Subject: [PATCH 5/6] fmt --- .../scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala index aa7c0868f3..b259529471 100644 --- a/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala +++ b/src/test/scala/edu/ie3/simona/model/thermal/ThermalGridSpec.scala @@ -115,7 +115,6 @@ class ThermalGridSpec energyDemand.hasRequiredDemand shouldBe true energyDemand.hasAdditionalDemand shouldBe true } - } "adding two demands" should { From 4a5d0d61b1d0787554755b9527d4894e9522774c Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Thu, 16 Jan 2025 12:44:05 +0100 Subject: [PATCH 6/6] adapt def of hasAdditionalDemand --- src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala | 3 +-- 1 file changed, 1 insertion(+), 2 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 f9d9e5a2a6..56de254c2a 100644 --- a/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala +++ b/src/main/scala/edu/ie3/simona/model/thermal/ThermalGrid.scala @@ -578,8 +578,7 @@ object ThermalGrid { def hasRequiredDemand: Boolean = required > zeroMWh - def hasAdditionalDemand: Boolean = possible > zeroMWh - + def hasAdditionalDemand: Boolean = possible > required } object ThermalEnergyDemand {