Actual source code: ex5.c

petsc-3.6.2 2015-10-02
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: const PetscScalar Vds  = 0.221599610176842;
 67: const PetscScalar Vqs  = 0.985390081525825;
 68: const PetscScalar Edp  = 0.204001061991491;
 69: const PetscScalar Eqp  = 0.930016956074682;
 70: PetscScalar       Ids  = -0.315782941309702;
 71: PetscScalar       Iqs  = 0.081163163902447;
 72: PetscReal         tmax = 20.0;

 74: /* Saves the solution at each time to a matrix */
 77: PetscErrorCode SaveSolution(TS ts)
 78: {
 79:   PetscErrorCode    ierr;
 80:   AppCtx            *user;
 81:   Vec               X;
 82:   PetscScalar       *mat;
 83:   const PetscScalar *x;
 84:   PetscInt          idx;
 85:   PetscReal         t;

 88:   TSGetApplicationContext(ts,&user);
 89:   TSGetTime(ts,&t);
 90:   TSGetSolution(ts,&X);
 91:   idx      =  3*user->stepnum;
 92:   MatDenseGetArray(user->Sol,&mat);
 93:   VecGetArrayRead(X,&x);
 94:   mat[idx] = t;
 95:   PetscMemcpy(mat+idx+1,x,2*sizeof(PetscScalar));
 96:   MatDenseRestoreArray(user->Sol,&mat);
 97:   VecRestoreArrayRead(X,&x);
 98:   user->stepnum++;
 99:   return(0);
100: }


105: /* Computes the wind speed using Weibull distribution */
106: PetscErrorCode WindSpeeds(AppCtx *user)
107: {
109:   PetscScalar    *x,*t,avg_dev,sum;
110:   PetscInt       i;

113:   user->cw       = 5;
114:   user->kw       = 2; /* Rayleigh distribution */
115:   user->nsamples = 2000;
116:   user->Tw       = 0.2;
117:   PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Wind Speed Options","");
118:   {
119:     PetscOptionsReal("-cw","","",user->cw,&user->cw,NULL);
120:     PetscOptionsReal("-kw","","",user->kw,&user->kw,NULL);
121:     PetscOptionsInt("-nsamples","","",user->nsamples,&user->nsamples,NULL);
122:     PetscOptionsReal("-Tw","","",user->Tw,&user->Tw,NULL);
123:   }
124:   PetscOptionsEnd();
125:   VecCreate(PETSC_COMM_WORLD,&user->wind_data);
126:   VecSetSizes(user->wind_data,PETSC_DECIDE,user->nsamples);
127:   VecSetFromOptions(user->wind_data);
128:   VecDuplicate(user->wind_data,&user->t_wind);

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

134:   /* Wind speed deviation = (-log(rand)/cw)^(1/kw) */
135:   VecSetRandom(user->wind_data,NULL);
136:   VecLog(user->wind_data);
137:   VecScale(user->wind_data,-1/user->cw);
138:   VecGetArray(user->wind_data,&x);
139:   for (i=0;i < user->nsamples;i++) x[i] = PetscPowScalar(x[i],(1/user->kw));
140:   VecRestoreArray(user->wind_data,&x);
141:   VecSum(user->wind_data,&sum);
142:   avg_dev = sum/user->nsamples;
143:   /* Wind speed (t) = (1 + wind speed deviation(t) - avg_dev)*average wind speed */
144:   VecShift(user->wind_data,(1-avg_dev));
145:   VecScale(user->wind_data,vwa);
146:   return(0);
147: }

151: /* Sets the parameters for wind turbine */
152: PetscErrorCode SetWindTurbineParams(AppCtx *user)
153: {
155:   user->Rt  = 35;
156:   user->Ar  = PETSC_PI*user->Rt*user->Rt;
157:   user->nGB = 1.0/89.0;
158:   user->rho = 1.225;
159:   user->Ht  = 1.5;
160:   return(0);
161: }

165: /* Sets the parameters for induction generator */
166: PetscErrorCode SetInductionGeneratorParams(AppCtx *user)
167: {
169:   user->np = 4;
170:   user->Xm = 3.0;
171:   user->Xs = 0.1;
172:   user->Xr = 0.08;
173:   user->Rs = 0.01;
174:   user->Rr = 0.01;
175:   user->Xp = user->Xs + user->Xm*user->Xr/(user->Xm + user->Xr);
176:   user->Hm = 1.0;
177:   user->Te = 0.011063063063251968;
178:   return(0);
179: }

183: /* Computes the power extracted from wind */
184: PetscErrorCode GetWindPower(PetscScalar wm,PetscScalar vw,PetscScalar *Pw,AppCtx *user)
185: {
186:   PetscScalar temp,lambda,lambda_i,cp;

189:   temp     = user->nGB*2*user->Rt*ws/user->np;
190:   lambda   = temp*wm/vw;
191:   lambda_i = 1/(1/lambda + 0.002);
192:   cp       = 0.44*(125/lambda_i - 6.94)*PetscExpScalar(-16.5/lambda_i);
193:   *Pw      = 0.5*user->rho*cp*user->Ar*vw*vw*vw/(MVAbase*1e6);
194:   return(0);
195: }

199: /*
200:      Defines the ODE passed to the ODE solver
201: */
202: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *user)
203: {
204:   PetscErrorCode    ierr;
205:   PetscScalar       *f,wm,Pw,*wd;
206:   const PetscScalar *u,*udot;
207:   PetscInt          stepnum;

210:   TSGetTimeStepNumber(ts,&stepnum);
211:   /*  The next three lines allow us to access the entries of the vectors directly */
212:   VecGetArrayRead(U,&u);
213:   VecGetArrayRead(Udot,&udot);
214:   VecGetArray(F,&f);
215:   VecGetArray(user->wind_data,&wd);

217:   f[0] = user->Tw*udot[0] - wd[stepnum] + u[0];
218:   wm   = 1-u[1];
219:   GetWindPower(wm,u[0],&Pw,user);
220:   f[1] = 2.0*(user->Ht+user->Hm)*udot[1] - Pw/wm + user->Te;

222:   VecRestoreArray(user->wind_data,&wd);
223:   VecRestoreArrayRead(U,&u);
224:   VecRestoreArrayRead(Udot,&udot);
225:   VecRestoreArray(F,&f);
226:   return(0);
227: }

231: int main(int argc,char **argv)
232: {
233:   TS             ts;            /* ODE integrator */
234:   Vec            U;             /* solution will be stored here */
235:   Mat            A;             /* Jacobian matrix */
237:   PetscMPIInt    size;
238:   PetscInt       n = 2,idx;
239:   AppCtx         user;
240:   PetscScalar    *u;
241:   SNES           snes;
242:   PetscScalar       *mat;
243:   const PetscScalar *x;
244:   Mat         B;
245:   PetscScalar *amat;
246:   PetscViewer viewer;



250:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
251:      Initialize program
252:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
253:   PetscInitialize(&argc,&argv,(char*)0,help);
254:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
255:   if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");

257:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
258:     Create necessary matrix and vectors
259:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
260:   MatCreate(PETSC_COMM_WORLD,&A);
261:   MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE);
262:   MatSetFromOptions(A);
263:   MatSetUp(A);

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

267:   /* Create wind speed data using Weibull distribution */
268:   WindSpeeds(&user);
269:   /* Set parameters for wind turbine and induction generator */
270:   SetWindTurbineParams(&user);
271:   SetInductionGeneratorParams(&user);

273:   VecGetArray(U,&u);
274:   u[0] = vwa;
275:   u[1] = s;
276:   VecRestoreArray(U,&u);

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

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

283:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
284:      Create timestepping solver context
285:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
286:   TSCreate(PETSC_COMM_WORLD,&ts);
287:   TSSetProblemType(ts,TS_NONLINEAR);
288:   TSSetType(ts,TSBEULER);
289:   TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&user);

291:   TSGetSNES(ts,&snes);
292:   SNESSetJacobian(snes,A,A,SNESComputeJacobianDefault,NULL);
293:   /*  TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&user); */
294:   TSSetApplicationContext(ts,&user);

296:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
297:      Set initial conditions
298:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
299:   TSSetSolution(ts,U);

301:   /* Save initial solution */
302:   idx=3*user.stepnum;

304:   MatDenseGetArray(user.Sol,&mat);
305:   VecGetArrayRead(U,&x);

307:   mat[idx] = 0.0;

309:   PetscMemcpy(mat+idx+1,x,2*sizeof(PetscScalar));
310:   MatDenseRestoreArray(user.Sol,&mat);
311:   VecRestoreArrayRead(U,&x);
312:   user.stepnum++;


315:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
316:      Set solver options
317:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
318:   TSSetDuration(ts,2000,20.0);
319:   TSSetInitialTimeStep(ts,0.0,.01);
320:   TSSetFromOptions(ts);
321:   TSSetPostStep(ts,SaveSolution);
322:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
323:      Solve nonlinear system
324:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
325:   TSSolve(ts,U);

327:   MatCreateSeqDense(PETSC_COMM_SELF,3,user.stepnum,NULL,&B);
328:   MatDenseGetArray(user.Sol,&mat);
329:   MatDenseGetArray(B,&amat);
330:   PetscMemcpy(amat,mat,user.stepnum*3*sizeof(PetscScalar));
331:   MatDenseRestoreArray(B,&amat);
332:   MatDenseRestoreArray(user.Sol,&mat);

334:   PetscViewerBinaryOpen(PETSC_COMM_SELF,"out.bin",FILE_MODE_WRITE,&viewer);
335:   MatView(B,viewer);
336:   PetscViewerDestroy(&viewer);
337:   MatDestroy(&user.Sol);
338:   MatDestroy(&B);
339:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
340:      Free work space.  All PETSc objects should be destroyed when they are no longer needed.
341:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
342:   VecDestroy(&user.wind_data);
343:   VecDestroy(&user.t_wind);
344:   MatDestroy(&A);
345:   VecDestroy(&U);
346:   TSDestroy(&ts);

348:   PetscFinalize();
349:   return(0);
350: }