Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Select Git revision
  • 3eadad401920e6faf8108ca2a97bb7d1d8509f06
  • stable default
  • feat-fchv-bus
  • fix-h2-ice-bus
  • powertrains-multiple-axles
  • amdm3/develop
  • issue-1039
  • amdm3/main
  • test/nuget_publish
  • IEPC-experiments
  • amdm2/main
  • amdm2/develop
  • aptngearbox-not-auto
  • playground
  • official/main
  • official/develop
  • issue-templates
  • pdf-reports
  • HEV-timeruns-dev
  • timerun-empower-hybrids
  • timerun-pwheel-hybrids
  • Release/v5.0.3
  • Release/v5.0.1
  • Release/5.0.0-RC
  • Nuget/v0.11.4-DEV
  • Release/v0.11.4-DEV
  • Release/4.3.4-DEV
  • Release/4.3.3
  • Release/4.3.2-RC
  • Release/v4.3.0-DEV
  • Release/4.2.7
  • XMLConverterTool/4.2.6.0
  • Release/4.2.6-RC
  • Release/v4.2.5
  • Release/v4.2.3
  • Release/v4.2.2.3539-RC
  • Release/v4.2.1.3469
  • Release/v0.11.2.3456-DEV
  • Release/v4.2.0.3448-RC
  • Release/v4.1.3.3415
  • Release/v4.1.1.3413
41 results

CombustionEngineData.cs

Blame
  • Forked from VECTO / VECTO Sim
    11811 commits behind the upstream repository.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    CombustionEngineData.cs 7.03 KiB
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Newtonsoft.Json;
    using TUGraz.VectoCore.Exceptions;
    using TUGraz.VectoCore.Models.SimulationComponent.Data.Engine;
    
    namespace TUGraz.VectoCore.Models.SimulationComponent.Data
    {
        /// <summary>
        /// Represents the CombustionEngineData. Fileformat: .veng
        /// </summary>
        /// <code>
        /// {
        ///  "Header": {
        ///    "CreatedBy": " ()",
        ///    "Date": "3/4/2015 12:26:24 PM",
        ///    "AppVersion": "2.0.4-beta3",
        ///    "FileVersion": 2
        ///  },
        ///  "Body": {
        ///    "SavedInDeclMode": false,
        ///    "ModelName": "Generic 24t Coach",
        ///    "Displacement": 12730.0,
        ///    "IdlingSpeed": 560.0,
        ///    "Inertia": 3.8,
        ///    "FullLoadCurves": [
        ///      {
        ///        "Path": "24t Coach.vfld",
        ///        "Gears": "0 - 99"
        ///      }
        ///    ],
        ///    "FuelMap": "24t Coach.vmap",
        ///    "WHTC-Urban": 0.0,
        ///    "WHTC-Rural": 0.0,
        ///    "WHTC-Motorway": 0.0
        ///  }
        /// }
        /// </code>
        public class CombustionEngineData : SimulationComponentData
        {
            /// <summary>
            /// A class which represents the json data format for serializing and deserializing the EngineData files.
            /// </summary>
            public class Data
            {
                public class DataHeader
                {
                    [JsonProperty(Required = Required.Always)]
                    public string CreatedBy;
    
                    [JsonProperty(Required = Required.Always)]
                    public DateTime Date;
    
                    [JsonProperty(Required = Required.Always)]
                    public string AppVersion;
    
                    [JsonProperty(Required = Required.Always)]
                    public double FileVersion;
                }
    
                [JsonProperty(Required = Required.Always)]
                public DataHeader Header;
    
                public class DataBody
                {
                    public class DataFullLoadCurve
                    {
                        [JsonProperty(Required = Required.Always)]
                        public string Gears;
    
                        [JsonProperty(Required = Required.Always)]
                        public string Path;
                    }
                    
                    [JsonProperty(Required = Required.Always)]
                    public IList<DataFullLoadCurve> FullLoadCurves;
    
                    [JsonProperty("SavedInDeclMode")]
                    public bool SavedInDeclarationMode;
    
                    [JsonProperty(Required = Required.Always)]
                    public string ModelName;
    
                    [JsonProperty(Required = Required.Always)]
                    public double Displacement;
    
                    [JsonProperty("IdlingSpeed", Required = Required.Always)]
                    public double IdleSpeed;
    
                    [JsonProperty(Required = Required.Always)]
                    public double Inertia;
    
                    [JsonProperty(Required = Required.Always)]
                    public string FuelMap;
    
                    [JsonProperty("WHTC-Urban")]
                    public double WHTCUrban;
    
                    [JsonProperty("WHTC-Rural")]
                    public double WHTCRural;
    
                    [JsonProperty("WHTC-Motorway")]
                    public double WHTCMotorway;
                }
    
                [JsonProperty(Required = Required.Always)]
                public DataBody Body;
            }
    
            private Data _data;
    
            public bool SavedInDeclarationMode
            {
                get { return _data.Body.SavedInDeclarationMode; }
                protected set { _data.Body.SavedInDeclarationMode = value; }
            }
    
            public string ModelName
            {
                get { return _data.Body.ModelName; }
                protected set { _data.Body.ModelName = value; }
            }
    
            public double Displacement
            {
                get { return _data.Body.Displacement; }
                protected set { _data.Body.Displacement = value; }
            }
    
            public double IdleSpeed
            {
                get { return _data.Body.IdleSpeed; }
                protected set { _data.Body.IdleSpeed = value; }
            }
    
            public double Inertia
            {
                get { return _data.Body.Inertia; }
                protected set { _data.Body.Inertia = value; }
            }
    
            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 gearRange in _fullLoadCurves.Keys)
                {
                    var low = uint.Parse(gearRange.Split('-').First().Trim());
                    if (low <= gear)
                    {
                        var high = uint.Parse(gearRange.Split('-').Last().Trim());
                        if (high >= gear)
                            return _fullLoadCurves[gearRange];
                    }
                }
                throw new KeyNotFoundException(string.Format("Gear '{0}' was not found in the FullLoadCurves.", gear));
            }
        }
    }