Skip to content

Commit

Permalink
add MolarEnergy, implement Energy unimplemented method
Browse files Browse the repository at this point in the history
  • Loading branch information
nvinuesa committed Jun 25, 2017
1 parent 258d2b7 commit c6a9637
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 8 deletions.
2 changes: 1 addition & 1 deletion shared/src/main/scala/squants/energy/Energy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class Energy private (val value: Double, val unit: EnergyUnit)
def /(that: Temperature): ThermalCapacity = JoulesPerKelvin(this.toJoules / that.toKelvinDegrees)
def /(that: ThermalCapacity) = Kelvin(this.toJoules / that.toJoulesPerKelvin)

def /(that: ChemicalAmount) = ??? // return MolarEnergy
def /(that: ChemicalAmount): MolarEnergy = JoulesPerMol(this.toJoules / that.toMoles)
def /(that: Angle): Torque = NewtonMeters(toJoules / that.toRadians)
def /(that: Area) = ??? // Insolation, Energy Area Density

Expand Down
48 changes: 48 additions & 0 deletions shared/src/main/scala/squants/energy/MolarEnergy.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package squants.energy

import squants.mass.{ChemicalAmount, Moles}
import squants.{AbstractQuantityNumeric, Dimension, PrimaryUnit, Quantity, SiUnit, UnitConverter, UnitOfMeasure}

/**
*
* @author Nicolas Vinuesa
* @since 1.4
*
* @param value Double
*/
final class MolarEnergy private (val value: Double, val unit: MolarEnergyUnit)
extends Quantity[MolarEnergy] {

def dimension = MolarEnergy

def *(that: ChemicalAmount): Energy = Joules(this.toJoulesPerMol * that.toMoles)

def toJoulesPerMol = to(JoulesPerMol)
}

object MolarEnergy extends Dimension[MolarEnergy] {
private[energy] def apply[A](n: A, unit: MolarEnergyUnit)(implicit num: Numeric[A]) = new MolarEnergy(num.toDouble(n), unit)
def apply = parse _
def name = "MolarEnergy"
def primaryUnit = JoulesPerMol
def siUnit = JoulesPerMol
def units = Set(JoulesPerMol)
}

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

object JoulesPerMol extends MolarEnergyUnit with PrimaryUnit with SiUnit {
val symbol = Joules.symbol + "/" + Moles.symbol
}

object MolarEnergyConversions {
lazy val joulePerMol = JoulesPerMol(1)

implicit class MolarEnergyConversions[A](n: A)(implicit num: Numeric[A]) {
def joulesPerMol = JoulesPerMol(n)
}

implicit object MolarEnergyNumeric extends AbstractQuantityNumeric[MolarEnergy](MolarEnergy.primaryUnit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ object ImplicitDimensions {
object energy {
implicit val implicitEnergy: Dimension[Energy] = Energy
implicit val implicitEnergyDensity: Dimension[EnergyDensity] = EnergyDensity
implicit val implicitMolarEnergy: Dimension[MolarEnergy] = MolarEnergy
implicit val implicitPower: Dimension[Power] = Power
implicit val implicitPowerRamp: Dimension[PowerRamp] = PowerRamp
implicit val implicitSpecificEnergy: Dimension[SpecificEnergy] = SpecificEnergy
Expand Down
18 changes: 11 additions & 7 deletions shared/src/test/scala/squants/energy/EnergySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

package squants.energy

import org.scalatest.{ FlatSpec, Matchers }
import squants.electro.{ Coulombs, Volts }
import squants.mass.Kilograms
import squants.motion.{ MetersPerSecond, NewtonSeconds, Newtons }
import squants.space.{ CubicMeters, Meters }
import squants.thermal.{ JoulesPerKelvin, Kelvin }
import org.scalatest.{FlatSpec, Matchers}
import squants.electro.{Coulombs, Volts}
import squants.mass.{Kilograms, Moles}
import squants.motion.{MetersPerSecond, NewtonSeconds, Newtons}
import squants.space.{CubicMeters, Meters}
import squants.thermal.{JoulesPerKelvin, Kelvin}
import squants.time.Hours
import squants.{ MetricSystem, QuantityParseException }
import squants.{MetricSystem, QuantityParseException}

/**
* @author garyKeorkunian
Expand Down Expand Up @@ -147,6 +147,10 @@ class EnergySpec extends FlatSpec with Matchers {
Joules(10) / Kelvin(2) should be(JoulesPerKelvin(5))
}

it should "return MolarEnergy when divided by ChemicalAmount" in {
Joules(10) / Moles(2) should be(JoulesPerMol(5))
}

behavior of "KineticEnergyCalculations"

it should "calculate Kinetic Energy from Mass and Velocity" in {
Expand Down
60 changes: 60 additions & 0 deletions shared/src/test/scala/squants/energy/MolarEnergySpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package squants.energy

import org.scalatest.{FlatSpec, Matchers}
import squants.QuantityParseException
import squants.mass.Moles

/**
* @author Nicolas Vinuesa
* @since 1.4
*
*/
class MolarEnergySpec extends FlatSpec with Matchers {

behavior of "MolarEnergy and its Units of Measure"

it should "create values using UOM factories" in {
JoulesPerMol(1).toJoulesPerMol should be(1)
}

it should "create values from properly formatted Strings" in {
MolarEnergy("10.22 J/mol").get should be(JoulesPerMol(10.22))
MolarEnergy("10.22 zz").failed.get should be(QuantityParseException("Unable to parse MolarEnergy", "10.22 zz"))
MolarEnergy("zz J/mol").failed.get should be(QuantityParseException("Unable to parse MolarEnergy", "zz J/mol"))
}

it should "properly convert to all supported Units of Measure" in {
val x = JoulesPerMol(10)
x.toJoulesPerMol should be(10.0)
}

it should "return properly formatted strings for all supported Units of Measure" in {
JoulesPerMol(1).toString(JoulesPerMol) should be("1.0 J/mol")
}

it should "return Energy when multiplied by ChemicalAmount" in {
JoulesPerMol(1) * Moles(1) should be(Joules(1))
}

behavior of "MolarEnergyConversions"

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

joulePerMol should be(JoulesPerMol(1))
}

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

val d = 10.22
d.joulesPerMol should be(JoulesPerMol(d))
}

it should "provide Numeric support" in {
import MolarEnergyConversions.MolarEnergyNumeric

val rs = List(JoulesPerMol(100), JoulesPerMol(10))
rs.sum should be(JoulesPerMol(110))
}
}

0 comments on commit c6a9637

Please sign in to comment.