Skip to content

Commit

Permalink
fix(energy): Add base energy units
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-p-may committed Jun 14, 2023
1 parent 81607f7 commit 642469b
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 4 deletions.
104 changes: 100 additions & 4 deletions ph_units/unit_types/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,97 @@
from ph_units.unit_types._base import Base_UnitType


class WattHoursPerMeterCubed(Base_UnitType):
"""WH/M3"""
class WattHour(Base_UnitType):
"""Wh"""

__symbol__ = "WH/M3"
__symbol__ = "WH"
__aliases__ = []
__factors__ = {"WH/M3": "{}*1", "W/CFM": "{}*1.699010796"}
__factors__ = {
"WH": "{}*1",
"KWH": "{}*0.001",
"BTU": "{}*3.41214",
"KBTU": "{}*0.00341214",
"MJ": "{}*0.0036",
"KJ": "{}*3.6",
}


class KiloWattHour(Base_UnitType):
"""KWH"""

__symbol__ = "KWH"
__aliases__ = []
__factors__ = {
"WH": "{}*1000",
"KWH": "{}*1",
"BTU": "{}*3412.14",
"KBTU": "{}*3.41214",
"MJ": "{}*3.6",
"KJ": "{}*3600",
}


class BTU(Base_UnitType):
"""BTU"""

__symbol__ = "BTU"
__aliases__ = []
__factors__ = {
"WH": "{}*0.293071",
"KWH": "{}*0.000293071",
"BTU": "{}*1",
"KBTU": "{}*0.001",
"MJ": "{}*0.00105506",
"KJ": "{}*1.05506",
}


class KiloBTU(Base_UnitType):
"""KBTU"""

__symbol__ = "KBTU"
__aliases__ = []
__factors__ = {
"WH": "{}*293.071",
"KWH": "{}*0.293071",
"BTU": "{}*1000",
"KBTU": "{}*1",
"MJ": "{}*1.05506",
"KJ": "{}*1055.06",
}


class MegaJoule(Base_UnitType):
"""MJ"""

__symbol__ = "MJ"
__aliases__ = []
__factors__ = {
"WH": "{}*277.778",
"KWH": "{}*0.277778",
"BTU": "{}*947.817",
"KBTU": "{}*0.947817",
"MJ": "{}*1",
"KJ": "{}*1000",
}


class KiloJoule(Base_UnitType):
"""KJ"""

__symbol__ = "KJ"
__aliases__ = []
__factors__ = {
"WH": "{}*0.277778",
"KWH": "{}*0.000277778",
"BTU": "{}*0.947817",
"KBTU": "{}*0.000947817",
"MJ": "{}*0.001",
"KJ": "{}*1",
}


# ----------------- Energy Per Area -----------------


class WattHoursPerKilometerSquared(Base_UnitType):
Expand Down Expand Up @@ -110,6 +195,17 @@ class BtuPerFootSquared(Base_UnitType):
}


# ----------------- Energy Per Volume -----------------


class WattHoursPerMeterCubed(Base_UnitType):
"""WH/M3"""

__symbol__ = "WH/M3"
__aliases__ = []
__factors__ = {"WH/M3": "{}*1", "W/CFM": "{}*1.699010796"}


class MegaJoulePerMeterCubedKelvin(Base_UnitType):
"""MJ/M3K"""

Expand Down
54 changes: 54 additions & 0 deletions tests/test_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,57 @@ def test_Btu_foot_squared():
assert convert(1, "BTU/FT2", "KWH/FT2") == 0.000293071
assert convert(1, "BTU/FT2", "BTU/FT2") == 1
assert convert(1, "BTU/FT2", "KBTU/FT2") == 0.001


def test_wh():
assert convert(1, "WH", "WH") == 1
assert convert(1, "WH", "BTU") == pytest.approx(3.41214)
assert convert(1, "WH", "KWH") == 0.001
assert convert(1, "WH", "KBTU") == pytest.approx(0.003412141633)
assert convert(1, "WH", "MJ") == pytest.approx(0.0036)
assert convert(1, "WH", "KJ") == pytest.approx(3.6)


def test_KWH():
assert convert(1, "KWH", "WH") == 1000
assert convert(1, "KWH", "BTU") == pytest.approx(3412.141633)
assert convert(1, "KWH", "KWH") == 1
assert convert(1, "KWH", "KBTU") == pytest.approx(3.412141633)
assert convert(1, "KWH", "MJ") == pytest.approx(3.6)
assert convert(1, "KWH", "KJ") == pytest.approx(3600)


def test_BTU():
assert convert(1, "BTU", "WH") == pytest.approx(0.293071)
assert convert(1, "BTU", "BTU") == 1
assert convert(1, "BTU", "KWH") == pytest.approx(0.000293071)
assert convert(1, "BTU", "KBTU") == pytest.approx(0.001)
assert convert(1, "BTU", "MJ") == pytest.approx(0.00105506)
assert convert(1, "BTU", "KJ") == pytest.approx(1.05506)


def test_KBTU():
assert convert(1, "KBTU", "WH") == pytest.approx(293.071)
assert convert(1, "KBTU", "BTU") == 1000
assert convert(1, "KBTU", "KWH") == pytest.approx(0.293071)
assert convert(1, "KBTU", "KBTU") == 1
assert convert(1, "KBTU", "MJ") == pytest.approx(1.05506)
assert convert(1, "KBTU", "KJ") == pytest.approx(1055.06)


def test_MegaJoule():
assert convert(1, "MJ", "WH") == pytest.approx(277.778)
assert convert(1, "MJ", "BTU") == pytest.approx(947.817)
assert convert(1, "MJ", "KWH") == pytest.approx(0.277778)
assert convert(1, "MJ", "KBTU") == pytest.approx(0.947817)
assert convert(1, "MJ", "MJ") == 1
assert convert(1, "MJ", "KJ") == pytest.approx(1000)


def test_KiloJoule():
assert convert(1, "KJ", "WH") == pytest.approx(0.277778)
assert convert(1, "KJ", "BTU") == pytest.approx(0.947817)
assert convert(1, "KJ", "KWH") == pytest.approx(0.000277778)
assert convert(1, "KJ", "KBTU") == pytest.approx(0.000947817)
assert convert(1, "KJ", "MJ") == pytest.approx(0.001)
assert convert(1, "KJ", "KJ") == 1

0 comments on commit 642469b

Please sign in to comment.