/* Class BlackBox
*
* This class contains the constructor to create an instance of
* a generalised BlackBox with a single input, single output
* and a gain. It contins the methods for obtaining the
* transfer function in the s-domain and the z-domain.
*
* This class is the superclass for several sub-classes,
* e.g. Prop (P controller), PropDeriv (PD controller),
* PropInt (PI controller), PropIntDeriv (PID controller),
* FirstOrder, SecondOrder, AtoD (ADC), DtoA (DAC),
* ZeroOrderHold, DelayLine, OpenLoop (Open Loop Path),
* of use in control engineering.
*
* Author: Michael Thomas Flanagan.
*
* Created: August 2002
* Updated: 17 July 2003, 18 May 2005, 6 April 2008, 6 October 2009, 30 October 2009,
* 2-9 November 2009, 20 January 2010, 23-25 May 2010, 3 June 2010, 18 January 2011, 13 - 27 April 2012
*
*
* DOCUMENTATION:
* See Michael T Flanagan's JAVA library on-line web page:
* http://www.ee.ucl.ac.uk/~mflanaga/java/BlackBox.html
* http://www.ee.ucl.ac.uk/~mflanaga/java/
*
* Copyright (c) 2002 - 2011 Michael Thomas Flanagan
*
* PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/
package flanagan.control;
import flanagan.math.Fmath;
import flanagan.math.Conv;
import flanagan.complex.*;
import flanagan.plot.Plot;
import flanagan.plot.PlotGraph;
import flanagan.plot.PlotPoleZero;
import flanagan.io.Db;
import flanagan.interpolation.CubicSpline;
public class BlackBox{
protected int sampLen = 0; // Length of array of stored inputs, outputs and time
protected double[] inputT = null; // Array of input signal in the time domain
protected double[] outputT = null; // Array of output signal in the time domain
protected double[] time = null; // Array of time at which inputs were taken (seconds)
protected double forgetFactor = 1.0D; // Forgetting factor, e.g. in exponential forgetting of error values
protected double deltaT = 0.0D; // Sampling time (seconds)
protected double sampFreq = 0.0D; // Sampling frequency (Hz)
protected Complex inputS = new Complex(); // Input signal in the s-domain
protected Complex outputS = new Complex(); // Output signal in the s-domain
protected Complex sValue = new Complex(); // Laplacian s
protected Complex zValue = new Complex(); // z-transform z
protected ComplexPoly sNumer = new ComplexPoly(1.0D); // Transfer function numerator in the s-domain
protected ComplexPoly sDenom = new ComplexPoly(1.0D); // Transfer function denominator in the s-domain
protected ComplexPoly zNumer = new ComplexPoly(1.0D); // Transfer function numerator in the z-domain
protected ComplexPoly zDenom = new ComplexPoly(1.0D); // Transfer function denominator in the z-domain
protected boolean sNumerSet = false; // = true when numerator entered
protected boolean sDenomSet = false; // = true when denominator entered
protected Complex sNumerScaleFactor = Complex.plusOne();// s-domain numerator/(product of s - zeros)
protected Complex sDenomScaleFactor = Complex.plusOne();// s-domain denominator/(product of s - poles)
protected Complex sNumerWorkingFactor = Complex.plusOne();// s-domain numerator/(product of s - zeros) at that point in the program
protected Complex sDenomWorkingFactor = Complex.plusOne();// s-domain denominator/(product of s - poles) at that point in the program
protected Complex[] sPoles = null; // Poles in the s-domain
protected Complex[] sZeros = null; // Zeros in the s-domain
protected Complex[] zPoles = null; // Poles in the z-domain
protected Complex[] zZeros = null; // Zeros in the z-domain
protected int sNumerDeg = 0; // Degree of transfer function numerator in the s-domain
protected int sDenomDeg = 0; // Degree of transfer function denominator in the s-domain
protected int zNumerDeg = 0; // Degree of transfer function numerator in the z-domain
protected int zDenomDeg = 0; // Degree of transfer function denominator in the z-domain
protected double deadTime = 0.0D; // Time delay between an input and the matching output [in s-domain = exp(-s.deadTime)]
protected int orderPade = 2; // Order(1 to 4)of the pade approximation for exp(-sT)
// default option = 2
protected ComplexPoly sNumerPade = new ComplexPoly(1.0D); // Transfer function numerator in the s-domain including Pade approximation
protected ComplexPoly sDenomPade = new ComplexPoly(1.0D); // Transfer function denominator in the s-domain including Pade approximation
protected Complex[] sPolesPade = null; // Poles in the s-domain including Pade approximation
protected Complex[] sZerosPade = null; // Zeros in the s-domain including Pade approximation
protected int sNumerDegPade = 0; // Degree of transfer function numerator in the s-domain including Pade approximation
protected int sDenomDegPade = 0; // Degree of transfer function denominator in the s-domain including Pade approximation
protected boolean maptozero = true; // if true infinity s zeros map to zero
// if false infinity s zeros map to minus one
protected boolean padeAdded = false; // if true Pade poles and zeros added
// if false No Pade poles and zeros added
protected double integrationSum=0.0D; // Stored integration sum in numerical integrations
protected int integMethod = 1; // numerical integration method
// = 0 Trapezium Rule [default option]
// = 1 Backward Rectangular Rule
// = 2 Foreward Rectangular Rule
protected int ztransMethod = 0; // z trasform method
// = 0 s -> z mapping (ad hoc procedure) from the continuous time domain erived s domain functions
// = 1 specific z transform, e.g. of a difference equation
protected String name = "BlackBox"; // Superclass or subclass name, e.g. pid, pd, firstorder.
// user may rename an instance of the superclass or subclass
protected String fixedName = "BlackBox"; // Super class or subclass permanent name, e.g. pid, pd, firstorder.
// user must NOT change fixedName in any instance of the superclass or subclass
// fixedName is used as an identifier in classes such as OpenPath, ClosedLoop
protected int nPlotPoints = 400; // number of points used tp lot response curves, e.g. step input response curve
protected String[] subclassName = {"BlackBox", "OpenLoop", "ClosedLoop", "Prop", "PropDeriv", "PropInt", "PropIntDeriv", "FirstOrder", "SecondOrder", "Compensator", "LowPassPassive", "HighPassPassive", "Transducer", "DelayLine", "ZeroOrderHold", "AtoD", "DtoA"};
protected int nSubclasses = subclassName.length; // number of subclasses plus superclass
protected int subclassIndex = 0; // = 0 BlackBox
// = 1 OpenLoop
// = 2 ClosedLoop
// = 3 Prop
// = 4 PropDeriv
// = 5 PropInt
// = 6 PropIntDeriv
// = 7 FirstOrder
// = 8 SecondOrder
// = 9 Compensator
// = 10 LowPassPassive
// = 11 HighPassPassive
// = 12 Transducer
// = 13 DelayLine
// = 14 ZeroOrderHold
// = 15 AtoD
// = 16 DtoA
// Constructor
public BlackBox(){
}
// Constructor with fixedName supplied
// for use by subclasses
public BlackBox(String name){
this.name = name;
this.fixedName = name;
this.setSubclassIndex();
}
// Set subclass index
protected void setSubclassIndex(){
boolean test = true;
int i = 0;
while(test){
if(this.fixedName.equals(subclassName[i])){
this.subclassIndex = i;
test = false;
}
else{
i++;
if(i>=this.nSubclasses){
System.out.println("Subclass name, " + this.fixedName + ", not recognised as a recorded subclass within this library");
System.out.println("Subclass, " + this.fixedName + ", handled as BlackBox");
this.subclassIndex = i;
test = false;
}
}
}
}
// Set the transfer function numerator in the s-domain
// Enter as an array of real (double) coefficients of the polynomial a + bs +c.s.s + d.s.s.s + ....
public void setSnumer(double[] coeff){
this.sNumerDeg = coeff.length-1;
this.sNumer = new ComplexPoly(coeff);
this.sNumerSet = true;
this.calcPolesZerosS();
this.addDeadTimeExtras();
}
// Method to set extra terms to s-domain numerator and denominator and
// to calculate extra zeros and poles if the dead time is not zero.
protected void addDeadTimeExtras()
{
this.sNumerDegPade = this.sNumerDeg;
this.sNumerPade = this.sNumer.copy();
this.sDenomDegPade = this.sDenomDeg;
this.sDenomPade = this.sDenom.copy();
if(this.deadTime==0.0D){
this.transferPolesZeros();
}
else{
this.pade();
}
}
// Set the transfer function numerator in the s-domain
// Enter as an array of Complex coefficients of the polynomial a + bs +c.s.s + d.s.s.s + ....
public void setSnumer(Complex[] coeff){
this.sNumerDeg = coeff.length-1;
this.sNumer = new ComplexPoly(coeff);
this.sNumerSet = true;
this.calcPolesZerosS();
this.addDeadTimeExtras();
}
// Set the transfer function numerator in the s-domain
// Enter as an existing instance of ComplexPoly
public void setSnumer(ComplexPoly coeff){
this.sNumerDeg = coeff.getDeg();
this.sNumer = ComplexPoly.copy(coeff);
this.sNumerSet = true;
this.calcPolesZerosS();
this.addDeadTimeExtras();
}
// Set the transfer function denominator in the s-domain
// Enter as an array of real (double) coefficients of the polynomial a + bs +c.s.s + d.s.s.s + ....
public void setSdenom(double[] coeff){
this.sDenomDeg = coeff.length-1;
this.sDenom = new ComplexPoly(coeff);
this.sDenomSet = true;
this.calcPolesZerosS();
this.addDeadTimeExtras();
}
// Set the transfer function denomonator in the s-domain
// Enter as an array of Complex coefficients of the polynomial a + bs +c.s.s + d.s.s.s + ....
public void setSdenom(Complex[] coeff){
this.sDenomDeg = coeff.length-1;
this.sDenom = new ComplexPoly(coeff);
this.sDenomSet = true;
this.calcPolesZerosS();
this.addDeadTimeExtras();
}
// Set the transfer function denominator in the s-domain
// Enter as an existing instance of ComplexPoly
public void setSdenom(ComplexPoly coeff){
this.sDenomDeg = coeff.getDeg();
this.sDenom = coeff.copy();
this.sDenomSet = true;
this.calcPolesZerosS();
this.addDeadTimeExtras();
}
// calculate constant converting product of root terms to the value of the polynomial
public static Complex scaleFactor(ComplexPoly poly, Complex[] roots){
int nRoots = roots.length;
// calculate mean of the poles
Complex mean = new Complex(0.0D, 0.0);
for(int i=0; inRoots-1)test = false;
}
}
// calculate product of roots-mean
Complex product = new Complex(1.0D, 0.0);
for(int i=0; i5){
orderPade=4;
System.out.println("BlackBox does not support Pade approximations above an order of 4");
System.out.println("The order has been set to 4");
}
if(orderPade<1){
orderPade=1;
System.out.println("Pade approximation order was less than 1");
System.out.println("The order has been set to 1");
}
this.orderPade = orderPade;
this.pade();
}
// Set the Pade approximation order
public void setPadeOrder(int orderPade){
if(orderPade>5){
orderPade=4;
System.out.println("BlackBox does not support Pade approximations above an order of 4");
System.out.println("The order has been set to 4");
}
if(orderPade<1){
orderPade=2;
System.out.println("Pade approximation order was less than 1");
System.out.println("The order has been set to 2");
}
this.orderPade = orderPade;
this.pade();
}
// Get the dead time
public double getDeadTime(){
return this.deadTime;
}
// Get the Pade approximation order
public int getPadeOrder(){
return this.orderPade;
}
// Resets the s-domain Pade inclusive numerator and denominator adding a Pade approximation
// Also calculates and stores additional zeros and poles arising from the Pade approximation
protected void pade(){
ComplexPoly sNumerExtra = null;
ComplexPoly sDenomExtra = null;
Complex[] newZeros = null;
Complex[] newPoles = null;
switch(orderPade){
case 1: this.sNumerDegPade = this.sNumerDeg + 1;
this.sDenomDegPade = this.sDenomDeg + 1;
this.sNumerPade = new ComplexPoly(sNumerDegPade);
this.sDenomPade = new ComplexPoly(sDenomDegPade);
sNumerExtra = new ComplexPoly(1.0D, -this.deadTime/2.0D);
sDenomExtra = new ComplexPoly(1.0D, this.deadTime/2.0D);
this.sNumerPade = this.sNumer.times(sNumerExtra);
this.sDenomPade = this.sDenom.times(sDenomExtra);
newZeros = Complex.oneDarray(1);
newZeros[0].reset(2.0/this.deadTime, 0.0D);
newPoles = Complex.oneDarray(1);
newPoles[0].reset(-2.0/this.deadTime, 0.0D);
break;
case 2: this.sNumerDegPade = this.sNumerDeg + 2;
this.sDenomDegPade = this.sDenomDeg + 2;
this.sNumerPade = new ComplexPoly(sNumerDegPade);
this.sDenomPade = new ComplexPoly(sDenomDegPade);
sNumerExtra = new ComplexPoly(1.0D, -this.deadTime/2.0D, Math.pow(this.deadTime, 2)/12.0D);
sDenomExtra = new ComplexPoly(1.0D, this.deadTime/2.0D, Math.pow(this.deadTime, 2)/12.0D);
this.sNumerPade = this.sNumer.times(sNumerExtra);
this.sDenomPade = this.sDenom.times(sDenomExtra);
newZeros = sNumerExtra.rootsNoMessages();
newPoles = sDenomExtra.rootsNoMessages();
break;
case 3: this.sNumerDegPade = this.sNumerDeg + 3;
this.sDenomDegPade = this.sDenomDeg + 3;
this.sNumerPade = new ComplexPoly(sNumerDegPade);
this.sDenomPade = new ComplexPoly(sDenomDegPade);
double[] termn3 = new double[4];
termn3[0] = 1.0D;
termn3[1] = -this.deadTime/2.0D;
termn3[2] = Math.pow(this.deadTime, 2)/10.0D;
termn3[3] = -Math.pow(this.deadTime, 3)/120.0D;
sNumerExtra = new ComplexPoly(termn3);
this.sNumerPade = this.sNumer.times(sNumerExtra);
newZeros = sNumerExtra.rootsNoMessages();
double[] termd3 = new double[4];
termd3[0] = 1.0D;
termd3[1] = this.deadTime/2.0D;
termd3[2] = Math.pow(this.deadTime, 2)/10.0D;
termd3[3] = Math.pow(this.deadTime, 3)/120.0D;
sDenomExtra = new ComplexPoly(termd3);
this.sDenomPade = this.sDenom.times(sDenomExtra);
newPoles = sDenomExtra.rootsNoMessages();
break;
case 4: this.sNumerDegPade = this.sNumerDeg + 4;
this.sDenomDegPade = this.sDenomDeg + 4;
this.sNumerPade = new ComplexPoly(sNumerDegPade);
this.sDenomPade = new ComplexPoly(sDenomDegPade);
double[] termn4 = new double[5];
termn4[0] = 1.0D;
termn4[1] = -this.deadTime/2.0D;
termn4[2] = 3.0D*Math.pow(this.deadTime, 2)/28.0D;
termn4[3] = -Math.pow(this.deadTime, 3)/84.0D;
termn4[4] = Math.pow(this.deadTime, 4)/1680.0D;
sNumerExtra = new ComplexPoly(termn4);
this.sNumerPade = this.sNumer.times(sNumerExtra);
newZeros = sNumerExtra.rootsNoMessages();
double[] termd4 = new double[5];
termd4[0] = 1.0D;
termd4[1] = this.deadTime/2.0D;
termd4[2] = 3.0D*Math.pow(this.deadTime, 2)/28.0D;
termd4[3] = Math.pow(this.deadTime, 3)/84.0D;
termd4[4] = Math.pow(this.deadTime, 4)/1680.0D;
sDenomExtra = new ComplexPoly(termd4);
this.sDenomPade = this.sDenom.times(sDenomExtra);
newPoles = sDenomExtra.rootsNoMessages();
break;
default: this.orderPade = 2;
this.sNumerDegPade = this.sNumerDeg + 2;
this.sDenomDegPade = this.sDenomDeg + 2;
this.sNumerPade = new ComplexPoly(sNumerDegPade);
this.sDenomPade = new ComplexPoly(sDenomDegPade);
sNumerExtra = new ComplexPoly(1.0D, -this.deadTime/2.0D, Math.pow(this.deadTime, 2)/12.0D);
sDenomExtra = new ComplexPoly(1.0D, this.deadTime/2.0D, Math.pow(this.deadTime, 2)/12.0D);
this.sNumerPade = this.sNumer.times(sNumerExtra);
this.sDenomPade = this.sDenom.times(sDenomExtra);
newZeros = sNumerExtra.rootsNoMessages();
newPoles = sDenomExtra.rootsNoMessages();
break;
}
// store zeros and poles arising from the Pade term
if(this.sNumerPade!=null && this.sNumerDegPade>0){
sZerosPade = Complex.oneDarray(sNumerDegPade);
for(int i=0; i0){
sPolesPade = Complex.oneDarray(sDenomDegPade);
for(int i=0; i0 && this.sZeros!=null){
this.sZerosPade = Complex.oneDarray(this.sNumerDeg);
for(int i=0; i0 && this.sPoles!=null){
this.sPolesPade = Complex.oneDarray(this.sDenomDeg);
for(int i=0; ithis.deltaT){
System.out.println(this.name+"."+method+": The dead time is greater than the sampling period");
System.out.println("Dead time: "+this.deadTime);
System.out.println("Sampling period: "+this.deltaT);
System.out.println("!!! The results of this program may not be physically meaningful !!!");
warning = true;
}
return warning;
}
// Perform z transform for a given delta T
// Uses maptozAdHoc in this class but may be overridden in a subclass
public void zTransform(double deltat){
this.mapstozAdHoc(deltat);
}
// Perform z transform using an already set delta T
// Uses maptozAdHoc in this class but may be overridden in a subclass
public void zTransform(){
this.mapstozAdHoc();
}
// Map s-plane zeros and poles of the transfer function onto the z-plane using the ad-hoc method
// for a given sampling period.
// References:
// John Dorsey, Continuous and Discrete Control Systems, pp 490-491, McGraw Hill (2002)
// J R Leigh, Applied Digital Control, pp 78-80, Prentice-Hall (1985)
public void mapstozAdHoc(double deltaT){
this.deltaT = deltaT;
this.mapstozAdHoc();
}
// Map s-plane zeros and poles of the transfer function onto the z-plane using the ad-hoc method
// for an already set sampling period.
// References:
// John Dorsey, Continuous and Discrete Control Systems, pp 490-491, McGraw Hill (2002)
// J R Leigh, Applied Digital Control, pp 78-80, Prentice-Hall (1985)
public void mapstozAdHoc(){
this.deadTimeWarning("mapstozAdHoc");
if(!this.padeAdded)this.transferPolesZeros();
// Calculate z-poles
this.zDenomDeg = this.sDenomDegPade;
ComplexPoly root = new ComplexPoly(1);
this.zDenom = new ComplexPoly(this.zDenomDeg);
if(zDenomDeg>0){
this.zPoles = Complex.oneDarray(this.zDenomDeg);
for(int i=0; ithis.sDenomDegPade)infZeros=this.sDenomDegPade-this.sNumerDegPade;
// total number of zeros
this.zNumerDeg = this.sNumerDegPade + infZeros;
this.zNumer = new ComplexPoly(zNumerDeg);
this.zZeros = Complex.oneDarray(zNumerDeg);
// zero values
if(this.zNumerDeg>0){
for(int i=0; i0){
if(maptozero){
for(int i=this.sNumerDegPade; i0){
for(int i=0; i0){
for(int i=0; i0){
for(int i=0; i0){
for(int i=0; i1){
System.out.println("z transform method option number " + ztransMethod + " not recognised");
System.out.println("z tr methodansform option number set in BlackBox to the default value of 0 (s -> z ad hoc mapping)");
this.integMethod = 0;
}
else{
this.ztransMethod = ztransMethod;
}
}
// Set the integration method [number option]
// 0 = trapezium, 1 = Backward rectangular, 2 = Foreward rectangular
public void setIntegrateOption(int integMethod){
if(integMethod<0 || integMethod>2){
System.out.println("integration method option number " + integMethod + " not recognised");
System.out.println("integration method option number set in BlackBox to the default value of 0 (trapezium rule)");
this.integMethod = 0;
}
else{
this.integMethod = integMethod;
}
}
// Set the integration method [String option]
// trapezium; trapezium, tutin. Backward rectangular; back backward. Foreward rectangular; foreward, fore
// Continuous time equivalent: continuous, cont
public void setIntegrateOption(String integMethodS){
if(integMethodS.equals("trapezium") || integMethodS.equals("Trapezium") ||integMethodS.equals("tutin") || integMethodS.equals("Tutin")){
this.integMethod = 0;
}
else{
if(integMethodS.equals("backward") || integMethodS.equals("Backward") ||integMethodS.equals("back") || integMethodS.equals("Back")){
this.integMethod = 1;
}
else{
if(integMethodS.equals("foreward") || integMethodS.equals("Foreward") ||integMethodS.equals("fore") || integMethodS.equals("Fore")){
this.integMethod = 2;
}
else{
System.out.println("integration method option " + integMethodS + " not recognised");
System.out.println("integration method option number set in PID to the default value of 0 (trapezium rule)");
this.integMethod = 0;
}
}
}
}
// Reset the length of the arrays storing the times, time domain inputs and time domain outputs
public void setSampleLength(int samplen){
if(samplen==0)throw new IllegalArgumentException("Entered sample length must be greater than zero");
if(samplen==1)samplen=2;
if(this.sampLen==0){
this.sampLen = samplen;
this.time = new double[samplen];
this.inputT = new double[samplen];
this.outputT = new double[samplen];
}
else{
String question = "BlackBox setSampleLength: Do you wish to replace the sample length, " + this.sampLen + " with " + samplen;
if(Db.yesNo(question)){
int holdS = this.sampLen;
this.sampLen=samplen;
if(this.time!=null){
this.deltaT = this.time[holdS-1]/(samplen-1);
double[] holdT = Conv.copy(time);
double[] holdI = Conv.copy(inputT);
this.time = new double[this.sampLen];
this.inputT = new double[this.sampLen];
CubicSpline cs = new CubicSpline(holdT, holdI);
this.time[0] = holdT[0];
this.inputT[0] = holdI[0];
for(int i=1; i0; i--){
this.time[i]= this.time[i+1]-deltaT;
this.inputT[i]=inputt;
}
this.time[0]=0.0;
this.inputT[0]=inputt;
}
}
// Enter a set of current time domain times and input values
public void setInputT(double[] ttime, double[] inputt){
int samplen = ttime.length;
if(samplen!=inputt.length)throw new IllegalArgumentException("time and input arrays are of different lengths: " + samplen + ", " + inputt.length);
if(samplen==1){
this.setInputT(ttime[0], inputt[0]);
}
else{
this.sampLen = samplen;
this.time = ttime;
this.inputT = inputt;
this.outputT = new double[this.sampLen];
this.deltaT = ttime[this.sampLen]/(this.sampLen - 1);
this.sampFreq = 1.0/this.deltaT;
}
}
// Reset s-domain input
public void setInputS(Complex input){
this.inputS=input;
}
// Reset all inputs, outputs and times to zero
public void resetZero(){
for(int i=0; i0)this.sZeros = this.sNumer.rootsNoMessages();
if(this.sZeros!=null){
this.sNumerScaleFactor = BlackBox.scaleFactor(this.sNumer, this.sZeros);
}
else{
this.sNumerScaleFactor = this.sNumer.coeffCopy(0);
}
}
if(this.sDenom!=null){
if(this.sDenom.getDeg()>0)this.sPoles = this.sDenom.rootsNoMessages();
if(this.sPoles!=null){
this.sDenomScaleFactor = BlackBox.scaleFactor(this.sDenom, this.sPoles);
}
else{
this.sDenomScaleFactor = this.sDenom.coeffCopy(0);
}
}
if(this.sNumerPade!=null){
if(this.sNumerPade.getDeg()>0)this.sZerosPade = this.sNumerPade.rootsNoMessages();
}
if(this.sDenomPade!=null){
if(this.sDenomPade.getDeg()>0)this.sPolesPade = this.sDenomPade.rootsNoMessages();
}
}
// Eliminates identical poles and zeros in the s-domain
protected void zeroPoleCancellation(){
boolean check = false;
boolean testI = true;
boolean testJ = false;
if(this.sNumerDegPade==0 || this.sDenomDegPade==0)testI=false;
if(this.sZerosPade==null || this.sPolesPade==null)testI=false;
if(testI){
int nPoles = this.sPolesPade.length;
int nZeros = this.sZerosPade.length;
for(int i=nZeros-1; i>=0; i--){
testJ = false;
for(int j=nPoles-1; j>=0; j--){
if(this.sZerosPade[i].isEqual(this.sPolesPade[j])){
if(j=0; i--){
testJ = false;
for(int j=nPoles-1; j>=0; j--){
if(this.sZeros[i].isEqual(this.sPoles[j])){
if(jMath.abs(ssdr)*0.01){
System.out.println("method getSteadyStateValue: The imaginary part, " + ssdi + ", is greater than 1 per cent of the the real part, " + ssdr);
System.out.println("Magnitude has been returned");
}
return ssc.abs();
}
// Get steadty state value for a step input of magnitude, mag
public double getSeadyStateValue(double mag){
Complex num = this.sNumer.evaluate(Complex.zero());
Complex den = this.sDenom.evaluate(Complex.zero());
Complex ssc = num.over(den);
double ssdr = ssc.getReal();
double ssdi = ssc.getImag();
if(Math.abs(ssdi)>Math.abs(ssdr)*0.01){
System.out.println("method getSteadyStateValue: The imaginary part, " + ssdi + ", is greater than 1 per cent of the the real part, " + ssdr);
System.out.println("Magnitude has been returned");
}
return mag*ssc.abs();
}
// Evaluate the s-domain tranfer function for the present value of s
// deadtime evaluated as exponential term
public Complex evalTransFunctS(){
if(!this.padeAdded)this.transferPolesZeros();
Complex num = this.sNumer.evaluate(this.sValue);
Complex den = this.sDenom.evaluate(this.sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return num.over(den).times(lagterm);
}
// Evaluate the s-domain tranfer function for a given Complex value of s
public Complex evalTransFunctS(Complex sValue){
if(!this.padeAdded)this.transferPolesZeros();
this.sValue = Complex.copy(sValue);
Complex num = this.sNumer.evaluate(sValue);
Complex den = this.sDenom.evaluate(sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return num.over(den).times(lagterm);
}
// Evaluate the s-domain tranfer function for a sine wave input at a given frequency (s^-1)
public Complex evalTransFunctS(double freq){
if(!this.padeAdded)this.transferPolesZeros();
this.sValue.reset(0.0D, 2.0D*Math.PI*freq);
Complex num = this.sNumer.evaluate(this.sValue);
Complex den = this.sDenom.evaluate(this.sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return num.over(den).times(lagterm);
}
// Evaluate the magnitude of the s-domain tranfer function for the present value of s
public double evalMagTransFunctS(){
if(!this.padeAdded)this.transferPolesZeros();
Complex num = this.sNumer.evaluate(this.sValue);
Complex den = this.sDenom.evaluate(this.sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return (num.over(den).times(lagterm)).abs();
}
// Evaluate the magnitude of the s-domain tranfer function for a given Complex value of s
public double evalMagTransFunctS(Complex sValue){
if(!this.padeAdded)this.transferPolesZeros();
this.sValue = Complex.copy(sValue);
Complex num = this.sNumer.evaluate(sValue);
Complex den = this.sDenom.evaluate(sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return (num.over(den).times(lagterm)).abs();
}
// Evaluate the magnitude of the s-domain tranfer function for a sine wave input at a given frequency (s^-1)
public double evalMagTransFunctS(double freq){
if(!this.padeAdded)this.transferPolesZeros();
this.sValue.reset(0.0D, 2.0D*Math.PI*freq);
Complex num = this.sNumer.evaluate(this.sValue);
Complex den = this.sDenom.evaluate(this.sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return (num.over(den).times(lagterm)).abs();
}
// Evaluate the phase of the s-domain tranfer function for the present value of s
public double evalPhaseTransFunctS(){
if(!this.padeAdded)this.transferPolesZeros();
Complex num = this.sNumer.evaluate(this.sValue);
Complex den = this.sDenom.evaluate(this.sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return (num.over(den).times(lagterm)).arg();
}
// Evaluate the phase of the s-domain tranfer function for a given Complex value of s
public double evalPhaseTransFunctS(Complex sValue){
if(!this.padeAdded)this.transferPolesZeros();
this.sValue = Complex.copy(sValue);
Complex num = this.sNumer.evaluate(sValue);
Complex den = this.sDenom.evaluate(sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return (num.over(den).times(lagterm)).arg();
}
// Evaluate the phase of the s-domain tranfer function for a sine wave input at a given frequency (s^-1)
public double evalPhaseTransFunctS(double freq){
if(!this.padeAdded)this.transferPolesZeros();
this.sValue.reset(0.0D, 2.0D*Math.PI*freq);
Complex num = this.sNumer.evaluate(this.sValue);
Complex den = this.sDenom.evaluate(this.sValue);
Complex lagterm = Complex.plusOne();
if(this.deadTime!=0)lagterm = Complex.exp(this.sValue.times(-this.deadTime));
return (num.over(den).times(lagterm)).arg();
}
// Evaluate the z-domain tranfer function for the present value of z
public Complex evalTransFunctZ(){
Complex num = this.zNumer.evaluate(this.zValue);
Complex den = this.zDenom.evaluate(this.zValue);
return num.over(den);
}
// Evaluate the z-domain tranfer function for a given Complex value of z
public Complex evalTransFunctZ(Complex zValue){
this.zValue = Complex.copy(zValue);
Complex num = this.zNumer.evaluate(zValue);
Complex den = this.zDenom.evaluate(zValue);
return num.over(den);
}
// Evaluate the magnitude of the z-domain tranfer function for the present value of z
public double evalMagTransFunctZ(){
Complex num = this.zNumer.evaluate(this.zValue);
Complex den = this.zDenom.evaluate(this.zValue);
return num.over(den).abs();
}
// Evaluate the magnitude of the z-domain tranfer function for a given Complex value of z
public double evalMagTransFunctZ(Complex zValue){
this.zValue = Complex.copy(zValue);
Complex num = this.zNumer.evaluate(zValue);
Complex den = this.zDenom.evaluate(zValue);
return num.over(den).abs();
}
// Evaluate the phase of the z-domain tranfer function for the present value of z
public double evalPhaseTransFunctZ(){
Complex num = this.zNumer.evaluate(this.zValue);
Complex den = this.zDenom.evaluate(this.zValue);
return num.over(den).arg();
}
// Evaluate the phase of the z-domain tranfer function for a given Complex value of z
public double evalPhaseTransFunctZ(Complex zValue){
this.zValue = Complex.copy(zValue);
Complex num = this.zNumer.evaluate(zValue);
Complex den = this.zDenom.evaluate(zValue);
return num.over(den).arg();
}
// Get the integration method option
public int getIntegMethod(){
return this.integMethod;
}
// Get the z transform method option
public int getZtransformMethod(){
return this.ztransMethod;
}
// Get the length of the time, input (time domain) and output (time domain) arrays
public int getSampleLength(){
return this.sampLen;
}
// Get the forgetting factor
public double getForgetFactor(){
return this.forgetFactor;
}
// Get the current time
public double getCurrentTime(){
return this.time[this.sampLen-1];
}
// Get the time array
public double[] getTime(){
return this.time;
}
// Get the current time domain input
public double getCurrentInputT(){
return this.inputT[this.sampLen-1];
}
// Get the time domain input array
public double[] getInputT(){
return this.inputT;
}
// Get the s-domain input
public Complex getInputS(){
return this.inputS;
}
// Get the sampling period
public double getDeltaT(){
return this.deltaT;
}
// Get the sampling frequency
public double getSampFreq(){
return this.sampFreq;
}
// Get the Laplacian s value
public Complex getS(){
return this.sValue;
}
// Get the z-transform z value
public Complex getZ(){
return this.zValue;
}
// Get the degree of the original s-domain numerator polynomial
public int getSnumerDeg(){
return this.sNumerDeg;
}
// Get the degree of the s-domain numerator polynomial after any dead time Pade approximation added
public int getSnumerPadeDeg(){
return this.sNumerDegPade;
}
// Get the degree of the original s-domain denominator polynomial
public int getSdenomDeg(){
return this.sDenomDeg;
}
// Get the degree of the s-domain denominator polynomial after any dead time Pade approximation added
public int getSdenomPadeDeg(){
return this.sDenomDegPade;
}
// Get the original s-domain numerator polynomial
public ComplexPoly getSnumer(){
return this.sNumer.times(this.sNumerWorkingFactor);
}
// Get the s-domain numerator polynomial after any dead time Pade approximation added
public ComplexPoly getSnumerPade(){
return this.sNumerPade.times(this.sNumerWorkingFactor);
}
// Get the original s-domain denominator polynomial
public ComplexPoly getSdenom(){
return this.sDenom.times(this.sDenomWorkingFactor);
}
// Get the s-domain denominator polynomial after any dead time Pade approximation added
public ComplexPoly getSdenomPade(){
return this.sDenomPade.times(this.sDenomWorkingFactor);
}
// Get the degree of the z-domain numerator polynomial
public int getZnumerDeg(){
return this.zNumerDeg;
}
// Get the degree of the z-domain denominator polynomial
public int getZdenomDeg(){
return this.zDenomDeg;
}
// Get the z-domain numerator polynomial
public ComplexPoly getZnumer(){
return this.zNumer;
}
// Get the z-domain denominator polynomial
public ComplexPoly getZdenom(){
return this.zDenom;
}
// Get the s-domain zeros without any Pade zeros
public Complex[] getZerosS(){
if(this.sZeros==null)this.calcPolesZerosS();
if(this.sZeros==null){
System.out.println("Method BlackBox.getZerosS:");
System.out.println("There are either no s-domain zeros for this transfer function");
System.out.println("or the s-domain numerator polynomial has not been set");
System.out.println("null returned");
return null;
}
else{
return this.sZeros;
}
}
// Get the s-domain zeros plusany Pade zeros
public Complex[] getZerosPadeS(){
if(this.sZeros==null)this.calcPolesZerosS();
if(!this.padeAdded)this.transferPolesZeros();
if(this.sZerosPade==null){
System.out.println("Method BlackBox.getZerosPadeS:");
System.out.println("There are either no s-domain zeros for this transfer function");
System.out.println("or the s-domain numerator polynomial has not been set");
System.out.println("null returned");
return null;
}
else{
return this.sZerosPade;
}
}
// Get the s-domain poles without any Pade poles
public Complex[] getPolesS(){
if(this.sPoles==null)this.calcPolesZerosS();
if(this.sPoles==null){
System.out.println("Method BlackBox.getPolesS:");
System.out.println("There are either no s-domain poles for this transfer function");
System.out.println("or the s-domain denominator polynomial has not been set");
System.out.println("null returned");
return null;
}
else{
return this.sPoles;
}
}
// Get the s-domain poles plus any Pade poles
public Complex[] getPolesPadeS(){
if(this.sPoles==null)this.calcPolesZerosS();
if(!this.padeAdded)this.transferPolesZeros();
if(this.sPolesPade==null){
System.out.println("Method BlackBox.getPolesPadeS:");
System.out.println("There are either no s-domain poles for this transfer function");
System.out.println("or the s-domain denominator polynomial has not been set");
System.out.println("null returned");
return null;
}
else{
return this.sPolesPade;
}
}
// Get the z-domain zeros
public Complex[] getZerosZ(){
if(this.zZeros==null){
System.out.println("Method BlackBox.getZerosZ:");
System.out.println("There are either no z-domain zeros for this transfer function");
System.out.println("or the z-domain numerator polynomial has not been set");
System.out.println("null returned");
return null;
}
else{
return this.zZeros;
}
}
// Get the z-domain poles
public Complex[] getPolesZ(){
if(this.zPoles==null){
System.out.println("Method BlackBox.getPolesZ:");
System.out.println("There are either no z-domain poles for this transfer function");
System.out.println("or the z-domain denominator polynomial has not been set");
System.out.println("null returned");
return null;
}
else{
return this.zPoles;
}
}
// Get the map infinity zeros to zero or -1 option
// maptozero: if true infinity s zeros map to zero
// if false infinity s zeros map to minus one
public boolean getMaptozero(){
return this.maptozero;
}
// Get the name of the black box
public String getName(){
return this.name;
}
// Plot the poles and zeros of the BlackBox transfer function in the s-domain
// Excludes any Pade poles and zeros
public void plotPoleZeroS(){
if(this.sNumer==null)throw new IllegalArgumentException("s domain numerator has not been set");
if(this.sDenom==null)throw new IllegalArgumentException("s domain denominator has not been set");
PlotPoleZero ppz = new PlotPoleZero(this.sNumer, this.sDenom);
ppz.setS();
ppz.pzPlot(this.name);
}
// Plot the poles and zeros of the BlackBox transfer function in the s-domain
// Includes Pade poles and zeros
public void plotPoleZeroPadeS(){
if(!this.padeAdded)this.transferPolesZeros();
if(this.sNumerPade==null)throw new IllegalArgumentException("s domain numerator has not been set");
if(this.sDenomPade==null)throw new IllegalArgumentException("s domain denominator has not been set");
PlotPoleZero ppz = new PlotPoleZero(this.sNumerPade, this.sDenomPade);
ppz.setS();
ppz.pzPlot(this.name);
}
// Plot the poles and zeros of the BlackBox transfer function in the z-domain
public void plotPoleZeroZ(){
PlotPoleZero ppz = new PlotPoleZero(this.zNumer, this.zDenom);
if(this.zNumer==null)throw new IllegalArgumentException("z domain numerator has not been set");
if(this.zDenom==null)throw new IllegalArgumentException("z domain denominator has not been set");
ppz.setZ();
ppz.pzPlot(this.name);
}
// Bode plots for the magnitude and phase of the s-domain transfer function
public void plotBode(double lowFreq, double highFreq){
if(!this.padeAdded)this.transferPolesZeros();
int nPoints = 100;
double[][] cdata = new double[2][nPoints];
double[] logFreqArray = new double[nPoints+1];
double logLow = Fmath.log10(2.0D*Math.PI*lowFreq);
double logHigh = Fmath.log10(2.0D*Math.PI*highFreq);
double incr = (logHigh - logLow)/((double)nPoints-1.0D);
double freqArray = lowFreq;
logFreqArray[0]=logLow;
for(int i=0; i1.e-5){
outTest=false;
}
else{
System.out.println("output in Blackbox.getCurrentOutputT() has a significant imaginary part");
System.out.println("time = " + this.time[this.sampLen-1] + " real = " + outReal + " imag = " + outImag);
System.out.println("Output equated to the real part");
}
}
//for(int i=0; i1.e-5){
System.out.println("BlackBox inverseTransformToReal coefficient A[" + i + "] has a significant imaginary part: " + com[0][i]);
System.out.println("A equated to the real part");
System.out.println("inverseTransform method may be more appropriate");
}
ret[1][i] = com[1][i].getReal();
if(Math.abs((ret[1][i]-com[1][i].getImag())/ret[1][i])>1.e-5){
System.out.println("BlackBox inverseTransformToReal coefficient a[" + i + "] has a significant imaginary part: " + com[1][i]);
System.out.println("a equated to the real part");
System.out.println("inverseTransform method may be more appropriate");
}
ret[2][i] = com[2][i].getReal();
}
return ret;
}
// Returns the coefficients A, the constant a and the power n in the f(A.exp(-at),n) term for the
// the inverse Laplace transform of a complex polynolial divided
// by a complex polynomial expanded as partial fractions
// A and a are returnd as a 3 x n Complex array were n is the number of terms
// in the partial fraction. The first row contains the A values, the second the a values and the third the power of the denominator
// denominator scaling factor calculated
public static Complex[][] inverseTransform(ComplexPoly numer, ComplexPoly denom){
Complex[] roots = denom.rootsNoMessages();
Complex magDenom = BlackBox.scaleFactor(denom, roots);
Complex magNumer = Complex.plusOne();
return inverseTransform(numer, denom, magNumer, magDenom);
}
// Returns the coefficients A, the constant a and the power n in the f(A.exp(-at),n) term for the
// the inverse Laplace transform of a complex polynolial divided
// by a complex polynomial expanded as partial fractions
// A and a are returnd as a 3 x n Complex array were n is the number of terms
// in the partial fraction. The first row contains the A values, the second the a values and the third the power of the denominator
// denominator scaling factor provided
public static Complex[][] inverseTransform(ComplexPoly numer, ComplexPoly denom, Complex magNumer, Complex magDenom){
int polesN = denom.getDeg(); // number of poles
int zerosN = numer.getDeg(); // numer of zeros
if(zerosN>=polesN)throw new IllegalArgumentException("The degree of the numerator is equal to or greater than the degree of the denominator");
Complex[][] ret = Complex.twoDarray(3, polesN); // array for returning coefficients, constants and powers
// Special case: input = A/(B + C)s
if(polesN==1 && zerosN==0){
Complex num = numer.coeffCopy(0);
Complex den0 = denom.coeffCopy(0);
Complex den1 = denom.coeffCopy(1);
ret[0][0] = num.over(den1);
ret[1][0] = Complex.minusOne().times(den0.over(den1));
ret[2][0] = new Complex(1.0, 0.0);
return ret;
}
int nDifferentRoots = polesN; // number of roots of different values
int nSetsIdenticalRoots = 0; // number of sets roots of identical value
Complex[] poles = denom.rootsNoMessages(); // poles array
int[] polePower = new int[polesN]; // power, n, of each (s - root)^n term
boolean[] poleSet = new boolean[polesN]; // true if root has been identified as either equal to another root
int[] poleIdent = new int[polesN]; // same integer for identical (s-root) terms; integer = index of first case of that root
int[] poleHighestPower = new int[polesN]; // highest pole power for that set of identical poles
boolean[] termSet = new boolean[polesN]; // false if n in (s-root)^n is greater than 1 and less than maximum value of n for that root
double identicalRootLimit = 1.0e-2; // roots treated as identical if equal to one part in identicalRootLimit
int[] numberInSet = new int[polesN]; // number of poles indentical to this pole including this pole
// Find identical roots within identicalRootLimit and assign power n [ (s-a)^n] to all roots
int power = 0;
Complex identPoleAverage = new Complex();
int lastPowerIndex=0;
for(int i=0; i0){
for(int i=0; i1 && !subSet[i]){
currentNumberInSet = numberInSet[i];
shifts = Complex.oneDarray(numberInSet[i]);
int centre = numberInSet[i]/2;
if(Fmath.isEven(numberInSet[i])){
for(int j=0; j1000000)throw new IllegalArgumentException("a non repeating set of substitution values could not be foumd");
}
else{
jj++;
}
if(jj>=polesN)testjj = false;
}
ii++;
if(ii>=polesN-1)testii = false;
}
// Set up the linear equations
// Create vector and matrix arrays
Complex[][] mat = Complex.twoDarray(polesN, polesN);
Complex[] vec = Complex.oneDarray(polesN);
// Fill vector
for(int i=0; i0){
vec[i] = numer.evaluate(subValues[i]);
}
else{
vec[i] = numer.coeffCopy(0);
}
}
// fill matrix
for(int i=0; i
Ads help maintain this website.