diff --git a/VectoCore/Models/Connector/Ports/Impl/Response.cs b/VectoCore/Models/Connector/Ports/Impl/Response.cs index 55929de638f68a9a6519d32f26917e8008bf9a9b..bbe2b2c24cd82846d50cab2789d45fefd5c6f604 100644 --- a/VectoCore/Models/Connector/Ports/Impl/Response.cs +++ b/VectoCore/Models/Connector/Ports/Impl/Response.cs @@ -63,9 +63,8 @@ namespace TUGraz.VectoCore.Models.Connector.Ports.Impl internal class ResponseDryRun : AbstractResponse { - public Watt EngineDeltaFullLoad { get; set; } - public Watt EngineDeltaDragLoad { get; set; } - public Watt GearboxDeltaFullLoad { get; set; } + public Watt DeltaFullLoad { get; set; } + public Watt DeltaDragLoad { get; set; } } internal class ResponseGearShift : AbstractResponse {} diff --git a/VectoCore/Models/Simulation/Impl/DistanceRun.cs b/VectoCore/Models/Simulation/Impl/DistanceRun.cs index 4f0b205b7cf93159cb4d73a2ae9b7c4f0238d893..460271afe123f79476e1f0a03ca111998ad05736 100644 --- a/VectoCore/Models/Simulation/Impl/DistanceRun.cs +++ b/VectoCore/Models/Simulation/Impl/DistanceRun.cs @@ -20,28 +20,26 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl ds = Constants.SimulationSettings.DriveOffDistance; } - IResponse response; + IResponse response = null; var requestDone = false; - - do { + while (!requestDone) { response = CyclePort.Request(AbsTime, ds); + response.Switch(). - Case<ResponseSuccess>(_ => requestDone = true). - Case<ResponseCycleFinished>(_ => requestDone = true). + Case<ResponseSuccess>(r => { + AbsTime = AbsTime + r.SimulationInterval; + dt = r.SimulationInterval; + requestDone = true; + }). + Case<ResponseCycleFinished>(() => requestDone = true). Case<ResponseDrivingCycleDistanceExceeded>(r => { ds = r.MaxDistance; }); - } while (!requestDone); + } //while (response is ResponseFailTimeInterval) { // _dt = (response as ResponseFailTimeInterval).DeltaT; // response = CyclePort.Request(_absTime, _dt); //} - if (response is ResponseCycleFinished) { - return response; - } - - AbsTime = AbsTime + response.SimulationInterval; - dt = response.SimulationInterval; return response; } diff --git a/VectoCore/Models/Simulation/Impl/TimeRun.cs b/VectoCore/Models/Simulation/Impl/TimeRun.cs index b9e4f364538feb7ff1f68b339c55607efa6bc6de..e97bab4dbb11ef9452b4769469f2f54eaea58997 100644 --- a/VectoCore/Models/Simulation/Impl/TimeRun.cs +++ b/VectoCore/Models/Simulation/Impl/TimeRun.cs @@ -11,10 +11,6 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl protected override IResponse DoSimulationStep() { var response = CyclePort.Request(AbsTime, dt); - - if (response is ResponseCycleFinished) { - return response; - } return response; } diff --git a/VectoCore/Models/Simulation/Impl/VectoRun.cs b/VectoCore/Models/Simulation/Impl/VectoRun.cs index 7ec4b31e23d9acb69945e0e857642301d6a73efb..6483914b3baa02fd39149b03e40bfb0344cef361 100644 --- a/VectoCore/Models/Simulation/Impl/VectoRun.cs +++ b/VectoCore/Models/Simulation/Impl/VectoRun.cs @@ -49,7 +49,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl Container.CommitSimulationStep(AbsTime, dt); } - // set _dt to difference to next full second. + // todo set _dt to difference to next full second. AbsTime += dt; } while (response is ResponseSuccess); diff --git a/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs b/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs index 1dd22d194bd0133cc093ad57678bc1d53cb5087a..c07ae3c86f2eb4f6fd291c5ad4994c2d5bcc905d 100644 --- a/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs +++ b/VectoCore/Models/SimulationComponent/Data/AccelerationCurve.cs @@ -138,8 +138,6 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Data var b = -d / k; var c = 0.SI<Meter>() - m / k; var t = Math.Log(((v2 * k + d) / (v1 * k + d)).Cast<Scalar>()) / k; - // TODO @@@quam: remove .SI<> when bug #VECTO-111 is fixed... - return m / k * Math.Exp((k * t).Value()) + b * t + c; } } diff --git a/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs index eb0e39937d882c2ce90a255db25aa38f55c5dbef..3d7682ac3ce5ce4ccb1f43c2247a5545c3ed41e4 100644 --- a/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs +++ b/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs @@ -98,8 +98,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl if (dryRun) { return new ResponseDryRun { - EngineDeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), - EngineDeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower), + DeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), + DeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower), EnginePowerRequest = requestedEnginePower }; } diff --git a/VectoCore/Models/SimulationComponent/Impl/Driver.cs b/VectoCore/Models/SimulationComponent/Impl/Driver.cs index fddfa9d21cda4751252b2f500bc95cf9fd9a47e3..ad178ed4335ccbf42ee631bc09fe519bb7a4ee7f 100644 --- a/VectoCore/Models/SimulationComponent/Impl/Driver.cs +++ b/VectoCore/Models/SimulationComponent/Impl/Driver.cs @@ -177,11 +177,10 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl CurrentState.Acceleration); var retVal = Next.Request(absTime, CurrentState.dt, CurrentState.Acceleration, gradient); CurrentState.Response = retVal; - if (retVal is ResponseSuccess) { - retVal.SimulationInterval = CurrentState.dt; - } else { - Log.DebugFormat("unhandled response from powertrain: {0}", retVal); - } + + retVal.Switch(). + Case<ResponseSuccess>(r => r.SimulationInterval = CurrentState.dt). + Default(() => Log.DebugFormat("unhandled response from powertrain: {0}", retVal)); return retVal; //new ResponseDrivingCycleDistanceExceeded() { SimulationInterval = CurrentState.dt }; } @@ -189,7 +188,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl private bool SearchBreakingPower(Second absTime, ref Meter ds, Radian gradient, ResponseDryRun response, bool coasting) { var exceeded = new List<Watt>(); // only used while testing - var breakingPower = response.EngineDeltaDragLoad.Abs(); + var breakingPower = response.DeltaDragLoad.Abs(); if (DataBus.ClutchState() != ClutchState.ClutchClosed) { breakingPower = response.AxlegearPowerRequest.Abs(); } @@ -199,7 +198,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl do { ds = originalDs; var delta = DataBus.ClutchState() == ClutchState.ClutchClosed - ? -response.EngineDeltaDragLoad + ? -response.DeltaDragLoad : -response.AxlegearPowerRequest; exceeded.Add(delta); @@ -408,14 +407,16 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl ds = originalDs; response.Switch(). Case<ResponseEngineOverload>(r => delta = r.Delta). - Case<ResponseDryRun>(r => delta = coasting ? -r.EngineDeltaDragLoad : r.EngineDeltaFullLoad). + Case<ResponseGearboxOverload>(r => delta = r.Delta). + Case<ResponseDryRun>(r => delta = coasting ? -r.DeltaDragLoad : r.DeltaFullLoad). Default(r => { throw new VectoSimulationException(string.Format("Unknown response type. {0}", r)); }); exceeded.Add(delta); acceleration.Add(CurrentState.Acceleration.Value()); if (delta.IsEqual(0, Constants.SimulationSettings.EngineFLDPowerTolerance)) { - Log.DebugFormat("found operating point in {0} iterations. Engine Power req: {2}, delta: {1}", exceeded.Count, - delta, response.EnginePowerRequest); + Log.DebugFormat( + "found operating point in {0} iterations. Engine Power req: {2}, Gearbox Power req: {3} delta: {1}", + exceeded.Count, delta, response.EnginePowerRequest, response.GearboxPowerRequest); return true; } if (delta > 0) { diff --git a/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs b/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs index 2dfea899927deb9df9c43b7ff3ddb27a86c67fcf..d071c7ff4f448016910e04ae736a5012a870f205 100644 --- a/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs +++ b/VectoCore/Models/SimulationComponent/Impl/EngineOnlyCombustionEngine.cs @@ -31,8 +31,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl if (dryRun) { return new ResponseDryRun { - EngineDeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), - EngineDeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower) + DeltaFullLoad = (requestedEnginePower - _currentState.DynamicFullLoadPower), + DeltaDragLoad = (requestedEnginePower - _currentState.FullDragPower) }; } diff --git a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs index 157ed811ebf69258b0e348c01a10b48c7b818bdd..29cf28ff99016f417ee283b3b098b4f05f43f75d 100644 --- a/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs +++ b/VectoCore/Models/SimulationComponent/Impl/Gearbox.cs @@ -132,7 +132,7 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl response.GearboxPowerRequest = inTorque * inEngineSpeed; response.Switch(). - Case<ResponseDryRun>(r => r.GearboxDeltaFullLoad = (maxTorque - inTorque) * inEngineSpeed); + Case<ResponseDryRun>(r => r.DeltaFullLoad = (maxTorque - inTorque) * inEngineSpeed); return response; } diff --git a/VectoCore/Utils/SwitchExtension.cs b/VectoCore/Utils/SwitchExtension.cs index 8694ff548c8e2e726c30065f0d02069a69f422a7..1912bc155501dd1c7fce0e77b2d789f09f5affd3 100644 --- a/VectoCore/Utils/SwitchExtension.cs +++ b/VectoCore/Utils/SwitchExtension.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.CompilerServices; namespace TUGraz.VectoCore.Utils { @@ -11,46 +10,46 @@ namespace TUGraz.VectoCore.Utils /// </remarks> public static class SwitchExtension { - public static Switcher<T> Switch<T>(this T self) + public static Switch<T> Switch<T>(this T self) { - return new Switcher<T>(self); + return new Switch<T>(self); } + } - public class Switcher<T> - { - private readonly T _value; - private bool _handled; + public class Switch<T> + { + private readonly T _value; + private bool _handled; - internal Switcher(T value) - { - _value = value; - _handled = false; - } + internal Switch(T value) + { + _value = value; + _handled = false; + } - public Switcher<T> Case<TTarget>(Action action) where TTarget : T - { - return Case<TTarget>(_ => action()); - } + public Switch<T> Case<TFilter>(Action action) where TFilter : T + { + return Case<TFilter>(_ => action()); + } - public Switcher<T> Case<TTarget>(Action<TTarget> action) where TTarget : T - { - if (!_handled && _value is TTarget) { - action((TTarget)_value); - _handled = true; - } - return this; + public Switch<T> Case<TFilter>(Action<TFilter> action) where TFilter : T + { + if (!_handled && _value is TFilter) { + action((TFilter)_value); + _handled = true; } + return this; + } - public void Default(Action action) - { - Default(_ => action()); - } + public void Default(Action action) + { + Default(_ => action()); + } - public void Default(Action<T> action) - { - if (!_handled) { - action(_value); - } + public void Default(Action<T> action) + { + if (!_handled) { + action(_value); } } }