This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos Quiroz <carlos.m.quiroz@gmail.com>
- Loading branch information
Showing
6 changed files
with
156 additions
and
25 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
modules/model-tests/shared/src/test/scala/gem/RadialVelocitySpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package gem | ||
|
||
import cats.tests.CatsSuite | ||
import cats.kernel.laws.discipline._ | ||
|
||
import coulomb._ | ||
import coulomb.si._ | ||
import coulomb.siprefix._ | ||
import gem.arb._ | ||
|
||
final class RadialVelocitySpec extends CatsSuite { | ||
import ArbRadialVelocity._ | ||
|
||
// Laws | ||
checkAll("RadialVelocity", EqTests[RadialVelocity].eqv) | ||
checkAll("RadialVelocityOrder", OrderTests[RadialVelocity].order) | ||
|
||
test("toRedshift") { | ||
assert( | ||
// Note the speed is given in Meter per second but coulomb will convert | ||
RadialVelocity(0.withUnit[Meter %/ Second]) | ||
.flatMap(_.toRedshift) | ||
.exists(_ === Redshift.Zero) | ||
) | ||
assert(RadialVelocity.CRadialVelocity.toRedshift.isEmpty) | ||
assert( | ||
RadialVelocity(1000.withUnit[(Kilo %* Meter) %/ Second]) | ||
.flatMap(_.toRedshift) | ||
.exists(_ === Redshift(0.003341222805847144)) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
modules/model/shared/src/main/scala/gem/RadialVelocity.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package gem | ||
|
||
import cats._ | ||
import coulomb._ | ||
import coulomb.si._ | ||
import coulomb.siprefix._ | ||
import spire.std.bigDecimal._ | ||
|
||
/** | ||
* Representation of a radial velocity in kilomoters per second | ||
* Valid range is ]-C, C[ where C is the speed of light | ||
* This is often represented as RV | ||
*/ | ||
final case class RadialVelocity private (rv: RadialVelocity.RVQuantity) { | ||
|
||
/** | ||
* Converts the radial velocity to a Redshift, approximate | ||
* a return value of None should be understood as an infinity Redshift | ||
*/ | ||
def toRedshift: Option[Redshift] = | ||
// Though we forbid constructing an RV with value C there is at least one instance with that value | ||
if (rv.value.abs < RadialVelocity.CValue) { | ||
val i = (rv / RadialVelocity.C).value | ||
val t = (1 + i) / (1 - i) | ||
Some(Redshift(BigDecimal.decimal(scala.math.sqrt(t.toDouble) - 1).round(rv.value.mc))) | ||
} else None | ||
} | ||
|
||
object RadialVelocity { | ||
type RVUnit = (Kilo %* Meter) %/ Second | ||
type RVQuantity = Quantity[BigDecimal, RVUnit] | ||
|
||
val CValue: BigDecimal = BigDecimal.decimal(299792.458) // Use the default math context | ||
|
||
val C: RVQuantity = | ||
CValue.withUnit[RVUnit] // Speed of light in km/s | ||
|
||
val CRadialVelocity: RadialVelocity = new RadialVelocity(C) | ||
|
||
def apply(rv: RadialVelocity.RVQuantity): Option[RadialVelocity] = | ||
if (rv.value.abs < CValue) Some(new RadialVelocity(rv)) else None | ||
|
||
def unsafeFromRVQuantity(rv: RadialVelocity.RVQuantity): RadialVelocity = | ||
apply(rv).getOrElse(sys.error(s"Value of rv $rv not allowed")) | ||
|
||
/** | ||
* `No RadialVelocity` | ||
* @group Constructors | ||
*/ | ||
val Zero: RadialVelocity = new RadialVelocity(0.withUnit[RVUnit]) | ||
|
||
/** @group Typeclass Instances */ | ||
implicit val order: Order[RadialVelocity] = | ||
Order.by(_.rv.value) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
modules/testkit/shared/src/main/scala/gem/arb/ArbRadialVelocity.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2016-2020 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package gem | ||
package arb | ||
|
||
import coulomb._ | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Gen | ||
import org.scalacheck.Cogen | ||
|
||
trait ArbRadialVelocity { | ||
|
||
implicit val arbRedshift: Arbitrary[RadialVelocity] = | ||
Arbitrary { | ||
for { | ||
rv <- | ||
Gen.chooseNum(-1 * RadialVelocity.CValue.toDouble + 1, RadialVelocity.CValue.toDouble - 1) | ||
} yield RadialVelocity.unsafeFromRVQuantity(rv.withUnit[RadialVelocity.RVUnit]) | ||
} | ||
|
||
implicit val cogRedshift: Cogen[RadialVelocity] = | ||
Cogen[BigDecimal].contramap(_.rv.value) | ||
} | ||
|
||
object ArbRadialVelocity extends ArbRadialVelocity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters