Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register missing units to addUnit #282

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased/Snapshot]

### Added
- Added quantity for volumetric flow rate [#363](https://github.com/ie3-institute/PowerSystemUtils/issues/363)
- 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)

## [2.0.0]

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/edu/ie3/util/quantities/PowerSystemUnits.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -76,26 +78,29 @@ public class PowerSystemUnits extends Units {
public static final Unit<Energy> WATTHOUR =
new TransformedUnit<>("Wh", JOULE, DoubleConverterFactory.withFactor(3600));

/** Varhour */
public static final Unit<Energy> VARHOUR =
new TransformedUnit<>("varh", JOULE, DoubleConverterFactory.withFactor(3600));

/** Kilowatthour */
public static final Unit<Energy> KILOWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, KILO);

public static final Unit<Energy> KILOVARHOUR = DoubleConverterFactory.withPrefix(VARHOUR, KILO);
/** Kilovarhour */
public static final Unit<Energy> KILOVARHOUR = MetricPrefix.KILO(VARHOUR);

/** Megawatthour */
public static final Unit<Energy> MEGAWATTHOUR = DoubleConverterFactory.withPrefix(WATTHOUR, MEGA);

public static final Unit<Energy> MEGAVARHOUR = DoubleConverterFactory.withPrefix(VARHOUR, MEGA);
/** Megavarhour */
public static final Unit<Energy> MEGAVARHOUR = MetricPrefix.MEGA(VARHOUR);

/** Watthour per metre */
public static final Unit<SpecificEnergy> WATTHOUR_PER_METRE =
new ProductUnit<>(WATTHOUR.divide(METRE));

/** Kilowatthour per Kilometre */
public static final Unit<SpecificEnergy> KILOWATTHOUR_PER_KILOMETRE =
new TransformedUnit<>("kWh/km", WATTHOUR_PER_METRE, DoubleConverterFactory.withFactor(1d));
danielfeismann marked this conversation as resolved.
Show resolved Hide resolved
new TransformedUnit<>("kWh/km", WATTHOUR_PER_METRE, MultiplyConverter.of(1d));

/** Watthour per squaremetre */
public static final Unit<Irradiation> WATTHOUR_PER_SQUAREMETRE =
Expand Down Expand Up @@ -226,13 +231,17 @@ public class PowerSystemUnits extends Units {
private static final HashSet<String> REGISTERED_LABELS = new HashSet<>();

static {
// varh, kvarh, Mvarh are kept out of this because they register for the same units as Wh, kWh,
// MWh
Comment on lines -229 to -230
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this remark for a reason... Something broke, when I added varh etc., because they collided with Wh etc.
You should do thorough testing, also in SIMONA, with this...

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²");
Expand All @@ -241,8 +250,12 @@ 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, "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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,4 +76,41 @@ 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"() {
danielfeismann marked this conversation as resolved.
Show resolved Hide resolved
when:
def dut = Quantities.getQuantity(input)

then:
dut.unit.equals(expectedUnit)

where:
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³"
}

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"
}

}