diff --git a/VectoCommon/VectoCommon/Utils/SI.cs b/VectoCommon/VectoCommon/Utils/SI.cs index 55e3a1df345fb78a7a384e539678b88b4598e9ae..658a9a8b3244e427321b99adf62ca896b26d114b 100644 --- a/VectoCommon/VectoCommon/Utils/SI.cs +++ b/VectoCommon/VectoCommon/Utils/SI.cs @@ -104,7 +104,9 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private Newton(double val) : base(val, Units) { } - [DebuggerHidden] + public override string UnitString { get { return "N"; } } + + [DebuggerHidden] public static NewtonMeter operator *(Newton newton, Meter meter) { return SIBase<NewtonMeter>.Create(newton.Val * meter.Value()); @@ -224,6 +226,8 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private KilogramPerMeter(double val) : base(val, Units) { } + public override string UnitString { get { return "kg/m"; } } + public static KilogramPerMeterMass operator /(KilogramPerMeter kpm, Kilogram kg) { return SIBase<KilogramPerMeterMass>.Create(kpm.Val / kg.Value()); @@ -244,6 +248,8 @@ namespace TUGraz.VectoCommon.Utils private LiterPerSecond(double val) : base(val, 0.001, Units) { } + public override string UnitString { get { return "l/s"; } } + [DebuggerHidden] public static Liter operator *(LiterPerSecond l, Second second) { @@ -329,7 +335,9 @@ namespace TUGraz.VectoCommon.Utils //[DebuggerHidden] private Liter(double val) : base(val , 0.001, Units) { } - public static Kilogram operator *(Liter liter, KilogramPerCubicMeter kilogramPerCubicMeter) + public override string UnitString { get { return "l"; } } + + public static Kilogram operator *(Liter liter, KilogramPerCubicMeter kilogramPerCubicMeter) { return SIBase<Kilogram>.Create(liter.AsBasicUnit * kilogramPerCubicMeter.Value()); } @@ -345,7 +353,9 @@ namespace TUGraz.VectoCommon.Utils //[DebuggerHidden] private NormLiter(double val) : base(val , 0.001, Units) { } - public static NormLiterPerSecond operator /(NormLiter nl, Second s) + public override string UnitString { get { return "Nl"; } } + + public static NormLiterPerSecond operator /(NormLiter nl, Second s) { return SIBase<NormLiterPerSecond>.Create(nl.Val / s.Value()); } @@ -361,7 +371,9 @@ namespace TUGraz.VectoCommon.Utils //[DebuggerHidden] private NormLiterPerSecond(double val) : base(val, 0.001, Units) { } - public static NormLiter operator *(NormLiterPerSecond nips, Second s) + public override string UnitString { get { return "Nl/s"; } } + + public static NormLiter operator *(NormLiterPerSecond nips, Second s) { return SIBase<NormLiter>.Create(nips.Val * s.Value()); } @@ -387,7 +399,7 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private KilogramPerSecond(double value) : base(value, Units) { } - [DebuggerHidden] + [DebuggerHidden] public static Kilogram operator *(KilogramPerSecond kilogramPerSecond, Second second) { return SIBase<Kilogram>.Create(kilogramPerSecond.Val * second.Value()); @@ -435,7 +447,7 @@ namespace TUGraz.VectoCommon.Utils } /// <summary> - /// SI Class for Kilogram Square Meter [kgm^2]. + /// SI Class for Kilogram per Cubic Meter [kg/m^3]. /// </summary> public class KilogramPerCubicMeter : SIBase<KilogramPerCubicMeter> { @@ -444,7 +456,9 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private KilogramPerCubicMeter(double value) : base(value, Units) { } - [DebuggerHidden] + public override string UnitString { get { return "kg/m^3"; } } + + [DebuggerHidden] public static Kilogram operator *(KilogramPerCubicMeter kilogramPerCubicMeter, CubicMeter cubicMeter) { return SIBase<Kilogram>.Create(kilogramPerCubicMeter.Val * cubicMeter.Value()); @@ -462,7 +476,9 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private KilogramPerWattSecond(double val) : base(val, Units) { } - } + + public override string UnitString { get { return "kg/Ws"; } } + } /// <summary> /// SI Class for watt second [Ws]. @@ -475,7 +491,9 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private WattSecond(double val) : base(val, Units) { } - [DebuggerHidden] + public override string UnitString { get { return "Ws"; } } + + [DebuggerHidden] public static Watt operator /(WattSecond wattSecond, Second second) { return SIBase<Watt>.Create(wattSecond.Val / second.Value()); @@ -492,15 +510,17 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private Watt(double val) : base(val, Units) { } - /// <summary> - /// Implements the operator /. - /// </summary> - /// <param name="watt">The watt.</param> - /// <param name="newtonMeter">The newton meter.</param> - /// <returns> - /// The result of the operator. - /// </returns> - [DebuggerHidden] + public override string UnitString { get { return "W"; } } + + /// <summary> + /// Implements the operator /. + /// </summary> + /// <param name="watt">The watt.</param> + /// <param name="newtonMeter">The newton meter.</param> + /// <returns> + /// The result of the operator. + /// </returns> + [DebuggerHidden] public static PerSecond operator /(Watt watt, NewtonMeter newtonMeter) { return SIBase<PerSecond>.Create(watt.Val / newtonMeter.Value()); @@ -550,6 +570,8 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private Joule(double val) : base(val, Units) { } + public override string UnitString { get { return "J"; } } + public static implicit operator Joule(WattSecond self) { return Create(self.Value()); @@ -572,9 +594,7 @@ namespace TUGraz.VectoCommon.Utils } /// <summary> - /// SI Class for Watt [W]. - /// J = Ws - /// W = kgm^2/s^3 + /// SI Class for Joule / kg. /// </summary> public class JoulePerKilogramm : SIBase<JoulePerKilogramm> { @@ -582,7 +602,9 @@ namespace TUGraz.VectoCommon.Utils private JoulePerKilogramm(double val) : base(val, Units) { } - public static Joule operator *(Kilogram kg, JoulePerKilogramm jpg) + public override string UnitString { get { return "J/kg"; } } + + public static Joule operator *(Kilogram kg, JoulePerKilogramm jpg) { return SIBase<Joule>.Create(kg.Value() * jpg.Val); } @@ -599,7 +621,9 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private JoulePerMeter(double val) : base(val, Units) { } - } + + public override string UnitString { get { return "J/m"; } } + } /// <summary> /// SI Class for one per second [1/s]. @@ -706,7 +730,9 @@ namespace TUGraz.VectoCommon.Utils [DebuggerHidden] private NewtonMeter(double val) : base(val, Units) { } - [DebuggerHidden] + public override string UnitString { get { return "Nm"; } } + + [DebuggerHidden] public static Watt operator *(NewtonMeter newtonMeter, PerSecond perSecond) { return SIBase<Watt>.Create(newtonMeter.Val * perSecond.Value()); @@ -757,7 +783,9 @@ namespace TUGraz.VectoCommon.Utils { private static readonly int[] Units = { 1, 2, -1, 0, 0, 0, 0 }; private NewtonMeterSecond(double val) : base(val, Units) { } - } + + public override string UnitString { get { return "Nms"; } } + } /// <summary> /// SI Class for Amperer [A]. @@ -792,7 +820,9 @@ namespace TUGraz.VectoCommon.Utils private static readonly int[] Units = { 1, 2, -2, -1, 0, 0, 0 }; private Volt(double val) : base(val, Units) { } - public static Watt operator *(Volt volt, Ampere ampere) + public override string UnitString { get { return "V"; } } + + public static Watt operator *(Volt volt, Ampere ampere) { return SIBase<Watt>.Create(volt.Val * ampere.Value()); } @@ -808,6 +838,8 @@ namespace TUGraz.VectoCommon.Utils private static readonly int[] Units = { 0, 2, 0, 0, 0, 0, 0 }; private VolumePerMeter(double val) : base(val, Units) { } + public override string UnitString { get { return "m^3/m"; } } + public static VolumePerMeterMass operator /(VolumePerMeter vpm, Kilogram kg) { return SIBase<VolumePerMeterMass>.Create(vpm.Val / kg.Value()); @@ -825,6 +857,8 @@ namespace TUGraz.VectoCommon.Utils private static readonly int[] Units = { -1, 2, 0, 0, 0, 0, 0 }; private VolumePerMeterMass(double val) : base (val, Units) { } + + public override string UnitString { get { return "m^3/kgm"; } } } public class VolumePerMeterVolume : SIBase<VolumePerMeterVolume> @@ -832,6 +866,8 @@ namespace TUGraz.VectoCommon.Utils private static readonly int[] Units = { 0, -1, 0, 0, 0, 0, 0 }; private VolumePerMeterVolume(double val) : base (val, Units) { } + + public override string UnitString { get { return "m^3/kgm^3"; } } } public class KilogramPerMeterCubicMeter : SIBase<KilogramPerMeterCubicMeter> @@ -839,13 +875,18 @@ namespace TUGraz.VectoCommon.Utils private static readonly int[] Units = { 1, -4, 0, 0, 0, 0, 0 }; private KilogramPerMeterCubicMeter(double val) : base(val, Units) { } + + public override string UnitString { get { return "kg/(m m^3)"; } } } + public class KilogramPerMeterMass : SIBase<KilogramPerMeterMass> { private static readonly int[] Units = { 0, -1, 0, 0, 0, 0, 0 }; private KilogramPerMeterMass(double val) : base(val, Units) { } + + public override string UnitString { get { return "kg/(m kg)"; } } } public class SpecificFuelConsumption : SIBase<SpecificFuelConsumption> @@ -1060,7 +1101,7 @@ namespace TUGraz.VectoCommon.Utils /// <remarks> /// Usage: new SI(1.0).Newton.Meter, new SI(2.3).Rounds.Per.Minute /// </remarks> - [DebuggerDisplay("{Val}")] + [DebuggerDisplay("{Val} [{UnitString}]")] public class SI : IComparable { /// <summary> @@ -1232,7 +1273,7 @@ namespace TUGraz.VectoCommon.Utils } } catch (DivideByZeroException ex) { throw new VectoException( - string.Format("Can not compute division by zero ([{0}] / 0[{1}])", si1.GetUnitString(), si2.GetUnitString()), ex); + string.Format("Can not compute division by zero ([{0}] / 0[{1}])", si1.UnitString, si2.UnitString), ex); } var unitArray = SIUtils.CombineUnits(si1._units, SIUtils.MultiplyUnits(si2._units, -1)); @@ -1244,7 +1285,7 @@ namespace TUGraz.VectoCommon.Utils public static SI operator /(SI si1, double d) { if (d.IsEqual(0)) { - throw new VectoException(string.Format("Can not compute division by zero ([{0}] / 0)", si1.GetUnitString()), new DivideByZeroException()); + throw new VectoException(string.Format("Can not compute division by zero ([{0}] / 0)", si1.UnitString), new DivideByZeroException()); } return new SI(si1.Val / d, si1); @@ -1254,7 +1295,7 @@ namespace TUGraz.VectoCommon.Utils public static SI operator /(double d, SI si1) { if (si1.IsEqual(0)) { - throw new VectoException(string.Format("Can not compute division by zero (x / 0[{0}])", si1.GetUnitString()), + throw new VectoException(string.Format("Can not compute division by zero (x / 0[{0}])", si1.UnitString), new DivideByZeroException()); } @@ -1376,11 +1417,11 @@ namespace TUGraz.VectoCommon.Utils /// <summary> /// Returns the Unit Part of the SI Unit Expression. /// </summary> - public string GetUnitString(int[] units = null) + public static string GetUnitString(int[] units = null) { if (units == null) { - units = _units; - } + return ""; + } return Unit.GetUnitString(units); } @@ -1389,13 +1430,18 @@ namespace TUGraz.VectoCommon.Utils return ToString(null); } + public virtual string UnitString + { + get { return GetUnitString(_units); } + } + private string ToString(string format) { if (string.IsNullOrEmpty(format)) { format = "F4"; } - return string.Format(CultureInfo.InvariantCulture, "{0:" + format + "} [{2}]", Val, format, GetUnitString()); + return string.Format(CultureInfo.InvariantCulture, "{0:" + format + "} [{2}]", Val, format, UnitString); } #endregion @@ -1675,7 +1721,7 @@ namespace TUGraz.VectoCommon.Utils if (showUnit.Value) { return (Val * outputFactor.Value).ToString("F" + decimals.Value, CultureInfo.InvariantCulture) + " [" + - GetUnitString() + "]"; + UnitString + "]"; } return (Val * outputFactor.Value).ToString("F" + decimals.Value, CultureInfo.InvariantCulture); diff --git a/VectoCommon/VectoCommon/Utils/SIConvertExtensionMethods.cs b/VectoCommon/VectoCommon/Utils/SIConvertExtensionMethods.cs index b10c15f28ccd4323a7728b3362abc0ed04a717e9..923dc0bfebd89ec8c181ad82225e3a80b1b16ad1 100644 --- a/VectoCommon/VectoCommon/Utils/SIConvertExtensionMethods.cs +++ b/VectoCommon/VectoCommon/Utils/SIConvertExtensionMethods.cs @@ -77,7 +77,7 @@ namespace TUGraz.VectoCommon.Utils public static implicit operator ConvertedSI(SI self) { - return self == null ? null : new ConvertedSI(self.Value(), self.GetUnitString()); + return self == null ? null : new ConvertedSI(self.Value(), self.UnitString); } public override string ToString() diff --git a/VectoCommon/VectoCommon/Utils/SIUtils.cs b/VectoCommon/VectoCommon/Utils/SIUtils.cs index bea72ab8ad91e64f2c15f536d556bb4464b804ae..8516ee8955620793a9109d7560f2d04c270de1d7 100644 --- a/VectoCommon/VectoCommon/Utils/SIUtils.cs +++ b/VectoCommon/VectoCommon/Utils/SIUtils.cs @@ -102,7 +102,9 @@ namespace TUGraz.VectoCommon.Utils } } string result; - + if (numerator == "" && denominator == "") { + return "-"; + } if (numerator == "") { if (denominator == "") { result = "-"; diff --git a/VectoCommon/VectoCommon/Utils/Validation.cs b/VectoCommon/VectoCommon/Utils/Validation.cs index c927e9356815b7f7c669a7469b7038d2385f289e..f5abe7f8a8a383070a4d207ee6fe6d235a6a989c 100644 --- a/VectoCommon/VectoCommon/Utils/Validation.cs +++ b/VectoCommon/VectoCommon/Utils/Validation.cs @@ -395,7 +395,7 @@ namespace TUGraz.VectoCommon.Utils var si = value as SI; if (si != null) { - _unit = si.GetUnitString(); + _unit = si.UnitString; } var validationService = validationContext.GetService(typeof(VectoValidationModeServiceContainer)) as VectoValidationModeServiceContainer; diff --git a/VectoCore/VectoCoreTest/Utils/AssertHelper.cs b/VectoCore/VectoCoreTest/Utils/AssertHelper.cs index e48b4d21a9d75041044350b7f14fa7d8873e5c37..ff45cf7277ab7d3c6c7d80d9c9e9ef046c49cef7 100644 --- a/VectoCore/VectoCoreTest/Utils/AssertHelper.cs +++ b/VectoCore/VectoCoreTest/Utils/AssertHelper.cs @@ -144,7 +144,7 @@ namespace TUGraz.VectoCore.Tests.Utils Assert.AreEqual(expectedVal, actualVal); } else if (propertyType == typeof(SI)) { Assert.AreEqual((expectedVal as SI).Value(), (actualVal as SI).Value()); - Assert.AreEqual((expectedVal as SI).GetUnitString(), (actualVal as SI).GetUnitString()); + Assert.AreEqual((expectedVal as SI).UnitString, (actualVal as SI).UnitString); } else if (expectedVal is IEnumerable<object>) { Assert.IsTrue(actualVal is IList); var expectedEnumerable = (expectedVal as IEnumerable<object>).ToArray(); diff --git a/VectoCore/VectoCoreTest/Utils/SITest.cs b/VectoCore/VectoCoreTest/Utils/SITest.cs index 5d384d4befbf0806074e172337d432214e15ec5f..0f33c29dde7675d1c3565dbd7abd0bc3bc5fb2e2 100644 --- a/VectoCore/VectoCoreTest/Utils/SITest.cs +++ b/VectoCore/VectoCoreTest/Utils/SITest.cs @@ -154,16 +154,18 @@ namespace TUGraz.VectoCore.Tests.Utils var v4 = 100.SI<Watt>(); var d = 700; + v1.ToString(); + Assert.IsTrue(v1 > v2); Assert.IsFalse(v1 < v2); AssertHelper.Exception<VectoException>(() => { var x = v1 < v4; }, - "Operator '<' can only operate on SI Objects with the same unit. Got: 600.0000 [kgm^2/s^2] < 100.0000 [kgm^2/s^3]"); + "Operator '<' can only operate on SI Objects with the same unit. Got: 600.0000 [Nm] < 100.0000 [W]"); AssertHelper.Exception<VectoException>(() => { var x = v1 > v4; }, - "Operator '>' can only operate on SI Objects with the same unit. Got: 600.0000 [kgm^2/s^2] > 100.0000 [kgm^2/s^3]"); + "Operator '>' can only operate on SI Objects with the same unit. Got: 600.0000 [Nm] > 100.0000 [W]"); AssertHelper.Exception<VectoException>(() => { var x = v1 <= v4; }, - "Operator '<=' can only operate on SI Objects with the same unit. Got: 600.0000 [kgm^2/s^2] <= 100.0000 [kgm^2/s^3]"); + "Operator '<=' can only operate on SI Objects with the same unit. Got: 600.0000 [Nm] <= 100.0000 [W]"); AssertHelper.Exception<VectoException>(() => { var x = v1 >= v4; }, - "Operator '>=' can only operate on SI Objects with the same unit. Got: 600.0000 [kgm^2/s^2] >= 100.0000 [kgm^2/s^3]"); + "Operator '>=' can only operate on SI Objects with the same unit. Got: 600.0000 [Nm] >= 100.0000 [W]"); SI si = null; Assert.IsFalse(si > 3); @@ -420,7 +422,7 @@ namespace TUGraz.VectoCore.Tests.Utils Assert.AreEqual("3.0000 [-]", 3.SI().ToOutputFormat(showUnit: true)); Assert.AreEqual("3.5000", 3.5.SI().ToOutputFormat()); Assert.AreEqual("3.5000", 3.5.SI<Newton>().ToOutputFormat()); - Assert.AreEqual("3.50 [kgm/s^2]", 3.5.SI<Newton>().ToOutputFormat(2, showUnit: true)); + Assert.AreEqual("3.50 [N]", 3.5.SI<Newton>().ToOutputFormat(2, showUnit: true)); Assert.AreEqual("18.00 [m/s]", 5.SI<MeterPerSecond>().ToOutputFormat(2, 3.6, true)); Assert.AreEqual("18.0000", 5.SI<MeterPerSecond>().ToOutputFormat(outputFactor: 3.6)); @@ -529,16 +531,16 @@ namespace TUGraz.VectoCore.Tests.Utils public void SI_NewTests() { UnitInstance sikg = Unit.SI.Kilo.Gramm; - Assert.AreEqual("kg", 1.SI().GetUnitString(sikg.GetSIUnits())); + Assert.AreEqual("kg", SI.GetUnitString(sikg.GetSIUnits())); UnitInstance ui1 = Unit.SI.Kilo.Gramm.Meter.Per.Square.Second; - Assert.AreEqual("kgm/s^2", 1.SI().GetUnitString(ui1.GetSIUnits())); + Assert.AreEqual("kgm/s^2", SI.GetUnitString(ui1.GetSIUnits())); UnitInstance ui3 = Unit.SI.Kilo.Gramm.Per.Watt.Second; - Assert.AreEqual("s^2/m^2", 1.SI().GetUnitString(ui3.GetSIUnits())); + Assert.AreEqual("s^2/m^2", SI.GetUnitString(ui3.GetSIUnits())); Assert.AreEqual(1, ui3.Factor); - var kg = 3000.SI(Unit.SI.Kilo.Gramm); + var kg = 3000.SI(Unit.SI.Kilo.Gramm).Cast<Kilogram>(); Assert.AreEqual("3000.0000 [kg]", kg.ToOutputFormat(showUnit: true)); var ton = 3.SI(Unit.SI.Ton); @@ -548,15 +550,15 @@ namespace TUGraz.VectoCore.Tests.Utils Assert.AreEqual("0.0070 [m^3]", val1.ToOutputFormat(showUnit: true)); var uni = Unit.SI.Cubic.Dezi.Meter; - Assert.AreEqual("m^3", 1.SI().GetUnitString(uni.GetSIUnits())); + Assert.AreEqual("m^3", SI.GetUnitString(uni.GetSIUnits())); AssertHelper.AreRelativeEqual(0.001, uni.Factor); var uni2 = Unit.SI.Cubic.Centi.Meter; - Assert.AreEqual("m^3", 1.SI().GetUnitString(uni2.GetSIUnits())); + Assert.AreEqual("m^3", SI.GetUnitString(uni2.GetSIUnits())); AssertHelper.AreRelativeEqual(0.000001, uni2.Factor); var uni1 = Unit.SI.Kilo.Meter.Per.Hour; - Assert.AreEqual("m/s", 1.SI().GetUnitString(uni1.GetSIUnits())); + Assert.AreEqual("m/s", SI.GetUnitString(uni1.GetSIUnits())); AssertHelper.AreRelativeEqual(0.2777777777, uni1.Factor); NewtonMeter newtonMeter = 5.SI<NewtonMeter>(); @@ -568,7 +570,7 @@ namespace TUGraz.VectoCore.Tests.Utils AssertHelper.AreRelativeEqual((6.0/3600).SI<Liter>(), (2.SI<Second>() * 3.SI(Unit.SI.Liter.Per.Hour)).Cast<Liter>()); AssertHelper.AreRelativeEqual(2.13093, 2.13093.SI(Unit.SI.Liter).Cast<Liter>().Value()); - Assert.AreEqual("m^3", 2.13093.SI(Unit.SI.Liter).GetUnitString()); + Assert.AreEqual("m^3", 2.13093.SI(Unit.SI.Liter).UnitString); } //[TestCase]