Actual source code: ex5.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Basic equation for an induction generator driven by a wind turbine.\n";

\begin{eqnarray}
T_w\frac{dv_w}{dt} & = & v_w - v_we \\
2(H_t+H_m)\frac{ds}{dt} & = & P_w - P_e
\end{eqnarray}
 10: /*
 11:  - Pw is the power extracted from the wind turbine given by
 12:            Pw = 0.5*\rho*cp*Ar*vw^3

 14:  - The wind speed time series is modeled using a Weibull distribution and then
 15:    passed through a low pass filter (with time constant T_w).
 16:  - v_we is the wind speed data calculated using Weibull distribution while v_w is
 17:    the output of the filter.
 18:  - P_e is assumed as constant electrical torque

 20:  - This example does not work with adaptive time stepping!

 22: Reference:
 23: Power System Modeling and Scripting - F. Milano
 24: */
 25: #include <petscts.h>

 27: #define freq 50
 28: #define ws (2*PETSC_PI*freq)
 29: #define MVAbase 100

 31: typedef struct {
 32:   /* Parameters for wind speed model */
 33:   PetscInt  nsamples; /* Number of wind samples */
 34:   PetscReal cw;   /* Scale factor for Weibull distribution */
 35:   PetscReal kw;   /* Shape factor for Weibull distribution */
 36:   Vec       wind_data; /* Vector to hold wind speeds */
 37:   Vec       t_wind; /* Vector to hold wind speed times */
 38:   PetscReal Tw;     /* Filter time constant */

 40:   /* Wind turbine parameters */
 41:   PetscScalar Rt; /* Rotor radius */
 42:   PetscScalar Ar; /* Area swept by rotor (pi*R*R) */
 43:   PetscReal   nGB; /* Gear box ratio */
 44:   PetscReal   Ht;  /* Turbine inertia constant */
 45:   PetscReal   rho; /* Atmospheric pressure */

 47:   /* Induction generator parameters */
 48:   PetscInt    np; /* Number of poles */
 49:   PetscReal   Xm; /* Magnetizing reactance */
 50:   PetscReal   Xs; /* Stator Reactance */
 51:   PetscReal   Xr; /* Rotor reactance */
 52:   PetscReal   Rs; /* Stator resistance */
 53:   PetscReal   Rr; /* Rotor resistance */
 54:   PetscReal   Hm; /* Motor inertia constant */
 55:   PetscReal   Xp; /* Xs + Xm*Xr/(Xm + Xr) */
 56:   PetscScalar Te; /* Electrical Torque */

 58:   Mat      Sol;   /* Solution matrix */
 59:   PetscInt stepnum;   /* Column number of solution matrix */
 60: } AppCtx;

 62: /* Initial values computed by Power flow and initialization */
 63: PetscScalar s = -0.00011577790353;
 64: /*Pw = 0.011064344110238; %Te*wm */
 65: PetscScalar       vwa  = 22.317142184449754;
 66: PetscReal         tmax = 20.0;

 68: /* Saves the solution at each time to a matrix */
 69: PetscErrorCode SaveSolution(TS ts)
 70: {
 71:   PetscErrorCode    ierr;
 72:   AppCtx            *user;
 73:   Vec               X;
 74:   PetscScalar       *mat;
 75:   const PetscScalar *x;
 76:   PetscInt          idx;
 77:   PetscReal         t;

 80:   TSGetApplicationContext(ts,&user);
 81:   TSGetTime(ts,&t);
 82:   TSGetSolution(ts,&X);
 83:   idx      =  3*user->stepnum;
 84:   MatDenseGetArray(user->Sol,&mat);
 85:   VecGetArrayRead(X,&x);
 86:   mat[idx] = t;
 87:   PetscMemcpy(mat+idx+1,x,2*sizeof(PetscScalar));
 88:   MatDenseRestoreArray(user->Sol,&mat);
 89:   VecRestoreArrayRead(X,&x);
 90:   user->stepnum++;
 91:   return(0);
 92: }


 95: /* Computes the wind speed using Weibull distribution */
 96: PetscErrorCode WindSpeeds(AppCtx *user)
 97: {
 99:   PetscScalar    *x,*t,avg_dev,sum;
100:   PetscInt       i;

103:   user->cw       = 5;
104:   user->kw       = 2; /* Rayleigh distribution */
105:   user->nsamples = 2000;
106:   user->Tw       = 0.2;
107:   PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Wind Speed Options","");
108:   {
109:     PetscOptionsReal("-cw","","",user->cw,&user->cw,NULL);
110:     PetscOptionsReal("-kw","","",user->kw,&user->kw,NULL);
111:     PetscOptionsInt("-nsamples","","",user->nsamples,&user->nsamples,NULL);
112:     PetscOptionsReal("-Tw","","",user->Tw,&user->Tw,NULL);
113:   }
114:   PetscOptionsEnd();
115:   VecCreate(PETSC_COMM_WORLD,&user->wind_data);
116:   VecSetSizes(user->wind_data,PETSC_DECIDE,user->nsamples);
117:   VecSetFromOptions(user->wind_data);
118:   VecDuplicate(user->wind_data,&user->t_wind);

120:   VecGetArray(user->t_wind,&t);
121:   for (i=0; i < user->nsamples; i++) t[i] = (i+1)*tmax/user->nsamples;
122:   VecRestoreArray(user->t_wind,&t);

124:   /* Wind speed deviation = (-log(rand)/cw)^(1/kw) */
125:   VecSetRandom(user->wind_data,NULL);
126:   VecLog(user->wind_data);
127:   VecScale(user->wind_data,-1/user->cw);
128:   VecGetArray(user->wind_data,&x);
129:   for (i=0;i < user->nsamples;i++) x[i] = PetscPowScalar(x[i],(1/user->kw));
130:   VecRestoreArray(user->wind_data,&x);
131:   VecSum(user->wind_data,&sum);
132:   avg_dev = sum/user->nsamples;
133:   /* Wind speed (t) = (1 + wind speed deviation(t) - avg_dev)*average wind speed */
134:   VecShift(user->wind_data,(1-avg_dev));
135:   VecScale(user->wind_data,vwa);
136:   return(0);
137: }

139: /* Sets the parameters for wind turbine */
140: PetscErrorCode SetWindTurbineParams(AppCtx *user)
141: {
143:   user->Rt  = 35;
144:   user->Ar  = PETSC_PI*user->Rt*user->Rt;
145:   user->nGB = 1.0/89.0;
146:   user->rho = 1.225;
147:   user->Ht  = 1.5;
148:   return(0);
149: }

151: /* Sets the parameters for induction generator */
152: PetscErrorCode SetInductionGeneratorParams(AppCtx *user)
153: {
155:   user->np = 4;
156:   user->Xm = 3.0;
157:   user->Xs = 0.1;
158:   user->Xr = 0.08;
159:   user->Rs = 0.01;
160:   user->Rr = 0.01;
161:   user->Xp = user->Xs + user->Xm*user->Xr/(user->Xm + user->Xr);
162:   user->Hm = 1.0;
163:   user->Te = 0.011063063063251968;
164:   return(0);
165: }

167: /* Computes the power extracted from wind */
168: PetscErrorCode GetWindPower(PetscScalar wm,PetscScalar vw,PetscScalar *Pw,AppCtx *user)
169: {
170:   PetscScalar temp,lambda,lambda_i,cp;

173:   temp     = user->nGB*2*user->Rt*ws/user->np;
174:   lambda   = temp*wm/vw;
175:   lambda_i = 1/(1/lambda + 0.002);
176:   cp       = 0.44*(125/lambda_i - 6.94)*PetscExpScalar(-16.5/lambda_i);
177:   *Pw      = 0.5*user->rho*cp*user->Ar*vw*vw*vw/(MVAbase*1e6);
178:   return(0);
179: }

181: /*
182:      Defines the ODE passed to the ODE solver
183: */
184: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *user)
185: {
186:   PetscErrorCode    ierr;
187:   PetscScalar       *f,wm,Pw,*wd;
188:   const PetscScalar *u,*udot;
189:   PetscInt          stepnum;

192:   TSGetStepNumber(ts,&stepnum);
193:   /*  The next three lines allow us to access the entries of the vectors directly */
194:   VecGetArrayRead(U,&u);
195:   VecGetArrayRead(Udot,&udot);
196:   VecGetArray(F,&f);
197:   VecGetArray(user->wind_data,&wd);

199:   f[0] = user->Tw*udot[0] - wd[stepnum] + u[0];
200:   wm   = 1-u[1];
201:   GetWindPower(wm,u[0],&Pw,user);
202:   f[1] = 2.0*(user->Ht+user->Hm)*udot[1] - Pw/wm + user->Te;

204:   VecRestoreArray(user->wind_data,&wd);
205:   VecRestoreArrayRead(U,&u);
206:   VecRestoreArrayRead(Udot,&udot);
207:   VecRestoreArray(F,&f);
208:   return(0);
209: }

211: int main(int argc,char **argv)
212: {
213:   TS             ts;            /* ODE integrator */
214:   Vec            U;             /* solution will be stored here */
215:   Mat            A;             /* Jacobian matrix */
217:   PetscMPIInt    size;
218:   PetscInt       n = 2,idx;
219:   AppCtx         user;
220:   PetscScalar    *u;
221:   SNES           snes;
222:   PetscScalar       *mat;
223:   const PetscScalar *x;
224:   Mat         B;
225:   PetscScalar *amat;
226:   PetscViewer viewer;



230:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
231:      Initialize program
232:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
233:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
234:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
235:   if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");

237:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
238:     Create necessary matrix and vectors
239:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
240:   MatCreate(PETSC_COMM_WORLD,&A);
241:   MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE);
242:   MatSetFromOptions(A);
243:   MatSetUp(A);

245:   MatCreateVecs(A,&U,NULL);

247:   /* Create wind speed data using Weibull distribution */
248:   WindSpeeds(&user);
249:   /* Set parameters for wind turbine and induction generator */
250:   SetWindTurbineParams(&user);
251:   SetInductionGeneratorParams(&user);

253:   VecGetArray(U,&u);
254:   u[0] = vwa;
255:   u[1] = s;
256:   VecRestoreArray(U,&u);

258:   /* Create matrix to save solutions at each time step */
259:   user.stepnum = 0;

261:   MatCreateSeqDense(PETSC_COMM_SELF,3,2010,NULL,&user.Sol);

263:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
264:      Create timestepping solver context
265:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
266:   TSCreate(PETSC_COMM_WORLD,&ts);
267:   TSSetProblemType(ts,TS_NONLINEAR);
268:   TSSetType(ts,TSBEULER);
269:   TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&user);

271:   TSGetSNES(ts,&snes);
272:   SNESSetJacobian(snes,A,A,SNESComputeJacobianDefault,NULL);
273:   /*  TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&user); */
274:   TSSetApplicationContext(ts,&user);

276:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
277:      Set initial conditions
278:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
279:   TSSetSolution(ts,U);

281:   /* Save initial solution */
282:   idx=3*user.stepnum;

284:   MatDenseGetArray(user.Sol,&mat);
285:   VecGetArrayRead(U,&x);

287:   mat[idx] = 0.0;

289:   PetscMemcpy(mat+idx+1,x,2*sizeof(PetscScalar));
290:   MatDenseRestoreArray(user.Sol,&mat);
291:   VecRestoreArrayRead(U,&x);
292:   user.stepnum++;


295:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
296:      Set solver options
297:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
298:   TSSetMaxTime(ts,20.0);
299:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP);
300:   TSSetTimeStep(ts,.01);
301:   TSSetFromOptions(ts);
302:   TSSetPostStep(ts,SaveSolution);
303:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
304:      Solve nonlinear system
305:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
306:   TSSolve(ts,U);

308:   MatCreateSeqDense(PETSC_COMM_SELF,3,user.stepnum,NULL,&B);
309:   MatDenseGetArray(user.Sol,&mat);
310:   MatDenseGetArray(B,&amat);
311:   PetscMemcpy(amat,mat,user.stepnum*3*sizeof(PetscScalar));
312:   MatDenseRestoreArray(B,&amat);
313:   MatDenseRestoreArray(user.Sol,&mat);

315:   PetscViewerBinaryOpen(PETSC_COMM_SELF,"out.bin",FILE_MODE_WRITE,&viewer);
316:   MatView(B,viewer);
317:   PetscViewerDestroy(&viewer);
318:   MatDestroy(&user.Sol);
319:   MatDestroy(&B);
320:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
321:      Free work space.  All PETSc objects should be destroyed when they are no longer needed.
322:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
323:   VecDestroy(&user.wind_data);
324:   VecDestroy(&user.t_wind);
325:   MatDestroy(&A);
326:   VecDestroy(&U);
327:   TSDestroy(&ts);

329:   PetscFinalize();
330:   return ierr;
331: }