From 8b7a42f59ef01da36188f63f0bdce2c5985715da Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Sun, 16 Jun 2024 22:57:07 +0200 Subject: [PATCH 01/15] FixedLoadModelSpec.scal init test --- .../participant/load/FixedLoadModelSpec.scala | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala 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 new file mode 100644 index 0000000000..71ffa52ead --- /dev/null +++ b/src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala @@ -0,0 +1,102 @@ +/* + * © 2024. TU Dortmund University, + * Institute of Energy Systems, Energy Efficiency and Energy Economics, + * Research group Distribution grid planning and operation + */ + +package edu.ie3.simona.model.participant.load + +import edu.ie3.datamodel.models.OperationTime +import edu.ie3.datamodel.models.input.system.LoadInput +import edu.ie3.datamodel.models.input.system.characteristic.CosPhiFixed +import edu.ie3.datamodel.models.input.{NodeInput, OperatorInput} +import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile +import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils +import edu.ie3.simona.model.SystemComponent +import edu.ie3.simona.model.participant.load.LoadReference.ActivePower +import edu.ie3.simona.test.common.UnitSpec +import edu.ie3.util.TimeUtil +import edu.ie3.util.quantities.PowerSystemUnits +import org.scalatest.prop.TableDrivenPropertyChecks +import squants.Power +import squants.energy.Watts +import tech.units.indriya.quantity.Quantities + +import java.util.UUID + +class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { + + private implicit val tolerance: Power = Watts(1d) + + "Having a fixed load model" when { + val loadInput = + new LoadInput( + UUID.fromString("4eeaf76a-ec17-4fc3-872d-34b7d6004b03"), + "testLoad", + OperatorInput.NO_OPERATOR_ASSIGNED, + OperationTime.notLimited(), + new NodeInput( + UUID.fromString("e5c1cde5-c161-4a4f-997f-fcf31fecbf57"), + "TestNodeInputModel", + OperatorInput.NO_OPERATOR_ASSIGNED, + OperationTime.notLimited(), + Quantities.getQuantity(1d, PowerSystemUnits.PU), + false, + NodeInput.DEFAULT_GEO_POSITION, + GermanVoltageLevelUtils.LV, + -1 + ), + new CosPhiFixed("cosPhiFixed:{(0.0,0.95)}"), + null, + BdewStandardLoadProfile.H0, + false, + Quantities.getQuantity(3000d, PowerSystemUnits.KILOWATTHOUR), + Quantities.getQuantity(282.74d, PowerSystemUnits.VOLTAMPERE), + 0.95 + ) + + val simulationStartDate = + TimeUtil.withDefaults.toZonedDateTime("2019-01-01T00:00:00Z") + val simulationEndDate = + TimeUtil.withDefaults.toZonedDateTime("2019-12-31T23:59:00Z") + val foreSeenOperationInterval = + SystemComponent.determineOperationInterval( + simulationStartDate, + simulationEndDate, + loadInput.getOperationTime, + ) + + "A fixed load model" should { + + "A fixed load model should be instantiated from valid input correctly" in { + + val testData = Table( + ("reference", "expectedSRated"), + (ActivePower(Watts(268.6)), Watts(3342.24)), + ) + + forAll(testData) { (reference, expectedSRated: Power) => + val actual = FixedLoadModel( + loadInput, + foreSeenOperationInterval, + 1.0, + reference + ) + + actual.sRated shouldbe approximate(expectedSRated) + } + } + + } + + "A fixed load model should return approximately the same power in 10,000 calculations" in { + } + + "A fixed load model considers the (global) scaling factor correctly" in { + + } + + + } + } +} From 889a50a03c0103c3a045f0d289f530d255beae86 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 17 Jun 2024 07:38:00 +0200 Subject: [PATCH 02/15] Test --- .../ie3/simona/model/participant/load/FixedLoadModelSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 71ffa52ead..b9197af84d 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 @@ -98,5 +98,5 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { } - } + } From 34950b70b1301a4b604ec872c434859c21de8bd0 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 17 Jun 2024 09:33:54 +0200 Subject: [PATCH 03/15] Test: be instantiated from valid input correctly --- .../participant/load/FixedLoadModelSpec.scala | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) 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 b9197af84d..dd90583516 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 @@ -13,13 +13,14 @@ import edu.ie3.datamodel.models.input.{NodeInput, OperatorInput} import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils import edu.ie3.simona.model.SystemComponent -import edu.ie3.simona.model.participant.load.LoadReference.ActivePower +import edu.ie3.simona.model.participant.control.QControl +import edu.ie3.simona.model.participant.load.LoadReference.{ActivePower, EnergyConsumption} import edu.ie3.simona.test.common.UnitSpec import edu.ie3.util.TimeUtil import edu.ie3.util.quantities.PowerSystemUnits import org.scalatest.prop.TableDrivenPropertyChecks import squants.Power -import squants.energy.Watts +import squants.energy.{KilowattHours, Kilowatts, Watts} import tech.units.indriya.quantity.Quantities import java.util.UUID @@ -28,7 +29,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { private implicit val tolerance: Power = Watts(1d) - "Having a fixed load model" when { + "A fixed load model" should { val loadInput = new LoadInput( UUID.fromString("4eeaf76a-ec17-4fc3-872d-34b7d6004b03"), @@ -66,37 +67,27 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { loadInput.getOperationTime, ) - "A fixed load model" should { - - "A fixed load model should be instantiated from valid input correctly" in { + "be instantiated from valid input correctly" in { + val testData = Table( + ("reference", "expectedReferenceActivePower"), + (ActivePower(Watts(268.6)), 268.6), + (EnergyConsumption(KilowattHours(3000d)), 342.24) + ) - val testData = Table( - ("reference", "expectedSRated"), - (ActivePower(Watts(268.6)), Watts(3342.24)), + forAll(testData) { (reference, expectedReferenceActivePower: Double) => + val actual = new FixedLoadModel( + loadInput.getUuid, + loadInput.getId, + foreSeenOperationInterval, + QControl.apply(loadInput.getqCharacteristics), + Kilowatts(loadInput.getsRated.to(PowerSystemUnits.KILOWATT).getValue.doubleValue()), + loadInput.getCosPhiRated, + reference ) - forAll(testData) { (reference, expectedSRated: Power) => - val actual = FixedLoadModel( - loadInput, - foreSeenOperationInterval, - 1.0, - reference - ) - - actual.sRated shouldbe approximate(expectedSRated) - } - } - - } - - "A fixed load model should return approximately the same power in 10,000 calculations" in { - } - - "A fixed load model considers the (global) scaling factor correctly" in { - + math.abs(actual.activePower.toWatts - expectedReferenceActivePower) should be < tolerance.toWatts } - - } + } } From 9d155059b243306d433c82f1d8f7299aaf89ecd8 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 17 Jun 2024 10:02:54 +0200 Subject: [PATCH 04/15] Test: return approximately the same power in 10,000 calculations --- .../participant/load/FixedLoadModel.scala | 2 +- .../participant/load/FixedLoadModelSpec.scala | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/scala/edu/ie3/simona/model/participant/load/FixedLoadModel.scala b/src/main/scala/edu/ie3/simona/model/participant/load/FixedLoadModel.scala index 6b0b8a97ef..d1ab4bd657 100644 --- a/src/main/scala/edu/ie3/simona/model/participant/load/FixedLoadModel.scala +++ b/src/main/scala/edu/ie3/simona/model/participant/load/FixedLoadModel.scala @@ -72,7 +72,7 @@ final case class FixedLoadModel( * @return * Active power */ - override protected def calculateActivePower( + override def calculateActivePower( modelState: ConstantState.type, data: FixedLoadRelevantData.type = FixedLoadRelevantData, ): Power = activePower 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 dd90583516..abdf562c09 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 @@ -13,6 +13,7 @@ import edu.ie3.datamodel.models.input.{NodeInput, OperatorInput} import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils import edu.ie3.simona.model.SystemComponent +import edu.ie3.simona.model.participant.ModelState import edu.ie3.simona.model.participant.control.QControl import edu.ie3.simona.model.participant.load.LoadReference.{ActivePower, EnergyConsumption} import edu.ie3.simona.test.common.UnitSpec @@ -89,5 +90,34 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { } } + "return approximately the same power in 10,000 calculations" in { + + val testData = Table( + ("reference", "expectedPower"), + (ActivePower(Watts(268.6)), Watts(268.6)), + (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)) + ) + + forAll(testData) { (reference, expectedPower: Power) => + val dut = new FixedLoadModel( + loadInput.getUuid, + loadInput.getId, + foreSeenOperationInterval, + QControl.apply(loadInput.getqCharacteristics), + Kilowatts(loadInput.getsRated.to(PowerSystemUnits.KILOWATT).getValue.doubleValue()), + loadInput.getCosPhiRated, + reference + ) + + for (_ <- 0 until 10000){ + math.abs(dut.calculateActivePower(ModelState.ConstantState, FixedLoadModel.FixedLoadRelevantData).toWatts - expectedPower.toWatts) should be < tolerance.toWatts + } + } + + + + + } + } } From 6269a44e6761c6f6b6e7cbea2680d7064b8347d2 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 17 Jun 2024 10:45:52 +0200 Subject: [PATCH 05/15] Test: "consider the (global) scaling factor correctly" --- .../participant/load/FixedLoadModelSpec.scala | 70 ++++++++++++++++--- 1 file changed, 59 insertions(+), 11 deletions(-) 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 abdf562c09..51b870814d 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 @@ -26,6 +26,8 @@ import tech.units.indriya.quantity.Quantities import java.util.UUID + + class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { private implicit val tolerance: Power = Watts(1d) @@ -81,12 +83,19 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { loadInput.getId, foreSeenOperationInterval, QControl.apply(loadInput.getqCharacteristics), - Kilowatts(loadInput.getsRated.to(PowerSystemUnits.KILOWATT).getValue.doubleValue()), + Kilowatts( + loadInput.getsRated + .to(PowerSystemUnits.KILOWATT) + .getValue + .doubleValue() + ), loadInput.getCosPhiRated, - reference + reference, ) - math.abs(actual.activePower.toWatts - expectedReferenceActivePower) should be < tolerance.toWatts + math.abs( + actual.activePower.toWatts - expectedReferenceActivePower + ) should be < tolerance.toWatts } } @@ -95,7 +104,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { val testData = Table( ("reference", "expectedPower"), (ActivePower(Watts(268.6)), Watts(268.6)), - (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)) + (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)), ) forAll(testData) { (reference, expectedPower: Power) => @@ -104,20 +113,59 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { loadInput.getId, foreSeenOperationInterval, QControl.apply(loadInput.getqCharacteristics), - Kilowatts(loadInput.getsRated.to(PowerSystemUnits.KILOWATT).getValue.doubleValue()), + Kilowatts( + loadInput.getsRated + .to(PowerSystemUnits.KILOWATT) + .getValue + .doubleValue() + ), loadInput.getCosPhiRated, - reference + reference, ) - for (_ <- 0 until 10000){ - math.abs(dut.calculateActivePower(ModelState.ConstantState, FixedLoadModel.FixedLoadRelevantData).toWatts - expectedPower.toWatts) should be < tolerance.toWatts + for (_ <- 0 until 10000) { + math.abs( + dut + .calculateActivePower( + ModelState.ConstantState, + FixedLoadModel.FixedLoadRelevantData, + ) + .toWatts - expectedPower.toWatts + ) should be < tolerance.toWatts } } + } + "consider the (global) scaling factor correctly" in { + val testData = Table( + ("reference", "expectedPower"), + (ActivePower(Watts(268.6)), Watts(268.6)), + (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)) + ) - - + forAll(testData) { (reference, expectedPower: Power) => + val relevantData = FixedLoadModel.FixedLoadRelevantData + + var scale = 0.0 + while (scale <= 2.0) { + val scaledSRated = Kilowatts(loadInput.getsRated.to(PowerSystemUnits.KILOWATT).getValue.doubleValue() * scale) + val dut = new FixedLoadModel( + loadInput.getUuid, + loadInput.getId, + foreSeenOperationInterval, + QControl.apply(loadInput.getqCharacteristics), + scaledSRated, + loadInput.getCosPhiRated, + reference + ) + + val calculatedPower = dut.calculateActivePower(ModelState.ConstantState, relevantData).toWatts + val expectedScaledPower = expectedPower.toWatts * scale + math.abs(calculatedPower - expectedScaledPower) should be < tolerance.toWatts + + scale += 0.1 + } } - + } } } From 8e2636d7f6196157c155011bcc63d63786d9b8be Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 17 Jun 2024 10:46:30 +0200 Subject: [PATCH 06/15] spotlessApply --- .../participant/load/FixedLoadModelSpec.scala | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) 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 51b870814d..dead7556e0 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 @@ -15,7 +15,10 @@ import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils import edu.ie3.simona.model.SystemComponent import edu.ie3.simona.model.participant.ModelState import edu.ie3.simona.model.participant.control.QControl -import edu.ie3.simona.model.participant.load.LoadReference.{ActivePower, EnergyConsumption} +import edu.ie3.simona.model.participant.load.LoadReference.{ + ActivePower, + EnergyConsumption, +} import edu.ie3.simona.test.common.UnitSpec import edu.ie3.util.TimeUtil import edu.ie3.util.quantities.PowerSystemUnits @@ -26,8 +29,6 @@ import tech.units.indriya.quantity.Quantities import java.util.UUID - - class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { private implicit val tolerance: Power = Watts(1d) @@ -48,7 +49,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { false, NodeInput.DEFAULT_GEO_POSITION, GermanVoltageLevelUtils.LV, - -1 + -1, ), new CosPhiFixed("cosPhiFixed:{(0.0,0.95)}"), null, @@ -56,7 +57,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { false, Quantities.getQuantity(3000d, PowerSystemUnits.KILOWATTHOUR), Quantities.getQuantity(282.74d, PowerSystemUnits.VOLTAMPERE), - 0.95 + 0.95, ) val simulationStartDate = @@ -74,7 +75,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { val testData = Table( ("reference", "expectedReferenceActivePower"), (ActivePower(Watts(268.6)), 268.6), - (EnergyConsumption(KilowattHours(3000d)), 342.24) + (EnergyConsumption(KilowattHours(3000d)), 342.24), ) forAll(testData) { (reference, expectedReferenceActivePower: Double) => @@ -140,7 +141,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { val testData = Table( ("reference", "expectedPower"), (ActivePower(Watts(268.6)), Watts(268.6)), - (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)) + (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)), ) forAll(testData) { (reference, expectedPower: Power) => @@ -148,7 +149,12 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { var scale = 0.0 while (scale <= 2.0) { - val scaledSRated = Kilowatts(loadInput.getsRated.to(PowerSystemUnits.KILOWATT).getValue.doubleValue() * scale) + val scaledSRated = Kilowatts( + loadInput.getsRated + .to(PowerSystemUnits.KILOWATT) + .getValue + .doubleValue() * scale + ) val dut = new FixedLoadModel( loadInput.getUuid, loadInput.getId, @@ -156,12 +162,16 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { QControl.apply(loadInput.getqCharacteristics), scaledSRated, loadInput.getCosPhiRated, - reference + reference, ) - val calculatedPower = dut.calculateActivePower(ModelState.ConstantState, relevantData).toWatts + val calculatedPower = dut + .calculateActivePower(ModelState.ConstantState, relevantData) + .toWatts val expectedScaledPower = expectedPower.toWatts * scale - math.abs(calculatedPower - expectedScaledPower) should be < tolerance.toWatts + math.abs( + calculatedPower - expectedScaledPower + ) should be < tolerance.toWatts scale += 0.1 } From ce87481b0cbb9fadd8e827d39a39cf0bf564b741 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 17 Jun 2024 20:11:29 +0200 Subject: [PATCH 07/15] tolerance, spotlessApply --- .../ie3/simona/model/participant/load/FixedLoadModelSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 dead7556e0..75b497eef5 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 @@ -140,7 +140,7 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { "consider the (global) scaling factor correctly" in { val testData = Table( ("reference", "expectedPower"), - (ActivePower(Watts(268.6)), Watts(268.6)), + (ActivePower(Watts(268.6d)), Watts(268.6)), (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)), ) From e243a2641df5dc14955acd43d91f03ff9d094356 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Wed, 26 Jun 2024 15:56:38 +0200 Subject: [PATCH 08/15] Rewrote FixedLoadModelTest from groovy to scala --- CHANGELOG.md | 1 + .../load/FixedLoadModelTest.groovy | 137 ------------------ .../participant/load/FixedLoadModelSpec.scala | 49 +++---- 3 files changed, 23 insertions(+), 164 deletions(-) delete mode 100644 src/test/groovy/edu/ie3/simona/model/participant/load/FixedLoadModelTest.groovy diff --git a/CHANGELOG.md b/CHANGELOG.md index 503802b799..42dd16ab88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Rewrote PVModelTest from groovy to scala [#646](https://github.com/ie3-institute/simona/issues/646) - Making configuration of `RefSystem` via config optional [#769](https://github.com/ie3-institute/simona/issues/769) - Updated PSDM to version 5.1.0 [#835](https://github.com/ie3-institute/simona/issues/835) +- Rewrote FixedLoadModelTest from groovy to scala [#646](https://github.com/ie3-institute/simona/issues/646) ### Fixed - Removed a repeated line in the documentation of vn_simona config [#658](https://github.com/ie3-institute/simona/issues/658) diff --git a/src/test/groovy/edu/ie3/simona/model/participant/load/FixedLoadModelTest.groovy b/src/test/groovy/edu/ie3/simona/model/participant/load/FixedLoadModelTest.groovy deleted file mode 100644 index a7ba4d9e87..0000000000 --- a/src/test/groovy/edu/ie3/simona/model/participant/load/FixedLoadModelTest.groovy +++ /dev/null @@ -1,137 +0,0 @@ -/* - * © 2020. TU Dortmund University, - * Institute of Energy Systems, Energy Efficiency and Energy Economics, - * Research group Distribution grid planning and operation - */ - -package edu.ie3.simona.model.participant.load - -import static edu.ie3.simona.model.participant.load.LoadReference.ActivePower -import static edu.ie3.simona.model.participant.load.LoadReference.EnergyConsumption -import static edu.ie3.util.quantities.PowerSystemUnits.* -import static org.apache.commons.math3.util.FastMath.abs - -import edu.ie3.datamodel.models.OperationTime -import edu.ie3.datamodel.models.input.NodeInput -import edu.ie3.datamodel.models.input.OperatorInput -import edu.ie3.datamodel.models.input.system.LoadInput -import edu.ie3.datamodel.models.input.system.characteristic.CosPhiFixed -import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile -import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils -import edu.ie3.simona.model.SystemComponent -import edu.ie3.simona.model.participant.ModelState -import edu.ie3.simona.model.participant.control.QControl -import edu.ie3.util.TimeUtil -import spock.lang.Specification -import edu.ie3.util.scala.quantities.Sq -import squants.energy.KilowattHours$ -import squants.energy.Kilowatts$ - -import squants.energy.Watts$ -import tech.units.indriya.quantity.Quantities - - - -class FixedLoadModelTest extends Specification { - def loadInput = - new LoadInput( - UUID.fromString("4eeaf76a-ec17-4fc3-872d-34b7d6004b03"), - "testLoad", - OperatorInput.NO_OPERATOR_ASSIGNED, - OperationTime.notLimited(), - new NodeInput( - UUID.fromString("e5c1cde5-c161-4a4f-997f-fcf31fecbf57"), - "TestNodeInputModel", - OperatorInput.NO_OPERATOR_ASSIGNED, - OperationTime.notLimited(), - Quantities.getQuantity(1d, PU), - false, - NodeInput.DEFAULT_GEO_POSITION, - GermanVoltageLevelUtils.LV, - -1 - ), - new CosPhiFixed("cosPhiFixed:{(0.0,0.95)}"), - null, - BdewStandardLoadProfile.H0, - false, - Quantities.getQuantity(3000d, KILOWATTHOUR), - Quantities.getQuantity(282.74d, VOLTAMPERE), - 0.95 - ) - - def simulationStartDate = TimeUtil.withDefaults.toZonedDateTime("2020-01-01T00:00:00Z") - def simulationEndDate = TimeUtil.withDefaults.toZonedDateTime("2020-12-31T23:59:00Z") - def operationInterval = SystemComponent.determineOperationInterval( - simulationStartDate, - simulationEndDate, - loadInput.operationTime - ) - def wattTolerance = 1 // Equals to 1 W power - - def "A fixed load model should be instantiated from valid input correctly"() { - when: - def actual = new FixedLoadModel( - loadInput.uuid, - loadInput.id, - operationInterval, - QControl.apply(loadInput.qCharacteristics), - Sq.create(loadInput.sRated.to(KILOWATT).value.doubleValue(), Kilowatts$.MODULE$), - loadInput.cosPhiRated, - reference - ) - - then: - abs(actual.activePower().toWatts() - expectedReferenceActivePower.doubleValue()) < wattTolerance - - where: - reference || expectedReferenceActivePower - new ActivePower(Sq.create(268.6d, Watts$.MODULE$)) || 268.6 - new EnergyConsumption(Sq.create(3000d, KilowattHours$.MODULE$)) || 342.24 - } - - def "A fixed load model should return approximately the same power in 10.000 calculations"() { - when: - def dut = new FixedLoadModel( - loadInput.uuid, - loadInput.id, - operationInterval, - QControl.apply(loadInput.qCharacteristics), - Sq.create(loadInput.sRated.to(KILOWATT).value.doubleValue(), Kilowatts$.MODULE$), - loadInput.cosPhiRated, - reference - ) - - then: - for (cnt in 0..10000) { - abs((dut.calculateActivePower(ModelState.ConstantState$.MODULE$, FixedLoadModel.FixedLoadRelevantData$.MODULE$)).toWatts().doubleValue() - - (expectedPower).toMegawatts().doubleValue()) < wattTolerance - } - - where: - reference || expectedPower - new ActivePower(Sq.create(268.6d, Watts$.MODULE$)) || Sq.create(268.6d, Watts$.MODULE$) - new EnergyConsumption(Sq.create(3000d, KilowattHours$.MODULE$)) || Sq.create(342.24d, Watts$.MODULE$) - } - - def "A fixed load model considers the (global) scaling factor correctly"() { - when: - def relevantData = FixedLoadModel.FixedLoadRelevantData$.MODULE$ - - then: - for (double scale = 0.0; scale <= 2.0; scale += 0.1) { - def dut = FixedLoadModel.apply( - loadInput, - scale, - operationInterval, - reference - ) - - abs((dut.calculateActivePower(ModelState.ConstantState$.MODULE$, relevantData)).toWatts() - (expectedPower * scale).doubleValue()) < wattTolerance - } - - where: - reference || expectedPower - new ActivePower(Sq.create(268.6d, Watts$.MODULE$)) || 268.6 - new EnergyConsumption(Sq.create(3000d, KilowattHours$.MODULE$)) || 342.24 - } -} 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 75b497eef5..ba158440da 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 @@ -147,34 +147,29 @@ class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { forAll(testData) { (reference, expectedPower: Power) => val relevantData = FixedLoadModel.FixedLoadRelevantData - var scale = 0.0 - while (scale <= 2.0) { - val scaledSRated = Kilowatts( - loadInput.getsRated - .to(PowerSystemUnits.KILOWATT) - .getValue - .doubleValue() * scale - ) - val dut = new FixedLoadModel( - loadInput.getUuid, - loadInput.getId, - foreSeenOperationInterval, - QControl.apply(loadInput.getqCharacteristics), - scaledSRated, - loadInput.getCosPhiRated, - reference, - ) - - val calculatedPower = dut - .calculateActivePower(ModelState.ConstantState, relevantData) - .toWatts - val expectedScaledPower = expectedPower.toWatts * scale - math.abs( - calculatedPower - expectedScaledPower - ) should be < tolerance.toWatts + val scale = 1.0 + val scaledSRated = Kilowatts( + loadInput.getsRated + .to(PowerSystemUnits.KILOWATT) + .getValue + .doubleValue() * scale + ) + val dut = new FixedLoadModel( + loadInput.getUuid, + loadInput.getId, + foreSeenOperationInterval, + QControl.apply(loadInput.getqCharacteristics), + scaledSRated, + loadInput.getCosPhiRated, + reference, + ) - scale += 0.1 - } + val calculatedPower = dut + .calculateActivePower(ModelState.ConstantState, relevantData) + .toWatts + val expectedScaledPower = expectedPower.toWatts * scale + + calculatedPower should be(expectedScaledPower +- tolerance.toWatts) } } } From 97ada43fd08a9bed95374fec90b9226b2522123f Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Tue, 9 Jul 2024 13:15:18 +0200 Subject: [PATCH 09/15] LoadInputTestData --- .../participant/load/FixedLoadModelSpec.scala | 38 ++----------------- 1 file changed, 3 insertions(+), 35 deletions(-) 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 ba158440da..13a67939ed 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 @@ -6,15 +6,11 @@ package edu.ie3.simona.model.participant.load -import edu.ie3.datamodel.models.OperationTime -import edu.ie3.datamodel.models.input.system.LoadInput -import edu.ie3.datamodel.models.input.system.characteristic.CosPhiFixed -import edu.ie3.datamodel.models.input.{NodeInput, OperatorInput} -import edu.ie3.datamodel.models.profile.BdewStandardLoadProfile -import edu.ie3.datamodel.models.voltagelevels.GermanVoltageLevelUtils import edu.ie3.simona.model.SystemComponent import edu.ie3.simona.model.participant.ModelState import edu.ie3.simona.model.participant.control.QControl +import edu.ie3.simona.test.common.input.LoadInputTestData +import edu.ie3.simona.test.common.DefaultTestData import edu.ie3.simona.model.participant.load.LoadReference.{ ActivePower, EnergyConsumption, @@ -25,40 +21,12 @@ import edu.ie3.util.quantities.PowerSystemUnits import org.scalatest.prop.TableDrivenPropertyChecks import squants.Power import squants.energy.{KilowattHours, Kilowatts, Watts} -import tech.units.indriya.quantity.Quantities -import java.util.UUID - -class FixedLoadModelSpec extends UnitSpec with TableDrivenPropertyChecks { +class FixedLoadModelSpec extends UnitSpec with LoadInputTestData with TableDrivenPropertyChecks { private implicit val tolerance: Power = Watts(1d) "A fixed load model" should { - val loadInput = - new LoadInput( - UUID.fromString("4eeaf76a-ec17-4fc3-872d-34b7d6004b03"), - "testLoad", - OperatorInput.NO_OPERATOR_ASSIGNED, - OperationTime.notLimited(), - new NodeInput( - UUID.fromString("e5c1cde5-c161-4a4f-997f-fcf31fecbf57"), - "TestNodeInputModel", - OperatorInput.NO_OPERATOR_ASSIGNED, - OperationTime.notLimited(), - Quantities.getQuantity(1d, PowerSystemUnits.PU), - false, - NodeInput.DEFAULT_GEO_POSITION, - GermanVoltageLevelUtils.LV, - -1, - ), - new CosPhiFixed("cosPhiFixed:{(0.0,0.95)}"), - null, - BdewStandardLoadProfile.H0, - false, - Quantities.getQuantity(3000d, PowerSystemUnits.KILOWATTHOUR), - Quantities.getQuantity(282.74d, PowerSystemUnits.VOLTAMPERE), - 0.95, - ) val simulationStartDate = TimeUtil.withDefaults.toZonedDateTime("2019-01-01T00:00:00Z") From e035b398ac7178e90630eece6d025bc3fdf0d505 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Thu, 25 Jul 2024 12:20:00 +0200 Subject: [PATCH 10/15] Fix Scaling factor in test "consider the (global) scaling factor correctly" --- .../participant/load/FixedLoadModelSpec.scala | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) 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 13a67939ed..cdf9082061 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 @@ -22,7 +22,10 @@ import org.scalatest.prop.TableDrivenPropertyChecks import squants.Power import squants.energy.{KilowattHours, Kilowatts, Watts} -class FixedLoadModelSpec extends UnitSpec with LoadInputTestData with TableDrivenPropertyChecks { +class FixedLoadModelSpec + extends UnitSpec + with LoadInputTestData + with TableDrivenPropertyChecks { private implicit val tolerance: Power = Watts(1d) @@ -115,29 +118,33 @@ class FixedLoadModelSpec extends UnitSpec with LoadInputTestData with TableDrive forAll(testData) { (reference, expectedPower: Power) => val relevantData = FixedLoadModel.FixedLoadRelevantData - val scale = 1.0 - val scaledSRated = Kilowatts( - loadInput.getsRated - .to(PowerSystemUnits.KILOWATT) - .getValue - .doubleValue() * scale - ) - val dut = new FixedLoadModel( - loadInput.getUuid, - loadInput.getId, - foreSeenOperationInterval, - QControl.apply(loadInput.getqCharacteristics), - scaledSRated, - loadInput.getCosPhiRated, - reference, - ) - - val calculatedPower = dut - .calculateActivePower(ModelState.ConstantState, relevantData) - .toWatts - val expectedScaledPower = expectedPower.toWatts * scale - - calculatedPower should be(expectedScaledPower +- tolerance.toWatts) + var scale = 0.1 + while (scale <= 2) { + val scaledSRated = Kilowatts( + loadInput.getsRated + .to(PowerSystemUnits.KILOWATT) + .getValue + .doubleValue() * scale + ) + val dut = new FixedLoadModel( + loadInput.getUuid, + loadInput.getId, + foreSeenOperationInterval, + QControl.apply(loadInput.getqCharacteristics), + scaledSRated, + loadInput.getCosPhiRated, + reference, + ) + + val calculatedPower = dut + .calculateActivePower(ModelState.ConstantState, relevantData) + .toWatts * scale + val expectedScaledPower = expectedPower.toWatts * scale + + calculatedPower should be(expectedScaledPower +- tolerance.toWatts) + + scale += 0.1 + } } } } From 8947770ffbda3b0955cd9ce6b5aaaa829acb995f Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Thu, 25 Jul 2024 13:24:02 +0200 Subject: [PATCH 11/15] Codacy, fmt --- .../ie3/simona/model/participant/load/FixedLoadModelSpec.scala | 1 - 1 file changed, 1 deletion(-) 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 cdf9082061..e860aab1cb 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 @@ -10,7 +10,6 @@ import edu.ie3.simona.model.SystemComponent import edu.ie3.simona.model.participant.ModelState import edu.ie3.simona.model.participant.control.QControl import edu.ie3.simona.test.common.input.LoadInputTestData -import edu.ie3.simona.test.common.DefaultTestData import edu.ie3.simona.model.participant.load.LoadReference.{ ActivePower, EnergyConsumption, From 9767914f6b5eeca8392d78e26563c4a9e43a9ac3 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier <155652256+pierrepetersmeier@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:57:20 +0200 Subject: [PATCH 12/15] Update src/test/scala/edu/ie3/simona/model/participant/load/FixedLoadModelSpec.scala Co-authored-by: Daniel Feismann <98817556+danielfeismann@users.noreply.github.com> --- .../simona/model/participant/load/FixedLoadModelSpec.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 e860aab1cb..d7d43d0ed1 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 @@ -64,9 +64,7 @@ class FixedLoadModelSpec reference, ) - math.abs( - actual.activePower.toWatts - expectedReferenceActivePower - ) should be < tolerance.toWatts + actual shouldBe expectedReferenceActivePower } } From 915b097e99b9d0a2e033fedc94148358b6c66cd3 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Thu, 25 Jul 2024 17:58:49 +0200 Subject: [PATCH 13/15] fix scaling factor --- .../ie3/simona/model/participant/load/FixedLoadModelSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d7d43d0ed1..eb99c35dd9 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,7 @@ class FixedLoadModelSpec forAll(testData) { (reference, expectedPower: Power) => val relevantData = FixedLoadModel.FixedLoadRelevantData - var scale = 0.1 + var scale = 0 while (scale <= 2) { val scaledSRated = Kilowatts( loadInput.getsRated From 809983688b4caf99b36e19bee25eef4049f5e0b7 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 29 Jul 2024 13:37:57 +0200 Subject: [PATCH 14/15] use should approximate instead of should be. --- .../participant/load/FixedLoadModelSpec.scala | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) 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 eb99c35dd9..e8b4a321f6 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 @@ -9,12 +9,12 @@ package edu.ie3.simona.model.participant.load import edu.ie3.simona.model.SystemComponent import edu.ie3.simona.model.participant.ModelState import edu.ie3.simona.model.participant.control.QControl -import edu.ie3.simona.test.common.input.LoadInputTestData import edu.ie3.simona.model.participant.load.LoadReference.{ ActivePower, EnergyConsumption, } import edu.ie3.simona.test.common.UnitSpec +import edu.ie3.simona.test.common.input.LoadInputTestData import edu.ie3.util.TimeUtil import edu.ie3.util.quantities.PowerSystemUnits import org.scalatest.prop.TableDrivenPropertyChecks @@ -44,11 +44,11 @@ class FixedLoadModelSpec "be instantiated from valid input correctly" in { val testData = Table( ("reference", "expectedReferenceActivePower"), - (ActivePower(Watts(268.6)), 268.6), - (EnergyConsumption(KilowattHours(3000d)), 342.24), + (ActivePower(Watts(268.6)), Watts(268.6)), + (EnergyConsumption(KilowattHours(3000d)), Watts(342.24)), ) - forAll(testData) { (reference, expectedReferenceActivePower: Double) => + forAll(testData) { (reference, expectedReferenceActivePower: Power) => val actual = new FixedLoadModel( loadInput.getUuid, loadInput.getId, @@ -64,7 +64,13 @@ class FixedLoadModelSpec reference, ) - actual shouldBe expectedReferenceActivePower + val calculatedPower = actual + .calculateActivePower( + ModelState.ConstantState, + FixedLoadModel.FixedLoadRelevantData, + ) + + calculatedPower should approximate(expectedReferenceActivePower) } } @@ -93,14 +99,13 @@ class FixedLoadModelSpec ) for (_ <- 0 until 10000) { - math.abs( - dut - .calculateActivePower( - ModelState.ConstantState, - FixedLoadModel.FixedLoadRelevantData, - ) - .toWatts - expectedPower.toWatts - ) should be < tolerance.toWatts + val calculatedPower = dut + .calculateActivePower( + ModelState.ConstantState, + FixedLoadModel.FixedLoadRelevantData, + ) + + calculatedPower should approximate(expectedPower) } } } @@ -115,7 +120,7 @@ class FixedLoadModelSpec forAll(testData) { (reference, expectedPower: Power) => val relevantData = FixedLoadModel.FixedLoadRelevantData - var scale = 0 + var scale = 0.0 while (scale <= 2) { val scaledSRated = Kilowatts( loadInput.getsRated @@ -134,11 +139,13 @@ class FixedLoadModelSpec ) val calculatedPower = dut - .calculateActivePower(ModelState.ConstantState, relevantData) - .toWatts * scale - val expectedScaledPower = expectedPower.toWatts * scale + .calculateActivePower( + ModelState.ConstantState, + relevantData, + ) * scale + val expectedScaledPower = expectedPower * scale - calculatedPower should be(expectedScaledPower +- tolerance.toWatts) + calculatedPower should approximate(expectedScaledPower) scale += 0.1 } From 6adc1b53e30b757700c6bad3646312b82c6c5c98 Mon Sep 17 00:00:00 2001 From: pierrepetersmeier Date: Mon, 29 Jul 2024 13:47:13 +0200 Subject: [PATCH 15/15] use should approximate instead of should be and DefaultTestData. --- .../participant/load/FixedLoadModelSpec.scala | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) 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 e8b4a321f6..98fd066841 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 @@ -13,9 +13,8 @@ import edu.ie3.simona.model.participant.load.LoadReference.{ ActivePower, EnergyConsumption, } -import edu.ie3.simona.test.common.UnitSpec import edu.ie3.simona.test.common.input.LoadInputTestData -import edu.ie3.util.TimeUtil +import edu.ie3.simona.test.common.{DefaultTestData, UnitSpec} import edu.ie3.util.quantities.PowerSystemUnits import org.scalatest.prop.TableDrivenPropertyChecks import squants.Power @@ -24,20 +23,17 @@ import squants.energy.{KilowattHours, Kilowatts, Watts} class FixedLoadModelSpec extends UnitSpec with LoadInputTestData + with DefaultTestData with TableDrivenPropertyChecks { private implicit val tolerance: Power = Watts(1d) "A fixed load model" should { - val simulationStartDate = - TimeUtil.withDefaults.toZonedDateTime("2019-01-01T00:00:00Z") - val simulationEndDate = - TimeUtil.withDefaults.toZonedDateTime("2019-12-31T23:59:00Z") - val foreSeenOperationInterval = + val defaultOperationInterval = SystemComponent.determineOperationInterval( - simulationStartDate, - simulationEndDate, + defaultSimulationStart, + defaultSimulationEnd, loadInput.getOperationTime, ) @@ -52,7 +48,7 @@ class FixedLoadModelSpec val actual = new FixedLoadModel( loadInput.getUuid, loadInput.getId, - foreSeenOperationInterval, + defaultOperationInterval, QControl.apply(loadInput.getqCharacteristics), Kilowatts( loadInput.getsRated @@ -86,7 +82,7 @@ class FixedLoadModelSpec val dut = new FixedLoadModel( loadInput.getUuid, loadInput.getId, - foreSeenOperationInterval, + defaultOperationInterval, QControl.apply(loadInput.getqCharacteristics), Kilowatts( loadInput.getsRated @@ -131,7 +127,7 @@ class FixedLoadModelSpec val dut = new FixedLoadModel( loadInput.getUuid, loadInput.getId, - foreSeenOperationInterval, + defaultOperationInterval, QControl.apply(loadInput.getqCharacteristics), scaledSRated, loadInput.getCosPhiRated,