From 0f56ab3076e4f1349b26b021dc6757e38ce1090d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20Sk=C3=A5lheim?= Date: Fri, 12 Jan 2024 15:59:40 +0100 Subject: [PATCH] TimeConstant Test added. Does not work for lage differences in time constants. --- Dynamic/Identification/GainSchedIdentifier.cs | 2 +- .../Tests/GainSchedIdentifyTests.cs | 59 ++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/Dynamic/Identification/GainSchedIdentifier.cs b/Dynamic/Identification/GainSchedIdentifier.cs index 8e0f5246..a7a4956e 100644 --- a/Dynamic/Identification/GainSchedIdentifier.cs +++ b/Dynamic/Identification/GainSchedIdentifier.cs @@ -221,7 +221,7 @@ public GainSchedParameters GainSchedIdentify(UnitDataSet dataSet, FittingSpecs f // Time constant thresholds // TODO: Should consider to remove this threshold as this should be the same thresholds as gainSchedThresholds - GSp2.TimeConstantThresholds = GS_TimeConstantThreshold2; + GSp2.TimeConstantThresholds = new double[] { potential_gainthresholds[i]}; GSp2.LinearGainThresholds[0] = potential_gainthresholds[i]; diff --git a/TimeSeriesAnalysis.Tests/Tests/GainSchedIdentifyTests.cs b/TimeSeriesAnalysis.Tests/Tests/GainSchedIdentifyTests.cs index db1e6e81..f5708dd1 100644 --- a/TimeSeriesAnalysis.Tests/Tests/GainSchedIdentifyTests.cs +++ b/TimeSeriesAnalysis.Tests/Tests/GainSchedIdentifyTests.cs @@ -30,9 +30,9 @@ public void GainSchedIdentify_ReturnsParametersWithNumberOfLinearGainsNotExceedi GainSchedParameters correct_gain_sched_parameters = new GainSchedParameters { - TimeConstant_s = new double[] { 5, 25 }, + TimeConstant_s = new double[] { 3, 10 }, TimeConstantThresholds = new double[] { 3.1 }, - LinearGains = new List { new double[] { 1 }, new double[] { 2 } }, + LinearGains = new List { new double[] { 1 }, new double[] { 5 } }, LinearGainThresholds = new double[] { 3.1 }, TimeDelay_s = 0, Bias = 0 @@ -148,6 +148,61 @@ public void GainSchedIdentify_GainsNotLargerThanTheBiggestPossibleGain() "The largest gain in the best fitting model cannot exceed the largest gain amplitude of the correct model"); } + + + [TestCase()] + public void GainSchedIdentify_EstimatedTimeConstantsAreCloseToCorrectTimeConstants() + { + // Arrange + var unitData = new UnitDataSet("test"); /* Create an instance of TimeSeries with test data */ + double[] u1 = TimeSeriesCreator.ThreeSteps(N/5, N/3, N/2, N, 0, 1, 2, 3); + double[] u2 = TimeSeriesCreator.ThreeSteps(3*N/5, 2*N/3, 4*N/5, N, 0, 1, 2, 3); + double[] u = u1.Zip(u2, (x, y) => x+y).ToArray(); + double[,] U = Array2D.CreateFromList(new List { u }); + unitData.U = U; + unitData.Times = TimeSeriesCreator.CreateDateStampArray( + new DateTime(2000, 1, 1), timeBase_s, N); + + var gainSchedIdentifier = new GainSchedIdentifier(); + + GainSchedParameters correct_gain_sched_parameters = new GainSchedParameters + { + TimeConstant_s = new double[] { 3, 10 }, + TimeConstantThresholds = new double[] { 3.1 }, + LinearGains = new List { new double[] { 1 }, new double[] { 5 } }, + LinearGainThresholds = new double[] { 3.1 }, + TimeDelay_s = 0, + Bias = 0 + }; + GainSchedModel correct_model = new GainSchedModel(correct_gain_sched_parameters, "Correct gain sched model"); + var correct_plantSim = new PlantSimulator(new List { correct_model }); + var inputData = new TimeSeriesDataSet(); + inputData.Add(correct_plantSim.AddExternalSignal(correct_model, SignalType.External_U, (int)INDEX.FIRST), u); + inputData.CreateTimestamps(timeBase_s); + var CorrectisSimulatable = correct_plantSim.Simulate(inputData, out TimeSeriesDataSet CorrectsimData); + SISOTests.CommonAsserts(inputData, CorrectsimData, correct_plantSim); + double[] simY1 = CorrectsimData.GetValues(correct_model.GetID(), SignalType.Output_Y); + unitData.Y_meas = simY1; + + Shared.EnablePlots(); + Plot.FromList(new List { + simY1, + unitData.U.GetColumn(0) }, + new List { "y1=correct_model", "y3=u1" }, + timeBase_s, + "GainSched - Time constant inspection"); + Shared.DisablePlots(); + + // Act + GainSchedParameters best_params = gainSchedIdentifier.GainSchedIdentify(unitData); + + // Assert + for (int k = 0; k < correct_gain_sched_parameters.TimeConstant_s.Length; k++) + { + Assert.That(Math.Pow(best_params.TimeConstant_s[k] - correct_gain_sched_parameters.TimeConstant_s[k], 2), Is.LessThanOrEqualTo(1), + "There are too large differences in the time constant on index " + k.ToString()); + } + } // TODO: Additional test cases as needed... // Helper methods for creating test TimeSeries instances, gain scheduling slices, and other setup tasks...