Actual source code: ex36SE.c


  2: static char help[] = "Transistor amplifier (semi-explicit).\n";

  4: /*F
  5:   [I 0] [y'] = f(t,y,z)
  6:   [0 0] [z'] = g(t,y,z)
  7:   Useful options: -ts_monitor_lg_solution -ts_monitor_lg_timestep -lg_indicate_data_points 0
  8: F*/

 10: /*
 11:    Include "petscts.h" so that we can use TS solvers.  Note that this
 12:    file automatically includes:
 13:      petscsys.h       - base PETSc routines   petscvec.h - vectors
 14:      petscmat.h - matrices
 15:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 16:      petscviewer.h - viewers               petscpc.h  - preconditioners
 17:      petscksp.h   - linear solvers
 18: */
 19: #include <petscts.h>

 21: FILE *gfilepointer_data, *gfilepointer_info;

 23: /* Defines the source  */
 24: PetscErrorCode Ue(PetscScalar t, PetscScalar *U)
 25: {
 26:   PetscFunctionBeginUser;
 27:   U = 0.4 * sin(200 * pi * t);
 28:   PetscFunctionReturn(PETSC_SUCCESS);
 29: }
 30: * /

 32:   /*
 33:      Defines the DAE passed to the time solver
 34: */
 35:   static PetscErrorCode IFunctionSemiExplicit(TS ts, PetscReal t, Vec Y, Vec Ydot, Vec F, void *ctx)
 36: {
 37:   const PetscScalar *y, *ydot;
 38:   PetscScalar       *f;

 40:   PetscFunctionBeginUser;
 41:   /*  The next three lines allow us to access the entries of the vectors directly */
 42:   PetscCall(VecGetArrayRead(Y, &y));
 43:   PetscCall(VecGetArrayRead(Ydot, &ydot));
 44:   PetscCall(VecGetArray(F, &f));

 46:   f[0] = -400 * PetscSinReal(200 * PETSC_PI * t) + 1000 * y[3] + ydot[0];
 47:   f[1] = 0.5 - 1 / (2. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.)) + (500 * y[1]) / 9. + ydot[1];
 48:   f[2] = -222.5522222222222 + 33 / (100. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.)) + (1000 * y[4]) / 27. + ydot[2];
 49:   f[3] = 0.0006666766666666667 - 1 / (1.e8 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.)) + PetscSinReal(200 * PETSC_PI * t) / 2500. + y[0] / 4500. - (11 * y[3]) / 9000.;
 50:   f[4] = 0.0006676566666666666 - 99 / (1.e8 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.)) + y[2] / 9000. - y[4] / 4500.;

 52:   PetscCall(VecRestoreArrayRead(Y, &y));
 53:   PetscCall(VecRestoreArrayRead(Ydot, &ydot));
 54:   PetscCall(VecRestoreArray(F, &f));
 55:   PetscFunctionReturn(PETSC_SUCCESS);
 56: }

 58: /*
 59:      Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian.
 60: */
 61: static PetscErrorCode IJacobianSemiExplicit(TS ts, PetscReal t, Vec Y, Vec Ydot, PetscReal a, Mat A, Mat B, void *ctx)
 62: {
 63:   PetscInt           rowcol[] = {0, 1, 2, 3, 4};
 64:   const PetscScalar *y, *ydot;
 65:   PetscScalar        J[5][5];

 67:   PetscFunctionBeginUser;
 68:   PetscCall(VecGetArrayRead(Y, &y));
 69:   PetscCall(VecGetArrayRead(Ydot, &ydot));

 71:   PetscCall(PetscMemzero(J, sizeof(J)));

 73:   J[0][0] = a;
 74:   J[0][3] = 1000;
 75:   J[1][0] = 250 / (13. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 76:   J[1][1] = 55.55555555555556 + a + 250 / (13. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 77:   J[1][3] = -250 / (13. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 78:   J[2][0] = -165 / (13. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 79:   J[2][1] = -165 / (13. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 80:   J[2][2] = a;
 81:   J[2][3] = 165 / (13. * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 82:   J[2][4] = 37.03703703703704;
 83:   J[3][0] = 0.00022222222222222223 + 1 / (2.6e6 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 84:   J[3][1] = 1 / (2.6e6 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 85:   J[3][3] = -0.0012222222222222222 - 1 / (2.6e6 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 86:   J[4][0] = 99 / (2.6e6 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 87:   J[4][1] = 99 / (2.6e6 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 88:   J[4][2] = 0.00011111111111111112;
 89:   J[4][3] = -99 / (2.6e6 * PetscExpReal((500 * (y[0] + y[1] - y[3])) / 13.));
 90:   J[4][4] = -0.00022222222222222223;

 92:   PetscCall(MatSetValues(B, 5, rowcol, 5, rowcol, &J[0][0], INSERT_VALUES));

 94:   PetscCall(VecRestoreArrayRead(Y, &y));
 95:   PetscCall(VecRestoreArrayRead(Ydot, &ydot));

 97:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 98:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
 99:   if (A != B) {
100:     PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
101:     PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
102:   }
103:   PetscFunctionReturn(PETSC_SUCCESS);
104: }

106: int main(int argc, char **argv)
107: {
108:   TS           ts; /* ODE integrator */
109:   Vec          Y;  /* solution will be stored here */
110:   Mat          A;  /* Jacobian matrix */
111:   PetscMPIInt  size;
112:   PetscInt     n = 5;
113:   PetscScalar *y;

115:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116:      Initialize program
117:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
118:   PetscFunctionBeginUser;
119:   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
120:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
121:   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Only for sequential runs");

123:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124:     Create necessary matrix and vectors
125:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
126:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
127:   PetscCall(MatSetSizes(A, n, n, PETSC_DETERMINE, PETSC_DETERMINE));
128:   PetscCall(MatSetFromOptions(A));
129:   PetscCall(MatSetUp(A));

131:   PetscCall(MatCreateVecs(A, &Y, NULL));

133:   PetscCall(VecGetArray(Y, &y));
134:   y[0] = -3.0;
135:   y[1] = 3.0;
136:   y[2] = 6.0;
137:   y[3] = 0.0;
138:   y[4] = 6.0;
139:   PetscCall(VecRestoreArray(Y, &y));

141:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142:      Create timestepping solver context
143:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144:   PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
145:   PetscCall(TSSetProblemType(ts, TS_NONLINEAR));
146:   PetscCall(TSSetType(ts, TSARKIMEX));
147:   PetscCall(TSSetEquationType(ts, TS_EQ_DAE_IMPLICIT_INDEX1));
148:   PetscCall(TSARKIMEXSetFullyImplicit(ts, PETSC_TRUE));
149:   /*PetscCall(TSSetType(ts,TSROSW));*/
150:   PetscCall(TSSetIFunction(ts, NULL, IFunctionSemiExplicit, NULL));
151:   PetscCall(TSSetIJacobian(ts, A, A, IJacobianSemiExplicit, NULL));

153:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154:      Set initial conditions
155:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156:   PetscCall(TSSetSolution(ts, Y));

158:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159:      Set solver options
160:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161:   PetscCall(TSSetMaxTime(ts, 0.15));
162:   PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER));
163:   PetscCall(TSSetTimeStep(ts, .001));
164:   PetscCall(TSSetFromOptions(ts));

166:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167:      Do Time stepping
168:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169:   PetscCall(TSSolve(ts, Y));

171:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172:      Free work space.  All PETSc objects should be destroyed when they are no longer needed.
173:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
174:   PetscCall(MatDestroy(&A));
175:   PetscCall(VecDestroy(&Y));
176:   PetscCall(TSDestroy(&ts));
177:   PetscCall(PetscFinalize());
178:   return 0;
179: }