source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/control/FirstOrder.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 12.5 KB
Line 
1/* Class FirstOrder
2*
3* This class contains the constructor to create an instance of
4* a first order process,
5* a.d(output)/dt + b.output = c.input
6* and the methods needed to use this process in simulation
7* of control loops.
8*
9* This class is a subclass of the superclass BlackBox.
10*
11* Author: Michael Thomas Flanagan.
12*
13* Created: August 2002
14* Updated: 20 April 2003, 3 May 2005, 3 April 2006, 2 July 2006, 6 April 2008,
15* 2 December 2008, 2-7 November 2009, 23 May 2010, 24 May 2010
16*
17*
18* DOCUMENTATION:
19* See Michael T Flanagan's JAVA library on-line web page:
20* http://www.ee.ucl.ac.uk/~mflanaga/java/FirstOrder.html
21* http://www.ee.ucl.ac.uk/~mflanaga/java/
22*
23* Copyright (c) 2002 - 2010 Michael Thomas Flanagan
24*
25* PERMISSION TO COPY:
26*
27* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
28* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
29* and associated documentation or publications.
30*
31* 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
32* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
33*
34* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
35* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
36*
37* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
38* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
39* or its derivatives.
40*
41***************************************************************************************/
42
43
44package agents.anac.y2015.agentBuyogV2.flanagan.control;
45
46import agents.anac.y2015.agentBuyogV2.flanagan.complex.Complex;
47import agents.anac.y2015.agentBuyogV2.flanagan.complex.ComplexPoly;
48import agents.anac.y2015.agentBuyogV2.flanagan.plot.*;
49
50public class FirstOrder extends BlackBox{
51
52 private double aConst = 1.0D; // a constant in differential equation above
53 private double bConst = 1.0D; // b constant in differential equation above
54 private double cConst = 1.0D; // c constant in differential equation above
55
56 // Constructor
57 // Sets all constants to unity
58 public FirstOrder(){
59 super("FirstOrder");
60 super.sPoles = Complex.oneDarray(1);
61 super.setSnumer(new ComplexPoly(1.0D));
62 super.setSdenom(new ComplexPoly(1.0D, 1.0D));
63 super.setZtransformMethod(1);
64 super.addDeadTimeExtras();
65 }
66
67 // Constructor
68 // within constants set from argument list
69 public FirstOrder(double aa, double bb, double cc){
70 super("FirstOrder");
71 this.aConst = aa;
72 this.bConst = bb;
73 this.cConst = cc;
74 super.sPoles = Complex.oneDarray(1);
75 super.setSnumer(new ComplexPoly(this.cConst));
76 super.setSdenom(new ComplexPoly(this.bConst, this.aConst));
77 super.setZtransformMethod(1);
78 super.addDeadTimeExtras();
79 }
80
81 // Set coefficients
82 public void setCoeff(double aa, double bb, double cc){
83 this.aConst = aa;
84 this.bConst = bb;
85 this.cConst = cc;
86 Complex[] num = Complex.oneDarray(1);
87 num[0].reset(this.cConst, 0.0);
88 super.sNumer.resetPoly(num);
89 Complex[] den = Complex.oneDarray(2);
90 den[0].reset(this.bConst, 0.0);
91 den[1].reset(this.aConst, 0.0);
92 super.sDenom.resetPoly(den);
93 this.calcPolesZerosS();
94 super.addDeadTimeExtras();
95 }
96
97 public void setA(double aa){
98 this.aConst = aa;
99 Complex co = new Complex(this.aConst, 0.0);
100 super.sDenom.resetCoeff(1, co);
101 this.calcPolesZerosS();
102 super.addDeadTimeExtras();
103 }
104
105 public void setB(double bb){
106 this.bConst = bb;
107 Complex co = new Complex(this.bConst, 0.0);
108 super.sDenom.resetCoeff(0, co);
109 this.calcPolesZerosS();
110 super.addDeadTimeExtras();
111 }
112
113 public void setC(double cc){
114 this.cConst = cc;
115 Complex co = new Complex(this.cConst, 0.0);
116 super.sNumer.resetCoeff(0, co);
117 this.calcPolesZerosS();
118 super.addDeadTimeExtras();
119 }
120
121 // Get coefficients
122 public double getA(){
123 return this.aConst;
124 }
125
126 public double getB(){
127 return this.bConst;
128 }
129
130 public double getC(){
131 return this.cConst;
132 }
133
134 // Get time constant
135 public double getTimeConstant(){
136 return this.aConst/this.bConst;
137 }
138
139 // Calculate the zeros and poles in the s-domain
140 protected void calcPolesZerosS(){
141 super.sPoles = Complex.oneDarray(1);
142 super.sPoles[0].setReal(-bConst/aConst);
143 if(super.sNumerSet)super.sNumerScaleFactor = super.sNumer.coeffCopy(0);
144 if(super.sDenomSet)super.sDenomScaleFactor = BlackBox.scaleFactor(super.sDenom, super.sPoles);
145
146 }
147
148 // Plots the time course for a step input
149 public void stepInput(double stepMag, double finalTime){
150
151 if(this.bConst/this.aConst==0.0){
152 // Calculate time course outputs
153 int n = 51; // number of points on plot
154 double incrT = finalTime/(double)(n-2); // plotting increment
155 double cdata[][] = new double [2][n]; // plotting array
156
157 cdata[0][0]=0.0D;
158 cdata[0][1]=0.0D;
159 for(int i=2; i<n; i++){
160 cdata[0][i]=cdata[0][i-1]+incrT;
161 }
162 double kpterm = this.cConst*stepMag/this.bConst;
163 cdata[1][0]=0.0D;
164 for(int i=1; i<n; i++){
165 cdata[1][i] = kpterm;
166 }
167 if(super.deadTime!=0.0D)for(int i=0; i<n; i++)cdata[0][i] += super.deadTime;
168
169 // Plot
170 PlotGraph pg = new PlotGraph(cdata);
171
172 pg.setGraphTitle("Step Input Transient: Step magnitude = "+stepMag);
173 pg.setGraphTitle2(this.getName());
174 pg.setXaxisLegend("Time");
175 pg.setXaxisUnitsName("s");
176 pg.setYaxisLegend("Output");
177 pg.setPoint(0);
178 pg.setLine(3);
179 pg.plot();
180 }
181 else{
182 super.stepInput(stepMag, finalTime);
183 }
184 }
185
186
187 // Perform z transform using an already set delta T
188 public void zTransform(){
189 if(super.deltaT==0.0D)System.out.println("z-transform attempted in FirstOrder with a zero sampling period");
190 super.deadTimeWarning("zTransform");
191 if(ztransMethod==0){
192 this.mapstozAdHoc();
193 }
194 else{
195 Complex[] ncoef = null;
196 Complex[] dcoef = null;
197 switch(this.integMethod){
198 // Trapezium rule
199 case 0: ncoef = Complex.oneDarray(2);
200 ncoef[0].reset(this.deltaT*this.cConst,0.0D);
201 ncoef[1].reset(this.deltaT*this.cConst,0.0D);
202 super.zNumer=new ComplexPoly(1);
203 super.zNumer.resetPoly(ncoef);
204 super.zNumerDeg=1;
205 dcoef = Complex.oneDarray(2);
206 dcoef[0].reset(this.bConst*this.deltaT - 2*this.aConst,0.0D);
207 dcoef[1].reset(this.bConst*this.deltaT + 2*this.aConst,0.0D);
208 super.zDenom=new ComplexPoly(1);
209 super.zDenom.resetPoly(dcoef);
210 super.zDenomDeg=1;
211 super.zZeros = Complex.oneDarray(1);
212 super.zZeros[0].reset(-1.0D, 0.0D);
213 super.zPoles = Complex.oneDarray(1);
214 super.zPoles[0].reset((2.0D*this.aConst-super.deltaT*this.bConst)/(2.0D*this.aConst+super.deltaT*this.bConst), 0.0D);
215 break;
216 // Backward rectangulr rule
217 case 1: ncoef = Complex.oneDarray(2);
218 ncoef[0].reset(0.0D,0.0D);
219 ncoef[1].reset(this.cConst*this.deltaT,0.0D);
220 super.zNumer=new ComplexPoly(1);
221 super.zNumer.resetPoly(ncoef);
222 super.zNumerDeg=1;
223 dcoef = Complex.oneDarray(2);
224 dcoef[0].reset(this.bConst*this.deltaT + this.aConst,0.0D);
225 dcoef[1].reset(this.aConst,0.0D);
226 super.zDenom=new ComplexPoly(1);
227 super.zDenom.resetPoly(dcoef);
228 super.zDenomDeg=1;
229 super.zZeros = Complex.oneDarray(1);
230 super.zZeros[0].reset(0.0D, 0.0D);
231 super.zPoles = Complex.oneDarray(1);
232 super.zPoles[0].reset(this.aConst/(super.deltaT*this.bConst+this.aConst), 0.0D);
233 break;
234 // Foreward rectangular rule
235 case 2: ncoef = Complex.oneDarray(1);
236 ncoef[0].reset(this.cConst*this.deltaT,0.0D);
237 super.zNumer=new ComplexPoly(0);
238 super.zNumer.resetPoly(ncoef);
239 super.zNumerDeg=0;
240 dcoef = Complex.oneDarray(2);
241 dcoef[0].reset(-this.aConst,0.0D);
242 dcoef[1].reset(this.bConst*this.deltaT - this.aConst,0.0D);
243 super.zDenom=new ComplexPoly(1);
244 super.zDenom.resetPoly(dcoef);
245 super.zDenomDeg=1;
246 super.zPoles = Complex.oneDarray(1);
247 super.zPoles[0].reset(this.aConst/(super.deltaT*this.bConst-this.aConst), 0.0D);
248 break;
249 default: System.out.println("Integration method option in FirstOrder must be 0,1 or 2");
250 System.out.println("It was set at "+integMethod);
251 System.out.println("z-transform not performed");
252 }
253 }
254 }
255
256 // Perform z transform setting delta T
257 public void zTransform(double deltaT){
258 super.setDeltaT(deltaT);
259 this.zTransform();
260 }
261
262 // Get the s-domain output for a given s-value and a given input.
263 public Complex getOutputS(Complex sValue, Complex iinput){
264 super.sValue=sValue;
265 super.inputS=iinput;
266 return this.getOutputS();
267 }
268
269 // Get the s-domain output for the stored input and s-value.
270 public Complex getOutputS(){
271 Complex num = Complex.plusOne();
272 num = num.times(this.cConst);
273 Complex den = new Complex();
274 den = this.sValue.times(this.aConst);
275 den = den.plus(this.bConst);
276 Complex term = new Complex();
277 term = num.over(den);
278 super.outputS = term.times(super.inputS);
279 if(super.deadTime!=0.0D)super.outputS = super.outputS.times(Complex.exp(super.sValue.times(-super.deadTime)));
280 return super.outputS;
281 }
282
283 // Calculate the time domain output for a given input and given time
284 public double calcOutputT(double time, double input){
285 super.setInputT(time, input);
286 return calcOutputT();
287 }
288
289 // Calculates the time domain output for the stoted input and time
290 public double calcOutputT(){
291 super.deadTimeWarning("calcOutputT()");
292 return super.getCurrentOutputT();
293 }
294
295 // Get the s-domain zeros
296 public Complex[] getSzeros(){
297 System.out.println("This standard first order process (class FirstOrder) has no s-domain zeros");
298 return null;
299 }
300
301
302 // Deep copy
303 public FirstOrder copy(){
304 if(this==null){
305 return null;
306 }
307 else{
308 FirstOrder bb = new FirstOrder();
309 this.copyBBvariables(bb);
310
311 bb.aConst = this.aConst;
312 bb.bConst = this.bConst;
313 bb.cConst = this.cConst;
314
315 return bb;
316 }
317 }
318
319 // Clone - overrides Java.Object method clone
320 public Object clone(){
321 return (Object)this.copy();
322 }
323}
Note: See TracBrowser for help on using the repository browser.