diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/TransmissionLossMap.cs b/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/TransmissionLossMap.cs index da0429caac39d25c04a0142b81a4815101fea43c..88b3add79c00b2fad9b380b6a7ee28c79c3096b8 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/TransmissionLossMap.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Data/Gearbox/TransmissionLossMap.cs @@ -147,9 +147,9 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox _invertedLossMap = new DelauneyMap(); foreach (var entry in _entries) { var outTorque = (entry.InputTorque - entry.TorqueLoss) * _ratio; - var outSpeed = entry.InputSpeed.Value() / _ratio; + var outSpeed = (entry.InputSpeed / _ratio).ConvertTo().Rounds.Per.Minute.Value(); _lossMap.AddPoint(outSpeed, outTorque.Value(), entry.TorqueLoss.Value()); - _invertedLossMap.AddPoint(entry.InputSpeed.Value(), entry.InputTorque.Value(), entry.TorqueLoss.Value()); + _invertedLossMap.AddPoint(entry.InputSpeed.ConvertTo().Rounds.Per.Minute.Value(), entry.InputTorque.Value(), entry.TorqueLoss.Value()); } _lossMap.Triangulate(); @@ -165,7 +165,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox /// <returns>Torque loss as seen on input side (towards the engine).</returns> public NewtonMeter GetTorqueLoss(PerSecond outAngularVelocity, NewtonMeter outTorque) { - var torqueLoss = _lossMap.Interpolate(outAngularVelocity.Value(), outTorque.Value(), true).SI<NewtonMeter>(); + var torqueLoss = _lossMap.Interpolate(outAngularVelocity.ConvertTo().Rounds.Per.Minute.Value(), outTorque.Value(), true).SI<NewtonMeter>(); Log.Debug("GearboxLoss {0}: {1}, outAngularVelocity: {2}, outTorque: {3}", GearName, torqueLoss, outAngularVelocity, outTorque); @@ -182,7 +182,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox public NewtonMeter GetOutTorque(PerSecond inAngularVelocity, NewtonMeter inTorque, bool allowExtrapolation = false) { var torqueLoss = - _invertedLossMap.Interpolate(inAngularVelocity.Value(), inTorque.Value(), allowExtrapolation).SI<NewtonMeter>(); + _invertedLossMap.Interpolate(inAngularVelocity.ConvertTo().Rounds.Per.Minute.Value(), inTorque.Value(), allowExtrapolation).SI<NewtonMeter>(); return (inTorque - torqueLoss) / _ratio; } @@ -193,7 +193,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Gearbox public class GearLossMapEntry { - [Required, SIRange(0, 5000 * Constants.RPMToRad)] + [Required, SIRange(0, 5000)] public PerSecond InputSpeed { get; set; } [Required, SIRange(-50000, 50000)] diff --git a/VectoCore/VectoCore/Utils/DelauneyMap.cs b/VectoCore/VectoCore/Utils/DelauneyMap.cs index cb86d852a9be6cf0af524070a3370744d13d6f1a..534a3c2dbf7d95a2abbab52c0fa9f6355d82d144 100644 --- a/VectoCore/VectoCore/Utils/DelauneyMap.cs +++ b/VectoCore/VectoCore/Utils/DelauneyMap.cs @@ -31,12 +31,19 @@ using System; using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.Globalization; +using System.IO; using System.Linq; +using System.Net; using System.Threading; +using System.Windows.Forms.DataVisualization.Charting; using Newtonsoft.Json; using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.Models; using TUGraz.VectoCommon.Utils; +using TUGraz.VectoCore.Models.Declaration; namespace TUGraz.VectoCore.Utils { @@ -79,22 +86,31 @@ namespace TUGraz.VectoCore.Utils var max = Points.Max(point => Math.Max(Math.Abs(point.X), Math.Abs(point.Y))) * superTriangleScalingFactor; var superTriangle = new Triangle(new Point(max, 0), new Point(0, max), new Point(-max, -max)); var triangles = new List<Triangle> { superTriangle }; - + var pointCount = 0; + + var points = Points.ToArray(); + + //var xmin = points.Min(p => p.X) - 1; + //var xmax = points.Max(p => p.X) + 1; + //var ymin = points.Min(p => p.Y) - 1; + //var ymax = points.Max(p => p.Y) + 1; + // iteratively add each point into the correct triangle and split up the triangle - foreach (var point in Points) { - // If the vertex lies inside a triangle, the edges of the triangle are + for (var i = 0; i < points.Length; i++) { + var point = points[i]; + // If the vertex lies inside the circumcircle of a triangle, the edges of this triangle are // added to the edge buffer and the triangle is removed from list. var containerTriangles = triangles.FindAll(t => t.ContainsInCircumcircle(point)); triangles = triangles.Except(containerTriangles).ToList(); - + // Remove duplicate edges. This leaves the convex hull of the edges. // The edges in this convex hull are oriented counterclockwise! - var allEdges = containerTriangles.SelectMany(t => t.GetEdges()); - var groupedEdges = allEdges.GroupBy(edge => edge); - var convexHullEdges = groupedEdges.Where(group => group.Count() == 1).Select(group => group.Key); + var allEdges = containerTriangles.SelectMany(t => t.GetEdges()).ToList(); + var groupedEdges = allEdges.GroupBy(edge => edge).ToList(); + var convexHullEdges = groupedEdges.Where(group => group.Count() == 1).Select(group => group.Key).ToList(); - var newTriangles = convexHullEdges.Select(edge => new Triangle(edge.P1, edge.P2, point)); + var newTriangles = convexHullEdges.Select(edge => new Triangle(edge.P1, edge.P2, point)).ToList(); triangles.AddRange(newTriangles); pointCount++; @@ -107,6 +123,8 @@ namespace TUGraz.VectoCore.Utils throw new VectoException( "Delauney-Triangulation invariant violated! Triangle count and point count doesn't fit together."); } + //if (superTriangle.GetHashCode() == -540272836) + // DrawGraph(i, triangles, superTriangle, xmin, xmax, ymin, ymax); } _convexHull = triangles.FindAll(t => t.SharesVertexWith(superTriangle)). @@ -116,6 +134,31 @@ namespace TUGraz.VectoCore.Utils _triangles = triangles.FindAll(t => !t.SharesVertexWith(superTriangle)); } + private void DrawGraph(int i, List<Triangle> triangles, Triangle superTriangle, double xmin, double xmax, double ymin, double ymax) + { + using (var chart = new Chart { Width = 1500, Height = 700 }) { + chart.ChartAreas.Add(new ChartArea("main") { + AxisX = new Axis { Minimum = xmin, Maximum = xmax}, + AxisY = new Axis { Minimum = ymin, Maximum = ymax } + }); + + foreach (var tr in triangles) { + if (tr.SharesVertexWith(superTriangle)) + continue; + var series = new Series {ChartType = SeriesChartType.FastLine}; + series.Points.AddXY(tr.P1.X, tr.P1.Y); + series.Points.AddXY(tr.P2.X, tr.P2.Y); + series.Points.AddXY(tr.P3.X, tr.P3.Y); + series.Points.AddXY(tr.P1.X, tr.P1.Y); + chart.Series.Add(series); + } + + chart.Invalidate(); + Directory.CreateDirectory(string.Format("delauney\\{0}", superTriangle.GetHashCode())); + chart.SaveImage(string.Format("delauney\\{0}\\{1}.png", superTriangle.GetHashCode(), i), ChartImageFormat.Png); + } + } + public double Interpolate(double x, double y, bool allowExtrapolation = false) { var tr = _triangles.Find(triangle => triangle.IsInside(x, y, exact: true)) ?? diff --git a/VectoCore/VectoCore/Utils/VectoMath.cs b/VectoCore/VectoCore/Utils/VectoMath.cs index bbc4d81e07f56391e47ec40de78538748a582ec0..e271c468908e1b3963c0b28478d1dfeec127d382 100644 --- a/VectoCore/VectoCore/Utils/VectoMath.cs +++ b/VectoCore/VectoCore/Utils/VectoMath.cs @@ -402,8 +402,18 @@ namespace TUGraz.VectoCore.Utils var det20 = p2X * p0Y - p0X * p2Y; var result = p0Square * det12 + p1Square * det20 + p2Square * det01; - return result > 0; + + //double[,] m = { { P1.X - p.X, P1.Y - p.Y, (P1.X * P1.X - p.X*p.X) + (P1.Y * P1.Y - p.Y*p.Y) }, + // { P2.X - p.X, P2.Y - p.Y, (P2.X * P2.X - p.X*p.X) + (P2.Y * P2.Y - p.Y*p.Y) }, + // { P3.X - p.X, P3.Y - p.Y, (P3.X * P3.X - p.X*p.X) + (P3.Y * P3.Y - p.Y*p.Y) } }; + //var det = m[0, 0] * m[1, 1] * m[2, 2] + // + m[0, 1] * m[1, 2] * m[2, 0] + // + m[0, 2] * m[1, 0] * m[2, 1] + // - m[0, 0] * m[1, 2] * m[2, 1] + // - m[0, 1] * m[1, 0] * m[2, 2] + // - m[0, 2] * m[1, 1] * m[2, 0]; + //return det > 0; } public bool Contains(Point p)