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

Pressure units #210

Merged
merged 3 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 28 additions & 12 deletions shared/src/main/scala/squants/motion/Pressure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ final class Pressure private (val value: Double, val unit: PressureUnit)
def *(that: Area): Force = Newtons(toPascals * that.toSquareMeters)
def *(that: Time) = ??? // returns DynamicViscosity

def toPascals = to(Pascals)
def toBars = to(Bars)
def toPoundsPerSquareInch = to(PoundsPerSquareInch)
def toStandardAtmospheres = to(StandardAtmospheres)
def toPascals: Double = to(Pascals)
def toBars: Double = to(Bars)
def toPoundsPerSquareInch: Double = to(PoundsPerSquareInch)
def toStandardAtmospheres: Double = to(StandardAtmospheres)
def toMillimetersOfMercury: Double = to(MillimetersOfMercury)
def toTorr: Double = to(Torrs)
}

object Pressure extends Dimension[Pressure] {
Expand All @@ -42,7 +44,7 @@ object Pressure extends Dimension[Pressure] {
def name = "Pressure"
def primaryUnit = Pascals
def siUnit = Pascals
def units = Set(Pascals, Bars, PoundsPerSquareInch, StandardAtmospheres)
def units = Set(Pascals, Bars, PoundsPerSquareInch, StandardAtmospheres, MillimetersOfMercury, Torrs)
}

trait PressureUnit extends UnitOfMeasure[Pressure] with UnitConverter {
Expand All @@ -68,18 +70,32 @@ object StandardAtmospheres extends PressureUnit {
val conversionFactor = Newtons.conversionFactor * 1.01325e5
}

object MillimetersOfMercury extends PressureUnit {
val symbol = "mmHg"
val conversionFactor = Newtons.conversionFactor * 133.322387415
}

object Torrs extends PressureUnit {
val symbol = "Torr"
val conversionFactor = StandardAtmospheres.conversionFactor / 760d
}

object PressureConversions {
lazy val pascal = Pascals(1)
lazy val bar = Bars(1)
lazy val psi = PoundsPerSquareInch(1)
lazy val atm = StandardAtmospheres(1)
lazy val bar = Bars(1)
lazy val psi = PoundsPerSquareInch(1)
lazy val atm = StandardAtmospheres(1)
lazy val mmHg = MillimetersOfMercury(1)
lazy val torr = Torrs(1)

implicit class PressureConversions[A](n: A)(implicit num: Numeric[A]) {
def pascals = Pascals(n)
def bars = Bars(n)
def psi = PoundsPerSquareInch(n)
def atm = StandardAtmospheres(n)
def bars = Bars(n)
def psi = PoundsPerSquareInch(n)
def atm = StandardAtmospheres(n)
def mmHg = MillimetersOfMercury(n)
def torr = Torrs(n)
}

implicit object PressureNumeric extends AbstractQuantityNumeric[Pressure](Pressure.primaryUnit)
}
}
12 changes: 12 additions & 0 deletions shared/src/test/scala/squants/motion/PressureSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ class PressureSpec extends FlatSpec with Matchers {
Bars(10).toBars should be(10)
PoundsPerSquareInch(1).toPoundsPerSquareInch should be(1)
StandardAtmospheres(1).toStandardAtmospheres should be(1)
MillimetersOfMercury(1).toMillimetersOfMercury should be(1)
Torrs(1).toTorr should be(1)
}

it should "create values from properly formatted Strings" in {
Pressure("10.22 Pa").get should be(Pascals(10.22))
Pressure("10.22 bar").get should be(Bars(10.22))
Pressure("10.22 psi").get should be(PoundsPerSquareInch(10.22))
Pressure("10.22 mmHg").get should be(MillimetersOfMercury(10.22))
Pressure("10.22 Torr").get should be(Torrs(10.22))
Pressure("10.22 zz").failed.get should be(QuantityParseException("Unable to parse Pressure", "10.22 zz"))
Pressure("zz Pa").failed.get should be(QuantityParseException("Unable to parse Pressure", "zz Pa"))
}
Expand All @@ -43,13 +47,17 @@ class PressureSpec extends FlatSpec with Matchers {
x.toBars should be(1e-5)
x.toPoundsPerSquareInch should be(Newtons(1).toPoundForce / SquareMeters(1).toSquareInches +- tolerance)
x.toStandardAtmospheres should be(1d / 101325d)
x.toMillimetersOfMercury should be(1d / 133.322387415d)
x.toTorr should be(760d / 101325d)
}

it should "return properly formatted strings for all supported Units of Measure" in {
Pascals(1).toString(Pascals) should be("1.0 Pa")
Bars(1).toString(Bars) should be("1.0 bar")
PoundsPerSquareInch(1).toString(PoundsPerSquareInch) should be("1.0 psi")
StandardAtmospheres(1).toString(StandardAtmospheres) should be("1.0 atm")
MillimetersOfMercury(1).toString(MillimetersOfMercury) should be("1.0 mmHg")
Torrs(1).toString(Torrs) should be("1.0 Torr")
}

it should "return Force when multiplied by Area" in {
Expand All @@ -65,6 +73,8 @@ class PressureSpec extends FlatSpec with Matchers {
bar should be(Bars(1))
psi should be(PoundsPerSquareInch(1))
atm should be(StandardAtmospheres(1))
mmHg should be(MillimetersOfMercury(1))
torr should be(Torrs(1))
}
it should "provide implicit conversion from Double" in {
import PressureConversions._
Expand All @@ -74,6 +84,8 @@ class PressureSpec extends FlatSpec with Matchers {
d.bars should be(Bars(d))
d.psi should be(PoundsPerSquareInch(d))
d.atm should be(StandardAtmospheres(d))
d.mmHg should be(MillimetersOfMercury(d))
d.torr should be(Torrs(d))
}

it should "provide Numeric support" in {
Expand Down