diff --git a/Dynamic/SimulatableModels/GainSchedModel.cs b/Dynamic/SimulatableModels/GainSchedModel.cs index f3a11f74..77adfdbb 100644 --- a/Dynamic/SimulatableModels/GainSchedModel.cs +++ b/Dynamic/SimulatableModels/GainSchedModel.cs @@ -266,42 +266,24 @@ private double CalculateLinearProcessGainTerm(int inputIndex, double u, double u int gainSchedModelIdx = 0; for (int idx = 0; idx < modelParameters.LinearGainThresholds.Length; idx++) { - if (idx == 0) + if (u_GainSched < modelParameters.LinearGainThresholds[idx]) { - if (u_GainSched < modelParameters.LinearGainThresholds[idx]) - { - gainSchedModelIdx = idx; - break;// jump out of for-loop - } - else - { - gainSchedModelIdx = idx+1; - } + gainSchedModelIdx = idx; + break; } else if (idx == modelParameters.LinearGainThresholds.Length - 1) { - if (u_GainSched > modelParameters.LinearGainThresholds[idx]) - { - gainSchedModelIdx = idx; - } - } - else - { - if (u_GainSched > modelParameters.LinearGainThresholds[idx-1] && - u_GainSched < modelParameters.LinearGainThresholds[idx]) - { - gainSchedModelIdx = idx; - } + gainSchedModelIdx = idx + 1; } } - if (modelParameters.U0 != null) + if (modelParameters.U0 != null) // For curvature only { - processGainTerm += modelParameters.LinearGains.ElementAt(gainSchedModelIdx)[inputIndex] * (u- modelParameters.U0[inputIndex]); + processGainTerm = modelParameters.LinearGains.ElementAt(gainSchedModelIdx)[inputIndex] * (u- modelParameters.U0[inputIndex]); } else { - processGainTerm += modelParameters.LinearGains.ElementAt(gainSchedModelIdx)[inputIndex] * u; + processGainTerm = modelParameters.LinearGains.ElementAt(gainSchedModelIdx)[inputIndex] * u; } return processGainTerm; } diff --git a/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorMISOTests.cs b/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorMISOTests.cs index 0c4a975f..9436e996 100644 --- a/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorMISOTests.cs +++ b/TimeSeriesAnalysis.Tests/Tests/PlantSimulatorMISOTests.cs @@ -117,10 +117,12 @@ class MISOTests GainSchedModel gainSched2; GainSchedModel gainSched3; GainSchedModel gainSched4; + GainSchedModel gainSched5; GainSchedParameters gainSchedParameters1; GainSchedParameters gainSchedParameters2; GainSchedParameters gainSchedParameters3; GainSchedParameters gainSchedParameters4; + GainSchedParameters gainSchedParameters5; PidParameters pidParameters1; PidModel pidModel1; @@ -137,7 +139,7 @@ public void SetUp() TimeConstant_s = new double[] { 10,20 }, TimeConstantThresholds = new double[] { 2 }, LinearGains = new List { new double[] { 5 }, new double[] { 10 } }, - LinearGainThresholds = new double[] { 2 }, + LinearGainThresholds = new double[] { 4 }, TimeDelay_s = 0, Bias = 0 }; @@ -147,7 +149,7 @@ public void SetUp() TimeConstant_s = new double[] { 10, 20 }, TimeConstantThresholds = new double[] { 2 }, LinearGains = new List { new double[] { 20 }, new double[] { 5 } }, - LinearGainThresholds = new double[] { 3 }, + LinearGainThresholds = new double[] { 0.5 }, TimeDelay_s = 0, Bias = 0 }; @@ -157,7 +159,7 @@ public void SetUp() TimeConstant_s = new double[] { 10, 20 }, TimeConstantThresholds = new double[] { 2 }, LinearGains = new List { new double[] { -20 }, new double[] { -15 } }, - LinearGainThresholds = new double[] { 2 }, + LinearGainThresholds = new double[] { 1.5 }, TimeDelay_s = 0, Bias = 0 }; @@ -166,8 +168,18 @@ public void SetUp() { TimeConstant_s = new double[] { 10, 20 }, TimeConstantThresholds = new double[] { 2 }, - LinearGains = new List { new double[] { -20 }, new double[] { -15 } }, - LinearGainThresholds = new double[] { 2 }, + LinearGains = new List { new double[] { -20 }, new double[] { -15 }, new double[] { -10 } }, + LinearGainThresholds = new double[] { 1.5, 2.5 }, + TimeDelay_s = 0, + Bias = 0 + }; + + gainSchedParameters5 = new GainSchedParameters + { + TimeConstant_s = new double[] { 10, 20 }, + TimeConstantThresholds = new double[] { 2 }, + LinearGains = new List { new double[] { -10 }, new double[] { -15 }, new double[] { -10 }, new double[] { 10 } }, + LinearGainThresholds = new double[] { 0.5, 1.5, 2.5 }, TimeDelay_s = 0, Bias = 0 }; @@ -210,7 +222,8 @@ public void SetUp() gainSched1 = new GainSchedModel(gainSchedParameters1, "GainSched1"); gainSched2 = new GainSchedModel(gainSchedParameters2, "GainSched2"); gainSched3 = new GainSchedModel(gainSchedParameters3, "GainSched3"); - gainSched4 = new GainSchedModel(gainSchedParameters3, "GainSched4"); + gainSched4 = new GainSchedModel(gainSchedParameters4, "GainSched4"); + gainSched5 = new GainSchedModel(gainSchedParameters5, "GainSched5"); pidParameters1 = new PidParameters() @@ -253,9 +266,11 @@ public void DeserializeModelWithMissingInput_GivesGoodErrorMessage() var isOk = plantSim.Simulate(inputData, out TimeSeriesDataSet simData); } - [TestCase(1, 5, 30)] - [TestCase(2, 20, 15)] - [TestCase(3,-20, -45)] + [TestCase(1, 5, 15)] + [TestCase(2, 5, 15)] + [TestCase(3, -20, -45)] + [TestCase(4, -20, -30)] + [TestCase(5, -15, 30)] public void GainSched_Single_RunsAndConverges(int ver, double step1Out, double step3Out) { GainSchedModel gainSched = null; @@ -271,6 +286,14 @@ public void GainSched_Single_RunsAndConverges(int ver, double step1Out, double s { gainSched = gainSched3; } + else if (ver == 4) + { + gainSched = gainSched4; + } + else if (ver == 5) + { + gainSched = gainSched5; + } var plantSim = new PlantSimulator(new List { gainSched }); var inputData = new TimeSeriesDataSet(); @@ -285,18 +308,20 @@ public void GainSched_Single_RunsAndConverges(int ver, double step1Out, double s // Assert Assert.IsTrue(isSimulatable); - Assert.IsTrue(Math.Abs(simY1[N/3-2] - step1Out) < 0.2,"first step should have a gain of 5"); - Assert.IsTrue(Math.Abs(simY1.Last() - step3Out) < 0.2, "third step should have a gain of 10"); + Assert.IsTrue(Math.Abs(simY1[N/3-2] - step1Out) < 0.2,"first step should have a gain of " + step1Out.ToString()); + Assert.IsTrue(Math.Abs(simY1.Last() - step3Out) < 0.2, "third step should have a gain of " + step3Out.ToString()); // Assert.IsTrue(Math.Abs(simY.Last() - (1 * 55 + 0.5 * 45 + 5)) < 0.01); + Shared.EnablePlots(); Plot.FromList(new List { - simData.GetValues(gainSched.GetID(),SignalType.Output_Y), + simY1, inputData.GetValues(gainSched.GetID(),SignalType.External_U,0), - }, - new List { "y1=y_sim1", "y3=u1" }, + }, + new List { "y1=y_sim" + (ver + 5).ToString(), "y3=u1" }, timeBase_s, "GainSched_Single"); Shared.DisablePlots(); + }