From b4004288572b4df2d448ed4995cdecbeefc407cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steinar=20Elgs=C3=A6ter?= Date: Wed, 6 Nov 2024 09:23:11 +0100 Subject: [PATCH] - add protection to PlotGain methods to avoid referencing null objects if the model could not be identified. --- Dynamic/Identification/GainSchedIdentifier.cs | 31 ++++++++-------- TimeSeriesAnalysis.csproj | 2 +- Utility/PlotGain.cs | 37 ++++++++++++++++--- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/Dynamic/Identification/GainSchedIdentifier.cs b/Dynamic/Identification/GainSchedIdentifier.cs index bc0d9c5..784a122 100644 --- a/Dynamic/Identification/GainSchedIdentifier.cs +++ b/Dynamic/Identification/GainSchedIdentifier.cs @@ -172,16 +172,16 @@ static public GainSchedModel Identify(UnitDataSet dataSet, GainSchedFittingSpecs paramsToReturn = bestModel_pass1; } + if (paramsToReturn.Fitting.WasAbleToIdentify) + { + ////////////////////////////// + EstimateTimeDelay(ref paramsToReturn, ref dataSet); - ////////////////////////////// - EstimateTimeDelay(ref paramsToReturn, ref dataSet); - - ////////////////////////////// - //// EXPERIMENTAL: determining two time-constants. - // const int numTcIterations = 50; - // var bestParams = EvaluateMultipleTimeConstantsForGivenGainThreshold(ref paramsToReturn, dataSet,numTcIterations); - - + ////////////////////////////// + //// EXPERIMENTAL: determining two time-constants. + // const int numTcIterations = 50; + // var bestParams = EvaluateMultipleTimeConstantsForGivenGainThreshold(ref paramsToReturn, dataSet,numTcIterations); + } //////////////////////////////////// if (paramsToReturn.Fitting == null) { @@ -298,13 +298,14 @@ public static GainSchedModel IdentifyForGivenThresholds(UnitDataSet dataSet, Gai idParams.AddWarning(GainSchedIdentWarnings.InsufficientExcitationBetweenEachThresholdToBeCertainOfGains); // post-processing : improving the dynamic model terms by analysis after setting the static model + if (idParams.Fitting.WasAbleToIdentify) + { + // simulate the model and determine the optimal bias term: + DetermineOperatingPointAndSimulate(ref idParams, ref dataSet); - // simulate the model and determine the optimal bias term: - DetermineOperatingPointAndSimulate(ref idParams, ref dataSet); - - if (doTimeDelayEstimation) - EstimateTimeDelay(ref idParams, ref dataSet); - + if (doTimeDelayEstimation) + EstimateTimeDelay(ref idParams, ref dataSet); + } return new GainSchedModel(idParams, "identified"); } diff --git a/TimeSeriesAnalysis.csproj b/TimeSeriesAnalysis.csproj index ebd1c2b..c60c4c6 100644 --- a/TimeSeriesAnalysis.csproj +++ b/TimeSeriesAnalysis.csproj @@ -14,7 +14,7 @@ False https://github.com/equinor/TimeSeriesAnalysis.git readme.md - 1.3.24 + 1.3.25 Equinor Equinor true diff --git a/Utility/PlotGain.cs b/Utility/PlotGain.cs index b9d040f..3ae9b56 100644 --- a/Utility/PlotGain.cs +++ b/Utility/PlotGain.cs @@ -52,6 +52,7 @@ public static void PlotSteadyState(ISimulatableModel model1, ISimulatableModel m if (model1.GetType() == typeof(GainSchedModel)) { + tables_m1 = CreateSteadyStateXYTablesFromGainSchedModel((GainSchedModel)model1, model1Name, inputIdx, numberOfPlotPoints, uMin, uMax); } else if (model1.GetType() == typeof(UnitModel)) @@ -61,6 +62,7 @@ public static void PlotSteadyState(ISimulatableModel model1, ISimulatableModel m uMin = ((UnitModel)model1).modelParameters.Fitting.Umin; uMax = ((UnitModel)model1).modelParameters.Fitting.Umax; } + tables_m1 = CreateSteadyStateXYTablesFromUnitModel((UnitModel)model1, model1Name, inputIdx, numberOfPlotPoints, uMin, uMax); } @@ -135,11 +137,6 @@ public static void PlotGainSched(GainSchedModel model1, GainSchedModel model2 = } List tables_m1 = new List(); - /* if (model1.modelParameters.Fitting != null && uMin == null && uMax == null) - { - uMin = model1.modelParameters.Fitting.Umin; - uMax = model1.modelParameters.Fitting.Umax; - }*/ tables_m1 = CreateGainXYTablesFromGainSchedModel(model1, model1Name, inputIdx); if (model2 == null) @@ -172,16 +169,25 @@ public static void PlotGainSched(GainSchedModel model1, GainSchedModel model2 = /// /// /// - static private List CreateSteadyStateXYTablesFromGainSchedModel(GainSchedModel model, + static private List CreateSteadyStateXYTablesFromGainSchedModel(GainSchedModel model, string modelName, int inputIdx, int numberOfPlotPoints, double[] uMinExt = null, double[] uMaxExt = null) { + if (model.GetModelParameters().Fitting != null) + { + if (!model.GetModelParameters().Fitting.WasAbleToIdentify) + { + Console.WriteLine(model.ID + "unable to identify, skipping"); + return null; + } + } if (inputIdx > 0) { Console.WriteLine("currently only supports gain-sched variables with one input"); return null; } + string outputId = model.outputID; if (outputId == null) { @@ -230,6 +236,15 @@ static private List CreateSteadyStateXYTablesFromGainSchedModel(GainSch static private List CreateSteadyStateXYTablesFromUnitModel(UnitModel model, string modelName, int inputIdx, int numberOfPlotPoints,double[] uMinExt=null, double[] uMaxExt=null ) { + if (model.GetModelParameters().Fitting != null) + { + if (!model.GetModelParameters().Fitting.WasAbleToIdentify) + { + Console.WriteLine(model.ID + "unable to identify, skipping"); + return null; + } + } + string outputId = model.outputID; if (outputId == null) { @@ -297,6 +312,16 @@ static private List CreateSteadyStateXYTablesFromUnitModel(UnitModel mo /// static private List CreateGainXYTablesFromGainSchedModel(GainSchedModel model, string modelName, int inputIdx) { + + if (model.GetModelParameters().Fitting != null) + { + if (!model.GetModelParameters().Fitting.WasAbleToIdentify) + { + Console.WriteLine(model.ID + "unable to identify, skipping"); + return null; + } + } + string outputId = model.outputID; if (outputId == null) {