From 1956ef1c6072fd125a001df6634e69016df007b5 Mon Sep 17 00:00:00 2001 From: Michael Krisper <michael.krisper@tugraz.at> Date: Thu, 14 Jan 2016 16:04:30 +0100 Subject: [PATCH] refactored some magic numbers for unit conversion in validations into Constants with names --- VectoCore/Configuration/Constants.cs | 6 ++++++ VectoCore/Models/Simulation/Data/VectoRunData.cs | 11 +++++------ VectoCore/Models/Simulation/Impl/SimulatorFactory.cs | 2 +- .../SimulationComponent/Data/CombustionEngineData.cs | 11 ++++++----- .../Data/Engine/FuelConsumptionMap.cs | 3 ++- .../Models/SimulationComponent/Data/FullLoadCurve.cs | 7 ++++--- VectoCoreTest/Integration/DeclarationReportTest.cs | 2 -- .../Models/SimulationComponentData/ValidationTest.cs | 8 ++++---- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/VectoCore/Configuration/Constants.cs b/VectoCore/Configuration/Constants.cs index a62d67d249..d0a3a2ad88 100644 --- a/VectoCore/Configuration/Constants.cs +++ b/VectoCore/Configuration/Constants.cs @@ -14,12 +14,18 @@ * limitations under the Licence. */ +using System; using TUGraz.VectoCore.Utils; namespace TUGraz.VectoCore.Configuration { public static class Constants { + public const double RPMToRad = 2 * Math.PI / 60; + public const double Kilo = 1000; + public const double MeterPerSecondToKMH = 3.6; + public const double SecondsPerHour = 3600; + public static class Auxiliaries { public static class IDs diff --git a/VectoCore/Models/Simulation/Data/VectoRunData.cs b/VectoCore/Models/Simulation/Data/VectoRunData.cs index a4c191d80c..635b14c726 100644 --- a/VectoCore/Models/Simulation/Data/VectoRunData.cs +++ b/VectoCore/Models/Simulation/Data/VectoRunData.cs @@ -17,6 +17,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Runtime.Serialization; +using TUGraz.VectoCore.Configuration; using TUGraz.VectoCore.Exceptions; using TUGraz.VectoCore.Models.Declaration; using TUGraz.VectoCore.Models.SimulationComponent.Data; @@ -58,7 +59,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Data public bool IsEngineOnly { get; internal set; } - [Required] + [Required, MinLength(1)] public string JobName { get; set; } [Required] @@ -79,13 +80,11 @@ namespace TUGraz.VectoCore.Models.Simulation.Data [Required] public AuxiliaryType Type; - [Path] public string Path; - public string Technology; [Required] public string[] TechList; - [Required, SIRange(0, 100 * 1000)] public Watt PowerDemand; + [Required, SIRange(0, 100 * Constants.Kilo)] public Watt PowerDemand; [Required] public AuxiliaryDemandType DemandType; @@ -95,7 +94,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Data public class StartStopData { public bool Enabled; - [Required, SIRange(0, 120 / 3.6)] public MeterPerSecond MaxSpeed; + [Required, SIRange(0, 120 / Constants.MeterPerSecondToKMH)] public MeterPerSecond MaxSpeed; [Required, SIRange(0, 100)] public Second MinTime; [Required, SIRange(0, 100)] public Second Delay; } @@ -110,7 +109,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Data foreach (var gear in gearboxData.Gears) { for (var angularVelocity = engineData.IdleSpeed; angularVelocity < engineData.FullLoadCurve.RatedSpeed; - angularVelocity += 2.0 / 3.0 * (engineData.FullLoadCurve.RatedSpeed - engineData.IdleSpeed) / 10.09) { + angularVelocity += 2.0 / 3.0 * (engineData.FullLoadCurve.RatedSpeed - engineData.IdleSpeed) / 10.0) { for (var inTorque = engineData.FullLoadCurve.FullLoadStationaryTorque(angularVelocity) / 3; inTorque < engineData.FullLoadCurve.FullLoadStationaryTorque(angularVelocity); inTorque += 2.0 / 3.0 * engineData.FullLoadCurve.FullLoadStationaryTorque(angularVelocity) / 10.0) { diff --git a/VectoCore/Models/Simulation/Impl/SimulatorFactory.cs b/VectoCore/Models/Simulation/Impl/SimulatorFactory.cs index 1f1ae655a9..53f2df9776 100644 --- a/VectoCore/Models/Simulation/Impl/SimulatorFactory.cs +++ b/VectoCore/Models/Simulation/Impl/SimulatorFactory.cs @@ -111,7 +111,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl run = new DistanceRun(builder.Build(data)); } - var validationErrors = ValidationHelper.Validate(run); + var validationErrors = run.Validate(); if (validationErrors.Any()) { throw new VectoException("Validation of Run-Data Failed: " + string.Join("; ", validationErrors.Select(r => r.ErrorMessage))); diff --git a/VectoCore/Models/SimulationComponent/Data/CombustionEngineData.cs b/VectoCore/Models/SimulationComponent/Data/CombustionEngineData.cs index ed186075cb..ba26f053be 100644 --- a/VectoCore/Models/SimulationComponent/Data/CombustionEngineData.cs +++ b/VectoCore/Models/SimulationComponent/Data/CombustionEngineData.cs @@ -16,6 +16,7 @@ using System; using System.ComponentModel.DataAnnotations; +using TUGraz.VectoCore.Configuration; using TUGraz.VectoCore.Models.SimulationComponent.Data.Engine; using TUGraz.VectoCore.Utils; @@ -25,22 +26,22 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data { public string ModelName { get; internal set; } - [Required, SIRange(1000 * 1e-6, 20000 * 1e-6)] + [Required, SIRange(1000 / (Constants.Kilo * Constants.Kilo), 20000 / (Constants.Kilo * Constants.Kilo))] public CubicMeter Displacement { get; internal set; } - [Required, SIRange(400 * 2 * Math.PI / 60, 1000 * 2 * Math.PI / 60)] + [Required, SIRange(400 * Constants.RPMToRad, 1000 * Constants.RPMToRad)] public PerSecond IdleSpeed { get; internal set; } [Required, SIRange(0, 10)] public KilogramSquareMeter Inertia { get; internal set; } - [Required, SIRange(double.Epsilon, 1000 / (1000.0 * 1000 * 3600))] + [Required, SIRange(double.Epsilon, 1000 / (Constants.Kilo * Constants.Kilo * Constants.SecondsPerHour))] public KilogramPerWattSecond WHTCUrban { get; internal set; } - [Required, SIRange(double.Epsilon, 1000 / (1000.0 * 1000 * 3600))] + [Required, SIRange(double.Epsilon, 1000 / (Constants.Kilo * Constants.Kilo * Constants.SecondsPerHour))] public KilogramPerWattSecond WHTCRural { get; internal set; } - [Required, SIRange(double.Epsilon, 1000 / (1000.0 * 1000 * 3600))] + [Required, SIRange(double.Epsilon, 1000 / (Constants.Kilo * Constants.Kilo * Constants.SecondsPerHour))] public KilogramPerWattSecond WHTCMotorway { get; internal set; } [Required, ValidateObject] diff --git a/VectoCore/Models/SimulationComponent/Data/Engine/FuelConsumptionMap.cs b/VectoCore/Models/SimulationComponent/Data/Engine/FuelConsumptionMap.cs index 3292bb6425..265365eb54 100644 --- a/VectoCore/Models/SimulationComponent/Data/Engine/FuelConsumptionMap.cs +++ b/VectoCore/Models/SimulationComponent/Data/Engine/FuelConsumptionMap.cs @@ -20,6 +20,7 @@ using System.ComponentModel.DataAnnotations; using System.Data; using System.Diagnostics.Contracts; using System.Linq; +using TUGraz.VectoCore.Configuration; using TUGraz.VectoCore.Exceptions; using TUGraz.VectoCore.Utils; @@ -144,7 +145,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data.Engine private class FuelConsumptionEntry { - [Required, SIRange(0, 5000 * 2 * Math.PI / 60)] + [Required, SIRange(0, 5000 * Constants.RPMToRad)] public PerSecond EngineSpeed { get; set; } [Required] diff --git a/VectoCore/Models/SimulationComponent/Data/FullLoadCurve.cs b/VectoCore/Models/SimulationComponent/Data/FullLoadCurve.cs index c0f3744372..40832e2e42 100644 --- a/VectoCore/Models/SimulationComponent/Data/FullLoadCurve.cs +++ b/VectoCore/Models/SimulationComponent/Data/FullLoadCurve.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data; using System.Linq; +using TUGraz.VectoCore.Configuration; using TUGraz.VectoCore.Exceptions; using TUGraz.VectoCore.Models.Declaration; using TUGraz.VectoCore.Models.SimulationComponent.Data.Engine; @@ -37,7 +38,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data /// <summary> /// Get the rated speed from the given full-load curve (i.e. speed with max. power) /// </summary> - [Required, SIRange(0, 5000 * 2 * Math.PI / 60)] + [Required, SIRange(0, 5000 * Constants.RPMToRad)] public PerSecond RatedSpeed { get { return _ratedSpeed ?? ComputeRatedSpeed().Item1; } @@ -46,7 +47,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data /// <summary> /// Gets the maximum power. /// </summary> - [Required, SIRange(0, 10000 * 5000 * 2 * Math.PI / 60)] + [Required, SIRange(0, 10000 * 5000 * Constants.RPMToRad)] public Watt MaxPower { get { return _maxPower ?? ComputeRatedSpeed().Item2; } @@ -202,7 +203,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data internal class FullLoadCurveEntry { - [Required, SIRange(0, 5000 * 2 * Math.PI / 60)] + [Required, SIRange(0, 5000 * Constants.RPMToRad)] public PerSecond EngineSpeed { get; set; } [Required, SIRange(0, 10000)] diff --git a/VectoCoreTest/Integration/DeclarationReportTest.cs b/VectoCoreTest/Integration/DeclarationReportTest.cs index 9f3aa141a9..51f735a08c 100644 --- a/VectoCoreTest/Integration/DeclarationReportTest.cs +++ b/VectoCoreTest/Integration/DeclarationReportTest.cs @@ -15,10 +15,8 @@ */ using System.IO; -using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; using TUGraz.VectoCore.InputData.FileIO.JSON; -using TUGraz.VectoCore.Models.Simulation.Data; using TUGraz.VectoCore.Models.Simulation.Impl; using TUGraz.VectoCore.OutputData; using TUGraz.VectoCore.OutputData.FileIO; diff --git a/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs b/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs index 1b337d4ac8..b3c09ba2db 100644 --- a/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs +++ b/VectoCoreTest/Models/SimulationComponentData/ValidationTest.cs @@ -38,12 +38,12 @@ namespace TUGraz.VectoCore.Tests.Models.SimulationComponentData var data = new CombustionEngineData { ModelName = "asdf", - Displacement = 0.005.SI<CubicMeter>(), + Displacement = 5.SI().Cubic.Centi.Meter.Cast<CubicMeter>(), IdleSpeed = 560.RPMtoRad(), Inertia = 1.SI<KilogramSquareMeter>(), - WHTCUrban = 1.SI<KilogramPerWattSecond>() * 3.6e-9, - WHTCRural = 1.SI<KilogramPerWattSecond>() * 3.6e-9, - WHTCMotorway = 1.SI<KilogramPerWattSecond>() * 3.6e-9, + WHTCUrban = 1.SI().Gramm.Per.Kilo.Watt.Hour.Cast<KilogramPerWattSecond>(), + WHTCRural = 1.SI().Gramm.Per.Kilo.Watt.Hour.Cast<KilogramPerWattSecond>(), + WHTCMotorway = 1.SI().Gramm.Per.Kilo.Watt.Hour.Cast<KilogramPerWattSecond>(), FullLoadCurve = EngineFullLoadCurve.Create(fullLoad), ConsumptionMap = FuelConsumptionMap.Create(fuelConsumption) }; -- GitLab