Skip to content

Commit

Permalink
Add milliwatt hour energy unit (mWh) (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedragon authored and derekmorr committed Oct 3, 2017
1 parent 60a20c5 commit 1545e0b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion shared/src/main/scala/squants/energy/Energy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ final class Energy private (val value: Double, val unit: EnergyUnit)
def /(that: PowerRamp): TimeSquared = (this / that.timeIntegrated) * time

def toWattHours = to(WattHours)
def toMilliwattHours = to(MilliwattHours)
def toKilowattHours = to(KilowattHours)
def toMegawattHours = to(MegawattHours)
def toGigawattHours = to(GigawattHours)
Expand Down Expand Up @@ -99,7 +100,7 @@ object Energy extends Dimension[Energy] {
def name = "Energy"
def primaryUnit = WattHours
def siUnit = Joules
def units = Set(WattHours, KilowattHours, MegawattHours, GigawattHours,
def units = Set(WattHours, MilliwattHours, KilowattHours, MegawattHours, GigawattHours,
Joules, Picojoules, Nanojoules, Microjoules, Millijoules,
Kilojoules, Megajoules, Gigajoules, Terajoules,
BritishThermalUnits, MBtus, MMBtus, Ergs,
Expand All @@ -118,6 +119,11 @@ object WattHours extends EnergyUnit with PrimaryUnit {
val symbol = "Wh"
}

object MilliwattHours extends EnergyUnit {
val conversionFactor = Watts.conversionFactor * MetricSystem.Milli
val symbol = "mWh"
}

object KilowattHours extends EnergyUnit {
val conversionFactor = Watts.conversionFactor * MetricSystem.Kilo
val symbol = "kWh"
Expand Down Expand Up @@ -241,6 +247,8 @@ object ExaElectronVolt extends EnergyUnit {
object EnergyConversions {
lazy val wattHour = WattHours(1)
lazy val Wh = wattHour
lazy val milliwattHour = MilliwattHours(1)
lazy val mWh = milliwattHour
lazy val kilowattHour = KilowattHours(1)
lazy val kWh = kilowattHour
lazy val megawattHour = MegawattHours(1)
Expand Down Expand Up @@ -291,6 +299,7 @@ object EnergyConversions {
def terajoules = Terajoules(n)

def Wh = WattHours(n)
def mWh = MilliwattHours(n)
def kWh = KilowattHours(n)
def MWh = MegawattHours(n)
def GWh = GigawattHours(n)
Expand Down
8 changes: 8 additions & 0 deletions shared/src/test/scala/squants/energy/EnergySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class EnergySpec extends FlatSpec with Matchers {

it should "create values using UOM factories" in {
WattHours(1).toWattHours should be(1)
MilliwattHours(1).toMilliwattHours should be(1)
KilowattHours(1).toKilowattHours should be(1)
MegawattHours(1).toMegawattHours should be(1)
GigawattHours(1).toGigawattHours should be(1)
Expand Down Expand Up @@ -60,6 +61,7 @@ class EnergySpec extends FlatSpec with Matchers {
it should "create values from properly formatted Strings" in {
Energy("10.22 J").get should be(Joules(10.22))
Energy("10.22 Wh").get should be(WattHours(10.22))
Energy("10.22 mWh").get should be(MilliwattHours(10.22))
Energy("10.22 kWh").get should be(KilowattHours(10.22))
Energy("10.22 MWh").get should be(MegawattHours(10.22))
Energy("10.22 GWh").get should be(GigawattHours(10.22))
Expand All @@ -83,6 +85,7 @@ class EnergySpec extends FlatSpec with Matchers {
val x = WattHours(1)

x.toWattHours should be(1)
x.toMilliwattHours should be(1 / MetricSystem.Milli)
x.toKilowattHours should be(1 / MetricSystem.Kilo)
x.toMegawattHours should be(1 / MetricSystem.Mega)
x.toGigawattHours should be(1 / MetricSystem.Giga)
Expand Down Expand Up @@ -115,6 +118,7 @@ class EnergySpec extends FlatSpec with Matchers {

it should "return properly formatted strings for all supported Units of Measure" in {
WattHours(1).toString(WattHours) should be("1.0 Wh")
MilliwattHours(1).toString(MilliwattHours) should be("1.0 mWh")
KilowattHours(1).toString(KilowattHours) should be("1.0 kWh")
MegawattHours(1).toString(MegawattHours) should be("1.0 MWh")
GigawattHours(1).toString(GigawattHours) should be("1.0 GWh")
Expand Down Expand Up @@ -207,6 +211,8 @@ class EnergySpec extends FlatSpec with Matchers {

wattHour should be(WattHours(1))
Wh should be(WattHours(1))
milliwattHour should be(MilliwattHours(1))
mWh should be(MilliwattHours(1))
kilowattHour should be(KilowattHours(1))
kWh should be(KilowattHours(1))
megawattHour should be(MegawattHours(1))
Expand Down Expand Up @@ -242,6 +248,7 @@ class EnergySpec extends FlatSpec with Matchers {

val d = 10D
d.Wh should be(WattHours(d))
d.mWh should be(MilliwattHours(d))
d.kWh should be(KilowattHours(d))
d.MWh should be(MegawattHours(d))
d.GWh should be(GigawattHours(d))
Expand Down Expand Up @@ -280,6 +287,7 @@ class EnergySpec extends FlatSpec with Matchers {

"10.22 J".toEnergy.get should be(Joules(10.22))
"10.22 Wh".toEnergy.get should be(WattHours(10.22))
"10.22 mWh".toEnergy.get should be(MilliwattHours(10.22))
"10.22 kWh".toEnergy.get should be(KilowattHours(10.22))
"10.22 MWh".toEnergy.get should be(MegawattHours(10.22))
"10.22 GWh".toEnergy.get should be(GigawattHours(10.22))
Expand Down

0 comments on commit 1545e0b

Please sign in to comment.