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

Permeability #271

Merged
merged 6 commits into from
Jul 8, 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
2 changes: 1 addition & 1 deletion shared/src/main/scala/squants/electro/Inductance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Inductance private (val value: Double, val unit: InductanceUnit)
def dimension = Inductance

def *(that: ElectricCurrent): MagneticFlux = Webers(this.toHenry * that.toAmperes)
def /(that: Length) = ??? // returns Permeability
def /(that: Length): Permeability = HenriesPerMeter(this.toHenry / that.toMeters)

def toHenry = to(Henry)
def toMillihenry = to(Millihenry)
Expand Down
66 changes: 66 additions & 0 deletions shared/src/main/scala/squants/electro/Permeability.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* *\
** Squants **
** **
** Scala Quantities and Units of Measure Library and DSL **
** (c) 2015-2017, Carlos Quiroz **
** **
\* */

package squants.electro

import squants._
import squants.motion.Newtons

/**
* Quantity and units for Permeability
*
* https://en.wikipedia.org/wiki/Permeability_(electromagnetism)
*
* @author cquiroz
* @since 1.4
*
* @param value value in [[squants.electro.HenriesPerMeter]]
*/
final class Permeability private (val value: Double, val unit: PermeabilityUnit)
extends Quantity[Permeability] {

val dimension = Permeability

def *(that: Length): Inductance = Henry(this.toHenriesPerMeter * that.toMeters)

def toHenriesPerMeter = to(HenriesPerMeter)
def toNewtonsPerAmpereSquared = to(NewtonsPerAmperesSquared)
}

object Permeability extends Dimension[Permeability] {
private[electro] def apply[A](n: A, unit: PermeabilityUnit)(implicit num: Numeric[A]) = new Permeability(num.toDouble(n), unit)
val apply = parse _
val name = "Permeability"
val primaryUnit = HenriesPerMeter
val siUnit = HenriesPerMeter
val units = Set[UnitOfMeasure[Permeability]](HenriesPerMeter, NewtonsPerAmperesSquared)
}

trait PermeabilityUnit extends UnitOfMeasure[Permeability] with UnitConverter {
def apply[A](n: A)(implicit num: Numeric[A]) = Permeability(n, this)
}

object HenriesPerMeter extends PermeabilityUnit with PrimaryUnit with SiUnit {
val symbol = s"${Henry.symbol}/${Meters.symbol}"
}

object NewtonsPerAmperesSquared extends PermeabilityUnit with PrimaryUnit with SiUnit {
val symbol = s"${Newtons.symbol}/${Amperes.symbol}²"
}

object PermeabilityConversions {
lazy val henriesPerMeter = HenriesPerMeter(1)
lazy val newtonsPerAmperesSquared = NewtonsPerAmperesSquared(1)

implicit class PermeabilityConversions[A](n: A)(implicit num: Numeric[A]) {
def henriesPerMeter = HenriesPerMeter(n)
def newtonsPerAmperesSquared = NewtonsPerAmperesSquared(n)
}

implicit object PermeabilityNumeric extends AbstractQuantityNumeric[Permeability](Permeability.primaryUnit)
}
6 changes: 5 additions & 1 deletion shared/src/test/scala/squants/electro/InductanceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package squants.electro

import org.scalatest.{FlatSpec, Matchers}
import squants.{MetricSystem, QuantityParseException}

import squants.space.Meters
/**
* @author garyKeorkunian
* @since 0.1
Expand Down Expand Up @@ -59,6 +59,10 @@ class InductanceSpec extends FlatSpec with Matchers {
Henry(1) * Amperes(1) should be(Webers(1))
}

it should "return Permeability when divided by Length" in {
Henry(1) / Meters(1) should be(HenriesPerMeter(1))
}

behavior of "InductanceConversions"

it should "provide aliases for single unit values" in {
Expand Down
77 changes: 77 additions & 0 deletions shared/src/test/scala/squants/electro/PermeabilitySpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* *\
** Squants **
** **
** Scala Quantities and Units of Measure Library and DSL **
** (c) 2015-2017, Carlos Quiroz **
** **
\* */

package squants.electro

import org.scalatest.{FlatSpec, Matchers}
import squants.{MetricSystem, QuantityParseException}
import squants.space.Meters

/**
* @author cquiroz
* @since 1.4
*
*/
class PermeabilitySpec extends FlatSpec with Matchers {

behavior of "Permeability and its Units of Measure"

it should "create values using UOM factories" in {
HenriesPerMeter(1).toHenriesPerMeter should be(1)
NewtonsPerAmperesSquared(1).toNewtonsPerAmpereSquared should be(1)
}

it should "create values from properly formatted Strings" in {
Permeability("10.22 H/m").get should be(HenriesPerMeter(10.22))
Permeability("10.22 N/A²").get should be(HenriesPerMeter(10.22))
Permeability("10.22 zz").failed.get should be(QuantityParseException("Unable to parse Permeability", "10.22 zz"))
Permeability("zz H").failed.get should be(QuantityParseException("Unable to parse Permeability", "zz H"))
}

it should "properly convert to all supported Units of Measure" in {
val x = HenriesPerMeter(1)
x.toHenriesPerMeter should be(1.0)
x.toNewtonsPerAmpereSquared should be(1.0)
val y = NewtonsPerAmperesSquared(1)
y.toHenriesPerMeter should be(1.0)
y.toNewtonsPerAmpereSquared should be(1.0)
}

it should "return properly formatted strings for all supported Units of Measure" in {
HenriesPerMeter(1).toString(HenriesPerMeter) should be("1.0 H/m")
NewtonsPerAmperesSquared(1).toString(NewtonsPerAmperesSquared) should be("1.0 N/A²")
}

it should "return Inductance when multiplied by Distance" in {
HenriesPerMeter(1) * Meters(1) should be(Henry(1))
}

behavior of "PermeabilityConversions"

it should "provide aliases for single unit values" in {
import PermeabilityConversions._

henriesPerMeter should be(HenriesPerMeter(1))
newtonsPerAmperesSquared should be(NewtonsPerAmperesSquared(1))
}

it should "provide implicit conversion from Double" in {
import PermeabilityConversions._

val d = 10d
d.henriesPerMeter should be(HenriesPerMeter(d))
d.newtonsPerAmperesSquared should be(NewtonsPerAmperesSquared(d))
}

it should "provide Numeric support" in {
import PermeabilityConversions.PermeabilityNumeric

val is = List(HenriesPerMeter(100), HenriesPerMeter(10))
is.sum should be(HenriesPerMeter(110))
}
}