Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Lecture Set 1
Some Basic ToolsSome Basic Tools
S.D. Sudhoff
Spring 2010
1
Outline
• Time Domain Simulation (ECE546, MA514)• Basic Methods for Time Domain Simulation• MATLAB• ACSL
• Single and Multi-Objective Optimization (ECE580) (ECE580) • GOSET
2
Time-Domain Simulation
• Resistor-Companion (Circuit) Simulation• The Good
• The Bad
• Languages• SPICE, PSPICE, SABER,PSCAD • SPICE, PSPICE, SABER,PSCAD
• State-Variable (System) Simulation• The Good
• The Bad
• Languages• Matlab/Simulink, ACSL, EASY5, Dymola, PLECS
3
Background: Integration Methods
• Consider
• Forward Euler
( , )dx
f x udt
=
4
Background: Basic Integration Methods
• Backward Euler
• Trapezoidal
5
Summary
• We have
( )
1
1 1 1
1 1 1
( , )
( , )
( , ) ( , )2
k k k k
x k k k
k k k k k k
x x hf x u
x x hf x u
hx x f x u f x u
+
+ + +
+ + +
= += +
= + +
6
2
Convergence of Forward Euler
7
Convergence of Backward Euler
8
Resistor-Companion Simulation [1]
9
[1] L.O. Chua, P.M. Lin, Computer Aided Analysis of Electric Circuits:Algorithms & Computation Techniques, Prentice-Hall, 1975.
Resistor-Companion Simulation [1]
10
Comments on Resistive Companion Simulation
• Good
• Bad
11
State Variable Based Simulation
• The Problem
• Solution Methods
f( , )d
pdt
= =x x x u
• Explicit• Forward Euler
• Runga-Kutta
• Implicit • Backward Euler
• Trapezoidal
• Gears 12
Forward Euler
• Recall
• Bad Features
1 ( , )k k k kx x hf x u+ = +
• Good Features
13
Quick Example
• Suppose we have
• And that122
12211
1.0
1.0
xxpx
uxxpx
+−=++−=
• Find x(t2) where t2=t1+∆t; ∆t=0.1 s
0)(
1)(
5)(
11
12
11
===
tu
tx
tx
14
Quick Example (Continued)
15
Observations from Euler’s Method
• To predict a future value of state, we needed to find the present value of the time derivative of the state variables, based on what we know – that is the present value of state variables and present value of input variables.and present value of input variables.
• This is true of all other explicit integration algorithms as well
16
The Runge-Kutta Algorithm
• A practical explicit algorithm for
• The fourth-order implementation (RK4)
1 f( , )n nt=k x
f( , )d
tdt
=x x
17
( )
1
2 1
3 2
4 3
1 1 2 3 4
1
f( , )
1 1f ,
2 2
1 1f ,
2 2
f ,
1( 2 2 )
6
n n
n n
n n
n n
n n
n n
t
t h h
t h h
t h x h
t t h
+
+
=
= + +
= + +
= + +
= + + + +
= +
k x
k x k
k x k
k k
x x k k k k
Trapezoidal Predictor-Corrector Method
• Again consider
• Algorithm• Step 1- Initial Guess
f( , )d
tdt
=x x
18
• Step 2 – Iterate
until g=G or
• Step 3 – Finish step
1,1 f( , )n n n nx h t+ = +x xɶ
( )1, 1 1 1,f( , ) f( , )2n g n n n n n gh
x t t+ + + += + +x x xɶ ɶ
, 1 ,n g n g e+ −
MATLAB Example
• Lets consider an average-value model of a dc/dc converter
ˆ ˆ
ˆ ˆls o
us L
v dv
i di
=
=
19
0
0
ˆˆ
ˆˆ ˆˆ
ˆ ˆˆ
us L
out
in ls L LL
us out
i di
vi
R
v v r ipi
L
i ipv
C
=
=
− −=
−=
dcdc_sim.m% A simple simulation of a dc/dc converter
% model parametersP.L=5e-3;P.rL=0.5;P.C=1000e-6;P.R=10.0;P.d=0.5;P.vin=300;
% time values where we want the answertvals=linspace(0,0.2,200);
% grab the waveformsil=x(:,1);vout=x(:,2);
% plot the currentfigure(1)plot(tvals,il)xlabel('t, s');ylabel('i_L, A');axis([0 0.2 0 400]);title('Fast Average Inductor Current');
20
% initial condition xic=[0 0];
% maximum time stepmaxt=2e-5;
% peform the simulation[t,x]=odefsrk(@sdcdc,tvals,xic,P,maxt);
% plot the voltagefigure(2)plot(tvals,vout)xlabel('t, s');ylabel('v_{out}, A');axis([0 0.2 0 1000]);title('Fast Average Output Voltage')
RK4 (odefsrk.m)function [t,y]=odefsrk(fhandle,tspan,yic,par,maxt)% This routine solves a ordinary differential equation% using a 4th order Runga-Kutta method.% % [t,y] = odefsrk(fhandle,tspan,yic,par,maxt);%% Inputs:%% fhandle = a handle to the function whose output is the time derivative% of the system model. The inputs to this function are time,% state, and parameter vales.% tspan = a vector whose elements describe at which point in time the% solution is sought
21
% solution is sought% yic = a vector which describes the initial condition of the system% being simulated% par = a structure which cointains data or parameters needed to % evaluate the time derivative of the state variables% maxt = the maximum allowed time step% % Outputs:%% t = a vector of times at which the state vector has been found% y = a matrix wherein each row cointains the state vector at a% given time. Each column is the time history of a particular% state%%
sdcdc.mfunction [varargout]=sdcdc(t,x,P)% This routine contains the dynamics of an simple dc/dc converter% model% % [px] = sdcdc(t,x,P);% [il,vout,vls,ius,iout] = sdcdc(t,x,P);%% Inputs:%% t = time (s)% x = state vector% x(1) = fast average of inductor current (A)% x(2) = fast average of output voltage (V)
22
% x(2) = fast average of output voltage (V)% P = structure with parameters% P.L = inductor inductance (H)% P.rL = inductor series resistance (Ohms)% P.C = capacitor capacitance (F)% P.R = load resistance (Ohms)% P.vin = input voltage (V)% P.d = duty cycle (upper switch on / total period)% % Outputs:%% px = time derivative of state vector% px(1) = time derivative of fast average of inductor current (A)% px(2) = time derivative of fast average of output voltage (V)% il = fast average inductor current (A)% vout = fast average output voltage (V)% vls = fast average lower switch voltage (V)% ius = fast average current out ot upper switch (A)%
sdcdc.m (continued)% Internal:%% pil = time derivative of il (A/s)% pvout = time derivative of vout (V/s)%% Written by S.D. Sudhoff % School of Electrical and Computer Engineering% 1285 Electrical Engineering Building% West Lafayette, IN 47907-1285% Phone: 765-494-3246% Fax: 765-494-0676% E-mail: [email protected]
23
% decompose state vectoril=x(1);vout=x(2);
% variables of interestvls=P.d*vout;ius=P.d*il;iout=vout/P.R;
% compute derivatives of statepil=(P.vin-P.rL*il-vls)/P.L;pvout=(ius-iout)/P.C;
% pack the state vectorpx=[pil pvout]';
sdcdc.m (continued again)
% output variablesif nargout==1
varargout={px};else
varargout={px,vls,ius,iout};end
end
24
Results
250
300
350
400Fast Average Inductor Current
600
700
800
900
1000Fast Average Output Voltage
Typing ‘dcdc_sim’ from the MATLAB workspace ...
25
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.20
50
100
150
200
t, s
i L,
A
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.20
100
200
300
400
500
600
t, s
v out
, A
ACSL
• ACSL is • A programming language for simulation of ODEs
• It is a non-sequential language
• Consists of• A builder• A builder
• A translator
• A compiler
• A run time environment
26
ACSL - The Bad and Good
• Bad Features• Hard to learn
• Not very forgiving
• Easy to write poor code
• Good Features• Easy to write good code
• Extremely computationally efficient
27
Some Things to Know
• ACSL only sees 72 character lines
• ACSL only likes 8 character file names
• ACSL doesn’t distinguish between upper and lower case
• 2 is an integer. 2.0 is real. Watch your types !
28
ACSL Files
• Project File: *.PRJ (Created By Builder)
• Source Code: *.CSL (Created By You)
• Command File: *.CMD (Created By You)
• Macros: *.MAC (Created By You)
• Fortran and C Routines: (Created By You)• Fortran and C Routines: (Created By You)
• Fortran Program *.F (Created By ACSL)
• Data File *.rrr (Created By ACSL at Runtime)
• Log File *.log (Created By ACSL at Runtime)
• Error File *.err (Created By ACSL)
29
ACSL Program Structure
PROGRAMINITIAL
Statements executed before run beginsState Variables do not contain initial conditions yet
END ! InitialDYNAMIC
DERIVATIVEStatements to be integrated continuouslyStatements to be integrated continuouslyStatements are sorted.
END ! DerivativeDISCRETE
Statements to be executed at discrete points in timeEND ! DISCRETE
Statements to be executed each communication intervalEND ! DynamicTERMINAL
Statements executed after the run terminatesEND ! Terminal
END ! Program
30
ACSL Example 1
• Lets consider an average-value model of a dc/dc converter
ˆ ˆ
ˆ ˆls o
us L
v dv
i di
=
=
31
0
0
ˆˆ
ˆˆ ˆˆ
ˆ ˆˆ
us L
out
in ls L LL
us out
i di
vi
R
v v r ipi
L
i ipv
C
=
=
− −=
−=
Example 1: sdcdc.csl
PROGRAM SDCDC
DYNAMIC
ALGORITHM IALG=4CINTERVAL CINT=2.0e-4MAXTERVAL MAXT=2.0e-5MINTERVAL MINT=2.0e-6NSTEPS NSTEP=1
"compute dependent sources" vls=d*voutius=d*il
"compute derivative of inductor current"piL=(vin-vls-rL*iL)/LiL=INTEG(piL,0.0)
32
NSTEPS NSTEP=1CONSTANT Tstop=0.2TERMT(t .GE. Tstop-1.5*CINT,'Exit On Tstop');
DERIVATIVE
CONSTANT L=5.0e-3CONSTANT rL=0.5CONSTANT C=1000.0e-6CONSTANT R=10.0CONSTANT vin=300.0CONSTANT d=0.5
"compute derivative of output voltage"pvout=(ius-iout)/Cvout=INTEG(pvout,0.0)
"compute output current"iout=vout/r
END ! Derivative
END ! Dynamic
END ! Program
ALGORITHM sets the integration algorithmCINTERVAL sets the rate that data is logged
MAXTERVAL maximum integration interval
Simulation Parameters
MINTERVAL minimum integration interval
NSTEPS number of integration intervals in one cint
TERMT terminates program on logical condition(t.GT.tstop) with message (Exit on Tstop)
33
CONSTANTS sets a variable as a constant
INTEG integrates a variable, for example
Other Statements
INTEG integrates a variable, for examplex=INTEG(px,xic) makes x equal to the time integral of px,with an initial condition of xic
34
ACSL Integration Algorithms
IALG algorithm step order
1 Adams-Moulton variable varible 2 Gear's Stiff variable variable
3 Runge-Kutta (Euler) fixed first
4 Runge-Kutta fixed second
5 Runge-Kutta fixed fourth 5 Runge-Kutta fixed fourth
6 none - -
7 user supplied - -
8 Runge-Kutta-Fehlberg variable second
9 Runge-Kutta-Fehlberg variable fifth
10 Diff. Alg. Sys. Solver variable variable
35
For fixed step algorithms, MINTERVAL is ignored
To directly set the integration step size and data loggingrate, set:
Integration Algorithm Time Steps
ALGORITHM ialg=4MAXTERVAL maxt=(desired step size)CINTERVAL cint=(desired data logging rate)NSTEPS nstep=1
Integration step size will be: min(maxt,cint/nstep)or in this case: min(maxt,cint)
36
For variable step algorithms, set nstep high
For example,
ALGORITHM ialg=2CINTERVAL cint=5e-4
Integration Algorithm Time Steps
CINTERVAL cint=5e-4MAXTERVAL maxt=2e-3MINTERVAL mint=2e-5NSTEPS nstep=1000
Integration step size starts at: cint/nstepand is bounded by mint and maxt.If nstep is large, step size will start out small. 37
Example 1: sdcdc.cmd
s strplt=.t.s calplt=.f.s hvdprn=.f.
prepare t,vhat,ihatprepare il,vl,pilprepare il,vl,pilprepare ic,iout,vout
proced dostudy
startplot il,vout
end
38
s = set a variable
s weditg = .false. disables the write event discriptorso that ACSL will not createexcessive .log files.
Runtime & Command File Commands
excessive .log files.s hvdprn = .false. disables high volume display
so that ACSL does not write highvolume information to the screen.
s strplt = .true. enables strip plotss calplt = .false. disables continuous plotss alcplt= .false. disables plot color (all traces are black)
39
1. Start ACSL builder.2. Select New Project from the Project menu.3. Go to directory where sdcdc.csl
and dcdc.cmd are stored.
Running Example 1
4. Single click on sdcdc.csl5. In the File name box, change the extension to .prj, and hit ‘save’6. In the Files column, single click boostavg.csl, and click ‘Add’7. Select Run ACSL on the Tools menu
40
From ACSL runtime environment, type
Running Example 1
dostudy
41
• To search for errors when compiling, ACSL creates a error file (.out). Perform a search on this file with the word error.
• You can change ialg, maxt, mint, cint, and nstps at the runtime command line (no need to quit ACSL and change the .csl file).
Random Notes on Running ACSL
change the .csl file).
• For controlling the range on plots use /xlo, /xhi, /lo, and /hi
• For example: plot vout/lo=0/hi=1000,il/lo=-200/hi=200/xl=0/xhi=0.0899
• To change the default x-axis variable use /xaxis
• For example: plot vout/xaxis=il 42
• If you change the .csl file, you must quit ACSL and re-start so that ACSL will re-compile.
• You must quit ACSL before you can run ACSL again. If not, you get a link error.
• If you change the .cmdfile, quit ACSL, and re-start
Random Notes on Running ACSL
• If you change the .cmdfile, quit ACSL, and re-start ACSL, the program will not recompile. It will simply issue the new commands.
• For future note: If you change a macro (.mac) file, save a new copy of the .csl file before running ACSL so it compiles properly.
43
More ACSL Features
• SCHEDULE Operator and DISCRETE Blocks
• IF THEN Constructs
• PROCEDURAL Blocks
• MACRO Facilities
44
SCHEDULE Operator and DISCRETE BLOCKS
• Forms:• SCHEDULE block/flag .AT. time-expression
• SCHEDULE block/flag .XZ. real-expression
• SCHEDULE block/flag .XP. real-expression
• SCHEDULE block/flag .XN. real-expression
• Where:• block = optional name of DISCRETE block
• flag = optional flag set true when event occurs
• .AT. = time to schedule event (cannot be in DERIVATIVE block)
• .XP. = positive zero crossing (used in DERIVATIVE block)
• .XN. = negative zero crossing (used in DERIVATIVE block)
• .XZ. = zero crossing (used in DERIVATIVE block) 45
Example 2: A Basketball
• Dynamics:
• Collision:
vpx
gpv
=−=
• Collision:
beforeafter kvv || −=
46
Basket.csl Basket.cmd
PROGRAM BASKET
DERIVATIVE
ALGORITHM IALG=4MAXTERVAL MAXT=1.0e-3MINTERVAL MINT=1.0e-6CINTERVAL CINT=1.0e-3CONSTANT tstop=5.0TERMT(t .GT. tstop,'Exit on Tstop')
CONSTANT g=9.8 ! acceleration due to gravit y m/sCONSTANT v0=0.0 ! initial velocity, m/sCONSTANT x0=2.0 ! initial height m
s weditg=.f.s hvdprn=.f.prepare /all
CONSTANT x0=2.0 ! initial height mCONSTANT k=0.9 ! rebound coefficient
! dynamic model of basketballpv=-gv=INTEG(pv,v0)px=vx=INTEG(v,x0)
! the floor is at height 0SCHEDULE HITFLOOR .XN. X
END
DISCRETE HITFLOORv=-k*v
END
END 47
Results
48
Example 3: Squarewave to RL Load
• Circuit:
• Dynamics: • Dynamics:
Lrivpi /)( −=
49
Square.csl
PROGRAM SQUARE
DERIVATIVE
ALGORITHM IALG=4MAXTERVAL MAXT=1.0e-4MINTERVAL MINT=1.0e-6CINTERVAL CINT=1.0e - 4CINTERVAL CINT=1.0e - 4CONSTANT tstop=0.5TERMT(t .GT. tstop,'Exit on Tstop')
! dynamic model of rl loadCONSTANT l=10.0e-3 ! Inductance in HCONSTANT r=1.0 ! Resistance in OhmsCONSTANT i0=0.0 ! Initial current in Api=(v-r*i)/l ! Time derivative of cu rrent A/si=INTEG(pi,i0) ! Current, A
50
Square.csl (cont.)
! voltage sourceCONSTANT vmag=5.0 ! Voltage magnitudeCONSTANT freq=10.0 ! FrequencyINITIAL
halfperiod=1.0/(2.0*freq); ! Half-Periodv=vmag ! Inital Voltagev=vmag ! Inital VoltageSCHEDULE CHNGSGN .AT. t+halfperiod! Schedule First Sign Change
END
END
DISCRETE CHNGSGNv=-v ! Change sign of voltageSCHEDULE CHNGSGN .AT. t+halfperiod ! Schedule next c hange
END ! Discrete Block
END ! Program
51
Results:
52
ACSL MACRO LANGUAGE
• Primary means of constructing well organized code
• Primary means of constructing component model libraries
53
Example: DC/DC Converter
ˆ ˆ
ˆ ˆls o
us L
v dv
i di
=
=
54
0
0
ˆˆ
ˆˆ ˆˆ
ˆ ˆˆ
us L
out
in ls L LL
us out
i di
vi
R
v v r ipi
L
i ipv
C
=
=
− −=
−=
ADCDC1.CSL
"---------------------------------------------------------------------"" "" Author: S.D. Sudhoff "" Electrical Engineering Building "" 465 Northwestern Avenue "" Purdue University "" West Lafayette, IN 47905-1285 "" (765) 494-3246 "" Date: 7/6/98; Updated 8/16/2010 "" Version: 1.1 "" ""---------------------------------------------------------------------"
55
INCLUDE 'adcdc.mac'
PROGRAM ADCDC1
DERIVATIVE
ALGORITHM IALG=4MAXTERVAL MAXT=1.0e-4CINTERVAL CINT=1.0e-4TERMT(t .GE. tstop,'Terminated on Tstop')CONSTANT tstop=0.2
"ideal power source"CONSTANT vin=300.0
ADCDC1.CSL (Cont.)
"no control"CONSTANT d=0.5
"dc/dc converter"ADCDC(con1,d,vin,iout,vout,iin, &
"Lcon1=5.0e-3","rLcon1=0.5","Ccon1=1000.0e-6")
"resistive load on converter"CONSTANT R=10.0iout=vout/R
END ! Derivative
56
END ! Program
ADCDC.MAC
"---------------------------------------------------------------------"" "" Author: S.D. Sudhoff "" Electrical Engineering Building "" 465 Northwestern Avenue "" Purdue University "" West Lafayette, IN 47905-1285 "" (765) 494-3246 "" Date: 7/6/98; Updated 8/17/2010. "" Version: 1.0 "" ""---------------------------------------------------------------------"" --------------------------------------------------------------------- "" --------------------------------------------------------------------- "" "" MACRO: ADCDC "" DESCRIPTION: NLAM model of two quadrant dc/dc converter "" "" CONCATENATION "" "" z - identifier "" "" INPUTS "" "" d - duty cycle (upper switch on over total period) "" vin - input voltage (V) "" iout - current out of output terminal (A) "" “
57
ADCDC.MAC (Cont)
" OUTPUTS "" "" vout - output voltage (V) "" iL - inductor current (current into input) (A) "" "" PARAMETERS "" "" L&z - inductance of leg inductor (H) "" rL&z - resistance of leg inductor (Ohms) "" C&z - output capacitance (F) "" "" INTERNAL "" "
58
" "" vls - average voltage across lower switch (V) "" ius - average current out of upper switch (A) "" p&iL - time derivative of iin (A/s) "" p&vout - time derivative of vout (V/s) "" ""---------------------------------------------------------------------"
MACRO ADCDC(z,d,vin,iout,vout,iL,par_L,par_rL,par_C)
INITIAL
CONSTANT par_LCONSTANT par_rLCONSTANT par_C
END
ADCDC.MAC (Cont.)
MACRO REDEFINE vls,ius
"compute the average voltage across the lower switch"vls=d*vout
"compute the current out of the upper switch"ius=d*iin
"compute the input current"p&iL=(vin-vls-rL&z*iL)/L&ziL=INTEG(p&iL,0.0)
59
"compute the output capacitor current"p&vout=(ius-iout)/C&zvout=INTEG(p&vout,0.0)
MACRO END
Important Points
• MACRO REDEFINE
• Concatenation (&)
• Concatenation variable (&z)
• INITIAL Section
• Parameter passing
• MACROS can call MACROS
• Extensive MACRO Language Available
60
ADCDC1.CMD"---------------------------------------------------------------------"" "" Author: S.D. Sudhoff "" Electrical Engineering Building "" 465 Northwestern Avenue "" Purdue University "" West Lafayette, IN 47905-1285 "" (765) 494-3246 "" Date: 7/6/98 "" Version: 1.0 "" ""---------------------------------------------------------------------"
61
s hvdprn=.f.prepare /all
proced study1
startplot iin,vout
end
proced study2
action/var=0.1/val=0.75/loc=dstartaction /clearplot iin,vout
end
RESULTS
62
study1 study2
IF, IF THEN ELSE
IF (lexpr) STATEMENT
IF (lexpr) THEN
• Forms:
IF (lexpr) THENblock1
ELSE block2
END IF
63
IF, IF THEN ELSE
• SORTING RULES• Variables on left must be defined for all possible
cases, or disaster can result
• Material in IF THEN construct moved as a block when sorting with variables on the right as inputs when sorting with variables on the right as inputs and variables on the left as outputs
64
IF THEN Example
IF (x .GT. 0) THENz=sqrt(x)y=z**2y=z**2
ELSEy=x**2z=2*y
END IF
65
ACSL PROCEDURALS
• Allows user to specify order of operations
• Internal sequence not seen or affected by sorter
• Handy for assigning elements of arrays, for iterative solutions, or situations where you iterative solutions, or situations where you may want to recompute a variable if it goes out of range
66
PROCEDURAL Constructs
PROCEDURAL(x1,x2,..xn = y1,y2)unsorted statemens
END
• Syntax
END
• ExamplePROCEDURAL(v=i1,i3)
v(1)=3*i1+i3v(2)=i1*i3
END
67
PROCEDURAL Constructs
• 2nd Example
PROCEDURAL(y=x,z)y=x*3 - z^2y=x*3 - z^2IF (y .LT. 0.0) THEN
y=-yEND IF
END
68
PROCEDURAL Constructs
• 3rd Example
PROCEDURAL(y=x,v,N)PROCEDURAL(y=x,v,N)y=0.0DO L10 i=1,N
y=y+x(i)*v(i)L10.. CONTINUE
END
69