diff --git a/VectoCore/VectoCore/InputData/Reader/ComponentData/AuxiliaryDataReader.cs b/VectoCore/VectoCore/InputData/Reader/ComponentData/AuxiliaryDataReader.cs index ad81e9b1dfb005646b9acac5a75085fe1ccd8454..191a65a37ced513c232765aa87f15595f12d20d6 100644 --- a/VectoCore/VectoCore/InputData/Reader/ComponentData/AuxiliaryDataReader.cs +++ b/VectoCore/VectoCore/InputData/Reader/ComponentData/AuxiliaryDataReader.cs @@ -31,7 +31,6 @@ using System.Data; using System.IO; -using System.Linq; using System.Text; using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.InputData; @@ -41,30 +40,47 @@ using TUGraz.VectoCore.Utils; namespace TUGraz.VectoCore.InputData.Reader.ComponentData { + /// <summary> + /// Reads the auxiliary demand map. + /// </summary> public static class AuxiliaryDataReader { + /// <summary> + /// Factory method. + /// </summary> + /// <param name="data"></param> + /// <returns></returns> public static AuxiliaryData Create(IAuxiliaryEngineeringInputData data) { var map = ReadAuxMap(data.ID, data.DemandMap); return new AuxiliaryData(data.TransmissionRatio, data.EfficiencyToEngine, data.EfficiencyToSupply, map); } + /// <summary> + /// Reads the demand map from a file. + /// </summary> + /// <param name="fileName"></param> + /// <param name="id"></param> + /// <returns></returns> public static AuxiliaryData ReadFromFile(string fileName, string id) { try { - var stream = new StreamReader(fileName); - stream.ReadLine(); // skip header "Transmission ration to engine rpm [-]" - var transmissionRatio = stream.ReadLine().IndulgentParse(); - stream.ReadLine(); // skip header "Efficiency to engine [-]" - var efficiencyToEngine = stream.ReadLine().IndulgentParse(); - stream.ReadLine(); // skip header "Efficiency auxiliary to supply [-]" - var efficiencyToSupply = stream.ReadLine().IndulgentParse(); + using (var reader = new StreamReader(fileName)) { + reader.ReadLine(); // skip header "Transmission ration to engine rpm [-]" + var transmissionRatio = reader.ReadLine().IndulgentParse(); + reader.ReadLine(); // skip header "Efficiency to engine [-]" + var efficiencyToEngine = reader.ReadLine().IndulgentParse(); + reader.ReadLine(); // skip header "Efficiency auxiliary to supply [-]" + var efficiencyToSupply = reader.ReadLine().IndulgentParse(); - var m = new MemoryStream(Encoding.UTF8.GetBytes(stream.ReadToEnd())); - var table = VectoCSVFile.ReadStream(m); - var map = ReadAuxMap(id, table); + var m = new MemoryStream(Encoding.UTF8.GetBytes(reader.ReadToEnd())); + reader.Close(); - return new AuxiliaryData(transmissionRatio, efficiencyToEngine, efficiencyToSupply, map); + var table = VectoCSVFile.ReadStream(m); + var map = ReadAuxMap(id, table); + + return new AuxiliaryData(transmissionRatio, efficiencyToEngine, efficiencyToSupply, map); + } } catch (FileNotFoundException e) { throw new VectoException("Auxiliary file not found: " + fileName, e); } @@ -85,25 +101,21 @@ namespace TUGraz.VectoCore.InputData.Reader.ComponentData private static void FillFromColumnIndizes(DataTable table, DelaunayMap map) { - var data = table.Rows.Cast<DataRow>().Select(row => new { - AuxiliarySpeed = row.ParseDouble(0).RPMtoRad(), - MechanicalPower = row.ParseDouble(1).SI().Kilo.Watt.Cast<Watt>(), - SupplyPower = row.ParseDouble(2).SI().Kilo.Watt.Cast<Watt>() - }); - foreach (var d in data) { - map.AddPoint(d.AuxiliarySpeed.Value(), d.SupplyPower.Value(), d.MechanicalPower.Value()); + for (var i = 0; i < table.Rows.Count; i++) { + var row = table.Rows[i]; + map.AddPoint(row.ParseDouble(0).RPMtoRad().Value(), + row.ParseDouble(1).SI().Kilo.Watt.Cast<Watt>().Value(), + row.ParseDouble(2).SI().Kilo.Watt.Cast<Watt>().Value()); } } private static void FillFromColumnNames(DataTable table, DelaunayMap map) { - var data = table.Rows.Cast<DataRow>().Select(row => new { - AuxiliarySpeed = row.ParseDouble(Fields.AuxSpeed).RPMtoRad(), - MechanicalPower = row.ParseDouble(Fields.MechPower).SI().Kilo.Watt.Cast<Watt>(), - SupplyPower = row.ParseDouble(Fields.SupplyPower).SI().Kilo.Watt.Cast<Watt>() - }); - foreach (var d in data) { - map.AddPoint(d.AuxiliarySpeed.Value(), d.SupplyPower.Value(), d.MechanicalPower.Value()); + for (var i = 0; i < table.Rows.Count; i++) { + var row = table.Rows[i]; + map.AddPoint(row.ParseDouble(Fields.AuxSpeed).RPMtoRad().Value(), + row.ParseDouble(Fields.MechPower).SI().Kilo.Watt.Cast<Watt>().Value(), + row.ParseDouble(Fields.SupplyPower).SI().Kilo.Watt.Cast<Watt>().Value()); } } @@ -113,7 +125,7 @@ namespace TUGraz.VectoCore.InputData.Reader.ComponentData columns.Contains(Fields.SupplyPower); } - public static class Fields + internal static class Fields { /// <summary>[1/min]</summary> public const string AuxSpeed = "Auxiliary speed"; diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineAuxiliary.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineAuxiliary.cs index 28237ca525f4c4d6b885dc4fea7c05a04c4431b4..bb73f908d3c0495b75aeca6791879ec4931c1e64 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineAuxiliary.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/EngineAuxiliary.cs @@ -31,7 +31,6 @@ using System; using System.Collections.Generic; -using System.Linq; using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.Utils; using TUGraz.VectoCore.Configuration; @@ -42,10 +41,13 @@ using TUGraz.VectoCore.OutputData; namespace TUGraz.VectoCore.Models.SimulationComponent.Impl { + /// <summary> + /// Container Class for Auxiliaries which are connected to the Engine. + /// </summary> public class EngineAuxiliary : StatefulVectoSimulationComponent<EngineAuxiliary.State>, IAuxInProvider, IAuxPort { - protected readonly Dictionary<string, Func<PerSecond, Watt>> _auxiliaries = + protected readonly Dictionary<string, Func<PerSecond, Watt>> Auxiliaries = new Dictionary<string, Func<PerSecond, Watt>>(); public EngineAuxiliary(IVehicleContainer container) : base(container) {} @@ -55,16 +57,30 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl return this; } + /// <summary> + /// Adds a constant power demand auxiliary. + /// </summary> + /// <param name="auxId"></param> + /// <param name="powerDemand"></param> public void AddConstant(string auxId, Watt powerDemand) { Add(auxId, _ => powerDemand); } + /// <summary> + /// Adds an auxiliary which gets its power demand from the driving cycle. + /// </summary> + /// <param name="auxId"></param> public void AddCycle(string auxId) { Add(auxId, _ => DataBus.CycleData.LeftSample.AdditionalAuxPowerDemand); } + /// <summary> + /// Adds an auxiliary which calculates the demand based on a aux-map and the engine speed. + /// </summary> + /// <param name="auxId"></param> + /// <param name="data"></param> public void AddMapping(string auxId, AuxiliaryData data) { if (!DataBus.CycleData.LeftSample.AuxiliarySupplyPower.ContainsKey(auxId)) { @@ -83,9 +99,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl }); } + /// <summary> + /// Adds an auxiliary with a function returning the power demand based on the engine speed. + /// </summary> + /// <param name="auxId"></param> + /// <param name="powerLossFunction"></param> public void Add(string auxId, Func<PerSecond, Watt> powerLossFunction) { - _auxiliaries[auxId] = powerLossFunction; + Auxiliaries[auxId] = powerLossFunction; } public NewtonMeter Initialize(NewtonMeter torque, PerSecond angularSpeed) @@ -94,6 +115,16 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl return ComputePowerDemand(angularSpeed) / angularSpeed; } + /// <summary> + /// Calculates the torque demand for all registered auxiliaries for the the current engine + /// </summary> + /// <param name="absTime"></param> + /// <param name="dt"></param> + /// <param name="torquePowerTrain"></param> + /// <param name="torqueEngine"></param> + /// <param name="angularSpeed"></param> + /// <param name="dryRun"></param> + /// <returns></returns> public NewtonMeter TorqueDemand(Second absTime, Second dt, NewtonMeter torquePowerTrain, NewtonMeter torqueEngine, PerSecond angularSpeed, bool dryRun = false) { @@ -108,8 +139,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl protected Watt ComputePowerDemand(PerSecond engineSpeed) { - CurrentState.PowerDemands = new Dictionary<string, Watt>(_auxiliaries.Count); - foreach (var item in _auxiliaries) { + CurrentState.PowerDemands = new Dictionary<string, Watt>(Auxiliaries.Count); + foreach (var item in Auxiliaries) { var value = item.Value(engineSpeed); if (value != null) { CurrentState.PowerDemands[item.Key] = value; @@ -132,7 +163,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl } if (container[ModalResultField.P_aux] == null || container[ModalResultField.P_aux] == DBNull.Value) { // only overwrite if nobody else already wrote the total aux power - container[ModalResultField.P_aux] = auxPowerDemand; } }