diff --git a/VectoCommon/VectoCommon/Utils/VectoMath.cs b/VectoCommon/VectoCommon/Utils/VectoMath.cs index 52e7dda65c722f954948e0250d9e395732858645..5e2960b8286443a56a83cb46c37dc6376dc4cc3c 100644 --- a/VectoCommon/VectoCommon/Utils/VectoMath.cs +++ b/VectoCommon/VectoCommon/Utils/VectoMath.cs @@ -164,12 +164,22 @@ namespace TUGraz.VectoCommon.Utils [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Max<T>(T c1, T c2) where T : IComparable { - return c1.CompareTo(c2) >= 0 ? c1 : c2; + return c1.CompareTo(c2) > 0 ? c1 : c2; + } + + /// <summary> + /// Returns the maximum of two values. + /// </summary> + [DebuggerStepThrough] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T Max<T>(double c1, T c2) where T : SIBase<T> + { + return c1 > c2.Value() ? c1.SI<T>() : c2; } [DebuggerStepThrough] [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T Max<T>(T c1, T c2, T c3) where T : IComparable + public static T Max<T>(T c1, T c2, T c3) where T : SIBase<T> { return Max(Max(c1, c2), c3); } diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs index d4e0793639458abecf9e7674ba07d3f0006d085d..807d96a3d4dc55f4a47d7567dc488c690706d7c1 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/CombustionEngine.cs @@ -605,8 +605,8 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl var prevEngineSpeed = _engine.PreviousState.EngineSpeed; var dragLoad = _engine.ModelData.FullLoadCurve.DragLoadStationaryPower(prevEngineSpeed); - var nextEnginePower = (_lastEnginePower - dragLoad) * - VectoMath.Max(idleTime.Value() * PeDropSlope + PeDropOffset, 0) + dragLoad; + var nextEnginePower = (_lastEnginePower - dragLoad) * Math.Max(0, idleTime.Value() * PeDropSlope + PeDropOffset) + + dragLoad; var auxDemandResponse = RequestPort.Request(absTime, dt, 0.SI<NewtonMeter>(), prevEngineSpeed, true); diff --git a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs index f5abf10e10be1be0698e6672eeef4f97ed0438d4..cc93a435396dc9161a8b321621b6f3bc6f35ecb5 100644 --- a/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs +++ b/VectoCore/VectoCore/Models/SimulationComponent/Impl/Vehicle.cs @@ -30,6 +30,7 @@ */ using System; +using TUGraz.VectoCommon.Exceptions; using TUGraz.VectoCommon.Models; using TUGraz.VectoCommon.Utils; using TUGraz.VectoCore.Configuration; @@ -102,7 +103,14 @@ namespace TUGraz.VectoCore.Models.SimulationComponent.Impl CurrentState.DriverAcceleration = DriverAcceleration(acceleration); CurrentState.RollingResistance = RollingResistance(gradient); - CurrentState.AirDragResistance = AirDragResistance(PreviousState.Velocity, CurrentState.Velocity); + try { + CurrentState.AirDragResistance = AirDragResistance(PreviousState.Velocity, CurrentState.Velocity); + } catch (VectoException ex) { + Log.Warn("Exception during calculation of AirDragResistance: absTime: {0}, dist: {1}, v: {2}. {3}", absTime, + CurrentState.Distance, CurrentState.Velocity, ex); + CurrentState.AirDragResistance = AirDragResistance(VectoMath.Max(0, PreviousState.Velocity), + VectoMath.Max(0, CurrentState.Velocity)); + } CurrentState.SlopeResistance = SlopeResistance(gradient); // DriverAcceleration = vehicleTractionForce - RollingResistance - AirDragResistance - SlopeResistance