diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/TimeRun.cs b/VectoCore/VectoCore/Models/Simulation/Impl/TimeRun.cs index f5aa88bef0053f8c0aac802b3bfbea4540ec07a7..47f210f5946281859960e8c99cea999037bed04f 100644 --- a/VectoCore/VectoCore/Models/Simulation/Impl/TimeRun.cs +++ b/VectoCore/VectoCore/Models/Simulation/Impl/TimeRun.cs @@ -39,7 +39,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl { public class TimeRun : VectoRun { - public TimeRun(IVehicleContainer container) : base(container) {} + public TimeRun(IVehicleContainer container) : base(container) { } protected override IResponse DoSimulationStep() { @@ -51,15 +51,20 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl do { response = CyclePort.Request(AbsTime, dt); debug.Add(response); - - response.Switch(). - Case<ResponseSuccess>(r => { dt = r.SimulationInterval; }). - Case<ResponseFailTimeInterval>(r => { dt = r.DeltaT; }). - Case<ResponseCycleFinished>(r => { + switch (response) { + case ResponseSuccess r: + dt = r.SimulationInterval; + break; + case ResponseFailTimeInterval r: + dt = r.DeltaT; + break; + case ResponseCycleFinished r: FinishedWithoutErrors = true; Log.Info("========= Driving Cycle Finished"); - }). - Default(r => { throw new VectoException("TimeRun got an unexpected response: {0}", r); }); + break; + default: + throw new VectoException("TimeRun got an unexpected response: {0}", response); + } if (loopCount++ > Constants.SimulationSettings.MaximumIterationCountForSimulationStep) { throw new VectoSimulationException("Maximum iteration count for a single simulation interval reached! Aborting!"); } diff --git a/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs b/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs index bd914050011c15eb7f0527706d477cb7d3d7f238..b5404afbcfeb8839d5720b4bdb73179bc11694ad 100644 --- a/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs +++ b/VectoCore/VectoCore/Models/Simulation/Impl/VehicleContainer.cs @@ -98,7 +98,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl internal readonly Dictionary<PowertrainPosition, IElectricMotorInfo> ElectricMotors = new Dictionary<PowertrainPosition, IElectricMotorInfo>(); - + public VehicleContainer(ExecutionMode executionMode, IModalDataContainer modData = null, WriteSumData writeSumData = null) { @@ -123,7 +123,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl public virtual Second AbsTime { get; set; } public IElectricMotorInfo ElectricMotorInfo(PowertrainPosition pos) { - return ElectricMotors.ContainsKey(pos) ? ElectricMotors[pos] : null; + return ElectricMotors.ContainsKey(pos) ? ElectricMotors[pos] : null; } @@ -143,68 +143,65 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl get { return HybridController; } } - + public virtual void AddComponent(VectoSimulationComponent component) { var commitPriority = 0; - var ignoreComponent = false; - component.Switch() - .If<IEngineInfo>(c => { - EngineInfo = c; - commitPriority = 2; - HasCombustionEngine = true; - }) - .If<IEngineControl>(c => { EngineCtl = c; }) - .If<IDriverInfo>(c => DriverInfo = c) - .If<IGearboxInfo>(c => { - GearboxInfo = c; - commitPriority = 4; - HasGearbox = true; - }) - .If<IGearboxControl>(c => GearboxCtl = c) - .If<ITorqueConverterInfo>(c => TorqueConverterInfo = c) - .If<ITorqueConverterControl>(c => TorqueConverterCtl = c) - .If<IAxlegearInfo>(c => AxlegearInfo = c) - .If<IAngledriveInfo>(c => AngledriveInfo = c) - .If<IWheelsInfo>(c => WheelsInfo = c) - .If<IVehicleInfo>(c => { - VehicleInfo = c; - commitPriority = 5; - }) - .If<ISimulationOutPort>(c => Cycle = c) - .If<IMileageCounter>(c => MileageCounter = c) - .If<IBrakes>(c => Brakes = c) - .If<IClutchInfo>(c => ClutchInfo = c) - .If<IDrivingCycleInfo>(c => { - DrivingCycleInfo = c; - commitPriority = 6; - }) - .If<PTOCycleController>(c => { commitPriority = 99; }) - .If<VTPCycle>(_ => { commitPriority = 0; }) - .If<IElectricMotorInfo>(c => { - if (c.Position == PowertrainPosition.HybridPositionNotSet) { - ignoreComponent = true; - return; - } - if (ElectricMotors.ContainsKey(c.Position)) { - throw new VectoException("There is already an electric machine at position {0}", - c.Position); - } - - ElectricMotors[c.Position] = c; - HasElectricMotor = true; - }) - .If<IHybridController>(c => { HybridController = c; }) - .If<IRESSInfo>(c => BatteryInfo = c) - .If<BusAuxiliariesAdapter>(c => BusAux = c) - .If<IDCDCConverter>(c => DCDCConverter = c); - - - if (ignoreComponent) { - return; + + if (component is IEngineControl c1) { EngineCtl = c1; } + if (component is IDriverInfo c2) { DriverInfo = c2; } + if (component is IGearboxControl c3) { GearboxCtl = c3; } + if (component is ITorqueConverterInfo c4) { TorqueConverterInfo = c4; } + if (component is ITorqueConverterControl c5) { TorqueConverterCtl = c5; } + if (component is IAxlegearInfo c6) { AxlegearInfo = c6; } + if (component is IAngledriveInfo c7) { AngledriveInfo = c7; } + if (component is IWheelsInfo c8) { WheelsInfo = c8; } + if (component is ISimulationOutPort c9) { Cycle = c9; } + if (component is IMileageCounter c10) { MileageCounter = c10; } + if (component is IBrakes c11) { Brakes = c11; } + if (component is IClutchInfo c12) { ClutchInfo = c12; } + if (component is IHybridController c13) { HybridController = c13; } + if (component is IRESSInfo c14) { BatteryInfo = c14; } + if (component is BusAuxiliariesAdapter c15) { BusAux = c15; } + if (component is IDCDCConverter c16) { DCDCConverter = c16; } + + if (component is IEngineInfo c17){ + EngineInfo = c17; + commitPriority = 2; + HasCombustionEngine = true; } + if (component is IGearboxInfo c18) { + GearboxInfo = c18; + commitPriority = 4; + HasGearbox = true; + } + + if (component is IVehicleInfo c19) { + VehicleInfo = c19; + commitPriority = 5; + } + + if (component is IDrivingCycleInfo c20) { + DrivingCycleInfo = c20; + commitPriority = 6; + } + if (component is PTOCycleController c21) { commitPriority = 99; } + if (component is VTPCycle c22) { commitPriority = 0; } + if (component is IElectricMotorInfo c23) { + if (c23.Position == PowertrainPosition.HybridPositionNotSet) { + return; + } + if (ElectricMotors.ContainsKey(c23.Position)) { + throw new VectoException("There is already an electric machine at position {0}", c23.Position); + } + + ElectricMotors[c23.Position] = c23; + HasElectricMotor = true; + } + _components.Add(Tuple.Create(commitPriority, component)); + //todo mk20210617 use sorted list with inverse commitPriority (-commitPriority) _components = _components.OrderBy(x => x.Item1).Reverse().ToList(); } @@ -274,12 +271,12 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl public virtual bool HasGearbox { get; private set; } - + public virtual VectoRunData RunData { get; set; } public virtual ExecutionMode ExecutionMode { get; } - + } public class ExemptedRunContainer : VehicleContainer @@ -303,7 +300,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl public override IMileageCounter MileageCounter { get { return _mileageCounter; } - + } #endregion diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs index b2c924b55a59308c3599a05814f6f2d1588e324d..6b62c5d065916c9508fc668b2f1a8138aca964c8 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/MeasuredSpeedDrivingCycle.cs @@ -140,7 +140,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl // reduce the current simulation interval to extend the remaining interval return new ResponseFailTimeInterval(this) { AbsTime = absTime, - DeltaT = (CycleIterator.RightSample.Time - absTime)/2 + DeltaT = (CycleIterator.RightSample.Time - absTime) / 2 }; } } @@ -176,33 +176,36 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl do { response = NextComponent.Request(absTime, dt, acceleration, gradient, false); debug.Add(response); - response.Switch() - .Case<ResponseGearShift>(() => response = NextComponent.Request(absTime, dt, acceleration, gradient, false)) - .Case<ResponseUnderload>(r => { + + switch (response) { + case ResponseGearShift _: + response = NextComponent.Request(absTime, dt, acceleration, gradient, false); + break; + case ResponseUnderload r: response = HandleUnderload(absTime, dt, r, gradient, ref acceleration); - }) - .Case<ResponseOverload>(r => { + break; + case ResponseOverload r: response = HandleOverload(absTime, dt, r, gradient, ref acceleration); - }) - .Case<ResponseEngineSpeedTooHigh>(r => { + break; + case ResponseEngineSpeedTooHigh r: acceleration = SearchAlgorithm.Search(acceleration, r.DeltaEngineSpeed, Constants.SimulationSettings.OperatingPointInitialSearchIntervalAccelerating, getYValue: result => ((ResponseDryRun)result).DeltaEngineSpeed, + // ReSharper disable once AccessToModifiedClosure evaluateFunction: x => NextComponent.Request(absTime, dt, x, gradient, true), - criterion: - y => ((ResponseDryRun)y).DeltaEngineSpeed.Value()); - Log.Info( - "Found operating point for driver acceleration. absTime: {0}, dt: {1}, acceleration: {2}, gradient: {3}", + criterion: y => ((ResponseDryRun)y).DeltaEngineSpeed.Value()); + Log.Info("Found operating point for driver acceleration. absTime: {0}, dt: {1}, acceleration: {2}, gradient: {3}", absTime, dt, acceleration, gradient); - }) - .Case<ResponseFailTimeInterval>(r => { + break; + case ResponseFailTimeInterval r: dt = r.DeltaT; - }) - .Case<ResponseSuccess>() - .Default( - r => { - throw new UnexpectedResponseException("MeasuredSpeedDrivingCycle received an unexpected response.", r); - }); + break; + case ResponseSuccess _: + break; + default: + throw new UnexpectedResponseException("MeasuredSpeedDrivingCycle received an unexpected response.", response); + } + } while (!(response is ResponseSuccess || response is ResponseFailTimeInterval) && (++responseCount < 10)); AbsTime = absTime + dt; @@ -261,7 +264,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl private IResponse HandleOverload(Second absTime, Second dt, ResponseOverload r, Radian gradient, ref MeterPerSquareSecond acceleration) { - IResponse response; if (DataBus.ClutchInfo.ClutchClosed(absTime)) { acceleration = SearchAlgorithm.Search(acceleration, r.Delta, Constants.SimulationSettings.OperatingPointInitialSearchIntervalAccelerating, @@ -301,7 +303,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl criterion: y => ((ResponseDryRun)y).DeltaFullLoad.Value()); } } - response = NextComponent.Request(absTime, dt, acceleration, gradient, false); + var response = NextComponent.Request(absTime, dt, acceleration, gradient, false); return response; } @@ -323,28 +325,17 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl AdvanceState(); } - public double Progress - { - get { return AbsTime == null ? 0 : AbsTime.Value() / Data.Entries.Last().Time.Value(); } - } + public double Progress => AbsTime == null ? 0 : AbsTime.Value() / Data.Entries.Last().Time.Value(); - public CycleData CycleData - { - get - { - return new CycleData { - AbsTime = CycleIterator.LeftSample.Time, - AbsDistance = null, - LeftSample = CycleIterator.LeftSample, - RightSample = CycleIterator.RightSample, - }; - } - } + public CycleData CycleData => + new CycleData { + AbsTime = CycleIterator.LeftSample.Time, + AbsDistance = null, + LeftSample = CycleIterator.LeftSample, + RightSample = CycleIterator.RightSample, + }; - public bool PTOActive - { - get { return false; } - } + public bool PTOActive => false; public DrivingCycleData.DrivingCycleEntry CycleLookAhead(Meter distance) { @@ -352,25 +343,15 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl //throw new System.NotImplementedException(); } - public Meter Altitude - { - get { return CycleIterator.LeftSample.Altitude; } - } + public Meter Altitude => CycleIterator.LeftSample.Altitude; - public Radian RoadGradient { get { return CycleIterator.LeftSample.RoadGradient; } } - public MeterPerSecond TargetSpeed - { - get { return CycleIterator.LeftSample.VehicleTargetSpeed; } - } - public Second StopTime - { - get { return CycleIterator.LeftSample.StoppingTime; } - } + public Radian RoadGradient => CycleIterator.LeftSample.RoadGradient; - public Meter CycleStartDistance - { - get { return 0.SI<Meter>(); } - } + public MeterPerSecond TargetSpeed => CycleIterator.LeftSample.VehicleTargetSpeed; + + public Second StopTime => CycleIterator.LeftSample.StoppingTime; + + public Meter CycleStartDistance => 0.SI<Meter>(); public IReadOnlyList<DrivingCycleData.DrivingCycleEntry> LookAhead(Meter lookaheadDistance) { @@ -389,25 +370,16 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl return retVal; } - public SpeedChangeEntry LastTargetspeedChange { get { return null; } } + public SpeedChangeEntry LastTargetspeedChange => null; - public void FinishSimulation() - { - Data.Finish(); - } + public void FinishSimulation() => Data.Finish(); public DrivingBehavior DriverBehavior { get; internal set; } - public DrivingAction DrivingAction - { - get { return DrivingAction.Accelerate; } - } + public DrivingAction DrivingAction => DrivingAction.Accelerate; public MeterPerSquareSecond DriverAcceleration { get; protected set; } - public Meter Distance - { - get { return CurrentState.Distance; } - } + public Meter Distance => CurrentState.Distance; } } \ No newline at end of file diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs index 4841b37ea40749e5a87d7579526c3b021c912466..1c8ee412bba22725e1a01068789efb6c0db3d9c3 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/PowertrainDrivingCycle.cs @@ -118,10 +118,11 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl CurrentState.InAngularVelocity = angularVelocity; CurrentState.InTorque = CycleIterator.LeftSample.Torque; debug.Add(response); - response.Switch() - .Case<ResponseGearShift>( - () => response = NextComponent.Request(absTime, dt, CurrentState.InTorque, angularVelocity, false)) - .Case<ResponseUnderload>(r => { + switch (response) { + case ResponseGearShift _: + response = NextComponent.Request(absTime, dt, CurrentState.InTorque, angularVelocity, false); + break; + case ResponseUnderload r: var torqueInterval = -r.Delta / (angularVelocity.IsEqual(0) ? 10.RPMtoRad() : angularVelocity); var torque = SearchAlgorithm.Search(CycleIterator.LeftSample.Torque, r.Delta, torqueInterval, getYValue: result => ((ResponseDryRun)result).DeltaDragLoad, @@ -129,31 +130,31 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl criterion: y => ((ResponseDryRun)y).DeltaDragLoad.Value()); response = NextComponent.Request(absTime, dt, torque, angularVelocity, false); CurrentState.InTorque = torque; - }) - .Case<ResponseOverload>(r => { - var torque = SearchAlgorithm.Search(CycleIterator.LeftSample.Torque, r.Delta, - 50.SI<NewtonMeter>(), + break; + case ResponseOverload r: + var torque2 = SearchAlgorithm.Search(CycleIterator.LeftSample.Torque, r.Delta, 50.SI<NewtonMeter>(), getYValue: result => ((ResponseDryRun)result).DeltaFullLoad, evaluateFunction: t => NextComponent.Request(absTime, dt, t, angularVelocity, true), criterion: y => ((ResponseDryRun)y).DeltaFullLoad.Value()); - response = NextComponent.Request(absTime, dt, torque, angularVelocity, false); + response = NextComponent.Request(absTime, dt, torque2, angularVelocity, false); CurrentState.InAngularVelocity = angularVelocity; - CurrentState.InTorque = torque; - }) - .Case<ResponseEngineSpeedTooHigh>(r => { + CurrentState.InTorque = torque2; + break; + case ResponseEngineSpeedTooHigh r: angularVelocity = SearchAlgorithm.Search(angularVelocity, r.DeltaEngineSpeed, 1.RPMtoRad(), getYValue: result => ((ResponseDryRun)result).DeltaEngineSpeed, evaluateFunction: x => NextComponent.Request(absTime, dt, CurrentState.InTorque, x, true), criterion: y => ((ResponseDryRun)y).DeltaEngineSpeed.Value()); - }) - .Case<ResponseFailTimeInterval>(r => { dt = r.DeltaT; }) - .Case<ResponseSuccess>(() => { }) - .Default( - r => { - throw new UnexpectedResponseException( - "PowertrainDrivingCycle received an unexpected response.", r); - }); + break; + case ResponseFailTimeInterval r: + dt = r.DeltaT; + break; + case ResponseSuccess _: + break; + default: + throw new UnexpectedResponseException("PowertrainDrivingCycle received an unexpected response.", response); + } } while (!(response is ResponseSuccess || response is ResponseFailTimeInterval) && (++responseCount < 10)); AbsTime = absTime + dt; @@ -162,17 +163,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl return response; } - public double Progress - { - get { return Math.Max(0, AbsTime.Value() / Data.Entries.Last().Time.Value()); } - } + public double Progress => Math.Max(0, AbsTime.Value() / Data.Entries.Last().Time.Value()); #endregion #region VectoSimulationComponent - protected override void DoWriteModalResults(Second time, Second simulationInterval, - IModalDataContainer container) { } + protected override void DoWriteModalResults(Second time, Second simulationInterval, IModalDataContainer container) + { } protected override void DoCommitSimulationStep(Second time, Second simulationInterval) { @@ -182,60 +180,34 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl #endregion - public CycleData CycleData - { - get - { - return new CycleData { - AbsTime = CycleIterator.LeftSample.Time, - AbsDistance = null, - LeftSample = CycleIterator.LeftSample, - RightSample = CycleIterator.RightSample, - }; - } - } + public CycleData CycleData => + new CycleData { + AbsTime = CycleIterator.LeftSample.Time, + AbsDistance = null, + LeftSample = CycleIterator.LeftSample, + RightSample = CycleIterator.RightSample, + }; - public bool PTOActive - { - get { return true; } - } + public bool PTOActive => true; public DrivingCycleData.DrivingCycleEntry CycleLookAhead(Meter distance) { - return new DrivingCycleData.DrivingCycleEntry() { + return new DrivingCycleData.DrivingCycleEntry { Altitude = 0.SI<Meter>() }; } - public Meter Altitude - { - get { return 0.SI<Meter>(); } - } + public Meter Altitude => 0.SI<Meter>(); - public Radian RoadGradient - { - get { return 0.SI<Radian>(); } - } + public Radian RoadGradient => 0.SI<Radian>(); - public MeterPerSecond TargetSpeed - { - get { throw new NotImplementedException("Targetspeed in Powertrain not available?"); } - } + public MeterPerSecond TargetSpeed => throw new NotImplementedException("Targetspeed in Powertrain not available?"); - public Second StopTime - { - get { return CycleIterator.LeftSample.StoppingTime; } - } + public Second StopTime => CycleIterator.LeftSample.StoppingTime; - public Meter CycleStartDistance - { - get { return 0.SI<Meter>(); } - } + public Meter CycleStartDistance => 0.SI<Meter>(); - public IReadOnlyList<DrivingCycleData.DrivingCycleEntry> LookAhead(Meter lookaheadDistance) - { - throw new NotImplementedException(); - } + public IReadOnlyList<DrivingCycleData.DrivingCycleEntry> LookAhead(Meter lookaheadDistance) => throw new NotImplementedException(); public IReadOnlyList<DrivingCycleData.DrivingCycleEntry> LookAhead(Second time) { @@ -249,14 +221,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl return retVal; } - public SpeedChangeEntry LastTargetspeedChange - { - get { return null; } - } + public SpeedChangeEntry LastTargetspeedChange => null; - public void FinishSimulation() - { - Data.Finish(); - } + public void FinishSimulation() => Data.Finish(); } } \ No newline at end of file diff --git a/VectoCore/VectoCore/OutputData/ModFilter/ModalData1HzFilter.cs b/VectoCore/VectoCore/OutputData/ModFilter/ModalData1HzFilter.cs index 7575b637e20910f624a490ceb6b18effa2f92f9e..b1c71cfb34302a32c16e0f299fe64fd409d19599 100644 --- a/VectoCore/VectoCore/OutputData/ModFilter/ModalData1HzFilter.cs +++ b/VectoCore/VectoCore/OutputData/ModFilter/ModalData1HzFilter.cs @@ -184,14 +184,12 @@ namespace TUGraz.VectoCore.OutputData.ModFilter private static IEnumerable<object> MultiplyRow(IEnumerable<object> row, SI dt) { return row.Select(val => { - if (val is SI) { - val = (SI)val * dt.Value(); - } else { - val.Switch() - .Case<int>(i => val = i * dt.Value()) - .Case<double>(d => val = d * dt.Value()) - .Case<float>(f => val = f * dt.Value()) - .Case<uint>(ui => val = ui * dt.Value()); + switch (val) { + case SI si: val = si * dt.Value(); break; + case int i: val = i * dt.Value(); break; + case double d: val = d * dt.Value(); break; + case float f: val = f * dt.Value(); break; + case uint ui: val = ui * dt.Value(); break; } return val; }); @@ -214,19 +212,17 @@ namespace TUGraz.VectoCore.OutputData.ModFilter val = (SI)val + (SI)addVal; } } else { - val.Switch() - .Case<int>(i => val = i + (int)addVal) - .Case<double>(d => val = d + (double)addVal) - .Case<float>(f => val = f + (float)addVal) - .Case<uint>(ui => val = ui + (uint)addVal); + switch (val) { + case int x: val = x + (int)addVal; break; + case double x: val = x + (double)addVal; break; + case float x: val = x + (float)addVal; break; + case uint x: val = x + (uint)addVal; break; + } } return val; }).ToArray(); } - public string ID - { - get { return "1Hz"; } - } + public string ID => "1Hz"; } } \ No newline at end of file diff --git a/VectoCore/VectoCore/OutputData/XML/Engineering/XMLEngineeringWriter.cs b/VectoCore/VectoCore/OutputData/XML/Engineering/XMLEngineeringWriter.cs index 035b6af287c6e4ef17da2c821dd40047d7fccef6..d28c8e218ee971c9f5e80571de3eff007f7918be 100644 --- a/VectoCore/VectoCore/OutputData/XML/Engineering/XMLEngineeringWriter.cs +++ b/VectoCore/VectoCore/OutputData/XML/Engineering/XMLEngineeringWriter.cs @@ -190,17 +190,14 @@ namespace TUGraz.VectoCore.OutputData.XML.Engineering public string GetComponentFilename(IComponentInputData component) { - string formatString = null; - component.Switch() - .Case<IEngineEngineeringInputData>(c => formatString = "ENG_{0}.xml"); - - return RemoveInvalidFileCharacters(string.Format(formatString ?? "{0}", component.Model)); + var formatString = component is IEngineEngineeringInputData ? "ENG_{0}.xml" : "{0}"; + return RemoveInvalidFileCharacters(string.Format(formatString, component.Model)); } public string RemoveInvalidFileCharacters(string filename) { - string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); - Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch))); + var regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); + var r = new Regex($"[{Regex.Escape(regexSearch)}]"); return r.Replace(filename, ""); } } diff --git a/VectoCore/VectoCoreTest/Integration/SimulationRuns/FullPowertrain.cs b/VectoCore/VectoCoreTest/Integration/SimulationRuns/FullPowertrain.cs index a4e46f489499a0966fe906469d1b5214188ea992..4dfc179aca1dd162c11ba9f7b725c2080e16fd6e 100644 --- a/VectoCore/VectoCoreTest/Integration/SimulationRuns/FullPowertrain.cs +++ b/VectoCore/VectoCoreTest/Integration/SimulationRuns/FullPowertrain.cs @@ -106,7 +106,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var cycle = new DistanceBasedDrivingCycle(container, cycleData); var cyclePort = cycle.OutPort(); - cycle.AddComponent(new Driver(container, driverData, new DefaultDriverStrategy(container))) + cycle.AddComponent(new Driver(container, driverData, new DefaultDriverStrategy(container))) .AddComponent(new Vehicle(container, vehicleData, airDragData)) .AddComponent(new Wheels(container, vehicleData.DynamicTyreRadius, vehicleData.WheelsInertia)) .AddComponent(new Brakes(container)) @@ -125,22 +125,26 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var cnt = 0; do { response = cyclePort.Request(absTime, ds); - response.Switch(). - Case<ResponseDrivingCycleDistanceExceeded>(r => ds = r.MaxDistance). - Case<ResponseCycleFinished>(r => { }). - Case<ResponseSuccess>(r => { + switch (response) { + case ResponseCycleFinished _: + break; + case ResponseDrivingCycleDistanceExceeded r: + ds = r.MaxDistance; + break; + case ResponseSuccess r: container.CommitSimulationStep(absTime, r.SimulationInterval); absTime += r.SimulationInterval; - ds = container.VehicleInfo.VehicleSpeed.IsEqual(0) ? Constants.SimulationSettings.DriveOffDistance : Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed; - if (cnt++ % 100 == 0) { modData.Finish(VectoRun.Status.Success); } - }). - Default(r => Assert.Fail("Unexpected Response: {0}", r)); + break; + default: + Assert.Fail("Unexpected Response: {0}", response); + break; + } } while (!(response is ResponseCycleFinished)); modData.Finish(VectoRun.Status.Success); Assert.IsInstanceOf<ResponseCycleFinished>(response); @@ -157,7 +161,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var vehicleData = CreateVehicleData(3300.SI<Kilogram>()); var driverData = CreateDriverData(AccelerationFile); var airDragData = CreateAirdragData(); - + var runData = new VectoRunData() { JobName = "Coach_FullPowertrain", EngineData = engineData, @@ -176,7 +180,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var cycle = new DistanceBasedDrivingCycle(container, cycleData); - var cyclePort = cycle.OutPort(); + var cyclePort = cycle.OutPort(); cycle.AddComponent(new Driver(container, driverData, new DefaultDriverStrategy(container))) .AddComponent(new Vehicle(container, vehicleData, airDragData)) .AddComponent(new Wheels(container, vehicleData.DynamicTyreRadius, vehicleData.WheelsInertia)) @@ -210,23 +214,29 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns } Log.Info("Test Got Response: {0},", response); - response.Switch(). - Case<ResponseDrivingCycleDistanceExceeded>(r => ds = r.MaxDistance). - Case<ResponseCycleFinished>(r => { }). - Case<ResponseGearShift>(r => { Log.Debug("Gearshift"); }). - Case<ResponseSuccess>(r => { + switch (response) { + case ResponseCycleFinished _: + break; + case ResponseDrivingCycleDistanceExceeded r: + ds = r.MaxDistance; + break; + case ResponseGearShift _: + Log.Debug("Gearshift"); + break; + case ResponseSuccess r: container.CommitSimulationStep(absTime, r.SimulationInterval); absTime += r.SimulationInterval; - ds = container.VehicleInfo.VehicleSpeed.IsEqual(0) ? Constants.SimulationSettings.DriveOffDistance : Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed; - if (cnt++ % 100 == 0) { modData.Finish(VectoRun.Status.Success); } - }). - Default(r => Assert.Fail("Unexpected Response: {0}", r)); + break; + default: + Assert.Fail("Unexpected Response: {0}", response); + break; + } } modData.Finish(VectoRun.Status.Success); Assert.IsInstanceOf<ResponseSuccess>(response); @@ -292,26 +302,30 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns } Log.Info("Test Got Response: {0},", response); - response.Switch(). - Case<ResponseDrivingCycleDistanceExceeded>(r => ds = r.MaxDistance). - Case<ResponseCycleFinished>(r => { }). - Case<ResponseGearShift>(r => { Log.Debug("Gearshift"); }). - Case<ResponseSuccess>(r => { + switch (response) { + case ResponseCycleFinished _: + break; + case ResponseDrivingCycleDistanceExceeded r: + ds = r.MaxDistance; + break; + case ResponseGearShift _: + Log.Debug("Gearshift"); + break; + case ResponseSuccess r: container.CommitSimulationStep(absTime, r.SimulationInterval); absTime += r.SimulationInterval; - ds = container.VehicleInfo.VehicleSpeed.IsEqual(0) ? Constants.SimulationSettings.DriveOffDistance : Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed; - if (cnt++ % 100 == 0) { modData.Finish(VectoRun.Status.Success); } - }). - Default(r => { + break; + default: modData.Finish(VectoRun.Status.Success); - Assert.Fail("Unexpected Response: {0}", r); - }); + Assert.Fail("Unexpected Response: {0}", response); + break; + } } modData.Finish(VectoRun.Status.Success); Assert.IsInstanceOf<ResponseCycleFinished>(response); @@ -347,8 +361,8 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns Gears = ratios.Select((ratio, i) => Tuple.Create((uint)i, new GearData { -// MaxTorque = ratio > 5 ? 2300.SI<NewtonMeter>() : null, - LossMap = TransmissionLossMapReader.ReadFromFile(GearboxLossMap, ratio, string.Format("Gear {0}", i)), + // MaxTorque = ratio > 5 ? 2300.SI<NewtonMeter>() : null, + LossMap = TransmissionLossMapReader.ReadFromFile(GearboxLossMap, ratio, $"Gear {i}"), Ratio = ratio, ShiftPolygon = ShiftPolygonReader.ReadFromFile(GearboxShiftPolygonFile) })) @@ -441,7 +455,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns private static AirdragData CreateAirdragData() { - return new AirdragData() { + return new AirdragData { CrossWindCorrectionCurve = new CrosswindCorrectionCdxALookup(3.2634.SI<SquareMeter>(), CrossWindCorrectionCurveReader.GetNoCorrectionCurve(3.2634.SI<SquareMeter>()), @@ -458,9 +472,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns //Deceleration = -0.5.SI<MeterPerSquareSecond>() LookAheadDecisionFactor = new LACDecisionFactor() }, - OverSpeed = new DriverData.OverSpeedData { - Enabled = false - }, + OverSpeed = new DriverData.OverSpeedData { Enabled = false } }; } } diff --git a/VectoCore/VectoCoreTest/Integration/SimulationRuns/MinimalPowertrain.cs b/VectoCore/VectoCoreTest/Integration/SimulationRuns/MinimalPowertrain.cs index 106437d8e235251e0edd8c8ad536ba36c9c60fa0..cba3cbc4320426276fec74b6c97d6995d31d9fc1 100644 --- a/VectoCore/VectoCoreTest/Integration/SimulationRuns/MinimalPowertrain.cs +++ b/VectoCore/VectoCoreTest/Integration/SimulationRuns/MinimalPowertrain.cs @@ -91,7 +91,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns }; var modData = new ModalDataContainer(runData, fileWriter, null); var container = new VehicleContainer(ExecutionMode.Engineering, modData) { - RunData = new VectoRunData() { + RunData = new VectoRunData() { VehicleData = vehicleData, DriverData = driverData } @@ -106,6 +106,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns .AddComponent(engine); var gbx = new MockGearbox(container); + // ReSharper disable once ObjectCreationAsStatement new DummyCycle(container); var driverPort = driver.OutPort(); @@ -140,8 +141,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var driverData = CreateDriverData(AccelerationFile); var fileWriter = new FileOutputWriter("Coach_MinimalPowertrain"); - var runData = new VectoRunData() - { + var runData = new VectoRunData() { JobName = "Coach_MinimalPowertrain", VehicleData = vehicleData, EngineData = engineData, @@ -170,13 +170,10 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns .AddComponent(new CombustionEngine(container, engineData)); //engine.IdleController.RequestPort = clutch.IdleControlPort; - var gbx = new MockGearbox(container); - gbx.Gear = new GearshiftPosition(0); + var gbx = new MockGearbox(container) { Gear = new GearshiftPosition(0) }; var cyclePort = cycle.OutPort(); - cyclePort.Initialize(); - gbx.Gear = new GearshiftPosition(0); var absTime = 0.SI<Second>(); @@ -190,29 +187,30 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var cnt = 0; while (!(response is ResponseCycleFinished) && container.MileageCounter.Distance < 17000) { response = cyclePort.Request(absTime, ds); - response.Switch(). - Case<ResponseDrivingCycleDistanceExceeded>(r => ds = r.MaxDistance). - Case<ResponseCycleFinished>(r => { }). - Case<ResponseSuccess>(r => { + switch (response) { + case ResponseCycleFinished _: + break; + case ResponseDrivingCycleDistanceExceeded r: + ds = r.MaxDistance; + break; + case ResponseSuccess r: container.CommitSimulationStep(absTime, r.SimulationInterval); absTime += r.SimulationInterval; - ds = container.VehicleInfo.VehicleSpeed.IsEqual(0) ? Constants.SimulationSettings.DriveOffDistance - : (Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed) - .Cast<Meter>(); - + : (Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed).Cast<Meter>(); if (cnt++ % 100 == 0) { modData.Finish(VectoRun.Status.Success); } - }). - Default(r => Assert.Fail("Unexpected Response: {0}", r)); + break; + default: + Assert.Fail("Unexpected Response: {0}", response); + break; + } } Assert.IsInstanceOf<ResponseCycleFinished>(response); - modData.Finish(VectoRun.Status.Success); - NLog.LogManager.EnableLogging(); } @@ -229,8 +227,7 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var driverData = CreateDriverData(AccelerationFile2); var fileWriter = new FileOutputWriter("Coach_MinimalPowertrainOverload"); - var runData = new VectoRunData() - { + var runData = new VectoRunData() { JobName = "Coach_MinimalPowertrain", SimulationType = SimulationType.DistanceCycle, VehicleData = vehicleData, @@ -269,19 +266,19 @@ namespace TUGraz.VectoCore.Tests.Integration.SimulationRuns var ds = Constants.SimulationSettings.DriveOffDistance; while (container.MileageCounter.Distance < 100) { var response = cyclePort.Request(absTime, ds); - response.Switch(). - Case<ResponseDrivingCycleDistanceExceeded>(r => ds = r.MaxDistance). - Case<ResponseSuccess>(r => { + switch (response) { + case ResponseDrivingCycleDistanceExceeded r: + ds = r.MaxDistance; + break; + case ResponseSuccess r: container.CommitSimulationStep(absTime, r.SimulationInterval); absTime += r.SimulationInterval; - ds = container.VehicleInfo.VehicleSpeed.IsEqual(0) ? Constants.SimulationSettings.DriveOffDistance - : (Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed) - .Cast<Meter>(); - + : (Constants.SimulationSettings.TargetTimeInterval * container.VehicleInfo.VehicleSpeed).Cast<Meter>(); modData.Finish(VectoRun.Status.Success); - }); + break; + } } modData.Finish(VectoRun.Status.Success); diff --git a/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs b/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs index 3b7b8bce436008f7224e824ef29eccd9ad9c78bd..949e4396bd99d65745f235ec0e3bd69ae55b8b6e 100644 --- a/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs +++ b/VectoCore/VectoCoreTest/Models/Simulation/DrivingCycleTests.cs @@ -138,9 +138,11 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation for (var i = 0; i < 100; i++) { response = cycle.OutPort().Request(absTime, dt); - response.Switch() - .Case<ResponseFailTimeInterval>(r => dt = r.DeltaT) - .Case<ResponseSuccess>(r => { + switch (response) { + case ResponseFailTimeInterval r: + dt = r.DeltaT; + break; + case ResponseSuccess _: container.CommitSimulationStep(absTime, dt); Assert.AreEqual(absTime, outPort.AbsTime); Assert.AreEqual(dt, outPort.Dt); @@ -158,8 +160,10 @@ namespace TUGraz.VectoCore.Tests.Models.Simulation absTime += dt; dt = 1.SI<Second>(); - }) - .Default(r => { throw new UnexpectedResponseException("Got an unexpected response", r); }); + break; + default: + throw new UnexpectedResponseException("Got an unexpected response", response); + } } }