xref: /petsc/src/ts/tutorials/power_grid/ex1.c (revision 40badf4fbc550ac1f60bd080eaff6de6d55b946d)
1 
2 static char help[] = "Basic equation for generator stability analysis.\n";
3 
4 /*F
5 
6 \begin{eqnarray}
7                  \frac{2 H}{\omega_s}\frac{d \omega}{dt} & = & P_m - \frac{EV}{X} \sin(\theta) \\
8                  \frac{d \theta}{dt} = \omega - \omega_s
9 \end{eqnarray}
10 
11 F*/
12 
13 /*
14    Include "petscts.h" so that we can use TS solvers.  Note that this
15    file automatically includes:
16      petscsys.h       - base PETSc routines   petscvec.h - vectors
17      petscmat.h - matrices
18      petscis.h     - index sets            petscksp.h - Krylov subspace methods
19      petscviewer.h - viewers               petscpc.h  - preconditioners
20      petscksp.h   - linear solvers
21 */
22 
23 #include <petscts.h>
24 
25 typedef struct {
26   PetscScalar H,omega_s,E,V,X;
27   PetscRandom rand;
28 } AppCtx;
29 
30 /*
31      Defines the ODE passed to the ODE solver
32 */
33 static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *ctx)
34 {
35   PetscScalar        *f,r;
36   const PetscScalar  *u,*udot;
37   static PetscScalar R = .4;
38 
39   PetscFunctionBegin;
40   CHKERRQ(PetscRandomGetValue(ctx->rand,&r));
41   if (r > .9) R = .5;
42   if (r < .1) R = .4;
43   R = .4;
44   /*  The next three lines allow us to access the entries of the vectors directly */
45   CHKERRQ(VecGetArrayRead(U,&u));
46   CHKERRQ(VecGetArrayRead(Udot,&udot));
47   CHKERRQ(VecGetArray(F,&f));
48   f[0] = 2.0*ctx->H*udot[0]/ctx->omega_s + ctx->E*ctx->V*PetscSinScalar(u[1])/ctx->X - R;
49   f[1] = udot[1] - u[0] + ctx->omega_s;
50 
51   CHKERRQ(VecRestoreArrayRead(U,&u));
52   CHKERRQ(VecRestoreArrayRead(Udot,&udot));
53   CHKERRQ(VecRestoreArray(F,&f));
54   PetscFunctionReturn(0);
55 }
56 
57 /*
58      Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian.
59 */
60 static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat A,Mat B,AppCtx *ctx)
61 {
62   PetscInt          rowcol[] = {0,1};
63   PetscScalar       J[2][2];
64   const PetscScalar *u,*udot;
65 
66   PetscFunctionBegin;
67   CHKERRQ(VecGetArrayRead(U,&u));
68   CHKERRQ(VecGetArrayRead(Udot,&udot));
69   J[0][0] = 2.0*ctx->H*a/ctx->omega_s;   J[0][1] = -ctx->E*ctx->V*PetscCosScalar(u[1])/ctx->X;
70   J[1][0] = -1.0;                        J[1][1] = a;
71   CHKERRQ(MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES));
72   CHKERRQ(VecRestoreArrayRead(U,&u));
73   CHKERRQ(VecRestoreArrayRead(Udot,&udot));
74 
75   CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
76   CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
77   if (A != B) {
78     CHKERRQ(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
79     CHKERRQ(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
80   }
81   PetscFunctionReturn(0);
82 }
83 
84 int main(int argc,char **argv)
85 {
86   TS             ts;            /* ODE integrator */
87   Vec            U;             /* solution will be stored here */
88   Mat            A;             /* Jacobian matrix */
89   PetscErrorCode ierr;
90   PetscMPIInt    size;
91   PetscInt       n = 2;
92   AppCtx         ctx;
93   PetscScalar    *u;
94 
95   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96      Initialize program
97      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
99   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
100   PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Only for sequential runs");
101 
102   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103     Create necessary matrix and vectors
104     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
105   CHKERRQ(MatCreate(PETSC_COMM_WORLD,&A));
106   CHKERRQ(MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE));
107   CHKERRQ(MatSetFromOptions(A));
108   CHKERRQ(MatSetUp(A));
109 
110   CHKERRQ(MatCreateVecs(A,&U,NULL));
111 
112   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113     Set runtime options
114     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
115   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Reaction options","");CHKERRQ(ierr);
116   {
117     ctx.omega_s = 1.0;
118     CHKERRQ(PetscOptionsScalar("-omega_s","","",ctx.omega_s,&ctx.omega_s,NULL));
119     ctx.H       = 1.0;
120     CHKERRQ(PetscOptionsScalar("-H","","",ctx.H,&ctx.H,NULL));
121     ctx.E       = 1.0;
122     CHKERRQ(PetscOptionsScalar("-E","","",ctx.E,&ctx.E,NULL));
123     ctx.V       = 1.0;
124     CHKERRQ(PetscOptionsScalar("-V","","",ctx.V,&ctx.V,NULL));
125     ctx.X       = 1.0;
126     CHKERRQ(PetscOptionsScalar("-X","","",ctx.X,&ctx.X,NULL));
127 
128     CHKERRQ(VecGetArray(U,&u));
129     u[0] = 1;
130     u[1] = .7;
131     CHKERRQ(VecRestoreArray(U,&u));
132     CHKERRQ(PetscOptionsGetVec(NULL,NULL,"-initial",U,NULL));
133   }
134   ierr = PetscOptionsEnd();CHKERRQ(ierr);
135 
136   CHKERRQ(PetscRandomCreate(PETSC_COMM_WORLD,&ctx.rand));
137   CHKERRQ(PetscRandomSetFromOptions(ctx.rand));
138 
139   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
140      Create timestepping solver context
141      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
142   CHKERRQ(TSCreate(PETSC_COMM_WORLD,&ts));
143   CHKERRQ(TSSetProblemType(ts,TS_NONLINEAR));
144   CHKERRQ(TSSetType(ts,TSROSW));
145   CHKERRQ(TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&ctx));
146   CHKERRQ(TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&ctx));
147 
148   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149      Set initial conditions
150    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
151   CHKERRQ(TSSetSolution(ts,U));
152 
153   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154      Set solver options
155    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156   CHKERRQ(TSSetMaxTime(ts,2000.0));
157   CHKERRQ(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP));
158   CHKERRQ(TSSetTimeStep(ts,.001));
159   CHKERRQ(TSSetFromOptions(ts));
160 
161   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162      Solve nonlinear system
163      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
164   CHKERRQ(TSSolve(ts,U));
165 
166   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167      Free work space.  All PETSc objects should be destroyed when they are no longer needed.
168    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169   CHKERRQ(MatDestroy(&A));
170   CHKERRQ(VecDestroy(&U));
171   CHKERRQ(TSDestroy(&ts));
172   CHKERRQ(PetscRandomDestroy(&ctx.rand));
173   ierr = PetscFinalize();
174   return ierr;
175 }
176 
177 /*TEST
178 
179    build:
180      requires: !complex !single
181 
182    test:
183       args: -ksp_gmres_cgs_refinement_type refine_always -snes_type newtonls -ts_max_steps 10
184 
185    test:
186       suffix: 2
187       args: -ts_max_steps 10
188 
189 TEST*/
190