Skip to content

Commit

Permalink
- add protection to PlotGain methods to avoid referencing null object…
Browse files Browse the repository at this point in the history
…s if the model could not be identified.
  • Loading branch information
Steinar Elgsæter committed Nov 6, 2024
1 parent d17d5de commit b400428
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
31 changes: 16 additions & 15 deletions Dynamic/Identification/GainSchedIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion TimeSeriesAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<RunAnalyzersDuringLiveAnalysis>False</RunAnalyzersDuringLiveAnalysis>
<RepositoryUrl>https://github.com/equinor/TimeSeriesAnalysis.git</RepositoryUrl>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<Version>1.3.24</Version>
<Version>1.3.25</Version>
<Company>Equinor</Company>
<Authors>Equinor</Authors>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
37 changes: 31 additions & 6 deletions Utility/PlotGain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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);
}

Expand Down Expand Up @@ -135,11 +137,6 @@ public static void PlotGainSched(GainSchedModel model1, GainSchedModel model2 =
}
List<XYTable> tables_m1 = new List<XYTable>();

/* 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)
Expand Down Expand Up @@ -172,16 +169,25 @@ public static void PlotGainSched(GainSchedModel model1, GainSchedModel model2 =
/// <param name="uMaxExt"></param>
/// <param name="numberOfPlotPoints"></param>
/// <returns></returns>
static private List<XYTable> CreateSteadyStateXYTablesFromGainSchedModel(GainSchedModel model,
static private List<XYTable> 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)
{
Expand Down Expand Up @@ -230,6 +236,15 @@ static private List<XYTable> CreateSteadyStateXYTablesFromGainSchedModel(GainSch
static private List<XYTable> 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)
{
Expand Down Expand Up @@ -297,6 +312,16 @@ static private List<XYTable> CreateSteadyStateXYTablesFromUnitModel(UnitModel mo
/// <returns></returns>
static private List<XYTable> 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)
{
Expand Down

0 comments on commit b400428

Please sign in to comment.