Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static Airspeed<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new Airspeed from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new Airspeed instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static Airspeed<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new Airspeed from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static FlowSpeed<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new FlowSpeed from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new FlowSpeed instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static FlowSpeed<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new FlowSpeed from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static GroundSpeed<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new GroundSpeed from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new GroundSpeed instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static GroundSpeed<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new GroundSpeed from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static GroupVelocity<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new GroupVelocity from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new GroupVelocity instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static GroupVelocity<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new GroupVelocity from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static PhaseVelocity<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new PhaseVelocity from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new PhaseVelocity instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static PhaseVelocity<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new PhaseVelocity from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static SoundSpeed<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new SoundSpeed from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new SoundSpeed instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static SoundSpeed<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new SoundSpeed from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static Speed<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new <see cref="Speed{T}"/> from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new <see cref="Speed{T}"/> instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static Speed<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new <see cref="Speed{T}"/> from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ namespace ktsu.Semantics.Quantities;
/// <returns>A new <see cref="Velocity1D{T}"/> instance.</returns>
public static Velocity1D<T> FromMeterPerSecond(T value) => Create(value);

/// <summary>
/// Creates a new <see cref="Velocity1D{T}"/> from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new <see cref="Velocity1D{T}"/> instance.</returns>
public static Velocity1D<T> FromKilometerPerSecond(T value) => Create((value * MetricMagnitudes.Values<T>.Kilo));

/// <summary>
/// Creates a new <see cref="Velocity1D{T}"/> from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace ktsu.Semantics.Quantities;
/// <returns>A new <see cref="Velocity2D{T}"/> storing the SI-base equivalent.</returns>
public static Velocity2D<T> FromMeterPerSecond(T x, T y) => new() { X = x, Y = y };

/// <summary>Creates a <see cref="Velocity2D{T}"/> from components in KilometerPerSecond.</summary>
/// <param name="x">The X component, in KilometerPerSecond.</param>
/// <param name="y">The Y component, in KilometerPerSecond.</param>
/// <returns>A new <see cref="Velocity2D{T}"/> storing the SI-base equivalent.</returns>
public static Velocity2D<T> FromKilometerPerSecond(T x, T y) => new() { X = (x * MetricMagnitudes.Values<T>.Kilo), Y = (y * MetricMagnitudes.Values<T>.Kilo) };

/// <summary>Creates a <see cref="Velocity2D{T}"/> from components in KilometerPerHour.</summary>
/// <param name="x">The X component, in KilometerPerHour.</param>
/// <param name="y">The Y component, in KilometerPerHour.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ namespace ktsu.Semantics.Quantities;
/// <returns>A new <see cref="Velocity3D{T}"/> storing the SI-base equivalent.</returns>
public static Velocity3D<T> FromMeterPerSecond(T x, T y, T z) => new() { X = x, Y = y, Z = z };

/// <summary>Creates a <see cref="Velocity3D{T}"/> from components in KilometerPerSecond.</summary>
/// <param name="x">The X component, in KilometerPerSecond.</param>
/// <param name="y">The Y component, in KilometerPerSecond.</param>
/// <param name="z">The Z component, in KilometerPerSecond.</param>
/// <returns>A new <see cref="Velocity3D{T}"/> storing the SI-base equivalent.</returns>
public static Velocity3D<T> FromKilometerPerSecond(T x, T y, T z) => new() { X = (x * MetricMagnitudes.Values<T>.Kilo), Y = (y * MetricMagnitudes.Values<T>.Kilo), Z = (z * MetricMagnitudes.Values<T>.Kilo) };

/// <summary>Creates a <see cref="Velocity3D{T}"/> from components in KilometerPerHour.</summary>
/// <param name="x">The X component, in KilometerPerHour.</param>
/// <param name="y">The Y component, in KilometerPerHour.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ namespace ktsu.Semantics.Quantities;
/// <returns>A new <see cref="Velocity4D{T}"/> storing the SI-base equivalent.</returns>
public static Velocity4D<T> FromMeterPerSecond(T x, T y, T z, T w) => new() { X = x, Y = y, Z = z, W = w };

/// <summary>Creates a <see cref="Velocity4D{T}"/> from components in KilometerPerSecond.</summary>
/// <param name="x">The X component, in KilometerPerSecond.</param>
/// <param name="y">The Y component, in KilometerPerSecond.</param>
/// <param name="z">The Z component, in KilometerPerSecond.</param>
/// <param name="w">The W component, in KilometerPerSecond.</param>
/// <returns>A new <see cref="Velocity4D{T}"/> storing the SI-base equivalent.</returns>
public static Velocity4D<T> FromKilometerPerSecond(T x, T y, T z, T w) => new() { X = (x * MetricMagnitudes.Values<T>.Kilo), Y = (y * MetricMagnitudes.Values<T>.Kilo), Z = (z * MetricMagnitudes.Values<T>.Kilo), W = (w * MetricMagnitudes.Values<T>.Kilo) };

/// <summary>Creates a <see cref="Velocity4D{T}"/> from components in KilometerPerHour.</summary>
/// <param name="x">The X component, in KilometerPerHour.</param>
/// <param name="y">The Y component, in KilometerPerHour.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ namespace ktsu.Semantics.Quantities;
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static WindSpeed<T> FromMeterPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative(value, nameof(value)));

/// <summary>
/// Creates a new WindSpeed from a value in KilometerPerSecond.
/// </summary>
/// <param name="value">The value in KilometerPerSecond.</param>
/// <returns>A new WindSpeed instance.</returns>
/// <exception cref="System.ArgumentException">Thrown when the resulting magnitude would be negative.</exception>
public static WindSpeed<T> FromKilometerPerSecond(T value) => Create(Vector0Guards.EnsureNonNegative((value * MetricMagnitudes.Values<T>.Kilo), nameof(value)));

/// <summary>
/// Creates a new WindSpeed from a value in KilometerPerHour.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ namespace ktsu.Semantics.Quantities;
/// <returns>A new <see cref="WindVelocity3D{T}"/> storing the SI-base equivalent.</returns>
public static WindVelocity3D<T> FromMeterPerSecond(T x, T y, T z) => new() { X = x, Y = y, Z = z };

/// <summary>Creates a <see cref="WindVelocity3D{T}"/> from components in KilometerPerSecond.</summary>
/// <param name="x">The X component, in KilometerPerSecond.</param>
/// <param name="y">The Y component, in KilometerPerSecond.</param>
/// <param name="z">The Z component, in KilometerPerSecond.</param>
/// <returns>A new <see cref="WindVelocity3D{T}"/> storing the SI-base equivalent.</returns>
public static WindVelocity3D<T> FromKilometerPerSecond(T x, T y, T z) => new() { X = (x * MetricMagnitudes.Values<T>.Kilo), Y = (y * MetricMagnitudes.Values<T>.Kilo), Z = (z * MetricMagnitudes.Values<T>.Kilo) };

/// <summary>Creates a <see cref="WindVelocity3D{T}"/> from components in KilometerPerHour.</summary>
/// <param name="x">The X component, in KilometerPerHour.</param>
/// <param name="y">The Y component, in KilometerPerHour.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,39 @@ public KilometerPerHour() { }
T IUnit.ToBaseOffsetAs<T>() => T.Zero;
}

/// <summary>
/// Kilometers per second - 1000 meters per second; the unit orbital state vectors are quoted in.
/// </summary>
public sealed record KilometerPerSecond : IUnit, IVelocityUnit
{
/// <summary>Gets the full name of the unit.</summary>
public string Name => "KilometerPerSecond";

/// <summary>Gets the symbol/abbreviation of the unit.</summary>
public string Symbol => "km/s";

/// <summary>Gets the unit system this unit belongs to.</summary>
public UnitSystem System => UnitSystem.SIDerived;

/// <summary>Gets the physical dimension this unit measures.</summary>
public DimensionInfo Dimension => PhysicalDimensions.Velocity;

/// <summary>Gets the multiplication factor used in the to-base affine conversion.</summary>
public double ToBaseFactor => MetricMagnitudes.Kilo;

/// <summary>Gets the additive offset used in the to-base affine conversion.</summary>
public double ToBaseOffset => 0d;

/// <summary>Initializes a new instance of the unit.</summary>
public KilometerPerSecond() { }

/// <inheritdoc/>
T IUnit.ToBaseFactorAs<T>() => MetricMagnitudes.Values<T>.Kilo;

/// <inheritdoc/>
T IUnit.ToBaseOffsetAs<T>() => T.Zero;
}

/// <summary>
/// Miles per hour - Imperial unit of velocity.
/// </summary>
Expand Down Expand Up @@ -6594,6 +6627,9 @@ public static class Units
/// <summary>Singleton <c>KilometerPerHour</c> instance.</summary>
public static readonly KilometerPerHour KilometerPerHour = new KilometerPerHour();

/// <summary>Singleton <c>KilometerPerSecond</c> instance.</summary>
public static readonly KilometerPerSecond KilometerPerSecond = new KilometerPerSecond();

/// <summary>Singleton <c>Kilomole</c> instance.</summary>
public static readonly Kilomole Kilomole = new Kilomole();

Expand Down
2 changes: 1 addition & 1 deletion Semantics.SourceGenerators/Metadata/dimensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
"symbol": "L T⁻¹",
"dimensionalFormula": { "length": 1, "time": -1 },
"availableUnits": [
"MeterPerSecond", "KilometerPerHour", "MilePerHour", "FootPerSecond", "Knot"
"MeterPerSecond", "KilometerPerSecond", "KilometerPerHour", "MilePerHour", "FootPerSecond", "Knot"
],
"quantities": {
"vector0": {
Expand Down
7 changes: 7 additions & 0 deletions Semantics.SourceGenerators/Metadata/units.json
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,13 @@
"system": "SIDerived",
"conversionFactor": "KilometerPerHourToMeterPerSecond"
},
{
"name": "KilometerPerSecond",
"symbol": "km/s",
"description": "Kilometers per second - 1000 meters per second; the unit orbital state vectors are quoted in.",
"system": "SIDerived",
"magnitude": "Kilo"
},
{
"name": "MilePerHour",
"symbol": "mph",
Expand Down
36 changes: 36 additions & 0 deletions Semantics.Test/Quantities/StorageConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,42 @@ public void KilometersPerHourConvertAndRoundTrip()
AssertValue("10", speed.In(Units.MeterPerSecond), terminates: false);
}

[TestMethod]
public void AKilometerPerSecondIsExactlyAThousandMetersPerSecond()
{
Speed<T> speed = Speed<T>.FromKilometerPerSecond(Of("7.66"));

AssertValue("7660", speed.Value, terminates: true);
AssertValue("7.66", speed.In(Units.KilometerPerSecond), terminates: true);

// Against a neighbour defined by a different mechanism: kilometers per hour carries the
// repeating factor 5/18, so agreeing with it at 3600 to 1 ties the new unit to something
// other than its own definition. Non-terminating because that factor is.
AssertValue("3600", Speed<T>.FromKilometerPerSecond(T.One).In(Units.KilometerPerHour), terminates: false);
}

/// <summary>
/// The whole of what <c>KilometerPerSecond</c> is for: an orbital state is published in
/// kilometers and kilometers per second together, and until this unit existed the second half
/// had to be multiplied by a thousand at the call site — in a library whose point is that units
/// are not spelled by hand.
/// </summary>
[TestMethod]
public void AStateVectorIsBuiltFromItsPublishedUnitsWithNoArithmeticAtTheCallSite()
{
Position3D<T> position = Position3D<T>.FromKilometer(Of("6778"), Of("-1.5"), T.Zero);
Velocity3D<T> velocity = Velocity3D<T>.FromKilometerPerSecond(Of("0.5"), Of("7.5"), Of("-1.25"));

AssertValue("6778000", position.X, terminates: true);
AssertValue("500", velocity.X, terminates: true);
AssertValue("7500", velocity.Y, terminates: true);
AssertValue("-1250", velocity.Z, terminates: true);

// Componentwise, and signed, exactly as the position factory beside it is — the vector
// forms carry no Vector0 guard, so a retrograde component is ordinary.
Assert.AreEqual(Speed<T>.FromKilometerPerSecond(Of("7.5")).Value, velocity.Y);
}

[TestMethod]
public void AKnotIsTheRepeatingFractionOfAMeterPerSecond()
{
Expand Down
Loading