Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 2ddc7694 authored by Michael KRISPER's avatar Michael KRISPER
Browse files

refactored strings, rewrote CombustionEngineData

parent c7a3406d
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using NLog.Layouts;
using TUGraz.VectoCore.Exceptions;
using TUGraz.VectoCore.Models.SimulationComponent.Data.Engine;
......@@ -43,90 +46,178 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
/// </code>
public class CombustionEngineData : SimulationComponentData
{
private readonly Dictionary<string, FullLoadCurve> _fullLoadCurves = new Dictionary<string, FullLoadCurve>();
public static CombustionEngineData ReadFromFile(string fileName)
public class Data
{
//todo: file exception handling: file not readable, wrong file format
return ReadFromJson(File.ReadAllText(fileName));
}
public class DataHeader
{
[JsonProperty(Required = Required.Always)]
public string CreatedBy;
public static CombustionEngineData ReadFromJson(string json)
{
CombustionEngineData engine = new CombustionEngineData();
var results = JsonConvert.DeserializeObject<dynamic>(json);
[JsonProperty(Required = Required.Always)]
public DateTime Date;
[JsonProperty(Required = Required.Always)]
public string AppVersion;
//todo: handle error when fields not exist
var header = results["Header"];
[JsonProperty(Required = Required.Always)]
public double FileVersion;
}
if (header["FileVersion"] > 2)
throw new UnsupportedFileVersion("Unsupported Version of .veng file. Got Version: " + header["FileVersion"]);
[JsonProperty(Required = Required.Always)]
public DataHeader Header;
var body = results["Body"];
public class DataBody
{
public class DataFullLoadCurve
{
[JsonProperty(Required = Required.Always)]
public string Gears;
if (header["FileVersion"] > 1)
engine.SavedInDeclarationMode = body["SavedInDeclMode"];
[JsonProperty(Required = Required.Always)]
public string Path;
}
[JsonProperty(Required = Required.Always)]
public IList<DataFullLoadCurve> FullLoadCurves;
engine.ModelName = body["ModelName"];
engine.Displacement = body["Displacement"];
engine.IdleSpeed = body["IdlingSpeed"];
engine.Inertia = body["Inertia"];
[JsonProperty("SavedInDeclMode")]
public bool SavedInDeclarationMode;
foreach (dynamic loadCurve in body["FullLoadCurves"])
engine._fullLoadCurves[loadCurve["Gears"].Value] = FullLoadCurve.ReadFromFile(loadCurve["Path"].Value);
[JsonProperty(Required = Required.Always)]
public string ModelName;
engine.ConsumptionMap = FuelConsumptionMap.ReadFromFile(body["FuelMap"].Value);
[JsonProperty(Required = Required.Always)]
public double Displacement;
if (body["WHTC-Urban"] != null)
engine.WHTCUrban = body["WHTC-Urban"].Value;
[JsonProperty("IdlingSpeed", Required = Required.Always)]
public double IdleSpeed;
if (body["WHTC-Rural"] != null)
engine.WHTCRural = body["WHTC-Rural"].Value;
[JsonProperty(Required = Required.Always)]
public double Inertia;
if (body["WHTC-Motorway"] != null)
engine.WHTCMotorway = body["WHTC-Motorway"].Value;
[JsonProperty(Required = Required.Always)]
public string FuelMap;
return engine;
}
[JsonProperty("WHTC-Urban")]
public double WHTCUrban;
public double WHTCMotorway { get; set; }
[JsonProperty("WHTC-Rural")]
public double WHTCRural;
public double WHTCRural { get; set; }
[JsonProperty("WHTC-Motorway")]
public double WHTCMotorway;
}
public double WHTCUrban { get; set; }
[JsonProperty(Required = Required.Always)]
public DataBody Body;
}
public bool SavedInDeclarationMode { get; set; }
private Data _data;
/// <summary>
/// Engine description (e.g., mode, type, etc.
/// </summary>
public String ModelName { get; set; }
public bool SavedInDeclarationMode
{
get { return _data.Body.SavedInDeclarationMode; }
protected set { _data.Body.SavedInDeclarationMode = value; }
}
/// <summary>
/// Engine displacement [ccm]
/// </summary>
public double Displacement { get; set; }
public string ModelName
{
get { return _data.Body.ModelName; }
protected set { _data.Body.ModelName = value; }
}
public double IdleSpeed { get; set; }
public double Displacement
{
get { return _data.Body.Displacement; }
protected set { _data.Body.Displacement = value; }
}
public double RatedSpeed { get; set; }
public double IdleSpeed
{
get { return _data.Body.IdleSpeed; }
protected set { _data.Body.IdleSpeed = value; }
}
public double Inertia { get; set; }
public double Inertia
{
get { return _data.Body.Inertia; }
protected set { _data.Body.Inertia = value; }
}
public double MaxPower { get; set; }
public double WHTCUrban
{
get { return _data.Body.WHTCUrban; }
protected set { _data.Body.WHTCUrban = value; }
}
public double WHTCRural
{
get { return _data.Body.WHTCRural; }
protected set { _data.Body.WHTCRural = value; }
}
public double WHTCMotorway
{
get { return _data.Body.WHTCMotorway; }
protected set { _data.Body.WHTCMotorway = value; }
}
public FuelConsumptionMap ConsumptionMap { get; set; }
private readonly Dictionary<string, FullLoadCurve> _fullLoadCurves = new Dictionary<string, FullLoadCurve>();
public static CombustionEngineData ReadFromFile(string fileName)
{
//todo: file exception handling: file not readable
return ReadFromJson(File.ReadAllText(fileName), Path.GetDirectoryName(fileName));
}
public static CombustionEngineData ReadFromJson(string json, string basePath = "")
{
var combustionEngineData = new CombustionEngineData();
//todo handle conversion errors
var d = JsonConvert.DeserializeObject<Data>(json);
combustionEngineData._data = d;
if (d.Header.FileVersion > 2)
throw new UnsupportedFileVersion("Unsupported Version of .veng file. Got Version: " + d.Header.FileVersion);
combustionEngineData.ConsumptionMap = FuelConsumptionMap.ReadFromFile(Path.Combine(basePath, d.Body.FuelMap));
foreach (var loadCurve in d.Body.FullLoadCurves)
{
var fullLoadCurve = FullLoadCurve.ReadFromFile(Path.Combine(basePath, loadCurve.Path));
combustionEngineData._fullLoadCurves[loadCurve.Gears] = fullLoadCurve;
}
return combustionEngineData;
}
public string WriteToJson()
{
_data.Header.Date = DateTime.Now;
_data.Header.FileVersion = 2;
_data.Header.AppVersion = "3.0.0"; // todo: get current app version!
_data.Header.CreatedBy = ""; // todo: get current user
_data.Body.SavedInDeclarationMode = false; //todo: get declaration mode setting
return JsonConvert.SerializeObject(_data);
}
public FullLoadCurve GetFullLoadCurve(uint gear)
{
foreach (var gear_range in _fullLoadCurves.Keys)
foreach (var gearRange in _fullLoadCurves.Keys)
{
var low = uint.Parse(gear_range.Split('-').First().Trim());
var low = uint.Parse(gearRange.Split('-').First().Trim());
if (low <= gear)
{
var high = uint.Parse(gear_range.Split('-').Last().Trim());
var high = uint.Parse(gearRange.Split('-').Last().Trim());
if (high >= gear)
return _fullLoadCurves[gear_range];
return _fullLoadCurves[gearRange];
}
}
throw new KeyNotFoundException(string.Format("Gear '{0}' was not found in the FullLoadCurves.", gear));
......
......@@ -18,6 +18,13 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
/// </summary>
public class FuelConsumptionMap
{
private static class Fields
{
public const string EngineSpeed = "engine speed";
public const string Torque = "torque";
public const string FuelConsumption = "fuel consumption";
};
private class FuelConsumptionEntry
{
public double EngineSpeed { get; set; }
......@@ -25,24 +32,27 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
public double FuelConsumption { get; set; }
}
private List<FuelConsumptionEntry> entries;
private IList<FuelConsumptionEntry> entries;
public static FuelConsumptionMap ReadFromFile(string fileName)
public FuelConsumptionMap(string fileName)
{
var fuelConsumptionMap = new FuelConsumptionMap();
var data = VectoCSVReader.Read(fileName);
fuelConsumptionMap.entries = new List<FuelConsumptionEntry>();
entries = new List<FuelConsumptionEntry>();
//todo: catch exceptions if value format is wrong.
foreach (DataRow row in data.Rows)
{
var entry = new FuelConsumptionEntry();
entry.EngineSpeed = row.GetDouble("engine speed");
entry.Torque = row.GetDouble("torque");
entry.FuelConsumption = row.GetDouble("fuel consumption");
fuelConsumptionMap.entries.Add(entry);
entry.EngineSpeed = row.GetDouble(Fields.EngineSpeed);
entry.Torque = row.GetDouble(Fields.Torque);
entry.FuelConsumption = row.GetDouble(Fields.FuelConsumption);
entries.Add(entry);
}
return fuelConsumptionMap;
}
public static FuelConsumptionMap ReadFromFile(string fileName)
{
return new FuelConsumptionMap(fileName);
}
}
}
......@@ -17,6 +17,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
/// </summary>
public class FullLoadCurve
{
private static class Fields
{
public const string EngineSpeed = "n";
public const string TorqueFullLoad = "Mfull";
public const string TorqueDrag = "Mdrag";
public const string PT1 = "PT1";
}
private class FullLoadCurveEntry
{
public double EngineSpeed { get; set; }
......@@ -37,10 +46,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine
foreach (DataRow row in data.Rows)
{
var entry = new FullLoadCurveEntry();
entry.EngineSpeed = row.GetDouble("n");
entry.TorqueFullLoad = row.GetDouble("Mfull");
entry.TorqueDrag = row.GetDouble("Mdrag");
entry.PT1 = row.GetDouble("PT1");
entry.EngineSpeed = row.GetDouble(Fields.EngineSpeed);
entry.TorqueFullLoad = row.GetDouble(Fields.TorqueFullLoad);
entry.TorqueDrag = row.GetDouble(Fields.TorqueDrag);
entry.PT1 = row.GetDouble(Fields.PT1);
fullLoadCurve.entries.Add(entry);
}
return fullLoadCurve;
......
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Reflection;
namespace TUGraz.VectoCore.Models.SimulationComponent.Data
......@@ -10,12 +12,24 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
public ModalResults()
{
foreach (ModalResultField value in Enum.GetValues(typeof(ModalResultField)))
Columns.Add(value.ToString(), value.GetDataType());
Columns.Add(value.GetName(), value.GetDataType());
}
public DataTable ReadFromFile(string fileName)
public ModalResults ReadFromFile(string fileName)
{
throw new NotImplementedException();
var modalResults = new ModalResults();
var data = VectoCSVReader.Read(fileName);
foreach (DataRow row in data.Rows)
{
var new_row = modalResults.NewRow();
foreach (DataColumn col in row.Table.Columns)
{
new_row.SetField(col, row.Field<object>(col));
}
modalResults.Rows.Add(new_row);
}
modalResults.Load(data.CreateDataReader());
return modalResults;
}
}
......@@ -23,58 +37,58 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
/// Enum with field definitions of the Modal Results File (.vmod).
/// </summary>
public enum ModalResultField
{
[ModalResultFieldAttr(typeof(double))] time, // [s] Time step.
[ModalResultFieldAttr(typeof(double))] dist, // [km] Travelled distance.
[ModalResultFieldAttr(typeof(double))] v_act, // [km/h] Actual vehicle speed.
[ModalResultFieldAttr(typeof(double))] v_targ, // [km/h] Target vehicle speed.
[ModalResultFieldAttr(typeof(double))] acc, // [m/s2] Vehicle acceleration.
[ModalResultFieldAttr(typeof(double))] grad, // [%] Road gradient.
[ModalResultFieldAttr(typeof(double))] n, // [1/min] Engine speed.
[ModalResultFieldAttr(typeof(double))] Tq_eng, // [Nm] Engine torque.
[ModalResultFieldAttr(typeof(double))] Tq_clutch, // [Nm] Torque at clutch (before clutch, engine-side)
[ModalResultFieldAttr(typeof(double))] Tq_full, // [Nm] Full load torque
[ModalResultFieldAttr(typeof(double))] Tq_drag, // [Nm] Motoring torque
[ModalResultFieldAttr(typeof(double))] Pe_eng, // [kW] Engine power.
[ModalResultFieldAttr(typeof(double))] Pe_full, // [kW] Engine full load power.
[ModalResultFieldAttr(typeof(double))] Pe_drag, // [kW] Engine drag power.
[ModalResultFieldAttr(typeof(double))] Pe_clutch, // [kW] Engine power at clutch (equals Pe minus loss due to rotational inertia Pa Eng).
[ModalResultFieldAttr(typeof(double))] Gear, // [-] Gear. "0" = clutch opened / neutral. "0.5" = lock-up clutch is open (AT with torque converter only, see Gearbox)
[ModalResultFieldAttr(typeof(double))] PlossGB, // [kW] Gearbox losses.
[ModalResultFieldAttr(typeof(double))] PlossDiff, // [kW] Losses in differential / axle transmission.
[ModalResultFieldAttr(typeof(double))] PlossRetarder, // [kW] Retarder losses.
[ModalResultFieldAttr(typeof(double))] PaEng, // [kW] Rotational acceleration power: Engine.
[ModalResultFieldAttr(typeof(double))] PaGB, // [kW] Rotational acceleration power: Gearbox.
[ModalResultFieldAttr(typeof(double))] PaVeh, // [kW] Vehicle acceleration power.
[ModalResultFieldAttr(typeof(double))] Proll, // [kW] Rolling resistance power demand.
[ModalResultFieldAttr(typeof(double))] Pair, // [kW] Air resistance power demand.
[ModalResultFieldAttr(typeof(double))] Pgrad, // [kW] Power demand due to road gradient.
[ModalResultFieldAttr(typeof(double))] Paux, // [kW] Total auxiliary power demand .
[ModalResultFieldAttr(typeof(double))] Pwheel, // [kW] Total power demand at wheel = sum of rolling, air, acceleration and road gradient resistance.
[ModalResultFieldAttr(typeof(double))] Pbrake, // [kW] Brake power. Drag power is included in Pe.
[ModalResultFieldAttr(typeof(double))] Paux_xxx, // [kW] Power demand of Auxiliary with ID xxx. See also Aux Dialog and Driving Cycle.
[ModalResultFieldAttr(typeof(double))] FC, // [g/h] Fuel consumption from FC map..
[ModalResultFieldAttr(typeof(double))] FC_AUXc, // [g/h] Fuel consumption after Auxiliary-Start/Stop Correction. (Based on FC.)
[ModalResultFieldAttr(typeof(double))] FC_WHTCc, // [g/h] Fuel consumption after WHTC Correction. (Based on FC-AUXc.)
[ModalResultFieldAttr(typeof(double))] TCν, // [-] Torque converter speed ratio
[ModalResultFieldAttr(typeof(double))] TCmu, // [-] Torque converter torque ratio
[ModalResultFieldAttr(typeof(double))] TC_M_Out, // [Nm] Torque converter output torque
[ModalResultFieldAttr(typeof(double))] TC_n_Out, // [1/min] Torque converter output speed
}
{
[ModalResultField(typeof(double))] time, // [s] Time step.
[ModalResultField(typeof(double))] n, // [1/min] Engine speed.
[ModalResultField(typeof(double))] Tq_eng, // [Nm] Engine torque.
[ModalResultField(typeof(double))] Tq_clutch, // [Nm] Torque at clutch (before clutch, engine-side)
[ModalResultField(typeof(double))] Tq_full, // [Nm] Full load torque
[ModalResultField(typeof(double))] Tq_drag, // [Nm] Motoring torque
[ModalResultField(typeof(double))] Pe_eng, // [kW] Engine power.
[ModalResultField(typeof(double))] Pe_full, // [kW] Engine full load power.
[ModalResultField(typeof(double))] Pe_drag, // [kW] Engine drag power.
[ModalResultField(typeof(double))] Pe_clutch, // [kW] Engine power at clutch (equals Pe minus loss due to rotational inertia Pa Eng).
[ModalResultField(typeof(double), "Pa")] PaEng, // [kW] Rotational acceleration power: Engine.
[ModalResultField(typeof(double))] Paux, // [kW] Total auxiliary power demand .
[ModalResultField(typeof(double))] FC, // [g/h] Fuel consumption from FC map..
[ModalResultField(typeof(double), "FC-AUXc")] FCAUXc, // [g/h] Fuel consumption after Auxiliary-Start/Stop Correction. (Based on FC.)
[ModalResultField(typeof(double), "FC-WHTCc")] FCWHTCc, // [g/h] Fuel consumption after WHTC Correction. (Based on FC-AUXc.)
[ModalResultField(typeof(double))] dist, // [km] Travelled distance.
[ModalResultField(typeof(double))] v_act, // [km/h] Actual vehicle speed.
[ModalResultField(typeof(double))] v_targ, // [km/h] Target vehicle speed.
[ModalResultField(typeof(double))] acc, // [m/s2] Vehicle acceleration.
[ModalResultField(typeof(double))] grad, // [%] Road gradient.
[ModalResultField(typeof(double))] Gear, // [-] Gear. "0" = clutch opened / neutral. "0.5" = lock-up clutch is open (AT with torque converter only, see Gearbox)
[ModalResultField(typeof(double), "Ploss GB")] PlossGB, // [kW] Gearbox losses.
[ModalResultField(typeof(double), "Ploss Diff")] PlossDiff, // [kW] Losses in differential / axle transmission.
[ModalResultField(typeof(double), "Ploss Retarder")] PlossRetarder, // [kW] Retarder losses.
[ModalResultField(typeof(double), "Pa GB")] PaGB, // [kW] Rotational acceleration power: Gearbox.
[ModalResultField(typeof(double), "Pa Veh")] PaVeh, // [kW] Vehicle acceleration power.
[ModalResultField(typeof(double))] Proll, // [kW] Rolling resistance power demand.
[ModalResultField(typeof(double))] Pair, // [kW] Air resistance power demand.
[ModalResultField(typeof(double))] Pgrad, // [kW] Power demand due to road gradient.
[ModalResultField(typeof(double))] Pwheel, // [kW] Total power demand at wheel = sum of rolling, air, acceleration and road gradient resistance.
[ModalResultField(typeof(double))] Pbrake, // [kW] Brake power. Drag power is included in Pe.
//[ModalResultField(typeof(double))] Paux_xxx, // [kW] Power demand of Auxiliary with ID xxx. See also Aux Dialog and Driving Cycle.
[ModalResultField(typeof(double))] TCν, // [-] Torque converter speed ratio
[ModalResultField(typeof(double), "TCµ")] TCmu, // [-] Torque converter torque ratio
[ModalResultField(typeof(double))] TC_M_Out, // [Nm] Torque converter output torque
[ModalResultField(typeof(double))] TC_n_Out, // [1/min] Torque converter output speed
}
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
class ModalResultFieldAttr : Attribute
[AttributeUsage(AttributeTargets.Field)]
class ModalResultFieldAttribute : Attribute
{
internal ModalResultFieldAttr(Type fieldType)
internal ModalResultFieldAttribute(Type fieldType, string name=null)
{
this.FieldType = fieldType;
FieldType = fieldType;
Name = name;
}
public Type FieldType { get; private set; }
public string Name { get; private set; }
}
public static class ModalResultFieldExtensions
......@@ -84,9 +98,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data
return GetAttr(field).FieldType;
}
private static ModalResultFieldAttr GetAttr(ModalResultField field)
public static string GetName(this ModalResultField field)
{
return GetAttr(field).Name ?? field.ToString();
}
private static ModalResultFieldAttribute GetAttr(ModalResultField field)
{
return (ModalResultFieldAttr)Attribute.GetCustomAttribute(ForValue(field), typeof (ModalResultFieldAttr));
return (ModalResultFieldAttribute)Attribute.GetCustomAttribute(ForValue(field), typeof (ModalResultFieldAttribute));
}
private static MemberInfo ForValue(ModalResultField field)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment