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: {
27: U = 0.4 * sin(200 * pi * t);
28: return 0;
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;
41: /* The next three lines allow us to access the entries of the vectors directly */
42: VecGetArrayRead(Y, &y);
43: VecGetArrayRead(Ydot, &ydot);
44: 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: VecRestoreArrayRead(Y, &y);
53: VecRestoreArrayRead(Ydot, &ydot);
54: VecRestoreArray(F, &f);
55: return 0;
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];
68: VecGetArrayRead(Y, &y);
69: VecGetArrayRead(Ydot, &ydot);
71: 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: MatSetValues(B, 5, rowcol, 5, rowcol, &J[0][0], INSERT_VALUES);
94: VecRestoreArrayRead(Y, &y);
95: VecRestoreArrayRead(Ydot, &ydot);
97: MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
98: MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
99: if (A != B) {
100: MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
101: MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
102: }
103: return 0;
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: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
119: PetscInitialize(&argc, &argv, (char *)0, help);
120: MPI_Comm_size(PETSC_COMM_WORLD, &size);
123: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124: Create necessary matrix and vectors
125: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
126: MatCreate(PETSC_COMM_WORLD, &A);
127: MatSetSizes(A, n, n, PETSC_DETERMINE, PETSC_DETERMINE);
128: MatSetFromOptions(A);
129: MatSetUp(A);
131: MatCreateVecs(A, &Y, NULL);
133: 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: VecRestoreArray(Y, &y);
141: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142: Create timestepping solver context
143: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144: TSCreate(PETSC_COMM_WORLD, &ts);
145: TSSetProblemType(ts, TS_NONLINEAR);
146: TSSetType(ts, TSARKIMEX);
147: TSSetEquationType(ts, TS_EQ_DAE_IMPLICIT_INDEX1);
148: TSARKIMEXSetFullyImplicit(ts, PETSC_TRUE);
149: /*TSSetType(ts,TSROSW);*/
150: TSSetIFunction(ts, NULL, IFunctionSemiExplicit, NULL);
151: TSSetIJacobian(ts, A, A, IJacobianSemiExplicit, NULL);
153: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154: Set initial conditions
155: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156: TSSetSolution(ts, Y);
158: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159: Set solver options
160: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161: TSSetMaxTime(ts, 0.15);
162: TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER);
163: TSSetTimeStep(ts, .001);
164: TSSetFromOptions(ts);
166: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167: Do Time stepping
168: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169: TSSolve(ts, Y);
171: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172: Free work space. All PETSc objects should be destroyed when they are no longer needed.
173: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
174: MatDestroy(&A);
175: VecDestroy(&Y);
176: TSDestroy(&ts);
177: PetscFinalize();
178: return 0;
179: }