From e8b2bb6b347e0218f82da23367b28e34c0f2f2ff Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Tue, 21 Jul 2015 13:27:11 +0200
Subject: [PATCH] mapping auxiliary data (delauney map)

---
 .../Models/Declaration/TorqueConverter.cs     |  4 +-
 .../SimulationComponent/Impl/Auxiliary.cs     |  7 ++--
 .../Impl/MappingAuxiliaryData.cs              | 37 ++++++++++++++++---
 .../SimulationComponent/Impl/Vehicle.cs       |  4 +-
 .../Utils/IEnumberableExtensionMethods.cs     |  6 +--
 VectoCoreTest/Utils/TestModalDataWriter.cs    |  3 ++
 6 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/VectoCore/Models/Declaration/TorqueConverter.cs b/VectoCore/Models/Declaration/TorqueConverter.cs
index 6c5bd62ebd..9c053939de 100644
--- a/VectoCore/Models/Declaration/TorqueConverter.cs
+++ b/VectoCore/Models/Declaration/TorqueConverter.cs
@@ -25,7 +25,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public NewtonMeter LookupTorque(double nu, PerSecond angularSpeedIn, PerSecond referenceSpeed)
 		{
-			var sec = Data.GetSection(kv => kv.Key < nu);
+			var sec = Data.GetSamples(kv => kv.Key < nu);
 
 			if (nu < sec.Item1.Key || sec.Item2.Key < nu) {
 				Log.Warn(string.Format("TCextrapol: nu = {0} [n_out/n_in]", nu));
@@ -37,7 +37,7 @@ namespace TUGraz.VectoCore.Models.Declaration
 
 		public double LookupMu(double nu)
 		{
-			var sec = Data.GetSection(kv => kv.Key < nu);
+			var sec = Data.GetSamples(kv => kv.Key < nu);
 
 			if (nu < sec.Item1.Key || sec.Item2.Key < nu) {
 				Log.Warn(string.Format("TCextrapol: nu = {0} [n_out/n_in]", nu));
diff --git a/VectoCore/Models/SimulationComponent/Impl/Auxiliary.cs b/VectoCore/Models/SimulationComponent/Impl/Auxiliary.cs
index 5d6e52db99..75d8f3fb85 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Auxiliary.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Auxiliary.cs
@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using Common.Logging;
 using TUGraz.VectoCore.Exceptions;
 using TUGraz.VectoCore.Models.Connector.Ports;
@@ -13,6 +12,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 {
 	public class Auxiliary : VectoSimulationComponent, IAuxiliary, ITnInPort, ITnOutPort
 	{
+		public const string DirectAuxiliaryId = "";
+
 		private readonly Dictionary<string, Func<PerSecond, Watt>> _auxDict = new Dictionary<string, Func<PerSecond, Watt>>();
 
 		private readonly Dictionary<string, Watt> _powerDemands = new Dictionary<string, Watt>();
@@ -88,7 +89,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		public void AddDirect(IDrivingCycleCockpit cycle)
 		{
-			_auxDict[""] = ignored => cycle.CycleData().LeftSample.AdditionalAuxPowerDemand;
+			_auxDict[DirectAuxiliaryId] = ignored => cycle.CycleData().LeftSample.AdditionalAuxPowerDemand;
 		}
 
 		public void AddMapping(string auxId, IDrivingCycleCockpit cycle, MappingAuxiliaryData data)
@@ -102,8 +103,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 			_auxDict[auxId] = speed => {
 				var powerSupply = cycle.CycleData().LeftSample.AuxiliarySupplyPower[auxId];
 
-				var powerAuxOut = powerSupply / data.EfficiencyToSupply;
 				var nAuxiliary = speed * data.TransitionRatio;
+				var powerAuxOut = powerSupply / data.EfficiencyToSupply;
 				var powerAuxIn = data.GetPowerDemand(nAuxiliary, powerAuxOut);
 				return powerAuxIn / data.EfficiencyToEngine;
 			};
diff --git a/VectoCore/Models/SimulationComponent/Impl/MappingAuxiliaryData.cs b/VectoCore/Models/SimulationComponent/Impl/MappingAuxiliaryData.cs
index 66f5f6fc68..c4a4dfc1d5 100644
--- a/VectoCore/Models/SimulationComponent/Impl/MappingAuxiliaryData.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/MappingAuxiliaryData.cs
@@ -1,4 +1,6 @@
-using System;
+using System.Data;
+using System.IO;
+using System.Linq;
 using TUGraz.VectoCore.Utils;
 
 namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
@@ -9,16 +11,39 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 		public double TransitionRatio { get; set; }
 		public double EfficiencyToEngine { get; set; }
 
+		private readonly DelauneyMap _map = new DelauneyMap();
+
 		public Watt GetPowerDemand(PerSecond nAuxiliary, Watt powerAuxOut)
 		{
-			//todo GetPowerDemand
-			throw new NotImplementedException();
+			return _map.Interpolate(nAuxiliary.Value(), powerAuxOut.Value()).SI<Watt>();
 		}
 
-		public static MappingAuxiliaryData ReadFromFile(string filePath)
+		public static MappingAuxiliaryData ReadFromFile(string fileName)
 		{
-			//todo ReadFromFile
-			throw new NotImplementedException();
+			var auxData = new MappingAuxiliaryData();
+
+			var stream = new StreamReader(fileName);
+			stream.ReadLine(); // skip header "Transmission ration to engine rpm [-]"
+			auxData.TransitionRatio = stream.ReadLine().ToDouble();
+			stream.ReadLine(); // skip header "Efficiency to engine [-]"
+			auxData.EfficiencyToEngine = stream.ReadLine().ToDouble();
+			stream.ReadLine(); // skip header "Efficiency auxiliary to supply [-]"
+			auxData.EfficiencyToSupply = stream.ReadLine().ToDouble();
+
+			var table = VectoCSVFile.ReadStream(stream.BaseStream);
+
+			var data = table.Rows.Cast<DataRow>().Select(row => new {
+				AuxiliarySpeed = row.ParseDouble("Auxiliary speed").RPMtoRad(),
+				MechanicalPower = row.ParseDouble("Mechanical power").SI().Kilo.Watt.Cast<Watt>(),
+				SupplyPower = row.ParseDouble("Supply power").SI().Kilo.Watt.Cast<Watt>()
+			});
+
+			foreach (var d in data) {
+				auxData._map.AddPoint(d.AuxiliarySpeed.Value(), d.SupplyPower.Value(), d.MechanicalPower.Value());
+			}
+			auxData._map.Triangulate();
+
+			return auxData;
 		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs b/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
index 3795e8505d..a970d3680a 100644
--- a/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
+++ b/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs
@@ -111,7 +111,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 
 		private double AirDragInterpolate(IEnumerable<Point> curve, MeterPerSecond x)
 		{
-			var p = curve.GetSection(c => c.X < x);
+			var p = curve.GetSamples(c => c.X < x);
 
 			if (x < p.Item1.X || p.Item2.X < x) {
 				Log.Error(_data.CrossWindCorrectionMode == CrossWindCorrectionMode.VAirBeta
@@ -151,7 +151,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl
 					var vAir = VectoMath.Sqrt<MeterPerSecond>(vAirX * vAirX + vAirY * vAirY);
 					var beta = Math.Atan((vAirY / vAirX).Value()).ToDegree();
 
-					var sec = betaValues.GetSection(b => b.Key < beta);
+					var sec = betaValues.GetSamples(b => b.Key < beta);
 					var deltaCdA = VectoMath.Interpolate(sec.Item1.Key, sec.Item2.Key, sec.Item1.Value, sec.Item2.Value, beta);
 					var cdA = cdA0Actual + deltaCdA;
 
diff --git a/VectoCore/Utils/IEnumberableExtensionMethods.cs b/VectoCore/Utils/IEnumberableExtensionMethods.cs
index a5a036f2b0..a769421832 100644
--- a/VectoCore/Utils/IEnumberableExtensionMethods.cs
+++ b/VectoCore/Utils/IEnumberableExtensionMethods.cs
@@ -29,7 +29,7 @@ namespace TUGraz.VectoCore.Utils
 		/// Get the two adjacent items where the predicate is true.
 		/// If the predicate never gets true, the last 2 elements are returned.
 		/// </summary>
-		public static Tuple<T, T> GetSection<T>(this IEnumerable<T> self, Func<T, bool> skip, out int index)
+		public static Tuple<T, T> GetSamples<T>(this IEnumerable<T> self, Func<T, bool> skip, out int index)
 		{
 			var list = self.ToList();
 			var skipList = list.Select((arg1, i) => new { skip = skip(arg1) && i < list.Count - 1, i, value = arg1 });
@@ -42,10 +42,10 @@ namespace TUGraz.VectoCore.Utils
 		/// Get the two adjacent items where the predicate is true.
 		/// If the predicate never gets true, the last 2 elements are returned.
 		/// </summary>
-		public static Tuple<T, T> GetSection<T>(this IEnumerable<T> self, Func<T, bool> predicate)
+		public static Tuple<T, T> GetSamples<T>(this IEnumerable<T> self, Func<T, bool> predicate)
 		{
 			int unused;
-			return self.GetSection(predicate, out unused);
+			return self.GetSamples(predicate, out unused);
 		}
 	}
 }
\ No newline at end of file
diff --git a/VectoCoreTest/Utils/TestModalDataWriter.cs b/VectoCoreTest/Utils/TestModalDataWriter.cs
index a9e4d8ccc6..c0c415bcb6 100644
--- a/VectoCoreTest/Utils/TestModalDataWriter.cs
+++ b/VectoCoreTest/Utils/TestModalDataWriter.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Data;
 using System.Linq;
 using TUGraz.VectoCore.Models.Simulation.Data;
+using TUGraz.VectoCore.Utils;
 
 namespace TUGraz.VectoCore.Tests.Utils
 {
@@ -40,6 +41,8 @@ namespace TUGraz.VectoCore.Tests.Utils
 			return Data.Rows.Cast<DataRow>().Select(x => x.Field<T>((int)key));
 		}
 
+		public Dictionary<string, Watt> Auxiliaries { get; set; }
+
 		public object this[ModalResultField key]
 		{
 			get { return CurrentRow[key.GetName()]; }
-- 
GitLab