From 18cabf015da53ee31a6590474e0f653345af5cd8 Mon Sep 17 00:00:00 2001 From: Daniel Feismann <98817556+danielfeismann@users.noreply.github.com> Date: Fri, 22 Jul 2022 08:01:45 +0200 Subject: [PATCH 1/8] register MWh, kvarh, Mvarh and varh to addUnit --- .../edu/ie3/util/quantities/PowerSystemUnits.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java index 59c00518..8104e05f 100644 --- a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java +++ b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java @@ -77,14 +77,22 @@ public class PowerSystemUnits extends Units { public static final Unit WATTHOUR = new TransformedUnit<>("Wh", JOULE, MultiplyConverter.of(3600)); + /** Varhour */ public static final Unit VARHOUR = new TransformedUnit<>("VArh", JOULE, MultiplyConverter.of(3600)); /** Kilowatthour */ public static final Unit KILOWATTHOUR = MetricPrefix.KILO(WATTHOUR); + /** Kilovarhour */ public static final Unit KILOVARHOUR = MetricPrefix.KILO(VARHOUR); + /** Megawatthour */ + public static final Unit MEGAWATTHOUR = MetricPrefix.MEGA(WATTHOUR); + + /** Megavarhour */ + public static final Unit MEGAVARHOUR = MetricPrefix.MEGA(VARHOUR); + /** Watthour per metre */ public static final Unit WATTHOUR_PER_METRE = new ProductUnit<>(WATTHOUR.divide(METRE)); @@ -196,6 +204,7 @@ public class PowerSystemUnits extends Units { addUnit(WATTHOUR_PER_METRE, "Wh/m"); addUnit(KILOWATTHOUR_PER_KILOMETRE, "kWh/km"); addUnit(KILOWATTHOUR, "kWh"); + addUnit(MEGAWATTHOUR, "MWh"); addUnit(OHM_PER_KILOMETRE, "Ω/km"); addUnit(SIEMENS_PER_KILOMETRE, "S/km"); addUnit(VOLTAMPERE, "VA"); @@ -207,6 +216,9 @@ public class PowerSystemUnits extends Units { addUnit(VAR, "VAr"); addUnit(KILOVAR, "kVAr"); addUnit(MEGAVAR, "MVAr"); + addUnit(VARHOUR, "varh"); + addUnit(KILOVARHOUR, "kvarh"); + addUnit(MEGAVARHOUR, "Mvarh"); addUnit(PU, "PU"); addUnit(EURO, "EUR"); addUnit(EURO_PER_KILOMETRE, "EUR/km"); From fd97702e51a3a9887cf0f99d63c84c4c27f9b9d2 Mon Sep 17 00:00:00 2001 From: Daniel Feismann <98817556+danielfeismann@users.noreply.github.com> Date: Thu, 28 Jul 2022 20:54:44 +0200 Subject: [PATCH 2/8] test for labeling units correctly --- .../quantities/PowerSystemUnitsTest.groovy | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy index 73cc708e..df29ed10 100644 --- a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy +++ b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy @@ -73,4 +73,26 @@ class PowerSystemUnitsTest extends Specification { Math.PI || 180.0 Math.PI * 3 / 2 || 270.0 } + + def "Units are labeled with the correct label of the expected unit symbol"() { + when: + def getUnitLabel = PowerSystemUnits.addUnit(input).symbol + + then: + getUnitLabel.equals(expectedLabel) + + where: + input || expectedLabel + WATTHOUR || "Wh" + KILOWATTHOUR_PER_KILOMETRE || "kWh/km" + VOLTAMPERE || "VA" + PU_PER_HOUR || "p.u./h" + VAR || "var" + VARHOUR || "varh" + PU || "p.u." + EURO || "€" + MICROFARAD_PER_KILOMETRE || "µF/km" + FARAD_PER_KILOMETRE || "F/km" + DEGREE_GEOM || "°" + } } From b5ed06f20e5cb055f356df061660fa5ee3eda80b Mon Sep 17 00:00:00 2001 From: Daniel Feismann <98817556+danielfeismann@users.noreply.github.com> Date: Thu, 28 Jul 2022 20:58:39 +0200 Subject: [PATCH 3/8] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7192e45d..f9c29d39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added implicit classes for `loactiontec.jts` Geometries that represent geographical geometries with functionality before located in `GeoUtils` [#163] (https://github.com/ie3-institute/PowerSystemUtils/issues/163) - `OsmEntity` and `OsmContainer` to provide a simple, lightweight representation of openstreetmap data +- Register missing units for serialization/deserialization. Added test for labeling unit symbols correctly within PowerSystemUnits [#280] (https://github.com/ie3-institute/PowerSystemUtils/issues/280) ### Changed - Refactored `GeoUtils`, moved them to the scala package and tailored them toward the `loactiontec.jts` Geometries used in the `OsmContainer` [#163] (https://github.com/ie3-institute/PowerSystemUtils/issues/163) From 4135c88191e322f2169416fbccf7a190b821e495 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Mon, 26 Jun 2023 13:40:15 +0200 Subject: [PATCH 4/8] add some units --- .../ie3/util/quantities/PowerSystemUnits.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java index da2e21e9..af278f8d 100644 --- a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java +++ b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java @@ -12,9 +12,11 @@ import java.util.HashSet; import java.util.logging.Level; import java.util.logging.Logger; +import javax.measure.MetricPrefix; import javax.measure.Unit; import javax.measure.quantity.*; import tech.units.indriya.format.SimpleUnitFormat; +import tech.units.indriya.function.MultiplyConverter; import tech.units.indriya.unit.*; import tech.units.indriya.unit.Units; @@ -76,18 +78,21 @@ public class PowerSystemUnits extends Units { public static final Unit WATTHOUR = new TransformedUnit<>("Wh", JOULE, DoubleConverterFactory.withFactor(3600)); + /** Varhour */ public static final Unit VARHOUR = new TransformedUnit<>("varh", JOULE, DoubleConverterFactory.withFactor(3600)); /** Kilowatthour */ public static final Unit KILOWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, KILO); - public static final Unit KILOVARHOUR = DoubleConverterFactory.withPrefix(VARHOUR, KILO); + /** Kilovarhour */ + public static final Unit KILOVARHOUR = MetricPrefix.KILO(VARHOUR); /** Megawatthour */ public static final Unit MEGAWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, MEGA); - public static final Unit MEGAVARHOUR = DoubleConverterFactory.withPrefix(VARHOUR, MEGA); + /** Megavarhour */ + public static final Unit MEGAVARHOUR = MetricPrefix.MEGA(VARHOUR); /** Watthour per metre */ public static final Unit WATTHOUR_PER_METRE = @@ -95,7 +100,7 @@ public class PowerSystemUnits extends Units { /** Kilowatthour per Kilometre */ public static final Unit KILOWATTHOUR_PER_KILOMETRE = - new TransformedUnit<>("kWh/km", WATTHOUR_PER_METRE, DoubleConverterFactory.withFactor(1d)); + new TransformedUnit<>("kWh/km", WATTHOUR_PER_METRE, MultiplyConverter.of(1d)); /** Watthour per squaremetre */ public static final Unit WATTHOUR_PER_SQUAREMETRE = @@ -226,13 +231,17 @@ public class PowerSystemUnits extends Units { private static final HashSet REGISTERED_LABELS = new HashSet<>(); static { - // varh, kvarh, Mvarh are kept out of this because they register for the same units as Wh, kWh, - // MWh + addUnit(WATTHOUR, "Wh"); addUnit(WATTHOUR_PER_METRE, "Wh/m"); addUnit(KILOWATTHOUR_PER_KILOMETRE, "kWh/km"); + addUnit(KILOWATTHOUR, "kWh"); + addUnit(MEGAWATTHOUR, "MWh"); addUnit(OHM_PER_KILOMETRE, "Ω/km"); addUnit(SIEMENS_PER_KILOMETRE, "S/km"); addUnit(VOLTAMPERE, "VA"); + addUnit(WATT, "W"); + addUnit(KILOWATT, "kW"); + addUnit(MEGAWATT, "MW"); addUnit(KILOVOLTAMPERE, "kVA"); addUnit(MEGAVOLTAMPERE, "MVA"); addUnit(WATT_PER_SQUAREMETRE, "W/m²"); @@ -246,6 +255,7 @@ public class PowerSystemUnits extends Units { addUnit(MEGAVARHOUR, "Mvarh"); addUnit(PU, "p.u."); addUnit(EURO, "EUR"); + addUnit(EURO, "€"); addUnit(EURO_PER_KILOMETRE, "EUR/km"); addUnit(EURO_PER_WATTHOUR, "EUR/Wh"); addUnit(EURO_PER_KILOWATTHOUR, "EUR/kWh"); From adecc40c1530f08a1619a3ed8b35a25d64c644f6 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Mon, 26 Jun 2023 13:40:31 +0200 Subject: [PATCH 5/8] test for registered units --- .../quantities/PowerSystemUnitsTest.groovy | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy index df29ed10..10cf91e0 100644 --- a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy +++ b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy @@ -76,23 +76,28 @@ class PowerSystemUnitsTest extends Specification { def "Units are labeled with the correct label of the expected unit symbol"() { when: - def getUnitLabel = PowerSystemUnits.addUnit(input).symbol + def dut = Quantities.getQuantity(input) then: - getUnitLabel.equals(expectedLabel) + dut.unit.equals(expectedUnit) where: - input || expectedLabel - WATTHOUR || "Wh" - KILOWATTHOUR_PER_KILOMETRE || "kWh/km" - VOLTAMPERE || "VA" - PU_PER_HOUR || "p.u./h" - VAR || "var" - VARHOUR || "varh" - PU || "p.u." - EURO || "€" - MICROFARAD_PER_KILOMETRE || "µF/km" - FARAD_PER_KILOMETRE || "F/km" - DEGREE_GEOM || "°" + expectedUnit || input + WATTHOUR || "1 Wh" + KILOWATTHOUR_PER_KILOMETRE || "1 kWh/km" + VOLTAMPERE || "1 VA" + PU_PER_HOUR || "1 p.u./h" + VAR || "1 var" + VARHOUR || "1 varh" + PU || "1 p.u." + EURO || "99 EUR" + EURO || "99 €" + MICROFARAD_PER_KILOMETRE || "5 µF/km" + FARAD_PER_KILOMETRE || "3.14 F/km" + DEGREE_GEOM || "42 °" + CUBIC_METRE_PER_SECOND || "430431 m³/s" + PERCENT_PER_HOUR || "4 %/h" + MEGAWATT || "87 MW" + KILOWATTHOUR_PER_KELVIN_TIMES_CUBICMETRE || "2.034 kWh/K*m³" } } From 1a8b43f16f0c509130d476997aef099f9d948263 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Mon, 26 Jun 2023 13:49:50 +0200 Subject: [PATCH 6/8] test for exception when parsing unregistered unit --- .../ie3/util/quantities/PowerSystemUnitsTest.groovy | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy index 10cf91e0..c243d994 100644 --- a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy +++ b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy @@ -5,10 +5,13 @@ */ package edu.ie3.util.quantities +import edu.ie3.util.interval.LeftOpenInterval import spock.lang.Shared import spock.lang.Specification import tech.units.indriya.quantity.Quantities +import javax.measure.format.MeasurementParseException + import static edu.ie3.util.quantities.PowerSystemUnits.* import static tech.units.indriya.unit.Units.JOULE import static tech.units.indriya.unit.Units.RADIAN @@ -100,4 +103,14 @@ class PowerSystemUnitsTest extends Specification { MEGAWATT || "87 MW" KILOWATTHOUR_PER_KELVIN_TIMES_CUBICMETRE || "2.034 kWh/K*m³" } + + def "when an unregistered Unit should be parsed an Exception should be thrown"() { + when: + def dut = Quantities.getQuantity("1 abc") + + then: + MeasurementParseException exception = thrown(MeasurementParseException.class) + exception.message == "Parse Error" + } + } From b84dbc555a27bf5612321a6f728ad873a96bcecf Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Mon, 26 Jun 2023 15:32:56 +0200 Subject: [PATCH 7/8] Using DoubleConverterFactory --- src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java index af278f8d..bd0c4eb5 100644 --- a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java +++ b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java @@ -16,7 +16,6 @@ import javax.measure.Unit; import javax.measure.quantity.*; import tech.units.indriya.format.SimpleUnitFormat; -import tech.units.indriya.function.MultiplyConverter; import tech.units.indriya.unit.*; import tech.units.indriya.unit.Units; @@ -100,7 +99,7 @@ public class PowerSystemUnits extends Units { /** Kilowatthour per Kilometre */ public static final Unit KILOWATTHOUR_PER_KILOMETRE = - new TransformedUnit<>("kWh/km", WATTHOUR_PER_METRE, MultiplyConverter.of(1d)); + new TransformedUnit<>("kWh/km", WATTHOUR_PER_METRE, DoubleConverterFactory.withFactor(1d)); /** Watthour per squaremetre */ public static final Unit WATTHOUR_PER_SQUAREMETRE = From ee6dc906f9547d0efa5a2fbd7db5504a98495fc1 Mon Sep 17 00:00:00 2001 From: danielfeismann Date: Tue, 4 Jul 2023 09:47:17 +0200 Subject: [PATCH 8/8] handle codacy issues --- CHANGELOG.md | 2 +- .../java/edu/ie3/util/quantities/PowerSystemUnits.java | 5 ++--- .../edu/ie3/util/quantities/PowerSystemUnitsTest.groovy | 9 ++++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4705e097..54c4ff56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added quantity for volumetric flow rate [#363](https://github.com/ie3-institute/PowerSystemUtils/issues/363) -- Register missing units for serialization/deserialization. Added test for labeling unit symbols correctly within PowerSystemUnits [#280] (https://github.com/ie3-institute/PowerSystemUtils/issues/280) +- Register missing units for serialization/deserialization. Added test for labeling unit symbols correctly within PowerSystemUnits [#280](https://github.com/ie3-institute/PowerSystemUtils/issues/280) ## [2.0.0] diff --git a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java index bd0c4eb5..260dbb5c 100644 --- a/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java +++ b/src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java @@ -12,7 +12,6 @@ import java.util.HashSet; import java.util.logging.Level; import java.util.logging.Logger; -import javax.measure.MetricPrefix; import javax.measure.Unit; import javax.measure.quantity.*; import tech.units.indriya.format.SimpleUnitFormat; @@ -85,13 +84,13 @@ public class PowerSystemUnits extends Units { public static final Unit KILOWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, KILO); /** Kilovarhour */ - public static final Unit KILOVARHOUR = MetricPrefix.KILO(VARHOUR); + public static final Unit KILOVARHOUR = KILO(VARHOUR); /** Megawatthour */ public static final Unit MEGAWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, MEGA); /** Megavarhour */ - public static final Unit MEGAVARHOUR = MetricPrefix.MEGA(VARHOUR); + public static final Unit MEGAVARHOUR = MEGA(VARHOUR); /** Watthour per metre */ public static final Unit WATTHOUR_PER_METRE = diff --git a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy index c243d994..71eededc 100644 --- a/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy +++ b/src/test/groovy/edu/ie3/util/quantities/PowerSystemUnitsTest.groovy @@ -5,7 +5,6 @@ */ package edu.ie3.util.quantities -import edu.ie3.util.interval.LeftOpenInterval import spock.lang.Shared import spock.lang.Specification import tech.units.indriya.quantity.Quantities @@ -82,7 +81,7 @@ class PowerSystemUnitsTest extends Specification { def dut = Quantities.getQuantity(input) then: - dut.unit.equals(expectedUnit) + dut.unit == expectedUnit where: expectedUnit || input @@ -106,11 +105,11 @@ class PowerSystemUnitsTest extends Specification { def "when an unregistered Unit should be parsed an Exception should be thrown"() { when: - def dut = Quantities.getQuantity("1 abc") + def dut = Quantities.getQuantity("1 kWh") + dut then: - MeasurementParseException exception = thrown(MeasurementParseException.class) + MeasurementParseException exception = thrown(MeasurementParseException) exception.message == "Parse Error" } - }