Actual source code: ex5.c
petsc-3.12.0 2019-09-29
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: /*T
27: T*/
29: #include <petscts.h>
31: #define freq 50
32: #define ws (2*PETSC_PI*freq)
33: #define MVAbase 100
35: typedef struct {
36: /* Parameters for wind speed model */
37: PetscInt nsamples; /* Number of wind samples */
38: PetscReal cw; /* Scale factor for Weibull distribution */
39: PetscReal kw; /* Shape factor for Weibull distribution */
40: Vec wind_data; /* Vector to hold wind speeds */
41: Vec t_wind; /* Vector to hold wind speed times */
42: PetscReal Tw; /* Filter time constant */
44: /* Wind turbine parameters */
45: PetscScalar Rt; /* Rotor radius */
46: PetscScalar Ar; /* Area swept by rotor (pi*R*R) */
47: PetscReal nGB; /* Gear box ratio */
48: PetscReal Ht; /* Turbine inertia constant */
49: PetscReal rho; /* Atmospheric pressure */
51: /* Induction generator parameters */
52: PetscInt np; /* Number of poles */
53: PetscReal Xm; /* Magnetizing reactance */
54: PetscReal Xs; /* Stator Reactance */
55: PetscReal Xr; /* Rotor reactance */
56: PetscReal Rs; /* Stator resistance */
57: PetscReal Rr; /* Rotor resistance */
58: PetscReal Hm; /* Motor inertia constant */
59: PetscReal Xp; /* Xs + Xm*Xr/(Xm + Xr) */
60: PetscScalar Te; /* Electrical Torque */
62: Mat Sol; /* Solution matrix */
63: PetscInt stepnum; /* Column number of solution matrix */
64: } AppCtx;
66: /* Initial values computed by Power flow and initialization */
67: PetscScalar s = -0.00011577790353;
68: /*Pw = 0.011064344110238; %Te*wm */
69: PetscScalar vwa = 22.317142184449754;
70: PetscReal tmax = 20.0;
72: /* Saves the solution at each time to a matrix */
73: PetscErrorCode SaveSolution(TS ts)
74: {
75: PetscErrorCode ierr;
76: AppCtx *user;
77: Vec X;
78: PetscScalar *mat;
79: const PetscScalar *x;
80: PetscInt idx;
81: PetscReal t;
84: TSGetApplicationContext(ts,&user);
85: TSGetTime(ts,&t);
86: TSGetSolution(ts,&X);
87: idx = 3*user->stepnum;
88: MatDenseGetArray(user->Sol,&mat);
89: VecGetArrayRead(X,&x);
90: mat[idx] = t;
91: PetscArraycpy(mat+idx+1,x,2);
92: MatDenseRestoreArray(user->Sol,&mat);
93: VecRestoreArrayRead(X,&x);
94: user->stepnum++;
95: return(0);
96: }
99: /* Computes the wind speed using Weibull distribution */
100: PetscErrorCode WindSpeeds(AppCtx *user)
101: {
103: PetscScalar *x,*t,avg_dev,sum;
104: PetscInt i;
107: user->cw = 5;
108: user->kw = 2; /* Rayleigh distribution */
109: user->nsamples = 2000;
110: user->Tw = 0.2;
111: PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Wind Speed Options","");
112: {
113: PetscOptionsReal("-cw","","",user->cw,&user->cw,NULL);
114: PetscOptionsReal("-kw","","",user->kw,&user->kw,NULL);
115: PetscOptionsInt("-nsamples","","",user->nsamples,&user->nsamples,NULL);
116: PetscOptionsReal("-Tw","","",user->Tw,&user->Tw,NULL);
117: }
118: PetscOptionsEnd();
119: VecCreate(PETSC_COMM_WORLD,&user->wind_data);
120: VecSetSizes(user->wind_data,PETSC_DECIDE,user->nsamples);
121: VecSetFromOptions(user->wind_data);
122: VecDuplicate(user->wind_data,&user->t_wind);
124: VecGetArray(user->t_wind,&t);
125: for (i=0; i < user->nsamples; i++) t[i] = (i+1)*tmax/user->nsamples;
126: VecRestoreArray(user->t_wind,&t);
128: /* Wind speed deviation = (-log(rand)/cw)^(1/kw) */
129: VecSetRandom(user->wind_data,NULL);
130: VecLog(user->wind_data);
131: VecScale(user->wind_data,-1/user->cw);
132: VecGetArray(user->wind_data,&x);
133: for (i=0;i < user->nsamples;i++) x[i] = PetscPowScalar(x[i],(1/user->kw));
134: VecRestoreArray(user->wind_data,&x);
135: VecSum(user->wind_data,&sum);
136: avg_dev = sum/user->nsamples;
137: /* Wind speed (t) = (1 + wind speed deviation(t) - avg_dev)*average wind speed */
138: VecShift(user->wind_data,(1-avg_dev));
139: VecScale(user->wind_data,vwa);
140: return(0);
141: }
143: /* Sets the parameters for wind turbine */
144: PetscErrorCode SetWindTurbineParams(AppCtx *user)
145: {
147: user->Rt = 35;
148: user->Ar = PETSC_PI*user->Rt*user->Rt;
149: user->nGB = 1.0/89.0;
150: user->rho = 1.225;
151: user->Ht = 1.5;
152: return(0);
153: }
155: /* Sets the parameters for induction generator */
156: PetscErrorCode SetInductionGeneratorParams(AppCtx *user)
157: {
159: user->np = 4;
160: user->Xm = 3.0;
161: user->Xs = 0.1;
162: user->Xr = 0.08;
163: user->Rs = 0.01;
164: user->Rr = 0.01;
165: user->Xp = user->Xs + user->Xm*user->Xr/(user->Xm + user->Xr);
166: user->Hm = 1.0;
167: user->Te = 0.011063063063251968;
168: return(0);
169: }
171: /* Computes the power extracted from wind */
172: PetscErrorCode GetWindPower(PetscScalar wm,PetscScalar vw,PetscScalar *Pw,AppCtx *user)
173: {
174: PetscScalar temp,lambda,lambda_i,cp;
177: temp = user->nGB*2*user->Rt*ws/user->np;
178: lambda = temp*wm/vw;
179: lambda_i = 1/(1/lambda + 0.002);
180: cp = 0.44*(125/lambda_i - 6.94)*PetscExpScalar(-16.5/lambda_i);
181: *Pw = 0.5*user->rho*cp*user->Ar*vw*vw*vw/(MVAbase*1e6);
182: return(0);
183: }
185: /*
186: Defines the ODE passed to the ODE solver
187: */
188: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *user)
189: {
190: PetscErrorCode ierr;
191: PetscScalar *f,wm,Pw,*wd;
192: const PetscScalar *u,*udot;
193: PetscInt stepnum;
196: TSGetStepNumber(ts,&stepnum);
197: /* The next three lines allow us to access the entries of the vectors directly */
198: VecGetArrayRead(U,&u);
199: VecGetArrayRead(Udot,&udot);
200: VecGetArray(F,&f);
201: VecGetArray(user->wind_data,&wd);
203: f[0] = user->Tw*udot[0] - wd[stepnum] + u[0];
204: wm = 1-u[1];
205: GetWindPower(wm,u[0],&Pw,user);
206: f[1] = 2.0*(user->Ht+user->Hm)*udot[1] - Pw/wm + user->Te;
208: VecRestoreArray(user->wind_data,&wd);
209: VecRestoreArrayRead(U,&u);
210: VecRestoreArrayRead(Udot,&udot);
211: VecRestoreArray(F,&f);
212: return(0);
213: }
215: int main(int argc,char **argv)
216: {
217: TS ts; /* ODE integrator */
218: Vec U; /* solution will be stored here */
219: Mat A; /* Jacobian matrix */
220: PetscErrorCode ierr;
221: PetscMPIInt size;
222: PetscInt n = 2,idx;
223: AppCtx user;
224: PetscScalar *u;
225: SNES snes;
226: PetscScalar *mat;
227: const PetscScalar *x,*rmat;
228: Mat B;
229: PetscScalar *amat;
230: PetscViewer viewer;
234: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
235: Initialize program
236: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
237: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
238: MPI_Comm_size(PETSC_COMM_WORLD,&size);
239: if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");
241: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
242: Create necessary matrix and vectors
243: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
244: MatCreate(PETSC_COMM_WORLD,&A);
245: MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE);
246: MatSetFromOptions(A);
247: MatSetUp(A);
249: MatCreateVecs(A,&U,NULL);
251: /* Create wind speed data using Weibull distribution */
252: WindSpeeds(&user);
253: /* Set parameters for wind turbine and induction generator */
254: SetWindTurbineParams(&user);
255: SetInductionGeneratorParams(&user);
257: VecGetArray(U,&u);
258: u[0] = vwa;
259: u[1] = s;
260: VecRestoreArray(U,&u);
262: /* Create matrix to save solutions at each time step */
263: user.stepnum = 0;
265: MatCreateSeqDense(PETSC_COMM_SELF,3,2010,NULL,&user.Sol);
267: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
268: Create timestepping solver context
269: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
270: TSCreate(PETSC_COMM_WORLD,&ts);
271: TSSetProblemType(ts,TS_NONLINEAR);
272: TSSetType(ts,TSBEULER);
273: TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&user);
275: TSGetSNES(ts,&snes);
276: SNESSetJacobian(snes,A,A,SNESComputeJacobianDefault,NULL);
277: /* TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&user); */
278: TSSetApplicationContext(ts,&user);
280: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281: Set initial conditions
282: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
283: TSSetSolution(ts,U);
285: /* Save initial solution */
286: idx=3*user.stepnum;
288: MatDenseGetArray(user.Sol,&mat);
289: VecGetArrayRead(U,&x);
291: mat[idx] = 0.0;
293: PetscArraycpy(mat+idx+1,x,2);
294: MatDenseRestoreArray(user.Sol,&mat);
295: VecRestoreArrayRead(U,&x);
296: user.stepnum++;
299: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
300: Set solver options
301: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
302: TSSetMaxTime(ts,20.0);
303: TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP);
304: TSSetTimeStep(ts,.01);
305: TSSetFromOptions(ts);
306: TSSetPostStep(ts,SaveSolution);
307: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
308: Solve nonlinear system
309: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
310: TSSolve(ts,U);
312: MatCreateSeqDense(PETSC_COMM_SELF,3,user.stepnum,NULL,&B);
313: MatDenseGetArrayRead(user.Sol,&rmat);
314: MatDenseGetArray(B,&amat);
315: PetscArraycpy(amat,rmat,user.stepnum*3);
316: MatDenseRestoreArray(B,&amat);
317: MatDenseRestoreArrayRead(user.Sol,&rmat);
319: PetscViewerBinaryOpen(PETSC_COMM_SELF,"out.bin",FILE_MODE_WRITE,&viewer);
320: MatView(B,viewer);
321: PetscViewerDestroy(&viewer);
322: MatDestroy(&user.Sol);
323: MatDestroy(&B);
324: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
325: Free work space. All PETSc objects should be destroyed when they are no longer needed.
326: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
327: VecDestroy(&user.wind_data);
328: VecDestroy(&user.t_wind);
329: MatDestroy(&A);
330: VecDestroy(&U);
331: TSDestroy(&ts);
333: PetscFinalize();
334: return ierr;
335: }
338: /*TEST
340: build:
341: requires: !complex
343: test:
346: TEST*/