You are on page 1of 3

HOMEWORK.

CPP

February 15, 2012

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int i, nstate1, nstate2;
float q, g, ptie;
float k = 0.5;
float step();
float pload1, pload2;
float s[10], ds[10], zs[10];
// defining states array of hydro plant control
float x[8], dx[8], zx[8];
// system and steam plant control system.
float error1, error2, t, nref1, nref2, cout1, cout2; // t = time
float h1 = 4;
float h2 = 3;
float d1 = 4;
float d2 = 4;
float tb = 10;
float tc = 3;
float ta = 0.5;
float r1 = 0.04;
float r2 = 0.04;
float tw = 2;
float tg = 0.5;
float tp = 0.5;
float kp = 2.5;
float ki = 1.25;
float delt = 0.005;
int main (int argc, char *argv[])
{
cout<<"Starting your program"<<endl;
if (argc != 2)
{
cout<<"You forgot to enter the file name"<<endl;
exit(1);
}
ofstream out (argv[1]);
if (!out)
{
cout<<"cannot open file"<<endl;
exit(1);
}
out<<"Program - simulate the power system"<<endl;
out<<"governing loops"<<endl;
out<<"---"<<endl;
out<<"----"<<endl;
out<<"-----"<<endl;
out<<"Time, x0, x1, x2, x3, x4, s0, s1, s2, s3, s4, s5, s6, s7"<<endl;
out<<"data"<<endl;
// setting initial conditions for the tie line x[3] = 0;
s[5] = 1;
ptie = k*(s[5]-x[3]);
// setting initial conditions
// - steam plant nstate1 = 5;
pload1 = 1.25;
x[0] = pload1-ptie;
x[4] = pload1-ptie;
x[1] = (1-(tc/tb))*(pload1-ptie);
x[2] = 1;
nref1 = 1+r1*(pload1-ptie);
for (i=0; i<nstate1; ++i)

Page 1

HOMEWORK.CPP

February 15, 2012

{
zx[i] = x[i];
dx[i] = 0;
}
// - hydro plant nstate2 = 8;
pload2 = 0.25;
q = 0.75;
g = 0.75;
error2 = 0;
s[0] = pload2+ptie;
s[1] = pload2+ptie;
s[2] = (1+(2*q)/g)*(pload2+ptie);
s[3] = 1;
s[4] = pload2+ptie;
s[6] = pload2+ptie;
s[7] = pload2+ptie;
nref2 = error2+1+r2*(pload2+ptie);
for (i=0; i<nstate2; ++i)
{
zs[i] = s[i];
ds[i] = 0;
}
// start simulation
t = 0;
while (t <= 300)
{
if (t >= 5)
{
pload1 = 1.35;
ptie = k*(s[5]-x[3]);
}
error1 = nref1-x[2];
cout1 = (1/r1)*error1;
dx[0] = (cout1-x[0])/ta;
dx[1] = (((1-(tc/tb))*x[0])-x[1])/tb;
dx[4] = 0;
dx[2] = (x[4]-(pload1-ptie)-(d1*(x[2]-1)))/(2*h1);
dx[3] = x[2];
error2 = nref2-(r2*s[4])-s[3];
cout2 = kp*error2;
ds[0] = ki*error2;
ds[6] = 0;
ds[1] = (s[6]-s[1])/tg;
ds[2] = (((1+((2*q)/g))*s[1])-s[2])/((g*tw)/2);
ds[7] = 0;
ds[3] = (s[7]-(pload2+ptie)-(d2*(s[3]-1)))/(2*h2);
ds[4] = (s[1]-s[4])/tp;
ds[5] = s[3];
out<<t<<" "<<x[0]<<" "<<x[1]<<" "<<x[2]<<" "<<x[3]<<" "<<x[4]<<" "<<s[0]<<" ";
out<<s[1]<<" "<<s[2]<<" "<<s[3]<<" "<<s[4]<<" "<<s[5];
out<<" "<<s[6]<<" "<<s[7]<<endl;
step();
t += delt;
}
out<<"end of simulation"<<endl;
return (0);
}
float step()
{
int i;
for (i=0; i<nstate1; ++i)
{

Page 2

HOMEWORK.CPP
if (i==4)
{
x[4] = x[1]+((tc/tb)*x[0]);
goto start;
}
zx[i] += dx[i]*delt;
x[i] = zx[i]+(dx[i]*(delt/2));
start:

February 15, 2012

// start is at the end of this for loop block.

}
for (i=0; i<nstate2; ++i)
{
if (i==6)
{
s[6] = s[0]+cout2;
goto loop;
// loop is at the end of this for loop block.
}
else if (i==7)
{
s[7] = s[2]+(((-2*q)/g)*s[1]);
goto loop;
}
zs[i] += ds[i]*delt;
s[i] = zs[i]+(ds[i]*(delt/2));
loop:
}
return (0);
}

Page 3

You might also like