diff --git a/VectoCommon/VectoCommon/Utils/SI.cs b/VectoCommon/VectoCommon/Utils/SI.cs index 5b67d2cfca838e1d8af4a4913f2d4d96809ecb20..c40df194cfc0f2f4cf768d977a67b5dea0c4f618 100644 --- a/VectoCommon/VectoCommon/Utils/SI.cs +++ b/VectoCommon/VectoCommon/Utils/SI.cs @@ -872,15 +872,8 @@ namespace TUGraz.VectoCommon.Utils Reverse = reverse; Exponent = exponent; - var tmpNumerator = numerator.ToList(); var tmpDenominator = denominator.ToList(); - - foreach (var v in tmpDenominator.ToArray().Where(v => tmpNumerator.Contains(v))) { - tmpNumerator.Remove(v); - tmpDenominator.Remove(v); - } - - Numerator = tmpNumerator.ToArray(); + Numerator = numerator.Where(n => !tmpDenominator.Remove(n)).ToArray(); Denominator = tmpDenominator.ToArray(); if (double.IsNaN(Val)) { diff --git a/VectoCore/VectoCore/Utils/Formulas.cs b/VectoCore/VectoCore/Utils/Formulas.cs index cba55efec85b6ea6807a9592d8ed4a17d6d24c54..b3ab0808532f39bae83025d276c1cb2f89f49eef 100644 --- a/VectoCore/VectoCore/Utils/Formulas.cs +++ b/VectoCore/VectoCore/Utils/Formulas.cs @@ -62,7 +62,7 @@ namespace TUGraz.VectoCore.Utils throw new VectoException("v2 must not be greater than v1 v1: {0} v2: {1}", v1.Value(), v2.Value()); } - return ((v2 - v1) * (v1 + v2) / deceleration / 2.0).Cast<Meter>(); + return ((v2.Value() - v1.Value()) * (v1.Value() + v2.Value()) / deceleration.Value() / 2.0).SI<Meter>(); } /// <summary> @@ -79,21 +79,20 @@ namespace TUGraz.VectoCore.Utils public static Watt InertiaPower(PerSecond currentOmega, PerSecond previousOmega, KilogramSquareMeter inertia, Second dt) { - var deltaOmega = currentOmega - previousOmega; - - var alpha = deltaOmega / dt; - var torque = inertia * alpha; + var deltaOmega = currentOmega.Value() - previousOmega.Value(); - var avgOmega = (currentOmega + previousOmega) / 2; - return (torque * avgOmega).Cast<Watt>(); - } + var alpha = deltaOmega / dt.Value(); + var torque = inertia.Value() * alpha; + var avgOmega = (currentOmega.Value() + previousOmega.Value()) / 2; + return (torque * avgOmega).SI<Watt>(); + } public static Watt InertiaPower(PerSecond omega, PerSquareSecond alpha, KilogramSquareMeter inertia, Second dt) { - var torque = inertia * alpha; - var power = torque * (omega + alpha / 2 * dt); - return power; + var torque = inertia.Value() * alpha.Value(); + var power = torque * (omega.Value() + alpha.Value() / 2 * dt.Value()); + return power.SI<Watt>(); } } } \ No newline at end of file diff --git a/VectoCore/VectoCore/Utils/SearchAlgorithm.cs b/VectoCore/VectoCore/Utils/SearchAlgorithm.cs index 9cff57e03547eae0111b2c84af6d7a6e208e9a9b..9feb4fd9b40953af4e6718b577e5dcff9a288db6 100644 --- a/VectoCore/VectoCore/Utils/SearchAlgorithm.cs +++ b/VectoCore/VectoCore/Utils/SearchAlgorithm.cs @@ -95,8 +95,7 @@ namespace TUGraz.VectoCore.Utils try { result = InterpolateSearch(x, y, interval, getYValue, evaluateFunction, criterion, abortCriterion, ref iterationCount); - } - catch (VectoException ex) { + } catch (VectoException ex) { var log = LogManager.GetLogger(typeof(SearchAlgorithm).FullName); log.Debug("Falling back to LineSearch. InterpolationSearch failed: " + ex.Message); result = LineSearch(x, y, interval, getYValue, evaluateFunction, criterion, abortCriterion, ref iterationCount); @@ -104,7 +103,6 @@ namespace TUGraz.VectoCore.Utils return result; } - /// <summary> /// Line Search Algorithm. /// Phase 1: Linear Bracketing: Search iterative for the area of interest (with fixed step size). @@ -167,16 +165,16 @@ namespace TUGraz.VectoCore.Utils var xmax = debug.Data.Max(d => d.x).Value(); var ymin = debug.Data.Min(d => d.y).Value(); var ymax = debug.Data.Max(d => d.y).Value(); - + var rand = new Random().Next(); using (var f = new StreamWriter(File.Open("LineSearch-" + Thread.CurrentThread.ManagedThreadId + "-statistics.csv", FileMode.Append))) { foreach (var d in debug.Data) { - f.WriteLine(string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", - rand, - (d.x.Value()-xmin)/(xmax-xmin), - (d.y.Value()-ymin)/(ymax-ymin), - d.x.Value()/Math.Max(Math.Abs(xmax),Math.Abs(xmin)), - d.y.Value()/Math.Max(Math.Abs(ymax),Math.Abs(ymin)), + f.WriteLine(string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", + rand, + (d.x.Value() - xmin) / (xmax - xmin), + (d.y.Value() - ymin) / (ymax - ymin), + d.x.Value() / Math.Max(Math.Abs(xmax), Math.Abs(xmin)), + d.y.Value() / Math.Max(Math.Abs(ymax), Math.Abs(ymin)), d.x, d.y)); } } @@ -186,36 +184,41 @@ namespace TUGraz.VectoCore.Utils /// Interpolating Search algorithm. /// Calculates linear equation of 2 points and jumps directly to root-point. /// </summary> - private static T InterpolateSearch<T>(T x1, SI y1, T interval, Func<object, SI> getYValue, + private static T InterpolateSearch<T>(T x1SI, SI y1SI, T intervalSI, Func<object, SI> getYValue, Func<T, object> evaluateFunction, Func<object, double> criterion, Func<object, int, bool> abortCriterion, ref int iterationCount) where T : SIBase<T> { + var x1 = x1SI.Value(); + var interval = intervalSI.Value(); + var y1 = y1SI.Value(); + var log = LogManager.GetLogger(typeof(SearchAlgorithm).FullName); var debug = new DebugData(); debug.Add(new { x = x1, y = y1 }); log.Debug("Log Disabled during InterpolateSearch."); LogManager.DisableLogging(); + try { var x2 = x1 + interval; - var result = evaluateFunction(x2); + var result = evaluateFunction(x2.SI<T>()); if (criterion(result).IsEqual(0, Constants.SimulationSettings.InterpolateSearchTolerance)) { LogManager.EnableLogging(); log.Debug("InterpolateSearch found an operating point after 1 function call."); AppendDebug(debug); LogManager.DisableLogging(); iterationCount++; - return x2; + return x2.SI<T>(); } for (var count = 2; count < 30; count++, iterationCount++) { - var y2 = getYValue(result); + var y2 = getYValue(result).Value(); debug.Add(new { x = x2, y = y2, delta = criterion(result), result }); try { var k = (y2 - y1) / (x2 - x1); var d = y2 - k * x2; x1 = x2; - x2 = (-d / k).Cast<T>(); + x2 = -d / k; } catch (VectoException ex) { if (!(ex.InnerException is DivideByZeroException)) { throw; @@ -226,17 +229,17 @@ namespace TUGraz.VectoCore.Utils LogManager.DisableLogging(); AppendDebug(debug); //iterationCount += count; - return x2; + return x2.SI<T>(); } - result = evaluateFunction(x2); + result = evaluateFunction(x2.SI<T>()); if (criterion(result).IsEqual(0, Constants.SimulationSettings.InterpolateSearchTolerance)) { debug.Add(new { x = x2, y = getYValue(result), delta = criterion(result), result }); LogManager.EnableLogging(); log.Debug("InterpolateSearch found an operating point after {0} function calls.", count); LogManager.DisableLogging(); AppendDebug(debug); - return x2; + return x2.SI<T>(); } if (abortCriterion != null && abortCriterion(result, iterationCount)) { LogManager.EnableLogging(); diff --git a/VectoCore/VectoCore/Utils/VectoMath.cs b/VectoCore/VectoCore/Utils/VectoMath.cs index 3e618f365969f89bac71bc60e3e701087e2dcd5d..9611a22aeab9b4561ee86c76ae83108b95126566 100644 --- a/VectoCore/VectoCore/Utils/VectoMath.cs +++ b/VectoCore/VectoCore/Utils/VectoMath.cs @@ -56,20 +56,23 @@ namespace TUGraz.VectoCore.Utils public static TResult Interpolate<T, TResult>(T x1, T x2, TResult y1, TResult y2, T xint) where T : SI where TResult : SIBase<TResult> { - return ((xint - x1) * (y2 - y1) / (x2 - x1) + y1).Cast<TResult>(); + return Interpolate(x1.Value(), x2.Value(), y1.Value(), y2.Value(), xint.Value()).SI<TResult>(); } - - public static double Interpolate<T>(T x1, T x2, double y1, double y2, T xint) - where T : SI + public static double Interpolate<T>(T x1, T x2, double y1, double y2, T xint) where T : SI { - return (((xint - x1) * (y2 - y1) / (x2 - x1)).Cast<Scalar>() + y1).Value(); + return Interpolate(x1.Value(), x2.Value(), y1, y2, xint.Value()); } public static TResult Interpolate<TResult>(double x1, double x2, TResult y1, TResult y2, double xint) where TResult : SIBase<TResult> { - return ((xint - x1) * (y2 - y1) / (x2 - x1) + y1).Cast<TResult>(); + return Interpolate(x1, x2, y1.Value(), y2.Value(), xint).SI<TResult>(); + } + + public static double Interpolate(Point p1, Point p2, double x) + { + return Interpolate(p1.X, p2.X, p1.Y, p2.Y, x); } /// <summary> @@ -77,12 +80,7 @@ namespace TUGraz.VectoCore.Utils /// </summary> public static double Interpolate(double x1, double x2, double y1, double y2, double xint) { - return ((xint - x1) * (y2 - y1) / (x2 - x1) + y1); - } - - public static double Interpolate(Point p1, Point p2, double x) - { - return Interpolate(p1.X, p2.X, p1.Y, p2.Y, x); + return (xint - x1) * (y2 - y1) / (x2 - x1) + y1; } /// <summary> @@ -112,7 +110,9 @@ namespace TUGraz.VectoCore.Utils public static T Limit<T>(this T value, T lowerBound, T upperBound) where T : IComparable { if (lowerBound.CompareTo(upperBound) > 0) { - throw new VectoException("VectoMath.Limit: lowerBound must not be greater than upperBound. lowerBound: {0}, upperBound: {1}", lowerBound, upperBound); + throw new VectoException( + "VectoMath.Limit: lowerBound must not be greater than upperBound. lowerBound: {0}, upperBound: {1}", lowerBound, + upperBound); } if (value.CompareTo(upperBound) > 0) {