Actual source code: ex3opt_fd.c


  2: static char help[] = "Finds optimal parameter P_m for the generator system while maintaining generator stability.\n";

  4: /*F

  6: \begin{eqnarray}
  7:                  \frac{d \theta}{dt} = \omega_b (\omega - \omega_s)
  8:                  \frac{2 H}{\omega_s}\frac{d \omega}{dt} & = & P_m - P_max \sin(\theta) -D(\omega - \omega_s)\\
  9: \end{eqnarray}

 11: F*/

 13: /*
 14:   Solve the same optimization problem as in ex3opt.c.
 15:   Use finite difference to approximate the gradients.
 16: */
 17: #include <petsctao.h>
 18: #include <petscts.h>
 19: #include "ex3.h"

 21: PetscErrorCode FormFunction(Tao, Vec, PetscReal *, void *);

 23: PetscErrorCode monitor(Tao tao, AppCtx *ctx)
 24: {
 25:   FILE              *fp;
 26:   PetscInt           iterate;
 27:   PetscReal          f, gnorm, cnorm, xdiff;
 28:   Vec                X, G;
 29:   const PetscScalar *x, *g;
 30:   TaoConvergedReason reason;

 33:   TaoGetSolutionStatus(tao, &iterate, &f, &gnorm, &cnorm, &xdiff, &reason);
 34:   TaoGetSolution(tao, &X);
 35:   TaoGetGradient(tao, &G, NULL, NULL);
 36:   VecGetArrayRead(X, &x);
 37:   VecGetArrayRead(G, &g);
 38:   fp = fopen("ex3opt_fd_conv.out", "a");
 39:   PetscFPrintf(PETSC_COMM_WORLD, fp, "%" PetscInt_FMT " %g %.12lf %.12lf\n", iterate, (double)gnorm, (double)PetscRealPart(x[0]), (double)PetscRealPart(g[0]));
 40:   VecRestoreArrayRead(X, &x);
 41:   VecRestoreArrayRead(G, &g);
 42:   fclose(fp);
 43:   return 0;
 44: }

 46: int main(int argc, char **argv)
 47: {
 48:   Vec          p;
 49:   PetscScalar *x_ptr;
 50:   PetscMPIInt  size;
 51:   AppCtx       ctx;
 52:   Vec          lowerb, upperb;
 53:   Tao          tao;
 54:   KSP          ksp;
 55:   PC           pc;
 56:   PetscBool    printtofile;
 57:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 58:      Initialize program
 59:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 61:   PetscInitialize(&argc, &argv, NULL, help);
 63:   MPI_Comm_size(PETSC_COMM_WORLD, &size);

 66:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 67:     Set runtime options
 68:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 69:   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Swing equation options", "");
 70:   {
 71:     ctx.beta    = 2;
 72:     ctx.c       = 10000.0;
 73:     ctx.u_s     = 1.0;
 74:     ctx.omega_s = 1.0;
 75:     ctx.omega_b = 120.0 * PETSC_PI;
 76:     ctx.H       = 5.0;
 77:     PetscOptionsScalar("-Inertia", "", "", ctx.H, &ctx.H, NULL);
 78:     ctx.D = 5.0;
 79:     PetscOptionsScalar("-D", "", "", ctx.D, &ctx.D, NULL);
 80:     ctx.E        = 1.1378;
 81:     ctx.V        = 1.0;
 82:     ctx.X        = 0.545;
 83:     ctx.Pmax     = ctx.E * ctx.V / ctx.X;
 84:     ctx.Pmax_ini = ctx.Pmax;
 85:     PetscOptionsScalar("-Pmax", "", "", ctx.Pmax, &ctx.Pmax, NULL);
 86:     ctx.Pm = 1.06;
 87:     PetscOptionsScalar("-Pm", "", "", ctx.Pm, &ctx.Pm, NULL);
 88:     ctx.tf  = 0.1;
 89:     ctx.tcl = 0.2;
 90:     PetscOptionsReal("-tf", "Time to start fault", "", ctx.tf, &ctx.tf, NULL);
 91:     PetscOptionsReal("-tcl", "Time to end fault", "", ctx.tcl, &ctx.tcl, NULL);
 92:     printtofile = PETSC_FALSE;
 93:     PetscOptionsBool("-printtofile", "Print convergence results to file", "", printtofile, &printtofile, NULL);
 94:   }
 95:   PetscOptionsEnd();

 97:   /* Create TAO solver and set desired solution method */
 98:   TaoCreate(PETSC_COMM_WORLD, &tao);
 99:   TaoSetType(tao, TAOBLMVM);
100:   if (printtofile) TaoSetMonitor(tao, (PetscErrorCode(*)(Tao, void *))monitor, (void *)&ctx, PETSC_NULL);
101:   TaoSetMaximumIterations(tao, 30);
102:   /*
103:      Optimization starts
104:   */
105:   /* Set initial solution guess */
106:   VecCreateSeq(PETSC_COMM_WORLD, 1, &p);
107:   VecGetArray(p, &x_ptr);
108:   x_ptr[0] = ctx.Pm;
109:   VecRestoreArray(p, &x_ptr);

111:   TaoSetSolution(tao, p);
112:   /* Set routine for function and gradient evaluation */
113:   TaoSetObjective(tao, FormFunction, (void *)&ctx);
114:   TaoSetGradient(tao, NULL, TaoDefaultComputeGradient, (void *)&ctx);

116:   /* Set bounds for the optimization */
117:   VecDuplicate(p, &lowerb);
118:   VecDuplicate(p, &upperb);
119:   VecGetArray(lowerb, &x_ptr);
120:   x_ptr[0] = 0.;
121:   VecRestoreArray(lowerb, &x_ptr);
122:   VecGetArray(upperb, &x_ptr);
123:   x_ptr[0] = 1.1;
124:   VecRestoreArray(upperb, &x_ptr);
125:   TaoSetVariableBounds(tao, lowerb, upperb);

127:   /* Check for any TAO command line options */
128:   TaoSetFromOptions(tao);
129:   TaoGetKSP(tao, &ksp);
130:   if (ksp) {
131:     KSPGetPC(ksp, &pc);
132:     PCSetType(pc, PCNONE);
133:   }

135:   /* SOLVE THE APPLICATION */
136:   TaoSolve(tao);

138:   VecView(p, PETSC_VIEWER_STDOUT_WORLD);

140:   /* Free TAO data structures */
141:   TaoDestroy(&tao);
142:   VecDestroy(&p);
143:   VecDestroy(&lowerb);
144:   VecDestroy(&upperb);
145:   PetscFinalize();
146:   return 0;
147: }

149: /* ------------------------------------------------------------------ */
150: /*
151:    FormFunction - Evaluates the function and corresponding gradient.

153:    Input Parameters:
154:    tao - the Tao context
155:    X   - the input vector
156:    ptr - optional user-defined context, as set by TaoSetObjectiveAndGradient()

158:    Output Parameters:
159:    f   - the newly evaluated function
160: */
161: PetscErrorCode FormFunction(Tao tao, Vec P, PetscReal *f, void *ctx0)
162: {
163:   AppCtx            *ctx = (AppCtx *)ctx0;
164:   TS                 ts, quadts;
165:   Vec                U; /* solution will be stored here */
166:   Mat                A; /* Jacobian matrix */
167:   PetscInt           n = 2;
168:   PetscReal          ftime;
169:   PetscInt           steps;
170:   PetscScalar       *u;
171:   const PetscScalar *x_ptr, *qx_ptr;
172:   Vec                q;
173:   PetscInt           direction[2];
174:   PetscBool          terminate[2];

176:   VecGetArrayRead(P, &x_ptr);
177:   ctx->Pm = x_ptr[0];
178:   VecRestoreArrayRead(P, &x_ptr);
179:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
180:     Create necessary matrix and vectors
181:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
182:   MatCreate(PETSC_COMM_WORLD, &A);
183:   MatSetSizes(A, n, n, PETSC_DETERMINE, PETSC_DETERMINE);
184:   MatSetType(A, MATDENSE);
185:   MatSetFromOptions(A);
186:   MatSetUp(A);

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

190:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
191:      Create timestepping solver context
192:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
193:   TSCreate(PETSC_COMM_WORLD, &ts);
194:   TSSetProblemType(ts, TS_NONLINEAR);
195:   TSSetType(ts, TSCN);
196:   TSSetIFunction(ts, NULL, (TSIFunction)IFunction, ctx);
197:   TSSetIJacobian(ts, A, A, (TSIJacobian)IJacobian, ctx);

199:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
200:      Set initial conditions
201:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
202:   VecGetArray(U, &u);
203:   u[0] = PetscAsinScalar(ctx->Pm / ctx->Pmax);
204:   u[1] = 1.0;
205:   VecRestoreArray(U, &u);
206:   TSSetSolution(ts, U);

208:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209:      Set solver options
210:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
211:   TSSetMaxTime(ts, 1.0);
212:   TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP);
213:   TSSetTimeStep(ts, 0.03125);
214:   TSCreateQuadratureTS(ts, PETSC_TRUE, &quadts);
215:   TSGetSolution(quadts, &q);
216:   VecSet(q, 0.0);
217:   TSSetRHSFunction(quadts, NULL, (TSRHSFunction)CostIntegrand, ctx);
218:   TSSetFromOptions(ts);

220:   direction[0] = direction[1] = 1;
221:   terminate[0] = terminate[1] = PETSC_FALSE;

223:   TSSetEventHandler(ts, 2, direction, terminate, EventFunction, PostEventFunction, (void *)ctx);

225:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
226:      Solve nonlinear system
227:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
228:   TSSolve(ts, U);

230:   TSGetSolveTime(ts, &ftime);
231:   TSGetStepNumber(ts, &steps);
232:   VecGetArrayRead(q, &qx_ptr);
233:   *f = -ctx->Pm + qx_ptr[0];
234:   VecRestoreArrayRead(q, &qx_ptr);

236:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
237:      Free work space.  All PETSc objects should be destroyed when they are no longer needed.
238:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
239:   MatDestroy(&A);
240:   VecDestroy(&U);
241:   TSDestroy(&ts);
242:   return 0;
243: }

245: /*TEST

247:    build:
248:       requires: !complex !single

250:    test:
251:       args: -ts_type cn -pc_type lu -tao_monitor -tao_gatol 1e-3

253: TEST*/