Skip to content

Commit

Permalink
-change to how GainSchedModel.Iterate treats linear gains.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steinar Elgsæter authored and Steinar Elgsæter committed Oct 4, 2024
1 parent 62f401b commit b29b4a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
70 changes: 35 additions & 35 deletions Dynamic/SimulatableModels/GainSchedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace TimeSeriesAnalysis.Dynamic
/// Remember that with more thresholds defined, the higher the requirement for information content in data will be if the model is to be identified from it.
/// </para>
/// <para>
/// This should not be confuesed with "gain-scheduled" PID-control, which is a similar concept but applied to PID-control parameters.
/// This should not be confused with "gain-scheduled" PID-control, which is a similar concept but applied to PID-control parameters.
/// </para>
///
/// </remarks>
Expand Down Expand Up @@ -301,6 +301,39 @@ public void SetModelParameters(GainSchedParameters parameters)
return u0;
}

public double CalculateLinearProcessGainTerm(int inputIndex, double u, double u_GainSched)

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Build

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Build

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Build

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Build

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Tests

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Tests

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Tests

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'

Check warning on line 304 in Dynamic/SimulatableModels/GainSchedModel.cs

View workflow job for this annotation

GitHub Actions / Tests

Missing XML comment for publicly visible type or member 'GainSchedModel.CalculateLinearProcessGainTerm(int, double, double)'
{

int activeGainSchedModelIdx = 0;
for (int idx = 0; idx<modelParameters.LinearGainThresholds.Length; idx++)
{
if (u_GainSched<modelParameters.LinearGainThresholds[idx])
{
activeGainSchedModelIdx = idx;
break;
}
else if (idx == modelParameters.LinearGainThresholds.Length - 1)
{
activeGainSchedModelIdx = idx + 1;
}
}

double curve = 0;
// curve += modelParameters.LinearGains.ElementAt(i)[inputIndex] * modelParameters.LinearGainThresholds[i];
for (int curGainSchedModel = 1; curGainSchedModel < activeGainSchedModelIdx; curGainSchedModel++)
{
curve += modelParameters.LinearGains.ElementAt(curGainSchedModel)[inputIndex] * modelParameters.LinearGainThresholds[curGainSchedModel-1];
}
if (activeGainSchedModelIdx>0)
curve += modelParameters.LinearGains.ElementAt(activeGainSchedModelIdx)[inputIndex] *(u-modelParameters.LinearGainThresholds.ElementAt(activeGainSchedModelIdx-1));
else
curve += modelParameters.LinearGains.ElementAt(activeGainSchedModelIdx)[inputIndex] * u;
return curve;
}



/*
/// <summary>
/// Determine the process-gain(linear) contribution to the output of a particular index for a particular value
/// </summary>
Expand Down Expand Up @@ -334,7 +367,7 @@ private double CalculateLinearProcessGainTerm(int inputIndex, double u, double u
processGainTerm = modelParameters.LinearGains.ElementAt(gainSchedModelIdx)[inputIndex] * u;
}
return processGainTerm;
}
}*/

/// <summary>
/// Determine the time constant for a particular sceduling input
Expand Down Expand Up @@ -535,39 +568,6 @@ public double[] Iterate(double[] inputs, double timeBase_s,double badValueIndica
}



/// <summary>
/// Sovel quadratic equation "a x^2 + b*x +c =0" (second order of polynomial equation in a single variable x)
/// x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
private static double[] SolveQuadratic(double a, double b, double c)
{
double sqrtpart = b * b - 4 * a * c;
double x, x1, x2, img;
if (sqrtpart > 0)// two real solutions
{
x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
return new double[] { x1, x2 };
}
else if (sqrtpart < 0)// two imaginary solutions
{
sqrtpart = -sqrtpart;
x = -b / (2 * a);
img = System.Math.Sqrt(sqrtpart) / (2 * a);
return new double[] { x };// in this case, the answer is of course slightly wrong
}
else// one real solution
{
x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
return new double[] { x };
}
}


/// <summary>
/// Create a nice human-readable summary of all the important data contained in the model object.
/// This is especially useful for unit-testing and development.
Expand Down
14 changes: 7 additions & 7 deletions TimeSeriesAnalysis.Tests/Tests/GainSchedIdentifyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class GainSchedIdentifyTests
const double TimeConstantAllowedDev_s = 3.5;

[TestCase(1, 0,Description ="Two steps for every threshold(five thresholds)")]
// [TestCase(2, 0, Description = "Two steps for every threshold(five thresholds), ref model is constant gain")]
// [TestCase(11, 1,Description = "One steps between every threshold(ten thresholds, harder)")] // does not pass, for this case all the gains seem to be about twice as big as they should be...
// [TestCase(12, 1,Description = "One steps between every threshold(ten thresholds, harder), ref model is constant gain")]
[TestCase(2, 0, Description = "Two steps for every threshold(five thresholds), ref model is constant gain")]
// [TestCase(11, 1,Description = "One steps between every threshold(ten thresholds, harder)")] // does not pass, ...
// [TestCase(12, 1,Description = "One steps between every threshold(ten thresholds, harder), ref model is constant gain")]
public void GainEstOnly_CorrectGainsReturned(int ver, int expectedNumWarnings)
{
const double noiseAmplitude = 1.0;
Expand Down Expand Up @@ -113,15 +113,15 @@ public void GainEstOnly_CorrectGainsReturned(int ver, int expectedNumWarnings)
Assert.IsTrue(gsParams.Fitting.WasAbleToIdentify);
Assert.AreEqual(expectedNumWarnings, gsParams.GetWarningList().Count());

// simulate the gains-scheduled model:(TODO: this should be done automatically )
var gsIdentModel = new GainSchedModel(gsParams, "ident_model");
// simulate the gains-scheduled model
/*var gsIdentModel = new GainSchedModel(gsParams, "ident_model");
var inputDataIdent = new TimeSeriesDataSet();
inputDataIdent.Add(plantSim.AddExternalSignal(gsIdentModel, SignalType.External_U, (int)INDEX.FIRST), input);
inputDataIdent.CreateTimestamps(timeBase_s);
var identModelSim = new PlantSimulator(new List<ISimulatableModel> { gsIdentModel });
var isOk = identModelSim.Simulate(inputDataIdent, out TimeSeriesDataSet identModelSimData);
Assert.IsTrue(isOk);
Assert.IsTrue(isOk);*/

// Plotting gains(debugging)
bool doPlots = false;
Expand All @@ -130,7 +130,7 @@ public void GainEstOnly_CorrectGainsReturned(int ver, int expectedNumWarnings)
Shared.EnablePlots();
Plot.FromList(new List<double[]> {
dataSet.Y_meas,
dataSet.Y_sim, // identModelSimData.GetValues(gsIdentModel.ID, SignalType.Output_Y),
dataSet.Y_sim,
dataSet.U.GetColumn(0) },
new List<string> { "y1=y_meas", "y1=y_ident", "y3=u1" },
timeBase_s,
Expand Down

0 comments on commit b29b4a1

Please sign in to comment.